FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
msmpeg4dec.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-2013 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 #include "avcodec.h"
26 #include "dsputil.h"
27 #include "mpegvideo.h"
28 #include "msmpeg4.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/x86/asm.h"
31 #include "h263.h"
32 #include "mpeg4video.h"
33 #include "msmpeg4data.h"
34 #include "vc1data.h"
35 
36 #define DC_VLC_BITS 9
37 #define V2_INTRA_CBPC_VLC_BITS 3
38 #define V2_MB_TYPE_VLC_BITS 7
39 #define MV_VLC_BITS 9
40 #define V2_MV_VLC_BITS 9
41 #define TEX_VLC_BITS 9
42 
43 #define DEFAULT_INTER_INDEX 3
44 
45 static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
46  int32_t **dc_val_ptr)
47 {
48  int i;
49 
50  if (n < 4) {
51  i= 0;
52  } else {
53  i= n-3;
54  }
55 
56  *dc_val_ptr= &s->last_dc[i];
57  return s->last_dc[i];
58 }
59 
60 /****************************************/
61 /* decoding stuff */
62 
68 static VLC v2_mv_vlc;
70 
71 /* This is identical to h263 except that its range is multiplied by 2. */
72 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
73 {
74  int code, val, sign, shift;
75 
76  code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
77  av_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
78  if (code < 0)
79  return 0xffff;
80 
81  if (code == 0)
82  return pred;
83  sign = get_bits1(&s->gb);
84  shift = f_code - 1;
85  val = code;
86  if (shift) {
87  val = (val - 1) << shift;
88  val |= get_bits(&s->gb, shift);
89  val++;
90  }
91  if (sign)
92  val = -val;
93 
94  val += pred;
95  if (val <= -64)
96  val += 64;
97  else if (val >= 64)
98  val -= 64;
99 
100  return val;
101 }
102 
103 static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
104 {
105  int cbp, code, i;
106  uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
107 
108  if (s->pict_type == AV_PICTURE_TYPE_P) {
109  if (s->use_skip_mb_code) {
110  if (get_bits1(&s->gb)) {
111  /* skip mb */
112  s->mb_intra = 0;
113  for(i=0;i<6;i++)
114  s->block_last_index[i] = -1;
115  s->mv_dir = MV_DIR_FORWARD;
116  s->mv_type = MV_TYPE_16X16;
117  s->mv[0][0][0] = 0;
118  s->mv[0][0][1] = 0;
119  s->mb_skipped = 1;
120  *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
121  return 0;
122  }
123  }
124 
125  if(s->msmpeg4_version==2)
126  code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
127  else
129  if(code<0 || code>7){
130  av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
131  return -1;
132  }
133 
134  s->mb_intra = code >>2;
135 
136  cbp = code & 0x3;
137  } else {
138  s->mb_intra = 1;
139  if(s->msmpeg4_version==2)
140  cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
141  else
143  if(cbp<0 || cbp>3){
144  av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
145  return -1;
146  }
147  }
148 
149  if (!s->mb_intra) {
150  int mx, my, cbpy;
151 
153  if(cbpy<0){
154  av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
155  return -1;
156  }
157 
158  cbp|= cbpy<<2;
159  if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
160 
161  ff_h263_pred_motion(s, 0, 0, &mx, &my);
162  mx= msmpeg4v2_decode_motion(s, mx, 1);
163  my= msmpeg4v2_decode_motion(s, my, 1);
164 
165  s->mv_dir = MV_DIR_FORWARD;
166  s->mv_type = MV_TYPE_16X16;
167  s->mv[0][0][0] = mx;
168  s->mv[0][0][1] = my;
169  *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
170  } else {
171  if(s->msmpeg4_version==2){
172  s->ac_pred = get_bits1(&s->gb);
173  cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
174  } else{
175  s->ac_pred = 0;
176  cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
177  if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
178  }
179  *mb_type_ptr = MB_TYPE_INTRA;
180  }
181 
182  s->dsp.clear_blocks(s->block[0]);
183  for (i = 0; i < 6; i++) {
184  if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
185  {
186  av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
187  return -1;
188  }
189  }
190  return 0;
191 }
192 
193 static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
194 {
195  int cbp, code, i;
196  uint8_t *coded_val;
197  uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
198 
199  if (s->pict_type == AV_PICTURE_TYPE_P) {
200  if (s->use_skip_mb_code) {
201  if (get_bits1(&s->gb)) {
202  /* skip mb */
203  s->mb_intra = 0;
204  for(i=0;i<6;i++)
205  s->block_last_index[i] = -1;
206  s->mv_dir = MV_DIR_FORWARD;
207  s->mv_type = MV_TYPE_16X16;
208  s->mv[0][0][0] = 0;
209  s->mv[0][0][1] = 0;
210  s->mb_skipped = 1;
211  *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
212 
213  return 0;
214  }
215  }
216 
217  code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
218  if (code < 0)
219  return -1;
220  //s->mb_intra = (code & 0x40) ? 0 : 1;
221  s->mb_intra = (~code & 0x40) >> 6;
222 
223  cbp = code & 0x3f;
224  } else {
225  s->mb_intra = 1;
227  if (code < 0)
228  return -1;
229  /* predict coded block pattern */
230  cbp = 0;
231  for(i=0;i<6;i++) {
232  int val = ((code >> (5 - i)) & 1);
233  if (i < 4) {
234  int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
235  val = val ^ pred;
236  *coded_val = val;
237  }
238  cbp |= val << (5 - i);
239  }
240  }
241 
242  if (!s->mb_intra) {
243  int mx, my;
244  if(s->per_mb_rl_table && cbp){
245  s->rl_table_index = decode012(&s->gb);
247  }
248  ff_h263_pred_motion(s, 0, 0, &mx, &my);
249  if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
250  return -1;
251  s->mv_dir = MV_DIR_FORWARD;
252  s->mv_type = MV_TYPE_16X16;
253  s->mv[0][0][0] = mx;
254  s->mv[0][0][1] = my;
255  *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
256  } else {
257  av_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
258  ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
259  show_bits(&s->gb, 24));
260  s->ac_pred = get_bits1(&s->gb);
261  *mb_type_ptr = MB_TYPE_INTRA;
262  if(s->inter_intra_pred){
263  s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
264  av_dlog(s, "%d%d %d %d/",
265  s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
266  }
267  if(s->per_mb_rl_table && cbp){
268  s->rl_table_index = decode012(&s->gb);
270  }
271  }
272 
273  s->dsp.clear_blocks(s->block[0]);
274  for (i = 0; i < 6; i++) {
275  if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
276  {
277  av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
278  return -1;
279  }
280  }
281 
282  return 0;
283 }
284 
285 /* init all vlc decoding tables */
287 {
288  MpegEncContext *s = avctx->priv_data;
289  static volatile int done = 0;
290  int i, ret;
291  MVTable *mv;
292 
293  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
294  return ret;
295 
296  if (ff_h263_decode_init(avctx) < 0)
297  return -1;
298 
300 
301  if (!done) {
302  for(i=0;i<NB_RL_TABLES;i++) {
304  }
305  INIT_VLC_RL(ff_rl_table[0], 642);
306  INIT_VLC_RL(ff_rl_table[1], 1104);
307  INIT_VLC_RL(ff_rl_table[2], 554);
308  INIT_VLC_RL(ff_rl_table[3], 940);
309  INIT_VLC_RL(ff_rl_table[4], 962);
310  INIT_VLC_RL(ff_rl_table[5], 554);
311 
312  mv = &ff_mv_tables[0];
313  INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
314  mv->table_mv_bits, 1, 1,
315  mv->table_mv_code, 2, 2, 3714);
316  mv = &ff_mv_tables[1];
317  INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
318  mv->table_mv_bits, 1, 1,
319  mv->table_mv_code, 2, 2, 2694);
320 
322  &ff_table0_dc_lum[0][1], 8, 4,
323  &ff_table0_dc_lum[0][0], 8, 4, 1158);
325  &ff_table0_dc_chroma[0][1], 8, 4,
326  &ff_table0_dc_chroma[0][0], 8, 4, 1118);
328  &ff_table1_dc_lum[0][1], 8, 4,
329  &ff_table1_dc_lum[0][0], 8, 4, 1476);
331  &ff_table1_dc_chroma[0][1], 8, 4,
332  &ff_table1_dc_chroma[0][0], 8, 4, 1216);
333 
334  INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
335  &ff_v2_dc_lum_table[0][1], 8, 4,
336  &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
337  INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
338  &ff_v2_dc_chroma_table[0][1], 8, 4,
339  &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
340 
341  INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
342  &ff_v2_intra_cbpc[0][1], 2, 1,
343  &ff_v2_intra_cbpc[0][0], 2, 1, 8);
344  INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
345  &ff_v2_mb_type[0][1], 2, 1,
346  &ff_v2_mb_type[0][0], 2, 1, 128);
347  INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
348  &ff_mvtab[0][1], 2, 1,
349  &ff_mvtab[0][0], 2, 1, 538);
350 
351  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
352  &ff_wmv2_inter_table[0][0][1], 8, 4,
353  &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
354  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
355  &ff_wmv2_inter_table[1][0][1], 8, 4,
356  &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
357  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
358  &ff_wmv2_inter_table[2][0][1], 8, 4,
359  &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
360  INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
361  &ff_wmv2_inter_table[3][0][1], 8, 4,
362  &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
363 
365  &ff_msmp4_mb_i_table[0][1], 4, 2,
366  &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
367 
368  INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
369  &ff_table_inter_intra[0][1], 2, 1,
370  &ff_table_inter_intra[0][0], 2, 1, 8);
371  done = 1;
372  }
373 
374  switch(s->msmpeg4_version){
375  case 1:
376  case 2:
378  break;
379  case 3:
380  case 4:
382  break;
383  case 5:
384  if (CONFIG_WMV2_DECODER)
386  case 6:
387  //FIXME + TODO VC1 decode mb
388  break;
389  }
390 
391  s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
392 
393  return 0;
394 }
395 
397 {
398  int code;
399 
400  if(s->msmpeg4_version==1){
401  int start_code = get_bits_long(&s->gb, 32);
402  if(start_code!=0x00000100){
403  av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
404  return -1;
405  }
406 
407  skip_bits(&s->gb, 5); // frame number */
408  }
409 
410  s->pict_type = get_bits(&s->gb, 2) + 1;
411  if (s->pict_type != AV_PICTURE_TYPE_I &&
413  av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
414  return -1;
415  }
416 #if 0
417 {
418  static int had_i=0;
419  if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
420  if(!had_i) return -1;
421 }
422 #endif
423  s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
424  if(s->qscale==0){
425  av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
426  return -1;
427  }
428 
429  if (s->pict_type == AV_PICTURE_TYPE_I) {
430  code = get_bits(&s->gb, 5);
431  if(s->msmpeg4_version==1){
432  if(code==0 || code>s->mb_height){
433  av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
434  return -1;
435  }
436 
437  s->slice_height = code;
438  }else{
439  /* 0x17: one slice, 0x18: two slices, ... */
440  if (code < 0x17){
441  av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
442  return -1;
443  }
444 
445  s->slice_height = s->mb_height / (code - 0x16);
446  }
447 
448  switch(s->msmpeg4_version){
449  case 1:
450  case 2:
451  s->rl_chroma_table_index = 2;
452  s->rl_table_index = 2;
453 
454  s->dc_table_index = 0; //not used
455  break;
456  case 3:
458  s->rl_table_index = decode012(&s->gb);
459 
460  s->dc_table_index = get_bits1(&s->gb);
461  break;
462  case 4:
463  ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
464 
466  else s->per_mb_rl_table= 0;
467 
468  if(!s->per_mb_rl_table){
470  s->rl_table_index = decode012(&s->gb);
471  }
472 
473  s->dc_table_index = get_bits1(&s->gb);
474  s->inter_intra_pred= 0;
475  break;
476  }
477  s->no_rounding = 1;
479  av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
480  s->qscale,
482  s->rl_table_index,
483  s->dc_table_index,
484  s->per_mb_rl_table,
485  s->slice_height);
486  } else {
487  switch(s->msmpeg4_version){
488  case 1:
489  case 2:
490  if(s->msmpeg4_version==1)
491  s->use_skip_mb_code = 1;
492  else
493  s->use_skip_mb_code = get_bits1(&s->gb);
494  s->rl_table_index = 2;
496  s->dc_table_index = 0; //not used
497  s->mv_table_index = 0;
498  break;
499  case 3:
500  s->use_skip_mb_code = get_bits1(&s->gb);
501  s->rl_table_index = decode012(&s->gb);
503 
504  s->dc_table_index = get_bits1(&s->gb);
505 
506  s->mv_table_index = get_bits1(&s->gb);
507  break;
508  case 4:
509  s->use_skip_mb_code = get_bits1(&s->gb);
510 
512  else s->per_mb_rl_table= 0;
513 
514  if(!s->per_mb_rl_table){
515  s->rl_table_index = decode012(&s->gb);
517  }
518 
519  s->dc_table_index = get_bits1(&s->gb);
520 
521  s->mv_table_index = get_bits1(&s->gb);
522  s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
523  break;
524  }
525 
527  av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
528  s->use_skip_mb_code,
529  s->rl_table_index,
531  s->dc_table_index,
532  s->mv_table_index,
533  s->per_mb_rl_table,
534  s->qscale);
535 
536  if(s->flipflop_rounding){
537  s->no_rounding ^= 1;
538  }else{
539  s->no_rounding = 0;
540  }
541  }
542  av_dlog(s->avctx, "%d %d %d %d %d\n", s->pict_type, s->bit_rate,
543  s->inter_intra_pred, s->width, s->height);
544 
545  s->esc3_level_length= 0;
546  s->esc3_run_length= 0;
547 
548  return 0;
549 }
550 
552 {
553  int left= buf_size*8 - get_bits_count(&s->gb);
554  int length= s->msmpeg4_version>=3 ? 17 : 16;
555  /* the alt_bitstream reader could read over the end so we need to check it */
556  if(left>=length && left<length+8)
557  {
558  skip_bits(&s->gb, 5); /* fps */
559  s->bit_rate= get_bits(&s->gb, 11)*1024;
560  if(s->msmpeg4_version>=3)
561  s->flipflop_rounding= get_bits1(&s->gb);
562  else
563  s->flipflop_rounding= 0;
564  }
565  else if(left<length+8)
566  {
567  s->flipflop_rounding= 0;
568  if(s->msmpeg4_version != 2)
569  av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
570  }
571  else
572  {
573  av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
574  }
575 
576  return 0;
577 }
578 
579 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
580 {
581  int level, pred;
582 
583  if(s->msmpeg4_version<=2){
584  if (n < 4) {
585  level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
586  } else {
587  level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
588  }
589  if (level < 0)
590  return -1;
591  level-=256;
592  }else{ //FIXME optimize use unified tables & index
593  if (n < 4) {
595  } else {
597  }
598  if (level < 0){
599  av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
600  return -1;
601  }
602 
603  if (level == DC_MAX) {
604  level = get_bits(&s->gb, 8);
605  if (get_bits1(&s->gb))
606  level = -level;
607  } else if (level != 0) {
608  if (get_bits1(&s->gb))
609  level = -level;
610  }
611  }
612 
613  if(s->msmpeg4_version==1){
614  int32_t *dc_val;
615  pred = msmpeg4v1_pred_dc(s, n, &dc_val);
616  level += pred;
617 
618  /* update predictor */
619  *dc_val= level;
620  }else{
621  int16_t *dc_val;
622  pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
623  level += pred;
624 
625  /* update predictor */
626  if (n < 4) {
627  *dc_val = level * s->y_dc_scale;
628  } else {
629  *dc_val = level * s->c_dc_scale;
630  }
631  }
632 
633  return level;
634 }
635 
636 //#define ERROR_DETAILS
638  int n, int coded, const uint8_t *scan_table)
639 {
640  int level, i, last, run, run_diff;
641  int av_uninit(dc_pred_dir);
642  RLTable *rl;
643  RL_VLC_ELEM *rl_vlc;
644  int qmul, qadd;
645 
646  if (s->mb_intra) {
647  qmul=1;
648  qadd=0;
649 
650  /* DC coef */
651  level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
652 
653  if (level < 0){
654  av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
655  if(s->inter_intra_pred) level=0;
656  }
657  if (n < 4) {
658  rl = &ff_rl_table[s->rl_table_index];
659  if(level > 256*s->y_dc_scale){
660  av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
661  if(!s->inter_intra_pred) return -1;
662  }
663  } else {
664  rl = &ff_rl_table[3 + s->rl_chroma_table_index];
665  if(level > 256*s->c_dc_scale){
666  av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
667  if(!s->inter_intra_pred) return -1;
668  }
669  }
670  block[0] = level;
671 
672  run_diff = s->msmpeg4_version >= 4;
673  i = 0;
674  if (!coded) {
675  goto not_coded;
676  }
677  if (s->ac_pred) {
678  if (dc_pred_dir == 0)
679  scan_table = s->intra_v_scantable.permutated; /* left */
680  else
681  scan_table = s->intra_h_scantable.permutated; /* top */
682  } else {
683  scan_table = s->intra_scantable.permutated;
684  }
685  rl_vlc= rl->rl_vlc[0];
686  } else {
687  qmul = s->qscale << 1;
688  qadd = (s->qscale - 1) | 1;
689  i = -1;
690  rl = &ff_rl_table[3 + s->rl_table_index];
691 
692  if(s->msmpeg4_version==2)
693  run_diff = 0;
694  else
695  run_diff = 1;
696 
697  if (!coded) {
698  s->block_last_index[n] = i;
699  return 0;
700  }
701  if(!scan_table)
702  scan_table = s->inter_scantable.permutated;
703  rl_vlc= rl->rl_vlc[s->qscale];
704  }
705  {
706  OPEN_READER(re, &s->gb);
707  for(;;) {
708  UPDATE_CACHE(re, &s->gb);
709  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
710  if (level==0) {
711  int cache;
712  cache= GET_CACHE(re, &s->gb);
713  /* escape */
714  if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
715  if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
716  /* third escape */
717  if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
718  UPDATE_CACHE(re, &s->gb);
719  if(s->msmpeg4_version<=3){
720  last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
721  run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
722  level= SHOW_SBITS(re, &s->gb, 8);
723  SKIP_COUNTER(re, &s->gb, 1+6+8);
724  }else{
725  int sign;
726  last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
727  if(!s->esc3_level_length){
728  int ll;
729  av_dlog(s->avctx, "ESC-3 %X at %d %d\n",
730  show_bits(&s->gb, 24), s->mb_x, s->mb_y);
731  if(s->qscale<8){
732  ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
733  if(ll==0){
734  ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
735  }
736  }else{
737  ll=2;
738  while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
739  ll++;
740  SKIP_BITS(re, &s->gb, 1);
741  }
742  if(ll<8) SKIP_BITS(re, &s->gb, 1);
743  }
744 
745  s->esc3_level_length= ll;
746  s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
747  UPDATE_CACHE(re, &s->gb);
748  }
749  run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
750  SKIP_BITS(re, &s->gb, s->esc3_run_length);
751 
752  sign= SHOW_UBITS(re, &s->gb, 1);
753  SKIP_BITS(re, &s->gb, 1);
754 
755  level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
756  SKIP_BITS(re, &s->gb, s->esc3_level_length);
757  if(sign) level= -level;
758  }
759 
760 #if 0 // waste of time / this will detect very few errors
761  {
762  const int abs_level= FFABS(level);
763  const int run1= run - rl->max_run[last][abs_level] - run_diff;
764  if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
765  if(abs_level <= rl->max_level[last][run]){
766  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
767  return DECODING_AC_LOST;
768  }
769  if(abs_level <= rl->max_level[last][run]*2){
770  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
771  return DECODING_AC_LOST;
772  }
773  if(run1>=0 && abs_level <= rl->max_level[last][run1]){
774  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
775  return DECODING_AC_LOST;
776  }
777  }
778  }
779 #endif
780  //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
781  if (level>0) level= level * qmul + qadd;
782  else level= level * qmul - qadd;
783 #if 0 // waste of time too :(
784  if(level>2048 || level<-2048){
785  av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
786  return DECODING_AC_LOST;
787  }
788 #endif
789  i+= run + 1;
790  if(last) i+=192;
791 #ifdef ERROR_DETAILS
792  if(run==66)
793  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
794  else if((i>62 && i<192) || i>192+63)
795  av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
796 #endif
797  } else {
798  /* second escape */
799  SKIP_BITS(re, &s->gb, 2);
800  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
801  i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
802  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
803  LAST_SKIP_BITS(re, &s->gb, 1);
804 #ifdef ERROR_DETAILS
805  if(run==66)
806  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
807  else if((i>62 && i<192) || i>192+63)
808  av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
809 #endif
810  }
811  } else {
812  /* first escape */
813  SKIP_BITS(re, &s->gb, 1);
814  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
815  i+= run;
816  level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
817  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
818  LAST_SKIP_BITS(re, &s->gb, 1);
819 #ifdef ERROR_DETAILS
820  if(run==66)
821  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
822  else if((i>62 && i<192) || i>192+63)
823  av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
824 #endif
825  }
826  } else {
827  i+= run;
828  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
829  LAST_SKIP_BITS(re, &s->gb, 1);
830 #ifdef ERROR_DETAILS
831  if(run==66)
832  av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
833  else if((i>62 && i<192) || i>192+63)
834  av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
835 #endif
836  }
837  if (i > 62){
838  i-= 192;
839  if(i&(~63)){
840  const int left= get_bits_left(&s->gb);
841  if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
842  av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
843  i = 63;
844  break;
845  }else{
846  av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
847  return -1;
848  }
849  }
850 
851  block[scan_table[i]] = level;
852  break;
853  }
854 
855  block[scan_table[i]] = level;
856  }
857  CLOSE_READER(re, &s->gb);
858  }
859  not_coded:
860  if (s->mb_intra) {
861  ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
862  if (s->ac_pred) {
863  i = 63; /* XXX: not optimal */
864  }
865  }
866  if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
867  s->block_last_index[n] = i;
868 
869  return 0;
870 }
871 
873  int *mx_ptr, int *my_ptr)
874 {
875  MVTable *mv;
876  int code, mx, my;
877 
878  mv = &ff_mv_tables[s->mv_table_index];
879 
880  code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
881  if (code < 0){
882  av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
883  return -1;
884  }
885  if (code == mv->n) {
886  mx = get_bits(&s->gb, 6);
887  my = get_bits(&s->gb, 6);
888  } else {
889  mx = mv->table_mvx[code];
890  my = mv->table_mvy[code];
891  }
892 
893  mx += *mx_ptr - 32;
894  my += *my_ptr - 32;
895  /* WARNING : they do not do exactly modulo encoding */
896  if (mx <= -64)
897  mx += 64;
898  else if (mx >= 64)
899  mx -= 64;
900 
901  if (my <= -64)
902  my += 64;
903  else if (my >= 64)
904  my -= 64;
905  *mx_ptr = mx;
906  *my_ptr = my;
907  return 0;
908 }
909 
911  .name = "msmpeg4v1",
912  .type = AVMEDIA_TYPE_VIDEO,
913  .id = AV_CODEC_ID_MSMPEG4V1,
914  .priv_data_size = sizeof(MpegEncContext),
918  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
919  .max_lowres = 3,
920  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
921  .pix_fmts = ff_pixfmt_list_420,
922 };
923 
925  .name = "msmpeg4v2",
926  .type = AVMEDIA_TYPE_VIDEO,
927  .id = AV_CODEC_ID_MSMPEG4V2,
928  .priv_data_size = sizeof(MpegEncContext),
932  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
933  .max_lowres = 3,
934  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
935  .pix_fmts = ff_pixfmt_list_420,
936 };
937 
939  .name = "msmpeg4",
940  .type = AVMEDIA_TYPE_VIDEO,
941  .id = AV_CODEC_ID_MSMPEG4V3,
942  .priv_data_size = sizeof(MpegEncContext),
946  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
947  .max_lowres = 3,
948  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
949  .pix_fmts = ff_pixfmt_list_420,
950 };
951 
953  .name = "wmv1",
954  .type = AVMEDIA_TYPE_VIDEO,
955  .id = AV_CODEC_ID_WMV1,
956  .priv_data_size = sizeof(MpegEncContext),
960  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
961  .max_lowres = 3,
962  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
963  .pix_fmts = ff_pixfmt_list_420,
964 };