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