Libav
ituh263enc.c
Go to the documentation of this file.
1 /*
2  * ITU H263 bitstream encoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * H263+ 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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
30 #include <limits.h>
31 
32 #include "libavutil/attributes.h"
33 #include "avcodec.h"
34 #include "mpegvideo.h"
35 #include "h263.h"
36 #include "mathops.h"
37 #include "mpegutils.h"
38 #include "unary.h"
39 #include "flv.h"
40 #include "mpeg4video.h"
41 #include "internal.h"
42 
47 
51 static uint8_t fcode_tab[MAX_MV*2+1];
52 
58 
59 //unified encoding tables for run length encoding of coefficients
60 //unified in the sense that the specification specifies the encoding in several steps.
62 static uint8_t uni_h263_inter_rl_len [64*64*2*2];
63 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
64 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
65 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
66 
67 static const uint8_t wrong_run[102] = {
68  1, 2, 3, 5, 4, 10, 9, 8,
69 11, 15, 17, 16, 23, 22, 21, 20,
70 19, 18, 25, 24, 27, 26, 11, 7,
71  6, 1, 2, 13, 2, 2, 2, 2,
72  6, 12, 3, 9, 1, 3, 4, 3,
73  7, 4, 1, 1, 5, 5, 14, 6,
74  1, 7, 1, 8, 1, 1, 1, 1,
75 10, 1, 1, 5, 9, 17, 25, 24,
76 29, 33, 32, 41, 2, 23, 28, 31,
77  3, 22, 30, 4, 27, 40, 8, 26,
78  6, 39, 7, 38, 16, 37, 15, 10,
79 11, 12, 13, 14, 1, 21, 20, 18,
80 19, 2, 1, 34, 35, 36
81 };
82 
89  int i;
90 
91  if(aspect.num==0) aspect= (AVRational){1,1};
92 
93  for(i=1; i<6; i++){
94  if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
95  return i;
96  }
97  }
98 
99  return FF_ASPECT_EXTENDED;
100 }
101 
102 void ff_h263_encode_picture_header(MpegEncContext * s, int picture_number)
103 {
104  int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
105  int best_clock_code=1;
106  int best_divisor=60;
107  int best_error= INT_MAX;
108 
109  if(s->h263_plus){
110  for(i=0; i<2; i++){
111  int div, error;
112  div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
113  div= av_clip(div, 1, 127);
114  error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
115  if(error < best_error){
116  best_error= error;
117  best_divisor= div;
118  best_clock_code= i;
119  }
120  }
121  }
122  s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
123  coded_frame_rate= 1800000;
124  coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
125 
127 
128  /* Update the pointer to last GOB */
129  s->ptr_lastgob = put_bits_ptr(&s->pb);
130  put_bits(&s->pb, 22, 0x20); /* PSC */
131  temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
132  (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
133  put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
134 
135  put_bits(&s->pb, 1, 1); /* marker */
136  put_bits(&s->pb, 1, 0); /* h263 id */
137  put_bits(&s->pb, 1, 0); /* split screen off */
138  put_bits(&s->pb, 1, 0); /* camera off */
139  put_bits(&s->pb, 1, 0); /* freeze picture release off */
140 
142  if (!s->h263_plus) {
143  /* H.263v1 */
144  put_bits(&s->pb, 3, format);
145  put_bits(&s->pb, 1, (s->pict_type == AV_PICTURE_TYPE_P));
146  /* By now UMV IS DISABLED ON H.263v1, since the restrictions
147  of H.263v1 UMV implies to check the predicted MV after
148  calculation of the current MB to see if we're on the limits */
149  put_bits(&s->pb, 1, 0); /* Unrestricted Motion Vector: off */
150  put_bits(&s->pb, 1, 0); /* SAC: off */
151  put_bits(&s->pb, 1, s->obmc); /* Advanced Prediction */
152  put_bits(&s->pb, 1, 0); /* only I/P frames, no PB frame */
153  put_bits(&s->pb, 5, s->qscale);
154  put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
155  } else {
156  int ufep=1;
157  /* H.263v2 */
158  /* H.263 Plus PTYPE */
159 
160  put_bits(&s->pb, 3, 7);
161  put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
162  if (format == 8)
163  put_bits(&s->pb,3,6); /* Custom Source Format */
164  else
165  put_bits(&s->pb, 3, format);
166 
167  put_bits(&s->pb,1, s->custom_pcf);
168  put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
169  put_bits(&s->pb,1,0); /* SAC: off */
170  put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
171  put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
172  put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
173  put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
174  put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
175  put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
176  put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
177  put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
178  put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
179  put_bits(&s->pb,3,0); /* Reserved */
180 
181  put_bits(&s->pb, 3, s->pict_type == AV_PICTURE_TYPE_P);
182 
183  put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
184  put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
185  put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
186  put_bits(&s->pb,2,0); /* Reserved */
187  put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
188 
189  /* This should be here if PLUSPTYPE */
190  put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
191 
192  if (format == 8) {
193  /* Custom Picture Format (CPFMT) */
195 
196  put_bits(&s->pb,4,s->aspect_ratio_info);
197  put_bits(&s->pb,9,(s->width >> 2) - 1);
198  put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
199  put_bits(&s->pb,9,(s->height >> 2));
201  put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
202  put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
203  }
204  }
205  if(s->custom_pcf){
206  if(ufep){
207  put_bits(&s->pb, 1, best_clock_code);
208  put_bits(&s->pb, 7, best_divisor);
209  }
210  put_sbits(&s->pb, 2, temp_ref>>8);
211  }
212 
213  /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
214  if (s->umvplus)
215 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
216 //FIXME check actual requested range
217  put_bits(&s->pb,2,1); /* unlimited */
218  if(s->h263_slice_structured)
219  put_bits(&s->pb,2,0); /* no weird submodes */
220 
221  put_bits(&s->pb, 5, s->qscale);
222  }
223 
224  put_bits(&s->pb, 1, 0); /* no PEI */
225 
226  if(s->h263_slice_structured){
227  put_bits(&s->pb, 1, 1);
228 
229  assert(s->mb_x == 0 && s->mb_y == 0);
231 
232  put_bits(&s->pb, 1, 1);
233  }
234 
235  if(s->h263_aic){
236  s->y_dc_scale_table=
238  }else{
239  s->y_dc_scale_table=
241  }
242 }
243 
248 {
249  put_bits(&s->pb, 17, 1); /* GBSC */
250 
251  if(s->h263_slice_structured){
252  put_bits(&s->pb, 1, 1);
253 
255 
256  if(s->mb_num > 1583)
257  put_bits(&s->pb, 1, 1);
258  put_bits(&s->pb, 5, s->qscale); /* GQUANT */
259  put_bits(&s->pb, 1, 1);
260  put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I); /* GFID */
261  }else{
262  int gob_number= mb_line / s->gob_index;
263 
264  put_bits(&s->pb, 5, gob_number); /* GN */
265  put_bits(&s->pb, 2, s->pict_type == AV_PICTURE_TYPE_I); /* GFID */
266  put_bits(&s->pb, 5, s->qscale); /* GQUANT */
267  }
268 }
269 
274  int i;
275  int8_t * const qscale_table = s->current_picture.qscale_table;
276 
278 
279  for(i=1; i<s->mb_num; i++){
280  if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
281  qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
282  }
283  for(i=s->mb_num-2; i>=0; i--){
284  if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
285  qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
286  }
287 
288  if(s->codec_id != AV_CODEC_ID_H263P){
289  for(i=1; i<s->mb_num; i++){
290  int mb_xy= s->mb_index2xy[i];
291 
292  if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
293  s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
294  }
295  }
296  }
297 }
298 
299 static const int dquant_code[5]= {1,0,9,2,3};
300 
306 static void h263_encode_block(MpegEncContext * s, int16_t * block, int n)
307 {
308  int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
309  RLTable *rl;
310 
311  rl = &ff_h263_rl_inter;
312  if (s->mb_intra && !s->h263_aic) {
313  /* DC coef */
314  level = block[0];
315  /* 255 cannot be represented, so we clamp */
316  if (level > 254) {
317  level = 254;
318  block[0] = 254;
319  }
320  /* 0 cannot be represented also */
321  else if (level < 1) {
322  level = 1;
323  block[0] = 1;
324  }
325  if (level == 128) //FIXME check rv10
326  put_bits(&s->pb, 8, 0xff);
327  else
328  put_bits(&s->pb, 8, level);
329  i = 1;
330  } else {
331  i = 0;
332  if (s->h263_aic && s->mb_intra)
333  rl = &ff_rl_intra_aic;
334 
335  if(s->alt_inter_vlc && !s->mb_intra){
336  int aic_vlc_bits=0;
337  int inter_vlc_bits=0;
338  int wrong_pos=-1;
339  int aic_code;
340 
341  last_index = s->block_last_index[n];
342  last_non_zero = i - 1;
343  for (; i <= last_index; i++) {
344  j = s->intra_scantable.permutated[i];
345  level = block[j];
346  if (level) {
347  run = i - last_non_zero - 1;
348  last = (i == last_index);
349 
350  if(level<0) level= -level;
351 
352  code = get_rl_index(rl, last, run, level);
353  aic_code = get_rl_index(&ff_rl_intra_aic, last, run, level);
354  inter_vlc_bits += rl->table_vlc[code][1]+1;
355  aic_vlc_bits += ff_rl_intra_aic.table_vlc[aic_code][1]+1;
356 
357  if (code == rl->n) {
358  inter_vlc_bits += 1+6+8-1;
359  }
360  if (aic_code == ff_rl_intra_aic.n) {
361  aic_vlc_bits += 1+6+8-1;
362  wrong_pos += run + 1;
363  }else
364  wrong_pos += wrong_run[aic_code];
365  last_non_zero = i;
366  }
367  }
368  i = 0;
369  if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
370  rl = &ff_rl_intra_aic;
371  }
372  }
373 
374  /* AC coefs */
375  last_index = s->block_last_index[n];
376  last_non_zero = i - 1;
377  for (; i <= last_index; i++) {
378  j = s->intra_scantable.permutated[i];
379  level = block[j];
380  if (level) {
381  run = i - last_non_zero - 1;
382  last = (i == last_index);
383  sign = 0;
384  slevel = level;
385  if (level < 0) {
386  sign = 1;
387  level = -level;
388  }
389  code = get_rl_index(rl, last, run, level);
390  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
391  if (code == rl->n) {
392  if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
393  put_bits(&s->pb, 1, last);
394  put_bits(&s->pb, 6, run);
395 
396  assert(slevel != 0);
397 
398  if(level < 128)
399  put_sbits(&s->pb, 8, slevel);
400  else{
401  put_bits(&s->pb, 8, 128);
402  put_sbits(&s->pb, 5, slevel);
403  put_sbits(&s->pb, 6, slevel>>5);
404  }
405  }else{
406  ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
407  }
408  } else {
409  put_bits(&s->pb, 1, sign);
410  }
411  last_non_zero = i;
412  }
413  }
414 }
415 
416 /* Encode MV differences on H.263+ with Unrestricted MV mode */
417 static void h263p_encode_umotion(MpegEncContext * s, int val)
418 {
419  short sval = 0;
420  short i = 0;
421  short n_bits = 0;
422  short temp_val;
423  int code = 0;
424  int tcode;
425 
426  if ( val == 0)
427  put_bits(&s->pb, 1, 1);
428  else if (val == 1)
429  put_bits(&s->pb, 3, 0);
430  else if (val == -1)
431  put_bits(&s->pb, 3, 2);
432  else {
433 
434  sval = ((val < 0) ? (short)(-val):(short)val);
435  temp_val = sval;
436 
437  while (temp_val != 0) {
438  temp_val = temp_val >> 1;
439  n_bits++;
440  }
441 
442  i = n_bits - 1;
443  while (i > 0) {
444  tcode = (sval & (1 << (i-1))) >> (i-1);
445  tcode = (tcode << 1) | 1;
446  code = (code << 2) | tcode;
447  i--;
448  }
449  code = ((code << 1) | (val < 0)) << 1;
450  put_bits(&s->pb, (2*n_bits)+1, code);
451  }
452 }
453 
455  int16_t block[6][64],
456  int motion_x, int motion_y)
457 {
458  int cbpc, cbpy, i, cbp, pred_x, pred_y;
459  int16_t pred_dc;
460  int16_t rec_intradc[6];
461  int16_t *dc_ptr[6];
462  const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
463 
464  if (!s->mb_intra) {
465  /* compute cbp */
466  cbp= get_p_cbp(s, block, motion_x, motion_y);
467 
468  if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
469  /* skip macroblock */
470  put_bits(&s->pb, 1, 1);
471  if(interleaved_stats){
472  s->misc_bits++;
473  s->last_bits++;
474  }
475  s->skip_count++;
476 
477  return;
478  }
479  put_bits(&s->pb, 1, 0); /* mb coded */
480 
481  cbpc = cbp & 3;
482  cbpy = cbp >> 2;
483  if(s->alt_inter_vlc==0 || cbpc!=3)
484  cbpy ^= 0xF;
485  if(s->dquant) cbpc+= 8;
486  if(s->mv_type==MV_TYPE_16X16){
487  put_bits(&s->pb,
490 
491  put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
492  if(s->dquant)
493  put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
494 
495  if(interleaved_stats){
496  s->misc_bits+= get_bits_diff(s);
497  }
498 
499  /* motion vectors: 16x16 mode */
500  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
501 
502  if (!s->umvplus) {
503  ff_h263_encode_motion_vector(s, motion_x - pred_x,
504  motion_y - pred_y, 1);
505  }
506  else {
507  h263p_encode_umotion(s, motion_x - pred_x);
508  h263p_encode_umotion(s, motion_y - pred_y);
509  if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
510  /* To prevent Start Code emulation */
511  put_bits(&s->pb,1,1);
512  }
513  }else{
514  put_bits(&s->pb,
515  ff_h263_inter_MCBPC_bits[cbpc+16],
516  ff_h263_inter_MCBPC_code[cbpc+16]);
517  put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
518  if(s->dquant)
519  put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
520 
521  if(interleaved_stats){
522  s->misc_bits+= get_bits_diff(s);
523  }
524 
525  for(i=0; i<4; i++){
526  /* motion vectors: 8x8 mode*/
527  ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
528 
529  motion_x = s->current_picture.motion_val[0][s->block_index[i]][0];
530  motion_y = s->current_picture.motion_val[0][s->block_index[i]][1];
531  if (!s->umvplus) {
532  ff_h263_encode_motion_vector(s, motion_x - pred_x,
533  motion_y - pred_y, 1);
534  }
535  else {
536  h263p_encode_umotion(s, motion_x - pred_x);
537  h263p_encode_umotion(s, motion_y - pred_y);
538  if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
539  /* To prevent Start Code emulation */
540  put_bits(&s->pb,1,1);
541  }
542  }
543  }
544 
545  if(interleaved_stats){
546  s->mv_bits+= get_bits_diff(s);
547  }
548  } else {
549  assert(s->mb_intra);
550 
551  cbp = 0;
552  if (s->h263_aic) {
553  /* Predict DC */
554  for(i=0; i<6; i++) {
555  int16_t level = block[i][0];
556  int scale;
557 
558  if(i<4) scale= s->y_dc_scale;
559  else scale= s->c_dc_scale;
560 
561  pred_dc = ff_h263_pred_dc(s, i, &dc_ptr[i]);
562  level -= pred_dc;
563  /* Quant */
564  if (level >= 0)
565  level = (level + (scale>>1))/scale;
566  else
567  level = (level - (scale>>1))/scale;
568 
569  if(!s->modified_quant){
570  if (level < -127)
571  level = -127;
572  else if (level > 127)
573  level = 127;
574  }
575 
576  block[i][0] = level;
577  /* Reconstruction */
578  rec_intradc[i] = scale*level + pred_dc;
579  /* Oddify */
580  rec_intradc[i] |= 1;
581  //if ((rec_intradc[i] % 2) == 0)
582  // rec_intradc[i]++;
583  /* Clipping */
584  if (rec_intradc[i] < 0)
585  rec_intradc[i] = 0;
586  else if (rec_intradc[i] > 2047)
587  rec_intradc[i] = 2047;
588 
589  /* Update AC/DC tables */
590  *dc_ptr[i] = rec_intradc[i];
591  /* AIC can change CBP */
592  if (s->block_last_index[i] > 0 ||
593  (s->block_last_index[i] == 0 && level !=0))
594  cbp |= 1 << (5 - i);
595  }
596  }else{
597  for(i=0; i<6; i++) {
598  /* compute cbp */
599  if (s->block_last_index[i] >= 1)
600  cbp |= 1 << (5 - i);
601  }
602  }
603 
604  cbpc = cbp & 3;
605  if (s->pict_type == AV_PICTURE_TYPE_I) {
606  if(s->dquant) cbpc+=4;
607  put_bits(&s->pb,
610  } else {
611  if(s->dquant) cbpc+=8;
612  put_bits(&s->pb, 1, 0); /* mb coded */
613  put_bits(&s->pb,
614  ff_h263_inter_MCBPC_bits[cbpc + 4],
615  ff_h263_inter_MCBPC_code[cbpc + 4]);
616  }
617  if (s->h263_aic) {
618  /* XXX: currently, we do not try to use ac prediction */
619  put_bits(&s->pb, 1, 0); /* no AC prediction */
620  }
621  cbpy = cbp >> 2;
622  put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
623  if(s->dquant)
624  put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
625 
626  if(interleaved_stats){
627  s->misc_bits+= get_bits_diff(s);
628  }
629  }
630 
631  for(i=0; i<6; i++) {
632  /* encode each block */
633  h263_encode_block(s, block[i], i);
634 
635  /* Update INTRADC for decoding */
636  if (s->h263_aic && s->mb_intra) {
637  block[i][0] = rec_intradc[i];
638 
639  }
640  }
641 
642  if(interleaved_stats){
643  if (!s->mb_intra) {
644  s->p_tex_bits+= get_bits_diff(s);
645  s->f_count++;
646  }else{
647  s->i_tex_bits+= get_bits_diff(s);
648  s->i_count++;
649  }
650  }
651 }
652 
653 void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
654 {
655  int range, bit_size, sign, code, bits;
656 
657  if (val == 0) {
658  /* zero vector */
659  code = 0;
660  put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
661  } else {
662  bit_size = f_code - 1;
663  range = 1 << bit_size;
664  /* modulo encoding */
665  val = sign_extend(val, 6 + bit_size);
666  sign = val>>31;
667  val= (val^sign)-sign;
668  sign&=1;
669 
670  val--;
671  code = (val >> bit_size) + 1;
672  bits = val & (range - 1);
673 
674  put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
675  if (bit_size > 0) {
676  put_bits(&s->pb, bit_size, bits);
677  }
678  }
679 }
680 
682 {
683  int f_code;
684  int mv;
685 
686  for(f_code=1; f_code<=MAX_FCODE; f_code++){
687  for(mv=-MAX_MV; mv<=MAX_MV; mv++){
688  int len;
689 
690  if(mv==0) len= ff_mvtab[0][1];
691  else{
692  int val, bit_size, code;
693 
694  bit_size = f_code - 1;
695 
696  val=mv;
697  if (val < 0)
698  val = -val;
699  val--;
700  code = (val >> bit_size) + 1;
701  if(code<33){
702  len= ff_mvtab[code][1] + 1 + bit_size;
703  }else{
704  len= ff_mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
705  }
706  }
707 
708  mv_penalty[f_code][mv+MAX_MV]= len;
709  }
710  }
711 
712  for(f_code=MAX_FCODE; f_code>0; f_code--){
713  for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
714  fcode_tab[mv+MAX_MV]= f_code;
715  }
716  }
717 
718  for(mv=0; mv<MAX_MV*2+1; mv++){
719  umv_fcode_tab[mv]= 1;
720  }
721 }
722 
723 static av_cold void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab,
724  uint8_t *len_tab)
725 {
726  int slevel, run, last;
727 
728  assert(MAX_LEVEL >= 64);
729  assert(MAX_RUN >= 63);
730 
731  for(slevel=-64; slevel<64; slevel++){
732  if(slevel==0) continue;
733  for(run=0; run<64; run++){
734  for(last=0; last<=1; last++){
735  const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
736  int level= slevel < 0 ? -slevel : slevel;
737  int sign= slevel < 0 ? 1 : 0;
738  int bits, len, code;
739 
740  len_tab[index]= 100;
741 
742  /* ESC0 */
743  code= get_rl_index(rl, last, run, level);
744  bits= rl->table_vlc[code][0];
745  len= rl->table_vlc[code][1];
746  bits=bits*2+sign; len++;
747 
748  if(code!=rl->n && len < len_tab[index]){
749  if(bits_tab) bits_tab[index]= bits;
750  len_tab [index]= len;
751  }
752  /* ESC */
753  bits= rl->table_vlc[rl->n][0];
754  len = rl->table_vlc[rl->n][1];
755  bits=bits*2+last; len++;
756  bits=bits*64+run; len+=6;
757  bits=bits*256+(level&0xff); len+=8;
758 
759  if(len < len_tab[index]){
760  if(bits_tab) bits_tab[index]= bits;
761  len_tab [index]= len;
762  }
763  }
764  }
765  }
766 }
767 
769 {
770  static int done = 0;
771 
772  if (!done) {
773  done = 1;
774 
777 
780 
782  }
783  s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
784 
787  if(s->h263_aic){
790  }
791  s->ac_esc_length= 7+1+6+8;
792 
793  // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
794  switch(s->codec_id){
795  case AV_CODEC_ID_MPEG4:
796  s->fcode_tab= fcode_tab;
797  break;
798  case AV_CODEC_ID_H263P:
799  if(s->umvplus)
801  if(s->modified_quant){
802  s->min_qcoeff= -2047;
803  s->max_qcoeff= 2047;
804  }else{
805  s->min_qcoeff= -127;
806  s->max_qcoeff= 127;
807  }
808  break;
809  //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
810  case AV_CODEC_ID_FLV1:
811  if (s->h263_flv > 1) {
812  s->min_qcoeff= -1023;
813  s->max_qcoeff= 1023;
814  } else {
815  s->min_qcoeff= -127;
816  s->max_qcoeff= 127;
817  }
818  s->y_dc_scale_table=
820  break;
821  default: //nothing needed - default table already set in mpegvideo.c
822  s->min_qcoeff= -127;
823  s->max_qcoeff= 127;
824  s->y_dc_scale_table=
826  }
827 }
828 
830 {
831  int i, mb_pos;
832 
833  for(i=0; i<6; i++){
834  if(s->mb_num-1 <= ff_mba_max[i]) break;
835  }
836  mb_pos= s->mb_x + s->mb_width*s->mb_y;
837  put_bits(&s->pb, ff_mba_length[i], mb_pos);
838 }
#define av_const
Definition: attributes.h:60
static const int dquant_code[5]
Definition: ituh263enc.c:299
int aspect_ratio_info
Definition: mpegvideo.h:515
int picture_number
Definition: mpegvideo.h:253
av_cold void ff_h263_encode_init(MpegEncContext *s)
Definition: ituh263enc.c:768
uint8_t * fcode_tab
smallest fcode needed for each MV
Definition: mpegvideo.h:401
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:314
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:172
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:635
RLTable ff_h263_rl_inter
Definition: h263data.h:162
const uint8_t ff_h263_intra_MCBPC_code[9]
Definition: h263data.h:36
const uint8_t ff_h263_cbpy_tab[16][2]
Definition: h263data.h:85
int num
numerator
Definition: rational.h:44
enum AVCodecID codec_id
Definition: mpegvideo.h:235
int obmc
overlapped block motion compensation
Definition: mpegvideo.h:483
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:45
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1445
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:55
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:429
#define UNI_MPEG4_ENC_INDEX(last, run, level)
Definition: ituh263enc.c:65
mpegvideo header.
#define FF_ARRAY_ELEMS(a)
#define FF_ASPECT_EXTENDED
Definition: avcodec.h:1247
uint8_t permutated[64]
Definition: idctdsp.h:31
uint8_t run
Definition: svq3.c:146
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:432
const uint16_t ff_h263_format[8][2]
Definition: h263data.h:239
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:259
void ff_h263_encode_motion(MpegEncContext *s, int val, int f_code)
Definition: ituh263enc.c:653
void ff_h263_encode_gob_header(MpegEncContext *s, int mb_line)
Encode a group of blocks header.
Definition: ituh263enc.c:247
RLTable.
Definition: rl.h:38
int qscale
QP.
Definition: mpegvideo.h:332
void ff_h263_encode_mba(MpegEncContext *s)
Definition: ituh263enc.c:829
int h263_aic
Advanded INTRA Coding (AIC)
Definition: mpegvideo.h:210
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:313
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
int modified_quant
Definition: mpegvideo.h:494
static uint8_t uni_h263_inter_rl_len[64 *64 *2 *2]
Definition: ituh263enc.c:62
uint8_t ff_mba_length[7]
Definition: h263data.h:271
#define CANDIDATE_MB_TYPE_INTER
Definition: mpegutils.h:101
int alt_inter_vlc
alternative inter vlc
Definition: mpegvideo.h:493
uint8_t * ptr_lastgob
Definition: mpegvideo.h:597
uint8_t bits
Definition: crc.c:251
uint8_t
#define av_cold
Definition: attributes.h:66
static av_cold void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab)
Definition: ituh263enc.c:723
#define CANDIDATE_MB_TYPE_INTER4V
Definition: mpegutils.h:102
const uint8_t ff_h263_intra_MCBPC_bits[9]
Definition: h263data.h:37
const uint8_t ff_mvtab[33][2]
Definition: h263data.h:91
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:468
int no_rounding
apply no rounding to motion compensation (MPEG4, msmpeg4, ...) for b-frames rounding mode is always 0...
Definition: mpegvideo.h:406
const uint8_t ff_h263_inter_MCBPC_bits[28]
Definition: h263data.h:50
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:306
uint8_t(* mv_penalty)[MAX_MV *2+1]
amount of bits needed to encode a MV
Definition: mpegvideo.h:193
static void h263p_encode_umotion(MpegEncContext *s, int val)
Definition: ituh263enc.c:417
RLTable ff_rl_intra_aic
Definition: h263data.h:231
void ff_init_qscale_tab(MpegEncContext *s)
init s->current_picture.qscale_table from s->lambda_table
int ff_h263_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr)
Definition: h263.c:98
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:430
const AVRational ff_h263_pixel_aspect[16]
Definition: h263data.h:275
#define MAX_LEVEL
Definition: rl.h:35
int dquant
qscale difference to prev qscale
Definition: mpegvideo.h:338
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:770
int h263_plus
h263 plus headers
Definition: mpegvideo.h:232
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:199
uint8_t * inter_ac_vlc_last_length
Definition: mpegvideo.h:435
void ff_flv2_encode_ac_esc(PutBitContext *pb, int slevel, int level, int run, int last)
Definition: flvenc.c:70
static uint8_t fcode_tab[MAX_MV *2+1]
Minimal fcode that a motion vector component would need.
Definition: ituh263enc.c:51
int h263_slice_structured
Definition: mpegvideo.h:492
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:413
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
static void h263_encode_block(MpegEncContext *s, int16_t *block, int n)
Encode an 8x8 block.
Definition: ituh263enc.c:306
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:433
int n
number of entries of table_vlc minus 1
Definition: rl.h:39
const uint16_t(* table_vlc)[2]
Definition: rl.h:41
int umvplus
== H263+ && unrestricted_mv
Definition: mpegvideo.h:490
int16_t(*[2] motion_val)[2]
Definition: mpegvideo.h:107
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)
#define FFABS(a)
Definition: common.h:52
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:209
MotionEstContext me
Definition: mpegvideo.h:404
const uint8_t ff_aic_dc_scale_table[32]
Definition: h263data.h:248
const uint8_t ff_h263_inter_MCBPC_code[28]
Definition: h263data.h:41
int ac_esc_length
num of bits needed to encode the longest esc
Definition: mpegvideo.h:431
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:415
static av_cold void init_mv_penalty_and_fcode(MpegEncContext *s)
Definition: ituh263enc.c:681
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:419
static const int8_t mv[256][2]
Definition: 4xm.c:75
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:388
NULL
Definition: eval.c:55
Libavcodec external API header.
int h263_flv
use flv h263 header
Definition: mpegvideo.h:233
uint8_t ff_h263_static_rl_table_store[2][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: h263.c:43
static uint8_t umv_fcode_tab[MAX_MV *2+1]
Minimal fcode that a motion vector component would need in umv.
Definition: ituh263enc.c:57
void ff_h263_encode_picture_header(MpegEncContext *s, int picture_number)
Definition: ituh263enc.c:102
ScanTable intra_scantable
Definition: mpegvideo.h:214
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:223
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideo.c:55
uint8_t * inter_ac_vlc_length
Definition: mpegvideo.h:434
static const uint8_t wrong_run[102]
Definition: ituh263enc.c:67
int index
Definition: gxfenc.c:72
rational number numerator/denominator
Definition: rational.h:43
static void ff_h263_encode_motion_vector(MpegEncContext *s, int x, int y, int f_code)
Definition: h263.h:145
static uint8_t uni_h263_intra_aic_rl_len[64 *64 *2 *2]
Definition: ituh263enc.c:61
#define MAX_MV
Definition: mpegvideo.h:64
#define MAX_FCODE
Definition: mpegvideo.h:63
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:339
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:127
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:315
uint8_t level
Definition: svq3.c:147
MpegEncContext.
Definition: mpegvideo.h:204
int8_t * qscale_table
Definition: mpegvideo.h:104
#define MAX_RUN
Definition: rl.h:34
struct AVCodecContext * avctx
Definition: mpegvideo.h:221
PutBitContext pb
bit output
Definition: mpegvideo.h:277
static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV *2+1]
Table of number of bits a motion vector component needs.
Definition: ituh263enc.c:46
common internal api header.
int den
denominator
Definition: rational.h:45
av_const int ff_h263_aspect_to_info(AVRational aspect)
Return the 4 bit value that specifies the given aspect ratio.
Definition: ituh263enc.c:88
void ff_h263_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: ituh263enc.c:454
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition: rl.h:75
void ff_clean_h263_qscales(MpegEncContext *s)
modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
Definition: ituh263enc.c:273
int last_bits
temp var used for calculating the above vars
Definition: mpegvideo.h:469
int len
#define av_log2
Definition: intmath.h:85
av_cold void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: mpegvideo.c:1602
static int get_p_cbp(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
Definition: h263.h:156
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:238
#define CONFIG_FLV_ENCODER
Definition: config.h:966
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:2251
Predicted.
Definition: avutil.h:254
uint16_t ff_mba_max[6]
Definition: h263data.h:267
static int16_t block[64]
Definition: dct-test.c:88