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