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