FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
msmpeg4enc.c
Go to the documentation of this file.
1 /*
2  * MSMPEG4 encoder backend
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * msmpeg4v1 & v2 stuff by 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  * MSMPEG4 encoder backend
28  */
29 
30 #include <stdint.h>
31 #include <string.h>
32 
33 #include "libavutil/attributes.h"
34 #include "libavutil/avutil.h"
35 #include "libavutil/mem.h"
36 #include "mpegvideo.h"
37 #include "msmpeg4.h"
38 #include "h263.h"
39 #include "mpeg4video.h"
40 #include "msmpeg4.h"
41 #include "msmpeg4data.h"
42 #include "put_bits.h"
43 #include "rl.h"
44 #include "vc1data.h"
45 
47 
48 /* build the table which associate a (x,y) motion vector to a vlc */
50 {
51  int i, x, y;
52 
53  tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
54  /* mark all entries as not used */
55  for(i=0;i<4096;i++)
56  tab->table_mv_index[i] = tab->n;
57 
58  for(i=0;i<tab->n;i++) {
59  x = tab->table_mvx[i];
60  y = tab->table_mvy[i];
61  tab->table_mv_index[(x << 6) | y] = i;
62  }
63 }
64 
66 {
67  if (n == 0) {
68  put_bits(pb, 1, 0);
69  } else {
70  put_bits(pb, 1, 1);
71  put_bits(pb, 1, (n >= 2));
72  }
73 }
74 
75 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
76  int size=0;
77  int code;
78  int run_diff= intra ? 0 : 1;
79 
80  code = get_rl_index(rl, last, run, level);
81  size+= rl->table_vlc[code][1];
82  if (code == rl->n) {
83  int level1, run1;
84 
85  level1 = level - rl->max_level[last][run];
86  if (level1 < 1)
87  goto esc2;
88  code = get_rl_index(rl, last, run, level1);
89  if (code == rl->n) {
90  esc2:
91  size++;
92  if (level > MAX_LEVEL)
93  goto esc3;
94  run1 = run - rl->max_run[last][level] - run_diff;
95  if (run1 < 0)
96  goto esc3;
97  code = get_rl_index(rl, last, run1, level);
98  if (code == rl->n) {
99  esc3:
100  /* third escape */
101  size+=1+1+6+8;
102  } else {
103  /* second escape */
104  size+= 1+1+ rl->table_vlc[code][1];
105  }
106  } else {
107  /* first escape */
108  size+= 1+1+ rl->table_vlc[code][1];
109  }
110  } else {
111  size++;
112  }
113  return size;
114 }
115 
117 {
118  static int init_done=0;
119  int i;
120 
122  if(s->msmpeg4_version>=4){
123  s->min_qcoeff= -255;
124  s->max_qcoeff= 255;
125  }
126 
127  if (!init_done) {
128  /* init various encoding tables */
129  init_done = 1;
132  for(i=0;i<NB_RL_TABLES;i++)
134 
135  for(i=0; i<NB_RL_TABLES; i++){
136  int level;
137  for (level = 1; level <= MAX_LEVEL; level++) {
138  int run;
139  for(run=0; run<=MAX_RUN; run++){
140  int last;
141  for(last=0; last<2; last++){
142  rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[ i], last, run, level, 0);
143  }
144  }
145  }
146  }
147  }
148 }
149 
151 {
152  int i;
153  int best = 0, best_size = INT_MAX;
154  int chroma_best = 0, best_chroma_size = INT_MAX;
155 
156  for(i=0; i<3; i++){
157  int level;
158  int chroma_size=0;
159  int size=0;
160 
161  if(i>0){// ;)
162  size++;
163  chroma_size++;
164  }
165  for(level=0; level<=MAX_LEVEL; level++){
166  int run;
167  for(run=0; run<=MAX_RUN; run++){
168  int last;
169  const int last_size= size + chroma_size;
170  for(last=0; last<2; last++){
171  int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
172  int intra_luma_count = s->ac_stats[1][0][level][run][last];
173  int intra_chroma_count= s->ac_stats[1][1][level][run][last];
174 
176  size += intra_luma_count *rl_length[i ][level][run][last];
177  chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
178  }else{
179  size+= intra_luma_count *rl_length[i ][level][run][last]
180  +intra_chroma_count*rl_length[i+3][level][run][last]
181  +inter_count *rl_length[i+3][level][run][last];
182  }
183  }
184  if(last_size == size+chroma_size) break;
185  }
186  }
187  if(size<best_size){
188  best_size= size;
189  best= i;
190  }
191  if(chroma_size<best_chroma_size){
192  best_chroma_size= chroma_size;
193  chroma_best= i;
194  }
195  }
196 
197  if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best;
198 
199  memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
200 
201  s->rl_table_index = best;
202  s->rl_chroma_table_index= chroma_best;
203 
204  if(s->pict_type != s->last_non_b_pict_type){
205  s->rl_table_index= 2;
207  s->rl_chroma_table_index= 1;
208  else
209  s->rl_chroma_table_index= 2;
210  }
211 
212 }
213 
214 /* write MSMPEG4 compatible frame header */
216 {
217  find_best_tables(s);
218 
220  put_bits(&s->pb, 2, s->pict_type - 1);
221 
222  put_bits(&s->pb, 5, s->qscale);
223  if(s->msmpeg4_version<=2){
224  s->rl_table_index = 2;
225  s->rl_chroma_table_index = 2;
226  }
227 
228  s->dc_table_index = 1;
229  s->mv_table_index = 1; /* only if P frame */
230  s->use_skip_mb_code = 1; /* only if P frame */
231  s->per_mb_rl_table = 0;
232  if(s->msmpeg4_version==4)
233  s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P);
234  av_dlog(s, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
235  s->inter_intra_pred, s->width, s->height);
236 
237  if (s->pict_type == AV_PICTURE_TYPE_I) {
238  s->slice_height= s->mb_height/1;
239  put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
240 
241  if(s->msmpeg4_version==4){
243  if(s->bit_rate>MBAC_BITRATE)
244  put_bits(&s->pb, 1, s->per_mb_rl_table);
245  }
246 
247  if(s->msmpeg4_version>2){
248  if(!s->per_mb_rl_table){
251  }
252 
253  put_bits(&s->pb, 1, s->dc_table_index);
254  }
255  } else {
256  put_bits(&s->pb, 1, s->use_skip_mb_code);
257 
258  if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
259  put_bits(&s->pb, 1, s->per_mb_rl_table);
260 
261  if(s->msmpeg4_version>2){
262  if(!s->per_mb_rl_table)
264 
265  put_bits(&s->pb, 1, s->dc_table_index);
266 
267  put_bits(&s->pb, 1, s->mv_table_index);
268  }
269  }
270 
271  s->esc3_level_length= 0;
272  s->esc3_run_length= 0;
273 }
274 
276 {
277  unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1);
278  put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
279 
280  put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
281 
282  if(s->msmpeg4_version>=3)
283  put_bits(&s->pb, 1, s->flipflop_rounding);
284  else
286 }
287 
289  int mx, int my)
290 {
291  int code;
292  MVTable *mv;
293 
294  /* modulo encoding */
295  /* WARNING : you cannot reach all the MVs even with the modulo
296  encoding. This is a somewhat strange compromise they took !!! */
297  if (mx <= -64)
298  mx += 64;
299  else if (mx >= 64)
300  mx -= 64;
301  if (my <= -64)
302  my += 64;
303  else if (my >= 64)
304  my -= 64;
305 
306  mx += 32;
307  my += 32;
308 #if 0
309  if ((unsigned)mx >= 64 ||
310  (unsigned)my >= 64)
311  av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
312 #endif
313  mv = &ff_mv_tables[s->mv_table_index];
314 
315  code = mv->table_mv_index[(mx << 6) | my];
316  put_bits(&s->pb,
317  mv->table_mv_bits[code],
318  mv->table_mv_code[code]);
319  if (code == mv->n) {
320  /* escape : code literally */
321  put_bits(&s->pb, 6, mx);
322  put_bits(&s->pb, 6, my);
323  }
324 }
325 
327  if (s->mb_x == 0) {
328  if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
329  if(s->msmpeg4_version < 4){
331  }
332  s->first_slice_line = 1;
333  } else {
334  s->first_slice_line = 0;
335  }
336  }
337 }
338 
340 {
341  int range, bit_size, sign, code, bits;
342 
343  if (val == 0) {
344  /* zero vector */
345  code = 0;
346  put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]);
347  } else {
348  bit_size = s->f_code - 1;
349  range = 1 << bit_size;
350  if (val <= -64)
351  val += 64;
352  else if (val >= 64)
353  val -= 64;
354 
355  if (val >= 0) {
356  sign = 0;
357  } else {
358  val = -val;
359  sign = 1;
360  }
361  val--;
362  code = (val >> bit_size) + 1;
363  bits = val & (range - 1);
364 
365  put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
366  if (bit_size > 0) {
367  put_bits(&s->pb, bit_size, bits);
368  }
369  }
370 }
371 
373  int16_t block[6][64],
374  int motion_x, int motion_y)
375 {
376  int cbp, coded_cbp, i;
377  int pred_x, pred_y;
378  uint8_t *coded_block;
379 
381 
382  if (!s->mb_intra) {
383  /* compute cbp */
384  cbp = 0;
385  for (i = 0; i < 6; i++) {
386  if (s->block_last_index[i] >= 0)
387  cbp |= 1 << (5 - i);
388  }
389  if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
390  /* skip macroblock */
391  put_bits(&s->pb, 1, 1);
392  s->last_bits++;
393  s->misc_bits++;
394  s->skip_count++;
395 
396  return;
397  }
398  if (s->use_skip_mb_code)
399  put_bits(&s->pb, 1, 0); /* mb coded */
400 
401  if(s->msmpeg4_version<=2){
402  put_bits(&s->pb,
403  ff_v2_mb_type[cbp&3][1],
404  ff_v2_mb_type[cbp&3][0]);
405  if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
406  else coded_cbp= cbp;
407 
408  put_bits(&s->pb,
409  ff_h263_cbpy_tab[coded_cbp>>2][1],
410  ff_h263_cbpy_tab[coded_cbp>>2][0]);
411 
412  s->misc_bits += get_bits_diff(s);
413 
414  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
415  msmpeg4v2_encode_motion(s, motion_x - pred_x);
416  msmpeg4v2_encode_motion(s, motion_y - pred_y);
417  }else{
418  put_bits(&s->pb,
419  ff_table_mb_non_intra[cbp + 64][1],
420  ff_table_mb_non_intra[cbp + 64][0]);
421 
422  s->misc_bits += get_bits_diff(s);
423 
424  /* motion vector */
425  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
426  ff_msmpeg4_encode_motion(s, motion_x - pred_x,
427  motion_y - pred_y);
428  }
429 
430  s->mv_bits += get_bits_diff(s);
431 
432  for (i = 0; i < 6; i++) {
433  ff_msmpeg4_encode_block(s, block[i], i);
434  }
435  s->p_tex_bits += get_bits_diff(s);
436  } else {
437  /* compute cbp */
438  cbp = 0;
439  coded_cbp = 0;
440  for (i = 0; i < 6; i++) {
441  int val, pred;
442  val = (s->block_last_index[i] >= 1);
443  cbp |= val << (5 - i);
444  if (i < 4) {
445  /* predict value for close blocks only for luma */
446  pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
447  *coded_block = val;
448  val = val ^ pred;
449  }
450  coded_cbp |= val << (5 - i);
451  }
452 
453  if(s->msmpeg4_version<=2){
454  if (s->pict_type == AV_PICTURE_TYPE_I) {
455  put_bits(&s->pb,
456  ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
457  } else {
458  if (s->use_skip_mb_code)
459  put_bits(&s->pb, 1, 0); /* mb coded */
460  put_bits(&s->pb,
461  ff_v2_mb_type[(cbp&3) + 4][1],
462  ff_v2_mb_type[(cbp&3) + 4][0]);
463  }
464  put_bits(&s->pb, 1, 0); /* no AC prediction yet */
465  put_bits(&s->pb,
466  ff_h263_cbpy_tab[cbp>>2][1],
467  ff_h263_cbpy_tab[cbp>>2][0]);
468  }else{
469  if (s->pict_type == AV_PICTURE_TYPE_I) {
470  put_bits(&s->pb,
471  ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
472  } else {
473  if (s->use_skip_mb_code)
474  put_bits(&s->pb, 1, 0); /* mb coded */
475  put_bits(&s->pb,
476  ff_table_mb_non_intra[cbp][1],
477  ff_table_mb_non_intra[cbp][0]);
478  }
479  put_bits(&s->pb, 1, 0); /* no AC prediction yet */
480  if(s->inter_intra_pred){
481  s->h263_aic_dir=0;
483  }
484  }
485  s->misc_bits += get_bits_diff(s);
486 
487  for (i = 0; i < 6; i++) {
488  ff_msmpeg4_encode_block(s, block[i], i);
489  }
490  s->i_tex_bits += get_bits_diff(s);
491  s->i_count++;
492  }
493 }
494 
495 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
496 {
497  int sign, code;
498  int pred, av_uninit(extquant);
499  int extrabits = 0;
500 
501  int16_t *dc_val;
502  pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
503 
504  /* update predictor */
505  if (n < 4) {
506  *dc_val = level * s->y_dc_scale;
507  } else {
508  *dc_val = level * s->c_dc_scale;
509  }
510 
511  /* do the prediction */
512  level -= pred;
513 
514  if(s->msmpeg4_version<=2){
515  if (n < 4) {
516  put_bits(&s->pb,
517  ff_v2_dc_lum_table[level + 256][1],
518  ff_v2_dc_lum_table[level + 256][0]);
519  }else{
520  put_bits(&s->pb,
521  ff_v2_dc_chroma_table[level + 256][1],
522  ff_v2_dc_chroma_table[level + 256][0]);
523  }
524  }else{
525  sign = 0;
526  if (level < 0) {
527  level = -level;
528  sign = 1;
529  }
530  code = level;
531  if (code > DC_MAX)
532  code = DC_MAX;
533  else if( s->msmpeg4_version>=6 ) {
534  if( s->qscale == 1 ) {
535  extquant = (level + 3) & 0x3;
536  code = ((level+3)>>2);
537  } else if( s->qscale == 2 ) {
538  extquant = (level + 1) & 0x1;
539  code = ((level+1)>>1);
540  }
541  }
542 
543  if (s->dc_table_index == 0) {
544  if (n < 4) {
545  put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
546  } else {
547  put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
548  }
549  } else {
550  if (n < 4) {
551  put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
552  } else {
553  put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
554  }
555  }
556 
557  if(s->msmpeg4_version>=6 && s->qscale<=2)
558  extrabits = 3 - s->qscale;
559 
560  if (code == DC_MAX)
561  put_bits(&s->pb, 8 + extrabits, level);
562  else if(extrabits > 0)//== VC1 && s->qscale<=2
563  put_bits(&s->pb, extrabits, extquant);
564 
565  if (level != 0) {
566  put_bits(&s->pb, 1, sign);
567  }
568  }
569 }
570 
571 /* Encoding of a block. Very similar to MPEG4 except for a different
572  escape coding (same as H263) and more vlc tables.
573  */
575 {
576  int level, run, last, i, j, last_index;
577  int last_non_zero, sign, slevel;
578  int code, run_diff, dc_pred_dir;
579  const RLTable *rl;
580  const uint8_t *scantable;
581 
582  if (s->mb_intra) {
583  msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
584  i = 1;
585  if (n < 4) {
586  rl = &ff_rl_table[s->rl_table_index];
587  } else {
588  rl = &ff_rl_table[3 + s->rl_chroma_table_index];
589  }
590  run_diff = s->msmpeg4_version>=4;
591  scantable= s->intra_scantable.permutated;
592  } else {
593  i = 0;
594  rl = &ff_rl_table[3 + s->rl_table_index];
595  if(s->msmpeg4_version<=2)
596  run_diff = 0;
597  else
598  run_diff = 1;
599  scantable= s->inter_scantable.permutated;
600  }
601 
602  /* recalculate block_last_index for M$ wmv1 */
603  if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
604  for(last_index=63; last_index>=0; last_index--){
605  if(block[scantable[last_index]]) break;
606  }
607  s->block_last_index[n]= last_index;
608  }else
609  last_index = s->block_last_index[n];
610  /* AC coefs */
611  last_non_zero = i - 1;
612  for (; i <= last_index; i++) {
613  j = scantable[i];
614  level = block[j];
615  if (level) {
616  run = i - last_non_zero - 1;
617  last = (i == last_index);
618  sign = 0;
619  slevel = level;
620  if (level < 0) {
621  sign = 1;
622  level = -level;
623  }
624 
625  if(level<=MAX_LEVEL && run<=MAX_RUN){
626  s->ac_stats[s->mb_intra][n>3][level][run][last]++;
627  }
628 
629  s->ac_stats[s->mb_intra][n > 3][40][63][0]++; //esc3 like
630 
631  code = get_rl_index(rl, last, run, level);
632  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
633  if (code == rl->n) {
634  int level1, run1;
635 
636  level1 = level - rl->max_level[last][run];
637  if (level1 < 1)
638  goto esc2;
639  code = get_rl_index(rl, last, run, level1);
640  if (code == rl->n) {
641  esc2:
642  put_bits(&s->pb, 1, 0);
643  if (level > MAX_LEVEL)
644  goto esc3;
645  run1 = run - rl->max_run[last][level] - run_diff;
646  if (run1 < 0)
647  goto esc3;
648  code = get_rl_index(rl, last, run1+1, level);
649  if (s->msmpeg4_version == 4 && code == rl->n)
650  goto esc3;
651  code = get_rl_index(rl, last, run1, level);
652  if (code == rl->n) {
653  esc3:
654  /* third escape */
655  put_bits(&s->pb, 1, 0);
656  put_bits(&s->pb, 1, last);
657  if(s->msmpeg4_version>=4){
658  if(s->esc3_level_length==0){
659  s->esc3_level_length=8;
660  s->esc3_run_length= 6;
661  //ESCLVLSZ + ESCRUNSZ
662  if(s->qscale<8)
663  put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
664  else
665  put_bits(&s->pb, 8, 3);
666  }
667  put_bits(&s->pb, s->esc3_run_length, run);
668  put_bits(&s->pb, 1, sign);
669  put_bits(&s->pb, s->esc3_level_length, level);
670  }else{
671  put_bits(&s->pb, 6, run);
672  put_sbits(&s->pb, 8, slevel);
673  }
674  } else {
675  /* second escape */
676  put_bits(&s->pb, 1, 1);
677  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
678  put_bits(&s->pb, 1, sign);
679  }
680  } else {
681  /* first escape */
682  put_bits(&s->pb, 1, 1);
683  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
684  put_bits(&s->pb, 1, sign);
685  }
686  } else {
687  put_bits(&s->pb, 1, sign);
688  }
689  last_non_zero = i;
690  }
691  }
692 }