3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define UNCHECKED_BITSTREAM_READER 1
25 #include "libavutil/opt.h"
26 #include "error_resilience.h"
28 #include "mpegvideo.h"
29 #include "mpeg4video.h"
33 // The defines below define the number of bits that are read at once for
34 // reading vlc values. Changing these may improve speed and data cache needs
35 // be aware though that decreasing them may need the number of stages that is
36 // passed to get_vlc* to be increased.
37 #define SPRITE_TRAJ_VLC_BITS 6
39 #define MB_TYPE_B_VLC_BITS 4
42 static VLC dc_lum, dc_chrom;
43 static VLC sprite_trajectory;
44 static VLC mb_type_b_vlc;
46 static const int mb_type_b_map[4]= {
47 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
48 MB_TYPE_L0L1 | MB_TYPE_16x16,
49 MB_TYPE_L1 | MB_TYPE_16x16,
50 MB_TYPE_L0 | MB_TYPE_16x16,
55 * @param n block index (0-3 are luma, 4-5 are chroma)
56 * @param dir the ac prediction direction
58 void ff_mpeg4_pred_ac(MpegEncContext * s, int16_t *block, int n,
62 int16_t *ac_val, *ac_val1;
63 int8_t * const qscale_table = s->current_picture.qscale_table;
66 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
70 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
74 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
77 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
80 /* different qscale, we must rescale */
82 block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
86 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
88 ac_val -= 16 * s->block_wrap[n];
90 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
93 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
96 /* different qscale, we must rescale */
98 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
105 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
109 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
114 * check if the next stuff is a resync marker or the end.
117 static inline int mpeg4_is_resync(MpegEncContext *s){
118 int bits_count= get_bits_count(&s->gb);
119 int v= show_bits(&s->gb, 16);
121 if(s->workaround_bugs&FF_BUG_NO_PADDING && !s->resync_marker){
126 if(s->pict_type==AV_PICTURE_TYPE_B || (v>>(8-s->pict_type)!=1) || s->partitioned_frame)
128 skip_bits(&s->gb, 8+s->pict_type);
129 bits_count+= 8+s->pict_type;
130 v= show_bits(&s->gb, 16);
133 if(bits_count + 8 >= s->gb.size_in_bits){
135 v|= 0x7F >> (7-(bits_count&7));
140 if(v == ff_mpeg4_resync_prefix[bits_count&7]){
142 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
143 GetBitContext gb= s->gb;
145 skip_bits(&s->gb, 1);
146 align_get_bits(&s->gb);
148 for(len=0; len<32; len++){
149 if(get_bits1(&s->gb)) break;
152 mb_num= get_bits(&s->gb, mb_num_bits);
153 if(!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
158 if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
165 static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
168 int a= 2<<s->sprite_warping_accuracy;
169 int rho= 3-s->sprite_warping_accuracy;
171 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
172 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
173 int sprite_ref[4][2];
174 int virtual_ref[2][2];
181 if (w <= 0 || h <= 0)
182 return AVERROR_INVALIDDATA;
184 for(i=0; i<s->num_sprite_warping_points; i++){
188 length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
190 x= get_xbits(gb, length);
192 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(gb); /* marker bit */
194 length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
196 y=get_xbits(gb, length);
198 skip_bits1(gb); /* marker bit */
199 s->sprite_traj[i][0]= d[i][0]= x;
200 s->sprite_traj[i][1]= d[i][1]= y;
203 s->sprite_traj[i][0]= s->sprite_traj[i][1]= 0;
205 while((1<<alpha)<w) alpha++;
206 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
210 // Note, the 4th point isn't used for GMC
211 if(s->divx_version==500 && s->divx_build==413){
212 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
213 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
214 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
215 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
216 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
217 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
219 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
220 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
221 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
222 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
223 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
224 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
226 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
227 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
229 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
230 // perhaps it should be reordered to be more readable ...
231 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
232 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
233 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
234 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
235 virtual_ref[0][1]= 16*vop_ref[0][1]
236 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
237 virtual_ref[1][0]= 16*vop_ref[0][0]
238 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
239 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
240 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
242 switch(s->num_sprite_warping_points)
245 s->sprite_offset[0][0]= 0;
246 s->sprite_offset[0][1]= 0;
247 s->sprite_offset[1][0]= 0;
248 s->sprite_offset[1][1]= 0;
249 s->sprite_delta[0][0]= a;
250 s->sprite_delta[0][1]= 0;
251 s->sprite_delta[1][0]= 0;
252 s->sprite_delta[1][1]= a;
253 s->sprite_shift[0]= 0;
254 s->sprite_shift[1]= 0;
257 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
258 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
259 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
260 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
261 s->sprite_delta[0][0]= a;
262 s->sprite_delta[0][1]= 0;
263 s->sprite_delta[1][0]= 0;
264 s->sprite_delta[1][1]= a;
265 s->sprite_shift[0]= 0;
266 s->sprite_shift[1]= 0;
269 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
270 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
271 + ( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])
272 + (1<<(alpha+rho-1));
273 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
274 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
275 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])
276 + (1<<(alpha+rho-1));
277 s->sprite_offset[1][0]= ( (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
278 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
279 +2*w2*r*sprite_ref[0][0]
281 + (1<<(alpha+rho+1)));
282 s->sprite_offset[1][1]= ( (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
283 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
284 +2*w2*r*sprite_ref[0][1]
286 + (1<<(alpha+rho+1)));
287 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
288 s->sprite_delta[0][1]= (+r*sprite_ref[0][1] - virtual_ref[0][1]);
289 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
290 s->sprite_delta[1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
292 s->sprite_shift[0]= alpha+rho;
293 s->sprite_shift[1]= alpha+rho+2;
296 min_ab= FFMIN(alpha, beta);
299 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+beta+rho-min_ab))
300 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-vop_ref[0][0])
301 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-vop_ref[0][1])
302 + (1<<(alpha+beta+rho-min_ab-1));
303 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+beta+rho-min_ab))
304 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-vop_ref[0][0])
305 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-vop_ref[0][1])
306 + (1<<(alpha+beta+rho-min_ab-1));
307 s->sprite_offset[1][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-2*vop_ref[0][0] + 1)
308 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-2*vop_ref[0][1] + 1)
309 + 2*w2*h3*r*sprite_ref[0][0]
311 + (1<<(alpha+beta+rho-min_ab+1));
312 s->sprite_offset[1][1]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-2*vop_ref[0][0] + 1)
313 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-2*vop_ref[0][1] + 1)
314 + 2*w2*h3*r*sprite_ref[0][1]
316 + (1<<(alpha+beta+rho-min_ab+1));
317 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3;
318 s->sprite_delta[0][1]= (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3;
319 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3;
320 s->sprite_delta[1][1]= (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3;
322 s->sprite_shift[0]= alpha + beta + rho - min_ab;
323 s->sprite_shift[1]= alpha + beta + rho - min_ab + 2;
326 /* try to simplify the situation */
327 if( s->sprite_delta[0][0] == a<<s->sprite_shift[0]
328 && s->sprite_delta[0][1] == 0
329 && s->sprite_delta[1][0] == 0
330 && s->sprite_delta[1][1] == a<<s->sprite_shift[0])
332 s->sprite_offset[0][0]>>=s->sprite_shift[0];
333 s->sprite_offset[0][1]>>=s->sprite_shift[0];
334 s->sprite_offset[1][0]>>=s->sprite_shift[1];
335 s->sprite_offset[1][1]>>=s->sprite_shift[1];
336 s->sprite_delta[0][0]= a;
337 s->sprite_delta[0][1]= 0;
338 s->sprite_delta[1][0]= 0;
339 s->sprite_delta[1][1]= a;
340 s->sprite_shift[0]= 0;
341 s->sprite_shift[1]= 0;
342 s->real_sprite_warping_points=1;
345 int shift_y= 16 - s->sprite_shift[0];
346 int shift_c= 16 - s->sprite_shift[1];
348 s->sprite_offset[0][i]<<= shift_y;
349 s->sprite_offset[1][i]<<= shift_c;
350 s->sprite_delta[0][i]<<= shift_y;
351 s->sprite_delta[1][i]<<= shift_y;
352 s->sprite_shift[i]= 16;
354 s->real_sprite_warping_points= s->num_sprite_warping_points;
359 static int decode_new_pred(MpegEncContext *s, GetBitContext *gb){
360 int len = FFMIN(s->time_increment_bits + 3, 15);
365 check_marker(gb, "after new_pred");
371 * Decode the next video packet.
372 * @return <0 if something went wrong
374 int ff_mpeg4_decode_video_packet_header(MpegEncContext *s)
376 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
377 int header_extension=0, mb_num, len;
379 /* is there enough space left for a video packet + header */
380 if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
382 for(len=0; len<32; len++){
383 if(get_bits1(&s->gb)) break;
386 if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
387 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
391 if(s->shape != RECT_SHAPE){
392 header_extension= get_bits1(&s->gb);
393 //FIXME more stuff here
396 mb_num= get_bits(&s->gb, mb_num_bits);
397 if(mb_num>=s->mb_num){
398 av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
402 s->mb_x= mb_num % s->mb_width;
403 s->mb_y= mb_num / s->mb_width;
405 if(s->shape != BIN_ONLY_SHAPE){
406 int qscale= get_bits(&s->gb, s->quant_precision);
408 s->chroma_qscale=s->qscale= qscale;
411 if(s->shape == RECT_SHAPE){
412 header_extension= get_bits1(&s->gb);
414 if(header_extension){
417 while (get_bits1(&s->gb) != 0)
420 check_marker(&s->gb, "before time_increment in video packed header");
421 skip_bits(&s->gb, s->time_increment_bits); /* time_increment */
422 check_marker(&s->gb, "before vop_coding_type in video packed header");
424 skip_bits(&s->gb, 2); /* vop coding type */
425 //FIXME not rect stuff here
427 if(s->shape != BIN_ONLY_SHAPE){
428 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
429 //FIXME don't just ignore everything
430 if(s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
431 if (mpeg4_decode_sprite_trajectory(s, &s->gb) < 0)
432 return AVERROR_INVALIDDATA;
433 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
436 //FIXME reduced res stuff here
438 if (s->pict_type != AV_PICTURE_TYPE_I) {
439 int f_code = get_bits(&s->gb, 3); /* fcode_for */
441 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
444 if (s->pict_type == AV_PICTURE_TYPE_B) {
445 int b_code = get_bits(&s->gb, 3);
447 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
453 decode_new_pred(s, &s->gb);
459 * Get the average motion vector for a GMC MB.
460 * @param n either 0 for the x component or 1 for y
461 * @return the average MV for a GMC MB
463 static inline int get_amv(MpegEncContext *s, int n){
464 int x, y, mb_v, sum, dx, dy, shift;
465 int len = 1 << (s->f_code + 4);
466 const int a= s->sprite_warping_accuracy;
468 if(s->workaround_bugs & FF_BUG_AMV)
469 len >>= s->quarter_sample;
471 if(s->real_sprite_warping_points==1){
472 if(s->divx_version==500 && s->divx_build==413)
473 sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
475 sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
477 dx= s->sprite_delta[n][0];
478 dy= s->sprite_delta[n][1];
479 shift= s->sprite_shift[0];
480 if(n) dy -= 1<<(shift + a + 1);
481 else dx -= 1<<(shift + a + 1);
482 mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
495 sum= RSHIFT(sum, a+8-s->quarter_sample);
498 if (sum < -len) sum= -len;
499 else if (sum >= len) sum= len-1;
505 * Decode the dc value.
506 * @param n block index (0-3 are luma, 4-5 are chroma)
507 * @param dir_ptr the prediction direction will be stored here
508 * @return the quantized dc
510 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
515 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
517 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
518 if (code < 0 || code > 9 /* && s->nbit<9 */){
519 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
527 level= 2*get_bits1(&s->gb)-1;
529 if(get_bits1(&s->gb))
530 level = get_bits(&s->gb, code-1) + (1<<(code-1));
532 level = -get_bits(&s->gb, code-1) - (1<<(code-1));
535 level = get_xbits(&s->gb, code);
539 if(get_bits1(&s->gb)==0){ /* marker */
540 if(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT)){
541 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
548 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
552 * Decode first partition.
553 * @return number of MBs decoded or <0 if an error occurred
555 static int mpeg4_decode_partition_a(MpegEncContext *s){
557 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
559 /* decode first partition */
561 s->first_slice_line=1;
562 for(; s->mb_y<s->mb_height; s->mb_y++){
563 ff_init_block_index(s);
564 for(; s->mb_x<s->mb_width; s->mb_x++){
565 const int xy= s->mb_x + s->mb_y*s->mb_stride;
570 ff_update_block_index(s);
571 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
572 s->first_slice_line=0;
574 if(s->pict_type==AV_PICTURE_TYPE_I){
578 if(show_bits_long(&s->gb, 19)==DC_MARKER){
582 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
584 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
589 s->cbp_table[xy]= cbpc & 3;
590 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
594 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
596 s->current_picture.qscale_table[xy]= s->qscale;
598 s->mbintra_table[xy]= 1;
601 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
603 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
607 if(dc_pred_dir) dir|=1;
609 s->pred_dir_table[xy]= dir;
610 }else{ /* P/S_TYPE */
611 int mx, my, pred_x, pred_y, bits;
612 int16_t * const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
613 const int stride= s->b8_stride*2;
616 bits= show_bits(&s->gb, 17);
617 if(bits==MOTION_MARKER){
623 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
624 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
628 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
631 mot_val[0 ]= mot_val[2 ]=
632 mot_val[0+stride]= mot_val[2+stride]= mx;
633 mot_val[1 ]= mot_val[3 ]=
634 mot_val[1+stride]= mot_val[3+stride]= my;
636 if(s->mbintra_table[xy])
637 ff_clean_intra_table_entries(s);
641 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
643 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
649 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
651 s->mb_intra = ((cbpc & 4) != 0);
654 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
655 s->mbintra_table[xy]= 1;
656 mot_val[0 ]= mot_val[2 ]=
657 mot_val[0+stride]= mot_val[2+stride]= 0;
658 mot_val[1 ]= mot_val[3 ]=
659 mot_val[1+stride]= mot_val[3+stride]= 0;
661 if(s->mbintra_table[xy])
662 ff_clean_intra_table_entries(s);
664 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
665 s->mcsel= get_bits1(&s->gb);
668 if ((cbpc & 16) == 0) {
669 /* 16x16 motion prediction */
671 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
673 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
677 my = ff_h263_decode_motion(s, pred_y, s->f_code);
680 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
684 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
687 mot_val[0 ]= mot_val[2 ] =
688 mot_val[0+stride]= mot_val[2+stride]= mx;
689 mot_val[1 ]= mot_val[3 ]=
690 mot_val[1+stride]= mot_val[3+stride]= my;
693 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
695 int16_t *mot_val= ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
696 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
700 my = ff_h263_decode_motion(s, pred_y, s->f_code);
717 * decode second partition.
718 * @return <0 if an error occurred
720 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
722 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
724 s->mb_x= s->resync_mb_x;
725 s->first_slice_line=1;
726 for(s->mb_y= s->resync_mb_y; mb_num < mb_count; s->mb_y++){
727 ff_init_block_index(s);
728 for(; mb_num < mb_count && s->mb_x<s->mb_width; s->mb_x++){
729 const int xy= s->mb_x + s->mb_y*s->mb_stride;
732 ff_update_block_index(s);
733 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
734 s->first_slice_line=0;
736 if(s->pict_type==AV_PICTURE_TYPE_I){
737 int ac_pred= get_bits1(&s->gb);
738 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
740 av_log(s->avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
744 s->cbp_table[xy]|= cbpy<<2;
745 s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
746 }else{ /* P || S_TYPE */
747 if (IS_INTRA(s->current_picture.mb_type[xy])) {
749 int ac_pred = get_bits1(&s->gb);
750 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
753 av_log(s->avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
757 if(s->cbp_table[xy] & 8) {
758 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
760 s->current_picture.qscale_table[xy] = s->qscale;
764 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
766 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
770 if(dc_pred_dir) dir|=1;
772 s->cbp_table[xy]&= 3; //remove dquant
773 s->cbp_table[xy]|= cbpy<<2;
774 s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
775 s->pred_dir_table[xy]= dir;
776 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
777 s->current_picture.qscale_table[xy] = s->qscale;
780 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
783 av_log(s->avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
787 if(s->cbp_table[xy] & 8) {
788 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
790 s->current_picture.qscale_table[xy] = s->qscale;
792 s->cbp_table[xy]&= 3; //remove dquant
793 s->cbp_table[xy]|= (cbpy^0xf)<<2;
797 if(mb_num >= mb_count) return 0;
804 * Decode the first and second partition.
805 * @return <0 if error (and sets error type in the error_status_table)
807 int ff_mpeg4_decode_partitions(MpegEncContext *s)
810 const int part_a_error= s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_ERROR|ER_MV_ERROR) : ER_MV_ERROR;
811 const int part_a_end = s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_END |ER_MV_END) : ER_MV_END;
813 mb_num= mpeg4_decode_partition_a(s);
815 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
819 if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){
820 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
821 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
825 s->mb_num_left= mb_num;
827 if(s->pict_type==AV_PICTURE_TYPE_I){
828 while(show_bits(&s->gb, 9) == 1)
829 skip_bits(&s->gb, 9);
830 if(get_bits_long(&s->gb, 19)!=DC_MARKER){
831 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y);
835 while(show_bits(&s->gb, 10) == 1)
836 skip_bits(&s->gb, 10);
837 if(get_bits(&s->gb, 17)!=MOTION_MARKER){
838 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y);
842 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end);
844 if( mpeg4_decode_partition_b(s, mb_num) < 0){
845 if(s->pict_type==AV_PICTURE_TYPE_P)
846 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_DC_ERROR);
849 if(s->pict_type==AV_PICTURE_TYPE_P)
850 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_DC_END);
858 * @return <0 if an error occurred
860 static inline int mpeg4_decode_block(MpegEncContext * s, int16_t * block,
861 int n, int coded, int intra, int rvlc)
863 int level, i, last, run;
864 int av_uninit(dc_pred_dir);
866 RL_VLC_ELEM * rl_vlc;
867 const uint8_t * scan_table;
870 //Note intra & rvlc should be optimized away if this is inlined
873 if(s->use_intra_dc_vlc){
875 if(s->partitioned_frame){
876 level = s->dc_val[0][ s->block_index[n] ];
877 if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
878 else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
879 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
881 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
889 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
895 rl = &ff_rvlc_rl_intra;
896 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
898 rl = &ff_mpeg4_rl_intra;
899 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
902 if (dc_pred_dir == 0)
903 scan_table = s->intra_v_scantable.permutated; /* left */
905 scan_table = s->intra_h_scantable.permutated; /* top */
907 scan_table = s->intra_scantable.permutated;
914 s->block_last_index[n] = i;
917 if(rvlc) rl = &ff_rvlc_rl_inter;
918 else rl = &ff_h263_rl_inter;
920 scan_table = s->intra_scantable.permutated;
926 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
928 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
931 qmul = s->qscale << 1;
932 qadd = (s->qscale - 1) | 1;
934 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
936 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
941 OPEN_READER(re, &s->gb);
943 UPDATE_CACHE(re, &s->gb);
944 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
948 if(SHOW_UBITS(re, &s->gb, 1)==0){
949 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
951 }; SKIP_CACHE(re, &s->gb, 1);
953 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
954 run= SHOW_UBITS(re, &s->gb, 6);
955 SKIP_COUNTER(re, &s->gb, 1+1+6);
956 UPDATE_CACHE(re, &s->gb);
958 if(SHOW_UBITS(re, &s->gb, 1)==0){
959 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
961 }; SKIP_CACHE(re, &s->gb, 1);
963 level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
965 if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
966 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
968 }; SKIP_CACHE(re, &s->gb, 5);
970 level= level * qmul + qadd;
971 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
972 SKIP_COUNTER(re, &s->gb, 1+11+5+1);
978 cache= GET_CACHE(re, &s->gb);
983 if (cache&0x80000000) {
984 if (cache&0x40000000) {
986 SKIP_CACHE(re, &s->gb, 2);
987 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
988 run= SHOW_UBITS(re, &s->gb, 6);
989 SKIP_COUNTER(re, &s->gb, 2+1+6);
990 UPDATE_CACHE(re, &s->gb);
993 level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
995 if(SHOW_UBITS(re, &s->gb, 1)==0){
996 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
998 }; SKIP_CACHE(re, &s->gb, 1);
1000 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
1002 if(SHOW_UBITS(re, &s->gb, 1)==0){
1003 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
1007 SKIP_COUNTER(re, &s->gb, 1+12+1);
1011 if(s->error_recognition >= FF_ER_COMPLIANT){
1012 const int abs_level= FFABS(level);
1013 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1014 const int run1= run - rl->max_run[last][abs_level] - 1;
1015 if(abs_level <= rl->max_level[last][run]){
1016 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1019 if(s->error_recognition > FF_ER_COMPLIANT){
1020 if(abs_level <= rl->max_level[last][run]*2){
1021 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1024 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
1025 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1032 if (level>0) level= level * qmul + qadd;
1033 else level= level * qmul - qadd;
1035 if((unsigned)(level + 2048) > 4095){
1036 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)){
1037 if(level > 2560 || level<-2560){
1038 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
1042 level= level<0 ? -2048 : 2047;
1049 SKIP_BITS(re, &s->gb, 2);
1050 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1051 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
1052 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1053 LAST_SKIP_BITS(re, &s->gb, 1);
1057 SKIP_BITS(re, &s->gb, 1);
1058 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1060 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1061 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1062 LAST_SKIP_BITS(re, &s->gb, 1);
1067 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1068 LAST_SKIP_BITS(re, &s->gb, 1);
1073 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1077 block[scan_table[i]] = level;
1081 block[scan_table[i]] = level;
1083 CLOSE_READER(re, &s->gb);
1087 if(!s->use_intra_dc_vlc){
1088 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1090 i -= i>>31; //if(i == -1) i=0;
1093 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1095 i = 63; /* XXX: not optimal */
1098 s->block_last_index[n] = i;
1103 * decode partition C of one MB.
1104 * @return <0 if an error occurred
1106 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1109 const int xy= s->mb_x + s->mb_y*s->mb_stride;
1111 mb_type = s->current_picture.mb_type[xy];
1112 cbp = s->cbp_table[xy];
1114 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
1116 if (s->current_picture.qscale_table[xy] != s->qscale) {
1117 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1120 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
1123 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1124 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1126 s->mb_intra = IS_INTRA(mb_type);
1128 if (IS_SKIP(mb_type)) {
1131 s->block_last_index[i] = -1;
1132 s->mv_dir = MV_DIR_FORWARD;
1133 s->mv_type = MV_TYPE_16X16;
1134 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
1141 }else if(s->mb_intra){
1142 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1143 }else if(!s->mb_intra){
1144 // s->mcsel= 0; //FIXME do we need to init that
1146 s->mv_dir = MV_DIR_FORWARD;
1147 if (IS_8X8(mb_type)) {
1148 s->mv_type = MV_TYPE_8X8;
1150 s->mv_type = MV_TYPE_16X16;
1153 } else { /* I-Frame */
1155 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1158 if (!IS_SKIP(mb_type)) {
1160 s->dsp.clear_blocks(s->block[0]);
1161 /* decode each block */
1162 for (i = 0; i < 6; i++) {
1163 if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
1164 av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
1171 /* per-MB end of slice check */
1173 if(--s->mb_num_left <= 0){
1174 if(mpeg4_is_resync(s))
1179 if(mpeg4_is_resync(s)){
1180 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1181 if(s->cbp_table[xy+delta])
1188 static int mpeg4_decode_mb(MpegEncContext *s,
1189 int16_t block[6][64])
1191 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1193 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
1194 const int xy= s->mb_x + s->mb_y * s->mb_stride;
1196 av_assert2(s->h263_pred);
1198 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
1200 if (get_bits1(&s->gb)) {
1204 s->block_last_index[i] = -1;
1205 s->mv_dir = MV_DIR_FORWARD;
1206 s->mv_type = MV_TYPE_16X16;
1207 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
1208 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
1210 s->mv[0][0][0]= get_amv(s, 0);
1211 s->mv[0][0][1]= get_amv(s, 1);
1215 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
1223 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1225 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1230 s->dsp.clear_blocks(s->block[0]);
1232 s->mb_intra = ((cbpc & 4) != 0);
1233 if (s->mb_intra) goto intra;
1235 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
1236 s->mcsel= get_bits1(&s->gb);
1238 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1240 cbp = (cbpc & 3) | (cbpy << 2);
1242 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1244 if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
1245 s->interlaced_dct= get_bits1(&s->gb);
1247 s->mv_dir = MV_DIR_FORWARD;
1248 if ((cbpc & 16) == 0) {
1250 s->current_picture.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
1251 /* 16x16 global motion prediction */
1252 s->mv_type = MV_TYPE_16X16;
1255 s->mv[0][0][0] = mx;
1256 s->mv[0][0][1] = my;
1257 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
1258 s->current_picture.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
1259 /* 16x8 field motion prediction */
1260 s->mv_type= MV_TYPE_FIELD;
1262 s->field_select[0][0]= get_bits1(&s->gb);
1263 s->field_select[0][1]= get_bits1(&s->gb);
1265 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1268 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1272 my = ff_h263_decode_motion(s, pred_y/2, s->f_code);
1276 s->mv[0][i][0] = mx;
1277 s->mv[0][i][1] = my;
1280 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1281 /* 16x16 motion prediction */
1282 s->mv_type = MV_TYPE_16X16;
1283 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1284 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1289 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1293 s->mv[0][0][0] = mx;
1294 s->mv[0][0][1] = my;
1297 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1298 s->mv_type = MV_TYPE_8X8;
1300 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1301 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1305 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1308 s->mv[0][i][0] = mx;
1309 s->mv[0][i][1] = my;
1314 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
1315 int modb1; // first bit of modb
1316 int modb2; // second bit of modb
1319 s->mb_intra = 0; //B-frames never contain intra blocks
1320 s->mcsel=0; // ... true gmc blocks
1324 s->last_mv[i][0][0]=
1325 s->last_mv[i][0][1]=
1326 s->last_mv[i][1][0]=
1327 s->last_mv[i][1][1]= 0;
1330 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1333 /* if we skipped it in the future P Frame than skip it now too */
1334 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1339 s->block_last_index[i] = -1;
1341 s->mv_dir = MV_DIR_FORWARD;
1342 s->mv_type = MV_TYPE_16X16;
1347 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
1351 modb1= get_bits1(&s->gb);
1353 mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded
1356 modb2= get_bits1(&s->gb);
1357 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1359 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1362 mb_type= mb_type_b_map[ mb_type ];
1365 s->dsp.clear_blocks(s->block[0]);
1366 cbp= get_bits(&s->gb, 6);
1369 if ((!IS_DIRECT(mb_type)) && cbp) {
1370 if(get_bits1(&s->gb)){
1371 ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
1375 if(!s->progressive_sequence){
1377 s->interlaced_dct= get_bits1(&s->gb);
1379 if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){
1380 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1381 mb_type &= ~MB_TYPE_16x16;
1383 if(USES_LIST(mb_type, 0)){
1384 s->field_select[0][0]= get_bits1(&s->gb);
1385 s->field_select[0][1]= get_bits1(&s->gb);
1387 if(USES_LIST(mb_type, 1)){
1388 s->field_select[1][0]= get_bits1(&s->gb);
1389 s->field_select[1][1]= get_bits1(&s->gb);
1395 if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){
1396 s->mv_type= MV_TYPE_16X16;
1398 if(USES_LIST(mb_type, 0)){
1399 s->mv_dir = MV_DIR_FORWARD;
1401 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1402 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1403 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1404 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1407 if(USES_LIST(mb_type, 1)){
1408 s->mv_dir |= MV_DIR_BACKWARD;
1410 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1411 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1412 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1413 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1415 }else if(!IS_DIRECT(mb_type)){
1416 s->mv_type= MV_TYPE_FIELD;
1418 if(USES_LIST(mb_type, 0)){
1419 s->mv_dir = MV_DIR_FORWARD;
1422 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code);
1423 my = ff_h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code);
1424 s->last_mv[0][i][0]= s->mv[0][i][0] = mx;
1425 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2;
1429 if(USES_LIST(mb_type, 1)){
1430 s->mv_dir |= MV_DIR_BACKWARD;
1433 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code);
1434 my = ff_h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code);
1435 s->last_mv[1][i][0]= s->mv[1][i][0] = mx;
1436 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2;
1442 if(IS_DIRECT(mb_type)){
1443 if(IS_SKIP(mb_type))
1446 mx = ff_h263_decode_motion(s, 0, 1);
1447 my = ff_h263_decode_motion(s, 0, 1);
1450 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1451 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1453 s->current_picture.mb_type[xy] = mb_type;
1454 } else { /* I-Frame */
1456 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1458 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1466 s->ac_pred = get_bits1(&s->gb);
1468 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1470 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1472 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1474 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1477 cbp = (cbpc & 3) | (cbpy << 2);
1479 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
1482 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1485 if(!s->progressive_sequence)
1486 s->interlaced_dct= get_bits1(&s->gb);
1488 s->dsp.clear_blocks(s->block[0]);
1489 /* decode each block */
1490 for (i = 0; i < 6; i++) {
1491 if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
1498 /* decode each block */
1499 for (i = 0; i < 6; i++) {
1500 if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
1506 /* per-MB end of slice check */
1507 if(s->codec_id==AV_CODEC_ID_MPEG4){
1508 int next= mpeg4_is_resync(s);
1510 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1512 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1515 if(s->pict_type==AV_PICTURE_TYPE_B){
1516 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1517 ff_thread_await_progress(&s->next_picture_ptr->tf,
1518 (s->mb_x + delta >= s->mb_width) ? FFMIN(s->mb_y+1, s->mb_height-1) : s->mb_y, 0);
1519 if (s->next_picture.mbskip_table[xy + delta])
1531 static int mpeg4_decode_gop_header(MpegEncContext * s, GetBitContext *gb){
1532 int hours, minutes, seconds;
1534 if(!show_bits(gb, 23)){
1535 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1539 hours= get_bits(gb, 5);
1540 minutes= get_bits(gb, 6);
1542 seconds= get_bits(gb, 6);
1544 s->time_base= seconds + 60*(minutes + 60*hours);
1552 static int mpeg4_decode_profile_level(MpegEncContext * s, GetBitContext *gb){
1554 s->avctx->profile = get_bits(gb, 4);
1555 s->avctx->level = get_bits(gb, 4);
1557 // for Simple profile, level 0
1558 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1559 s->avctx->level = 0;
1565 static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){
1566 int width, height, vo_ver_id;
1569 skip_bits(gb, 1); /* random access */
1570 s->vo_type= get_bits(gb, 8);
1571 if (get_bits1(gb) != 0) { /* is_ol_id */
1572 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1573 skip_bits(gb, 3); /* vo_priority */
1577 s->aspect_ratio_info= get_bits(gb, 4);
1578 if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){
1579 s->avctx->sample_aspect_ratio.num= get_bits(gb, 8); // par_width
1580 s->avctx->sample_aspect_ratio.den= get_bits(gb, 8); // par_height
1582 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1585 if ((s->vol_control_parameters=get_bits1(gb))) { /* vol control parameter */
1586 int chroma_format= get_bits(gb, 2);
1587 if(chroma_format!=CHROMA_420){
1588 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1590 s->low_delay= get_bits1(gb);
1591 if(get_bits1(gb)){ /* vbv parameters */
1592 get_bits(gb, 15); /* first_half_bitrate */
1593 skip_bits1(gb); /* marker */
1594 get_bits(gb, 15); /* latter_half_bitrate */
1595 skip_bits1(gb); /* marker */
1596 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1597 skip_bits1(gb); /* marker */
1598 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1599 get_bits(gb, 11); /* first_half_vbv_occupancy */
1600 skip_bits1(gb); /* marker */
1601 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1602 skip_bits1(gb); /* marker */
1605 // set low delay flag only once the smartest? low delay detection won't be overridden
1606 if(s->picture_number==0)
1610 s->shape = get_bits(gb, 2); /* vol shape */
1611 if(s->shape != RECT_SHAPE) av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1612 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
1613 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1614 skip_bits(gb, 4); //video_object_layer_shape_extension
1617 check_marker(gb, "before time_increment_resolution");
1619 s->avctx->time_base.den = get_bits(gb, 16);
1620 if(!s->avctx->time_base.den){
1621 av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
1622 s->avctx->time_base.num = 0;
1626 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1627 if (s->time_increment_bits < 1)
1628 s->time_increment_bits = 1;
1630 check_marker(gb, "before fixed_vop_rate");
1632 if (get_bits1(gb) != 0) { /* fixed_vop_rate */
1633 s->avctx->time_base.num = get_bits(gb, s->time_increment_bits);
1635 s->avctx->time_base.num = 1;
1639 if (s->shape != BIN_ONLY_SHAPE) {
1640 if (s->shape == RECT_SHAPE) {
1641 check_marker(gb, "before width");
1642 width = get_bits(gb, 13);
1643 check_marker(gb, "before height");
1644 height = get_bits(gb, 13);
1645 check_marker(gb, "after height");
1646 if(width && height && !(s->width && s->codec_tag == AV_RL32("MP4S"))){ /* they should be non zero but who knows ... */
1647 if (s->width && s->height &&
1648 (s->width != width || s->height != height))
1649 s->context_reinit = 1;
1655 s->progressive_sequence=
1656 s->progressive_frame= get_bits1(gb)^1;
1657 s->interlaced_dct=0;
1658 if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1659 av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
1660 if (vo_ver_id == 1) {
1661 s->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1663 s->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1665 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1666 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
1667 if(s->vol_sprite_usage==STATIC_SPRITE){
1668 s->sprite_width = get_bits(gb, 13);
1669 skip_bits1(gb); /* marker */
1670 s->sprite_height= get_bits(gb, 13);
1671 skip_bits1(gb); /* marker */
1672 s->sprite_left = get_bits(gb, 13);
1673 skip_bits1(gb); /* marker */
1674 s->sprite_top = get_bits(gb, 13);
1675 skip_bits1(gb); /* marker */
1677 s->num_sprite_warping_points= get_bits(gb, 6);
1678 if(s->num_sprite_warping_points > 3){
1679 av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points);
1680 s->num_sprite_warping_points= 0;
1683 s->sprite_warping_accuracy = get_bits(gb, 2);
1684 s->sprite_brightness_change= get_bits1(gb);
1685 if(s->vol_sprite_usage==STATIC_SPRITE)
1686 s->low_latency_sprite= get_bits1(gb);
1688 // FIXME sadct disable bit if verid!=1 && shape not rect
1690 if (get_bits1(gb) == 1) { /* not_8_bit */
1691 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1692 if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n"); /* bits_per_pixel */
1693 if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precision %d\n", s->quant_precision);
1694 if(s->quant_precision<3 || s->quant_precision>9) {
1695 s->quant_precision = 5;
1698 s->quant_precision = 5;
1701 // FIXME a bunch of grayscale shape things
1703 if((s->mpeg_quant=get_bits1(gb))){ /* vol_quant_type */
1706 /* load default matrixes */
1707 for(i=0; i<64; i++){
1708 int j= s->dsp.idct_permutation[i];
1709 v= ff_mpeg4_default_intra_matrix[i];
1710 s->intra_matrix[j]= v;
1711 s->chroma_intra_matrix[j]= v;
1713 v= ff_mpeg4_default_non_intra_matrix[i];
1714 s->inter_matrix[j]= v;
1715 s->chroma_inter_matrix[j]= v;
1718 /* load custom intra matrix */
1721 for(i=0; i<64; i++){
1727 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1728 s->intra_matrix[j]= v;
1729 s->chroma_intra_matrix[j]= v;
1732 /* replicate last value */
1734 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1735 s->intra_matrix[j]= last;
1736 s->chroma_intra_matrix[j]= last;
1740 /* load custom non intra matrix */
1743 for(i=0; i<64; i++){
1749 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1750 s->inter_matrix[j]= v;
1751 s->chroma_inter_matrix[j]= v;
1754 /* replicate last value */
1756 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1757 s->inter_matrix[j]= last;
1758 s->chroma_inter_matrix[j]= last;
1762 // FIXME a bunch of grayscale shape things
1766 s->quarter_sample= get_bits1(gb);
1767 else s->quarter_sample=0;
1770 int pos= get_bits_count(gb);
1771 int estimation_method= get_bits(gb, 2);
1772 if(estimation_method<2){
1774 s->cplx_estimation_trash_i += 8*get_bits1(gb); //opaque
1775 s->cplx_estimation_trash_i += 8*get_bits1(gb); //transparent
1776 s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_cae
1777 s->cplx_estimation_trash_i += 8*get_bits1(gb); //inter_cae
1778 s->cplx_estimation_trash_i += 8*get_bits1(gb); //no_update
1779 s->cplx_estimation_trash_i += 8*get_bits1(gb); //upampling
1782 s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_blocks
1783 s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter_blocks
1784 s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter4v_blocks
1785 s->cplx_estimation_trash_i += 8*get_bits1(gb); //not coded blocks
1787 if(!check_marker(gb, "in complexity estimation part 1")){
1788 skip_bits_long(gb, pos - get_bits_count(gb));
1792 s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_coeffs
1793 s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_lines
1794 s->cplx_estimation_trash_i += 8*get_bits1(gb); //vlc_syms
1795 s->cplx_estimation_trash_i += 4*get_bits1(gb); //vlc_bits
1798 s->cplx_estimation_trash_p += 8*get_bits1(gb); //apm
1799 s->cplx_estimation_trash_p += 8*get_bits1(gb); //npm
1800 s->cplx_estimation_trash_b += 8*get_bits1(gb); //interpolate_mc_q
1801 s->cplx_estimation_trash_p += 8*get_bits1(gb); //forwback_mc_q
1802 s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel2
1803 s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel4
1805 if(!check_marker(gb, "in complexity estimation part 2")){
1806 skip_bits_long(gb, pos - get_bits_count(gb));
1809 if(estimation_method==1){
1810 s->cplx_estimation_trash_i += 8*get_bits1(gb); //sadct
1811 s->cplx_estimation_trash_p += 8*get_bits1(gb); //qpel
1814 av_log(s->avctx, AV_LOG_ERROR, "Invalid Complexity estimation method %d\n", estimation_method);
1817 s->cplx_estimation_trash_i=
1818 s->cplx_estimation_trash_p=
1819 s->cplx_estimation_trash_b= 0;
1822 s->resync_marker= !get_bits1(gb); /* resync_marker_disabled */
1824 s->data_partitioning= get_bits1(gb);
1825 if(s->data_partitioning){
1826 s->rvlc= get_bits1(gb);
1829 if(vo_ver_id != 1) {
1830 s->new_pred= get_bits1(gb);
1832 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1833 skip_bits(gb, 2); /* requested upstream message type */
1834 skip_bits1(gb); /* newpred segment type */
1836 s->reduced_res_vop= get_bits1(gb);
1837 if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
1841 s->reduced_res_vop= 0;
1844 s->scalability= get_bits1(gb);
1846 if (s->scalability) {
1847 GetBitContext bak= *gb;
1848 int h_sampling_factor_n;
1849 int h_sampling_factor_m;
1850 int v_sampling_factor_n;
1851 int v_sampling_factor_m;
1853 s->hierachy_type= get_bits1(gb);
1854 skip_bits(gb, 4); /* ref_layer_id */
1855 skip_bits1(gb); /* ref_layer_sampling_dir */
1856 h_sampling_factor_n= get_bits(gb, 5);
1857 h_sampling_factor_m= get_bits(gb, 5);
1858 v_sampling_factor_n= get_bits(gb, 5);
1859 v_sampling_factor_m= get_bits(gb, 5);
1860 s->enhancement_type= get_bits1(gb);
1862 if( h_sampling_factor_n==0 || h_sampling_factor_m==0
1863 || v_sampling_factor_n==0 || v_sampling_factor_m==0){
1864 /* illegal scalability header (VERY broken encoder),
1865 * trying to workaround */
1869 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
1871 // bin shape stuff FIXME
1875 if(s->avctx->debug&FF_DEBUG_PICT_INFO) {
1876 av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
1877 s->avctx->time_base.num, s->avctx->time_base.den,
1878 s->time_increment_bits,
1880 s->progressive_sequence,
1881 s->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
1882 s->data_partitioning ? "partition " : "", s->rvlc ? "rvlc " : ""
1890 * Decode the user data stuff in the header.
1891 * Also initializes divx/xvid/lavc_version/build.
1893 static int decode_user_data(MpegEncContext *s, GetBitContext *gb){
1897 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
1900 for(i=0; i<255 && get_bits_count(gb) < gb->size_in_bits; i++){
1901 if(show_bits(gb, 23) == 0) break;
1902 buf[i]= get_bits(gb, 8);
1906 /* divx detection */
1907 e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
1909 e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
1911 s->divx_version= ver;
1912 s->divx_build= build;
1913 s->divx_packed= e==3 && last=='p';
1914 if(s->divx_packed && !s->showed_packed_warning) {
1915 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
1916 "wasteful way to store B-frames ('packed B-frames'). "
1917 "Consider using a tool like VirtualDub or avidemux to fix it.\n");
1918 s->showed_packed_warning=1;
1922 /* libavcodec detection */
1923 e=sscanf(buf, "FFmpe%*[^b]b%d", &build)+3;
1925 e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
1927 e=sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3)+1;
1929 build= (ver<<16) + (ver2<<8) + ver3;
1932 if(strcmp(buf, "ffmpeg")==0){
1933 s->lavc_build= 4600;
1937 s->lavc_build= build;
1940 /* Xvid detection */
1941 e=sscanf(buf, "XviD%d", &build);
1943 s->xvid_build= build;
1949 static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
1950 int time_incr, time_increment;
1952 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
1953 if(s->pict_type==AV_PICTURE_TYPE_B && s->low_delay && s->vol_control_parameters==0 && !(s->flags & CODEC_FLAG_LOW_DELAY)){
1954 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
1958 s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
1959 if(s->partitioned_frame)
1960 s->decode_mb= mpeg4_decode_partitioned_mb;
1962 s->decode_mb= mpeg4_decode_mb;
1965 while (get_bits1(gb) != 0)
1968 check_marker(gb, "before time_increment");
1970 if(s->time_increment_bits==0 || !(show_bits(gb, s->time_increment_bits+1)&1)){
1971 av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
1973 for(s->time_increment_bits=1 ;s->time_increment_bits<16; s->time_increment_bits++){
1974 if ( s->pict_type == AV_PICTURE_TYPE_P
1975 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
1976 if((show_bits(gb, s->time_increment_bits+6)&0x37) == 0x30) break;
1978 if((show_bits(gb, s->time_increment_bits+5)&0x1F) == 0x18) break;
1981 av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
1982 if (s->avctx->time_base.den && 4*s->avctx->time_base.den < 1<<s->time_increment_bits) {
1983 s->avctx->time_base.den = 1<<s->time_increment_bits;
1987 if(IS_3IV1) time_increment= get_bits1(gb); //FIXME investigate further
1988 else time_increment= get_bits(gb, s->time_increment_bits);
1990 if(s->pict_type!=AV_PICTURE_TYPE_B){
1991 s->last_time_base= s->time_base;
1992 s->time_base+= time_incr;
1993 s->time= s->time_base*s->avctx->time_base.den + time_increment;
1994 if(s->workaround_bugs&FF_BUG_UMP4){
1995 if(s->time < s->last_non_b_time){
1996 /* header is not mpeg-4-compatible, broken encoder,
1997 * trying to workaround */
1999 s->time+= s->avctx->time_base.den;
2002 s->pp_time= s->time - s->last_non_b_time;
2003 s->last_non_b_time= s->time;
2005 s->time= (s->last_time_base + time_incr)*s->avctx->time_base.den + time_increment;
2006 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2007 if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
2008 /* messed up order, maybe after seeking? skipping current b-frame */
2009 return FRAME_SKIPPED;
2011 ff_mpeg4_init_direct_mv(s);
2013 if(s->t_frame==0) s->t_frame= s->pb_time;
2014 if(s->t_frame==0) s->t_frame=1; // 1/0 protection
2015 s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame)
2016 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
2017 s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
2018 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
2019 if(!s->progressive_sequence){
2020 if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
2021 return FRAME_SKIPPED;
2025 if(s->avctx->time_base.num)
2026 s->current_picture_ptr->f.pts = ROUNDED_DIV(s->time, s->avctx->time_base.num);
2028 s->current_picture_ptr->f.pts = AV_NOPTS_VALUE;
2029 if(s->avctx->debug&FF_DEBUG_PTS)
2030 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
2031 s->current_picture_ptr->f.pts);
2033 check_marker(gb, "before vop_coded");
2036 if (get_bits1(gb) != 1){
2037 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
2038 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2039 return FRAME_SKIPPED;
2042 decode_new_pred(s, gb);
2044 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == AV_PICTURE_TYPE_P
2045 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE))) {
2046 /* rounding type for motion estimation */
2047 s->no_rounding = get_bits1(gb);
2051 //FIXME reduced res stuff
2053 if (s->shape != RECT_SHAPE) {
2054 if (s->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2055 skip_bits(gb, 13); /* width */
2056 skip_bits1(gb); /* marker */
2057 skip_bits(gb, 13); /* height */
2058 skip_bits1(gb); /* marker */
2059 skip_bits(gb, 13); /* hor_spat_ref */
2060 skip_bits1(gb); /* marker */
2061 skip_bits(gb, 13); /* ver_spat_ref */
2063 skip_bits1(gb); /* change_CR_disable */
2065 if (get_bits1(gb) != 0) {
2066 skip_bits(gb, 8); /* constant_alpha_value */
2069 //FIXME complexity estimation stuff
2071 if (s->shape != BIN_ONLY_SHAPE) {
2072 skip_bits_long(gb, s->cplx_estimation_trash_i);
2073 if(s->pict_type != AV_PICTURE_TYPE_I)
2074 skip_bits_long(gb, s->cplx_estimation_trash_p);
2075 if(s->pict_type == AV_PICTURE_TYPE_B)
2076 skip_bits_long(gb, s->cplx_estimation_trash_b);
2078 if(get_bits_left(gb) < 3) {
2079 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2082 s->intra_dc_threshold= ff_mpeg4_dc_threshold[ get_bits(gb, 3) ];
2083 if(!s->progressive_sequence){
2084 s->top_field_first= get_bits1(gb);
2085 s->alternate_scan= get_bits1(gb);
2087 s->alternate_scan= 0;
2090 if(s->alternate_scan){
2091 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
2092 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
2093 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2094 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2096 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
2097 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
2098 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2099 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2102 if(s->pict_type == AV_PICTURE_TYPE_S && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
2103 if (mpeg4_decode_sprite_trajectory(s, gb) < 0)
2104 return AVERROR_INVALIDDATA;
2105 if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
2106 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2109 if (s->shape != BIN_ONLY_SHAPE) {
2110 s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
2112 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
2113 return -1; // makes no sense to continue, as there is nothing left from the image then
2116 if (s->pict_type != AV_PICTURE_TYPE_I) {
2117 s->f_code = get_bits(gb, 3); /* fcode_for */
2119 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
2121 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2126 if (s->pict_type == AV_PICTURE_TYPE_B) {
2127 s->b_code = get_bits(gb, 3);
2129 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (b_code=0)\n");
2131 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2136 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2137 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
2138 s->qscale, s->f_code, s->b_code,
2139 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
2140 gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first,
2141 s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
2142 s->sprite_warping_accuracy, 1-s->no_rounding, s->vo_type, s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold,
2143 s->cplx_estimation_trash_i, s->cplx_estimation_trash_p, s->cplx_estimation_trash_b,
2149 if(!s->scalability){
2150 if (s->shape!=RECT_SHAPE && s->pict_type!=AV_PICTURE_TYPE_I) {
2151 skip_bits1(gb); // vop shape coding type
2154 if(s->enhancement_type){
2155 int load_backward_shape= get_bits1(gb);
2156 if(load_backward_shape){
2157 av_log(s->avctx, AV_LOG_ERROR, "load backward shape isn't supported\n");
2160 skip_bits(gb, 2); //ref_select_code
2163 /* detect buggy encoders which don't set the low_delay flag (divx4/xvid/opendivx)*/
2164 // note we cannot detect divx5 without b-frames easily (although it's buggy too)
2165 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==-1 && s->picture_number==0){
2166 av_log(s->avctx, AV_LOG_WARNING, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2170 s->picture_number++; // better than pic number==0 always ;)
2172 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
2173 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
2175 if(s->workaround_bugs&FF_BUG_EDGE){
2176 s->h_edge_pos= s->width;
2177 s->v_edge_pos= s->height;
2183 * Decode mpeg4 headers.
2184 * @return <0 if no VOP found (or a damaged one)
2185 * FRAME_SKIPPED if a not coded VOP is found
2186 * 0 if a VOP is found
2188 int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb)
2190 unsigned startcode, v;
2192 /* search next start code */
2195 if(s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630){
2197 if(get_bits(gb, 8) == 0xF0)
2203 if(get_bits_count(gb) >= gb->size_in_bits){
2204 if(gb->size_in_bits==8 && (s->divx_version>=0 || s->xvid_build>=0) || s->codec_tag == AV_RL32("QMP4")){
2205 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2206 return FRAME_SKIPPED; //divx bug
2208 return -1; //end of stream
2211 /* use the bits after the test */
2212 v = get_bits(gb, 8);
2213 startcode = ((startcode << 8) | v) & 0xffffffff;
2215 if((startcode&0xFFFFFF00) != 0x100)
2216 continue; //no startcode
2218 if(s->avctx->debug&FF_DEBUG_STARTCODE){
2219 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2220 if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2221 else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2222 else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2223 else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2224 else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2225 else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2226 else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2227 else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2228 else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2229 else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2230 else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2231 else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2232 else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2233 else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2234 else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2235 else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2236 else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2237 else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2238 else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2239 else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2240 else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2241 else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2242 else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2243 else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2244 else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2245 else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2246 else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
2247 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2250 if(startcode >= 0x120 && startcode <= 0x12F){
2251 if(decode_vol_header(s, gb) < 0)
2254 else if(startcode == USER_DATA_STARTCODE){
2255 decode_user_data(s, gb);
2257 else if(startcode == GOP_STARTCODE){
2258 mpeg4_decode_gop_header(s, gb);
2260 else if(startcode == VOS_STARTCODE){
2261 mpeg4_decode_profile_level(s, gb);
2263 else if(startcode == VOP_STARTCODE){
2271 if(s->flags& CODEC_FLAG_LOW_DELAY)
2273 s->avctx->has_b_frames= !s->low_delay;
2274 return decode_vop_header(s, gb);
2277 av_cold void ff_mpeg4videodec_static_init(void) {
2278 static int done = 0;
2281 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2282 ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2283 ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2284 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2285 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2286 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2287 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2288 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2289 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2290 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2291 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2292 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2293 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2294 &ff_sprite_trajectory_tab[0][1], 4, 2,
2295 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2296 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2297 &ff_mb_type_b_tab[0][1], 2, 1,
2298 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2303 static av_cold int decode_init(AVCodecContext *avctx)
2305 MpegEncContext *s = avctx->priv_data;
2313 if((ret=ff_h263_decode_init(avctx)) < 0)
2316 ff_mpeg4videodec_static_init();
2319 s->low_delay = 0; //default, might be overridden in the vol header during header parsing
2320 s->decode_mb= mpeg4_decode_mb;
2321 s->time_increment_bits = 4; /* default value for broken headers */
2322 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
2324 avctx->internal->allocate_progress = 1;
2329 static const AVProfile mpeg4_video_profiles[] = {
2330 { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
2331 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
2332 { FF_PROFILE_MPEG4_CORE, "Core Profile" },
2333 { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
2334 { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
2335 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
2336 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
2337 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
2338 { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
2339 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
2340 { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
2341 { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
2342 { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
2343 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
2344 { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
2345 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
2346 { FF_PROFILE_UNKNOWN },
2349 static const AVOption mpeg4_options[] = {
2350 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
2351 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
2355 static const AVClass mpeg4_class = {
2356 "MPEG4 Video Decoder",
2357 av_default_item_name,
2359 LIBAVUTIL_VERSION_INT,
2362 static const AVClass mpeg4_vdpau_class = {
2363 "MPEG4 Video VDPAU Decoder",
2364 av_default_item_name,
2366 LIBAVUTIL_VERSION_INT,
2369 AVCodec ff_mpeg4_decoder = {
2371 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2372 .type = AVMEDIA_TYPE_VIDEO,
2373 .id = AV_CODEC_ID_MPEG4,
2374 .priv_data_size = sizeof(MpegEncContext),
2375 .init = decode_init,
2376 .close = ff_h263_decode_end,
2377 .decode = ff_h263_decode_frame,
2378 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2379 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2380 CODEC_CAP_FRAME_THREADS,
2381 .flush = ff_mpeg_flush,
2383 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
2384 .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
2385 .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_mpeg_update_thread_context),
2386 .priv_class = &mpeg4_class,
2390 #if CONFIG_MPEG4_VDPAU_DECODER
2391 AVCodec ff_mpeg4_vdpau_decoder = {
2392 .name = "mpeg4_vdpau",
2393 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2394 .type = AVMEDIA_TYPE_VIDEO,
2395 .id = AV_CODEC_ID_MPEG4,
2396 .priv_data_size = sizeof(MpegEncContext),
2397 .init = decode_init,
2398 .close = ff_h263_decode_end,
2399 .decode = ff_h263_decode_frame,
2400 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2401 CODEC_CAP_HWACCEL_VDPAU,
2402 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_MPEG4,
2404 .priv_class = &mpeg4_vdpau_class,