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