FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
msmpeg4.c
Go to the documentation of this file.
1 /*
2  * MSMPEG4 backend for encoder and decoder
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 backend for encoder and decoder
28  */
29 
30 #include "avcodec.h"
31 #include "dsputil.h"
32 #include "mpegvideo.h"
33 #include "msmpeg4.h"
34 #include "libavutil/x86/asm.h"
35 #include "h263.h"
36 #include "mpeg4video.h"
37 #include "msmpeg4data.h"
38 #include "vc1data.h"
39 #include "libavutil/imgutils.h"
40 
41 /*
42  * You can also call this codec : MPEG4 with a twist !
43  *
44  * TODO:
45  * - (encoding) select best mv table (two choices)
46  * - (encoding) select best vlc/dc table
47  */
48 //#define DEBUG
49 
50 #define DC_VLC_BITS 9
51 #define V2_INTRA_CBPC_VLC_BITS 3
52 #define V2_MB_TYPE_VLC_BITS 7
53 #define MV_VLC_BITS 9
54 #define V2_MV_VLC_BITS 9
55 #define TEX_VLC_BITS 9
56 
57 #define DEFAULT_INTER_INDEX 3
58 
59 /* This table is practically identical to the one from h263
60  * except that it is inverted. */
62 {
63  int level, uni_code, uni_len;
64 
65  if(ff_v2_dc_chroma_table[255 + 256][1])
66  return;
67 
68  for(level=-256; level<256; level++){
69  int size, v, l;
70  /* find number of bits */
71  size = 0;
72  v = abs(level);
73  while (v) {
74  v >>= 1;
75  size++;
76  }
77 
78  if (level < 0)
79  l= (-level) ^ ((1 << size) - 1);
80  else
81  l= level;
82 
83  /* luminance h263 */
84  uni_code= ff_mpeg4_DCtab_lum[size][0];
85  uni_len = ff_mpeg4_DCtab_lum[size][1];
86  uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
87 
88  if (size > 0) {
89  uni_code<<=size; uni_code|=l;
90  uni_len+=size;
91  if (size > 8){
92  uni_code<<=1; uni_code|=1;
93  uni_len++;
94  }
95  }
96  ff_v2_dc_lum_table[level + 256][0] = uni_code;
97  ff_v2_dc_lum_table[level + 256][1] = uni_len;
98 
99  /* chrominance h263 */
100  uni_code= ff_mpeg4_DCtab_chrom[size][0];
101  uni_len = ff_mpeg4_DCtab_chrom[size][1];
102  uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
103 
104  if (size > 0) {
105  uni_code<<=size; uni_code|=l;
106  uni_len+=size;
107  if (size > 8){
108  uni_code<<=1; uni_code|=1;
109  uni_len++;
110  }
111  }
112  ff_v2_dc_chroma_table[level + 256][0] = uni_code;
113  ff_v2_dc_chroma_table[level + 256][1] = uni_len;
114 
115  }
116 }
117 
119 {
120  switch(s->msmpeg4_version){
121  case 1:
122  case 2:
123  s->y_dc_scale_table=
125  break;
126  case 3:
127  if(s->workaround_bugs){
130  } else{
133  }
134  break;
135  case 4:
136  case 5:
139  break;
140 #if CONFIG_VC1_DECODER
141  case 6:
144  break;
145 #endif
146 
147  }
148 
149 
150  if(s->msmpeg4_version>=4){
155  }
156  //Note the default tables are set in common_init in mpegvideo.c
157 
159 }
160 
161 /* predict coded block */
162 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
163 {
164  int xy, wrap, pred, a, b, c;
165 
166  xy = s->block_index[n];
167  wrap = s->b8_stride;
168 
169  /* B C
170  * A X
171  */
172  a = s->coded_block[xy - 1 ];
173  b = s->coded_block[xy - 1 - wrap];
174  c = s->coded_block[xy - wrap];
175 
176  if (b == c) {
177  pred = a;
178  } else {
179  pred = c;
180  }
181 
182  /* store value */
183  *coded_block_ptr = &s->coded_block[xy];
184 
185  return pred;
186 }
187 
188 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
189  int32_t **dc_val_ptr)
190 {
191  int i;
192 
193  if (n < 4) {
194  i= 0;
195  } else {
196  i= n-3;
197  }
198 
199  *dc_val_ptr= &s->last_dc[i];
200  return s->last_dc[i];
201 }
202 
203 static int get_dc(uint8_t *src, int stride, int scale)
204 {
205  int y;
206  int sum=0;
207  for(y=0; y<8; y++){
208  int x;
209  for(x=0; x<8; x++){
210  sum+=src[x + y*stride];
211  }
212  }
213  return FASTDIV((sum + (scale>>1)), scale);
214 }
215 
216 /* dir = 0: left, dir = 1: top prediction */
218  int16_t **dc_val_ptr, int *dir_ptr)
219 {
220  int a, b, c, wrap, pred, scale;
221  int16_t *dc_val;
222 
223  /* find prediction */
224  if (n < 4) {
225  scale = s->y_dc_scale;
226  } else {
227  scale = s->c_dc_scale;
228  }
229 
230  wrap = s->block_wrap[n];
231  dc_val= s->dc_val[0] + s->block_index[n];
232 
233  /* B C
234  * A X
235  */
236  a = dc_val[ - 1];
237  b = dc_val[ - 1 - wrap];
238  c = dc_val[ - wrap];
239 
240  if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
241  b=c=1024;
242  }
243 
244  /* XXX: the following solution consumes divisions, but it does not
245  necessitate to modify mpegvideo.c. The problem comes from the
246  fact they decided to store the quantized DC (which would lead
247  to problems if Q could vary !) */
248 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
249  __asm__ volatile(
250  "movl %3, %%eax \n\t"
251  "shrl $1, %%eax \n\t"
252  "addl %%eax, %2 \n\t"
253  "addl %%eax, %1 \n\t"
254  "addl %0, %%eax \n\t"
255  "mull %4 \n\t"
256  "movl %%edx, %0 \n\t"
257  "movl %1, %%eax \n\t"
258  "mull %4 \n\t"
259  "movl %%edx, %1 \n\t"
260  "movl %2, %%eax \n\t"
261  "mull %4 \n\t"
262  "movl %%edx, %2 \n\t"
263  : "+b" (a), "+c" (b), "+D" (c)
264  : "g" (scale), "S" (ff_inverse[scale])
265  : "%eax", "%edx"
266  );
267 #else
268  /* #elif ARCH_ALPHA */
269  /* Divisions are extremely costly on Alpha; optimize the most
270  common case. But they are costly everywhere...
271  */
272  if (scale == 8) {
273  a = (a + (8 >> 1)) / 8;
274  b = (b + (8 >> 1)) / 8;
275  c = (c + (8 >> 1)) / 8;
276  } else {
277  a = FASTDIV((a + (scale >> 1)), scale);
278  b = FASTDIV((b + (scale >> 1)), scale);
279  c = FASTDIV((c + (scale >> 1)), scale);
280  }
281 #endif
282  /* XXX: WARNING: they did not choose the same test as MPEG4. This
283  is very important ! */
284  if(s->msmpeg4_version>3){
285  if(s->inter_intra_pred){
286  uint8_t *dest;
287  int wrap;
288 
289  if(n==1){
290  pred=a;
291  *dir_ptr = 0;
292  }else if(n==2){
293  pred=c;
294  *dir_ptr = 1;
295  }else if(n==3){
296  if (abs(a - b) < abs(b - c)) {
297  pred = c;
298  *dir_ptr = 1;
299  } else {
300  pred = a;
301  *dir_ptr = 0;
302  }
303  }else{
304  if(n<4){
305  wrap= s->linesize;
306  dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
307  }else{
308  wrap= s->uvlinesize;
309  dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
310  }
311  if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
312  else a= get_dc(dest-8, wrap, scale*8);
313  if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
314  else c= get_dc(dest-8*wrap, wrap, scale*8);
315 
316  if (s->h263_aic_dir==0) {
317  pred= a;
318  *dir_ptr = 0;
319  }else if (s->h263_aic_dir==1) {
320  if(n==0){
321  pred= c;
322  *dir_ptr = 1;
323  }else{
324  pred= a;
325  *dir_ptr = 0;
326  }
327  }else if (s->h263_aic_dir==2) {
328  if(n==0){
329  pred= a;
330  *dir_ptr = 0;
331  }else{
332  pred= c;
333  *dir_ptr = 1;
334  }
335  } else {
336  pred= c;
337  *dir_ptr = 1;
338  }
339  }
340  }else{
341  if (abs(a - b) < abs(b - c)) {
342  pred = c;
343  *dir_ptr = 1;
344  } else {
345  pred = a;
346  *dir_ptr = 0;
347  }
348  }
349  }else{
350  if (abs(a - b) <= abs(b - c)) {
351  pred = c;
352  *dir_ptr = 1;
353  } else {
354  pred = a;
355  *dir_ptr = 0;
356  }
357  }
358 
359  /* update predictor */
360  *dc_val_ptr = &dc_val[0];
361  return pred;
362 }
363 
364 /****************************************/
365 /* decoding stuff */
366 
372 static VLC v2_mv_vlc;
374 
375 /* This is identical to h263 except that its range is multiplied by 2. */
376 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
377 {
378  int code, val, sign, shift;
379 
380  code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
381  av_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
382  if (code < 0)
383  return 0xffff;
384 
385  if (code == 0)
386  return pred;
387  sign = get_bits1(&s->gb);
388  shift = f_code - 1;
389  val = code;
390  if (shift) {
391  val = (val - 1) << shift;
392  val |= get_bits(&s->gb, shift);
393  val++;
394  }
395  if (sign)
396  val = -val;
397 
398  val += pred;
399  if (val <= -64)
400  val += 64;
401  else if (val >= 64)
402  val -= 64;
403 
404  return val;
405 }
406 
408 {
409  int cbp, code, i;
410  uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
411 
412  if (s->pict_type == AV_PICTURE_TYPE_P) {
413  if (s->use_skip_mb_code) {
414  if (get_bits1(&s->gb)) {
415  /* skip mb */
416  s->mb_intra = 0;
417  for(i=0;i<6;i++)
418  s->block_last_index[i] = -1;
419  s->mv_dir = MV_DIR_FORWARD;
420  s->mv_type = MV_TYPE_16X16;
421  s->mv[0][0][0] = 0;
422  s->mv[0][0][1] = 0;
423  s->mb_skipped = 1;
424  *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
425  return 0;
426  }
427  }
428 
429  if(s->msmpeg4_version==2)
430  code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
431  else
433  if(code<0 || code>7){
434  av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
435  return -1;
436  }
437 
438  s->mb_intra = code >>2;
439 
440  cbp = code & 0x3;
441  } else {
442  s->mb_intra = 1;
443  if(s->msmpeg4_version==2)
444  cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
445  else
447  if(cbp<0 || cbp>3){
448  av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
449  return -1;
450  }
451  }
452 
453  if (!s->mb_intra) {
454  int mx, my, cbpy;
455 
457  if(cbpy<0){
458  av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
459  return -1;
460  }
461 
462  cbp|= cbpy<<2;
463  if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
464 
465  ff_h263_pred_motion(s, 0, 0, &mx, &my);
466  mx= msmpeg4v2_decode_motion(s, mx, 1);
467  my= msmpeg4v2_decode_motion(s, my, 1);
468 
469  s->mv_dir = MV_DIR_FORWARD;
470  s->mv_type = MV_TYPE_16X16;
471  s->mv[0][0][0] = mx;
472  s->mv[0][0][1] = my;
473  *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
474  } else {
475  if(s->msmpeg4_version==2){
476  s->ac_pred = get_bits1(&s->gb);
477  cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
478  } else{
479  s->ac_pred = 0;
480  cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
481  if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
482  }
483  *mb_type_ptr = MB_TYPE_INTRA;
484  }
485 
486  s->dsp.clear_blocks(s->block[0]);
487  for (i = 0; i < 6; i++) {
488  if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
489  {
490  av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
491  return -1;
492  }
493  }
494  return 0;
495 }
496 
498 {
499  int cbp, code, i;
500  uint8_t *coded_val;
501  uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
502 
503  if (s->pict_type == AV_PICTURE_TYPE_P) {
504  if (s->use_skip_mb_code) {
505  if (get_bits1(&s->gb)) {
506  /* skip mb */
507  s->mb_intra = 0;
508  for(i=0;i<6;i++)
509  s->block_last_index[i] = -1;
510  s->mv_dir = MV_DIR_FORWARD;
511  s->mv_type = MV_TYPE_16X16;
512  s->mv[0][0][0] = 0;
513  s->mv[0][0][1] = 0;
514  s->mb_skipped = 1;
515  *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
516 
517  return 0;
518  }
519  }
520 
521  code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
522  if (code < 0)
523  return -1;
524  //s->mb_intra = (code & 0x40) ? 0 : 1;
525  s->mb_intra = (~code & 0x40) >> 6;
526 
527  cbp = code & 0x3f;
528  } else {
529  s->mb_intra = 1;
531  if (code < 0)
532  return -1;
533  /* predict coded block pattern */
534  cbp = 0;
535  for(i=0;i<6;i++) {
536  int val = ((code >> (5 - i)) & 1);
537  if (i < 4) {
538  int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
539  val = val ^ pred;
540  *coded_val = val;
541  }
542  cbp |= val << (5 - i);
543  }
544  }
545 
546  if (!s->mb_intra) {
547  int mx, my;
548  if(s->per_mb_rl_table && cbp){
549  s->rl_table_index = decode012(&s->gb);
551  }
552  ff_h263_pred_motion(s, 0, 0, &mx, &my);
553  if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
554  return -1;
555  s->mv_dir = MV_DIR_FORWARD;
556  s->mv_type = MV_TYPE_16X16;
557  s->mv[0][0][0] = mx;
558  s->mv[0][0][1] = my;
559  *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
560  } else {
561  av_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
562  ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
563  show_bits(&s->gb, 24));
564  s->ac_pred = get_bits1(&s->gb);
565  *mb_type_ptr = MB_TYPE_INTRA;
566  if(s->inter_intra_pred){
567  s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
568  av_dlog(s, "%d%d %d %d/",
569  s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
570  }
571  if(s->per_mb_rl_table && cbp){
572  s->rl_table_index = decode012(&s->gb);
574  }
575  }
576 
577  s->dsp.clear_blocks(s->block[0]);
578  for (i = 0; i < 6; i++) {
579  if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
580  {
581  av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
582  return -1;
583  }
584  }
585 
586  return 0;
587 }
588 
589 /* init all vlc decoding tables */
591 {
592  MpegEncContext *s = avctx->priv_data;
593  static volatile int done = 0;
594  int i, ret;
595  MVTable *mv;
596 
597  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
598  return ret;
599 
600  if (ff_h263_decode_init(avctx) < 0)
601  return -1;
602 
604 
605  if (!done) {
606  for(i=0;i<NB_RL_TABLES;i++) {
608  }
609  INIT_VLC_RL(ff_rl_table[0], 642);
610  INIT_VLC_RL(ff_rl_table[1], 1104);
611  INIT_VLC_RL(ff_rl_table[2], 554);
612  INIT_VLC_RL(ff_rl_table[3], 940);
613  INIT_VLC_RL(ff_rl_table[4], 962);
614  INIT_VLC_RL(ff_rl_table[5], 554);
615 
616  mv = &ff_mv_tables[0];
617  INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
618  mv->table_mv_bits, 1, 1,
619  mv->table_mv_code, 2, 2, 3714);
620  mv = &ff_mv_tables[1];
621  INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
622  mv->table_mv_bits, 1, 1,
623  mv->table_mv_code, 2, 2, 2694);
624 
626  &ff_table0_dc_lum[0][1], 8, 4,
627  &ff_table0_dc_lum[0][0], 8, 4, 1158);
629  &ff_table0_dc_chroma[0][1], 8, 4,
630  &ff_table0_dc_chroma[0][0], 8, 4, 1118);
632  &ff_table1_dc_lum[0][1], 8, 4,
633  &ff_table1_dc_lum[0][0], 8, 4, 1476);
635  &ff_table1_dc_chroma[0][1], 8, 4,
636  &ff_table1_dc_chroma[0][0], 8, 4, 1216);
637 
638  INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
639  &ff_v2_dc_lum_table[0][1], 8, 4,
640  &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
641  INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
642  &ff_v2_dc_chroma_table[0][1], 8, 4,
643  &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
644 
645  INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
646  &ff_v2_intra_cbpc[0][1], 2, 1,
647  &ff_v2_intra_cbpc[0][0], 2, 1, 8);
648  INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
649  &ff_v2_mb_type[0][1], 2, 1,
650  &ff_v2_mb_type[0][0], 2, 1, 128);
651  INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
652  &ff_mvtab[0][1], 2, 1,
653  &ff_mvtab[0][0], 2, 1, 538);
654 
655  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
656  &ff_wmv2_inter_table[0][0][1], 8, 4,
657  &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
658  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
659  &ff_wmv2_inter_table[1][0][1], 8, 4,
660  &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
661  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
662  &ff_wmv2_inter_table[2][0][1], 8, 4,
663  &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
664  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
665  &ff_wmv2_inter_table[3][0][1], 8, 4,
666  &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
667 
669  &ff_msmp4_mb_i_table[0][1], 4, 2,
670  &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
671 
672  INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
673  &ff_table_inter_intra[0][1], 2, 1,
674  &ff_table_inter_intra[0][0], 2, 1, 8);
675  done = 1;
676  }
677 
678  switch(s->msmpeg4_version){
679  case 1:
680  case 2:
682  break;
683  case 3:
684  case 4:
686  break;
687  case 5:
688  if (CONFIG_WMV2_DECODER)
690  case 6:
691  //FIXME + TODO VC1 decode mb
692  break;
693  }
694 
695  s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
696 
697  return 0;
698 }
699 
701 {
702  int code;
703 
704  if(s->msmpeg4_version==1){
705  int start_code = get_bits_long(&s->gb, 32);
706  if(start_code!=0x00000100){
707  av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
708  return -1;
709  }
710 
711  skip_bits(&s->gb, 5); // frame number */
712  }
713 
714  s->pict_type = get_bits(&s->gb, 2) + 1;
715  if (s->pict_type != AV_PICTURE_TYPE_I &&
717  av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
718  return -1;
719  }
720 #if 0
721 {
722  static int had_i=0;
723  if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
724  if(!had_i) return -1;
725 }
726 #endif
727  s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
728  if(s->qscale==0){
729  av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
730  return -1;
731  }
732 
733  if (s->pict_type == AV_PICTURE_TYPE_I) {
734  code = get_bits(&s->gb, 5);
735  if(s->msmpeg4_version==1){
736  if(code==0 || code>s->mb_height){
737  av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
738  return -1;
739  }
740 
741  s->slice_height = code;
742  }else{
743  /* 0x17: one slice, 0x18: two slices, ... */
744  if (code < 0x17){
745  av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
746  return -1;
747  }
748 
749  s->slice_height = s->mb_height / (code - 0x16);
750  }
751 
752  switch(s->msmpeg4_version){
753  case 1:
754  case 2:
755  s->rl_chroma_table_index = 2;
756  s->rl_table_index = 2;
757 
758  s->dc_table_index = 0; //not used
759  break;
760  case 3:
762  s->rl_table_index = decode012(&s->gb);
763 
764  s->dc_table_index = get_bits1(&s->gb);
765  break;
766  case 4:
767  ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
768 
770  else s->per_mb_rl_table= 0;
771 
772  if(!s->per_mb_rl_table){
774  s->rl_table_index = decode012(&s->gb);
775  }
776 
777  s->dc_table_index = get_bits1(&s->gb);
778  s->inter_intra_pred= 0;
779  break;
780  }
781  s->no_rounding = 1;
783  av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
784  s->qscale,
786  s->rl_table_index,
787  s->dc_table_index,
788  s->per_mb_rl_table,
789  s->slice_height);
790  } else {
791  switch(s->msmpeg4_version){
792  case 1:
793  case 2:
794  if(s->msmpeg4_version==1)
795  s->use_skip_mb_code = 1;
796  else
797  s->use_skip_mb_code = get_bits1(&s->gb);
798  s->rl_table_index = 2;
800  s->dc_table_index = 0; //not used
801  s->mv_table_index = 0;
802  break;
803  case 3:
804  s->use_skip_mb_code = get_bits1(&s->gb);
805  s->rl_table_index = decode012(&s->gb);
807 
808  s->dc_table_index = get_bits1(&s->gb);
809 
810  s->mv_table_index = get_bits1(&s->gb);
811  break;
812  case 4:
813  s->use_skip_mb_code = get_bits1(&s->gb);
814 
816  else s->per_mb_rl_table= 0;
817 
818  if(!s->per_mb_rl_table){
819  s->rl_table_index = decode012(&s->gb);
821  }
822 
823  s->dc_table_index = get_bits1(&s->gb);
824 
825  s->mv_table_index = get_bits1(&s->gb);
826  s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
827  break;
828  }
829 
831  av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
832  s->use_skip_mb_code,
833  s->rl_table_index,
835  s->dc_table_index,
836  s->mv_table_index,
837  s->per_mb_rl_table,
838  s->qscale);
839 
840  if(s->flipflop_rounding){
841  s->no_rounding ^= 1;
842  }else{
843  s->no_rounding = 0;
844  }
845  }
846  av_dlog(s->avctx, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
847  s->inter_intra_pred, s->width, s->height);
848 
849  s->esc3_level_length= 0;
850  s->esc3_run_length= 0;
851 
852  return 0;
853 }
854 
856 {
857  int left= buf_size*8 - get_bits_count(&s->gb);
858  int length= s->msmpeg4_version>=3 ? 17 : 16;
859  /* the alt_bitstream reader could read over the end so we need to check it */
860  if(left>=length && left<length+8)
861  {
862  skip_bits(&s->gb, 5); /* fps */
863  s->bit_rate= get_bits(&s->gb, 11)*1024;
864  if(s->msmpeg4_version>=3)
865  s->flipflop_rounding= get_bits1(&s->gb);
866  else
867  s->flipflop_rounding= 0;
868  }
869  else if(left<length+8)
870  {
871  s->flipflop_rounding= 0;
872  if(s->msmpeg4_version != 2)
873  av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
874  }
875  else
876  {
877  av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
878  }
879 
880  return 0;
881 }
882 
883 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
884 {
885  int level, pred;
886 
887  if(s->msmpeg4_version<=2){
888  if (n < 4) {
889  level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
890  } else {
891  level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
892  }
893  if (level < 0)
894  return -1;
895  level-=256;
896  }else{ //FIXME optimize use unified tables & index
897  if (n < 4) {
899  } else {
901  }
902  if (level < 0){
903  av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
904  return -1;
905  }
906 
907  if (level == DC_MAX) {
908  level = get_bits(&s->gb, 8);
909  if (get_bits1(&s->gb))
910  level = -level;
911  } else if (level != 0) {
912  if (get_bits1(&s->gb))
913  level = -level;
914  }
915  }
916 
917  if(s->msmpeg4_version==1){
918  int32_t *dc_val;
919  pred = msmpeg4v1_pred_dc(s, n, &dc_val);
920  level += pred;
921 
922  /* update predictor */
923  *dc_val= level;
924  }else{
925  int16_t *dc_val;
926  pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
927  level += pred;
928 
929  /* update predictor */
930  if (n < 4) {
931  *dc_val = level * s->y_dc_scale;
932  } else {
933  *dc_val = level * s->c_dc_scale;
934  }
935  }
936 
937  return level;
938 }
939 
940 //#define ERROR_DETAILS
942  int n, int coded, const uint8_t *scan_table)
943 {
944  int level, i, last, run, run_diff;
945  int av_uninit(dc_pred_dir);
946  RLTable *rl;
947  RL_VLC_ELEM *rl_vlc;
948  int qmul, qadd;
949 
950  if (s->mb_intra) {
951  qmul=1;
952  qadd=0;
953 
954  /* DC coef */
955  level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
956 
957  if (level < 0){
958  av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
959  if(s->inter_intra_pred) level=0;
960  else return -1;
961  }
962  if (n < 4) {
963  rl = &ff_rl_table[s->rl_table_index];
964  if(level > 256*s->y_dc_scale){
965  av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
966  if(!s->inter_intra_pred) return -1;
967  }
968  } else {
969  rl = &ff_rl_table[3 + s->rl_chroma_table_index];
970  if(level > 256*s->c_dc_scale){
971  av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
972  if(!s->inter_intra_pred) return -1;
973  }
974  }
975  block[0] = level;
976 
977  run_diff = s->msmpeg4_version >= 4;
978  i = 0;
979  if (!coded) {
980  goto not_coded;
981  }
982  if (s->ac_pred) {
983  if (dc_pred_dir == 0)
984  scan_table = s->intra_v_scantable.permutated; /* left */
985  else
986  scan_table = s->intra_h_scantable.permutated; /* top */
987  } else {
988  scan_table = s->intra_scantable.permutated;
989  }
990  rl_vlc= rl->rl_vlc[0];
991  } else {
992  qmul = s->qscale << 1;
993  qadd = (s->qscale - 1) | 1;
994  i = -1;
995  rl = &ff_rl_table[3 + s->rl_table_index];
996 
997  if(s->msmpeg4_version==2)
998  run_diff = 0;
999  else
1000  run_diff = 1;
1001 
1002  if (!coded) {
1003  s->block_last_index[n] = i;
1004  return 0;
1005  }
1006  if(!scan_table)
1007  scan_table = s->inter_scantable.permutated;
1008  rl_vlc= rl->rl_vlc[s->qscale];
1009  }
1010  {
1011  OPEN_READER(re, &s->gb);
1012  for(;;) {
1013  UPDATE_CACHE(re, &s->gb);
1014  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1015  if (level==0) {
1016  int cache;
1017  cache= GET_CACHE(re, &s->gb);
1018  /* escape */
1019  if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
1020  if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
1021  /* third escape */
1022  if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
1023  UPDATE_CACHE(re, &s->gb);
1024  if(s->msmpeg4_version<=3){
1025  last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
1026  run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
1027  level= SHOW_SBITS(re, &s->gb, 8);
1028  SKIP_COUNTER(re, &s->gb, 1+6+8);
1029  }else{
1030  int sign;
1031  last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1032  if(!s->esc3_level_length){
1033  int ll;
1034  av_dlog(s->avctx, "ESC-3 %X at %d %d\n",
1035  show_bits(&s->gb, 24), s->mb_x, s->mb_y);
1036  if(s->qscale<8){
1037  ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
1038  if(ll==0){
1039  ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
1040  }
1041  }else{
1042  ll=2;
1043  while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
1044  ll++;
1045  SKIP_BITS(re, &s->gb, 1);
1046  }
1047  if(ll<8) SKIP_BITS(re, &s->gb, 1);
1048  }
1049 
1050  s->esc3_level_length= ll;
1051  s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
1052  UPDATE_CACHE(re, &s->gb);
1053  }
1054  run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1055  SKIP_BITS(re, &s->gb, s->esc3_run_length);
1056 
1057  sign= SHOW_UBITS(re, &s->gb, 1);
1058  SKIP_BITS(re, &s->gb, 1);
1059 
1060  level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1061  SKIP_BITS(re, &s->gb, s->esc3_level_length);
1062  if(sign) level= -level;
1063  }
1064 
1065 #if 0 // waste of time / this will detect very few errors
1066  {
1067  const int abs_level= FFABS(level);
1068  const int run1= run - rl->max_run[last][abs_level] - run_diff;
1069  if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1070  if(abs_level <= rl->max_level[last][run]){
1071  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1072  return DECODING_AC_LOST;
1073  }
1074  if(abs_level <= rl->max_level[last][run]*2){
1075  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1076  return DECODING_AC_LOST;
1077  }
1078  if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1079  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1080  return DECODING_AC_LOST;
1081  }
1082  }
1083  }
1084 #endif
1085  //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1086  if (level>0) level= level * qmul + qadd;
1087  else level= level * qmul - qadd;
1088 #if 0 // waste of time too :(
1089  if(level>2048 || level<-2048){
1090  av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
1091  return DECODING_AC_LOST;
1092  }
1093 #endif
1094  i+= run + 1;
1095  if(last) i+=192;
1096 #ifdef ERROR_DETAILS
1097  if(run==66)
1098  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
1099  else if((i>62 && i<192) || i>192+63)
1100  av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
1101 #endif
1102  } else {
1103  /* second escape */
1104  SKIP_BITS(re, &s->gb, 2);
1105  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1106  i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
1107  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1108  LAST_SKIP_BITS(re, &s->gb, 1);
1109 #ifdef ERROR_DETAILS
1110  if(run==66)
1111  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
1112  else if((i>62 && i<192) || i>192+63)
1113  av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
1114 #endif
1115  }
1116  } else {
1117  /* first escape */
1118  SKIP_BITS(re, &s->gb, 1);
1119  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1120  i+= run;
1121  level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1122  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1123  LAST_SKIP_BITS(re, &s->gb, 1);
1124 #ifdef ERROR_DETAILS
1125  if(run==66)
1126  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
1127  else if((i>62 && i<192) || i>192+63)
1128  av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
1129 #endif
1130  }
1131  } else {
1132  i+= run;
1133  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1134  LAST_SKIP_BITS(re, &s->gb, 1);
1135 #ifdef ERROR_DETAILS
1136  if(run==66)
1137  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
1138  else if((i>62 && i<192) || i>192+63)
1139  av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
1140 #endif
1141  }
1142  if (i > 62){
1143  i-= 192;
1144  if(i&(~63)){
1145  const int left= get_bits_left(&s->gb);
1146  if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
1147  av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
1148  i = 63;
1149  break;
1150  }else{
1151  av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1152  return -1;
1153  }
1154  }
1155 
1156  block[scan_table[i]] = level;
1157  break;
1158  }
1159 
1160  block[scan_table[i]] = level;
1161  }
1162  CLOSE_READER(re, &s->gb);
1163  }
1164  not_coded:
1165  if (s->mb_intra) {
1166  ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1167  if (s->ac_pred) {
1168  i = 63; /* XXX: not optimal */
1169  }
1170  }
1171  if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
1172  s->block_last_index[n] = i;
1173 
1174  return 0;
1175 }
1176 
1178  int *mx_ptr, int *my_ptr)
1179 {
1180  MVTable *mv;
1181  int code, mx, my;
1182 
1183  mv = &ff_mv_tables[s->mv_table_index];
1184 
1185  code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
1186  if (code < 0){
1187  av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
1188  return -1;
1189  }
1190  if (code == mv->n) {
1191  mx = get_bits(&s->gb, 6);
1192  my = get_bits(&s->gb, 6);
1193  } else {
1194  mx = mv->table_mvx[code];
1195  my = mv->table_mvy[code];
1196  }
1197 
1198  mx += *mx_ptr - 32;
1199  my += *my_ptr - 32;
1200  /* WARNING : they do not do exactly modulo encoding */
1201  if (mx <= -64)
1202  mx += 64;
1203  else if (mx >= 64)
1204  mx -= 64;
1205 
1206  if (my <= -64)
1207  my += 64;
1208  else if (my >= 64)
1209  my -= 64;
1210  *mx_ptr = mx;
1211  *my_ptr = my;
1212  return 0;
1213 }
1214 
1216  .name = "msmpeg4v1",
1217  .type = AVMEDIA_TYPE_VIDEO,
1218  .id = AV_CODEC_ID_MSMPEG4V1,
1219  .priv_data_size = sizeof(MpegEncContext),
1223  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1224  .max_lowres = 3,
1225  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
1226  .pix_fmts = ff_pixfmt_list_420,
1227 };
1228 
1230  .name = "msmpeg4v2",
1231  .type = AVMEDIA_TYPE_VIDEO,
1232  .id = AV_CODEC_ID_MSMPEG4V2,
1233  .priv_data_size = sizeof(MpegEncContext),
1237  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1238  .max_lowres = 3,
1239  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
1240  .pix_fmts = ff_pixfmt_list_420,
1241 };
1242 
1244  .name = "msmpeg4",
1245  .type = AVMEDIA_TYPE_VIDEO,
1246  .id = AV_CODEC_ID_MSMPEG4V3,
1247  .priv_data_size = sizeof(MpegEncContext),
1251  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1252  .max_lowres = 3,
1253  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
1254  .pix_fmts = ff_pixfmt_list_420,
1255 };
1256 
1258  .name = "wmv1",
1259  .type = AVMEDIA_TYPE_VIDEO,
1260  .id = AV_CODEC_ID_WMV1,
1261  .priv_data_size = sizeof(MpegEncContext),
1265  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
1266  .max_lowres = 3,
1267  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
1268  .pix_fmts = ff_pixfmt_list_420,
1269 };