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