FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpeg4videodec.c
Go to the documentation of this file.
1 /*
2  * MPEG4 decoder.
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #define UNCHECKED_BITSTREAM_READER 1
24 
25 #include "libavutil/opt.h"
26 #include "error_resilience.h"
27 #include "internal.h"
28 #include "mpegvideo.h"
29 #include "mpeg4video.h"
30 #include "h263.h"
31 #include "thread.h"
32 
33 // The defines below define the number of bits that are read at once for
34 // reading vlc values. Changing these may improve speed and data cache needs
35 // be aware though that decreasing them may need the number of stages that is
36 // passed to get_vlc* to be increased.
37 #define SPRITE_TRAJ_VLC_BITS 6
38 #define DC_VLC_BITS 9
39 #define MB_TYPE_B_VLC_BITS 4
40 
41 
45 
46 static const int mb_type_b_map[4]= {
51 };
52 
53 /**
54  * Predict the ac.
55  * @param n block index (0-3 are luma, 4-5 are chroma)
56  * @param dir the ac prediction direction
57  */
58 void ff_mpeg4_pred_ac(MpegEncContext * s, int16_t *block, int n,
59  int dir)
60 {
61  int i;
62  int16_t *ac_val, *ac_val1;
63  int8_t * const qscale_table = s->current_picture.qscale_table;
64 
65  /* find prediction */
66  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
67  ac_val1 = ac_val;
68  if (s->ac_pred) {
69  if (dir == 0) {
70  const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
71  /* left prediction */
72  ac_val -= 16;
73 
74  if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
75  /* same qscale */
76  for(i=1;i<8;i++) {
77  block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
78  }
79  }else{
80  /* different qscale, we must rescale */
81  for(i=1;i<8;i++) {
82  block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
83  }
84  }
85  } else {
86  const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
87  /* top prediction */
88  ac_val -= 16 * s->block_wrap[n];
89 
90  if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
91  /* same qscale */
92  for(i=1;i<8;i++) {
93  block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
94  }
95  }else{
96  /* different qscale, we must rescale */
97  for(i=1;i<8;i++) {
98  block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
99  }
100  }
101  }
102  }
103  /* left copy */
104  for(i=1;i<8;i++)
105  ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
106 
107  /* top copy */
108  for(i=1;i<8;i++)
109  ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
110 
111 }
112 
113 /**
114  * check if the next stuff is a resync marker or the end.
115  * @return 0 if not
116  */
117 static inline int mpeg4_is_resync(MpegEncContext *s){
118  int bits_count= get_bits_count(&s->gb);
119  int v= show_bits(&s->gb, 16);
120 
122  return 0;
123  }
124 
125  while(v<=0xFF){
126  if(s->pict_type==AV_PICTURE_TYPE_B || (v>>(8-s->pict_type)!=1) || s->partitioned_frame)
127  break;
128  skip_bits(&s->gb, 8+s->pict_type);
129  bits_count+= 8+s->pict_type;
130  v= show_bits(&s->gb, 16);
131  }
132 
133  if(bits_count + 8 >= s->gb.size_in_bits){
134  v>>=8;
135  v|= 0x7F >> (7-(bits_count&7));
136 
137  if(v==0x7F)
138  return s->mb_num;
139  }else{
140  if(v == ff_mpeg4_resync_prefix[bits_count&7]){
141  int len, mb_num;
142  int mb_num_bits= av_log2(s->mb_num - 1) + 1;
143  GetBitContext gb= s->gb;
144 
145  skip_bits(&s->gb, 1);
146  align_get_bits(&s->gb);
147 
148  for(len=0; len<32; len++){
149  if(get_bits1(&s->gb)) break;
150  }
151 
152  mb_num= get_bits(&s->gb, mb_num_bits);
153  if(!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
154  mb_num= -1;
155 
156  s->gb= gb;
157 
159  return mb_num;
160  }
161  }
162  return 0;
163 }
164 
166 {
167  int i;
168  int a= 2<<s->sprite_warping_accuracy;
169  int rho= 3-s->sprite_warping_accuracy;
170  int r=16/a;
171  const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
172  int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
173  int sprite_ref[4][2];
174  int virtual_ref[2][2];
175  int w2, h2, w3, h3;
176  int alpha=0, beta=0;
177  int w= s->width;
178  int h= s->height;
179  int min_ab;
180 
181  if (w <= 0 || h <= 0)
182  return AVERROR_INVALIDDATA;
183 
184  for(i=0; i<s->num_sprite_warping_points; i++){
185  int length;
186  int x=0, y=0;
187 
188  length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
189  if(length){
190  x= get_xbits(gb, length);
191  }
192  if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(gb); /* marker bit */
193 
194  length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
195  if(length){
196  y=get_xbits(gb, length);
197  }
198  skip_bits1(gb); /* marker bit */
199  s->sprite_traj[i][0]= d[i][0]= x;
200  s->sprite_traj[i][1]= d[i][1]= y;
201  }
202  for(; i<4; i++)
203  s->sprite_traj[i][0]= s->sprite_traj[i][1]= 0;
204 
205  while((1<<alpha)<w) alpha++;
206  while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
207  w2= 1<<alpha;
208  h2= 1<<beta;
209 
210 // Note, the 4th point isn't used for GMC
211  if(s->divx_version==500 && s->divx_build==413){
212  sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
213  sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
214  sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
215  sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
216  sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
217  sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
218  } else {
219  sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
220  sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
221  sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
222  sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
223  sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
224  sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
225  }
226 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
227  sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
228 
229 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
230 // perhaps it should be reordered to be more readable ...
231 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
232 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
233  virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
234  + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
235  virtual_ref[0][1]= 16*vop_ref[0][1]
236  + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
237  virtual_ref[1][0]= 16*vop_ref[0][0]
238  + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
239  virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
240  + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
241 
242  switch(s->num_sprite_warping_points)
243  {
244  case 0:
245  s->sprite_offset[0][0]= 0;
246  s->sprite_offset[0][1]= 0;
247  s->sprite_offset[1][0]= 0;
248  s->sprite_offset[1][1]= 0;
249  s->sprite_delta[0][0]= a;
250  s->sprite_delta[0][1]= 0;
251  s->sprite_delta[1][0]= 0;
252  s->sprite_delta[1][1]= a;
253  s->sprite_shift[0]= 0;
254  s->sprite_shift[1]= 0;
255  break;
256  case 1: //GMC only
257  s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
258  s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
259  s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
260  s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
261  s->sprite_delta[0][0]= a;
262  s->sprite_delta[0][1]= 0;
263  s->sprite_delta[1][0]= 0;
264  s->sprite_delta[1][1]= a;
265  s->sprite_shift[0]= 0;
266  s->sprite_shift[1]= 0;
267  break;
268  case 2:
269  s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
270  + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
271  + ( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])
272  + (1<<(alpha+rho-1));
273  s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
274  + (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
275  + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])
276  + (1<<(alpha+rho-1));
277  s->sprite_offset[1][0]= ( (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
278  +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
279  +2*w2*r*sprite_ref[0][0]
280  - 16*w2
281  + (1<<(alpha+rho+1)));
282  s->sprite_offset[1][1]= ( (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
283  +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
284  +2*w2*r*sprite_ref[0][1]
285  - 16*w2
286  + (1<<(alpha+rho+1)));
287  s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
288  s->sprite_delta[0][1]= (+r*sprite_ref[0][1] - virtual_ref[0][1]);
289  s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
290  s->sprite_delta[1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
291 
292  s->sprite_shift[0]= alpha+rho;
293  s->sprite_shift[1]= alpha+rho+2;
294  break;
295  case 3:
296  min_ab= FFMIN(alpha, beta);
297  w3= w2>>min_ab;
298  h3= h2>>min_ab;
299  s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+beta+rho-min_ab))
300  + (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-vop_ref[0][0])
301  + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-vop_ref[0][1])
302  + (1<<(alpha+beta+rho-min_ab-1));
303  s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+beta+rho-min_ab))
304  + (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-vop_ref[0][0])
305  + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-vop_ref[0][1])
306  + (1<<(alpha+beta+rho-min_ab-1));
307  s->sprite_offset[1][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-2*vop_ref[0][0] + 1)
308  + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-2*vop_ref[0][1] + 1)
309  + 2*w2*h3*r*sprite_ref[0][0]
310  - 16*w2*h3
311  + (1<<(alpha+beta+rho-min_ab+1));
312  s->sprite_offset[1][1]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-2*vop_ref[0][0] + 1)
313  + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-2*vop_ref[0][1] + 1)
314  + 2*w2*h3*r*sprite_ref[0][1]
315  - 16*w2*h3
316  + (1<<(alpha+beta+rho-min_ab+1));
317  s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3;
318  s->sprite_delta[0][1]= (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3;
319  s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3;
320  s->sprite_delta[1][1]= (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3;
321 
322  s->sprite_shift[0]= alpha + beta + rho - min_ab;
323  s->sprite_shift[1]= alpha + beta + rho - min_ab + 2;
324  break;
325  }
326  /* try to simplify the situation */
327  if( s->sprite_delta[0][0] == a<<s->sprite_shift[0]
328  && s->sprite_delta[0][1] == 0
329  && s->sprite_delta[1][0] == 0
330  && s->sprite_delta[1][1] == a<<s->sprite_shift[0])
331  {
332  s->sprite_offset[0][0]>>=s->sprite_shift[0];
333  s->sprite_offset[0][1]>>=s->sprite_shift[0];
334  s->sprite_offset[1][0]>>=s->sprite_shift[1];
335  s->sprite_offset[1][1]>>=s->sprite_shift[1];
336  s->sprite_delta[0][0]= a;
337  s->sprite_delta[0][1]= 0;
338  s->sprite_delta[1][0]= 0;
339  s->sprite_delta[1][1]= a;
340  s->sprite_shift[0]= 0;
341  s->sprite_shift[1]= 0;
343  }
344  else{
345  int shift_y= 16 - s->sprite_shift[0];
346  int shift_c= 16 - s->sprite_shift[1];
347  for(i=0; i<2; i++){
348  s->sprite_offset[0][i]<<= shift_y;
349  s->sprite_offset[1][i]<<= shift_c;
350  s->sprite_delta[0][i]<<= shift_y;
351  s->sprite_delta[1][i]<<= shift_y;
352  s->sprite_shift[i]= 16;
353  }
355  }
356  return 0;
357 }
358 
360  int len = FFMIN(s->time_increment_bits + 3, 15);
361 
362  get_bits(gb, len);
363  if (get_bits1(gb))
364  get_bits(gb, len);
365  check_marker(gb, "after new_pred");
366 
367  return 0;
368 }
369 
370 /**
371  * Decode the next video packet.
372  * @return <0 if something went wrong
373  */
375 {
376  int mb_num_bits= av_log2(s->mb_num - 1) + 1;
377  int header_extension=0, mb_num, len;
378 
379  /* is there enough space left for a video packet + header */
380  if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
381 
382  for(len=0; len<32; len++){
383  if(get_bits1(&s->gb)) break;
384  }
385 
387  av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
388  return -1;
389  }
390 
391  if(s->shape != RECT_SHAPE){
392  header_extension= get_bits1(&s->gb);
393  //FIXME more stuff here
394  }
395 
396  mb_num= get_bits(&s->gb, mb_num_bits);
397  if(mb_num>=s->mb_num){
398  av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
399  return -1;
400  }
401 
402  s->mb_x= mb_num % s->mb_width;
403  s->mb_y= mb_num / s->mb_width;
404 
405  if(s->shape != BIN_ONLY_SHAPE){
406  int qscale= get_bits(&s->gb, s->quant_precision);
407  if(qscale)
408  s->chroma_qscale=s->qscale= qscale;
409  }
410 
411  if(s->shape == RECT_SHAPE){
412  header_extension= get_bits1(&s->gb);
413  }
414  if(header_extension){
415  int time_incr=0;
416 
417  while (get_bits1(&s->gb) != 0)
418  time_incr++;
419 
420  check_marker(&s->gb, "before time_increment in video packed header");
421  skip_bits(&s->gb, s->time_increment_bits); /* time_increment */
422  check_marker(&s->gb, "before vop_coding_type in video packed header");
423 
424  skip_bits(&s->gb, 2); /* vop coding type */
425  //FIXME not rect stuff here
426 
427  if(s->shape != BIN_ONLY_SHAPE){
428  skip_bits(&s->gb, 3); /* intra dc vlc threshold */
429 //FIXME don't just ignore everything
431  if (mpeg4_decode_sprite_trajectory(s, &s->gb) < 0)
432  return AVERROR_INVALIDDATA;
433  av_log(s->avctx, AV_LOG_ERROR, "untested\n");
434  }
435 
436  //FIXME reduced res stuff here
437 
438  if (s->pict_type != AV_PICTURE_TYPE_I) {
439  int f_code = get_bits(&s->gb, 3); /* fcode_for */
440  if(f_code==0){
441  av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
442  }
443  }
444  if (s->pict_type == AV_PICTURE_TYPE_B) {
445  int b_code = get_bits(&s->gb, 3);
446  if(b_code==0){
447  av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
448  }
449  }
450  }
451  }
452  if (s->new_pred)
453  decode_new_pred(s, &s->gb);
454 
455  return 0;
456 }
457 
458 /**
459  * Get the average motion vector for a GMC MB.
460  * @param n either 0 for the x component or 1 for y
461  * @return the average MV for a GMC MB
462  */
463 static inline int get_amv(MpegEncContext *s, int n){
464  int x, y, mb_v, sum, dx, dy, shift;
465  int len = 1 << (s->f_code + 4);
466  const int a= s->sprite_warping_accuracy;
467 
468  if(s->workaround_bugs & FF_BUG_AMV)
469  len >>= s->quarter_sample;
470 
471  if(s->real_sprite_warping_points==1){
472  if(s->divx_version==500 && s->divx_build==413)
473  sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
474  else
475  sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
476  }else{
477  dx= s->sprite_delta[n][0];
478  dy= s->sprite_delta[n][1];
479  shift= s->sprite_shift[0];
480  if(n) dy -= 1<<(shift + a + 1);
481  else dx -= 1<<(shift + a + 1);
482  mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
483 
484  sum=0;
485  for(y=0; y<16; y++){
486  int v;
487 
488  v= mb_v + dy*y;
489  //XXX FIXME optimize
490  for(x=0; x<16; x++){
491  sum+= v>>shift;
492  v+= dx;
493  }
494  }
495  sum= RSHIFT(sum, a+8-s->quarter_sample);
496  }
497 
498  if (sum < -len) sum= -len;
499  else if (sum >= len) sum= len-1;
500 
501  return sum;
502 }
503 
504 /**
505  * Decode the dc value.
506  * @param n block index (0-3 are luma, 4-5 are chroma)
507  * @param dir_ptr the prediction direction will be stored here
508  * @return the quantized dc
509  */
510 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
511 {
512  int level, code;
513 
514  if (n < 4)
515  code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
516  else
517  code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
518  if (code < 0 || code > 9 /* && s->nbit<9 */){
519  av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
520  return -1;
521  }
522  if (code == 0) {
523  level = 0;
524  } else {
525  if(IS_3IV1){
526  if(code==1)
527  level= 2*get_bits1(&s->gb)-1;
528  else{
529  if(get_bits1(&s->gb))
530  level = get_bits(&s->gb, code-1) + (1<<(code-1));
531  else
532  level = -get_bits(&s->gb, code-1) - (1<<(code-1));
533  }
534  }else{
535  level = get_xbits(&s->gb, code);
536  }
537 
538  if (code > 8){
539  if(get_bits1(&s->gb)==0){ /* marker */
541  av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
542  return -1;
543  }
544  }
545  }
546  }
547 
548  return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
549 }
550 
551 /**
552  * Decode first partition.
553  * @return number of MBs decoded or <0 if an error occurred
554  */
556  int mb_num;
557  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
558 
559  /* decode first partition */
560  mb_num=0;
561  s->first_slice_line=1;
562  for(; s->mb_y<s->mb_height; s->mb_y++){
564  for(; s->mb_x<s->mb_width; s->mb_x++){
565  const int xy= s->mb_x + s->mb_y*s->mb_stride;
566  int cbpc;
567  int dir=0;
568 
569  mb_num++;
571  if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
572  s->first_slice_line=0;
573 
575  int i;
576 
577  do{
578  if(show_bits_long(&s->gb, 19)==DC_MARKER){
579  return mb_num-1;
580  }
581 
583  if (cbpc < 0){
584  av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
585  return -1;
586  }
587  }while(cbpc == 8);
588 
589  s->cbp_table[xy]= cbpc & 3;
591  s->mb_intra = 1;
592 
593  if(cbpc & 4) {
594  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
595  }
597 
598  s->mbintra_table[xy]= 1;
599  for(i=0; i<6; i++){
600  int dc_pred_dir;
601  int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
602  if(dc < 0){
603  av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
604  return -1;
605  }
606  dir<<=1;
607  if(dc_pred_dir) dir|=1;
608  }
609  s->pred_dir_table[xy]= dir;
610  }else{ /* P/S_TYPE */
611  int mx, my, pred_x, pred_y, bits;
612  int16_t * const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
613  const int stride= s->b8_stride*2;
614 
615 try_again:
616  bits= show_bits(&s->gb, 17);
617  if(bits==MOTION_MARKER){
618  return mb_num-1;
619  }
620  skip_bits1(&s->gb);
621  if(bits&0x10000){
622  /* skip mb */
625  mx= get_amv(s, 0);
626  my= get_amv(s, 1);
627  }else{
629  mx=my=0;
630  }
631  mot_val[0 ]= mot_val[2 ]=
632  mot_val[0+stride]= mot_val[2+stride]= mx;
633  mot_val[1 ]= mot_val[3 ]=
634  mot_val[1+stride]= mot_val[3+stride]= my;
635 
636  if(s->mbintra_table[xy])
638  continue;
639  }
640 
642  if (cbpc < 0){
643  av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
644  return -1;
645  }
646  if(cbpc == 20)
647  goto try_again;
648 
649  s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
650 
651  s->mb_intra = ((cbpc & 4) != 0);
652 
653  if(s->mb_intra){
655  s->mbintra_table[xy]= 1;
656  mot_val[0 ]= mot_val[2 ]=
657  mot_val[0+stride]= mot_val[2+stride]= 0;
658  mot_val[1 ]= mot_val[3 ]=
659  mot_val[1+stride]= mot_val[3+stride]= 0;
660  }else{
661  if(s->mbintra_table[xy])
663 
664  if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
665  s->mcsel= get_bits1(&s->gb);
666  else s->mcsel= 0;
667 
668  if ((cbpc & 16) == 0) {
669  /* 16x16 motion prediction */
670 
671  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
672  if(!s->mcsel){
673  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
674  if (mx >= 0xffff)
675  return -1;
676 
677  my = ff_h263_decode_motion(s, pred_y, s->f_code);
678  if (my >= 0xffff)
679  return -1;
681  } else {
682  mx = get_amv(s, 0);
683  my = get_amv(s, 1);
685  }
686 
687  mot_val[0 ]= mot_val[2 ] =
688  mot_val[0+stride]= mot_val[2+stride]= mx;
689  mot_val[1 ]= mot_val[3 ]=
690  mot_val[1+stride]= mot_val[3+stride]= my;
691  } else {
692  int i;
694  for(i=0;i<4;i++) {
695  int16_t *mot_val= ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
696  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
697  if (mx >= 0xffff)
698  return -1;
699 
700  my = ff_h263_decode_motion(s, pred_y, s->f_code);
701  if (my >= 0xffff)
702  return -1;
703  mot_val[0] = mx;
704  mot_val[1] = my;
705  }
706  }
707  }
708  }
709  }
710  s->mb_x= 0;
711  }
712 
713  return mb_num;
714 }
715 
716 /**
717  * decode second partition.
718  * @return <0 if an error occurred
719  */
720 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
721  int mb_num=0;
722  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
723 
724  s->mb_x= s->resync_mb_x;
725  s->first_slice_line=1;
726  for(s->mb_y= s->resync_mb_y; mb_num < mb_count; s->mb_y++){
728  for(; mb_num < mb_count && s->mb_x<s->mb_width; s->mb_x++){
729  const int xy= s->mb_x + s->mb_y*s->mb_stride;
730 
731  mb_num++;
733  if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
734  s->first_slice_line=0;
735 
737  int ac_pred= get_bits1(&s->gb);
738  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
739  if(cbpy<0){
740  av_log(s->avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
741  return -1;
742  }
743 
744  s->cbp_table[xy]|= cbpy<<2;
745  s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
746  }else{ /* P || S_TYPE */
747  if (IS_INTRA(s->current_picture.mb_type[xy])) {
748  int dir=0,i;
749  int ac_pred = get_bits1(&s->gb);
750  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
751 
752  if(cbpy<0){
753  av_log(s->avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
754  return -1;
755  }
756 
757  if(s->cbp_table[xy] & 8) {
758  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
759  }
760  s->current_picture.qscale_table[xy] = s->qscale;
761 
762  for(i=0; i<6; i++){
763  int dc_pred_dir;
764  int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
765  if(dc < 0){
766  av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
767  return -1;
768  }
769  dir<<=1;
770  if(dc_pred_dir) dir|=1;
771  }
772  s->cbp_table[xy]&= 3; //remove dquant
773  s->cbp_table[xy]|= cbpy<<2;
774  s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
775  s->pred_dir_table[xy]= dir;
776  } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
777  s->current_picture.qscale_table[xy] = s->qscale;
778  s->cbp_table[xy]= 0;
779  }else{
780  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
781 
782  if(cbpy<0){
783  av_log(s->avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
784  return -1;
785  }
786 
787  if(s->cbp_table[xy] & 8) {
788  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
789  }
790  s->current_picture.qscale_table[xy] = s->qscale;
791 
792  s->cbp_table[xy]&= 3; //remove dquant
793  s->cbp_table[xy]|= (cbpy^0xf)<<2;
794  }
795  }
796  }
797  if(mb_num >= mb_count) return 0;
798  s->mb_x= 0;
799  }
800  return 0;
801 }
802 
803 /**
804  * Decode the first and second partition.
805  * @return <0 if error (and sets error type in the error_status_table)
806  */
808 {
809  int mb_num;
810  const int part_a_error= s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_ERROR|ER_MV_ERROR) : ER_MV_ERROR;
811  const int part_a_end = s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_END |ER_MV_END) : ER_MV_END;
812 
813  mb_num= mpeg4_decode_partition_a(s);
814  if(mb_num<0){
815  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
816  return -1;
817  }
818 
819  if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){
820  av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
821  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
822  return -1;
823  }
824 
825  s->mb_num_left= mb_num;
826 
828  while(show_bits(&s->gb, 9) == 1)
829  skip_bits(&s->gb, 9);
830  if(get_bits_long(&s->gb, 19)!=DC_MARKER){
831  av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y);
832  return -1;
833  }
834  }else{
835  while(show_bits(&s->gb, 10) == 1)
836  skip_bits(&s->gb, 10);
837  if(get_bits(&s->gb, 17)!=MOTION_MARKER){
838  av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y);
839  return -1;
840  }
841  }
842  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end);
843 
844  if( mpeg4_decode_partition_b(s, mb_num) < 0){
847  return -1;
848  }else{
850  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_DC_END);
851  }
852 
853  return 0;
854 }
855 
856 /**
857  * Decode a block.
858  * @return <0 if an error occurred
859  */
860 static inline int mpeg4_decode_block(MpegEncContext * s, int16_t * block,
861  int n, int coded, int intra, int rvlc)
862 {
863  int level, i, last, run;
864  int av_uninit(dc_pred_dir);
865  RLTable * rl;
866  RL_VLC_ELEM * rl_vlc;
867  const uint8_t * scan_table;
868  int qmul, qadd;
869 
870  //Note intra & rvlc should be optimized away if this is inlined
871 
872  if(intra) {
873  if(s->use_intra_dc_vlc){
874  /* DC coef */
875  if(s->partitioned_frame){
876  level = s->dc_val[0][ s->block_index[n] ];
877  if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
878  else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
879  dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
880  }else{
881  level = mpeg4_decode_dc(s, n, &dc_pred_dir);
882  if (level < 0)
883  return -1;
884  }
885  block[0] = level;
886  i = 0;
887  }else{
888  i = -1;
889  ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
890  }
891  if (!coded)
892  goto not_coded;
893 
894  if(rvlc){
895  rl = &ff_rvlc_rl_intra;
896  rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
897  }else{
898  rl = &ff_mpeg4_rl_intra;
899  rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
900  }
901  if (s->ac_pred) {
902  if (dc_pred_dir == 0)
903  scan_table = s->intra_v_scantable.permutated; /* left */
904  else
905  scan_table = s->intra_h_scantable.permutated; /* top */
906  } else {
907  scan_table = s->intra_scantable.permutated;
908  }
909  qmul=1;
910  qadd=0;
911  } else {
912  i = -1;
913  if (!coded) {
914  s->block_last_index[n] = i;
915  return 0;
916  }
917  if(rvlc) rl = &ff_rvlc_rl_inter;
918  else rl = &ff_h263_rl_inter;
919 
920  scan_table = s->intra_scantable.permutated;
921 
922  if(s->mpeg_quant){
923  qmul=1;
924  qadd=0;
925  if(rvlc){
926  rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
927  }else{
928  rl_vlc = ff_h263_rl_inter.rl_vlc[0];
929  }
930  }else{
931  qmul = s->qscale << 1;
932  qadd = (s->qscale - 1) | 1;
933  if(rvlc){
934  rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
935  }else{
936  rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
937  }
938  }
939  }
940  {
941  OPEN_READER(re, &s->gb);
942  for(;;) {
943  UPDATE_CACHE(re, &s->gb);
944  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
945  if (level==0) {
946  /* escape */
947  if(rvlc){
948  if(SHOW_UBITS(re, &s->gb, 1)==0){
949  av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
950  return -1;
951  }; SKIP_CACHE(re, &s->gb, 1);
952 
953  last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
954  run= SHOW_UBITS(re, &s->gb, 6);
955  SKIP_COUNTER(re, &s->gb, 1+1+6);
956  UPDATE_CACHE(re, &s->gb);
957 
958  if(SHOW_UBITS(re, &s->gb, 1)==0){
959  av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
960  return -1;
961  }; SKIP_CACHE(re, &s->gb, 1);
962 
963  level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
964 
965  if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
966  av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
967  return -1;
968  }; SKIP_CACHE(re, &s->gb, 5);
969 
970  level= level * qmul + qadd;
971  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
972  SKIP_COUNTER(re, &s->gb, 1+11+5+1);
973 
974  i+= run + 1;
975  if(last) i+=192;
976  }else{
977  int cache;
978  cache= GET_CACHE(re, &s->gb);
979 
980  if(IS_3IV1)
981  cache ^= 0xC0000000;
982 
983  if (cache&0x80000000) {
984  if (cache&0x40000000) {
985  /* third escape */
986  SKIP_CACHE(re, &s->gb, 2);
987  last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
988  run= SHOW_UBITS(re, &s->gb, 6);
989  SKIP_COUNTER(re, &s->gb, 2+1+6);
990  UPDATE_CACHE(re, &s->gb);
991 
992  if(IS_3IV1){
993  level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
994  }else{
995  if(SHOW_UBITS(re, &s->gb, 1)==0){
996  av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
997  return -1;
998  }; SKIP_CACHE(re, &s->gb, 1);
999 
1000  level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
1001 
1002  if(SHOW_UBITS(re, &s->gb, 1)==0){
1003  av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
1004  return -1;
1005  }
1006 
1007  SKIP_COUNTER(re, &s->gb, 1+12+1);
1008  }
1009 
1010 #if 0
1011  if(s->error_recognition >= FF_ER_COMPLIANT){
1012  const int abs_level= FFABS(level);
1013  if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1014  const int run1= run - rl->max_run[last][abs_level] - 1;
1015  if(abs_level <= rl->max_level[last][run]){
1016  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1017  return -1;
1018  }
1019  if(s->error_recognition > FF_ER_COMPLIANT){
1020  if(abs_level <= rl->max_level[last][run]*2){
1021  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1022  return -1;
1023  }
1024  if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
1025  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1026  return -1;
1027  }
1028  }
1029  }
1030  }
1031 #endif
1032  if (level>0) level= level * qmul + qadd;
1033  else level= level * qmul - qadd;
1034 
1035  if((unsigned)(level + 2048) > 4095){
1037  if(level > 2560 || level<-2560){
1038  av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
1039  return -1;
1040  }
1041  }
1042  level= level<0 ? -2048 : 2047;
1043  }
1044 
1045  i+= run + 1;
1046  if(last) i+=192;
1047  } else {
1048  /* second escape */
1049  SKIP_BITS(re, &s->gb, 2);
1050  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1051  i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
1052  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1053  LAST_SKIP_BITS(re, &s->gb, 1);
1054  }
1055  } else {
1056  /* first escape */
1057  SKIP_BITS(re, &s->gb, 1);
1058  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1059  i+= run;
1060  level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1061  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1062  LAST_SKIP_BITS(re, &s->gb, 1);
1063  }
1064  }
1065  } else {
1066  i+= run;
1067  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1068  LAST_SKIP_BITS(re, &s->gb, 1);
1069  }
1070  if (i > 62){
1071  i-= 192;
1072  if(i&(~63)){
1073  av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1074  return -1;
1075  }
1076 
1077  block[scan_table[i]] = level;
1078  break;
1079  }
1080 
1081  block[scan_table[i]] = level;
1082  }
1083  CLOSE_READER(re, &s->gb);
1084  }
1085  not_coded:
1086  if (intra) {
1087  if(!s->use_intra_dc_vlc){
1088  block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1089 
1090  i -= i>>31; //if(i == -1) i=0;
1091  }
1092 
1093  ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1094  if (s->ac_pred) {
1095  i = 63; /* XXX: not optimal */
1096  }
1097  }
1098  s->block_last_index[n] = i;
1099  return 0;
1100 }
1101 
1102 /**
1103  * decode partition C of one MB.
1104  * @return <0 if an error occurred
1105  */
1107 {
1108  int cbp, mb_type;
1109  const int xy= s->mb_x + s->mb_y*s->mb_stride;
1110 
1111  mb_type = s->current_picture.mb_type[xy];
1112  cbp = s->cbp_table[xy];
1113 
1115 
1116  if (s->current_picture.qscale_table[xy] != s->qscale) {
1118  }
1119 
1121  int i;
1122  for(i=0; i<4; i++){
1123  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1124  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1125  }
1126  s->mb_intra = IS_INTRA(mb_type);
1127 
1128  if (IS_SKIP(mb_type)) {
1129  /* skip mb */
1130  for(i=0;i<6;i++)
1131  s->block_last_index[i] = -1;
1132  s->mv_dir = MV_DIR_FORWARD;
1133  s->mv_type = MV_TYPE_16X16;
1135  s->mcsel=1;
1136  s->mb_skipped = 0;
1137  }else{
1138  s->mcsel=0;
1139  s->mb_skipped = 1;
1140  }
1141  }else if(s->mb_intra){
1143  }else if(!s->mb_intra){
1144 // s->mcsel= 0; //FIXME do we need to init that
1145 
1146  s->mv_dir = MV_DIR_FORWARD;
1147  if (IS_8X8(mb_type)) {
1148  s->mv_type = MV_TYPE_8X8;
1149  } else {
1150  s->mv_type = MV_TYPE_16X16;
1151  }
1152  }
1153  } else { /* I-Frame */
1154  s->mb_intra = 1;
1156  }
1157 
1158  if (!IS_SKIP(mb_type)) {
1159  int i;
1160  s->dsp.clear_blocks(s->block[0]);
1161  /* decode each block */
1162  for (i = 0; i < 6; i++) {
1163  if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
1164  av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
1165  return -1;
1166  }
1167  cbp+=cbp;
1168  }
1169  }
1170 
1171  /* per-MB end of slice check */
1172 
1173  if(--s->mb_num_left <= 0){
1174  if(mpeg4_is_resync(s))
1175  return SLICE_END;
1176  else
1177  return SLICE_NOEND;
1178  }else{
1179  if(mpeg4_is_resync(s)){
1180  const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1181  if(s->cbp_table[xy+delta])
1182  return SLICE_END;
1183  }
1184  return SLICE_OK;
1185  }
1186 }
1187 
1189  int16_t block[6][64])
1190 {
1191  int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1192  int16_t *mot_val;
1193  static int8_t quant_tab[4] = { -1, -2, 1, 2 };
1194  const int xy= s->mb_x + s->mb_y * s->mb_stride;
1195 
1196  av_assert2(s->h263_pred);
1197 
1199  do{
1200  if (get_bits1(&s->gb)) {
1201  /* skip mb */
1202  s->mb_intra = 0;
1203  for(i=0;i<6;i++)
1204  s->block_last_index[i] = -1;
1205  s->mv_dir = MV_DIR_FORWARD;
1206  s->mv_type = MV_TYPE_16X16;
1209  s->mcsel=1;
1210  s->mv[0][0][0]= get_amv(s, 0);
1211  s->mv[0][0][1]= get_amv(s, 1);
1212 
1213  s->mb_skipped = 0;
1214  }else{
1216  s->mcsel=0;
1217  s->mv[0][0][0] = 0;
1218  s->mv[0][0][1] = 0;
1219  s->mb_skipped = 1;
1220  }
1221  goto end;
1222  }
1224  if (cbpc < 0){
1225  av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1226  return -1;
1227  }
1228  }while(cbpc == 20);
1229 
1230  s->dsp.clear_blocks(s->block[0]);
1231  dquant = cbpc & 8;
1232  s->mb_intra = ((cbpc & 4) != 0);
1233  if (s->mb_intra) goto intra;
1234 
1235  if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
1236  s->mcsel= get_bits1(&s->gb);
1237  else s->mcsel= 0;
1238  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1239 
1240  cbp = (cbpc & 3) | (cbpy << 2);
1241  if (dquant) {
1242  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1243  }
1244  if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
1245  s->interlaced_dct= get_bits1(&s->gb);
1246 
1247  s->mv_dir = MV_DIR_FORWARD;
1248  if ((cbpc & 16) == 0) {
1249  if(s->mcsel){
1251  /* 16x16 global motion prediction */
1252  s->mv_type = MV_TYPE_16X16;
1253  mx= get_amv(s, 0);
1254  my= get_amv(s, 1);
1255  s->mv[0][0][0] = mx;
1256  s->mv[0][0][1] = my;
1257  }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
1259  /* 16x8 field motion prediction */
1260  s->mv_type= MV_TYPE_FIELD;
1261 
1262  s->field_select[0][0]= get_bits1(&s->gb);
1263  s->field_select[0][1]= get_bits1(&s->gb);
1264 
1265  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1266 
1267  for(i=0; i<2; i++){
1268  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1269  if (mx >= 0xffff)
1270  return -1;
1271 
1272  my = ff_h263_decode_motion(s, pred_y/2, s->f_code);
1273  if (my >= 0xffff)
1274  return -1;
1275 
1276  s->mv[0][i][0] = mx;
1277  s->mv[0][i][1] = my;
1278  }
1279  }else{
1281  /* 16x16 motion prediction */
1282  s->mv_type = MV_TYPE_16X16;
1283  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1284  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1285 
1286  if (mx >= 0xffff)
1287  return -1;
1288 
1289  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1290 
1291  if (my >= 0xffff)
1292  return -1;
1293  s->mv[0][0][0] = mx;
1294  s->mv[0][0][1] = my;
1295  }
1296  } else {
1298  s->mv_type = MV_TYPE_8X8;
1299  for(i=0;i<4;i++) {
1300  mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1301  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1302  if (mx >= 0xffff)
1303  return -1;
1304 
1305  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1306  if (my >= 0xffff)
1307  return -1;
1308  s->mv[0][i][0] = mx;
1309  s->mv[0][i][1] = my;
1310  mot_val[0] = mx;
1311  mot_val[1] = my;
1312  }
1313  }
1314  } else if(s->pict_type==AV_PICTURE_TYPE_B) {
1315  int modb1; // first bit of modb
1316  int modb2; // second bit of modb
1317  int mb_type;
1318 
1319  s->mb_intra = 0; //B-frames never contain intra blocks
1320  s->mcsel=0; // ... true gmc blocks
1321 
1322  if(s->mb_x==0){
1323  for(i=0; i<2; i++){
1324  s->last_mv[i][0][0]=
1325  s->last_mv[i][0][1]=
1326  s->last_mv[i][1][0]=
1327  s->last_mv[i][1][1]= 0;
1328  }
1329 
1331  }
1332 
1333  /* if we skipped it in the future P Frame than skip it now too */
1334  s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1335 
1336  if(s->mb_skipped){
1337  /* skip mb */
1338  for(i=0;i<6;i++)
1339  s->block_last_index[i] = -1;
1340 
1341  s->mv_dir = MV_DIR_FORWARD;
1342  s->mv_type = MV_TYPE_16X16;
1343  s->mv[0][0][0] = 0;
1344  s->mv[0][0][1] = 0;
1345  s->mv[1][0][0] = 0;
1346  s->mv[1][0][1] = 0;
1348  goto end;
1349  }
1350 
1351  modb1= get_bits1(&s->gb);
1352  if(modb1){
1353  mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded
1354  cbp=0;
1355  }else{
1356  modb2= get_bits1(&s->gb);
1357  mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1358  if(mb_type<0){
1359  av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1360  return -1;
1361  }
1362  mb_type= mb_type_b_map[ mb_type ];
1363  if(modb2) cbp= 0;
1364  else{
1365  s->dsp.clear_blocks(s->block[0]);
1366  cbp= get_bits(&s->gb, 6);
1367  }
1368 
1369  if ((!IS_DIRECT(mb_type)) && cbp) {
1370  if(get_bits1(&s->gb)){
1371  ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
1372  }
1373  }
1374 
1375  if(!s->progressive_sequence){
1376  if(cbp)
1377  s->interlaced_dct= get_bits1(&s->gb);
1378 
1379  if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){
1380  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1381  mb_type &= ~MB_TYPE_16x16;
1382 
1383  if(USES_LIST(mb_type, 0)){
1384  s->field_select[0][0]= get_bits1(&s->gb);
1385  s->field_select[0][1]= get_bits1(&s->gb);
1386  }
1387  if(USES_LIST(mb_type, 1)){
1388  s->field_select[1][0]= get_bits1(&s->gb);
1389  s->field_select[1][1]= get_bits1(&s->gb);
1390  }
1391  }
1392  }
1393 
1394  s->mv_dir = 0;
1395  if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){
1396  s->mv_type= MV_TYPE_16X16;
1397 
1398  if(USES_LIST(mb_type, 0)){
1399  s->mv_dir = MV_DIR_FORWARD;
1400 
1401  mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1402  my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1403  s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1404  s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1405  }
1406 
1407  if(USES_LIST(mb_type, 1)){
1408  s->mv_dir |= MV_DIR_BACKWARD;
1409 
1410  mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1411  my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1412  s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1413  s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1414  }
1415  }else if(!IS_DIRECT(mb_type)){
1416  s->mv_type= MV_TYPE_FIELD;
1417 
1418  if(USES_LIST(mb_type, 0)){
1419  s->mv_dir = MV_DIR_FORWARD;
1420 
1421  for(i=0; i<2; i++){
1422  mx = ff_h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code);
1423  my = ff_h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code);
1424  s->last_mv[0][i][0]= s->mv[0][i][0] = mx;
1425  s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2;
1426  }
1427  }
1428 
1429  if(USES_LIST(mb_type, 1)){
1430  s->mv_dir |= MV_DIR_BACKWARD;
1431 
1432  for(i=0; i<2; i++){
1433  mx = ff_h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code);
1434  my = ff_h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code);
1435  s->last_mv[1][i][0]= s->mv[1][i][0] = mx;
1436  s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2;
1437  }
1438  }
1439  }
1440  }
1441 
1442  if(IS_DIRECT(mb_type)){
1443  if(IS_SKIP(mb_type))
1444  mx=my=0;
1445  else{
1446  mx = ff_h263_decode_motion(s, 0, 1);
1447  my = ff_h263_decode_motion(s, 0, 1);
1448  }
1449 
1451  mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1452  }
1453  s->current_picture.mb_type[xy] = mb_type;
1454  } else { /* I-Frame */
1455  do{
1457  if (cbpc < 0){
1458  av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1459  return -1;
1460  }
1461  }while(cbpc == 8);
1462 
1463  dquant = cbpc & 4;
1464  s->mb_intra = 1;
1465 intra:
1466  s->ac_pred = get_bits1(&s->gb);
1467  if(s->ac_pred)
1469  else
1471 
1472  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1473  if(cbpy<0){
1474  av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1475  return -1;
1476  }
1477  cbp = (cbpc & 3) | (cbpy << 2);
1478 
1480 
1481  if (dquant) {
1482  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1483  }
1484 
1485  if(!s->progressive_sequence)
1486  s->interlaced_dct= get_bits1(&s->gb);
1487 
1488  s->dsp.clear_blocks(s->block[0]);
1489  /* decode each block */
1490  for (i = 0; i < 6; i++) {
1491  if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
1492  return -1;
1493  cbp+=cbp;
1494  }
1495  goto end;
1496  }
1497 
1498  /* decode each block */
1499  for (i = 0; i < 6; i++) {
1500  if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
1501  return -1;
1502  cbp+=cbp;
1503  }
1504 end:
1505 
1506  /* per-MB end of slice check */
1507  if(s->codec_id==AV_CODEC_ID_MPEG4){
1508  int next= mpeg4_is_resync(s);
1509  if(next) {
1510  if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1511  return -1;
1512  } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1513  return SLICE_END;
1514 
1515  if(s->pict_type==AV_PICTURE_TYPE_B){
1516  const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1518  (s->mb_x + delta >= s->mb_width) ? FFMIN(s->mb_y+1, s->mb_height-1) : s->mb_y, 0);
1519  if (s->next_picture.mbskip_table[xy + delta])
1520  return SLICE_OK;
1521  }
1522 
1523  return SLICE_END;
1524  }
1525  }
1526 
1527  return SLICE_OK;
1528 }
1529 
1530 
1532  int hours, minutes, seconds;
1533 
1534  if(!show_bits(gb, 23)){
1535  av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1536  return -1;
1537  }
1538 
1539  hours= get_bits(gb, 5);
1540  minutes= get_bits(gb, 6);
1541  skip_bits1(gb);
1542  seconds= get_bits(gb, 6);
1543 
1544  s->time_base= seconds + 60*(minutes + 60*hours);
1545 
1546  skip_bits1(gb);
1547  skip_bits1(gb);
1548 
1549  return 0;
1550 }
1551 
1553 
1554  s->avctx->profile = get_bits(gb, 4);
1555  s->avctx->level = get_bits(gb, 4);
1556 
1557  // for Simple profile, level 0
1558  if (s->avctx->profile == 0 && s->avctx->level == 8) {
1559  s->avctx->level = 0;
1560  }
1561 
1562  return 0;
1563 }
1564 
1566  int width, height, vo_ver_id;
1567 
1568  /* vol header */
1569  skip_bits(gb, 1); /* random access */
1570  s->vo_type= get_bits(gb, 8);
1571  if (get_bits1(gb) != 0) { /* is_ol_id */
1572  vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1573  skip_bits(gb, 3); /* vo_priority */
1574  } else {
1575  vo_ver_id = 1;
1576  }
1577  s->aspect_ratio_info= get_bits(gb, 4);
1579  s->avctx->sample_aspect_ratio.num= get_bits(gb, 8); // par_width
1580  s->avctx->sample_aspect_ratio.den= get_bits(gb, 8); // par_height
1581  }else{
1583  }
1584 
1585  if ((s->vol_control_parameters=get_bits1(gb))) { /* vol control parameter */
1586  int chroma_format= get_bits(gb, 2);
1587  if(chroma_format!=CHROMA_420){
1588  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1589  }
1590  s->low_delay= get_bits1(gb);
1591  if(get_bits1(gb)){ /* vbv parameters */
1592  get_bits(gb, 15); /* first_half_bitrate */
1593  skip_bits1(gb); /* marker */
1594  get_bits(gb, 15); /* latter_half_bitrate */
1595  skip_bits1(gb); /* marker */
1596  get_bits(gb, 15); /* first_half_vbv_buffer_size */
1597  skip_bits1(gb); /* marker */
1598  get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1599  get_bits(gb, 11); /* first_half_vbv_occupancy */
1600  skip_bits1(gb); /* marker */
1601  get_bits(gb, 15); /* latter_half_vbv_occupancy */
1602  skip_bits1(gb); /* marker */
1603  }
1604  }else{
1605  // set low delay flag only once the smartest? low delay detection won't be overridden
1606  if(s->picture_number==0)
1607  s->low_delay=0;
1608  }
1609 
1610  s->shape = get_bits(gb, 2); /* vol shape */
1611  if(s->shape != RECT_SHAPE) av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1612  if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
1613  av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1614  skip_bits(gb, 4); //video_object_layer_shape_extension
1615  }
1616 
1617  check_marker(gb, "before time_increment_resolution");
1618 
1619  s->avctx->time_base.den = get_bits(gb, 16);
1620  if(!s->avctx->time_base.den){
1621  av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
1622  s->avctx->time_base.num = 0;
1623  return -1;
1624  }
1625 
1626  s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1627  if (s->time_increment_bits < 1)
1628  s->time_increment_bits = 1;
1629 
1630  check_marker(gb, "before fixed_vop_rate");
1631 
1632  if (get_bits1(gb) != 0) { /* fixed_vop_rate */
1634  }else
1635  s->avctx->time_base.num = 1;
1636 
1637  s->t_frame=0;
1638 
1639  if (s->shape != BIN_ONLY_SHAPE) {
1640  if (s->shape == RECT_SHAPE) {
1641  check_marker(gb, "before width");
1642  width = get_bits(gb, 13);
1643  check_marker(gb, "before height");
1644  height = get_bits(gb, 13);
1645  check_marker(gb, "after height");
1646  if(width && height && !(s->width && s->codec_tag == AV_RL32("MP4S"))){ /* they should be non zero but who knows ... */
1647  if (s->width && s->height &&
1648  (s->width != width || s->height != height))
1649  s->context_reinit = 1;
1650  s->width = width;
1651  s->height = height;
1652  }
1653  }
1654 
1656  s->progressive_frame= get_bits1(gb)^1;
1657  s->interlaced_dct=0;
1658  if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1659  av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
1660  if (vo_ver_id == 1) {
1661  s->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1662  } else {
1663  s->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1664  }
1665  if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1668  s->sprite_width = get_bits(gb, 13);
1669  skip_bits1(gb); /* marker */
1670  s->sprite_height= get_bits(gb, 13);
1671  skip_bits1(gb); /* marker */
1672  s->sprite_left = get_bits(gb, 13);
1673  skip_bits1(gb); /* marker */
1674  s->sprite_top = get_bits(gb, 13);
1675  skip_bits1(gb); /* marker */
1676  }
1678  if(s->num_sprite_warping_points > 3){
1679  av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points);
1681  return -1;
1682  }
1683  s->sprite_warping_accuracy = get_bits(gb, 2);
1686  s->low_latency_sprite= get_bits1(gb);
1687  }
1688  // FIXME sadct disable bit if verid!=1 && shape not rect
1689 
1690  if (get_bits1(gb) == 1) { /* not_8_bit */
1691  s->quant_precision = get_bits(gb, 4); /* quant_precision */
1692  if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n"); /* bits_per_pixel */
1693  if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precision %d\n", s->quant_precision);
1694  if(s->quant_precision<3 || s->quant_precision>9) {
1695  s->quant_precision = 5;
1696  }
1697  } else {
1698  s->quant_precision = 5;
1699  }
1700 
1701  // FIXME a bunch of grayscale shape things
1702 
1703  if((s->mpeg_quant=get_bits1(gb))){ /* vol_quant_type */
1704  int i, v;
1705 
1706  /* load default matrixes */
1707  for(i=0; i<64; i++){
1708  int j= s->dsp.idct_permutation[i];
1710  s->intra_matrix[j]= v;
1711  s->chroma_intra_matrix[j]= v;
1712 
1714  s->inter_matrix[j]= v;
1715  s->chroma_inter_matrix[j]= v;
1716  }
1717 
1718  /* load custom intra matrix */
1719  if(get_bits1(gb)){
1720  int last=0;
1721  for(i=0; i<64; i++){
1722  int j;
1723  v= get_bits(gb, 8);
1724  if(v==0) break;
1725 
1726  last= v;
1728  s->intra_matrix[j]= v;
1729  s->chroma_intra_matrix[j]= v;
1730  }
1731 
1732  /* replicate last value */
1733  for(; i<64; i++){
1734  int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1735  s->intra_matrix[j]= last;
1736  s->chroma_intra_matrix[j]= last;
1737  }
1738  }
1739 
1740  /* load custom non intra matrix */
1741  if(get_bits1(gb)){
1742  int last=0;
1743  for(i=0; i<64; i++){
1744  int j;
1745  v= get_bits(gb, 8);
1746  if(v==0) break;
1747 
1748  last= v;
1750  s->inter_matrix[j]= v;
1751  s->chroma_inter_matrix[j]= v;
1752  }
1753 
1754  /* replicate last value */
1755  for(; i<64; i++){
1756  int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1757  s->inter_matrix[j]= last;
1758  s->chroma_inter_matrix[j]= last;
1759  }
1760  }
1761 
1762  // FIXME a bunch of grayscale shape things
1763  }
1764 
1765  if(vo_ver_id != 1)
1766  s->quarter_sample= get_bits1(gb);
1767  else s->quarter_sample=0;
1768 
1769  if(!get_bits1(gb)){
1770  int pos= get_bits_count(gb);
1771  int estimation_method= get_bits(gb, 2);
1772  if(estimation_method<2){
1773  if(!get_bits1(gb)){
1774  s->cplx_estimation_trash_i += 8*get_bits1(gb); //opaque
1775  s->cplx_estimation_trash_i += 8*get_bits1(gb); //transparent
1776  s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_cae
1777  s->cplx_estimation_trash_i += 8*get_bits1(gb); //inter_cae
1778  s->cplx_estimation_trash_i += 8*get_bits1(gb); //no_update
1779  s->cplx_estimation_trash_i += 8*get_bits1(gb); //upampling
1780  }
1781  if(!get_bits1(gb)){
1782  s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_blocks
1783  s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter_blocks
1784  s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter4v_blocks
1785  s->cplx_estimation_trash_i += 8*get_bits1(gb); //not coded blocks
1786  }
1787  if(!check_marker(gb, "in complexity estimation part 1")){
1788  skip_bits_long(gb, pos - get_bits_count(gb));
1789  goto no_cplx_est;
1790  }
1791  if(!get_bits1(gb)){
1792  s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_coeffs
1793  s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_lines
1794  s->cplx_estimation_trash_i += 8*get_bits1(gb); //vlc_syms
1795  s->cplx_estimation_trash_i += 4*get_bits1(gb); //vlc_bits
1796  }
1797  if(!get_bits1(gb)){
1798  s->cplx_estimation_trash_p += 8*get_bits1(gb); //apm
1799  s->cplx_estimation_trash_p += 8*get_bits1(gb); //npm
1800  s->cplx_estimation_trash_b += 8*get_bits1(gb); //interpolate_mc_q
1801  s->cplx_estimation_trash_p += 8*get_bits1(gb); //forwback_mc_q
1802  s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel2
1803  s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel4
1804  }
1805  if(!check_marker(gb, "in complexity estimation part 2")){
1806  skip_bits_long(gb, pos - get_bits_count(gb));
1807  goto no_cplx_est;
1808  }
1809  if(estimation_method==1){
1810  s->cplx_estimation_trash_i += 8*get_bits1(gb); //sadct
1811  s->cplx_estimation_trash_p += 8*get_bits1(gb); //qpel
1812  }
1813  }else
1814  av_log(s->avctx, AV_LOG_ERROR, "Invalid Complexity estimation method %d\n", estimation_method);
1815  }else{
1816 no_cplx_est:
1820  }
1821 
1822  s->resync_marker= !get_bits1(gb); /* resync_marker_disabled */
1823 
1824  s->data_partitioning= get_bits1(gb);
1825  if(s->data_partitioning){
1826  s->rvlc= get_bits1(gb);
1827  }
1828 
1829  if(vo_ver_id != 1) {
1830  s->new_pred= get_bits1(gb);
1831  if(s->new_pred){
1832  av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1833  skip_bits(gb, 2); /* requested upstream message type */
1834  skip_bits1(gb); /* newpred segment type */
1835  }
1836  s->reduced_res_vop= get_bits1(gb);
1837  if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
1838  }
1839  else{
1840  s->new_pred=0;
1841  s->reduced_res_vop= 0;
1842  }
1843 
1844  s->scalability= get_bits1(gb);
1845 
1846  if (s->scalability) {
1847  GetBitContext bak= *gb;
1848  int h_sampling_factor_n;
1849  int h_sampling_factor_m;
1850  int v_sampling_factor_n;
1851  int v_sampling_factor_m;
1852 
1853  s->hierachy_type= get_bits1(gb);
1854  skip_bits(gb, 4); /* ref_layer_id */
1855  skip_bits1(gb); /* ref_layer_sampling_dir */
1856  h_sampling_factor_n= get_bits(gb, 5);
1857  h_sampling_factor_m= get_bits(gb, 5);
1858  v_sampling_factor_n= get_bits(gb, 5);
1859  v_sampling_factor_m= get_bits(gb, 5);
1860  s->enhancement_type= get_bits1(gb);
1861 
1862  if( h_sampling_factor_n==0 || h_sampling_factor_m==0
1863  || v_sampling_factor_n==0 || v_sampling_factor_m==0){
1864  /* illegal scalability header (VERY broken encoder),
1865  * trying to workaround */
1866  s->scalability=0;
1867  *gb= bak;
1868  }else
1869  av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
1870 
1871  // bin shape stuff FIXME
1872  }
1873  }
1874 
1875  if(s->avctx->debug&FF_DEBUG_PICT_INFO) {
1876  av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
1877  s->avctx->time_base.num, s->avctx->time_base.den,
1879  s->quant_precision,
1881  s->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
1882  s->data_partitioning ? "partition " : "", s->rvlc ? "rvlc " : ""
1883  );
1884  }
1885 
1886  return 0;
1887 }
1888 
1889 /**
1890  * Decode the user data stuff in the header.
1891  * Also initializes divx/xvid/lavc_version/build.
1892  */
1894  char buf[256];
1895  int i;
1896  int e;
1897  int ver = 0, build = 0, ver2 = 0, ver3 = 0;
1898  char last;
1899 
1900  for(i=0; i<255 && get_bits_count(gb) < gb->size_in_bits; i++){
1901  if(show_bits(gb, 23) == 0) break;
1902  buf[i]= get_bits(gb, 8);
1903  }
1904  buf[i]=0;
1905 
1906  /* divx detection */
1907  e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
1908  if(e<2)
1909  e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
1910  if(e>=2){
1911  s->divx_version= ver;
1912  s->divx_build= build;
1913  s->divx_packed= e==3 && last=='p';
1914  if(s->divx_packed && !s->showed_packed_warning) {
1915  av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
1916  "wasteful way to store B-frames ('packed B-frames'). "
1917  "Consider using a tool like VirtualDub or avidemux to fix it.\n");
1918  s->showed_packed_warning=1;
1919  }
1920  }
1921 
1922  /* libavcodec detection */
1923  e=sscanf(buf, "FFmpe%*[^b]b%d", &build)+3;
1924  if(e!=4)
1925  e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
1926  if(e!=4){
1927  e=sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3)+1;
1928  if (e>1)
1929  build= (ver<<16) + (ver2<<8) + ver3;
1930  }
1931  if(e!=4){
1932  if(strcmp(buf, "ffmpeg")==0){
1933  s->lavc_build= 4600;
1934  }
1935  }
1936  if(e==4){
1937  s->lavc_build= build;
1938  }
1939 
1940  /* Xvid detection */
1941  e=sscanf(buf, "XviD%d", &build);
1942  if(e==1){
1943  s->xvid_build= build;
1944  }
1945 
1946  return 0;
1947 }
1948 
1950  int time_incr, time_increment;
1951 
1952  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
1954  av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
1955  s->low_delay=0;
1956  }
1957 
1959  if(s->partitioned_frame)
1961  else
1963 
1964  time_incr=0;
1965  while (get_bits1(gb) != 0)
1966  time_incr++;
1967 
1968  check_marker(gb, "before time_increment");
1969 
1970  if(s->time_increment_bits==0 || !(show_bits(gb, s->time_increment_bits+1)&1)){
1971  av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
1972 
1974  if ( s->pict_type == AV_PICTURE_TYPE_P
1976  if((show_bits(gb, s->time_increment_bits+6)&0x37) == 0x30) break;
1977  }else
1978  if((show_bits(gb, s->time_increment_bits+5)&0x1F) == 0x18) break;
1979  }
1980 
1981  av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
1982  if (s->avctx->time_base.den && 4*s->avctx->time_base.den < 1<<s->time_increment_bits) {
1983  s->avctx->time_base.den = 1<<s->time_increment_bits;
1984  }
1985  }
1986 
1987  if(IS_3IV1) time_increment= get_bits1(gb); //FIXME investigate further
1988  else time_increment= get_bits(gb, s->time_increment_bits);
1989 
1990  if(s->pict_type!=AV_PICTURE_TYPE_B){
1991  s->last_time_base= s->time_base;
1992  s->time_base+= time_incr;
1993  s->time= s->time_base*s->avctx->time_base.den + time_increment;
1995  if(s->time < s->last_non_b_time){
1996  /* header is not mpeg-4-compatible, broken encoder,
1997  * trying to workaround */
1998  s->time_base++;
1999  s->time+= s->avctx->time_base.den;
2000  }
2001  }
2002  s->pp_time= s->time - s->last_non_b_time;
2003  s->last_non_b_time= s->time;
2004  }else{
2005  s->time= (s->last_time_base + time_incr)*s->avctx->time_base.den + time_increment;
2006  s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2007  if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
2008  /* messed up order, maybe after seeking? skipping current b-frame */
2009  return FRAME_SKIPPED;
2010  }
2012 
2013  if(s->t_frame==0) s->t_frame= s->pb_time;
2014  if(s->t_frame==0) s->t_frame=1; // 1/0 protection
2016  - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
2017  s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
2018  - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
2019  if(!s->progressive_sequence){
2020  if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
2021  return FRAME_SKIPPED;
2022  }
2023  }
2024 
2025  if(s->avctx->time_base.num)
2027  else
2029  if(s->avctx->debug&FF_DEBUG_PTS)
2030  av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
2031  s->current_picture_ptr->f.pts);
2032 
2033  check_marker(gb, "before vop_coded");
2034 
2035  /* vop coded */
2036  if (get_bits1(gb) != 1){
2038  av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2039  return FRAME_SKIPPED;
2040  }
2041  if (s->new_pred)
2042  decode_new_pred(s, gb);
2043 
2044  if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == AV_PICTURE_TYPE_P
2046  /* rounding type for motion estimation */
2047  s->no_rounding = get_bits1(gb);
2048  } else {
2049  s->no_rounding = 0;
2050  }
2051 //FIXME reduced res stuff
2052 
2053  if (s->shape != RECT_SHAPE) {
2054  if (s->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2055  skip_bits(gb, 13); /* width */
2056  skip_bits1(gb); /* marker */
2057  skip_bits(gb, 13); /* height */
2058  skip_bits1(gb); /* marker */
2059  skip_bits(gb, 13); /* hor_spat_ref */
2060  skip_bits1(gb); /* marker */
2061  skip_bits(gb, 13); /* ver_spat_ref */
2062  }
2063  skip_bits1(gb); /* change_CR_disable */
2064 
2065  if (get_bits1(gb) != 0) {
2066  skip_bits(gb, 8); /* constant_alpha_value */
2067  }
2068  }
2069 //FIXME complexity estimation stuff
2070 
2071  if (s->shape != BIN_ONLY_SHAPE) {
2073  if(s->pict_type != AV_PICTURE_TYPE_I)
2075  if(s->pict_type == AV_PICTURE_TYPE_B)
2077 
2078  if(get_bits_left(gb) < 3) {
2079  av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2080  return -1;
2081  }
2083  if(!s->progressive_sequence){
2084  s->top_field_first= get_bits1(gb);
2085  s->alternate_scan= get_bits1(gb);
2086  }else
2087  s->alternate_scan= 0;
2088  }
2089 
2090  if(s->alternate_scan){
2095  } else{
2100  }
2101 
2103  if (mpeg4_decode_sprite_trajectory(s, gb) < 0)
2104  return AVERROR_INVALIDDATA;
2105  if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
2106  if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2107  }
2108 
2109  if (s->shape != BIN_ONLY_SHAPE) {
2110  s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
2111  if(s->qscale==0){
2112  av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
2113  return -1; // makes no sense to continue, as there is nothing left from the image then
2114  }
2115 
2116  if (s->pict_type != AV_PICTURE_TYPE_I) {
2117  s->f_code = get_bits(gb, 3); /* fcode_for */
2118  if(s->f_code==0){
2119  av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
2120  s->f_code=1;
2121  return -1; // makes no sense to continue, as the MV decoding will break very quickly
2122  }
2123  }else
2124  s->f_code=1;
2125 
2126  if (s->pict_type == AV_PICTURE_TYPE_B) {
2127  s->b_code = get_bits(gb, 3);
2128  if(s->b_code==0){
2129  av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (b_code=0)\n");
2130  s->b_code=1;
2131  return -1; // makes no sense to continue, as the MV decoding will break very quickly
2132  }
2133  }else
2134  s->b_code=1;
2135 
2136  if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2137  av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
2138  s->qscale, s->f_code, s->b_code,
2139  s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
2144  s->time,
2145  time_increment
2146  );
2147  }
2148 
2149  if(!s->scalability){
2150  if (s->shape!=RECT_SHAPE && s->pict_type!=AV_PICTURE_TYPE_I) {
2151  skip_bits1(gb); // vop shape coding type
2152  }
2153  }else{
2154  if(s->enhancement_type){
2155  int load_backward_shape= get_bits1(gb);
2156  if(load_backward_shape){
2157  av_log(s->avctx, AV_LOG_ERROR, "load backward shape isn't supported\n");
2158  }
2159  }
2160  skip_bits(gb, 2); //ref_select_code
2161  }
2162  }
2163  /* detect buggy encoders which don't set the low_delay flag (divx4/xvid/opendivx)*/
2164  // note we cannot detect divx5 without b-frames easily (although it's buggy too)
2165  if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==-1 && s->picture_number==0){
2166  av_log(s->avctx, AV_LOG_WARNING, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2167  s->low_delay=1;
2168  }
2169 
2170  s->picture_number++; // better than pic number==0 always ;)
2171 
2172  s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
2174 
2176  s->h_edge_pos= s->width;
2177  s->v_edge_pos= s->height;
2178  }
2179  return 0;
2180 }
2181 
2182 /**
2183  * Decode mpeg4 headers.
2184  * @return <0 if no VOP found (or a damaged one)
2185  * FRAME_SKIPPED if a not coded VOP is found
2186  * 0 if a VOP is found
2187  */
2189 {
2190  unsigned startcode, v;
2191 
2192  /* search next start code */
2193  align_get_bits(gb);
2194 
2195  if(s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630){
2196  skip_bits(gb, 24);
2197  if(get_bits(gb, 8) == 0xF0)
2198  goto end;
2199  }
2200 
2201  startcode = 0xff;
2202  for(;;) {
2203  if(get_bits_count(gb) >= gb->size_in_bits){
2204  if(gb->size_in_bits==8 && (s->divx_version>=0 || s->xvid_build>=0) || s->codec_tag == AV_RL32("QMP4")){
2205  av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2206  return FRAME_SKIPPED; //divx bug
2207  }else
2208  return -1; //end of stream
2209  }
2210 
2211  /* use the bits after the test */
2212  v = get_bits(gb, 8);
2213  startcode = ((startcode << 8) | v) & 0xffffffff;
2214 
2215  if((startcode&0xFFFFFF00) != 0x100)
2216  continue; //no startcode
2217 
2218  if(s->avctx->debug&FF_DEBUG_STARTCODE){
2219  av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2220  if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2221  else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2222  else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2223  else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2224  else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2225  else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2226  else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2227  else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2228  else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2229  else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2230  else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2231  else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2232  else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2233  else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2234  else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2235  else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2236  else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2237  else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2238  else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2239  else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2240  else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2241  else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2242  else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2243  else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2244  else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2245  else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2246  else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
2247  av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2248  }
2249 
2250  if(startcode >= 0x120 && startcode <= 0x12F){
2251  if(decode_vol_header(s, gb) < 0)
2252  return -1;
2253  }
2254  else if(startcode == USER_DATA_STARTCODE){
2255  decode_user_data(s, gb);
2256  }
2257  else if(startcode == GOP_STARTCODE){
2258  mpeg4_decode_gop_header(s, gb);
2259  }
2260  else if(startcode == VOS_STARTCODE){
2262  }
2263  else if(startcode == VOP_STARTCODE){
2264  break;
2265  }
2266 
2267  align_get_bits(gb);
2268  startcode = 0xff;
2269  }
2270 end:
2271  if(s->flags& CODEC_FLAG_LOW_DELAY)
2272  s->low_delay=1;
2273  s->avctx->has_b_frames= !s->low_delay;
2274  return decode_vop_header(s, gb);
2275 }
2276 
2278  static int done = 0;
2279 
2280  if (!done) {
2287  INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2288  &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2289  &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2290  INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2291  &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2292  &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2293  INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2294  &ff_sprite_trajectory_tab[0][1], 4, 2,
2295  &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2296  INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2297  &ff_mb_type_b_tab[0][1], 2, 1,
2298  &ff_mb_type_b_tab[0][0], 2, 1, 16);
2299  done = 1;
2300  }
2301 }
2302 
2304 {
2305  MpegEncContext *s = avctx->priv_data;
2306  int ret;
2307 
2308  s->divx_version=
2309  s->divx_build=
2310  s->xvid_build=
2311  s->lavc_build= -1;
2312 
2313  if((ret=ff_h263_decode_init(avctx)) < 0)
2314  return ret;
2315 
2317 
2318  s->h263_pred = 1;
2319  s->low_delay = 0; //default, might be overridden in the vol header during header parsing
2321  s->time_increment_bits = 4; /* default value for broken headers */
2323 
2324  avctx->internal->allocate_progress = 1;
2325 
2326  return 0;
2327 }
2328 
2330  { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
2331  { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
2332  { FF_PROFILE_MPEG4_CORE, "Core Profile" },
2333  { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
2334  { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
2335  { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
2336  { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
2337  { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
2338  { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
2339  { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
2340  { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
2341  { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
2342  { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
2343  { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
2344  { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
2345  { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
2346  { FF_PROFILE_UNKNOWN },
2347 };
2348 
2349 static const AVOption mpeg4_options[] = {
2350  {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
2351  {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
2352  {NULL}
2353 };
2354 
2355 static const AVClass mpeg4_class = {
2356  "MPEG4 Video Decoder",
2358  mpeg4_options,
2360 };
2361 
2362 static const AVClass mpeg4_vdpau_class = {
2363  "MPEG4 Video VDPAU Decoder",
2365  mpeg4_options,
2367 };
2368 
2370  .name = "mpeg4",
2371  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2372  .type = AVMEDIA_TYPE_VIDEO,
2373  .id = AV_CODEC_ID_MPEG4,
2374  .priv_data_size = sizeof(MpegEncContext),
2375  .init = decode_init,
2378  .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2381  .flush = ff_mpeg_flush,
2382  .max_lowres = 3,
2383  .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
2384  .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
2385  .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_mpeg_update_thread_context),
2386  .priv_class = &mpeg4_class,
2387 };
2388 
2389 
2390 #if CONFIG_MPEG4_VDPAU_DECODER
2391 AVCodec ff_mpeg4_vdpau_decoder = {
2392  .name = "mpeg4_vdpau",
2393  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2394  .type = AVMEDIA_TYPE_VIDEO,
2395  .id = AV_CODEC_ID_MPEG4,
2396  .priv_data_size = sizeof(MpegEncContext),
2397  .init = decode_init,
2400  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2401  CODEC_CAP_HWACCEL_VDPAU,
2402  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_MPEG4,
2403  AV_PIX_FMT_NONE },
2404  .priv_class = &mpeg4_vdpau_class,
2405 };
2406 #endif