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