FFmpeg  4.4
speedhqenc.c
Go to the documentation of this file.
1 /*
2  * SpeedHQ encoder
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
5  * Copyright (c) 2003-2004 Michael Niedermayer
6  * Copyright (c) 2020 FFmpeg
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 /**
26  * @file
27  * SpeedHQ encoder.
28  */
29 
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/thread.h"
32 
33 #include "avcodec.h"
34 #include "mpeg12.h"
35 #include "mpegvideo.h"
36 #include "speedhqenc.h"
37 
38 extern RLTable ff_rl_speedhq;
40 
41 static uint16_t mpeg12_vlc_dc_lum_code_reversed[12];
43 
44 /* simple include everything table for dc, first byte is bits
45  * number next 3 are code */
46 static uint32_t speedhq_lum_dc_uni[512];
47 static uint32_t speedhq_chr_dc_uni[512];
48 
49 static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
50 
51 static uint32_t reverse(uint32_t num, int bits)
52 {
53  return bitswap_32(num) >> (32 - bits);
54 }
55 
56 static void reverse_code(const uint16_t *code, const uint8_t *bits,
57  uint16_t *reversed_code, int num_entries)
58 {
59  for (int i = 0; i < num_entries; i++)
60  reversed_code[i] = reverse(code[i], bits[i]);
61 }
62 
64 {
65  /* Exactly the same as MPEG-2, except little-endian. */
69  12);
73  12);
74 
76 
77  /* build unified dc encoding tables */
78  for (int i = -255; i < 256; i++) {
79  int adiff, index;
80  int bits, code;
81  int diff = i;
82 
83  adiff = FFABS(diff);
84  if (diff < 0)
85  diff--;
86  index = av_log2(2 * adiff);
87 
91  speedhq_lum_dc_uni[i + 255] = bits + (code << 8);
92 
96  speedhq_chr_dc_uni[i + 255] = bits + (code << 8);
97  }
98 
100 }
101 
103 {
104  static AVOnce init_static_once = AV_ONCE_INIT;
105 
106  av_assert0(s->slice_context_count == 1);
107 
108  if (s->width > 65500 || s->height > 65500) {
109  av_log(s, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
110  return AVERROR(EINVAL);
111  }
112 
113  s->min_qcoeff = -2048;
114  s->max_qcoeff = 2047;
115 
116  ff_thread_once(&init_static_once, speedhq_init_static_data);
117 
118  s->intra_ac_vlc_length =
119  s->intra_ac_vlc_last_length =
120  s->intra_chroma_ac_vlc_length =
121  s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
122 
123  switch (s->avctx->pix_fmt) {
124  case AV_PIX_FMT_YUV420P:
125  s->avctx->codec_tag = MKTAG('S','H','Q','0');
126  break;
127  case AV_PIX_FMT_YUV422P:
128  s->avctx->codec_tag = MKTAG('S','H','Q','2');
129  break;
130  case AV_PIX_FMT_YUV444P:
131  s->avctx->codec_tag = MKTAG('S','H','Q','4');
132  break;
133  default:
134  av_assert0(0);
135  }
136 
137  return 0;
138 }
139 
141 {
142  put_bits_le(&s->pb, 8, 100 - s->qscale * 2); /* FIXME why doubled */
143  put_bits_le(&s->pb, 24, 4); /* no second field */
144 
145  /* length of first slice, will be filled out later */
146  s->slice_start = 4;
147  put_bits_le(&s->pb, 24, 0);
148 }
149 
151 {
152  int slice_len;
153 
154  flush_put_bits_le(&s->pb);
155  slice_len = s->pb.buf_ptr - (s->pb.buf + s->slice_start);
156  AV_WL24(s->pb.buf + s->slice_start, slice_len);
157 
158  /* length of next slice, will be filled out later */
159  s->slice_start = s->pb.buf_ptr - s->pb.buf;
160  put_bits_le(&s->pb, 24, 0);
161 }
162 
163 static inline void encode_dc(PutBitContext *pb, int diff, int component)
164 {
165  unsigned int diff_u = diff + 255;
166  if (diff_u >= 511) {
167  int index;
168 
169  if (diff < 0) {
170  index = av_log2_16bit(-2 * diff);
171  diff--;
172  } else {
173  index = av_log2_16bit(2 * diff);
174  }
175  if (component == 0)
176  put_bits_le(pb,
180  else
181  put_bits_le(pb,
185  } else {
186  if (component == 0)
187  put_bits_le(pb,
188  speedhq_lum_dc_uni[diff + 255] & 0xFF,
189  speedhq_lum_dc_uni[diff + 255] >> 8);
190  else
191  put_bits_le(pb,
192  speedhq_chr_dc_uni[diff + 255] & 0xFF,
193  speedhq_chr_dc_uni[diff + 255] >> 8);
194  }
195 }
196 
197 static void encode_block(MpegEncContext *s, int16_t *block, int n)
198 {
199  int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
200  int code;
201  int component, val;
202 
203  /* DC coef */
204  component = (n <= 3 ? 0 : (n&1) + 1);
205  dc = block[0]; /* overflow is impossible */
206  val = s->last_dc[component] - dc; /* opposite of most codecs */
207  encode_dc(&s->pb, val, component);
208  s->last_dc[component] = dc;
209 
210  /* now quantify & encode AC coefs */
211  last_non_zero = 0;
212  last_index = s->block_last_index[n];
213 
214  for (i = 1; i <= last_index; i++) {
215  j = s->intra_scantable.permutated[i];
216  level = block[j];
217 
218  /* encode using VLC */
219  if (level != 0) {
220  run = i - last_non_zero - 1;
221 
222  alevel = level;
223  MASK_ABS(sign, alevel);
224  sign &= 1;
225 
226  if (alevel <= ff_rl_speedhq.max_level[0][run]) {
227  code = ff_rl_speedhq.index_run[0][run] + alevel - 1;
228  /* store the VLC & sign at once */
229  put_bits_le(&s->pb, ff_rl_speedhq.table_vlc[code][1] + 1,
231  } else {
232  /* escape seems to be pretty rare <5% so I do not optimize it */
234  /* escape: only clip in this case */
235  put_bits_le(&s->pb, 6, run);
236  put_bits_le(&s->pb, 12, level + 2048);
237  }
238  last_non_zero = i;
239  }
240  }
241  /* end of block */
243 }
244 
246 {
247  int i;
248  for(i=0;i<6;i++) {
249  encode_block(s, block[i], i);
250  }
251  if (s->chroma_format == CHROMA_444) {
252  encode_block(s, block[8], 8);
253  encode_block(s, block[9], 9);
254 
255  encode_block(s, block[6], 6);
256  encode_block(s, block[7], 7);
257 
258  encode_block(s, block[10], 10);
259  encode_block(s, block[11], 11);
260  } else if (s->chroma_format == CHROMA_422) {
261  encode_block(s, block[6], 6);
262  encode_block(s, block[7], 7);
263  }
264 
265  s->i_tex_bits += get_bits_diff(s);
266 }
267 
268 static int ff_speedhq_mb_rows_in_slice(int slice_num, int mb_height)
269 {
270  return mb_height / 4 + (slice_num < (mb_height % 4));
271 }
272 
273 int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
274 {
275  int slice_num = 0;
276  while (mb_y_order >= ff_speedhq_mb_rows_in_slice(slice_num, mb_height)) {
277  mb_y_order -= ff_speedhq_mb_rows_in_slice(slice_num, mb_height);
278  slice_num++;
279  }
280  *first_in_slice = (mb_y_order == 0);
281  return mb_y_order * 4 + slice_num;
282 }
283 
284 #if CONFIG_SPEEDHQ_ENCODER
285 static const AVClass speedhq_class = {
286  .class_name = "speedhq encoder",
287  .item_name = av_default_item_name,
288  .option = ff_mpv_generic_options,
289  .version = LIBAVUTIL_VERSION_INT,
290 };
291 
293  .name = "speedhq",
294  .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
295  .type = AVMEDIA_TYPE_VIDEO,
296  .id = AV_CODEC_ID_SPEEDHQ,
297  .priv_data_size = sizeof(MpegEncContext),
299  .encode2 = ff_mpv_encode_picture,
300  .close = ff_mpv_encode_end,
301  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
302  .pix_fmts = (const enum AVPixelFormat[]) {
305  },
306  .priv_class = &speedhq_class,
307 };
308 #endif
static double val(void *priv, double ch)
Definition: aeval.c:76
AVCodec ff_speedhq_encoder
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> dc
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define av_mod_uintp2
Definition: common.h:149
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
@ AV_CODEC_ID_SPEEDHQ
Definition: codec_id.h:274
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int index
Definition: gxfenc.c:89
int i
Definition: input.c:407
#define av_log2
Definition: intmath.h:83
#define av_log2_16bit
Definition: intmath.h:84
#define AV_WL24(p, d)
Definition: intreadwrite.h:464
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define AVOnce
Definition: thread.h:172
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
#define AV_ONCE_INIT
Definition: thread.h:173
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
#define MASK_ABS(mask, level)
Definition: mathops.h:155
static av_always_inline uint32_t bitswap_32(uint32_t x)
Definition: mathops.h:243
void ff_mpeg1_init_uni_ac_vlc(const RLTable *rl, uint8_t *uni_ac_vlc_len)
Definition: mpeg12enc.c:68
const uint16_t ff_mpeg12_vlc_dc_chroma_code[12]
Definition: mpeg12data.c:59
const uint16_t ff_mpeg12_vlc_dc_lum_code[12]
Definition: mpeg12data.c:52
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition: mpeg12data.c:62
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition: mpeg12data.c:55
mpegvideo header.
const AVOption ff_mpv_generic_options[]
Definition: mpegvideo_enc.c:87
int ff_mpv_encode_end(AVCodecContext *avctx)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:765
#define CHROMA_444
Definition: mpegvideo.h:490
#define CHROMA_422
Definition: mpegvideo.h:489
int ff_mpv_encode_init(AVCodecContext *avctx)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
static void put_bits_le(PutBitContext *s, int n, BitBuf value)
Definition: put_bits.h:225
static void flush_put_bits_le(PutBitContext *s)
Definition: put_bits.h:131
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: rl.c:28
#define MAX_LEVEL
Definition: rl.h:36
#define MAX_RUN
Definition: rl.h:35
const uint8_t * code
Definition: spdifenc.c:413
void ff_speedhq_encode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: speedhqenc.c:245
static int ff_speedhq_mb_rows_in_slice(int slice_num, int mb_height)
Definition: speedhqenc.c:268
void ff_speedhq_end_slice(MpegEncContext *s)
Definition: speedhqenc.c:150
static uint32_t speedhq_lum_dc_uni[512]
Definition: speedhqenc.c:46
static uint16_t mpeg12_vlc_dc_chroma_code_reversed[12]
Definition: speedhqenc.c:42
av_cold int ff_speedhq_encode_init(MpegEncContext *s)
Definition: speedhqenc.c:102
static uint16_t mpeg12_vlc_dc_lum_code_reversed[12]
Definition: speedhqenc.c:41
static void encode_dc(PutBitContext *pb, int diff, int component)
Definition: speedhqenc.c:163
static void encode_block(MpegEncContext *s, int16_t *block, int n)
Definition: speedhqenc.c:197
RLTable ff_rl_speedhq
Definition: speedhq.c:137
static void reverse_code(const uint16_t *code, const uint8_t *bits, uint16_t *reversed_code, int num_entries)
Definition: speedhqenc.c:56
static av_cold void speedhq_init_static_data(void)
Definition: speedhqenc.c:63
int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
Definition: speedhqenc.c:273
static uint8_t speedhq_static_rl_table_store[2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: speedhqenc.c:39
void ff_speedhq_encode_picture_header(MpegEncContext *s)
Definition: speedhqenc.c:140
static uint8_t uni_speedhq_ac_vlc_len[64 *64 *2]
Definition: speedhqenc.c:49
static uint32_t reverse(uint32_t num, int bits)
Definition: speedhqenc.c:51
static uint32_t speedhq_chr_dc_uni[512]
Definition: speedhqenc.c:47
SpeedHQ encoder.
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
MpegEncContext.
Definition: mpegvideo.h:81
RLTable.
Definition: rl.h:39
uint8_t * index_run[2]
encoding only
Definition: rl.h:45
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
uint8_t run
Definition: svq3.c:205
uint8_t level
Definition: svq3.c:206
#define av_log(a,...)
static int16_t block[64]
Definition: dct.c:116
static av_always_inline int diff(const uint32_t a, const uint32_t b)
uint8_t bits
Definition: vp3data.h:141