3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
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.
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.
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
23 #include "mpegvideo.h"
24 #include "mpeg4video.h"
28 // The defines below define the number of bits that are read at once for
29 // reading vlc values. Changing these may improve speed and data cache needs
30 // be aware though that decreasing them may need the number of stages that is
31 // passed to get_vlc* to be increased.
32 #define SPRITE_TRAJ_VLC_BITS 6
34 #define MB_TYPE_B_VLC_BITS 4
37 static VLC dc_lum, dc_chrom;
38 static VLC sprite_trajectory;
39 static VLC mb_type_b_vlc;
41 static const int mb_type_b_map[4]= {
42 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
43 MB_TYPE_L0L1 | MB_TYPE_16x16,
44 MB_TYPE_L1 | MB_TYPE_16x16,
45 MB_TYPE_L0 | MB_TYPE_16x16,
50 * @param n block index (0-3 are luma, 4-5 are chroma)
51 * @param dir the ac prediction direction
53 void ff_mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
57 int16_t *ac_val, *ac_val1;
58 int8_t * const qscale_table = s->current_picture.f.qscale_table;
61 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
65 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
69 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
72 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
75 /* different qscale, we must rescale */
77 block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
81 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
83 ac_val -= 16 * s->block_wrap[n];
85 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
88 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
91 /* different qscale, we must rescale */
93 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
100 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
104 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
109 * check if the next stuff is a resync marker or the end.
112 static inline int mpeg4_is_resync(MpegEncContext *s){
113 int bits_count= get_bits_count(&s->gb);
114 int v= show_bits(&s->gb, 16);
116 if(s->workaround_bugs&FF_BUG_NO_PADDING){
121 if(s->pict_type==AV_PICTURE_TYPE_B || (v>>(8-s->pict_type)!=1) || s->partitioned_frame)
123 skip_bits(&s->gb, 8+s->pict_type);
124 bits_count+= 8+s->pict_type;
125 v= show_bits(&s->gb, 16);
128 if(bits_count + 8 >= s->gb.size_in_bits){
130 v|= 0x7F >> (7-(bits_count&7));
135 if(v == ff_mpeg4_resync_prefix[bits_count&7]){
137 GetBitContext gb= s->gb;
139 skip_bits(&s->gb, 1);
140 align_get_bits(&s->gb);
142 for(len=0; len<32; len++){
143 if(get_bits1(&s->gb)) break;
148 if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
155 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb)
158 int a= 2<<s->sprite_warping_accuracy;
159 int rho= 3-s->sprite_warping_accuracy;
161 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
162 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
163 int sprite_ref[4][2];
164 int virtual_ref[2][2];
171 for(i=0; i<s->num_sprite_warping_points; i++){
175 length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
177 x= get_xbits(gb, length);
179 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(gb); /* marker bit */
181 length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
183 y=get_xbits(gb, length);
185 skip_bits1(gb); /* marker bit */
186 s->sprite_traj[i][0]= d[i][0]= x;
187 s->sprite_traj[i][1]= d[i][1]= y;
190 s->sprite_traj[i][0]= s->sprite_traj[i][1]= 0;
192 while((1<<alpha)<w) alpha++;
193 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
197 // Note, the 4th point isn't used for GMC
198 if(s->divx_version==500 && s->divx_build==413){
199 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
200 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
201 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
202 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
203 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
204 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
206 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
207 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
208 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
209 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
210 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
211 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
213 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
214 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
216 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
217 // perhaps it should be reordered to be more readable ...
218 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
219 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
220 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
221 + 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);
222 virtual_ref[0][1]= 16*vop_ref[0][1]
223 + 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);
224 virtual_ref[1][0]= 16*vop_ref[0][0]
225 + 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);
226 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
227 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
229 switch(s->num_sprite_warping_points)
232 s->sprite_offset[0][0]= 0;
233 s->sprite_offset[0][1]= 0;
234 s->sprite_offset[1][0]= 0;
235 s->sprite_offset[1][1]= 0;
236 s->sprite_delta[0][0]= a;
237 s->sprite_delta[0][1]= 0;
238 s->sprite_delta[1][0]= 0;
239 s->sprite_delta[1][1]= a;
240 s->sprite_shift[0]= 0;
241 s->sprite_shift[1]= 0;
244 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
245 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
246 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
247 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
248 s->sprite_delta[0][0]= a;
249 s->sprite_delta[0][1]= 0;
250 s->sprite_delta[1][0]= 0;
251 s->sprite_delta[1][1]= a;
252 s->sprite_shift[0]= 0;
253 s->sprite_shift[1]= 0;
256 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
257 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
258 + ( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])
259 + (1<<(alpha+rho-1));
260 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
261 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
262 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])
263 + (1<<(alpha+rho-1));
264 s->sprite_offset[1][0]= ( (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
265 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
266 +2*w2*r*sprite_ref[0][0]
268 + (1<<(alpha+rho+1)));
269 s->sprite_offset[1][1]= ( (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
270 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
271 +2*w2*r*sprite_ref[0][1]
273 + (1<<(alpha+rho+1)));
274 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
275 s->sprite_delta[0][1]= (+r*sprite_ref[0][1] - virtual_ref[0][1]);
276 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
277 s->sprite_delta[1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
279 s->sprite_shift[0]= alpha+rho;
280 s->sprite_shift[1]= alpha+rho+2;
283 min_ab= FFMIN(alpha, beta);
286 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+beta+rho-min_ab))
287 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-vop_ref[0][0])
288 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-vop_ref[0][1])
289 + (1<<(alpha+beta+rho-min_ab-1));
290 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+beta+rho-min_ab))
291 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-vop_ref[0][0])
292 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-vop_ref[0][1])
293 + (1<<(alpha+beta+rho-min_ab-1));
294 s->sprite_offset[1][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-2*vop_ref[0][0] + 1)
295 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-2*vop_ref[0][1] + 1)
296 + 2*w2*h3*r*sprite_ref[0][0]
298 + (1<<(alpha+beta+rho-min_ab+1));
299 s->sprite_offset[1][1]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-2*vop_ref[0][0] + 1)
300 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-2*vop_ref[0][1] + 1)
301 + 2*w2*h3*r*sprite_ref[0][1]
303 + (1<<(alpha+beta+rho-min_ab+1));
304 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3;
305 s->sprite_delta[0][1]= (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3;
306 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3;
307 s->sprite_delta[1][1]= (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3;
309 s->sprite_shift[0]= alpha + beta + rho - min_ab;
310 s->sprite_shift[1]= alpha + beta + rho - min_ab + 2;
313 /* try to simplify the situation */
314 if( s->sprite_delta[0][0] == a<<s->sprite_shift[0]
315 && s->sprite_delta[0][1] == 0
316 && s->sprite_delta[1][0] == 0
317 && s->sprite_delta[1][1] == a<<s->sprite_shift[0])
319 s->sprite_offset[0][0]>>=s->sprite_shift[0];
320 s->sprite_offset[0][1]>>=s->sprite_shift[0];
321 s->sprite_offset[1][0]>>=s->sprite_shift[1];
322 s->sprite_offset[1][1]>>=s->sprite_shift[1];
323 s->sprite_delta[0][0]= a;
324 s->sprite_delta[0][1]= 0;
325 s->sprite_delta[1][0]= 0;
326 s->sprite_delta[1][1]= a;
327 s->sprite_shift[0]= 0;
328 s->sprite_shift[1]= 0;
329 s->real_sprite_warping_points=1;
332 int shift_y= 16 - s->sprite_shift[0];
333 int shift_c= 16 - s->sprite_shift[1];
335 s->sprite_offset[0][i]<<= shift_y;
336 s->sprite_offset[1][i]<<= shift_c;
337 s->sprite_delta[0][i]<<= shift_y;
338 s->sprite_delta[1][i]<<= shift_y;
339 s->sprite_shift[i]= 16;
341 s->real_sprite_warping_points= s->num_sprite_warping_points;
346 * Decode the next video packet.
347 * @return <0 if something went wrong
349 int ff_mpeg4_decode_video_packet_header(MpegEncContext *s)
351 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
352 int header_extension=0, mb_num, len;
354 /* is there enough space left for a video packet + header */
355 if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
357 for(len=0; len<32; len++){
358 if(get_bits1(&s->gb)) break;
361 if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
362 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
366 if(s->shape != RECT_SHAPE){
367 header_extension= get_bits1(&s->gb);
368 //FIXME more stuff here
371 mb_num= get_bits(&s->gb, mb_num_bits);
372 if(mb_num>=s->mb_num){
373 av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
376 if(s->pict_type == AV_PICTURE_TYPE_B){
377 int mb_x = 0, mb_y = 0;
379 while (s->next_picture.f.mbskip_table[s->mb_index2xy[mb_num]]) {
381 ff_thread_await_progress(&s->next_picture_ptr->f, mb_y++, 0);
383 if (++mb_x == s->mb_width) mb_x = 0;
385 if(mb_num >= s->mb_num) return -1; // slice contains just skipped MBs which where already decoded
388 s->mb_x= mb_num % s->mb_width;
389 s->mb_y= mb_num / s->mb_width;
391 if(s->shape != BIN_ONLY_SHAPE){
392 int qscale= get_bits(&s->gb, s->quant_precision);
394 s->chroma_qscale=s->qscale= qscale;
397 if(s->shape == RECT_SHAPE){
398 header_extension= get_bits1(&s->gb);
400 if(header_extension){
403 while (get_bits1(&s->gb) != 0)
406 check_marker(&s->gb, "before time_increment in video packed header");
407 skip_bits(&s->gb, s->time_increment_bits); /* time_increment */
408 check_marker(&s->gb, "before vop_coding_type in video packed header");
410 skip_bits(&s->gb, 2); /* vop coding type */
411 //FIXME not rect stuff here
413 if(s->shape != BIN_ONLY_SHAPE){
414 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
415 //FIXME don't just ignore everything
416 if(s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
417 mpeg4_decode_sprite_trajectory(s, &s->gb);
418 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
421 //FIXME reduced res stuff here
423 if (s->pict_type != AV_PICTURE_TYPE_I) {
424 int f_code = get_bits(&s->gb, 3); /* fcode_for */
426 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
429 if (s->pict_type == AV_PICTURE_TYPE_B) {
430 int b_code = get_bits(&s->gb, 3);
432 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
437 //FIXME new-pred stuff
443 * Get the average motion vector for a GMC MB.
444 * @param n either 0 for the x component or 1 for y
445 * @return the average MV for a GMC MB
447 static inline int get_amv(MpegEncContext *s, int n){
448 int x, y, mb_v, sum, dx, dy, shift;
449 int len = 1 << (s->f_code + 4);
450 const int a= s->sprite_warping_accuracy;
452 if(s->workaround_bugs & FF_BUG_AMV)
453 len >>= s->quarter_sample;
455 if(s->real_sprite_warping_points==1){
456 if(s->divx_version==500 && s->divx_build==413)
457 sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
459 sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
461 dx= s->sprite_delta[n][0];
462 dy= s->sprite_delta[n][1];
463 shift= s->sprite_shift[0];
464 if(n) dy -= 1<<(shift + a + 1);
465 else dx -= 1<<(shift + a + 1);
466 mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
479 sum= RSHIFT(sum, a+8-s->quarter_sample);
482 if (sum < -len) sum= -len;
483 else if (sum >= len) sum= len-1;
489 * Decode the dc value.
490 * @param n block index (0-3 are luma, 4-5 are chroma)
491 * @param dir_ptr the prediction direction will be stored here
492 * @return the quantized dc
494 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
499 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
501 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
502 if (code < 0 || code > 9 /* && s->nbit<9 */){
503 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
511 level= 2*get_bits1(&s->gb)-1;
513 if(get_bits1(&s->gb))
514 level = get_bits(&s->gb, code-1) + (1<<(code-1));
516 level = -get_bits(&s->gb, code-1) - (1<<(code-1));
519 level = get_xbits(&s->gb, code);
523 if(get_bits1(&s->gb)==0){ /* marker */
524 if(s->err_recognition&AV_EF_BITSTREAM){
525 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
532 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
536 * Decode first partition.
537 * @return number of MBs decoded or <0 if an error occurred
539 static int mpeg4_decode_partition_a(MpegEncContext *s){
541 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
543 /* decode first partition */
545 s->first_slice_line=1;
546 for(; s->mb_y<s->mb_height; s->mb_y++){
547 ff_init_block_index(s);
548 for(; s->mb_x<s->mb_width; s->mb_x++){
549 const int xy= s->mb_x + s->mb_y*s->mb_stride;
554 ff_update_block_index(s);
555 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
556 s->first_slice_line=0;
558 if(s->pict_type==AV_PICTURE_TYPE_I){
562 if(show_bits_long(&s->gb, 19)==DC_MARKER){
566 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
568 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
573 s->cbp_table[xy]= cbpc & 3;
574 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
578 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
580 s->current_picture.f.qscale_table[xy]= s->qscale;
582 s->mbintra_table[xy]= 1;
585 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
587 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
591 if(dc_pred_dir) dir|=1;
593 s->pred_dir_table[xy]= dir;
594 }else{ /* P/S_TYPE */
595 int mx, my, pred_x, pred_y, bits;
596 int16_t * const mot_val = s->current_picture.f.motion_val[0][s->block_index[0]];
597 const int stride= s->b8_stride*2;
600 bits= show_bits(&s->gb, 17);
601 if(bits==MOTION_MARKER){
607 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
608 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
612 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
615 mot_val[0 ]= mot_val[2 ]=
616 mot_val[0+stride]= mot_val[2+stride]= mx;
617 mot_val[1 ]= mot_val[3 ]=
618 mot_val[1+stride]= mot_val[3+stride]= my;
620 if(s->mbintra_table[xy])
621 ff_clean_intra_table_entries(s);
625 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
627 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
633 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
635 s->mb_intra = ((cbpc & 4) != 0);
638 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
639 s->mbintra_table[xy]= 1;
640 mot_val[0 ]= mot_val[2 ]=
641 mot_val[0+stride]= mot_val[2+stride]= 0;
642 mot_val[1 ]= mot_val[3 ]=
643 mot_val[1+stride]= mot_val[3+stride]= 0;
645 if(s->mbintra_table[xy])
646 ff_clean_intra_table_entries(s);
648 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
649 s->mcsel= get_bits1(&s->gb);
652 if ((cbpc & 16) == 0) {
653 /* 16x16 motion prediction */
655 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
657 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
661 my = ff_h263_decode_motion(s, pred_y, s->f_code);
664 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
668 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
671 mot_val[0 ]= mot_val[2 ] =
672 mot_val[0+stride]= mot_val[2+stride]= mx;
673 mot_val[1 ]= mot_val[3 ]=
674 mot_val[1+stride]= mot_val[3+stride]= my;
677 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
679 int16_t *mot_val= ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
680 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
684 my = ff_h263_decode_motion(s, pred_y, s->f_code);
701 * decode second partition.
702 * @return <0 if an error occurred
704 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
706 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
708 s->mb_x= s->resync_mb_x;
709 s->first_slice_line=1;
710 for(s->mb_y= s->resync_mb_y; mb_num < mb_count; s->mb_y++){
711 ff_init_block_index(s);
712 for(; mb_num < mb_count && s->mb_x<s->mb_width; s->mb_x++){
713 const int xy= s->mb_x + s->mb_y*s->mb_stride;
716 ff_update_block_index(s);
717 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
718 s->first_slice_line=0;
720 if(s->pict_type==AV_PICTURE_TYPE_I){
721 int ac_pred= get_bits1(&s->gb);
722 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
724 av_log(s->avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
728 s->cbp_table[xy]|= cbpy<<2;
729 s->current_picture.f.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
730 }else{ /* P || S_TYPE */
731 if (IS_INTRA(s->current_picture.f.mb_type[xy])) {
733 int ac_pred = get_bits1(&s->gb);
734 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
737 av_log(s->avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
741 if(s->cbp_table[xy] & 8) {
742 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
744 s->current_picture.f.qscale_table[xy] = s->qscale;
748 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
750 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
754 if(dc_pred_dir) dir|=1;
756 s->cbp_table[xy]&= 3; //remove dquant
757 s->cbp_table[xy]|= cbpy<<2;
758 s->current_picture.f.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
759 s->pred_dir_table[xy]= dir;
760 } else if (IS_SKIP(s->current_picture.f.mb_type[xy])) {
761 s->current_picture.f.qscale_table[xy] = s->qscale;
764 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
767 av_log(s->avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
771 if(s->cbp_table[xy] & 8) {
772 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
774 s->current_picture.f.qscale_table[xy] = s->qscale;
776 s->cbp_table[xy]&= 3; //remove dquant
777 s->cbp_table[xy]|= (cbpy^0xf)<<2;
781 if(mb_num >= mb_count) return 0;
788 * Decode the first and second partition.
789 * @return <0 if error (and sets error type in the error_status_table)
791 int ff_mpeg4_decode_partitions(MpegEncContext *s)
794 const int part_a_error= s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_ERROR|ER_MV_ERROR) : ER_MV_ERROR;
795 const int part_a_end = s->pict_type==AV_PICTURE_TYPE_I ? (ER_DC_END |ER_MV_END) : ER_MV_END;
797 mb_num= mpeg4_decode_partition_a(s);
799 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
803 if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){
804 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
805 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
809 s->mb_num_left= mb_num;
811 if(s->pict_type==AV_PICTURE_TYPE_I){
812 while(show_bits(&s->gb, 9) == 1)
813 skip_bits(&s->gb, 9);
814 if(get_bits_long(&s->gb, 19)!=DC_MARKER){
815 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y);
819 while(show_bits(&s->gb, 10) == 1)
820 skip_bits(&s->gb, 10);
821 if(get_bits(&s->gb, 17)!=MOTION_MARKER){
822 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y);
826 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end);
828 if( mpeg4_decode_partition_b(s, mb_num) < 0){
829 if(s->pict_type==AV_PICTURE_TYPE_P)
830 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_DC_ERROR);
833 if(s->pict_type==AV_PICTURE_TYPE_P)
834 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_DC_END);
842 * @return <0 if an error occurred
844 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
845 int n, int coded, int intra, int rvlc)
847 int level, i, last, run;
850 RL_VLC_ELEM * rl_vlc;
851 const uint8_t * scan_table;
854 //Note intra & rvlc should be optimized away if this is inlined
857 if(s->use_intra_dc_vlc){
859 if(s->partitioned_frame){
860 level = s->dc_val[0][ s->block_index[n] ];
861 if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
862 else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
863 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
865 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
873 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
879 rl = &ff_rvlc_rl_intra;
880 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
882 rl = &ff_mpeg4_rl_intra;
883 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
886 if (dc_pred_dir == 0)
887 scan_table = s->intra_v_scantable.permutated; /* left */
889 scan_table = s->intra_h_scantable.permutated; /* top */
891 scan_table = s->intra_scantable.permutated;
898 s->block_last_index[n] = i;
901 if(rvlc) rl = &ff_rvlc_rl_inter;
902 else rl = &ff_h263_rl_inter;
904 scan_table = s->intra_scantable.permutated;
910 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
912 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
915 qmul = s->qscale << 1;
916 qadd = (s->qscale - 1) | 1;
918 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
920 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
925 OPEN_READER(re, &s->gb);
927 UPDATE_CACHE(re, &s->gb);
928 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
932 if(SHOW_UBITS(re, &s->gb, 1)==0){
933 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
935 }; SKIP_CACHE(re, &s->gb, 1);
937 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
938 run= SHOW_UBITS(re, &s->gb, 6);
939 SKIP_COUNTER(re, &s->gb, 1+1+6);
940 UPDATE_CACHE(re, &s->gb);
942 if(SHOW_UBITS(re, &s->gb, 1)==0){
943 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
945 }; SKIP_CACHE(re, &s->gb, 1);
947 level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
949 if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
950 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
952 }; SKIP_CACHE(re, &s->gb, 5);
954 level= level * qmul + qadd;
955 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
956 SKIP_COUNTER(re, &s->gb, 1+11+5+1);
962 cache= GET_CACHE(re, &s->gb);
967 if (cache&0x80000000) {
968 if (cache&0x40000000) {
970 SKIP_CACHE(re, &s->gb, 2);
971 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
972 run= SHOW_UBITS(re, &s->gb, 6);
973 SKIP_COUNTER(re, &s->gb, 2+1+6);
974 UPDATE_CACHE(re, &s->gb);
977 level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
979 if(SHOW_UBITS(re, &s->gb, 1)==0){
980 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
982 }; SKIP_CACHE(re, &s->gb, 1);
984 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
986 if(SHOW_UBITS(re, &s->gb, 1)==0){
987 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
991 SKIP_COUNTER(re, &s->gb, 1+12+1);
994 if (level>0) level= level * qmul + qadd;
995 else level= level * qmul - qadd;
997 if((unsigned)(level + 2048) > 4095){
998 if(s->err_recognition & AV_EF_BITSTREAM){
999 if(level > 2560 || level<-2560){
1000 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
1004 level= level<0 ? -2048 : 2047;
1011 SKIP_BITS(re, &s->gb, 2);
1012 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1013 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
1014 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1015 LAST_SKIP_BITS(re, &s->gb, 1);
1019 SKIP_BITS(re, &s->gb, 1);
1020 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1022 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
1023 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1024 LAST_SKIP_BITS(re, &s->gb, 1);
1029 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1030 LAST_SKIP_BITS(re, &s->gb, 1);
1035 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1039 block[scan_table[i]] = level;
1043 block[scan_table[i]] = level;
1045 CLOSE_READER(re, &s->gb);
1049 if(!s->use_intra_dc_vlc){
1050 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1052 i -= i>>31; //if(i == -1) i=0;
1055 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1057 i = 63; /* XXX: not optimal */
1060 s->block_last_index[n] = i;
1065 * decode partition C of one MB.
1066 * @return <0 if an error occurred
1068 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
1071 const int xy= s->mb_x + s->mb_y*s->mb_stride;
1073 mb_type = s->current_picture.f.mb_type[xy];
1074 cbp = s->cbp_table[xy];
1076 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
1078 if (s->current_picture.f.qscale_table[xy] != s->qscale) {
1079 ff_set_qscale(s, s->current_picture.f.qscale_table[xy]);
1082 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
1085 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
1086 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
1088 s->mb_intra = IS_INTRA(mb_type);
1090 if (IS_SKIP(mb_type)) {
1093 s->block_last_index[i] = -1;
1094 s->mv_dir = MV_DIR_FORWARD;
1095 s->mv_type = MV_TYPE_16X16;
1096 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
1103 }else if(s->mb_intra){
1104 s->ac_pred = IS_ACPRED(s->current_picture.f.mb_type[xy]);
1105 }else if(!s->mb_intra){
1106 // s->mcsel= 0; //FIXME do we need to init that
1108 s->mv_dir = MV_DIR_FORWARD;
1109 if (IS_8X8(mb_type)) {
1110 s->mv_type = MV_TYPE_8X8;
1112 s->mv_type = MV_TYPE_16X16;
1115 } else { /* I-Frame */
1117 s->ac_pred = IS_ACPRED(s->current_picture.f.mb_type[xy]);
1120 if (!IS_SKIP(mb_type)) {
1122 s->dsp.clear_blocks(s->block[0]);
1123 /* decode each block */
1124 for (i = 0; i < 6; i++) {
1125 if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
1126 av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
1133 /* per-MB end of slice check */
1135 if(--s->mb_num_left <= 0){
1136 if(mpeg4_is_resync(s))
1141 if(mpeg4_is_resync(s)){
1142 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1143 if(s->cbp_table[xy+delta])
1150 static int mpeg4_decode_mb(MpegEncContext *s,
1151 DCTELEM block[6][64])
1153 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1155 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
1156 const int xy= s->mb_x + s->mb_y * s->mb_stride;
1158 assert(s->h263_pred);
1160 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
1162 if (get_bits1(&s->gb)) {
1166 s->block_last_index[i] = -1;
1167 s->mv_dir = MV_DIR_FORWARD;
1168 s->mv_type = MV_TYPE_16X16;
1169 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
1170 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
1172 s->mv[0][0][0]= get_amv(s, 0);
1173 s->mv[0][0][1]= get_amv(s, 1);
1177 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
1185 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1187 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1192 s->dsp.clear_blocks(s->block[0]);
1194 s->mb_intra = ((cbpc & 4) != 0);
1195 if (s->mb_intra) goto intra;
1197 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
1198 s->mcsel= get_bits1(&s->gb);
1200 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1202 cbp = (cbpc & 3) | (cbpy << 2);
1204 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1206 if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
1207 s->interlaced_dct= get_bits1(&s->gb);
1209 s->mv_dir = MV_DIR_FORWARD;
1210 if ((cbpc & 16) == 0) {
1212 s->current_picture.f.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
1213 /* 16x16 global motion prediction */
1214 s->mv_type = MV_TYPE_16X16;
1217 s->mv[0][0][0] = mx;
1218 s->mv[0][0][1] = my;
1219 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
1220 s->current_picture.f.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
1221 /* 16x8 field motion prediction */
1222 s->mv_type= MV_TYPE_FIELD;
1224 s->field_select[0][0]= get_bits1(&s->gb);
1225 s->field_select[0][1]= get_bits1(&s->gb);
1227 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1230 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1234 my = ff_h263_decode_motion(s, pred_y/2, s->f_code);
1238 s->mv[0][i][0] = mx;
1239 s->mv[0][i][1] = my;
1242 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1243 /* 16x16 motion prediction */
1244 s->mv_type = MV_TYPE_16X16;
1245 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1246 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1251 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1255 s->mv[0][0][0] = mx;
1256 s->mv[0][0][1] = my;
1259 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1260 s->mv_type = MV_TYPE_8X8;
1262 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1263 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1267 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1270 s->mv[0][i][0] = mx;
1271 s->mv[0][i][1] = my;
1276 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
1277 int modb1; // first bit of modb
1278 int modb2; // second bit of modb
1281 s->mb_intra = 0; //B-frames never contain intra blocks
1282 s->mcsel=0; // ... true gmc blocks
1286 s->last_mv[i][0][0]=
1287 s->last_mv[i][0][1]=
1288 s->last_mv[i][1][0]=
1289 s->last_mv[i][1][1]= 0;
1292 ff_thread_await_progress(&s->next_picture_ptr->f, s->mb_y, 0);
1295 /* if we skipped it in the future P Frame than skip it now too */
1296 s->mb_skipped = s->next_picture.f.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1301 s->block_last_index[i] = -1;
1303 s->mv_dir = MV_DIR_FORWARD;
1304 s->mv_type = MV_TYPE_16X16;
1309 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
1313 modb1= get_bits1(&s->gb);
1315 mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded
1318 modb2= get_bits1(&s->gb);
1319 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1321 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1324 mb_type= mb_type_b_map[ mb_type ];
1327 s->dsp.clear_blocks(s->block[0]);
1328 cbp= get_bits(&s->gb, 6);
1331 if ((!IS_DIRECT(mb_type)) && cbp) {
1332 if(get_bits1(&s->gb)){
1333 ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
1337 if(!s->progressive_sequence){
1339 s->interlaced_dct= get_bits1(&s->gb);
1341 if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){
1342 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1343 mb_type &= ~MB_TYPE_16x16;
1345 if(USES_LIST(mb_type, 0)){
1346 s->field_select[0][0]= get_bits1(&s->gb);
1347 s->field_select[0][1]= get_bits1(&s->gb);
1349 if(USES_LIST(mb_type, 1)){
1350 s->field_select[1][0]= get_bits1(&s->gb);
1351 s->field_select[1][1]= get_bits1(&s->gb);
1357 if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){
1358 s->mv_type= MV_TYPE_16X16;
1360 if(USES_LIST(mb_type, 0)){
1361 s->mv_dir = MV_DIR_FORWARD;
1363 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1364 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1365 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1366 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1369 if(USES_LIST(mb_type, 1)){
1370 s->mv_dir |= MV_DIR_BACKWARD;
1372 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1373 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1374 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1375 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1377 }else if(!IS_DIRECT(mb_type)){
1378 s->mv_type= MV_TYPE_FIELD;
1380 if(USES_LIST(mb_type, 0)){
1381 s->mv_dir = MV_DIR_FORWARD;
1384 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code);
1385 my = ff_h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code);
1386 s->last_mv[0][i][0]= s->mv[0][i][0] = mx;
1387 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2;
1391 if(USES_LIST(mb_type, 1)){
1392 s->mv_dir |= MV_DIR_BACKWARD;
1395 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code);
1396 my = ff_h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code);
1397 s->last_mv[1][i][0]= s->mv[1][i][0] = mx;
1398 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2;
1404 if(IS_DIRECT(mb_type)){
1405 if(IS_SKIP(mb_type))
1408 mx = ff_h263_decode_motion(s, 0, 1);
1409 my = ff_h263_decode_motion(s, 0, 1);
1412 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1413 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1415 s->current_picture.f.mb_type[xy] = mb_type;
1416 } else { /* I-Frame */
1418 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1420 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1428 s->ac_pred = get_bits1(&s->gb);
1430 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1432 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
1434 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1436 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1439 cbp = (cbpc & 3) | (cbpy << 2);
1441 s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
1444 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1447 if(!s->progressive_sequence)
1448 s->interlaced_dct= get_bits1(&s->gb);
1450 s->dsp.clear_blocks(s->block[0]);
1451 /* decode each block */
1452 for (i = 0; i < 6; i++) {
1453 if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
1460 /* decode each block */
1461 for (i = 0; i < 6; i++) {
1462 if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
1468 /* per-MB end of slice check */
1469 if(s->codec_id==CODEC_ID_MPEG4){
1470 if(mpeg4_is_resync(s)){
1471 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1473 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture.f.mbskip_table[xy + delta]) {
1474 ff_thread_await_progress(&s->next_picture_ptr->f,
1475 (s->mb_x + delta >= s->mb_width) ? FFMIN(s->mb_y+1, s->mb_height-1) : s->mb_y, 0);
1478 if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture.f.mbskip_table[xy + delta])
1488 static int mpeg4_decode_gop_header(MpegEncContext * s, GetBitContext *gb){
1489 int hours, minutes, seconds;
1490 unsigned time_code = show_bits(gb, 18);
1492 if (time_code & 0x40) { /* marker_bit */
1493 hours = time_code >> 13;
1494 minutes = time_code >> 7 & 0x3f;
1495 seconds = time_code & 0x3f;
1496 s->time_base = seconds + 60*(minutes + 60*hours);
1497 skip_bits(gb, 20); /* time_code, closed_gov, broken_link */
1499 av_log(s->avctx, AV_LOG_WARNING, "GOP header missing marker_bit\n");
1505 static int mpeg4_decode_profile_level(MpegEncContext * s, GetBitContext *gb){
1506 int profile_and_level_indication;
1508 profile_and_level_indication = get_bits(gb, 8);
1510 s->avctx->profile = (profile_and_level_indication & 0xf0) >> 4;
1511 s->avctx->level = (profile_and_level_indication & 0x0f);
1513 // for Simple profile, level 0
1514 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1515 s->avctx->level = 0;
1521 static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){
1522 int width, height, vo_ver_id;
1525 skip_bits(gb, 1); /* random access */
1526 s->vo_type= get_bits(gb, 8);
1527 if (get_bits1(gb) != 0) { /* is_ol_id */
1528 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1529 skip_bits(gb, 3); /* vo_priority */
1533 s->aspect_ratio_info= get_bits(gb, 4);
1534 if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){
1535 s->avctx->sample_aspect_ratio.num= get_bits(gb, 8); // par_width
1536 s->avctx->sample_aspect_ratio.den= get_bits(gb, 8); // par_height
1538 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1541 if ((s->vol_control_parameters=get_bits1(gb))) { /* vol control parameter */
1542 int chroma_format= get_bits(gb, 2);
1543 if(chroma_format!=CHROMA_420){
1544 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1546 s->low_delay= get_bits1(gb);
1547 if(get_bits1(gb)){ /* vbv parameters */
1548 get_bits(gb, 15); /* first_half_bitrate */
1549 skip_bits1(gb); /* marker */
1550 get_bits(gb, 15); /* latter_half_bitrate */
1551 skip_bits1(gb); /* marker */
1552 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1553 skip_bits1(gb); /* marker */
1554 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1555 get_bits(gb, 11); /* first_half_vbv_occupancy */
1556 skip_bits1(gb); /* marker */
1557 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1558 skip_bits1(gb); /* marker */
1561 // set low delay flag only once the smartest? low delay detection won't be overriden
1562 if(s->picture_number==0)
1566 s->shape = get_bits(gb, 2); /* vol shape */
1567 if(s->shape != RECT_SHAPE) av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1568 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
1569 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1570 skip_bits(gb, 4); //video_object_layer_shape_extension
1573 check_marker(gb, "before time_increment_resolution");
1575 s->avctx->time_base.den = get_bits(gb, 16);
1576 if(!s->avctx->time_base.den){
1577 av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
1578 s->avctx->time_base.num = 0;
1582 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1583 if (s->time_increment_bits < 1)
1584 s->time_increment_bits = 1;
1586 check_marker(gb, "before fixed_vop_rate");
1588 if (get_bits1(gb) != 0) { /* fixed_vop_rate */
1589 s->avctx->time_base.num = get_bits(gb, s->time_increment_bits);
1591 s->avctx->time_base.num = 1;
1595 if (s->shape != BIN_ONLY_SHAPE) {
1596 if (s->shape == RECT_SHAPE) {
1597 skip_bits1(gb); /* marker */
1598 width = get_bits(gb, 13);
1599 skip_bits1(gb); /* marker */
1600 height = get_bits(gb, 13);
1601 skip_bits1(gb); /* marker */
1602 if(width && height && !(s->width && s->codec_tag == AV_RL32("MP4S"))){ /* they should be non zero but who knows ... */
1608 s->progressive_sequence=
1609 s->progressive_frame= get_bits1(gb)^1;
1610 s->interlaced_dct=0;
1611 if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1612 av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
1613 if (vo_ver_id == 1) {
1614 s->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1616 s->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1618 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1619 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
1620 if(s->vol_sprite_usage==STATIC_SPRITE){
1621 s->sprite_width = get_bits(gb, 13);
1622 skip_bits1(gb); /* marker */
1623 s->sprite_height= get_bits(gb, 13);
1624 skip_bits1(gb); /* marker */
1625 s->sprite_left = get_bits(gb, 13);
1626 skip_bits1(gb); /* marker */
1627 s->sprite_top = get_bits(gb, 13);
1628 skip_bits1(gb); /* marker */
1630 s->num_sprite_warping_points= get_bits(gb, 6);
1631 if(s->num_sprite_warping_points > 3){
1632 av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points);
1633 s->num_sprite_warping_points= 0;
1636 s->sprite_warping_accuracy = get_bits(gb, 2);
1637 s->sprite_brightness_change= get_bits1(gb);
1638 if(s->vol_sprite_usage==STATIC_SPRITE)
1639 s->low_latency_sprite= get_bits1(gb);
1641 // FIXME sadct disable bit if verid!=1 && shape not rect
1643 if (get_bits1(gb) == 1) { /* not_8_bit */
1644 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1645 if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n"); /* bits_per_pixel */
1646 if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precision %d\n", s->quant_precision);
1648 s->quant_precision = 5;
1651 // FIXME a bunch of grayscale shape things
1653 if((s->mpeg_quant=get_bits1(gb))){ /* vol_quant_type */
1656 /* load default matrixes */
1657 for(i=0; i<64; i++){
1658 int j= s->dsp.idct_permutation[i];
1659 v= ff_mpeg4_default_intra_matrix[i];
1660 s->intra_matrix[j]= v;
1661 s->chroma_intra_matrix[j]= v;
1663 v= ff_mpeg4_default_non_intra_matrix[i];
1664 s->inter_matrix[j]= v;
1665 s->chroma_inter_matrix[j]= v;
1668 /* load custom intra matrix */
1671 for(i=0; i<64; i++){
1677 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1678 s->intra_matrix[j]= v;
1679 s->chroma_intra_matrix[j]= v;
1682 /* replicate last value */
1684 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1685 s->intra_matrix[j]= last;
1686 s->chroma_intra_matrix[j]= last;
1690 /* load custom non intra matrix */
1693 for(i=0; i<64; i++){
1699 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1700 s->inter_matrix[j]= v;
1701 s->chroma_inter_matrix[j]= v;
1704 /* replicate last value */
1706 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1707 s->inter_matrix[j]= last;
1708 s->chroma_inter_matrix[j]= last;
1712 // FIXME a bunch of grayscale shape things
1716 s->quarter_sample= get_bits1(gb);
1717 else s->quarter_sample=0;
1720 int pos= get_bits_count(gb);
1721 int estimation_method= get_bits(gb, 2);
1722 if(estimation_method<2){
1724 s->cplx_estimation_trash_i += 8*get_bits1(gb); //opaque
1725 s->cplx_estimation_trash_i += 8*get_bits1(gb); //transparent
1726 s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_cae
1727 s->cplx_estimation_trash_i += 8*get_bits1(gb); //inter_cae
1728 s->cplx_estimation_trash_i += 8*get_bits1(gb); //no_update
1729 s->cplx_estimation_trash_i += 8*get_bits1(gb); //upampling
1732 s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_blocks
1733 s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter_blocks
1734 s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter4v_blocks
1735 s->cplx_estimation_trash_i += 8*get_bits1(gb); //not coded blocks
1737 if(!check_marker(gb, "in complexity estimation part 1")){
1738 skip_bits_long(gb, pos - get_bits_count(gb));
1742 s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_coeffs
1743 s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_lines
1744 s->cplx_estimation_trash_i += 8*get_bits1(gb); //vlc_syms
1745 s->cplx_estimation_trash_i += 4*get_bits1(gb); //vlc_bits
1748 s->cplx_estimation_trash_p += 8*get_bits1(gb); //apm
1749 s->cplx_estimation_trash_p += 8*get_bits1(gb); //npm
1750 s->cplx_estimation_trash_b += 8*get_bits1(gb); //interpolate_mc_q
1751 s->cplx_estimation_trash_p += 8*get_bits1(gb); //forwback_mc_q
1752 s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel2
1753 s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel4
1755 if(!check_marker(gb, "in complexity estimation part 2")){
1756 skip_bits_long(gb, pos - get_bits_count(gb));
1759 if(estimation_method==1){
1760 s->cplx_estimation_trash_i += 8*get_bits1(gb); //sadct
1761 s->cplx_estimation_trash_p += 8*get_bits1(gb); //qpel
1764 av_log(s->avctx, AV_LOG_ERROR, "Invalid Complexity estimation method %d\n", estimation_method);
1767 s->cplx_estimation_trash_i=
1768 s->cplx_estimation_trash_p=
1769 s->cplx_estimation_trash_b= 0;
1772 s->resync_marker= !get_bits1(gb); /* resync_marker_disabled */
1774 s->data_partitioning= get_bits1(gb);
1775 if(s->data_partitioning){
1776 s->rvlc= get_bits1(gb);
1779 if(vo_ver_id != 1) {
1780 s->new_pred= get_bits1(gb);
1782 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1783 skip_bits(gb, 2); /* requested upstream message type */
1784 skip_bits1(gb); /* newpred segment type */
1786 s->reduced_res_vop= get_bits1(gb);
1787 if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
1791 s->reduced_res_vop= 0;
1794 s->scalability= get_bits1(gb);
1796 if (s->scalability) {
1797 GetBitContext bak= *gb;
1798 int h_sampling_factor_n;
1799 int h_sampling_factor_m;
1800 int v_sampling_factor_n;
1801 int v_sampling_factor_m;
1803 s->hierachy_type= get_bits1(gb);
1804 skip_bits(gb, 4); /* ref_layer_id */
1805 skip_bits1(gb); /* ref_layer_sampling_dir */
1806 h_sampling_factor_n= get_bits(gb, 5);
1807 h_sampling_factor_m= get_bits(gb, 5);
1808 v_sampling_factor_n= get_bits(gb, 5);
1809 v_sampling_factor_m= get_bits(gb, 5);
1810 s->enhancement_type= get_bits1(gb);
1812 if( h_sampling_factor_n==0 || h_sampling_factor_m==0
1813 || v_sampling_factor_n==0 || v_sampling_factor_m==0){
1814 /* illegal scalability header (VERY broken encoder),
1815 * trying to workaround */
1819 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
1821 // bin shape stuff FIXME
1828 * Decode the user data stuff in the header.
1829 * Also initializes divx/xvid/lavc_version/build.
1831 static int decode_user_data(MpegEncContext *s, GetBitContext *gb){
1835 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
1838 for(i=0; i<255 && get_bits_count(gb) < gb->size_in_bits; i++){
1839 if(show_bits(gb, 23) == 0) break;
1840 buf[i]= get_bits(gb, 8);
1844 /* divx detection */
1845 e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
1847 e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
1849 s->divx_version= ver;
1850 s->divx_build= build;
1851 s->divx_packed= e==3 && last=='p';
1852 if(s->divx_packed && !s->showed_packed_warning) {
1853 av_log(s->avctx, AV_LOG_WARNING, "Invalid and inefficient vfw-avi packed B frames detected\n");
1854 s->showed_packed_warning=1;
1858 /* libavcodec detection */
1859 e=sscanf(buf, "FFmpe%*[^b]b%d", &build)+3;
1861 e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
1863 e=sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3)+1;
1865 build= (ver<<16) + (ver2<<8) + ver3;
1868 if(strcmp(buf, "ffmpeg")==0){
1869 s->lavc_build= 4600;
1873 s->lavc_build= build;
1876 /* Xvid detection */
1877 e=sscanf(buf, "XviD%d", &build);
1879 s->xvid_build= build;
1885 static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
1886 int time_incr, time_increment;
1888 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
1889 if(s->pict_type==AV_PICTURE_TYPE_B && s->low_delay && s->vol_control_parameters==0 && !(s->flags & CODEC_FLAG_LOW_DELAY)){
1890 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
1894 s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
1895 if(s->partitioned_frame)
1896 s->decode_mb= mpeg4_decode_partitioned_mb;
1898 s->decode_mb= mpeg4_decode_mb;
1901 while (get_bits1(gb) != 0)
1904 check_marker(gb, "before time_increment");
1906 if(s->time_increment_bits==0 || !(show_bits(gb, s->time_increment_bits+1)&1)){
1907 av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
1909 for(s->time_increment_bits=1 ;s->time_increment_bits<16; s->time_increment_bits++){
1910 if ( s->pict_type == AV_PICTURE_TYPE_P
1911 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
1912 if((show_bits(gb, s->time_increment_bits+6)&0x37) == 0x30) break;
1914 if((show_bits(gb, s->time_increment_bits+5)&0x1F) == 0x18) break;
1917 av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
1920 if(IS_3IV1) time_increment= get_bits1(gb); //FIXME investigate further
1921 else time_increment= get_bits(gb, s->time_increment_bits);
1923 if(s->pict_type!=AV_PICTURE_TYPE_B){
1924 s->last_time_base= s->time_base;
1925 s->time_base+= time_incr;
1926 s->time= s->time_base*s->avctx->time_base.den + time_increment;
1927 if(s->workaround_bugs&FF_BUG_UMP4){
1928 if(s->time < s->last_non_b_time){
1929 /* header is not mpeg-4-compatible, broken encoder,
1930 * trying to workaround */
1932 s->time+= s->avctx->time_base.den;
1935 s->pp_time= s->time - s->last_non_b_time;
1936 s->last_non_b_time= s->time;
1938 s->time= (s->last_time_base + time_incr)*s->avctx->time_base.den + time_increment;
1939 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
1940 if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
1941 /* messed up order, maybe after seeking? skipping current b-frame */
1942 return FRAME_SKIPPED;
1944 ff_mpeg4_init_direct_mv(s);
1946 if(s->t_frame==0) s->t_frame= s->pb_time;
1947 if(s->t_frame==0) s->t_frame=1; // 1/0 protection
1948 s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame)
1949 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
1950 s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
1951 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
1952 if(!s->progressive_sequence){
1953 if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
1954 return FRAME_SKIPPED;
1958 if(s->avctx->time_base.num)
1959 s->current_picture_ptr->f.pts = (s->time + s->avctx->time_base.num / 2) / s->avctx->time_base.num;
1961 s->current_picture_ptr->f.pts = AV_NOPTS_VALUE;
1962 if(s->avctx->debug&FF_DEBUG_PTS)
1963 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
1964 s->current_picture_ptr->f.pts);
1966 check_marker(gb, "before vop_coded");
1969 if (get_bits1(gb) != 1){
1970 if(s->avctx->debug&FF_DEBUG_PICT_INFO)
1971 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
1972 return FRAME_SKIPPED;
1974 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == AV_PICTURE_TYPE_P
1975 || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE))) {
1976 /* rounding type for motion estimation */
1977 s->no_rounding = get_bits1(gb);
1981 //FIXME reduced res stuff
1983 if (s->shape != RECT_SHAPE) {
1984 if (s->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
1985 skip_bits(gb, 13); /* width */
1986 skip_bits1(gb); /* marker */
1987 skip_bits(gb, 13); /* height */
1988 skip_bits1(gb); /* marker */
1989 skip_bits(gb, 13); /* hor_spat_ref */
1990 skip_bits1(gb); /* marker */
1991 skip_bits(gb, 13); /* ver_spat_ref */
1993 skip_bits1(gb); /* change_CR_disable */
1995 if (get_bits1(gb) != 0) {
1996 skip_bits(gb, 8); /* constant_alpha_value */
1999 //FIXME complexity estimation stuff
2001 if (s->shape != BIN_ONLY_SHAPE) {
2002 skip_bits_long(gb, s->cplx_estimation_trash_i);
2003 if(s->pict_type != AV_PICTURE_TYPE_I)
2004 skip_bits_long(gb, s->cplx_estimation_trash_p);
2005 if(s->pict_type == AV_PICTURE_TYPE_B)
2006 skip_bits_long(gb, s->cplx_estimation_trash_b);
2008 s->intra_dc_threshold= ff_mpeg4_dc_threshold[ get_bits(gb, 3) ];
2009 if(!s->progressive_sequence){
2010 s->top_field_first= get_bits1(gb);
2011 s->alternate_scan= get_bits1(gb);
2013 s->alternate_scan= 0;
2016 if(s->alternate_scan){
2017 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
2018 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
2019 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2020 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2022 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
2023 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
2024 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2025 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2028 if(s->pict_type == AV_PICTURE_TYPE_S && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
2029 mpeg4_decode_sprite_trajectory(s, gb);
2030 if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
2031 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2034 if (s->shape != BIN_ONLY_SHAPE) {
2035 s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
2037 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
2038 return -1; // makes no sense to continue, as there is nothing left from the image then
2041 if (s->pict_type != AV_PICTURE_TYPE_I) {
2042 s->f_code = get_bits(gb, 3); /* fcode_for */
2044 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
2045 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2050 if (s->pict_type == AV_PICTURE_TYPE_B) {
2051 s->b_code = get_bits(gb, 3);
2055 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2056 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",
2057 s->qscale, s->f_code, s->b_code,
2058 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")),
2059 gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first,
2060 s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
2061 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);
2064 if(!s->scalability){
2065 if (s->shape!=RECT_SHAPE && s->pict_type!=AV_PICTURE_TYPE_I) {
2066 skip_bits1(gb); // vop shape coding type
2069 if(s->enhancement_type){
2070 int load_backward_shape= get_bits1(gb);
2071 if(load_backward_shape){
2072 av_log(s->avctx, AV_LOG_ERROR, "load backward shape isn't supported\n");
2075 skip_bits(gb, 2); //ref_select_code
2078 /* detect buggy encoders which don't set the low_delay flag (divx4/xvid/opendivx)*/
2079 // note we cannot detect divx5 without b-frames easily (although it's buggy too)
2080 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==-1 && s->picture_number==0){
2081 av_log(s->avctx, AV_LOG_WARNING, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2085 s->picture_number++; // better than pic number==0 always ;)
2087 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
2088 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
2090 if(s->workaround_bugs&FF_BUG_EDGE){
2091 s->h_edge_pos= s->width;
2092 s->v_edge_pos= s->height;
2098 * Decode mpeg4 headers.
2099 * @return <0 if no VOP found (or a damaged one)
2100 * FRAME_SKIPPED if a not coded VOP is found
2101 * 0 if a VOP is found
2103 int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb)
2105 unsigned startcode, v;
2107 /* search next start code */
2110 if(s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630){
2112 if(get_bits(gb, 8) == 0xF0)
2118 if(get_bits_count(gb) >= gb->size_in_bits){
2119 if(gb->size_in_bits==8 && (s->divx_version>=0 || s->xvid_build>=0)){
2120 av_log(s->avctx, AV_LOG_WARNING, "frame skip %d\n", gb->size_in_bits);
2121 return FRAME_SKIPPED; //divx bug
2123 return -1; //end of stream
2126 /* use the bits after the test */
2127 v = get_bits(gb, 8);
2128 startcode = ((startcode << 8) | v) & 0xffffffff;
2130 if((startcode&0xFFFFFF00) != 0x100)
2131 continue; //no startcode
2133 if(s->avctx->debug&FF_DEBUG_STARTCODE){
2134 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2135 if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2136 else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2137 else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2138 else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2139 else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2140 else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2141 else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2142 else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2143 else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2144 else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2145 else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2146 else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2147 else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2148 else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2149 else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2150 else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2151 else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2152 else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2153 else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2154 else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2155 else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2156 else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2157 else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2158 else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2159 else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2160 else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2161 else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
2162 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2165 if(startcode >= 0x120 && startcode <= 0x12F){
2166 if(decode_vol_header(s, gb) < 0)
2169 else if(startcode == USER_DATA_STARTCODE){
2170 decode_user_data(s, gb);
2172 else if(startcode == GOP_STARTCODE){
2173 mpeg4_decode_gop_header(s, gb);
2175 else if(startcode == VOS_STARTCODE){
2176 mpeg4_decode_profile_level(s, gb);
2178 else if(startcode == VOP_STARTCODE){
2186 if(s->flags& CODEC_FLAG_LOW_DELAY)
2188 s->avctx->has_b_frames= !s->low_delay;
2189 return decode_vop_header(s, gb);
2192 static av_cold int decode_init(AVCodecContext *avctx)
2194 MpegEncContext *s = avctx->priv_data;
2196 static int done = 0;
2203 if((ret=ff_h263_decode_init(avctx)) < 0)
2209 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2210 ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2211 ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2212 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2213 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2214 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2215 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2216 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2217 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2218 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2219 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2220 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2221 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2222 &ff_sprite_trajectory_tab[0][1], 4, 2,
2223 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2224 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2225 &ff_mb_type_b_tab[0][1], 2, 1,
2226 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2230 s->low_delay = 0; //default, might be overriden in the vol header during header parsing
2231 s->decode_mb= mpeg4_decode_mb;
2232 s->time_increment_bits = 4; /* default value for broken headers */
2233 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
2238 static const AVProfile mpeg4_video_profiles[] = {
2239 { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
2240 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
2241 { FF_PROFILE_MPEG4_CORE, "Core Profile" },
2242 { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
2243 { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
2244 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
2245 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
2246 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
2247 { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
2248 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
2249 { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
2250 { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
2251 { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
2252 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
2253 { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
2254 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
2257 AVCodec ff_mpeg4_decoder = {
2259 .type = AVMEDIA_TYPE_VIDEO,
2260 .id = CODEC_ID_MPEG4,
2261 .priv_data_size = sizeof(MpegEncContext),
2262 .init = decode_init,
2263 .close = ff_h263_decode_end,
2264 .decode = ff_h263_decode_frame,
2265 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2266 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2267 CODEC_CAP_FRAME_THREADS,
2268 .flush = ff_mpeg_flush,
2269 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2270 .pix_fmts = ff_hwaccel_pixfmt_list_420,
2271 .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
2272 .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_mpeg_update_thread_context),
2276 #if CONFIG_MPEG4_VDPAU_DECODER
2277 AVCodec ff_mpeg4_vdpau_decoder = {
2278 .name = "mpeg4_vdpau",
2279 .type = AVMEDIA_TYPE_VIDEO,
2280 .id = CODEC_ID_MPEG4,
2281 .priv_data_size = sizeof(MpegEncContext),
2282 .init = decode_init,
2283 .close = ff_h263_decode_end,
2284 .decode = ff_h263_decode_frame,
2285 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2286 CODEC_CAP_HWACCEL_VDPAU,
2287 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2288 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_MPEG4,