FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ituh263enc.c
Go to the documentation of this file.
1 /*
2  * ITU H.263 bitstream encoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * H.263+ support.
5  * Copyright (c) 2001 Juan J. Sierralta P
6  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
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  * H.263 bitstream encoder.
28  */
29 
30 #include "config_components.h"
31 
32 #include <limits.h>
33 #include <string.h>
34 
35 #include "libavutil/attributes.h"
36 #include "libavutil/thread.h"
37 #include "avcodec.h"
38 #include "codec_internal.h"
39 #include "mpegvideo.h"
40 #include "flvenc.h"
41 #include "mpegvideoenc.h"
42 #include "h263.h"
43 #include "h263enc.h"
44 #include "h263data.h"
45 #include "h263dsp.h"
46 #include "mathops.h"
47 #include "mpegutils.h"
48 #include "internal.h"
49 #include "put_bits.h"
50 
51 /**
52  * Table of number of bits a motion vector component needs.
53  */
54 static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV*2+1];
55 
56 static av_cold void init_mv_penalty(void)
57 {
58  for (int f_code = 1; f_code <= MAX_FCODE; f_code++) {
59  for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
60  int len;
61 
62  if (mv == 0) len = 1; // ff_mvtab[0][1]
63  else {
64  int val, bit_size, code;
65 
66  bit_size = f_code - 1;
67 
68  val = mv;
69  if (val < 0)
70  val = -val;
71  val--;
72  code = (val >> bit_size) + 1;
73  if (code < 33) {
74  len = ff_mvtab[code][1] + 1 + bit_size;
75  } else {
76  len = 12 /* ff_mvtab[32][1] */ + av_log2(code>>5) + 2 + bit_size;
77  }
78  }
79 
80  mv_penalty[f_code][mv + MAX_DMV] = len;
81  }
82  }
83 }
84 
85 #if CONFIG_H263_ENCODER
86 /**
87  * Minimal fcode that a motion vector component would need in umv.
88  * All entries in this table are 1.
89  */
90 static uint8_t umv_fcode_tab[MAX_MV*2+1];
91 
92 //unified encoding tables for run length encoding of coefficients
93 //unified in the sense that the specification specifies the encoding in several steps.
94 static uint8_t uni_h263_intra_aic_rl_len [64*64*2*2];
95 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
96 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
97 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
98 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
99 
100 static av_cold void init_uni_h263_rl_tab(const RLTable *rl, uint8_t *len_tab)
101 {
102  const uint16_t (*table_vlc)[2] = rl->table_vlc;
103  const uint8_t *table_run = rl->table_run;
104  const uint8_t *table_level = rl->table_level;
105 
106  av_assert0(MAX_LEVEL >= 64);
107  av_assert0(MAX_RUN >= 63);
108 
109  // Note: The LUT only covers level values for which the escape value
110  // is eight bits (not 8 + 5 + 6)
111  memset(len_tab, H263_ESCAPE_CODE_LENGTH + 1 + 6 + 8,
112  sizeof(uni_h263_intra_aic_rl_len));
113 
114  len_tab += 64; // simplifies addressing
115  for (int i = 0; i < H263_RL_NB_ELEMS; ++i) {
116  int run = table_run[i];
117  int level = table_level[i];
118  int last = i >= H263_RL_NON_LAST_CODES;
119  int len = table_vlc[i][1];
120 
121  len_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] =
122  len_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = len + 1 /* sign */;
123  }
124  for (int run = 0; run < MAX_RUN; ++run) {
125  len_tab[UNI_MPEG4_ENC_INDEX(0, run, 0)] =
126  len_tab[UNI_MPEG4_ENC_INDEX(1, run, 0)] = 0; // is this necessary?
127  }
128 }
129 #endif
130 
132 {
133 #if CONFIG_H263_ENCODER
134  static uint8_t rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
135  ff_rl_init(&ff_rl_intra_aic, rl_intra_table);
137 
138  init_uni_h263_rl_tab(&ff_rl_intra_aic, uni_h263_intra_aic_rl_len);
139  init_uni_h263_rl_tab(&ff_h263_rl_inter, uni_h263_inter_rl_len);
140 
141  memset(umv_fcode_tab, 1, sizeof(umv_fcode_tab));
142 #endif
143 
144  init_mv_penalty();
145 }
146 
147 av_cold const uint8_t (*ff_h263_get_mv_penalty(void))[MAX_DMV*2+1]
148 {
149  static AVOnce init_static_once = AV_ONCE_INIT;
150 
151  ff_thread_once(&init_static_once, h263_encode_init_static);
152 
153  return mv_penalty;
154 }
155 
156 void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
157 {
158  if (val == 0) {
159  /* zero vector -- corresponds to ff_mvtab[0] */
160  put_bits(pb, 1, 1);
161  } else {
162  int sign, code, bits;
163  int bit_size = f_code - 1;
164  int range = 1 << bit_size;
165  /* modulo encoding */
166  val = sign_extend(val, 6 + bit_size);
167  sign = val>>31;
168  val= (val^sign)-sign;
169  sign&=1;
170 
171  val--;
172  code = (val >> bit_size) + 1;
173  bits = val & (range - 1);
174 
175  put_bits(pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
176  if (bit_size > 0) {
177  put_bits(pb, bit_size, bits);
178  }
179  }
180 }
181 
182 #if CONFIG_H263_ENCODER // Snow and SVQ1 need the above
183 static const uint8_t wrong_run[102] = {
184  1, 2, 3, 5, 4, 10, 9, 8,
185 11, 15, 17, 16, 23, 22, 21, 20,
186 19, 18, 25, 24, 27, 26, 11, 7,
187  6, 1, 2, 13, 2, 2, 2, 2,
188  6, 12, 3, 9, 1, 3, 4, 3,
189  7, 4, 1, 1, 5, 5, 14, 6,
190  1, 7, 1, 8, 1, 1, 1, 1,
191 10, 1, 1, 5, 9, 17, 25, 24,
192 29, 33, 32, 41, 2, 23, 28, 31,
193  3, 22, 30, 4, 27, 40, 8, 26,
194  6, 39, 7, 38, 16, 37, 15, 10,
195 11, 12, 13, 14, 1, 21, 20, 18,
196 19, 2, 1, 34, 35, 36
197 };
198 
199 /**
200  * Return the 4 bit value that specifies the given aspect ratio.
201  * This may be one of the standard aspect ratios or it specifies
202  * that the aspect will be stored explicitly later.
203  */
205  int i;
206 
207  if(aspect.num==0 || aspect.den==0) aspect= (AVRational){1,1};
208 
209  for(i=1; i<6; i++){
210  if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
211  return i;
212  }
213  }
214 
215  return FF_ASPECT_EXTENDED;
216 }
217 
218 static int h263_encode_picture_header(MPVMainEncContext *const m)
219 {
220  MPVEncContext *const s = &m->s;
221  int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
222  int best_clock_code=1;
223  int best_divisor=60;
224  int best_error= INT_MAX;
225  int custom_pcf;
226 
228 
229  if (s->c.codec_id == AV_CODEC_ID_H263P) {
230  for(i=0; i<2; i++){
231  int div, error;
232  div= (s->c.avctx->time_base.num*1800000LL + 500LL*s->c.avctx->time_base.den) / ((1000LL+i)*s->c.avctx->time_base.den);
233  div= av_clip(div, 1, 127);
234  error= FFABS(s->c.avctx->time_base.num*1800000LL - (1000LL+i)*s->c.avctx->time_base.den*div);
235  if(error < best_error){
236  best_error= error;
237  best_divisor= div;
238  best_clock_code= i;
239  }
240  }
241  }
242  custom_pcf = best_clock_code != 1 || best_divisor != 60;
243  coded_frame_rate= 1800000;
244  coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
245 
246  put_bits(&s->pb, 22, 0x20); /* PSC */
247  temp_ref = s->picture_number * (int64_t)coded_frame_rate * s->c.avctx->time_base.num / //FIXME use timestamp
248  (coded_frame_rate_base * (int64_t)s->c.avctx->time_base.den);
249  put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
250 
251  put_bits(&s->pb, 1, 1); /* marker */
252  put_bits(&s->pb, 1, 0); /* H.263 id */
253  put_bits(&s->pb, 1, 0); /* split screen off */
254  put_bits(&s->pb, 1, 0); /* camera off */
255  put_bits(&s->pb, 1, 0); /* freeze picture release off */
256 
258  if (s->c.codec_id != AV_CODEC_ID_H263P) {
259  /* H.263v1 */
260  put_bits(&s->pb, 3, format);
261  put_bits(&s->pb, 1, (s->c.pict_type == AV_PICTURE_TYPE_P));
262  /* By now UMV IS DISABLED ON H.263v1, since the restrictions
263  of H.263v1 UMV implies to check the predicted MV after
264  calculation of the current MB to see if we're on the limits */
265  put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */
266  put_bits(&s->pb, 1, 0); /* SAC: off */
267  put_bits(&s->pb, 1, s->c.obmc); /* Advanced Prediction */
268  put_bits(&s->pb, 1, 0); /* only I/P-frames, no PB-frame */
269  put_bits(&s->pb, 5, s->c.qscale);
270  put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
271  } else {
272  int ufep=1;
273  /* H.263v2 */
274  /* H.263 Plus PTYPE */
275 
276  put_bits(&s->pb, 3, 7);
277  put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
278  if (format == 8)
279  put_bits(&s->pb,3,6); /* Custom Source Format */
280  else
281  put_bits(&s->pb, 3, format);
282 
283  put_bits(&s->pb,1, custom_pcf);
284  put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
285  put_bits(&s->pb,1,0); /* SAC: off */
286  put_bits(&s->pb,1,s->c.obmc); /* Advanced Prediction Mode */
287  put_bits(&s->pb,1,s->c.h263_aic); /* Advanced Intra Coding */
288  put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
289  put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
290  put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
291  put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
292  put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
293  put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
294  put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
295  put_bits(&s->pb,3,0); /* Reserved */
296 
297  put_bits(&s->pb, 3, s->c.pict_type == AV_PICTURE_TYPE_P);
298 
299  put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
300  put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
301  put_bits(&s->pb,1,s->c.no_rounding); /* Rounding Type */
302  put_bits(&s->pb,2,0); /* Reserved */
303  put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
304 
305  /* This should be here if PLUSPTYPE */
306  put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
307 
308  if (format == 8) {
309  /* Custom Picture Format (CPFMT) */
310  unsigned aspect_ratio_info = ff_h263_aspect_to_info(s->c.avctx->sample_aspect_ratio);
311 
312  put_bits(&s->pb,4, aspect_ratio_info);
313  put_bits(&s->pb,9,(s->c.width >> 2) - 1);
314  put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
315  put_bits(&s->pb,9,(s->c.height >> 2));
316  if (aspect_ratio_info == FF_ASPECT_EXTENDED){
317  put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.num);
318  put_bits(&s->pb, 8, s->c.avctx->sample_aspect_ratio.den);
319  }
320  }
321  if (custom_pcf) {
322  if(ufep){
323  put_bits(&s->pb, 1, best_clock_code);
324  put_bits(&s->pb, 7, best_divisor);
325  }
326  put_sbits(&s->pb, 2, temp_ref>>8);
327  }
328 
329  /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
330  if (s->umvplus)
331 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
332 //FIXME check actual requested range
333  put_bits(&s->pb,2,1); /* unlimited */
334  if (s->h263_slice_structured)
335  put_bits(&s->pb,2,0); /* no weird submodes */
336 
337  put_bits(&s->pb, 5, s->c.qscale);
338  }
339 
340  put_bits(&s->pb, 1, 0); /* no PEI */
341 
342  if (s->h263_slice_structured) {
343  put_bits(&s->pb, 1, 1);
344 
345  av_assert1(s->c.mb_x == 0 && s->c.mb_y == 0);
347 
348  put_bits(&s->pb, 1, 1);
349  }
350 
351  return 0;
352 }
353 
355 {
356  int16_t *dc = s->c.dc_val;
357 
358  // The "- 1" is for the top-left entry
359  const int l_xy = s->c.block_index[2];
360  for (int i = l_xy - 2 * s->c.b8_stride - 1; i < l_xy; i += 2)
361  AV_WN32A(dc + i, 1024 << 16 | 1024);
362 
363  const int u_xy = s->c.block_index[4];
364  const int v_xy = s->c.block_index[5];
365  int16_t *dc2 = dc + v_xy - u_xy;
366  for (int i = u_xy - s->c.mb_stride - 1; i < u_xy; ++i)
367  dc[i] = dc2[i] = 1024;
368 }
369 
370 /**
371  * Encode a group of blocks header.
372  */
373 void ff_h263_encode_gob_header(MPVEncContext *const s, int mb_line)
374 {
375  put_bits(&s->pb, 17, 1); /* GBSC */
376 
377  if (s->h263_slice_structured) {
378  put_bits(&s->pb, 1, 1);
379 
381 
382  if(s->c.mb_num > 1583)
383  put_bits(&s->pb, 1, 1);
384  put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */
385  put_bits(&s->pb, 1, 1);
386  put_bits(&s->pb, 2, s->c.pict_type == AV_PICTURE_TYPE_I); /* GFID */
387  }else{
388  int gob_number = mb_line / s->gob_index;
389 
390  put_bits(&s->pb, 5, gob_number); /* GN */
391  put_bits(&s->pb, 2, s->c.pict_type == AV_PICTURE_TYPE_I); /* GFID */
392  put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */
393  }
394 }
395 
396 /**
397  * modify qscale so that encoding is actually possible in H.263 (limit difference to -2..2)
398  */
400 {
401  int8_t * const qscale_table = s->c.cur_pic.qscale_table;
402 
403  for (int i = 1; i < s->c.mb_num; i++) {
404  if (qscale_table[ s->c.mb_index2xy[i] ] - qscale_table[ s->c.mb_index2xy[i-1] ] > 2)
405  qscale_table[ s->c.mb_index2xy[i] ] = qscale_table[ s->c.mb_index2xy[i-1] ] + 2;
406  }
407  for(int i = s->c.mb_num - 2; i >= 0; i--) {
408  if (qscale_table[ s->c.mb_index2xy[i] ] - qscale_table[ s->c.mb_index2xy[i+1] ] > 2)
409  qscale_table[ s->c.mb_index2xy[i] ] = qscale_table[ s->c.mb_index2xy[i+1] ] + 2;
410  }
411 
412  if (s->c.codec_id != AV_CODEC_ID_H263P) {
413  for (int i = 1; i < s->c.mb_num; i++) {
414  int mb_xy = s->c.mb_index2xy[i];
415 
416  if (qscale_table[mb_xy] != qscale_table[s->c.mb_index2xy[i - 1]] &&
417  (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTER4V)) {
418  s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
419  }
420  }
421  }
422 }
423 
424 static const int dquant_code[5]= {1,0,9,2,3};
425 
426 static void flv2_encode_ac_esc(PutBitContext *pb, int slevel, int level,
427  int run, int last)
428 {
429  unsigned code;
430  int bits;
431  if (level < 64) { // 7-bit level
432  bits = 1 + 1 + 6 + 7;
433  code = (0 << (1 + 6 + 7)) |
434  (last << (6 + 7)) |
435  (run << 7) |
436  (slevel & 0x7f);
437  } else {
438  /* 11-bit level */
439  bits = 1 + 1 + 6 + 11;
440  code = (1 << (1 + 6 + 11)) |
441  (last << (6 + 11)) |
442  (run << 11) |
443  (slevel & 0x7ff);
444  }
445  put_bits(pb, bits, code);
446 }
447 
448 /**
449  * Encode an 8x8 block.
450  * @param block the 8x8 block
451  * @param n block index (0-3 are luma, 4-5 are chroma)
452  */
453 static void h263_encode_block(MPVEncContext *const s, int16_t block[], int n)
454 {
455  int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
456  const RLTable *rl;
457 
458  rl = &ff_h263_rl_inter;
459  if (s->c.mb_intra && !s->c.h263_aic) {
460  /* DC coef */
461  level = block[0];
462  /* 255 cannot be represented, so we clamp */
463  if (level > 254) {
464  level = 254;
465  block[0] = 254;
466  }
467  /* 0 cannot be represented also */
468  else if (level < 1) {
469  level = 1;
470  block[0] = 1;
471  }
472  if (level == 128) //FIXME check rv10
473  put_bits(&s->pb, 8, 0xff);
474  else
475  put_bits(&s->pb, 8, level);
476  i = 1;
477  } else {
478  i = 0;
479  if (s->c.h263_aic && s->c.mb_intra)
480  rl = &ff_rl_intra_aic;
481 
482  if (s->alt_inter_vlc && !s->c.mb_intra) {
483  int aic_vlc_bits=0;
484  int inter_vlc_bits=0;
485  int wrong_pos=-1;
486  int aic_code;
487 
488  last_index = s->c.block_last_index[n];
489  last_non_zero = i - 1;
490  for (; i <= last_index; i++) {
491  j = s->c.intra_scantable.permutated[i];
492  level = block[j];
493  if (level) {
494  run = i - last_non_zero - 1;
495  last = (i == last_index);
496 
497  if(level<0) level= -level;
498 
499  code = get_rl_index(rl, last, run, level);
500  aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level);
501  inter_vlc_bits += rl->table_vlc[code][1]+1;
502  aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1;
503 
504  if (code == rl->n) {
505  inter_vlc_bits += 1+6+8-1;
506  }
507  if (aic_code == ff_rl_intra_aic.n) {
508  aic_vlc_bits += 1+6+8-1;
509  wrong_pos += run + 1;
510  }else
511  wrong_pos += wrong_run[aic_code];
512  last_non_zero = i;
513  }
514  }
515  i = 0;
516  if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
517  rl = &ff_rl_intra_aic;
518  }
519  }
520 
521  /* AC coefs */
522  last_index = s->c.block_last_index[n];
523  last_non_zero = i - 1;
524  for (; i <= last_index; i++) {
525  j = s->c.intra_scantable.permutated[i];
526  level = block[j];
527  if (level) {
528  run = i - last_non_zero - 1;
529  last = (i == last_index);
530  sign = 0;
531  slevel = level;
532  if (level < 0) {
533  sign = 1;
534  level = -level;
535  }
536  code = get_rl_index(rl, last, run, level);
537  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
538  if (code == rl->n) {
539  if (!CONFIG_FLV_ENCODER || s->c.codec_id != AV_CODEC_ID_FLV1) {
540  put_bits(&s->pb, 1, last);
541  put_bits(&s->pb, 6, run);
542 
543  av_assert2(slevel != 0);
544 
545  if (level < 128) {
546  put_sbits(&s->pb, 8, slevel);
547  } else {
548  put_bits(&s->pb, 8, 128);
549  put_sbits(&s->pb, 5, slevel);
550  put_sbits(&s->pb, 6, slevel>>5);
551  }
552  } else {
553  flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
554  }
555  } else {
556  put_bits(&s->pb, 1, sign);
557  }
558  last_non_zero = i;
559  }
560  }
561 }
562 
563 /* Encode MV differences on H.263+ with Unrestricted MV mode */
564 static void h263p_encode_umotion(PutBitContext *pb, int val)
565 {
566  if ( val == 0)
567  put_bits(pb, 1, 1);
568  else {
569  unsigned code = (val < 0) << 1;
570  unsigned aval = val < 0 ? -val : val;
571  unsigned n_bits = 2;
572 
573  while (aval != 1) { // The leading digit is implicitly coded via length
574  unsigned tmp = (aval & 1) << 1 | 1;
575  aval >>= 1;
576  code |= tmp << n_bits;
577  n_bits += 2;
578  }
579  put_bits(pb, n_bits + 1, code);
580  }
581 }
582 
583 static int h263_pred_dc(MPVEncContext *const s, int n, int16_t **dc_val_ptr)
584 {
585  const int wrap = s->c.block_wrap[n];
586  const int xy = s->c.block_index[n];
587  int16_t *const dc_val = s->c.dc_val + xy;
588  int pred_dc;
589 
590  /* find prediction */
591  /* B C
592  * A X
593  */
594  int a = dc_val[-1];
595  int c = dc_val[-wrap];
596 
597  /* just DC prediction */
598  if (a != 1024 && c != 1024)
599  pred_dc = (a + c) >> 1;
600  else if (a != 1024)
601  pred_dc = a;
602  else
603  pred_dc = c;
604 
605  /* we assume pred is positive */
606  *dc_val_ptr = dc_val;
607  return pred_dc;
608 }
609 
610 static void h263_encode_mb(MPVEncContext *const s,
611  int16_t block[][64],
612  int motion_x, int motion_y)
613 {
614  int cbpc, cbpy, i, cbp, pred_x, pred_y;
615  int16_t pred_dc;
616  int16_t rec_intradc[6];
617  const int interleaved_stats = s->c.avctx->flags & AV_CODEC_FLAG_PASS1;
618 
619  if (!s->c.mb_intra) {
620  /* compute cbp */
621  cbp= get_p_cbp(s, block, motion_x, motion_y);
622 
623  if ((cbp | motion_x | motion_y | s->dquant | (s->c.mv_type - MV_TYPE_16X16)) == 0) {
624  /* skip macroblock */
625  put_bits(&s->pb, 1, 1);
626  if(interleaved_stats){
627  s->misc_bits++;
628  s->last_bits++;
629  }
630 
631  return;
632  }
633  put_bits(&s->pb, 1, 0); /* mb coded */
634 
635  cbpc = cbp & 3;
636  cbpy = cbp >> 2;
637  if (!s->alt_inter_vlc || cbpc!=3)
638  cbpy ^= 0xF;
639  if(s->dquant) cbpc+= 8;
640  if(s->c.mv_type==MV_TYPE_16X16){
641  put_bits(&s->pb,
644 
645  put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
646  if(s->dquant)
647  put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
648 
649  if(interleaved_stats){
650  s->misc_bits+= get_bits_diff(s);
651  }
652 
653  /* motion vectors: 16x16 mode */
654  ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
655 
656  if (!s->umvplus) {
657  ff_h263_encode_motion_vector(s, motion_x - pred_x,
658  motion_y - pred_y, 1);
659  }
660  else {
661  h263p_encode_umotion(&s->pb, motion_x - pred_x);
662  h263p_encode_umotion(&s->pb, motion_y - pred_y);
663  if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
664  /* To prevent Start Code emulation */
665  put_bits(&s->pb,1,1);
666  }
667  }else{
668  put_bits(&s->pb,
669  ff_h263_inter_MCBPC_bits[cbpc+16],
670  ff_h263_inter_MCBPC_code[cbpc+16]);
671  put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
672  if(s->dquant)
673  put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
674 
675  if(interleaved_stats){
676  s->misc_bits+= get_bits_diff(s);
677  }
678 
679  for(i=0; i<4; i++){
680  /* motion vectors: 8x8 mode*/
681  ff_h263_pred_motion(&s->c, i, 0, &pred_x, &pred_y);
682 
683  motion_x = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
684  motion_y = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
685  if (!s->umvplus) {
686  ff_h263_encode_motion_vector(s, motion_x - pred_x,
687  motion_y - pred_y, 1);
688  }
689  else {
690  h263p_encode_umotion(&s->pb, motion_x - pred_x);
691  h263p_encode_umotion(&s->pb, motion_y - pred_y);
692  if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
693  /* To prevent Start Code emulation */
694  put_bits(&s->pb,1,1);
695  }
696  }
697  }
698 
699  if(interleaved_stats){
700  s->mv_bits+= get_bits_diff(s);
701  }
702  } else {
703  av_assert2(s->c.mb_intra);
704 
705  cbp = 0;
706  if (s->c.h263_aic) {
707  /* Predict DC */
708  for(i=0; i<6; i++) {
709  int16_t level = block[i][0];
710  int16_t *dc_ptr;
711  int scale = i < 4 ? s->c.y_dc_scale : s->c.c_dc_scale;
712 
713  pred_dc = h263_pred_dc(s, i, &dc_ptr);
714  level -= pred_dc;
715  /* Quant */
716  if (level >= 0)
717  level = (level + (scale>>1))/scale;
718  else
719  level = (level - (scale>>1))/scale;
720 
721  if (!s->modified_quant) {
722  if (level < -127)
723  level = -127;
724  else if (level > 127)
725  level = 127;
726  }
727 
728  block[i][0] = level;
729  /* Reconstruction */
730  rec_intradc[i] = scale*level + pred_dc;
731  /* Oddify */
732  rec_intradc[i] |= 1;
733  //if ((rec_intradc[i] % 2) == 0)
734  // rec_intradc[i]++;
735  /* Clipping */
736  if (rec_intradc[i] < 0)
737  rec_intradc[i] = 0;
738  else if (rec_intradc[i] > 2047)
739  rec_intradc[i] = 2047;
740 
741  /* Update AC/DC tables */
742  *dc_ptr = rec_intradc[i];
743  /* AIC can change CBP */
744  if (s->c.block_last_index[i] > 0 ||
745  (s->c.block_last_index[i] == 0 && level !=0))
746  cbp |= 1 << (5 - i);
747  }
748  }else{
749  for(i=0; i<6; i++) {
750  /* compute cbp */
751  if (s->c.block_last_index[i] >= 1)
752  cbp |= 1 << (5 - i);
753  }
754  }
755 
756  cbpc = cbp & 3;
757  if (s->c.pict_type == AV_PICTURE_TYPE_I) {
758  if(s->dquant) cbpc+=4;
759  put_bits(&s->pb,
762  } else {
763  if(s->dquant) cbpc+=8;
764  put_bits(&s->pb, 1, 0); /* mb coded */
765  put_bits(&s->pb,
766  ff_h263_inter_MCBPC_bits[cbpc + 4],
767  ff_h263_inter_MCBPC_code[cbpc + 4]);
768  }
769  if (s->c.h263_aic) {
770  /* XXX: currently, we do not try to use ac prediction */
771  put_bits(&s->pb, 1, 0); /* no AC prediction */
772  }
773  cbpy = cbp >> 2;
774  put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
775  if(s->dquant)
776  put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
777 
778  if(interleaved_stats){
779  s->misc_bits+= get_bits_diff(s);
780  }
781  }
782 
783  for(i=0; i<6; i++) {
784  /* encode each block */
785  h263_encode_block(s, block[i], i);
786 
787  /* Update INTRADC for decoding */
788  if (s->c.h263_aic && s->c.mb_intra)
789  block[i][0] = rec_intradc[i];
790  }
791 
792  if(interleaved_stats){
793  if (!s->c.mb_intra) {
794  s->p_tex_bits+= get_bits_diff(s);
795  }else{
796  s->i_tex_bits+= get_bits_diff(s);
797  s->i_count++;
798  }
799  }
800 }
801 
802 void ff_h263_update_mb(MPVEncContext *const s)
803 {
804  const int mb_xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
805 
806  if (s->c.cur_pic.mbskip_table)
807  s->c.cur_pic.mbskip_table[mb_xy] = s->c.mb_skipped;
808 
809  if (s->c.mv_type == MV_TYPE_8X8)
810  s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_FORWARD_MV | MB_TYPE_8x8;
811  else if(s->c.mb_intra)
812  s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA;
813  else
814  s->c.cur_pic.mb_type[mb_xy] = MB_TYPE_FORWARD_MV | MB_TYPE_16x16;
815 
817 }
818 
820 {
821  MPVEncContext *const s = &m->s;
822 
823  s->me.mv_penalty = ff_h263_get_mv_penalty(); // FIXME exact table for MSMPEG4 & H.263+
824 
825  ff_h263dsp_init(&s->c.h263dsp);
826 
827  if (s->c.codec_id == AV_CODEC_ID_MPEG4)
828  return;
829 
830  s->intra_ac_vlc_length =s->inter_ac_vlc_length = uni_h263_inter_rl_len;
831  s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
832  if (s->c.h263_aic) {
833  s->intra_ac_vlc_length = uni_h263_intra_aic_rl_len;
834  s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
835 
836  s->c.y_dc_scale_table =
837  s->c.c_dc_scale_table = ff_aic_dc_scale_table;
838  }
839  s->ac_esc_length= 7+1+6+8;
840 
841  if (s->modified_quant)
842  s->c.chroma_qscale_table = ff_h263_chroma_qscale_table;
843 
844  // Only used for H.263 and H.263+
845  s->gob_index = H263_GOB_HEIGHT(s->c.height);
846 
847  // use fcodes >1 only for MPEG-4 & H.263 & H.263+ FIXME
848  switch(s->c.codec_id){
849  case AV_CODEC_ID_H263P:
850  if (s->umvplus)
851  m->fcode_tab = umv_fcode_tab + MAX_MV;
852  if (s->modified_quant) {
853  s->min_qcoeff= -2047;
854  s->max_qcoeff= 2047;
855  }else{
856  s->min_qcoeff= -127;
857  s->max_qcoeff= 127;
858  }
859  break;
860  // Note for MPEG-4 & H.263 the dc-scale table will be set per frame as needed later
861 #if CONFIG_FLV_ENCODER
862  case AV_CODEC_ID_FLV1:
864  /* format = 1; 11-bit codes */
865  s->min_qcoeff = -1023;
866  s->max_qcoeff = 1023;
867  break;
868 #endif
869  default: //nothing needed - default table already set in mpegvideo.c
870  s->min_qcoeff= -127;
871  s->max_qcoeff= 127;
872  }
873  // H.263, H.263+; will be overwritten for MSMPEG-4 later
874  if (!m->encode_picture_header)
875  m->encode_picture_header = h263_encode_picture_header;
876  if (!s->encode_mb)
877  s->encode_mb = h263_encode_mb;
878 }
879 
881 {
882  int i, mb_pos;
883 
884  for(i=0; i<6; i++){
885  if(s->c.mb_num-1 <= ff_mba_max[i]) break;
886  }
887  mb_pos= s->c.mb_x + s->c.mb_width*s->c.mb_y;
888  put_bits(&s->pb, ff_mba_length[i], mb_pos);
889 }
890 
891 #define OFFSET(x) offsetof(MpegEncContext, x)
892 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
893 static const AVOption h263_options[] = {
894  { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
895  { "mb_info", "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", FF_MPV_OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
898  { NULL },
899 };
900 
901 static const AVClass h263_class = {
902  .class_name = "H.263 encoder",
903  .item_name = av_default_item_name,
904  .option = h263_options,
905  .version = LIBAVUTIL_VERSION_INT,
906 };
907 
908 const FFCodec ff_h263_encoder = {
909  .p.name = "h263",
910  CODEC_LONG_NAME("H.263 / H.263-1996"),
911  .p.type = AVMEDIA_TYPE_VIDEO,
912  .p.id = AV_CODEC_ID_H263,
914  .color_ranges = AVCOL_RANGE_MPEG,
915  .p.priv_class = &h263_class,
917  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
918  .priv_data_size = sizeof(MPVMainEncContext),
921  .close = ff_mpv_encode_end,
922 };
923 
924 static const AVOption h263p_options[] = {
925  { "umv", "Use unlimited motion vectors.", FF_MPV_OFFSET(umvplus), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
926  { "aiv", "Use alternative inter VLC.", FF_MPV_OFFSET(alt_inter_vlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
927  { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
928  { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", FF_MPV_OFFSET(h263_slice_structured), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
931  { NULL },
932 };
933 static const AVClass h263p_class = {
934  .class_name = "H.263p encoder",
935  .item_name = av_default_item_name,
936  .option = h263p_options,
937  .version = LIBAVUTIL_VERSION_INT,
938 };
939 
940 const FFCodec ff_h263p_encoder = {
941  .p.name = "h263p",
942  CODEC_LONG_NAME("H.263+ / H.263-1998 / H.263 version 2"),
943  .p.type = AVMEDIA_TYPE_VIDEO,
944  .p.id = AV_CODEC_ID_H263P,
946  .color_ranges = AVCOL_RANGE_MPEG,
947  .p.priv_class = &h263p_class,
948  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
950  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
951  .priv_data_size = sizeof(MPVMainEncContext),
954  .close = ff_mpv_encode_end,
955 };
956 #endif
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
MPVMainEncContext::fcode_tab
const uint8_t * fcode_tab
smallest fcode needed for each MV
Definition: mpegvideoenc.h:239
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:386
MV_TYPE_16X16
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:175
h263data.h
FF_ASPECT_EXTENDED
#define FF_ASPECT_EXTENDED
Definition: h263.h:26
level
uint8_t level
Definition: svq3.c:208
av_clip
#define av_clip
Definition: common.h:100
MPVEncContext
Definition: mpegvideoenc.h:46
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
H263_GOB_HEIGHT
#define H263_GOB_HEIGHT(h)
Definition: h263.h:28
MAX_FCODE
#define MAX_FCODE
Definition: mpegvideoenc.h:285
thread.h
mpegvideoenc.h
int64_t
long long int64_t
Definition: coverity.c:34
mv
static const int8_t mv[256][2]
Definition: 4xm.c:81
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:291
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
h263enc.h
ff_clean_h263_qscales
void ff_clean_h263_qscales(MPVEncContext *s)
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
CANDIDATE_MB_TYPE_INTER
#define CANDIDATE_MB_TYPE_INTER
Definition: mpegvideoenc.h:291
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
MAX_DMV
#define MAX_DMV
Definition: motion_est.h:39
internal.h
av_const
#define av_const
Definition: attributes.h:84
h263dsp.h
AVOption
AVOption.
Definition: opt.h:429
MB_TYPE_16x16
#define MB_TYPE_16x16
Definition: mpegutils.h:41
FFCodec
Definition: codec_internal.h:127
AV_WN32A
#define AV_WN32A(p, v)
Definition: intreadwrite.h:534
init_mv_penalty
static av_cold void init_mv_penalty(void)
Definition: ituh263enc.c:56
mpegvideo.h
mpegutils.h
MPVMainEncContext::encode_picture_header
int(* encode_picture_header)(struct MPVMainEncContext *m)
Definition: mpegvideoenc.h:248
H263_RL_NB_ELEMS
#define H263_RL_NB_ELEMS
Definition: h263data.h:63
FF_MPV_COMMON_MOTION_EST_OPTS
#define FF_MPV_COMMON_MOTION_EST_OPTS
Definition: mpegvideoenc.h:377
ff_mpv_encode_picture
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
Definition: mpegvideo_enc.c:1942
FF_MPV_COMMON_OPTS
#define FF_MPV_COMMON_OPTS
Definition: mpegvideoenc.h:336
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ff_match_2uint16
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
Definition: utils.c:830
ff_h263_pred_motion
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:182
wrap
#define wrap(func)
Definition: neontest.h:65
ff_h263_pixel_aspect
const AVRational ff_h263_pixel_aspect[16]
Definition: h263data.c:273
RLTable
RLTable.
Definition: rl.h:39
mv_penalty
static uint8_t mv_penalty[MAX_FCODE+1][MAX_DMV *2+1]
Table of number of bits a motion vector component needs.
Definition: ituh263enc.c:54
ff_h263_encode_init
void ff_h263_encode_init(MPVMainEncContext *m)
val
static double val(void *priv, double ch)
Definition: aeval.c:77
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:353
ff_h263_update_motion_val
void ff_h263_update_motion_val(MpegEncContext *s)
Definition: h263.c:56
AVRational::num
int num
Numerator.
Definition: rational.h:59
dquant_code
static const int dquant_code[5]
Definition: mpeg4videoenc.c:450
RLTable::n
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
ff_h263_encode_motion
void ff_h263_encode_motion(PutBitContext *pb, int val, int f_code)
Definition: ituh263enc.c:156
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
ff_h263_encode_gob_header
void ff_h263_encode_gob_header(MPVEncContext *s, int mb_line)
MAX_MV
#define MAX_MV
Definition: motion_est.h:37
ff_h263_chroma_qscale_table
const uint8_t ff_h263_chroma_qscale_table[32]
Definition: h263data.c:260
s
#define s(width, name)
Definition: cbs_vp9.c:198
get_rl_index
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition: rl.h:101
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
UNI_MPEG4_ENC_INDEX
#define UNI_MPEG4_ENC_INDEX(last, run, level)
Definition: mpeg4videoenc.c:64
limits.h
FF_MPV_OFFSET
#define FF_MPV_OFFSET(x)
Definition: mpegvideoenc.h:332
ff_h263_get_mv_penalty
const av_cold uint8_t(* ff_h263_get_mv_penalty(void))[MAX_DMV *2+1]
Definition: ituh263enc.c:147
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
ff_h263_encode_mba
void ff_h263_encode_mba(MPVEncContext *s)
PutBitContext
Definition: put_bits.h:50
ff_rl_init
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition: rl.c:43
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
run
uint8_t run
Definition: svq3.c:207
RLTable::table_vlc
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
ff_h263_init_rl_inter
void ff_h263_init_rl_inter(void)
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
MB_TYPE_8x8
#define MB_TYPE_8x8
Definition: mpegutils.h:44
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:240
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
mathops.h
ff_mba_max
const uint16_t ff_mba_max[6]
Definition: h263data.c:265
ff_mpv_encode_end
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
Definition: mpegvideo_enc.c:1122
get_p_cbp
static int get_p_cbp(MPVEncContext *const s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h263enc.h:46
MPVMainEncContext
Definition: mpegvideoenc.h:199
AVOnce
#define AVOnce
Definition: thread.h:202
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
MV_TYPE_8X8
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition: mpegvideo.h:176
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
RLTable::table_level
const int8_t * table_level
Definition: rl.h:44
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ff_h263_rl_inter
RLTable ff_h263_rl_inter
Definition: h263data.c:159
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
codec_internal.h
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:56
put_bits_assume_flushed
static void put_bits_assume_flushed(const PutBitContext *s)
Inform the compiler that a PutBitContext is flushed (i.e.
Definition: put_bits.h:82
ff_h263_cbpy_tab
const uint8_t ff_h263_cbpy_tab[16][2]
Definition: h263data.c:82
ff_h263p_encoder
const FFCodec ff_h263p_encoder
ff_rl_intra_aic
RLTable ff_rl_intra_aic
Definition: h263data.c:228
ff_h263dsp_init
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
Definition: h263dsp.c:117
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:99
attributes.h
ff_h263_inter_MCBPC_bits
const uint8_t ff_h263_inter_MCBPC_bits[28]
Definition: h263data.c:47
pred_dc
static void FUNC() pred_dc(uint8_t *_src, const uint8_t *_top, const uint8_t *_left, ptrdiff_t stride, int log2_size, int c_idx)
Definition: pred_template.c:391
CANDIDATE_MB_TYPE_INTER4V
#define CANDIDATE_MB_TYPE_INTER4V
Definition: mpegvideoenc.h:292
get_bits_diff
static int get_bits_diff(MPVEncContext *s)
Definition: mpegvideoenc.h:409
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:68
ff_h263_encoder
const FFCodec ff_h263_encoder
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
ff_h263_update_mb
void ff_h263_update_mb(MPVEncContext *s)
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:57
ff_h263_format
const uint16_t ff_h263_format[8][2]
Definition: h263data.c:236
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
ff_h263_aspect_to_info
av_const int ff_h263_aspect_to_info(AVRational aspect)
len
int len
Definition: vorbis_enc_data.h:426
ff_flv_encode_picture_header
int ff_flv_encode_picture_header(MPVMainEncContext *const m)
Definition: flvenc.c:27
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:733
avcodec.h
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
h263_encode_init_static
static av_cold void h263_encode_init_static(void)
Definition: ituh263enc.c:131
ff_h263_mpeg4_reset_dc
void ff_h263_mpeg4_reset_dc(MPVEncContext *s)
AVClass::class_name
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:81
ff_aic_dc_scale_table
const uint8_t ff_aic_dc_scale_table[32]
Definition: h263data.c:245
ff_mvtab
const uint8_t ff_mvtab[33][2]
Definition: h263data.c:88
ff_h263_intra_MCBPC_bits
const uint8_t ff_h263_intra_MCBPC_bits[9]
Definition: h263data.c:33
OFFSET
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your see the OFFSET() macro
AV_CODEC_ID_H263P
@ AV_CODEC_ID_H263P
Definition: codec_id.h:71
ff_h263_intra_MCBPC_code
const uint8_t ff_h263_intra_MCBPC_code[9]
Definition: h263data.c:32
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:132
ff_mba_length
const uint8_t ff_mba_length[7]
Definition: h263data.c:269
flvenc.h
ff_h263_inter_MCBPC_code
const uint8_t ff_h263_inter_MCBPC_code[28]
Definition: h263data.c:38
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
ff_mpv_encode_init
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
Definition: mpegvideo_enc.c:559
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:273
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
ff_h263_encode_motion_vector
static void ff_h263_encode_motion_vector(MPVEncContext *s, int x, int y, int f_code)
Definition: h263enc.h:39
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
VE
#define VE
Definition: amfenc_av1.c:30
H263_RL_NON_LAST_CODES
#define H263_RL_NON_LAST_CODES
Definition: h263data.h:64
AV_CODEC_ID_FLV1
@ AV_CODEC_ID_FLV1
Definition: codec_id.h:73
put_bits.h
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
MPVMainEncContext::s
MPVEncContext s
The main slicecontext.
Definition: mpegvideoenc.h:200
mb_info
Definition: cinepakenc.c:87
MB_TYPE_FORWARD_MV
#define MB_TYPE_FORWARD_MV
Definition: mpegutils.h:49
RLTable::table_run
const int8_t * table_run
Definition: rl.h:43
H263_ESCAPE_CODE_LENGTH
#define H263_ESCAPE_CODE_LENGTH
Definition: h263data.h:65
MB_TYPE_INTRA
#define MB_TYPE_INTRA
Definition: mpegutils.h:64
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:290
h263.h