2 * H263/MPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * Copyright (c) 2001 Juan J. Sierralta P.
6 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
8 * This library 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 of the License, or (at your option) any later version.
13 * This library 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 this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * ac prediction encoding, b-frame support, error resilience, optimizations,
23 * qpel decoding, gmc decoding, interlaced decoding,
24 * by Michael Niedermayer <michaelni@gmx.at>
38 #include "mpegvideo.h"
40 #include "mpeg4data.h"
45 #define INTRA_MCBPC_VLC_BITS 6
46 #define INTER_MCBPC_VLC_BITS 7
47 #define CBPY_VLC_BITS 6
50 #define SPRITE_TRAJ_VLC_BITS 6
51 #define MB_TYPE_B_VLC_BITS 4
52 #define TEX_VLC_BITS 9
53 #define H263_MBTYPE_B_VLC_BITS 6
54 #define CBPC_B_VLC_BITS 3
56 #ifdef CONFIG_ENCODERS
57 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
59 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
60 static void h263p_encode_umotion(MpegEncContext * s, int val);
61 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
62 int n, int dc, uint8_t *scan_table,
63 PutBitContext *dc_pb, PutBitContext *ac_pb);
66 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
67 static int h263p_decode_umotion(MpegEncContext * s, int pred);
68 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
70 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
71 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
72 int n, int coded, int intra, int rvlc);
73 static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr);
74 #ifdef CONFIG_ENCODERS
75 static void mpeg4_encode_visual_object_header(MpegEncContext * s);
76 static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number);
77 #endif //CONFIG_ENCODERS
78 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb);
79 static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr);
81 #ifdef CONFIG_ENCODERS
82 static uint8_t uni_DCtab_lum_len[512];
83 static uint8_t uni_DCtab_chrom_len[512];
84 static uint16_t uni_DCtab_lum_bits[512];
85 static uint16_t uni_DCtab_chrom_bits[512];
87 static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
88 static uint8_t fcode_tab[MAX_MV*2+1];
89 static uint8_t umv_fcode_tab[MAX_MV*2+1];
91 static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
92 static uint8_t uni_mpeg4_intra_rl_len [64*64*2*2];
93 static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
94 static uint8_t uni_mpeg4_inter_rl_len [64*64*2*2];
95 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
96 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
97 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
110 #if 0 //3IV1 is quite rare and tis slows things down a tiny bit
111 #define IS_3IV1 s->avctx->codec_tag == ff_get_fourcc("3IV1")
116 int h263_get_picture_format(int width, int height)
120 if (width == 128 && height == 96)
122 else if (width == 176 && height == 144)
124 else if (width == 352 && height == 288)
126 else if (width == 704 && height == 576)
128 else if (width == 1408 && height == 1152)
135 #ifdef CONFIG_ENCODERS
137 static void aspect_to_info(MpegEncContext * s, AVRational aspect){
140 if(aspect.num==0) aspect= (AVRational){1,1};
143 if(av_cmp_q(pixel_aspect[i], aspect) == 0){
144 s->aspect_ratio_info=i;
149 s->aspect_ratio_info= FF_ASPECT_EXTENDED;
152 void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number)
156 align_put_bits(&s->pb);
158 put_bits(&s->pb, 17, 1);
159 put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */
160 put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) /
161 s->avctx->frame_rate) & 0xff); /* TemporalReference */
162 if (s->width == 352 && s->height == 288)
164 else if (s->width == 176 && s->height == 144)
166 else if (s->width == 128 && s->height == 96)
168 else if (s->width == 320 && s->height == 240)
170 else if (s->width == 160 && s->height == 120)
172 else if (s->width <= 255 && s->height <= 255)
173 format = 0; /* use 1 byte width & height */
175 format = 1; /* use 2 bytes width & height */
176 put_bits(&s->pb, 3, format); /* PictureSize */
178 put_bits(&s->pb, 8, s->width);
179 put_bits(&s->pb, 8, s->height);
180 } else if (format == 1) {
181 put_bits(&s->pb, 16, s->width);
182 put_bits(&s->pb, 16, s->height);
184 put_bits(&s->pb, 2, s->pict_type == P_TYPE); /* PictureType */
185 put_bits(&s->pb, 1, 1); /* DeblockingFlag: on */
186 put_bits(&s->pb, 5, s->qscale); /* Quantizer */
187 put_bits(&s->pb, 1, 0); /* ExtraInformation */
191 s->c_dc_scale_table= ff_aic_dc_scale_table;
194 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
198 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
200 int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
201 int best_clock_code=1;
203 int best_error= INT_MAX;
208 div= (s->avctx->frame_rate_base*1800000LL + 500LL*s->avctx->frame_rate) / ((1000LL+i)*s->avctx->frame_rate);
209 div= clip(1, div, 127);
210 error= ABS(s->avctx->frame_rate_base*1800000LL - (1000LL+i)*s->avctx->frame_rate*div);
211 if(error < best_error){
218 s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
219 coded_frame_rate= 1800000;
220 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
222 align_put_bits(&s->pb);
224 /* Update the pointer to last GOB */
225 s->ptr_lastgob = pbBufPtr(&s->pb);
226 put_bits(&s->pb, 22, 0x20); /* PSC */
227 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->frame_rate_base /
228 (coded_frame_rate_base * (int64_t)s->avctx->frame_rate);
229 put_bits(&s->pb, 8, temp_ref & 0xff); /* TemporalReference */
231 put_bits(&s->pb, 1, 1); /* marker */
232 put_bits(&s->pb, 1, 0); /* h263 id */
233 put_bits(&s->pb, 1, 0); /* split screen off */
234 put_bits(&s->pb, 1, 0); /* camera off */
235 put_bits(&s->pb, 1, 0); /* freeze picture release off */
237 format = h263_get_picture_format(s->width, s->height);
240 put_bits(&s->pb, 3, format);
241 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
242 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
243 of H.263v1 UMV implies to check the predicted MV after
244 calculation of the current MB to see if we're on the limits */
245 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
246 put_bits(&s->pb, 1, 0); /* SAC: off */
247 put_bits(&s->pb, 1, s->obmc); /* advanced prediction mode */
248 put_bits(&s->pb, 1, 0); /* not PB frame */
249 put_bits(&s->pb, 5, s->qscale);
250 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
254 /* H.263 Plus PTYPE */
256 put_bits(&s->pb, 3, 7);
257 put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
259 put_bits(&s->pb,3,6); /* Custom Source Format */
261 put_bits(&s->pb, 3, format);
263 put_bits(&s->pb,1, s->custom_pcf);
264 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
265 put_bits(&s->pb,1,0); /* SAC: off */
266 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
267 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
268 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
269 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
270 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
271 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
272 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
273 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
274 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
275 put_bits(&s->pb,3,0); /* Reserved */
277 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
279 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
280 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
281 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
282 put_bits(&s->pb,2,0); /* Reserved */
283 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
285 /* This should be here if PLUSPTYPE */
286 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
289 /* Custom Picture Format (CPFMT) */
290 aspect_to_info(s, s->avctx->sample_aspect_ratio);
292 put_bits(&s->pb,4,s->aspect_ratio_info);
293 put_bits(&s->pb,9,(s->width >> 2) - 1);
294 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
295 put_bits(&s->pb,9,(s->height >> 2));
296 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
297 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
298 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
303 put_bits(&s->pb, 1, best_clock_code);
304 put_bits(&s->pb, 7, best_divisor);
306 put_bits(&s->pb, 2, (temp_ref>>8)&3);
309 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
311 // put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
312 //FIXME check actual requested range
313 put_bits(&s->pb,2,1); /* unlimited */
314 if(s->h263_slice_structured)
315 put_bits(&s->pb,2,0); /* no weird submodes */
317 put_bits(&s->pb, 5, s->qscale);
320 put_bits(&s->pb, 1, 0); /* no PEI */
322 if(s->h263_slice_structured){
323 put_bits(&s->pb, 1, 1);
325 assert(s->mb_x == 0 && s->mb_y == 0);
326 ff_h263_encode_mba(s);
328 put_bits(&s->pb, 1, 1);
333 s->c_dc_scale_table= ff_aic_dc_scale_table;
336 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
341 * Encodes a group of blocks header.
343 void h263_encode_gob_header(MpegEncContext * s, int mb_line)
345 put_bits(&s->pb, 17, 1); /* GBSC */
347 if(s->h263_slice_structured){
348 put_bits(&s->pb, 1, 1);
350 ff_h263_encode_mba(s);
353 put_bits(&s->pb, 1, 1);
354 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
355 put_bits(&s->pb, 1, 1);
356 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
358 int gob_number= mb_line / s->gob_index;
360 put_bits(&s->pb, 5, gob_number); /* GN */
361 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
362 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
366 static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
371 for(j=1; j<=block_last_index; j++){
372 const int index= scantable[j];
373 int level= block[index];
376 if((level&(~127)) == 0){
377 if(j<block_last_index) rate+= s->intra_ac_vlc_length [UNI_AC_ENC_INDEX(j-last-1, level)];
378 else rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
380 rate += s->ac_esc_length;
390 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
394 int8_t * const qscale_table= s->current_picture.qscale_table;
396 memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
399 int16_t *ac_val, *ac_val1;
401 score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
403 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
406 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
408 ac_val-= s->block_wrap[n]*16;
409 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
412 const int level= block[n][s->dsp.idct_permutation[i ]];
413 block[n][s->dsp.idct_permutation[i ]] = level - ac_val[i+8];
414 ac_val1[i ]= block[n][s->dsp.idct_permutation[i<<3]];
418 /* different qscale, we must rescale */
420 const int level= block[n][s->dsp.idct_permutation[i ]];
421 block[n][s->dsp.idct_permutation[i ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
422 ac_val1[i ]= block[n][s->dsp.idct_permutation[i<<3]];
426 st[n]= s->intra_h_scantable.permutated;
428 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
429 /* left prediction */
431 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
434 const int level= block[n][s->dsp.idct_permutation[i<<3]];
435 block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
437 ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]];
440 /* different qscale, we must rescale */
442 const int level= block[n][s->dsp.idct_permutation[i<<3]];
443 block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
445 ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]];
448 st[n]= s->intra_v_scantable.permutated;
451 for(i=63; i>0; i--) //FIXME optimize
452 if(block[n][ st[n][i] ]) break;
453 s->block_last_index[n]= i;
455 score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
461 static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
464 memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);
467 int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
469 st[n]= s->intra_scantable.permutated;
473 block[n][s->dsp.idct_permutation[i ]] = ac_val[i+8];
476 /* left prediction */
478 block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i ];
485 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
487 void ff_clean_h263_qscales(MpegEncContext *s){
489 int8_t * const qscale_table= s->current_picture.qscale_table;
491 for(i=1; i<s->mb_num; i++){
492 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
493 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
495 for(i=s->mb_num-2; i>=0; i--){
496 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
497 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
502 * modify mb_type & qscale so that encoding is acually possible in mpeg4
504 void ff_clean_mpeg4_qscales(MpegEncContext *s){
506 int8_t * const qscale_table= s->current_picture.qscale_table;
508 ff_clean_h263_qscales(s);
510 for(i=1; i<s->mb_num; i++){
511 int mb_xy= s->mb_index2xy[i];
513 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
514 s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V;
515 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
519 if(s->pict_type== B_TYPE){
521 /* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than
522 for the actual adaptive quantization */
524 for(i=0; i<s->mb_num; i++){
525 int mb_xy= s->mb_index2xy[i];
526 odd += qscale_table[mb_xy]&1;
529 if(2*odd > s->mb_num) odd=1;
532 for(i=0; i<s->mb_num; i++){
533 int mb_xy= s->mb_index2xy[i];
534 if((qscale_table[mb_xy]&1) != odd)
535 qscale_table[mb_xy]++;
536 if(qscale_table[mb_xy] > 31)
537 qscale_table[mb_xy]= 31;
540 for(i=1; i<s->mb_num; i++){
541 int mb_xy= s->mb_index2xy[i];
542 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
543 s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT;
544 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
550 #endif //CONFIG_ENCODERS
553 * @return the mb_type
555 int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
556 const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
557 const int colocated_mb_type= s->next_picture.mb_type[mb_index];
558 int xy= s->block_index[0];
559 uint16_t time_pp= s->pp_time;
560 uint16_t time_pb= s->pb_time;
563 //FIXME avoid divides
565 if(IS_8X8(colocated_mb_type)){
566 s->mv_type = MV_TYPE_8X8;
568 xy= s->block_index[i];
569 s->mv[0][i][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
570 s->mv[0][i][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
571 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->next_picture.motion_val[0][xy][0]
572 : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
573 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->next_picture.motion_val[0][xy][1]
574 : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
576 return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
577 } else if(IS_INTERLACED(colocated_mb_type)){
578 s->mv_type = MV_TYPE_FIELD;
580 int field_select= s->next_picture.ref_index[0][s->block_index[2*i]];
581 if(s->top_field_first){
582 time_pp= s->pp_field_time - field_select + i;
583 time_pb= s->pb_field_time - field_select + i;
585 time_pp= s->pp_field_time + field_select - i;
586 time_pb= s->pb_field_time + field_select - i;
588 s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx;
589 s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my;
590 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0]
591 : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp;
592 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1]
593 : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp;
595 return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
597 s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
598 s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
599 s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->next_picture.motion_val[0][xy][0]
600 : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
601 s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->next_picture.motion_val[0][xy][1]
602 : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
603 if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
604 s->mv_type= MV_TYPE_16X16;
606 s->mv_type= MV_TYPE_8X8;
607 return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line
611 void ff_h263_update_motion_val(MpegEncContext * s){
612 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
613 //FIXME a lot of thet is only needed for !low_delay
614 const int wrap = s->b8_stride;
615 const int xy = s->block_index[0];
617 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
619 if(s->mv_type != MV_TYPE_8X8){
620 int motion_x, motion_y;
624 } else if (s->mv_type == MV_TYPE_16X16) {
625 motion_x = s->mv[0][0][0];
626 motion_y = s->mv[0][0][1];
627 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
629 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
630 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
631 motion_x = (motion_x>>1) | (motion_x&1);
633 s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
634 s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
636 s->current_picture.ref_index[0][xy ]=
637 s->current_picture.ref_index[0][xy + 1]= s->field_select[0][0];
638 s->current_picture.ref_index[0][xy + wrap ]=
639 s->current_picture.ref_index[0][xy + wrap + 1]= s->field_select[0][1];
642 /* no update if 8X8 because it has been done during parsing */
643 s->current_picture.motion_val[0][xy][0] = motion_x;
644 s->current_picture.motion_val[0][xy][1] = motion_y;
645 s->current_picture.motion_val[0][xy + 1][0] = motion_x;
646 s->current_picture.motion_val[0][xy + 1][1] = motion_y;
647 s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
648 s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
649 s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
650 s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
653 if(s->encoding){ //FIXME encoding MUST be cleaned up
654 if (s->mv_type == MV_TYPE_8X8)
655 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8;
657 s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA;
659 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
663 #ifdef CONFIG_ENCODERS
665 static inline int get_p_cbp(MpegEncContext * s,
666 DCTELEM block[6][64],
667 int motion_x, int motion_y){
670 if(s->flags & CODEC_FLAG_CBP_RD){
671 int best_cbpy_score= INT_MAX;
672 int best_cbpc_score= INT_MAX;
673 int cbpc = (-1), cbpy= (-1);
674 const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
675 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
678 int score= inter_MCBPC_bits[i + offset] * lambda;
679 if(i&1) score += s->coded_score[5];
680 if(i&2) score += s->coded_score[4];
682 if(score < best_cbpc_score){
683 best_cbpc_score= score;
689 int score= cbpy_tab[i ^ 0xF][1] * lambda;
690 if(i&1) score += s->coded_score[3];
691 if(i&2) score += s->coded_score[2];
692 if(i&4) score += s->coded_score[1];
693 if(i&8) score += s->coded_score[0];
695 if(score < best_cbpy_score){
696 best_cbpy_score= score;
701 if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){
702 if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)
706 for (i = 0; i < 6; i++) {
707 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
708 s->block_last_index[i]= -1;
709 memset(s->block[i], 0, sizeof(DCTELEM)*64);
714 for (i = 0; i < 6; i++) {
715 if (s->block_last_index[i] >= 0)
722 static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
723 int motion_x, int motion_y, int mb_type){
726 if(s->flags & CODEC_FLAG_CBP_RD){
728 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
731 if(s->coded_score[i] < 0){
732 score += s->coded_score[i];
739 if ((motion_x | motion_y | s->dquant | mb_type) == 0){
740 zero_score-= 4; //2*MV + mb_type + cbp bit
744 if(zero_score <= score){
749 for (i = 0; i < 6; i++) {
750 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
751 s->block_last_index[i]= -1;
752 memset(s->block[i], 0, sizeof(DCTELEM)*64);
756 for (i = 0; i < 6; i++) {
757 if (s->block_last_index[i] >= 0)
764 void mpeg4_encode_mb(MpegEncContext * s,
765 DCTELEM block[6][64],
766 int motion_x, int motion_y)
768 int cbpc, cbpy, pred_x, pred_y;
769 PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
770 PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
771 PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2 : &s->pb;
772 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
773 const int dquant_code[5]= {1,0,9,2,3};
775 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
779 if(s->pict_type==B_TYPE){
780 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
781 int mb_type= mb_type_table[s->mv_dir];
788 s->last_mv[i][1][1]= 0;
792 assert(s->dquant>=-2 && s->dquant<=2);
793 assert((s->dquant&1)==0);
796 /* nothing to do if this MB was skiped in the next P Frame */
797 if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ...
803 s->mv_dir= MV_DIR_FORWARD; //doesnt matter
804 s->qscale -= s->dquant;
810 cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
812 if ((cbp | motion_x | motion_y | mb_type) ==0) {
813 /* direct MB with MV={0,0} */
814 assert(s->dquant==0);
816 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
818 if(interleaved_stats){
826 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
827 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
828 put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
829 if(cbp) put_bits(&s->pb, 6, cbp);
833 put_bits(&s->pb, 2, (s->dquant>>2)+3);
835 put_bits(&s->pb, 1, 0);
837 s->qscale -= s->dquant;
839 if(!s->progressive_sequence){
841 put_bits(&s->pb, 1, s->interlaced_dct);
842 if(mb_type) // not diect mode
843 put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
846 if(interleaved_stats){
847 s->misc_bits+= get_bits_diff(s);
851 assert(s->mv_dir & MV_DIRECT);
852 h263_encode_motion(s, motion_x, 1);
853 h263_encode_motion(s, motion_y, 1);
857 assert(mb_type > 0 && mb_type < 4);
858 if(s->mv_type != MV_TYPE_FIELD){
859 if(s->mv_dir & MV_DIR_FORWARD){
860 h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
861 h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
862 s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0];
863 s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1];
866 if(s->mv_dir & MV_DIR_BACKWARD){
867 h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
868 h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
869 s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0];
870 s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1];
874 if(s->mv_dir & MV_DIR_FORWARD){
875 put_bits(&s->pb, 1, s->field_select[0][0]);
876 put_bits(&s->pb, 1, s->field_select[0][1]);
878 if(s->mv_dir & MV_DIR_BACKWARD){
879 put_bits(&s->pb, 1, s->field_select[1][0]);
880 put_bits(&s->pb, 1, s->field_select[1][1]);
882 if(s->mv_dir & MV_DIR_FORWARD){
884 h263_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
885 h263_encode_motion(s, s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code);
886 s->last_mv[0][i][0]= s->mv[0][i][0];
887 s->last_mv[0][i][1]= s->mv[0][i][1]*2;
891 if(s->mv_dir & MV_DIR_BACKWARD){
893 h263_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
894 h263_encode_motion(s, s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code);
895 s->last_mv[1][i][0]= s->mv[1][i][0];
896 s->last_mv[1][i][1]= s->mv[1][i][1]*2;
903 if(interleaved_stats){
904 s->mv_bits+= get_bits_diff(s);
907 /* encode each block */
908 for (i = 0; i < 6; i++) {
909 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, &s->pb);
912 if(interleaved_stats){
913 s->p_tex_bits+= get_bits_diff(s);
916 }else{ /* s->pict_type==B_TYPE */
917 cbp= get_p_cbp(s, block, motion_x, motion_y);
919 if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
920 /* check if the B frames can skip it too, as we must skip it if we skip here
921 why didnt they just compress the skip-mb bits instead of reusing them ?! */
922 if(s->max_b_frames>0){
929 if(x+16 > s->width) x= s->width-16;
930 if(y+16 > s->height) y= s->height-16;
932 offset= x + y*s->linesize;
933 p_pic= s->new_picture.data[0] + offset;
936 for(i=0; i<s->max_b_frames; i++){
939 Picture *pic= s->reordered_input_picture[i+1];
941 if(pic==NULL || pic->pict_type!=B_TYPE) break;
943 b_pic= pic->data[0] + offset + 16; //FIXME +16
944 diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
945 if(diff>s->qscale*70){ //FIXME check that 70 is optimal
954 /* skip macroblock */
955 put_bits(&s->pb, 1, 1);
957 if(interleaved_stats){
967 put_bits(&s->pb, 1, 0); /* mb coded */
971 if(s->mv_type==MV_TYPE_16X16){
972 if(s->dquant) cbpc+= 8;
974 inter_MCBPC_bits[cbpc],
975 inter_MCBPC_code[cbpc]);
977 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
979 put_bits(pb2, 2, dquant_code[s->dquant+2]);
981 if(!s->progressive_sequence){
983 put_bits(pb2, 1, s->interlaced_dct);
987 if(interleaved_stats){
988 s->misc_bits+= get_bits_diff(s);
991 /* motion vectors: 16x16 mode */
992 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
994 h263_encode_motion(s, motion_x - pred_x, s->f_code);
995 h263_encode_motion(s, motion_y - pred_y, s->f_code);
996 }else if(s->mv_type==MV_TYPE_FIELD){
997 if(s->dquant) cbpc+= 8;
999 inter_MCBPC_bits[cbpc],
1000 inter_MCBPC_code[cbpc]);
1002 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1004 put_bits(pb2, 2, dquant_code[s->dquant+2]);
1006 assert(!s->progressive_sequence);
1008 put_bits(pb2, 1, s->interlaced_dct);
1009 put_bits(pb2, 1, 1);
1011 if(interleaved_stats){
1012 s->misc_bits+= get_bits_diff(s);
1015 /* motion vectors: 16x8 interlaced mode */
1016 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1019 put_bits(&s->pb, 1, s->field_select[0][0]);
1020 put_bits(&s->pb, 1, s->field_select[0][1]);
1022 h263_encode_motion(s, s->mv[0][0][0] - pred_x, s->f_code);
1023 h263_encode_motion(s, s->mv[0][0][1] - pred_y, s->f_code);
1024 h263_encode_motion(s, s->mv[0][1][0] - pred_x, s->f_code);
1025 h263_encode_motion(s, s->mv[0][1][1] - pred_y, s->f_code);
1027 assert(s->mv_type==MV_TYPE_8X8);
1029 inter_MCBPC_bits[cbpc+16],
1030 inter_MCBPC_code[cbpc+16]);
1031 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1033 if(!s->progressive_sequence){
1035 put_bits(pb2, 1, s->interlaced_dct);
1038 if(interleaved_stats){
1039 s->misc_bits+= get_bits_diff(s);
1043 /* motion vectors: 8x8 mode*/
1044 h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1046 h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x, s->f_code);
1047 h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
1051 if(interleaved_stats){
1052 s->mv_bits+= get_bits_diff(s);
1055 /* encode each block */
1056 for (i = 0; i < 6; i++) {
1057 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, tex_pb);
1060 if(interleaved_stats){
1061 s->p_tex_bits+= get_bits_diff(s);
1067 int dc_diff[6]; //dc values with the dc prediction subtracted
1068 int dir[6]; //prediction direction
1069 int zigzag_last_index[6];
1070 uint8_t *scan_table[6];
1074 const int level= block[i][0];
1077 dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
1079 *dc_ptr = level * s->y_dc_scale;
1081 *dc_ptr = level * s->c_dc_scale;
1085 if(s->flags & CODEC_FLAG_AC_PRED){
1086 s->ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
1088 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
1091 scan_table[i]= s->intra_scantable.permutated;
1096 for (i = 0; i < 6; i++) {
1097 if (s->block_last_index[i] >= 1)
1098 cbp |= 1 << (5 - i);
1102 if (s->pict_type == I_TYPE) {
1103 if(s->dquant) cbpc+=4;
1105 intra_MCBPC_bits[cbpc],
1106 intra_MCBPC_code[cbpc]);
1108 if(s->dquant) cbpc+=8;
1109 put_bits(&s->pb, 1, 0); /* mb coded */
1111 inter_MCBPC_bits[cbpc + 4],
1112 inter_MCBPC_code[cbpc + 4]);
1114 put_bits(pb2, 1, s->ac_pred);
1116 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1118 put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
1120 if(!s->progressive_sequence){
1121 put_bits(dc_pb, 1, s->interlaced_dct);
1124 if(interleaved_stats){
1125 s->misc_bits+= get_bits_diff(s);
1128 /* encode each block */
1129 for (i = 0; i < 6; i++) {
1130 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
1133 if(interleaved_stats){
1134 s->i_tex_bits+= get_bits_diff(s);
1138 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
1140 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
1144 void h263_encode_mb(MpegEncContext * s,
1145 DCTELEM block[6][64],
1146 int motion_x, int motion_y)
1148 int cbpc, cbpy, i, cbp, pred_x, pred_y;
1150 int16_t rec_intradc[6];
1151 uint16_t *dc_ptr[6];
1152 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
1153 const int dquant_code[5]= {1,0,9,2,3};
1155 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
1158 cbp= get_p_cbp(s, block, motion_x, motion_y);
1160 if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
1161 /* skip macroblock */
1162 put_bits(&s->pb, 1, 1);
1163 if(interleaved_stats){
1171 put_bits(&s->pb, 1, 0); /* mb coded */
1175 if(s->alt_inter_vlc==0 || cbpc!=3)
1177 if(s->dquant) cbpc+= 8;
1178 if(s->mv_type==MV_TYPE_16X16){
1180 inter_MCBPC_bits[cbpc],
1181 inter_MCBPC_code[cbpc]);
1183 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1185 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1187 if(interleaved_stats){
1188 s->misc_bits+= get_bits_diff(s);
1191 /* motion vectors: 16x16 mode */
1192 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1195 h263_encode_motion(s, motion_x - pred_x, 1);
1196 h263_encode_motion(s, motion_y - pred_y, 1);
1199 h263p_encode_umotion(s, motion_x - pred_x);
1200 h263p_encode_umotion(s, motion_y - pred_y);
1201 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1202 /* To prevent Start Code emulation */
1203 put_bits(&s->pb,1,1);
1207 inter_MCBPC_bits[cbpc+16],
1208 inter_MCBPC_code[cbpc+16]);
1209 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1211 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1213 if(interleaved_stats){
1214 s->misc_bits+= get_bits_diff(s);
1218 /* motion vectors: 8x8 mode*/
1219 h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1221 motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
1222 motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
1224 h263_encode_motion(s, motion_x - pred_x, 1);
1225 h263_encode_motion(s, motion_y - pred_y, 1);
1228 h263p_encode_umotion(s, motion_x - pred_x);
1229 h263p_encode_umotion(s, motion_y - pred_y);
1230 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1231 /* To prevent Start Code emulation */
1232 put_bits(&s->pb,1,1);
1237 if(interleaved_stats){
1238 s->mv_bits+= get_bits_diff(s);
1241 assert(s->mb_intra);
1246 for(i=0; i<6; i++) {
1247 int16_t level = block[i][0];
1250 if(i<4) scale= s->y_dc_scale;
1251 else scale= s->c_dc_scale;
1253 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
1257 level = (level + (scale>>1))/scale;
1259 level = (level - (scale>>1))/scale;
1261 /* AIC can change CBP */
1262 if (level == 0 && s->block_last_index[i] == 0)
1263 s->block_last_index[i] = -1;
1265 if(!s->modified_quant){
1268 else if (level > 127)
1272 block[i][0] = level;
1273 /* Reconstruction */
1274 rec_intradc[i] = scale*level + pred_dc;
1276 rec_intradc[i] |= 1;
1277 //if ((rec_intradc[i] % 2) == 0)
1278 // rec_intradc[i]++;
1280 if (rec_intradc[i] < 0)
1282 else if (rec_intradc[i] > 2047)
1283 rec_intradc[i] = 2047;
1285 /* Update AC/DC tables */
1286 *dc_ptr[i] = rec_intradc[i];
1287 if (s->block_last_index[i] >= 0)
1288 cbp |= 1 << (5 - i);
1291 for(i=0; i<6; i++) {
1293 if (s->block_last_index[i] >= 1)
1294 cbp |= 1 << (5 - i);
1299 if (s->pict_type == I_TYPE) {
1300 if(s->dquant) cbpc+=4;
1302 intra_MCBPC_bits[cbpc],
1303 intra_MCBPC_code[cbpc]);
1305 if(s->dquant) cbpc+=8;
1306 put_bits(&s->pb, 1, 0); /* mb coded */
1308 inter_MCBPC_bits[cbpc + 4],
1309 inter_MCBPC_code[cbpc + 4]);
1312 /* XXX: currently, we do not try to use ac prediction */
1313 put_bits(&s->pb, 1, 0); /* no AC prediction */
1316 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1318 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1320 if(interleaved_stats){
1321 s->misc_bits+= get_bits_diff(s);
1325 for(i=0; i<6; i++) {
1326 /* encode each block */
1327 h263_encode_block(s, block[i], i);
1329 /* Update INTRADC for decoding */
1330 if (s->h263_aic && s->mb_intra) {
1331 block[i][0] = rec_intradc[i];
1336 if(interleaved_stats){
1338 s->p_tex_bits+= get_bits_diff(s);
1341 s->i_tex_bits+= get_bits_diff(s);
1348 void ff_h263_loop_filter(MpegEncContext * s){
1350 const int linesize = s->linesize;
1351 const int uvlinesize= s->uvlinesize;
1352 const int xy = s->mb_y * s->mb_stride + s->mb_x;
1353 uint8_t *dest_y = s->dest[0];
1354 uint8_t *dest_cb= s->dest[1];
1355 uint8_t *dest_cr= s->dest[2];
1357 // if(s->pict_type==B_TYPE && !s->readable) return;
1363 if(!IS_SKIP(s->current_picture.mb_type[xy])){
1365 s->dsp.h263_v_loop_filter(dest_y+8*linesize , linesize, qp_c);
1366 s->dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
1371 int qp_dt, qp_t, qp_tc;
1373 if(IS_SKIP(s->current_picture.mb_type[xy-s->mb_stride]))
1376 qp_t= s->current_picture.qscale_table[xy-s->mb_stride];
1384 const int chroma_qp= s->chroma_qscale_table[qp_tc];
1385 s->dsp.h263_v_loop_filter(dest_y , linesize, qp_tc);
1386 s->dsp.h263_v_loop_filter(dest_y+8, linesize, qp_tc);
1388 s->dsp.h263_v_loop_filter(dest_cb , uvlinesize, chroma_qp);
1389 s->dsp.h263_v_loop_filter(dest_cr , uvlinesize, chroma_qp);
1393 s->dsp.h263_h_loop_filter(dest_y-8*linesize+8 , linesize, qp_t);
1396 if(qp_t || IS_SKIP(s->current_picture.mb_type[xy-1-s->mb_stride]))
1399 qp_dt= s->current_picture.qscale_table[xy-1-s->mb_stride];
1402 const int chroma_qp= s->chroma_qscale_table[qp_dt];
1403 s->dsp.h263_h_loop_filter(dest_y -8*linesize , linesize, qp_dt);
1404 s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
1405 s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
1411 s->dsp.h263_h_loop_filter(dest_y +8, linesize, qp_c);
1412 if(s->mb_y + 1 == s->mb_height)
1413 s->dsp.h263_h_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
1418 if(qp_c || IS_SKIP(s->current_picture.mb_type[xy-1]))
1421 qp_lc= s->current_picture.qscale_table[xy-1];
1424 s->dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
1425 if(s->mb_y + 1 == s->mb_height){
1426 const int chroma_qp= s->chroma_qscale_table[qp_lc];
1427 s->dsp.h263_h_loop_filter(dest_y +8* linesize, linesize, qp_lc);
1428 s->dsp.h263_h_loop_filter(dest_cb , uvlinesize, chroma_qp);
1429 s->dsp.h263_h_loop_filter(dest_cr , uvlinesize, chroma_qp);
1435 static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr)
1437 int x, y, wrap, a, c, pred_dc, scale;
1438 int16_t *dc_val, *ac_val;
1440 /* find prediction */
1442 x = 2 * s->mb_x + (n & 1);
1443 y = 2 * s->mb_y + ((n & 2) >> 1);
1444 wrap = s->b8_stride;
1445 dc_val = s->dc_val[0];
1446 ac_val = s->ac_val[0][0];
1447 scale = s->y_dc_scale;
1451 wrap = s->mb_stride;
1452 dc_val = s->dc_val[n - 4 + 1];
1453 ac_val = s->ac_val[n - 4 + 1][0];
1454 scale = s->c_dc_scale;
1459 a = dc_val[(x - 1) + (y) * wrap];
1460 c = dc_val[(x) + (y - 1) * wrap];
1462 /* No prediction outside GOB boundary */
1463 if(s->first_slice_line && n!=3){
1465 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
1468 /* just DC prediction */
1469 if (a != 1024 && c != 1024)
1470 pred_dc = (a + c) >> 1;
1476 /* we assume pred is positive */
1477 //pred_dc = (pred_dc + (scale >> 1)) / scale;
1478 *dc_val_ptr = &dc_val[x + y * wrap];
1482 static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
1484 int x, y, wrap, a, c, pred_dc, scale, i;
1485 int16_t *dc_val, *ac_val, *ac_val1;
1487 /* find prediction */
1489 x = 2 * s->mb_x + (n & 1);
1490 y = 2 * s->mb_y + (n>> 1);
1491 wrap = s->b8_stride;
1492 dc_val = s->dc_val[0];
1493 ac_val = s->ac_val[0][0];
1494 scale = s->y_dc_scale;
1498 wrap = s->mb_stride;
1499 dc_val = s->dc_val[n - 4 + 1];
1500 ac_val = s->ac_val[n - 4 + 1][0];
1501 scale = s->c_dc_scale;
1504 ac_val += ((y) * wrap + (x)) * 16;
1510 a = dc_val[(x - 1) + (y) * wrap];
1511 c = dc_val[(x) + (y - 1) * wrap];
1513 /* No prediction outside GOB boundary */
1514 if(s->first_slice_line && n!=3){
1516 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
1521 if (s->h263_aic_dir) {
1522 /* left prediction */
1526 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
1531 /* top prediction */
1533 ac_val -= 16 * wrap;
1535 block[s->dsp.idct_permutation[i ]] += ac_val[i + 8];
1541 /* just DC prediction */
1542 if (a != 1024 && c != 1024)
1543 pred_dc = (a + c) >> 1;
1550 /* we assume pred is positive */
1551 block[0]=block[0]*scale + pred_dc;
1558 /* Update AC/DC tables */
1559 dc_val[(x) + (y) * wrap] = block[0];
1563 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
1566 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
1569 int16_t *h263_pred_motion(MpegEncContext * s, int block, int dir,
1573 int16_t *A, *B, *C, (*mot_val)[2];
1574 static const int off[4]= {2, 1, 1, -1};
1576 wrap = s->b8_stride;
1577 mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
1580 /* special case for first (slice) line */
1581 if (s->first_slice_line && block<3) {
1582 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
1583 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
1584 if(block==0){ //most common case
1585 if(s->mb_x == s->resync_mb_x){ //rare
1587 }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1588 C = mot_val[off[block] - wrap];
1593 *px = mid_pred(A[0], 0, C[0]);
1594 *py = mid_pred(A[1], 0, C[1]);
1601 if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1602 C = mot_val[off[block] - wrap];
1603 *px = mid_pred(A[0], 0, C[0]);
1604 *py = mid_pred(A[1], 0, C[1]);
1609 }else{ /* block==2*/
1610 B = mot_val[ - wrap];
1611 C = mot_val[off[block] - wrap];
1612 if(s->mb_x == s->resync_mb_x) //rare
1615 *px = mid_pred(A[0], B[0], C[0]);
1616 *py = mid_pred(A[1], B[1], C[1]);
1619 B = mot_val[ - wrap];
1620 C = mot_val[off[block] - wrap];
1621 *px = mid_pred(A[0], B[0], C[0]);
1622 *py = mid_pred(A[1], B[1], C[1]);
1627 #ifdef CONFIG_ENCODERS
1628 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
1630 int range, l, bit_size, sign, code, bits;
1635 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1637 bit_size = f_code - 1;
1638 range = 1 << bit_size;
1639 /* modulo encoding */
1646 val= (val^sign)-sign;
1651 } else if (val >= l) {
1655 assert(val>=-l && val<l);
1665 code = (val >> bit_size) + 1;
1666 bits = val & (range - 1);
1668 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1670 put_bits(&s->pb, bit_size, bits);
1676 /* Encode MV differences on H.263+ with Unrestricted MV mode */
1677 static void h263p_encode_umotion(MpegEncContext * s, int val)
1687 put_bits(&s->pb, 1, 1);
1689 put_bits(&s->pb, 3, 0);
1691 put_bits(&s->pb, 3, 2);
1694 sval = ((val < 0) ? (short)(-val):(short)val);
1697 while (temp_val != 0) {
1698 temp_val = temp_val >> 1;
1704 tcode = (sval & (1 << (i-1))) >> (i-1);
1705 tcode = (tcode << 1) | 1;
1706 code = (code << 2) | tcode;
1709 code = ((code << 1) | (val < 0)) << 1;
1710 put_bits(&s->pb, (2*n_bits)+1, code);
1711 //printf("\nVal = %d\tCode = %d", sval, code);
1715 static void init_mv_penalty_and_fcode(MpegEncContext *s)
1720 if(mv_penalty==NULL)
1721 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
1723 for(f_code=1; f_code<=MAX_FCODE; f_code++){
1724 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1727 if(mv==0) len= mvtab[0][1];
1729 int val, bit_size, range, code;
1731 bit_size = f_code - 1;
1732 range = 1 << bit_size;
1738 code = (val >> bit_size) + 1;
1740 len= mvtab[code][1] + 1 + bit_size;
1742 len= mvtab[32][1] + 2 + bit_size;
1746 mv_penalty[f_code][mv+MAX_MV]= len;
1750 for(f_code=MAX_FCODE; f_code>0; f_code--){
1751 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1752 fcode_tab[mv+MAX_MV]= f_code;
1756 for(mv=0; mv<MAX_MV*2+1; mv++){
1757 umv_fcode_tab[mv]= 1;
1762 #ifdef CONFIG_ENCODERS
1764 static void init_uni_dc_tab(void)
1766 int level, uni_code, uni_len;
1768 for(level=-256; level<256; level++){
1770 /* find number of bits */
1779 l= (-level) ^ ((1 << size) - 1);
1784 uni_code= DCtab_lum[size][0];
1785 uni_len = DCtab_lum[size][1];
1788 uni_code<<=size; uni_code|=l;
1791 uni_code<<=1; uni_code|=1;
1795 uni_DCtab_lum_bits[level+256]= uni_code;
1796 uni_DCtab_lum_len [level+256]= uni_len;
1799 uni_code= DCtab_chrom[size][0];
1800 uni_len = DCtab_chrom[size][1];
1803 uni_code<<=size; uni_code|=l;
1806 uni_code<<=1; uni_code|=1;
1810 uni_DCtab_chrom_bits[level+256]= uni_code;
1811 uni_DCtab_chrom_len [level+256]= uni_len;
1816 #endif //CONFIG_ENCODERS
1818 #ifdef CONFIG_ENCODERS
1819 static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
1820 int slevel, run, last;
1822 assert(MAX_LEVEL >= 64);
1823 assert(MAX_RUN >= 63);
1825 for(slevel=-64; slevel<64; slevel++){
1826 if(slevel==0) continue;
1827 for(run=0; run<64; run++){
1828 for(last=0; last<=1; last++){
1829 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1830 int level= slevel < 0 ? -slevel : slevel;
1831 int sign= slevel < 0 ? 1 : 0;
1832 int bits, len, code;
1835 len_tab[index]= 100;
1838 code= get_rl_index(rl, last, run, level);
1839 bits= rl->table_vlc[code][0];
1840 len= rl->table_vlc[code][1];
1841 bits=bits*2+sign; len++;
1843 if(code!=rl->n && len < len_tab[index]){
1844 bits_tab[index]= bits;
1845 len_tab [index]= len;
1849 bits= rl->table_vlc[rl->n][0];
1850 len= rl->table_vlc[rl->n][1];
1851 bits=bits*2; len++; //esc1
1852 level1= level - rl->max_level[last][run];
1854 code= get_rl_index(rl, last, run, level1);
1855 bits<<= rl->table_vlc[code][1];
1856 len += rl->table_vlc[code][1];
1857 bits += rl->table_vlc[code][0];
1858 bits=bits*2+sign; len++;
1860 if(code!=rl->n && len < len_tab[index]){
1861 bits_tab[index]= bits;
1862 len_tab [index]= len;
1868 bits= rl->table_vlc[rl->n][0];
1869 len= rl->table_vlc[rl->n][1];
1870 bits=bits*4+2; len+=2; //esc2
1871 run1 = run - rl->max_run[last][level] - 1;
1873 code= get_rl_index(rl, last, run1, level);
1874 bits<<= rl->table_vlc[code][1];
1875 len += rl->table_vlc[code][1];
1876 bits += rl->table_vlc[code][0];
1877 bits=bits*2+sign; len++;
1879 if(code!=rl->n && len < len_tab[index]){
1880 bits_tab[index]= bits;
1881 len_tab [index]= len;
1886 bits= rl->table_vlc[rl->n][0];
1887 len = rl->table_vlc[rl->n][1];
1888 bits=bits*4+3; len+=2; //esc3
1889 bits=bits*2+last; len++;
1890 bits=bits*64+run; len+=6;
1891 bits=bits*2+1; len++; //marker
1892 bits=bits*4096+(slevel&0xfff); len+=12;
1893 bits=bits*2+1; len++; //marker
1895 if(len < len_tab[index]){
1896 bits_tab[index]= bits;
1897 len_tab [index]= len;
1904 void h263_encode_init(MpegEncContext *s)
1906 static int done = 0;
1915 init_rl(&rl_intra_aic);
1917 init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1918 init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1920 init_mv_penalty_and_fcode(s);
1922 s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1924 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
1925 switch(s->codec_id){
1926 case CODEC_ID_MPEG4:
1927 s->fcode_tab= fcode_tab;
1928 s->min_qcoeff= -2048;
1929 s->max_qcoeff= 2047;
1930 s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
1931 s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
1932 s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
1933 s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
1934 s->luma_dc_vlc_length= uni_DCtab_lum_len;
1935 s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
1936 s->ac_esc_length= 7+2+1+6+1+12+1;
1937 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
1938 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1940 if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
1942 s->avctx->extradata= av_malloc(1024);
1943 init_put_bits(&s->pb, s->avctx->extradata, 1024);
1945 mpeg4_encode_visual_object_header(s);
1946 mpeg4_encode_vol_header(s, 0, 0);
1948 // ff_mpeg4_stuffing(&s->pb); ?
1949 flush_put_bits(&s->pb);
1950 s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
1954 case CODEC_ID_H263P:
1956 s->fcode_tab= umv_fcode_tab;
1957 if(s->modified_quant){
1958 s->min_qcoeff= -2047;
1959 s->max_qcoeff= 2047;
1961 s->min_qcoeff= -127;
1965 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
1967 if (s->h263_flv > 1) {
1968 s->min_qcoeff= -1023;
1969 s->max_qcoeff= 1023;
1971 s->min_qcoeff= -127;
1974 s->y_dc_scale_table=
1975 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1977 default: //nothing needed default table allready set in mpegvideo.c
1978 s->min_qcoeff= -127;
1980 s->y_dc_scale_table=
1981 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1986 * encodes a 8x8 block.
1987 * @param block the 8x8 block
1988 * @param n block index (0-3 are luma, 4-5 are chroma)
1990 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1992 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1996 if (s->mb_intra && !s->h263_aic) {
1999 /* 255 cannot be represented, so we clamp */
2004 /* 0 cannot be represented also */
2005 else if (level < 1) {
2009 if (level == 128) //FIXME check rv10
2010 put_bits(&s->pb, 8, 0xff);
2012 put_bits(&s->pb, 8, level);
2016 if (s->h263_aic && s->mb_intra)
2019 if(s->alt_inter_vlc && !s->mb_intra){
2021 int inter_vlc_bits=0;
2025 last_index = s->block_last_index[n];
2026 last_non_zero = i - 1;
2027 for (; i <= last_index; i++) {
2028 j = s->intra_scantable.permutated[i];
2031 run = i - last_non_zero - 1;
2032 last = (i == last_index);
2034 if(level<0) level= -level;
2036 code = get_rl_index(rl, last, run, level);
2037 aic_code = get_rl_index(&rl_intra_aic, last, run, level);
2038 inter_vlc_bits += rl->table_vlc[code][1]+1;
2039 aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1;
2041 if (code == rl->n) {
2042 inter_vlc_bits += 1+6+8-1;
2044 if (aic_code == rl_intra_aic.n) {
2045 aic_vlc_bits += 1+6+8-1;
2046 wrong_pos += run + 1;
2048 wrong_pos += wrong_run[aic_code];
2053 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
2059 last_index = s->block_last_index[n];
2060 last_non_zero = i - 1;
2061 for (; i <= last_index; i++) {
2062 j = s->intra_scantable.permutated[i];
2065 run = i - last_non_zero - 1;
2066 last = (i == last_index);
2073 code = get_rl_index(rl, last, run, level);
2074 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2075 if (code == rl->n) {
2076 if(s->h263_flv <= 1){
2077 put_bits(&s->pb, 1, last);
2078 put_bits(&s->pb, 6, run);
2080 assert(slevel != 0);
2083 put_bits(&s->pb, 8, slevel & 0xff);
2085 put_bits(&s->pb, 8, 128);
2086 put_bits(&s->pb, 5, slevel & 0x1f);
2087 put_bits(&s->pb, 6, (slevel>>5)&0x3f);
2090 if(level < 64) { // 7-bit level
2091 put_bits(&s->pb, 1, 0);
2092 put_bits(&s->pb, 1, last);
2093 put_bits(&s->pb, 6, run);
2095 put_bits(&s->pb, 7, slevel & 0x7f);
2098 put_bits(&s->pb, 1, 1);
2099 put_bits(&s->pb, 1, last);
2100 put_bits(&s->pb, 6, run);
2102 put_bits(&s->pb, 11, slevel & 0x7ff);
2106 put_bits(&s->pb, 1, sign);
2114 #ifdef CONFIG_ENCODERS
2116 /***************************************************/
2118 * add mpeg4 stuffing bits (01...1)
2120 void ff_mpeg4_stuffing(PutBitContext * pbc)
2123 put_bits(pbc, 1, 0);
2124 length= (-put_bits_count(pbc))&7;
2125 if(length) put_bits(pbc, length, (1<<length)-1);
2128 /* must be called before writing the header */
2129 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
2130 int time_div, time_mod;
2132 assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
2133 s->time= (s->current_picture_ptr->pts*s->time_increment_resolution + AV_TIME_BASE/2)/AV_TIME_BASE;
2135 time_div= s->time/s->time_increment_resolution;
2136 time_mod= s->time%s->time_increment_resolution;
2138 if(s->pict_type==B_TYPE){
2139 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2141 s->last_time_base= s->time_base;
2142 s->time_base= time_div;
2143 s->pp_time= s->time - s->last_non_b_time;
2144 s->last_non_b_time= s->time;
2148 static void mpeg4_encode_gop_header(MpegEncContext * s){
2149 int hours, minutes, seconds;
2152 put_bits(&s->pb, 16, 0);
2153 put_bits(&s->pb, 16, GOP_STARTCODE);
2155 time= s->current_picture_ptr->pts;
2156 if(s->reordered_input_picture[1])
2157 time= FFMIN(time, s->reordered_input_picture[1]->pts);
2158 time= (time*s->time_increment_resolution + AV_TIME_BASE/2)/AV_TIME_BASE;
2160 seconds= time/s->time_increment_resolution;
2161 minutes= seconds/60; seconds %= 60;
2162 hours= minutes/60; minutes %= 60;
2165 put_bits(&s->pb, 5, hours);
2166 put_bits(&s->pb, 6, minutes);
2167 put_bits(&s->pb, 1, 1);
2168 put_bits(&s->pb, 6, seconds);
2170 put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
2171 put_bits(&s->pb, 1, 0); //broken link == NO
2173 s->last_time_base= time / s->time_increment_resolution;
2175 ff_mpeg4_stuffing(&s->pb);
2178 static void mpeg4_encode_visual_object_header(MpegEncContext * s){
2179 int profile_and_level_indication;
2182 if(s->max_b_frames || s->quarter_sample){
2183 profile_and_level_indication= 0xF1; // adv simple level 1
2186 profile_and_level_indication= 0x01; // simple level 1
2191 put_bits(&s->pb, 16, 0);
2192 put_bits(&s->pb, 16, VOS_STARTCODE);
2194 put_bits(&s->pb, 8, profile_and_level_indication);
2196 put_bits(&s->pb, 16, 0);
2197 put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
2199 put_bits(&s->pb, 1, 1);
2200 put_bits(&s->pb, 4, vo_ver_id);
2201 put_bits(&s->pb, 3, 1); //priority
2203 put_bits(&s->pb, 4, 1); //visual obj type== video obj
2205 put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
2207 ff_mpeg4_stuffing(&s->pb);
2210 static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
2214 if(s->max_b_frames || s->quarter_sample){
2216 s->vo_type= ADV_SIMPLE_VO_TYPE;
2219 s->vo_type= SIMPLE_VO_TYPE;
2222 put_bits(&s->pb, 16, 0);
2223 put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
2224 put_bits(&s->pb, 16, 0);
2225 put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
2227 put_bits(&s->pb, 1, 0); /* random access vol */
2228 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
2229 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
2230 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
2231 put_bits(&s->pb, 3, 1); /* is obj layer priority */
2233 aspect_to_info(s, s->avctx->sample_aspect_ratio);
2235 put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
2236 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
2237 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
2238 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
2242 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
2243 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
2244 put_bits(&s->pb, 1, s->low_delay);
2245 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
2247 put_bits(&s->pb, 1, 0); /* vol control parameters= no */
2250 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
2251 put_bits(&s->pb, 1, 1); /* marker bit */
2253 put_bits(&s->pb, 16, s->time_increment_resolution);
2254 if (s->time_increment_bits < 1)
2255 s->time_increment_bits = 1;
2256 put_bits(&s->pb, 1, 1); /* marker bit */
2257 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
2258 put_bits(&s->pb, 1, 1); /* marker bit */
2259 put_bits(&s->pb, 13, s->width); /* vol width */
2260 put_bits(&s->pb, 1, 1); /* marker bit */
2261 put_bits(&s->pb, 13, s->height); /* vol height */
2262 put_bits(&s->pb, 1, 1); /* marker bit */
2263 put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
2264 put_bits(&s->pb, 1, 1); /* obmc disable */
2265 if (vo_ver_id == 1) {
2266 put_bits(&s->pb, 1, s->vol_sprite_usage); /* sprite enable */
2268 put_bits(&s->pb, 2, s->vol_sprite_usage); /* sprite enable */
2271 put_bits(&s->pb, 1, 0); /* not 8 bit == false */
2272 put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
2275 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
2276 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
2280 put_bits(&s->pb, 1, s->quarter_sample);
2281 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
2282 s->resync_marker= s->rtp_mode;
2283 put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
2284 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
2285 if(s->data_partitioning){
2286 put_bits(&s->pb, 1, 0); /* no rvlc */
2289 if (vo_ver_id != 1){
2290 put_bits(&s->pb, 1, 0); /* newpred */
2291 put_bits(&s->pb, 1, 0); /* reduced res vop */
2293 put_bits(&s->pb, 1, 0); /* scalability */
2295 ff_mpeg4_stuffing(&s->pb);
2298 if(!(s->flags & CODEC_FLAG_BITEXACT)){
2299 put_bits(&s->pb, 16, 0);
2300 put_bits(&s->pb, 16, 0x1B2); /* user_data */
2301 put_string(&s->pb, LIBAVCODEC_IDENT, 0);
2305 /* write mpeg4 VOP header */
2306 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
2309 int time_div, time_mod;
2311 if(s->pict_type==I_TYPE){
2312 if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
2313 if(s->strict_std_compliance < 2) //HACK, the reference sw is buggy
2314 mpeg4_encode_visual_object_header(s);
2315 if(s->strict_std_compliance < 2 || picture_number==0) //HACK, the reference sw is buggy
2316 mpeg4_encode_vol_header(s, 0, 0);
2318 mpeg4_encode_gop_header(s);
2321 s->partitioned_frame= s->data_partitioning && s->pict_type!=B_TYPE;
2323 //printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
2325 put_bits(&s->pb, 16, 0); /* vop header */
2326 put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
2327 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
2329 time_div= s->time/s->time_increment_resolution;
2330 time_mod= s->time%s->time_increment_resolution;
2331 time_incr= time_div - s->last_time_base;
2332 assert(time_incr >= 0);
2334 put_bits(&s->pb, 1, 1);
2336 put_bits(&s->pb, 1, 0);
2338 put_bits(&s->pb, 1, 1); /* marker */
2339 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
2340 put_bits(&s->pb, 1, 1); /* marker */
2341 put_bits(&s->pb, 1, 1); /* vop coded */
2342 if ( s->pict_type == P_TYPE
2343 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
2344 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
2346 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
2347 if(!s->progressive_sequence){
2348 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
2349 put_bits(&s->pb, 1, s->alternate_scan);
2351 //FIXME sprite stuff
2353 put_bits(&s->pb, 5, s->qscale);
2355 if (s->pict_type != I_TYPE)
2356 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
2357 if (s->pict_type == B_TYPE)
2358 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
2359 // printf("****frame %d\n", picture_number);
2362 #endif //CONFIG_ENCODERS
2365 * set qscale and update qscale dependant variables.
2367 void ff_set_qscale(MpegEncContext * s, int qscale)
2371 else if (qscale > 31)
2375 s->chroma_qscale= s->chroma_qscale_table[qscale];
2377 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
2378 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
2383 * @param n block index (0-3 are luma, 4-5 are chroma)
2384 * @param dc_val_ptr a pointer to the dc_val entry for the current MB will be stored here
2385 * @param dir_ptr pointer to an integer where the prediction direction will be stored
2386 * @return the quantized predicted dc
2388 static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr)
2390 int a, b, c, wrap, pred, scale;
2393 /* find prediction */
2395 scale = s->y_dc_scale;
2397 scale = s->c_dc_scale;
2402 wrap= s->block_wrap[n];
2403 dc_val = s->dc_val[0] + s->block_index[n];
2409 b = dc_val[ - 1 - wrap];
2410 c = dc_val[ - wrap];
2412 /* outside slice handling (we cant do that by memset as we need the dc for error resilience) */
2413 if(s->first_slice_line && n!=3){
2415 if(n!=1 && s->mb_x == s->resync_mb_x) b=a= 1024;
2417 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1){
2418 if(n==0 || n==4 || n==5)
2422 if (abs(a - b) < abs(b - c)) {
2424 *dir_ptr = 1; /* top */
2427 *dir_ptr = 0; /* left */
2429 /* we assume pred is positive */
2430 pred = FASTDIV((pred + (scale >> 1)), scale);
2432 /* prepare address for prediction update */
2433 *dc_val_ptr = &dc_val[0];
2440 * @param n block index (0-3 are luma, 4-5 are chroma)
2441 * @param dir the ac prediction direction
2443 void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
2447 int16_t *ac_val, *ac_val1;
2448 int8_t * const qscale_table= s->current_picture.qscale_table;
2450 /* find prediction */
2451 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2455 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
2456 /* left prediction */
2459 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
2462 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
2465 /* different qscale, we must rescale */
2467 block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
2471 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
2472 /* top prediction */
2473 ac_val -= 16 * s->block_wrap[n];
2475 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
2478 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
2481 /* different qscale, we must rescale */
2483 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
2490 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
2494 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
2498 #ifdef CONFIG_ENCODERS
2501 * encodes the dc value.
2502 * @param n block index (0-3 are luma, 4-5 are chroma)
2504 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
2507 // if(level<-255 || level>255) printf("dc overflow\n");
2511 put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
2514 put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
2518 /* find number of bits */
2528 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
2531 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
2534 /* encode remaining bits */
2537 level = (-level) ^ ((1 << size) - 1);
2538 put_bits(&s->pb, size, level);
2540 put_bits(&s->pb, 1, 1);
2546 * encodes a 8x8 block
2547 * @param n block index (0-3 are luma, 4-5 are chroma)
2549 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
2550 uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
2552 int i, last_non_zero;
2553 #if 0 //variables for the outcommented version
2554 int code, sign, last;
2559 const int last_index = s->block_last_index[n];
2561 if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
2562 /* mpeg4 based DC predictor */
2563 mpeg4_encode_dc(dc_pb, intra_dc, n);
2564 if(last_index<1) return;
2567 bits_tab= uni_mpeg4_intra_rl_bits;
2568 len_tab = uni_mpeg4_intra_rl_len;
2570 if(last_index<0) return;
2573 bits_tab= uni_mpeg4_inter_rl_bits;
2574 len_tab = uni_mpeg4_inter_rl_len;
2578 last_non_zero = i - 1;
2580 for (; i < last_index; i++) {
2581 int level = block[ scan_table[i] ];
2583 int run = i - last_non_zero - 1;
2585 if((level&(~127)) == 0){
2586 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2587 put_bits(ac_pb, len_tab[index], bits_tab[index]);
2589 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
2594 /*if(i<=last_index)*/{
2595 int level = block[ scan_table[i] ];
2596 int run = i - last_non_zero - 1;
2598 if((level&(~127)) == 0){
2599 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2600 put_bits(ac_pb, len_tab[index], bits_tab[index]);
2602 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
2606 for (; i <= last_index; i++) {
2607 const int slevel = block[ scan_table[i] ];
2610 int run = i - last_non_zero - 1;
2611 last = (i == last_index);
2618 code = get_rl_index(rl, last, run, level);
2619 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2620 if (code == rl->n) {
2622 level1 = level - rl->max_level[last][run];
2625 code = get_rl_index(rl, last, run, level1);
2626 if (code == rl->n) {
2628 put_bits(ac_pb, 1, 1);
2629 if (level > MAX_LEVEL)
2631 run1 = run - rl->max_run[last][level] - 1;
2634 code = get_rl_index(rl, last, run1, level);
2635 if (code == rl->n) {
2638 put_bits(ac_pb, 1, 1);
2639 put_bits(ac_pb, 1, last);
2640 put_bits(ac_pb, 6, run);
2641 put_bits(ac_pb, 1, 1);
2642 put_bits(ac_pb, 12, slevel & 0xfff);
2643 put_bits(ac_pb, 1, 1);
2646 put_bits(ac_pb, 1, 0);
2647 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2648 put_bits(ac_pb, 1, sign);
2652 put_bits(ac_pb, 1, 0);
2653 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2654 put_bits(ac_pb, 1, sign);
2657 put_bits(ac_pb, 1, sign);
2665 static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
2666 uint8_t *scan_table)
2668 int i, last_non_zero;
2671 const int last_index = s->block_last_index[n];
2674 if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
2675 /* mpeg4 based DC predictor */
2676 //mpeg4_encode_dc(dc_pb, intra_dc, n); //FIXME
2677 if(last_index<1) return len;
2680 len_tab = uni_mpeg4_intra_rl_len;
2682 if(last_index<0) return 0;
2685 len_tab = uni_mpeg4_inter_rl_len;
2689 last_non_zero = i - 1;
2690 for (; i < last_index; i++) {
2691 int level = block[ scan_table[i] ];
2693 int run = i - last_non_zero - 1;
2695 if((level&(~127)) == 0){
2696 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2697 len += len_tab[index];
2699 len += 7+2+1+6+1+12+1;
2704 /*if(i<=last_index)*/{
2705 int level = block[ scan_table[i] ];
2706 int run = i - last_non_zero - 1;
2708 if((level&(~127)) == 0){
2709 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2710 len += len_tab[index];
2712 len += 7+2+1+6+1+12+1;
2722 /***********************************************/
2725 static VLC intra_MCBPC_vlc;
2726 static VLC inter_MCBPC_vlc;
2727 static VLC cbpy_vlc;
2729 static VLC dc_lum, dc_chrom;
2730 static VLC sprite_trajectory;
2731 static VLC mb_type_b_vlc;
2732 static VLC h263_mbtype_b_vlc;
2733 static VLC cbpc_b_vlc;
2735 void init_vlc_rl(RLTable *rl)
2739 init_vlc(&rl->vlc, 9, rl->n + 1,
2740 &rl->table_vlc[0][1], 4, 2,
2741 &rl->table_vlc[0][0], 4, 2);
2744 for(q=0; q<32; q++){
2753 rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
2754 for(i=0; i<rl->vlc.table_size; i++){
2755 int code= rl->vlc.table[i][0];
2756 int len = rl->vlc.table[i][1];
2759 if(len==0){ // illegal code
2762 }else if(len<0){ //more bits needed
2766 if(code==rl->n){ //esc
2770 run= rl->table_run [code] + 1;
2771 level= rl->table_level[code] * qmul + qadd;
2772 if(code >= rl->last) run+=192;
2775 rl->rl_vlc[q][i].len= len;
2776 rl->rl_vlc[q][i].level= level;
2777 rl->rl_vlc[q][i].run= run;
2784 /* XXX: find a better solution to handle static init */
2785 void h263_decode_init_vlc(MpegEncContext *s)
2787 static int done = 0;
2792 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
2793 intra_MCBPC_bits, 1, 1,
2794 intra_MCBPC_code, 1, 1);
2795 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
2796 inter_MCBPC_bits, 1, 1,
2797 inter_MCBPC_code, 1, 1);
2798 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
2799 &cbpy_tab[0][1], 2, 1,
2800 &cbpy_tab[0][0], 2, 1);
2801 init_vlc(&mv_vlc, MV_VLC_BITS, 33,
2803 &mvtab[0][0], 2, 1);
2806 init_rl(&rvlc_rl_inter);
2807 init_rl(&rvlc_rl_intra);
2808 init_rl(&rl_intra_aic);
2809 init_vlc_rl(&rl_inter);
2810 init_vlc_rl(&rl_intra);
2811 init_vlc_rl(&rvlc_rl_inter);
2812 init_vlc_rl(&rvlc_rl_intra);
2813 init_vlc_rl(&rl_intra_aic);
2814 init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2815 &DCtab_lum[0][1], 2, 1,
2816 &DCtab_lum[0][0], 2, 1);
2817 init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2818 &DCtab_chrom[0][1], 2, 1,
2819 &DCtab_chrom[0][0], 2, 1);
2820 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2821 &sprite_trajectory_tab[0][1], 4, 2,
2822 &sprite_trajectory_tab[0][0], 4, 2);
2823 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2824 &mb_type_b_tab[0][1], 2, 1,
2825 &mb_type_b_tab[0][0], 2, 1);
2826 init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
2827 &h263_mbtype_b_tab[0][1], 2, 1,
2828 &h263_mbtype_b_tab[0][0], 2, 1);
2829 init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
2830 &cbpc_b_tab[0][1], 2, 1,
2831 &cbpc_b_tab[0][0], 2, 1);
2836 * Get the GOB height based on picture height.
2838 int ff_h263_get_gob_height(MpegEncContext *s){
2839 if (s->height <= 400)
2841 else if (s->height <= 800)
2847 int ff_h263_decode_mba(MpegEncContext *s)
2852 if(s->mb_num-1 <= ff_mba_max[i]) break;
2854 mb_pos= get_bits(&s->gb, ff_mba_length[i]);
2855 s->mb_x= mb_pos % s->mb_width;
2856 s->mb_y= mb_pos / s->mb_width;
2861 void ff_h263_encode_mba(MpegEncContext *s)
2866 if(s->mb_num-1 <= ff_mba_max[i]) break;
2868 mb_pos= s->mb_x + s->mb_width*s->mb_y;
2869 put_bits(&s->pb, ff_mba_length[i], mb_pos);
2873 * decodes the group of blocks header or slice header.
2874 * @return <0 if an error occured
2876 static int h263_decode_gob_header(MpegEncContext *s)
2878 unsigned int val, gfid, gob_number;
2881 /* Check for GOB Start Code */
2882 val = show_bits(&s->gb, 16);
2886 /* We have a GBSC probably with GSTUFF */
2887 skip_bits(&s->gb, 16); /* Drop the zeros */
2888 left= s->gb.size_in_bits - get_bits_count(&s->gb);
2889 //MN: we must check the bits left or we might end in a infinite loop (or segfault)
2890 for(;left>13; left--){
2891 if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
2896 if(s->h263_slice_structured){
2897 if(get_bits1(&s->gb)==0)
2900 ff_h263_decode_mba(s);
2902 if(s->mb_num > 1583)
2903 if(get_bits1(&s->gb)==0)
2906 s->qscale = get_bits(&s->gb, 5); /* SQUANT */
2907 if(get_bits1(&s->gb)==0)
2909 gfid = get_bits(&s->gb, 2); /* GFID */
2911 gob_number = get_bits(&s->gb, 5); /* GN */
2913 s->mb_y= s->gob_index* gob_number;
2914 gfid = get_bits(&s->gb, 2); /* GFID */
2915 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
2918 if(s->mb_y >= s->mb_height)
2927 static inline void memsetw(short *tab, int val, int n)
2934 #ifdef CONFIG_ENCODERS
2936 void ff_mpeg4_init_partitions(MpegEncContext *s)
2938 uint8_t *start= pbBufPtr(&s->pb);
2939 uint8_t *end= s->pb.buf_end;
2940 int size= end - start;
2941 int pb_size = (((int)start + size/3)&(~3)) - (int)start;
2942 int tex_size= (size - 2*pb_size)&(~3);
2944 set_put_bits_buffer_size(&s->pb, pb_size);
2945 init_put_bits(&s->tex_pb, start + pb_size , tex_size);
2946 init_put_bits(&s->pb2 , start + pb_size + tex_size, pb_size);
2949 void ff_mpeg4_merge_partitions(MpegEncContext *s)
2951 const int pb2_len = put_bits_count(&s->pb2 );
2952 const int tex_pb_len= put_bits_count(&s->tex_pb);
2953 const int bits= put_bits_count(&s->pb);
2955 if(s->pict_type==I_TYPE){
2956 put_bits(&s->pb, 19, DC_MARKER);
2957 s->misc_bits+=19 + pb2_len + bits - s->last_bits;
2958 s->i_tex_bits+= tex_pb_len;
2960 put_bits(&s->pb, 17, MOTION_MARKER);
2961 s->misc_bits+=17 + pb2_len;
2962 s->mv_bits+= bits - s->last_bits;
2963 s->p_tex_bits+= tex_pb_len;
2966 flush_put_bits(&s->pb2);
2967 flush_put_bits(&s->tex_pb);
2969 set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
2970 ff_copy_bits(&s->pb, s->pb2.buf , pb2_len);
2971 ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
2972 s->last_bits= put_bits_count(&s->pb);
2975 #endif //CONFIG_ENCODERS
2977 int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s){
2978 switch(s->pict_type){
2983 return s->f_code+15;
2985 return FFMAX(FFMAX(s->f_code, s->b_code)+15, 17);
2991 #ifdef CONFIG_ENCODERS
2993 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
2995 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
2997 put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
2998 put_bits(&s->pb, 1, 1);
3000 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
3001 put_bits(&s->pb, s->quant_precision, s->qscale);
3002 put_bits(&s->pb, 1, 0); /* no HEC */
3005 #endif //CONFIG_ENCODERS
3008 * check if the next stuff is a resync marker or the end.
3011 static inline int mpeg4_is_resync(MpegEncContext *s){
3012 const int bits_count= get_bits_count(&s->gb);
3014 if(s->workaround_bugs&FF_BUG_NO_PADDING){
3018 if(bits_count + 8 >= s->gb.size_in_bits){
3019 int v= show_bits(&s->gb, 8);
3020 v|= 0x7F >> (7-(bits_count&7));
3025 if(show_bits(&s->gb, 16) == ff_mpeg4_resync_prefix[bits_count&7]){
3027 GetBitContext gb= s->gb;
3029 skip_bits(&s->gb, 1);
3030 align_get_bits(&s->gb);
3032 for(len=0; len<32; len++){
3033 if(get_bits1(&s->gb)) break;
3038 if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
3046 * decodes the next video packet.
3047 * @return <0 if something went wrong
3049 static int mpeg4_decode_video_packet_header(MpegEncContext *s)
3051 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
3052 int header_extension=0, mb_num, len;
3054 /* is there enough space left for a video packet + header */
3055 if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
3057 for(len=0; len<32; len++){
3058 if(get_bits1(&s->gb)) break;
3061 if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
3062 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
3066 if(s->shape != RECT_SHAPE){
3067 header_extension= get_bits1(&s->gb);
3068 //FIXME more stuff here
3071 mb_num= get_bits(&s->gb, mb_num_bits);
3072 if(mb_num>=s->mb_num){
3073 av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
3076 if(s->pict_type == B_TYPE){
3077 while(s->next_picture.mbskip_table[ s->mb_index2xy[ mb_num ] ]) mb_num++;
3078 if(mb_num >= s->mb_num) return -1; // slice contains just skiped MBs which where allready decoded
3081 s->mb_x= mb_num % s->mb_width;
3082 s->mb_y= mb_num / s->mb_width;
3084 if(s->shape != BIN_ONLY_SHAPE){
3085 int qscale= get_bits(&s->gb, s->quant_precision);
3087 s->chroma_qscale=s->qscale= qscale;
3090 if(s->shape == RECT_SHAPE){
3091 header_extension= get_bits1(&s->gb);
3093 if(header_extension){
3097 while (get_bits1(&s->gb) != 0)
3100 check_marker(&s->gb, "before time_increment in video packed header");
3101 time_increment= get_bits(&s->gb, s->time_increment_bits);
3102 check_marker(&s->gb, "before vop_coding_type in video packed header");
3104 skip_bits(&s->gb, 2); /* vop coding type */
3105 //FIXME not rect stuff here
3107 if(s->shape != BIN_ONLY_SHAPE){
3108 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
3109 //FIXME dont just ignore everything
3110 if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
3111 mpeg4_decode_sprite_trajectory(s, &s->gb);
3112 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
3115 //FIXME reduced res stuff here
3117 if (s->pict_type != I_TYPE) {
3118 int f_code = get_bits(&s->gb, 3); /* fcode_for */
3120 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
3123 if (s->pict_type == B_TYPE) {
3124 int b_code = get_bits(&s->gb, 3);
3126 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
3131 //FIXME new-pred stuff
3133 //printf("parse ok %d %d %d %d\n", mb_num, s->mb_x + s->mb_y*s->mb_width, get_bits_count(gb), get_bits_count(&s->gb));
3138 void ff_mpeg4_clean_buffers(MpegEncContext *s)
3140 int c_wrap, c_xy, l_wrap, l_xy;
3142 l_wrap= s->b8_stride;
3143 l_xy= (2*s->mb_y-1)*l_wrap + s->mb_x*2 - 1;
3144 c_wrap= s->mb_stride;
3145 c_xy= (s->mb_y-1)*c_wrap + s->mb_x - 1;
3149 memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
3150 memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
3151 memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
3155 memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(int16_t));
3156 memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t));
3157 memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t));
3160 // we cant clear the MVs as they might be needed by a b frame
3161 // memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(int16_t));
3162 // memset(s->motion_val, 0, 2*sizeof(int16_t)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
3163 s->last_mv[0][0][0]=
3164 s->last_mv[0][0][1]=
3165 s->last_mv[1][0][0]=
3166 s->last_mv[1][0][1]= 0;
3170 * decodes the group of blocks / video packet header.
3171 * @return <0 if no resync found
3173 int ff_h263_resync(MpegEncContext *s){
3176 if(s->codec_id==CODEC_ID_MPEG4){
3178 align_get_bits(&s->gb);
3181 if(show_bits(&s->gb, 16)==0){
3182 if(s->codec_id==CODEC_ID_MPEG4)
3183 ret= mpeg4_decode_video_packet_header(s);
3185 ret= h263_decode_gob_header(s);
3189 //ok, its not where its supposed to be ...
3190 s->gb= s->last_resync_gb;
3191 align_get_bits(&s->gb);
3192 left= s->gb.size_in_bits - get_bits_count(&s->gb);
3194 for(;left>16+1+5+5; left-=8){
3195 if(show_bits(&s->gb, 16)==0){
3196 GetBitContext bak= s->gb;
3198 if(s->codec_id==CODEC_ID_MPEG4)
3199 ret= mpeg4_decode_video_packet_header(s);
3201 ret= h263_decode_gob_header(s);
3207 skip_bits(&s->gb, 8);
3214 * gets the average motion vector for a GMC MB.
3215 * @param n either 0 for the x component or 1 for y
3216 * @returns the average MV for a GMC MB
3218 static inline int get_amv(MpegEncContext *s, int n){
3219 int x, y, mb_v, sum, dx, dy, shift;
3220 int len = 1 << (s->f_code + 4);
3221 const int a= s->sprite_warping_accuracy;
3223 if(s->real_sprite_warping_points==1){
3224 if(s->divx_version==500 && s->divx_build==413)
3225 sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
3227 sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
3229 dx= s->sprite_delta[n][0];
3230 dy= s->sprite_delta[n][1];
3231 shift= s->sprite_shift[0];
3232 if(n) dy -= 1<<(shift + a + 1);
3233 else dx -= 1<<(shift + a + 1);
3234 mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
3237 for(y=0; y<16; y++){
3241 //XXX FIXME optimize
3242 for(x=0; x<16; x++){
3247 sum= RSHIFT(sum, a+8-s->quarter_sample);
3250 if (sum < -len) sum= -len;
3251 else if (sum >= len) sum= len-1;
3257 * decodes first partition.
3258 * @return number of MBs decoded or <0 if an error occured
3260 static int mpeg4_decode_partition_a(MpegEncContext *s){
3262 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
3264 /* decode first partition */
3266 s->first_slice_line=1;
3267 for(; s->mb_y<s->mb_height; s->mb_y++){
3268 ff_init_block_index(s);
3269 for(; s->mb_x<s->mb_width; s->mb_x++){
3270 const int xy= s->mb_x + s->mb_y*s->mb_stride;
3275 ff_update_block_index(s);
3276 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
3277 s->first_slice_line=0;
3279 if(s->pict_type==I_TYPE){
3283 if(show_bits_long(&s->gb, 19)==DC_MARKER){
3287 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
3289 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
3294 s->cbp_table[xy]= cbpc & 3;
3295 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
3299 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
3301 s->current_picture.qscale_table[xy]= s->qscale;
3303 s->mbintra_table[xy]= 1;
3306 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
3308 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
3312 if(dc_pred_dir) dir|=1;
3314 s->pred_dir_table[xy]= dir;
3315 }else{ /* P/S_TYPE */
3316 int mx, my, pred_x, pred_y, bits;
3317 int16_t * const mot_val= s->current_picture.motion_val[0][s->block_index[0]];
3318 const int stride= s->b8_stride*2;
3321 bits= show_bits(&s->gb, 17);
3322 if(bits==MOTION_MARKER){
3328 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
3329 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
3333 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
3336 mot_val[0 ]= mot_val[2 ]=
3337 mot_val[0+stride]= mot_val[2+stride]= mx;
3338 mot_val[1 ]= mot_val[3 ]=
3339 mot_val[1+stride]= mot_val[3+stride]= my;
3341 if(s->mbintra_table[xy])
3342 ff_clean_intra_table_entries(s);
3346 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
3348 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
3354 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
3356 s->mb_intra = ((cbpc & 4) != 0);
3359 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
3360 s->mbintra_table[xy]= 1;
3361 mot_val[0 ]= mot_val[2 ]=
3362 mot_val[0+stride]= mot_val[2+stride]= 0;
3363 mot_val[1 ]= mot_val[3 ]=
3364 mot_val[1+stride]= mot_val[3+stride]= 0;
3366 if(s->mbintra_table[xy])
3367 ff_clean_intra_table_entries(s);
3369 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
3370 s->mcsel= get_bits1(&s->gb);
3373 if ((cbpc & 16) == 0) {
3374 /* 16x16 motion prediction */
3376 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
3378 mx = h263_decode_motion(s, pred_x, s->f_code);
3382 my = h263_decode_motion(s, pred_y, s->f_code);
3385 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
3389 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
3392 mot_val[0 ]= mot_val[2 ] =
3393 mot_val[0+stride]= mot_val[2+stride]= mx;
3394 mot_val[1 ]= mot_val[3 ]=
3395 mot_val[1+stride]= mot_val[3+stride]= my;
3398 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
3400 int16_t *mot_val= h263_pred_motion(s, i, 0, &pred_x, &pred_y);
3401 mx = h263_decode_motion(s, pred_x, s->f_code);
3405 my = h263_decode_motion(s, pred_y, s->f_code);
3422 * decode second partition.
3423 * @return <0 if an error occured
3425 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
3427 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
3429 s->mb_x= s->resync_mb_x;
3430 s->first_slice_line=1;
3431 for(s->mb_y= s->resync_mb_y; mb_num < mb_count; s->mb_y++){
3432 ff_init_block_index(s);
3433 for(; mb_num < mb_count && s->mb_x<s->mb_width; s->mb_x++){
3434 const int xy= s->mb_x + s->mb_y*s->mb_stride;
3437 ff_update_block_index(s);
3438 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
3439 s->first_slice_line=0;
3441 if(s->pict_type==I_TYPE){
3442 int ac_pred= get_bits1(&s->gb);
3443 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3445 av_log(s->avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
3449 s->cbp_table[xy]|= cbpy<<2;
3450 s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
3451 }else{ /* P || S_TYPE */
3452 if(IS_INTRA(s->current_picture.mb_type[xy])){
3454 int ac_pred = get_bits1(&s->gb);
3455 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3458 av_log(s->avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
3462 if(s->cbp_table[xy] & 8) {
3463 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
3465 s->current_picture.qscale_table[xy]= s->qscale;
3469 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
3471 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
3475 if(dc_pred_dir) dir|=1;
3477 s->cbp_table[xy]&= 3; //remove dquant
3478 s->cbp_table[xy]|= cbpy<<2;
3479 s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
3480 s->pred_dir_table[xy]= dir;
3481 }else if(IS_SKIP(s->current_picture.mb_type[xy])){
3482 s->current_picture.qscale_table[xy]= s->qscale;
3483 s->cbp_table[xy]= 0;
3485 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3488 av_log(s->avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
3492 if(s->cbp_table[xy] & 8) {
3493 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
3495 s->current_picture.qscale_table[xy]= s->qscale;
3497 s->cbp_table[xy]&= 3; //remove dquant
3498 s->cbp_table[xy]|= (cbpy^0xf)<<2;
3502 if(mb_num >= mb_count) return 0;
3509 * decodes the first & second partition
3510 * @return <0 if error (and sets error type in the error_status_table)
3512 int ff_mpeg4_decode_partitions(MpegEncContext *s)
3515 const int part_a_error= s->pict_type==I_TYPE ? (DC_ERROR|MV_ERROR) : MV_ERROR;
3516 const int part_a_end = s->pict_type==I_TYPE ? (DC_END |MV_END) : MV_END;
3518 mb_num= mpeg4_decode_partition_a(s);
3520 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
3524 if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){
3525 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
3526 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
3530 s->mb_num_left= mb_num;
3532 if(s->pict_type==I_TYPE){
3533 while(show_bits(&s->gb, 9) == 1)
3534 skip_bits(&s->gb, 9);
3535 if(get_bits_long(&s->gb, 19)!=DC_MARKER){
3536 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y);
3540 while(show_bits(&s->gb, 10) == 1)
3541 skip_bits(&s->gb, 10);
3542 if(get_bits(&s->gb, 17)!=MOTION_MARKER){
3543 av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y);
3547 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end);
3549 if( mpeg4_decode_partition_b(s, mb_num) < 0){
3550 if(s->pict_type==P_TYPE)
3551 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, DC_ERROR);
3554 if(s->pict_type==P_TYPE)
3555 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, DC_END);
3562 * decode partition C of one MB.
3563 * @return <0 if an error occured
3565 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
3568 const int xy= s->mb_x + s->mb_y*s->mb_stride;
3570 mb_type= s->current_picture.mb_type[xy];
3571 cbp = s->cbp_table[xy];
3573 if(s->current_picture.qscale_table[xy] != s->qscale){
3574 ff_set_qscale(s, s->current_picture.qscale_table[xy] );
3577 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
3580 s->mv[0][i][0] = s->current_picture.motion_val[0][ s->block_index[i] ][0];
3581 s->mv[0][i][1] = s->current_picture.motion_val[0][ s->block_index[i] ][1];
3583 s->mb_intra = IS_INTRA(mb_type);
3585 if (IS_SKIP(mb_type)) {
3588 s->block_last_index[i] = -1;
3589 s->mv_dir = MV_DIR_FORWARD;
3590 s->mv_type = MV_TYPE_16X16;
3591 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
3598 }else if(s->mb_intra){
3599 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
3600 }else if(!s->mb_intra){
3601 // s->mcsel= 0; //FIXME do we need to init that
3603 s->mv_dir = MV_DIR_FORWARD;
3604 if (IS_8X8(mb_type)) {
3605 s->mv_type = MV_TYPE_8X8;
3607 s->mv_type = MV_TYPE_16X16;
3610 } else { /* I-Frame */
3612 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
3615 if (!IS_SKIP(mb_type)) {
3617 /* decode each block */
3618 for (i = 0; i < 6; i++) {
3619 if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
3620 av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
3627 /* per-MB end of slice check */
3629 if(--s->mb_num_left <= 0){
3630 //printf("%06X %d\n", show_bits(&s->gb, 24), s->gb.size_in_bits - get_bits_count(&s->gb));
3631 if(mpeg4_is_resync(s))
3636 if(mpeg4_is_resync(s)){
3637 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
3638 if(s->cbp_table[xy+delta])
3646 * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
3648 static void preview_obmc(MpegEncContext *s){
3649 GetBitContext gb= s->gb;
3651 int cbpc, i, pred_x, pred_y, mx, my;
3653 const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
3654 const int stride= s->b8_stride*2;
3657 s->block_index[i]+= 2;
3659 s->block_index[i]+= 1;
3662 assert(s->pict_type == P_TYPE);
3665 if (get_bits1(&s->gb)) {
3667 mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
3668 mot_val[0 ]= mot_val[2 ]=
3669 mot_val[0+stride]= mot_val[2+stride]= 0;
3670 mot_val[1 ]= mot_val[3 ]=
3671 mot_val[1+stride]= mot_val[3+stride]= 0;
3673 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
3676 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
3680 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
3682 get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3684 if(s->modified_quant){
3685 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
3686 else skip_bits(&s->gb, 5);
3688 skip_bits(&s->gb, 2);
3691 if ((cbpc & 16) == 0) {
3692 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
3693 /* 16x16 motion prediction */
3694 mot_val= h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
3696 mx = h263p_decode_umotion(s, pred_x);
3698 mx = h263_decode_motion(s, pred_x, 1);
3701 my = h263p_decode_umotion(s, pred_y);
3703 my = h263_decode_motion(s, pred_y, 1);
3705 mot_val[0 ]= mot_val[2 ]=
3706 mot_val[0+stride]= mot_val[2+stride]= mx;
3707 mot_val[1 ]= mot_val[3 ]=
3708 mot_val[1+stride]= mot_val[3+stride]= my;
3710 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
3712 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
3714 mx = h263p_decode_umotion(s, pred_x);
3716 mx = h263_decode_motion(s, pred_x, 1);
3719 my = h263p_decode_umotion(s, pred_y);
3721 my = h263_decode_motion(s, pred_y, 1);
3722 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
3723 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
3732 s->block_index[i]-= 2;
3734 s->block_index[i]-= 1;
3740 static void h263_decode_dquant(MpegEncContext *s){
3741 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
3743 if(s->modified_quant){
3744 if(get_bits1(&s->gb))
3745 s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
3747 s->qscale= get_bits(&s->gb, 5);
3749 s->qscale += quant_tab[get_bits(&s->gb, 2)];
3750 ff_set_qscale(s, s->qscale);
3753 int ff_h263_decode_mb(MpegEncContext *s,
3754 DCTELEM block[6][64])
3756 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
3758 const int xy= s->mb_x + s->mb_y * s->mb_stride;
3760 assert(!s->h263_pred);
3762 if (s->pict_type == P_TYPE) {
3764 if (get_bits1(&s->gb)) {
3768 s->block_last_index[i] = -1;
3769 s->mv_dir = MV_DIR_FORWARD;
3770 s->mv_type = MV_TYPE_16X16;
3771 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
3774 s->mb_skiped = !(s->obmc | s->loop_filter);
3777 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
3778 //fprintf(stderr, "\tCBPC: %d", cbpc);
3780 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
3786 s->mb_intra = ((cbpc & 4) != 0);
3787 if (s->mb_intra) goto intra;
3789 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3791 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
3794 cbp = (cbpc & 3) | (cbpy << 2);
3796 h263_decode_dquant(s);
3799 s->mv_dir = MV_DIR_FORWARD;
3800 if ((cbpc & 16) == 0) {
3801 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
3802 /* 16x16 motion prediction */
3803 s->mv_type = MV_TYPE_16X16;
3804 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
3806 mx = h263p_decode_umotion(s, pred_x);
3808 mx = h263_decode_motion(s, pred_x, 1);
3814 my = h263p_decode_umotion(s, pred_y);
3816 my = h263_decode_motion(s, pred_y, 1);
3820 s->mv[0][0][0] = mx;
3821 s->mv[0][0][1] = my;
3823 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
3824 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
3826 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
3827 s->mv_type = MV_TYPE_8X8;
3829 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
3831 mx = h263p_decode_umotion(s, pred_x);
3833 mx = h263_decode_motion(s, pred_x, 1);
3838 my = h263p_decode_umotion(s, pred_y);
3840 my = h263_decode_motion(s, pred_y, 1);
3843 s->mv[0][i][0] = mx;
3844 s->mv[0][i][1] = my;
3845 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
3846 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
3852 /* decode each block */
3853 for (i = 0; i < 6; i++) {
3854 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
3860 if(s->pict_type == P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
3863 } else if(s->pict_type==B_TYPE) {
3865 const int stride= s->b8_stride;
3866 int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ];
3867 int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ];
3868 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
3871 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
3872 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
3873 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
3874 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
3877 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
3879 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
3883 mb_type= h263_mb_type_b_map[ mb_type ];
3886 s->mb_intra = IS_INTRA(mb_type);
3887 if(HAS_CBP(mb_type)){
3888 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
3890 dquant = IS_QUANT(mb_type);
3894 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3897 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
3901 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
3904 cbp = (cbpc & 3) | (cbpy << 2);
3908 assert(!s->mb_intra);
3910 if(IS_QUANT(mb_type)){
3911 h263_decode_dquant(s);
3914 if(IS_DIRECT(mb_type)){
3915 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3916 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
3919 s->mv_type= MV_TYPE_16X16;
3922 if(USES_LIST(mb_type, 0)){
3923 int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my);
3924 s->mv_dir = MV_DIR_FORWARD;
3926 mx = h263_decode_motion(s, mx, 1);
3927 my = h263_decode_motion(s, my, 1);
3929 s->mv[0][0][0] = mx;
3930 s->mv[0][0][1] = my;
3931 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
3932 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
3935 if(USES_LIST(mb_type, 1)){
3936 int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my);
3937 s->mv_dir |= MV_DIR_BACKWARD;
3939 mx = h263_decode_motion(s, mx, 1);
3940 my = h263_decode_motion(s, my, 1);
3942 s->mv[1][0][0] = mx;
3943 s->mv[1][0][1] = my;
3944 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
3945 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
3949 s->current_picture.mb_type[xy]= mb_type;
3951 /* decode each block */
3952 for (i = 0; i < 6; i++) {
3953 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
3957 } else { /* I-Frame */
3959 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
3961 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
3969 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
3971 s->ac_pred = get_bits1(&s->gb);
3973 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
3975 s->h263_aic_dir = get_bits1(&s->gb);
3980 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3982 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
3985 cbp = (cbpc & 3) | (cbpy << 2);
3987 h263_decode_dquant(s);
3990 /* decode each block */
3991 for (i = 0; i < 6; i++) {
3992 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
3999 /* per-MB end of slice check */
4001 int v= show_bits(&s->gb, 16);
4003 if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
4004 v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
4014 int ff_mpeg4_decode_mb(MpegEncContext *s,
4015 DCTELEM block[6][64])
4017 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
4019 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
4020 const int xy= s->mb_x + s->mb_y * s->mb_stride;
4022 assert(s->h263_pred);
4024 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
4026 if (get_bits1(&s->gb)) {
4030 s->block_last_index[i] = -1;
4031 s->mv_dir = MV_DIR_FORWARD;
4032 s->mv_type = MV_TYPE_16X16;
4033 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
4034 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
4036 s->mv[0][0][0]= get_amv(s, 0);
4037 s->mv[0][0][1]= get_amv(s, 1);
4041 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
4049 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
4050 //fprintf(stderr, "\tCBPC: %d", cbpc);
4052 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
4058 s->mb_intra = ((cbpc & 4) != 0);
4059 if (s->mb_intra) goto intra;
4061 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
4062 s->mcsel= get_bits1(&s->gb);
4064 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
4066 cbp = (cbpc & 3) | (cbpy << 2);
4068 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
4070 if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
4071 s->interlaced_dct= get_bits1(&s->gb);
4073 s->mv_dir = MV_DIR_FORWARD;
4074 if ((cbpc & 16) == 0) {
4076 s->current_picture.mb_type[xy]= MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
4077 /* 16x16 global motion prediction */
4078 s->mv_type = MV_TYPE_16X16;
4081 s->mv[0][0][0] = mx;
4082 s->mv[0][0][1] = my;
4083 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
4084 s->current_picture.mb_type[xy]= MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
4085 /* 16x8 field motion prediction */
4086 s->mv_type= MV_TYPE_FIELD;
4088 s->field_select[0][0]= get_bits1(&s->gb);
4089 s->field_select[0][1]= get_bits1(&s->gb);
4091 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
4094 mx = h263_decode_motion(s, pred_x, s->f_code);
4098 my = h263_decode_motion(s, pred_y/2, s->f_code);
4102 s->mv[0][i][0] = mx;
4103 s->mv[0][i][1] = my;
4106 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
4107 /* 16x16 motion prediction */
4108 s->mv_type = MV_TYPE_16X16;
4109 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
4110 mx = h263_decode_motion(s, pred_x, s->f_code);
4115 my = h263_decode_motion(s, pred_y, s->f_code);
4119 s->mv[0][0][0] = mx;
4120 s->mv[0][0][1] = my;
4123 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
4124 s->mv_type = MV_TYPE_8X8;
4126 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
4127 mx = h263_decode_motion(s, pred_x, s->f_code);
4131 my = h263_decode_motion(s, pred_y, s->f_code);
4134 s->mv[0][i][0] = mx;
4135 s->mv[0][i][1] = my;
4140 } else if(s->pict_type==B_TYPE) {
4141 int modb1; // first bit of modb
4142 int modb2; // second bit of modb
4145 s->mb_intra = 0; //B-frames never contain intra blocks
4146 s->mcsel=0; // ... true gmc blocks
4150 s->last_mv[i][0][0]=
4151 s->last_mv[i][0][1]=
4152 s->last_mv[i][1][0]=
4153 s->last_mv[i][1][1]= 0;
4157 /* if we skipped it in the future P Frame than skip it now too */
4158 s->mb_skiped= s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
4163 s->block_last_index[i] = -1;
4165 s->mv_dir = MV_DIR_FORWARD;
4166 s->mv_type = MV_TYPE_16X16;
4171 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
4175 modb1= get_bits1(&s->gb);
4177 mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded
4180 modb2= get_bits1(&s->gb);
4181 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
4183 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
4186 mb_type= mb_type_b_map[ mb_type ];
4188 else cbp= get_bits(&s->gb, 6);
4190 if ((!IS_DIRECT(mb_type)) && cbp) {
4191 if(get_bits1(&s->gb)){
4192 ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
4196 if(!s->progressive_sequence){
4198 s->interlaced_dct= get_bits1(&s->gb);
4200 if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){
4201 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
4202 mb_type &= ~MB_TYPE_16x16;
4204 if(USES_LIST(mb_type, 0)){
4205 s->field_select[0][0]= get_bits1(&s->gb);
4206 s->field_select[0][1]= get_bits1(&s->gb);
4208 if(USES_LIST(mb_type, 1)){
4209 s->field_select[1][0]= get_bits1(&s->gb);
4210 s->field_select[1][1]= get_bits1(&s->gb);
4216 if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){
4217 s->mv_type= MV_TYPE_16X16;
4219 if(USES_LIST(mb_type, 0)){
4220 s->mv_dir = MV_DIR_FORWARD;
4222 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
4223 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
4224 s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
4225 s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my;
4228 if(USES_LIST(mb_type, 1)){
4229 s->mv_dir |= MV_DIR_BACKWARD;
4231 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
4232 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
4233 s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
4234 s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my;
4236 }else if(!IS_DIRECT(mb_type)){
4237 s->mv_type= MV_TYPE_FIELD;
4239 if(USES_LIST(mb_type, 0)){
4240 s->mv_dir = MV_DIR_FORWARD;
4243 mx = h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code);
4244 my = h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code);
4245 s->last_mv[0][i][0]= s->mv[0][i][0] = mx;
4246 s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2;
4250 if(USES_LIST(mb_type, 1)){
4251 s->mv_dir |= MV_DIR_BACKWARD;
4254 mx = h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code);
4255 my = h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code);
4256 s->last_mv[1][i][0]= s->mv[1][i][0] = mx;
4257 s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2;
4263 if(IS_DIRECT(mb_type)){
4264 if(IS_SKIP(mb_type))
4267 mx = h263_decode_motion(s, 0, 1);
4268 my = h263_decode_motion(s, 0, 1);
4271 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
4272 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
4274 s->current_picture.mb_type[xy]= mb_type;
4275 } else { /* I-Frame */
4277 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
4279 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
4287 s->ac_pred = get_bits1(&s->gb);
4289 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
4291 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
4293 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
4295 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
4298 cbp = (cbpc & 3) | (cbpy << 2);
4300 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
4303 if(!s->progressive_sequence)
4304 s->interlaced_dct= get_bits1(&s->gb);
4306 /* decode each block */
4307 for (i = 0; i < 6; i++) {
4308 if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
4315 /* decode each block */
4316 for (i = 0; i < 6; i++) {
4317 if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
4323 /* per-MB end of slice check */
4324 if(s->codec_id==CODEC_ID_MPEG4){
4325 if(mpeg4_is_resync(s)){
4326 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
4327 if(s->pict_type==B_TYPE && s->next_picture.mbskip_table[xy + delta])
4336 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
4338 int code, val, sign, shift, l;
4339 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
4346 sign = get_bits1(&s->gb);
4350 val = (val - 1) << shift;
4351 val |= get_bits(&s->gb, shift);
4358 /* modulo decoding */
4359 if (!s->h263_long_vectors) {
4360 l = 1 << (f_code + 4);
4361 val = ((val + l)&(l*2-1)) - l;
4363 /* horrible h263 long vector mode */
4364 if (pred < -31 && val < -63)
4366 if (pred > 32 && val > 63)
4373 /* Decodes RVLC of H.263+ UMV */
4374 static int h263p_decode_umotion(MpegEncContext * s, int pred)
4378 if (get_bits1(&s->gb)) /* Motion difference = 0 */
4381 code = 2 + get_bits1(&s->gb);
4383 while (get_bits1(&s->gb))
4386 code += get_bits1(&s->gb);
4391 code = (sign) ? (pred - code) : (pred + code);
4393 fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
4399 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
4402 int code, level, i, j, last, run;
4403 RLTable *rl = &rl_inter;
4404 const uint8_t *scan_table;
4405 GetBitContext gb= s->gb;
4407 scan_table = s->intra_scantable.permutated;
4408 if (s->h263_aic && s->mb_intra) {
4412 if (s->h263_aic_dir)
4413 scan_table = s->intra_v_scantable.permutated; /* left */
4415 scan_table = s->intra_h_scantable.permutated; /* top */
4417 } else if (s->mb_intra) {
4419 if(s->codec_id == CODEC_ID_RV10){
4420 if (s->rv10_version == 3 && s->pict_type == I_TYPE) {
4421 int component, diff;
4422 component = (n <= 3 ? 0 : n - 4 + 1);
4423 level = s->last_dc[component];
4424 if (s->rv10_first_dc_coded[component]) {
4425 diff = rv_decode_dc(s, n);
4429 level = level & 0xff; /* handle wrap round */
4430 s->last_dc[component] = level;
4432 s->rv10_first_dc_coded[component] = 1;
4435 level = get_bits(&s->gb, 8);
4440 level = get_bits(&s->gb, 8);
4441 if((level&0x7F) == 0){
4442 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
4443 if(s->error_resilience >= FF_ER_COMPLIANT)
4455 if (s->mb_intra && s->h263_aic)
4457 s->block_last_index[n] = i - 1;
4462 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
4464 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
4467 if (code == rl->n) {
4469 if (s->h263_flv > 1) {
4470 int is11 = get_bits1(&s->gb);
4471 last = get_bits1(&s->gb);
4472 run = get_bits(&s->gb, 6);
4474 level = get_sbits(&s->gb, 11);
4476 level = get_sbits(&s->gb, 7);
4479 last = get_bits1(&s->gb);
4480 run = get_bits(&s->gb, 6);
4481 level = (int8_t)get_bits(&s->gb, 8);
4483 if (s->codec_id == CODEC_ID_RV10) {
4484 /* XXX: should patch encoder too */
4485 level = get_sbits(&s->gb, 12);
4487 level = get_bits(&s->gb, 5);
4488 level |= get_sbits(&s->gb, 6)<<5;
4493 run = rl->table_run[code];
4494 level = rl->table_level[code];
4495 last = code >= rl->last;
4496 if (get_bits1(&s->gb))
4501 if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){
4502 //looks like a hack but no, its the way its supposed to work ...
4506 memset(block, 0, sizeof(DCTELEM)*64);
4509 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
4519 if (s->mb_intra && s->h263_aic) {
4520 h263_pred_acdc(s, block, n);
4523 s->block_last_index[n] = i;
4528 * decodes the dc value.
4529 * @param n block index (0-3 are luma, 4-5 are chroma)
4530 * @param dir_ptr the prediction direction will be stored here
4531 * @return the quantized dc
4533 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
4535 int level, pred, code;
4539 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
4541 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
4542 if (code < 0 || code > 9 /* && s->nbit<9 */){
4543 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
4551 level= 2*get_bits1(&s->gb)-1;
4553 if(get_bits1(&s->gb))
4554 level = get_bits(&s->gb, code-1) + (1<<(code-1));
4556 level = -get_bits(&s->gb, code-1) - (1<<(code-1));
4559 level = get_xbits(&s->gb, code);
4563 if(get_bits1(&s->gb)==0){ /* marker */
4564 if(s->error_resilience>=2){
4565 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
4571 pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
4574 if(s->error_resilience>=3){
4575 av_log(s->avctx, AV_LOG_ERROR, "dc<0 at %dx%d\n", s->mb_x, s->mb_y);
4581 *dc_val = level * s->y_dc_scale;
4583 *dc_val = level * s->c_dc_scale;
4586 *dc_val = level * 8;
4588 if(s->error_resilience>=3){
4589 if(*dc_val > 2048 + s->y_dc_scale + s->c_dc_scale){
4590 av_log(s->avctx, AV_LOG_ERROR, "dc overflow at %dx%d\n", s->mb_x, s->mb_y);
4599 * @return <0 if an error occured
4601 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
4602 int n, int coded, int intra, int rvlc)
4604 int level, i, last, run;
4607 RL_VLC_ELEM * rl_vlc;
4608 const uint8_t * scan_table;
4611 //Note intra & rvlc should be optimized away if this is inlined
4614 if(s->qscale < s->intra_dc_threshold){
4616 if(s->partitioned_frame){
4617 level = s->dc_val[0][ s->block_index[n] ];
4618 if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
4619 else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
4620 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
4622 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
4635 rl = &rvlc_rl_intra;
4636 rl_vlc = rvlc_rl_intra.rl_vlc[0];
4639 rl_vlc = rl_intra.rl_vlc[0];
4642 if (dc_pred_dir == 0)
4643 scan_table = s->intra_v_scantable.permutated; /* left */
4645 scan_table = s->intra_h_scantable.permutated; /* top */
4647 scan_table = s->intra_scantable.permutated;
4654 s->block_last_index[n] = i;
4657 if(rvlc) rl = &rvlc_rl_inter;
4658 else rl = &rl_inter;
4660 scan_table = s->intra_scantable.permutated;
4666 rl_vlc = rvlc_rl_inter.rl_vlc[0];
4668 rl_vlc = rl_inter.rl_vlc[0];
4671 qmul = s->qscale << 1;
4672 qadd = (s->qscale - 1) | 1;
4674 rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale];
4676 rl_vlc = rl_inter.rl_vlc[s->qscale];
4681 OPEN_READER(re, &s->gb);
4683 UPDATE_CACHE(re, &s->gb);
4684 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
4688 if(SHOW_UBITS(re, &s->gb, 1)==0){
4689 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
4691 }; SKIP_CACHE(re, &s->gb, 1);
4693 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
4694 run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
4695 SKIP_COUNTER(re, &s->gb, 1+1+6);
4696 UPDATE_CACHE(re, &s->gb);
4698 if(SHOW_UBITS(re, &s->gb, 1)==0){
4699 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
4701 }; SKIP_CACHE(re, &s->gb, 1);
4703 level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
4705 if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
4706 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
4708 }; SKIP_CACHE(re, &s->gb, 5);
4710 level= level * qmul + qadd;
4711 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1);
4712 SKIP_COUNTER(re, &s->gb, 1+11+5+1);
4718 cache= GET_CACHE(re, &s->gb);
4721 cache ^= 0xC0000000;
4723 if (cache&0x80000000) {
4724 if (cache&0x40000000) {
4728 SKIP_CACHE(re, &s->gb, 2);
4729 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
4730 run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
4731 SKIP_COUNTER(re, &s->gb, 2+1+6);
4732 UPDATE_CACHE(re, &s->gb);
4735 level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
4737 if(SHOW_UBITS(re, &s->gb, 1)==0){
4738 av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
4740 }; SKIP_CACHE(re, &s->gb, 1);
4742 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
4744 if(SHOW_UBITS(re, &s->gb, 1)==0){
4745 av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
4747 }; LAST_SKIP_CACHE(re, &s->gb, 1);
4749 SKIP_COUNTER(re, &s->gb, 1+12+1);
4753 if(intra) ulevel= level*s->qscale*s->intra_matrix[scan_table[1]];
4754 else ulevel= level*s->qscale*s->inter_matrix[scan_table[0]];
4756 ulevel= level*s->qscale*16;
4757 if(ulevel>1030*16 || ulevel<-1030*16){
4758 av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
4763 if(s->error_resilience >= FF_ER_COMPLIANT){
4764 const int abs_level= ABS(level);
4765 if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
4766 const int run1= run - rl->max_run[last][abs_level] - 1;
4767 if(abs_level <= rl->max_level[last][run]){
4768 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
4771 if(s->error_resilience > FF_ER_COMPLIANT){
4772 if(abs_level <= rl->max_level[last][run]*2){
4773 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
4776 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
4777 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
4784 if (level>0) level= level * qmul + qadd;
4785 else level= level * qmul - qadd;
4791 #if MIN_CACHE_BITS < 20
4792 LAST_SKIP_BITS(re, &s->gb, 2);
4793 UPDATE_CACHE(re, &s->gb);
4795 SKIP_BITS(re, &s->gb, 2);
4797 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
4798 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
4799 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
4800 LAST_SKIP_BITS(re, &s->gb, 1);
4804 #if MIN_CACHE_BITS < 19
4805 LAST_SKIP_BITS(re, &s->gb, 1);
4806 UPDATE_CACHE(re, &s->gb);
4808 SKIP_BITS(re, &s->gb, 1);
4810 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
4812 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
4813 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
4814 LAST_SKIP_BITS(re, &s->gb, 1);
4819 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
4820 LAST_SKIP_BITS(re, &s->gb, 1);
4825 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
4829 block[scan_table[i]] = level;
4833 block[scan_table[i]] = level;
4835 CLOSE_READER(re, &s->gb);
4839 if(s->qscale >= s->intra_dc_threshold){
4841 block[0] += ff_mpeg4_pred_dc(s, n, &dc_val, &dc_pred_dir);
4843 *dc_val = block[0] * s->y_dc_scale;
4845 *dc_val = block[0] * s->c_dc_scale;
4851 mpeg4_pred_ac(s, block, n, dc_pred_dir);
4853 i = 63; /* XXX: not optimal */
4856 s->block_last_index[n] = i;
4860 /* most is hardcoded. should extend to handle all h263 streams */
4861 int h263_decode_picture_header(MpegEncContext *s)
4863 int format, width, height, i;
4866 align_get_bits(&s->gb);
4868 startcode= get_bits(&s->gb, 22-8);
4870 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=8) {
4871 startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
4873 if(startcode == 0x20)
4877 if (startcode != 0x20) {
4878 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
4881 /* temporal reference */
4882 i = get_bits(&s->gb, 8); /* picture timestamp */
4883 if( (s->picture_number&~0xFF)+i < s->picture_number)
4885 s->picture_number= (s->picture_number&~0xFF) + i;
4887 /* PTYPE starts here */
4888 if (get_bits1(&s->gb) != 1) {
4890 av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
4893 if (get_bits1(&s->gb) != 0) {
4894 av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
4895 return -1; /* h263 id */
4897 skip_bits1(&s->gb); /* split screen off */
4898 skip_bits1(&s->gb); /* camera off */
4899 skip_bits1(&s->gb); /* freeze picture release off */
4901 format = get_bits(&s->gb, 3);
4906 7 extended PTYPE (PLUSPTYPE)
4909 if (format != 7 && format != 6) {
4912 width = h263_format[format][0];
4913 height = h263_format[format][1];
4917 s->pict_type = I_TYPE + get_bits1(&s->gb);
4919 s->h263_long_vectors = get_bits1(&s->gb);
4921 if (get_bits1(&s->gb) != 0) {
4922 av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
4923 return -1; /* SAC: off */
4925 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
4926 s->unrestricted_mv = s->h263_long_vectors || s->obmc;
4928 if (get_bits1(&s->gb) != 0) {
4929 av_log(s->avctx, AV_LOG_ERROR, "H263 PB frame not supported\n");
4930 return -1; /* not PB frame */
4932 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
4933 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
4937 s->avctx->sample_aspect_ratio= (AVRational){12,11};
4938 s->avctx->frame_rate = 30000;
4939 s->avctx->frame_rate_base= 1001;
4945 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
4947 /* ufep other than 0 and 1 are reserved */
4950 format = get_bits(&s->gb, 3);
4951 dprintf("ufep=1, format: %d\n", format);
4952 s->custom_pcf= get_bits1(&s->gb);
4953 s->umvplus = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
4954 if (get_bits1(&s->gb) != 0) {
4955 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
4957 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
4958 s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
4959 s->loop_filter= get_bits1(&s->gb);
4960 s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
4962 s->h263_slice_structured= get_bits1(&s->gb);
4963 if (get_bits1(&s->gb) != 0) {
4964 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
4966 if (get_bits1(&s->gb) != 0) {
4967 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
4969 s->alt_inter_vlc= get_bits1(&s->gb);
4970 s->modified_quant= get_bits1(&s->gb);
4971 if(s->modified_quant)
4972 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
4974 skip_bits(&s->gb, 1); /* Prevent start code emulation */
4976 skip_bits(&s->gb, 3); /* Reserved */
4977 } else if (ufep != 0) {
4978 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
4983 s->pict_type = get_bits(&s->gb, 3);
4984 switch(s->pict_type){
4985 case 0: s->pict_type= I_TYPE;break;
4986 case 1: s->pict_type= P_TYPE;break;
4987 case 3: s->pict_type= B_TYPE;break;
4988 case 7: s->pict_type= I_TYPE;break; //ZYGO
4992 skip_bits(&s->gb, 2);
4993 s->no_rounding = get_bits1(&s->gb);
4994 skip_bits(&s->gb, 4);
4996 /* Get the picture dimensions */
4999 /* Custom Picture Format (CPFMT) */
5000 s->aspect_ratio_info = get_bits(&s->gb, 4);
5001 dprintf("aspect: %d\n", s->aspect_ratio_info);
5006 3 - 10:11 (525-type 4:3)
5007 4 - 16:11 (CIF 16:9)
5008 5 - 40:33 (525-type 16:9)
5011 width = (get_bits(&s->gb, 9) + 1) * 4;
5013 height = get_bits(&s->gb, 9) * 4;
5014 dprintf("\nH.263+ Custom picture: %dx%d\n",width,height);
5015 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
5016 /* aspected dimensions */
5017 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
5018 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
5020 s->avctx->sample_aspect_ratio= pixel_aspect[s->aspect_ratio_info];
5023 width = h263_format[format][0];
5024 height = h263_format[format][1];
5025 s->avctx->sample_aspect_ratio= (AVRational){12,11};
5027 if ((width == 0) || (height == 0))
5034 s->avctx->frame_rate= 1800000;
5035 s->avctx->frame_rate_base= 1000 + get_bits1(&s->gb);
5036 s->avctx->frame_rate_base*= get_bits(&s->gb, 7);
5037 if(s->avctx->frame_rate_base == 0){
5038 av_log(s, AV_LOG_ERROR, "zero framerate\n");
5041 gcd= ff_gcd(s->avctx->frame_rate, s->avctx->frame_rate_base);
5042 s->avctx->frame_rate /= gcd;
5043 s->avctx->frame_rate_base /= gcd;
5044 // av_log(s->avctx, AV_LOG_DEBUG, "%d/%d\n", s->avctx->frame_rate, s->avctx->frame_rate_base);
5046 s->avctx->frame_rate = 30000;
5047 s->avctx->frame_rate_base= 1001;
5052 skip_bits(&s->gb, 2); //extended Temporal reference
5057 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
5060 if(s->h263_slice_structured){
5061 if (get_bits1(&s->gb) != 0) {
5062 av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
5064 if (get_bits1(&s->gb) != 0) {
5065 av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
5070 s->qscale = get_bits(&s->gb, 5);
5073 s->mb_width = (s->width + 15) / 16;
5074 s->mb_height = (s->height + 15) / 16;
5075 s->mb_num = s->mb_width * s->mb_height;
5078 while (get_bits1(&s->gb) != 0) {
5079 skip_bits(&s->gb, 8);
5082 if(s->h263_slice_structured){
5083 if (get_bits1(&s->gb) != 1) {
5084 av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
5088 ff_h263_decode_mba(s);
5090 if (get_bits1(&s->gb) != 1) {
5091 av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
5098 s->y_dc_scale_table=
5099 s->c_dc_scale_table= ff_aic_dc_scale_table;
5101 s->y_dc_scale_table=
5102 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
5105 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
5106 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
5107 s->qscale, av_get_pict_type_char(s->pict_type),
5108 s->gb.size_in_bits, 1-s->no_rounding,
5109 s->obmc ? " AP" : "",
5110 s->umvplus ? " UMV" : "",
5111 s->h263_long_vectors ? " LONG" : "",
5112 s->h263_plus ? " +" : "",
5113 s->h263_aic ? " AIC" : "",
5114 s->alt_inter_vlc ? " AIV" : "",
5115 s->modified_quant ? " MQ" : "",
5116 s->loop_filter ? " LOOP" : "",
5117 s->h263_slice_structured ? " SS" : "",
5118 s->avctx->frame_rate, s->avctx->frame_rate_base
5122 if (s->pict_type == I_TYPE && s->avctx->codec_tag == ff_get_fourcc("ZYGO")){
5124 for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
5125 av_log(s->avctx, AV_LOG_DEBUG, "\n");
5126 for(i=0; i<13; i++){
5128 int v= get_bits(&s->gb, 8);
5129 v |= get_sbits(&s->gb, 8)<<8;
5130 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
5132 av_log(s->avctx, AV_LOG_DEBUG, "\n");
5134 for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
5141 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb)
5144 int a= 2<<s->sprite_warping_accuracy;
5145 int rho= 3-s->sprite_warping_accuracy;
5147 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
5148 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
5149 int sprite_ref[4][2];
5150 int virtual_ref[2][2];
5152 int alpha=0, beta=0;
5157 for(i=0; i<s->num_sprite_warping_points; i++){
5161 length= get_vlc(gb, &sprite_trajectory);
5163 x= get_xbits(gb, length);
5165 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(gb); /* marker bit */
5167 length= get_vlc(gb, &sprite_trajectory);
5169 y=get_xbits(gb, length);
5171 skip_bits1(gb); /* marker bit */
5172 //printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy);
5177 while((1<<alpha)<w) alpha++;
5178 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
5182 // Note, the 4th point isnt used for GMC
5183 if(s->divx_version==500 && s->divx_build==413){
5184 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
5185 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
5186 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
5187 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
5188 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
5189 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
5191 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
5192 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
5193 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
5194 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
5195 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
5196 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
5198 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
5199 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
5201 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
5202 // perhaps it should be reordered to be more readable ...
5203 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
5204 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
5205 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
5206 + 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);
5207 virtual_ref[0][1]= 16*vop_ref[0][1]
5208 + 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);
5209 virtual_ref[1][0]= 16*vop_ref[0][0]
5210 + 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);
5211 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
5212 + 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);
5214 switch(s->num_sprite_warping_points)
5217 s->sprite_offset[0][0]= 0;
5218 s->sprite_offset[0][1]= 0;
5219 s->sprite_offset[1][0]= 0;
5220 s->sprite_offset[1][1]= 0;
5221 s->sprite_delta[0][0]= a;
5222 s->sprite_delta[0][1]= 0;
5223 s->sprite_delta[1][0]= 0;
5224 s->sprite_delta[1][1]= a;
5225 s->sprite_shift[0]= 0;
5226 s->sprite_shift[1]= 0;
5229 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
5230 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
5231 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
5232 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
5233 s->sprite_delta[0][0]= a;
5234 s->sprite_delta[0][1]= 0;
5235 s->sprite_delta[1][0]= 0;
5236 s->sprite_delta[1][1]= a;
5237 s->sprite_shift[0]= 0;
5238 s->sprite_shift[1]= 0;
5241 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
5242 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
5243 + ( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])
5244 + (1<<(alpha+rho-1));
5245 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
5246 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
5247 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])
5248 + (1<<(alpha+rho-1));
5249 s->sprite_offset[1][0]= ( (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
5250 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
5251 +2*w2*r*sprite_ref[0][0]
5253 + (1<<(alpha+rho+1)));
5254 s->sprite_offset[1][1]= ( (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
5255 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
5256 +2*w2*r*sprite_ref[0][1]
5258 + (1<<(alpha+rho+1)));
5259 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
5260 s->sprite_delta[0][1]= (+r*sprite_ref[0][1] - virtual_ref[0][1]);
5261 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
5262 s->sprite_delta[1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
5264 s->sprite_shift[0]= alpha+rho;
5265 s->sprite_shift[1]= alpha+rho+2;
5268 min_ab= FFMIN(alpha, beta);
5271 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+beta+rho-min_ab))
5272 + (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-vop_ref[0][0])
5273 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-vop_ref[0][1])
5274 + (1<<(alpha+beta+rho-min_ab-1));
5275 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+beta+rho-min_ab))
5276 + (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-vop_ref[0][0])
5277 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-vop_ref[0][1])
5278 + (1<<(alpha+beta+rho-min_ab-1));
5279 s->sprite_offset[1][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-2*vop_ref[0][0] + 1)
5280 + (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-2*vop_ref[0][1] + 1)
5281 + 2*w2*h3*r*sprite_ref[0][0]
5283 + (1<<(alpha+beta+rho-min_ab+1));
5284 s->sprite_offset[1][1]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-2*vop_ref[0][0] + 1)
5285 + (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-2*vop_ref[0][1] + 1)
5286 + 2*w2*h3*r*sprite_ref[0][1]
5288 + (1<<(alpha+beta+rho-min_ab+1));
5289 s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3;
5290 s->sprite_delta[0][1]= (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3;
5291 s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3;
5292 s->sprite_delta[1][1]= (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3;
5294 s->sprite_shift[0]= alpha + beta + rho - min_ab;
5295 s->sprite_shift[1]= alpha + beta + rho - min_ab + 2;
5298 /* try to simplify the situation */
5299 if( s->sprite_delta[0][0] == a<<s->sprite_shift[0]
5300 && s->sprite_delta[0][1] == 0
5301 && s->sprite_delta[1][0] == 0
5302 && s->sprite_delta[1][1] == a<<s->sprite_shift[0])
5304 s->sprite_offset[0][0]>>=s->sprite_shift[0];
5305 s->sprite_offset[0][1]>>=s->sprite_shift[0];
5306 s->sprite_offset[1][0]>>=s->sprite_shift[1];
5307 s->sprite_offset[1][1]>>=s->sprite_shift[1];
5308 s->sprite_delta[0][0]= a;
5309 s->sprite_delta[0][1]= 0;
5310 s->sprite_delta[1][0]= 0;
5311 s->sprite_delta[1][1]= a;
5312 s->sprite_shift[0]= 0;
5313 s->sprite_shift[1]= 0;
5314 s->real_sprite_warping_points=1;
5317 int shift_y= 16 - s->sprite_shift[0];
5318 int shift_c= 16 - s->sprite_shift[1];
5319 //printf("shifts %d %d\n", shift_y, shift_c);
5321 s->sprite_offset[0][i]<<= shift_y;
5322 s->sprite_offset[1][i]<<= shift_c;
5323 s->sprite_delta[0][i]<<= shift_y;
5324 s->sprite_delta[1][i]<<= shift_y;
5325 s->sprite_shift[i]= 16;
5327 s->real_sprite_warping_points= s->num_sprite_warping_points;
5330 printf("vop:%d:%d %d:%d %d:%d, sprite:%d:%d %d:%d %d:%d, virtual: %d:%d %d:%d\n",
5331 vop_ref[0][0], vop_ref[0][1],
5332 vop_ref[1][0], vop_ref[1][1],
5333 vop_ref[2][0], vop_ref[2][1],
5334 sprite_ref[0][0], sprite_ref[0][1],
5335 sprite_ref[1][0], sprite_ref[1][1],
5336 sprite_ref[2][0], sprite_ref[2][1],
5337 virtual_ref[0][0], virtual_ref[0][1],
5338 virtual_ref[1][0], virtual_ref[1][1]
5341 printf("offset: %d:%d , delta: %d %d %d %d, shift %d\n",
5342 s->sprite_offset[0][0], s->sprite_offset[0][1],
5343 s->sprite_delta[0][0], s->sprite_delta[0][1],
5344 s->sprite_delta[1][0], s->sprite_delta[1][1],
5350 static int mpeg4_decode_gop_header(MpegEncContext * s, GetBitContext *gb){
5351 int hours, minutes, seconds;
5353 hours= get_bits(gb, 5);
5354 minutes= get_bits(gb, 6);
5356 seconds= get_bits(gb, 6);
5358 s->time_base= seconds + 60*(minutes + 60*hours);
5366 static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){
5367 int width, height, vo_ver_id;
5370 skip_bits(gb, 1); /* random access */
5371 s->vo_type= get_bits(gb, 8);
5372 if (get_bits1(gb) != 0) { /* is_ol_id */
5373 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
5374 skip_bits(gb, 3); /* vo_priority */
5378 //printf("vo type:%d\n",s->vo_type);
5379 s->aspect_ratio_info= get_bits(gb, 4);
5380 if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){
5381 s->avctx->sample_aspect_ratio.num= get_bits(gb, 8); // par_width
5382 s->avctx->sample_aspect_ratio.den= get_bits(gb, 8); // par_height
5384 s->avctx->sample_aspect_ratio= pixel_aspect[s->aspect_ratio_info];
5387 if ((s->vol_control_parameters=get_bits1(gb))) { /* vol control parameter */
5388 int chroma_format= get_bits(gb, 2);
5389 if(chroma_format!=1){
5390 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
5392 s->low_delay= get_bits1(gb);
5393 if(get_bits1(gb)){ /* vbv parameters */
5394 get_bits(gb, 15); /* first_half_bitrate */
5395 skip_bits1(gb); /* marker */
5396 get_bits(gb, 15); /* latter_half_bitrate */
5397 skip_bits1(gb); /* marker */
5398 get_bits(gb, 15); /* first_half_vbv_buffer_size */
5399 skip_bits1(gb); /* marker */
5400 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
5401 get_bits(gb, 11); /* first_half_vbv_occupancy */
5402 skip_bits1(gb); /* marker */
5403 get_bits(gb, 15); /* latter_half_vbv_occupancy */
5404 skip_bits1(gb); /* marker */
5407 // set low delay flag only once so the smart? low delay detection wont be overriden
5408 if(s->picture_number==0)
5412 s->shape = get_bits(gb, 2); /* vol shape */
5413 if(s->shape != RECT_SHAPE) av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
5414 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
5415 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
5416 skip_bits(gb, 4); //video_object_layer_shape_extension
5419 check_marker(gb, "before time_increment_resolution");
5421 s->time_increment_resolution = get_bits(gb, 16);
5423 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
5424 if (s->time_increment_bits < 1)
5425 s->time_increment_bits = 1;
5427 check_marker(gb, "before fixed_vop_rate");
5429 if (get_bits1(gb) != 0) { /* fixed_vop_rate */
5430 skip_bits(gb, s->time_increment_bits);
5433 if (s->shape != BIN_ONLY_SHAPE) {
5434 if (s->shape == RECT_SHAPE) {
5435 skip_bits1(gb); /* marker */
5436 width = get_bits(gb, 13);
5437 skip_bits1(gb); /* marker */
5438 height = get_bits(gb, 13);
5439 skip_bits1(gb); /* marker */
5440 if(width && height){ /* they should be non zero but who knows ... */
5443 // printf("width/height: %d %d\n", width, height);
5447 s->progressive_sequence=
5448 s->progressive_frame= get_bits1(gb)^1;
5449 if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
5450 av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
5451 if (vo_ver_id == 1) {
5452 s->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
5454 s->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
5456 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
5457 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
5458 if(s->vol_sprite_usage==STATIC_SPRITE){
5459 s->sprite_width = get_bits(gb, 13);
5460 skip_bits1(gb); /* marker */
5461 s->sprite_height= get_bits(gb, 13);
5462 skip_bits1(gb); /* marker */
5463 s->sprite_left = get_bits(gb, 13);
5464 skip_bits1(gb); /* marker */
5465 s->sprite_top = get_bits(gb, 13);
5466 skip_bits1(gb); /* marker */
5468 s->num_sprite_warping_points= get_bits(gb, 6);
5469 s->sprite_warping_accuracy = get_bits(gb, 2);
5470 s->sprite_brightness_change= get_bits1(gb);
5471 if(s->vol_sprite_usage==STATIC_SPRITE)
5472 s->low_latency_sprite= get_bits1(gb);
5474 // FIXME sadct disable bit if verid!=1 && shape not rect
5476 if (get_bits1(gb) == 1) { /* not_8_bit */
5477 s->quant_precision = get_bits(gb, 4); /* quant_precision */
5478 if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n"); /* bits_per_pixel */
5479 if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precission %d\n", s->quant_precision);
5481 s->quant_precision = 5;
5484 // FIXME a bunch of grayscale shape things
5486 if((s->mpeg_quant=get_bits1(gb))){ /* vol_quant_type */
5489 /* load default matrixes */
5490 for(i=0; i<64; i++){
5491 int j= s->dsp.idct_permutation[i];
5492 v= ff_mpeg4_default_intra_matrix[i];
5493 s->intra_matrix[j]= v;
5494 s->chroma_intra_matrix[j]= v;
5496 v= ff_mpeg4_default_non_intra_matrix[i];
5497 s->inter_matrix[j]= v;
5498 s->chroma_inter_matrix[j]= v;
5501 /* load custom intra matrix */
5504 for(i=0; i<64; i++){
5510 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
5511 s->intra_matrix[j]= v;
5512 s->chroma_intra_matrix[j]= v;
5515 /* replicate last value */
5517 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
5518 s->intra_matrix[j]= last;
5519 s->chroma_intra_matrix[j]= last;
5523 /* load custom non intra matrix */
5526 for(i=0; i<64; i++){
5532 j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
5533 s->inter_matrix[j]= v;
5534 s->chroma_inter_matrix[j]= v;
5537 /* replicate last value */
5539 int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
5540 s->inter_matrix[j]= last;
5541 s->chroma_inter_matrix[j]= last;
5545 // FIXME a bunch of grayscale shape things
5549 s->quarter_sample= get_bits1(gb);
5550 else s->quarter_sample=0;
5552 if(!get_bits1(gb)) av_log(s->avctx, AV_LOG_ERROR, "Complexity estimation not supported\n");
5554 s->resync_marker= !get_bits1(gb); /* resync_marker_disabled */
5556 s->data_partitioning= get_bits1(gb);
5557 if(s->data_partitioning){
5558 s->rvlc= get_bits1(gb);
5561 if(vo_ver_id != 1) {
5562 s->new_pred= get_bits1(gb);
5564 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
5565 skip_bits(gb, 2); /* requested upstream message type */
5566 skip_bits1(gb); /* newpred segment type */
5568 s->reduced_res_vop= get_bits1(gb);
5569 if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
5573 s->reduced_res_vop= 0;
5576 s->scalability= get_bits1(gb);
5578 if (s->scalability) {
5579 GetBitContext bak= *gb;
5581 int ref_layer_sampling_dir;
5582 int h_sampling_factor_n;
5583 int h_sampling_factor_m;
5584 int v_sampling_factor_n;
5585 int v_sampling_factor_m;
5587 s->hierachy_type= get_bits1(gb);
5588 ref_layer_id= get_bits(gb, 4);
5589 ref_layer_sampling_dir= get_bits1(gb);
5590 h_sampling_factor_n= get_bits(gb, 5);
5591 h_sampling_factor_m= get_bits(gb, 5);
5592 v_sampling_factor_n= get_bits(gb, 5);
5593 v_sampling_factor_m= get_bits(gb, 5);
5594 s->enhancement_type= get_bits1(gb);
5596 if( h_sampling_factor_n==0 || h_sampling_factor_m==0
5597 || v_sampling_factor_n==0 || v_sampling_factor_m==0){
5599 // fprintf(stderr, "illegal scalability header (VERY broken encoder), trying to workaround\n");
5604 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
5606 // bin shape stuff FIXME
5613 * decodes the user data stuff in the header.
5614 * allso inits divx/xvid/lavc_version/build
5616 static int decode_user_data(MpegEncContext *s, GetBitContext *gb){
5620 int ver, build, ver2, ver3;
5623 buf[0]= show_bits(gb, 8);
5624 for(i=1; i<256; i++){
5625 buf[i]= show_bits(gb, 16)&0xFF;
5626 if(buf[i]==0) break;
5631 /* divx detection */
5632 e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
5634 e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
5636 s->divx_version= ver;
5637 s->divx_build= build;
5638 s->divx_packed= e==3 && last=='p';
5641 /* ffmpeg detection */
5642 e=sscanf(buf, "FFmpeg%d.%d.%db%d", &ver, &ver2, &ver3, &build);
5644 e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
5646 if(strcmp(buf, "ffmpeg")==0){
5647 s->ffmpeg_version= 0x000406;
5648 s->lavc_build= 4600;
5652 s->ffmpeg_version= ver*256*256 + ver2*256 + ver3;
5653 s->lavc_build= build;
5656 /* xvid detection */
5657 e=sscanf(buf, "XviD%d", &build);
5659 s->xvid_build= build;
5662 //printf("User Data: %s\n", buf);
5666 static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
5667 int time_incr, time_increment;
5669 s->pict_type = get_bits(gb, 2) + I_TYPE; /* pict type: I = 0 , P = 1 */
5670 if(s->pict_type==B_TYPE && s->low_delay && s->vol_control_parameters==0 && !(s->flags & CODEC_FLAG_LOW_DELAY)){
5671 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set, but shouldnt, clearing it\n");
5675 s->partitioned_frame= s->data_partitioning && s->pict_type!=B_TYPE;
5676 if(s->partitioned_frame)
5677 s->decode_mb= mpeg4_decode_partitioned_mb;
5679 s->decode_mb= ff_mpeg4_decode_mb;
5681 if(s->time_increment_resolution==0){
5682 s->time_increment_resolution=1;
5683 // fprintf(stderr, "time_increment_resolution is illegal\n");
5686 while (get_bits1(gb) != 0)
5689 check_marker(gb, "before time_increment");
5691 if(s->time_increment_bits==0){
5692 av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers arnt complete, trying to guess time_increment_bits\n");
5694 for(s->time_increment_bits=1 ;s->time_increment_bits<16; s->time_increment_bits++){
5695 if(show_bits(gb, s->time_increment_bits+1)&1) break;
5698 av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
5701 if(IS_3IV1) time_increment= get_bits1(gb); //FIXME investigate further
5702 else time_increment= get_bits(gb, s->time_increment_bits);
5704 // printf("%d %X\n", s->time_increment_bits, time_increment);
5705 //printf(" type:%d modulo_time_base:%d increment:%d\n", s->pict_type, time_incr, time_increment);
5706 if(s->pict_type!=B_TYPE){
5707 s->last_time_base= s->time_base;
5708 s->time_base+= time_incr;
5709 s->time= s->time_base*s->time_increment_resolution + time_increment;
5710 if(s->workaround_bugs&FF_BUG_UMP4){
5711 if(s->time < s->last_non_b_time){
5712 // fprintf(stderr, "header is not mpeg4 compatible, broken encoder, trying to workaround\n");
5714 s->time+= s->time_increment_resolution;
5717 s->pp_time= s->time - s->last_non_b_time;
5718 s->last_non_b_time= s->time;
5720 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
5721 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
5722 if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
5723 // printf("messed up order, seeking?, skiping current b frame\n");
5724 return FRAME_SKIPED;
5727 if(s->t_frame==0) s->t_frame= s->time - s->last_time_base;
5728 if(s->t_frame==0) s->t_frame=1; // 1/0 protection
5729 //printf("%Ld %Ld %d %d\n", s->last_non_b_time, s->time, s->pp_time, s->t_frame); fflush(stdout);
5730 s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame)
5731 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
5732 s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
5733 - ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
5736 s->current_picture_ptr->pts= s->time*(int64_t)AV_TIME_BASE / s->time_increment_resolution;
5737 if(s->avctx->debug&FF_DEBUG_PTS)
5738 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %f\n", s->current_picture_ptr->pts/(float)AV_TIME_BASE);
5740 check_marker(gb, "before vop_coded");
5743 if (get_bits1(gb) != 1){
5744 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
5745 return FRAME_SKIPED;
5747 //printf("time %d %d %d || %Ld %Ld %Ld\n", s->time_increment_bits, s->time_increment_resolution, s->time_base,
5748 //s->time, s->last_non_b_time, s->last_non_b_time - s->pp_time);
5749 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE
5750 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {
5751 /* rounding type for motion estimation */
5752 s->no_rounding = get_bits1(gb);
5756 //FIXME reduced res stuff
5758 if (s->shape != RECT_SHAPE) {
5759 if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
5760 int width, height, hor_spat_ref, ver_spat_ref;
5762 width = get_bits(gb, 13);
5763 skip_bits1(gb); /* marker */
5764 height = get_bits(gb, 13);
5765 skip_bits1(gb); /* marker */
5766 hor_spat_ref = get_bits(gb, 13); /* hor_spat_ref */
5767 skip_bits1(gb); /* marker */
5768 ver_spat_ref = get_bits(gb, 13); /* ver_spat_ref */
5770 skip_bits1(gb); /* change_CR_disable */
5772 if (get_bits1(gb) != 0) {
5773 skip_bits(gb, 8); /* constant_alpha_value */
5776 //FIXME complexity estimation stuff
5778 if (s->shape != BIN_ONLY_SHAPE) {
5779 s->intra_dc_threshold= mpeg4_dc_threshold[ get_bits(gb, 3) ];
5780 if(!s->progressive_sequence){
5781 s->top_field_first= get_bits1(gb);
5782 s->alternate_scan= get_bits1(gb);
5784 s->alternate_scan= 0;
5787 if(s->alternate_scan){
5788 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
5789 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
5790 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
5791 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
5793 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
5794 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
5795 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
5796 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
5799 if(s->pict_type == S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
5800 mpeg4_decode_sprite_trajectory(s, gb);
5801 if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
5802 if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
5805 if (s->shape != BIN_ONLY_SHAPE) {
5806 s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
5808 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
5809 return -1; // makes no sense to continue, as there is nothing left from the image then
5812 if (s->pict_type != I_TYPE) {
5813 s->f_code = get_bits(gb, 3); /* fcode_for */
5815 av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
5816 return -1; // makes no sense to continue, as the MV decoding will break very quickly
5821 if (s->pict_type == B_TYPE) {
5822 s->b_code = get_bits(gb, 3);
5826 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
5827 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\n",
5828 s->qscale, s->f_code, s->b_code,
5829 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
5830 gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first,
5831 s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
5832 s->sprite_warping_accuracy, 1-s->no_rounding, s->vo_type, s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold);
5835 if(!s->scalability){
5836 if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) {
5837 skip_bits1(gb); // vop shape coding type
5840 if(s->enhancement_type){
5841 int load_backward_shape= get_bits1(gb);
5842 if(load_backward_shape){
5843 av_log(s->avctx, AV_LOG_ERROR, "load backward shape isnt supported\n");
5846 skip_bits(gb, 2); //ref_select_code
5849 /* detect buggy encoders which dont set the low_delay flag (divx4/xvid/opendivx)*/
5850 // note we cannot detect divx5 without b-frames easyly (allthough its buggy too)
5851 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==0 && s->picture_number==0){
5852 av_log(s->avctx, AV_LOG_ERROR, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
5856 s->picture_number++; // better than pic number==0 allways ;)
5858 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
5859 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
5861 if(s->workaround_bugs&FF_BUG_EDGE){
5862 s->h_edge_pos= s->width;
5863 s->v_edge_pos= s->height;
5869 * decode mpeg4 headers
5870 * @return <0 if no VOP found (or a damaged one)
5871 * FRAME_SKIPPED if a not coded VOP is found
5872 * 0 if a VOP is found
5874 int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb)
5878 /* search next start code */
5882 v = get_bits(gb, 8);
5883 startcode = ((startcode << 8) | v) & 0xffffffff;
5885 if(get_bits_count(gb) >= gb->size_in_bits){
5886 if(gb->size_in_bits==8 && s->divx_version){
5887 av_log(s->avctx, AV_LOG_ERROR, "frame skip %d\n", gb->size_in_bits);
5888 return FRAME_SKIPED; //divx bug
5890 return -1; //end of stream
5893 if((startcode&0xFFFFFF00) != 0x100)
5894 continue; //no startcode
5896 if(s->avctx->debug&FF_DEBUG_STARTCODE){
5897 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
5898 if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
5899 else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
5900 else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
5901 else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
5902 else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
5903 else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
5904 else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
5905 else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
5906 else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
5907 else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
5908 else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
5909 else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
5910 else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
5911 else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
5912 else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
5913 else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
5914 else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
5915 else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
5916 else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
5917 else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Textutre Object start");
5918 else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Textutre Spatial Layer start");
5919 else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Textutre SNR Layer start");
5920 else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Textutre Tile start");
5921 else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Textutre Shape Layer start");
5922 else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
5923 else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
5924 else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
5925 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
5928 if(startcode >= 0x120 && startcode <= 0x12F){
5929 if(decode_vol_header(s, gb) < 0)
5932 else if(startcode == USER_DATA_STARTCODE){
5933 decode_user_data(s, gb);
5935 else if(startcode == GOP_STARTCODE){
5936 mpeg4_decode_gop_header(s, gb);
5938 else if(startcode == VOP_STARTCODE){
5939 return decode_vop_header(s, gb);
5947 /* don't understand why they choose a different header ! */
5948 int intel_h263_decode_picture_header(MpegEncContext *s)
5952 /* picture header */
5953 if (get_bits_long(&s->gb, 22) != 0x20) {
5954 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
5957 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
5959 if (get_bits1(&s->gb) != 1) {
5960 av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
5961 return -1; /* marker */
5963 if (get_bits1(&s->gb) != 0) {
5964 av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
5965 return -1; /* h263 id */
5967 skip_bits1(&s->gb); /* split screen off */
5968 skip_bits1(&s->gb); /* camera off */
5969 skip_bits1(&s->gb); /* freeze picture release off */
5971 format = get_bits(&s->gb, 3);
5973 av_log(s->avctx, AV_LOG_ERROR, "Intel H263 free format not supported\n");
5978 s->pict_type = I_TYPE + get_bits1(&s->gb);
5980 s->unrestricted_mv = get_bits1(&s->gb);
5981 s->h263_long_vectors = s->unrestricted_mv;
5983 if (get_bits1(&s->gb) != 0) {
5984 av_log(s->avctx, AV_LOG_ERROR, "SAC not supported\n");
5985 return -1; /* SAC: off */
5987 if (get_bits1(&s->gb) != 0) {
5989 av_log(s->avctx, AV_LOG_ERROR, "Advanced Prediction Mode not supported\n");
5990 // return -1; /* advanced prediction mode: off */
5992 if (get_bits1(&s->gb) != 0) {
5993 av_log(s->avctx, AV_LOG_ERROR, "PB frame mode no supported\n");
5994 return -1; /* PB frame mode */
5997 /* skip unknown header garbage */
5998 skip_bits(&s->gb, 41);
6000 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
6001 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
6004 while (get_bits1(&s->gb) != 0) {
6005 skip_bits(&s->gb, 8);
6009 s->y_dc_scale_table=
6010 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
6015 int flv_h263_decode_picture_header(MpegEncContext *s)
6017 int format, width, height;
6019 /* picture header */
6020 if (get_bits_long(&s->gb, 17) != 1) {
6021 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
6024 format = get_bits(&s->gb, 5);
6025 if (format != 0 && format != 1) {
6026 av_log(s->avctx, AV_LOG_ERROR, "Bad picture format\n");
6029 s->h263_flv = format+1;
6030 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
6031 format = get_bits(&s->gb, 3);
6034 width = get_bits(&s->gb, 8);
6035 height = get_bits(&s->gb, 8);
6038 width = get_bits(&s->gb, 16);
6039 height = get_bits(&s->gb, 16);
6065 if ((width == 0) || (height == 0))
6070 s->pict_type = I_TYPE + get_bits(&s->gb, 2);
6071 s->dropable= s->pict_type > P_TYPE;
6073 s->pict_type = P_TYPE;
6075 skip_bits1(&s->gb); /* deblocking flag */
6076 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
6080 s->unrestricted_mv = 1;
6081 s->h263_long_vectors = 0;
6084 while (get_bits1(&s->gb) != 0) {
6085 skip_bits(&s->gb, 8);
6089 if(s->avctx->debug & FF_DEBUG_PICT_INFO){
6090 av_log(s->avctx, AV_LOG_DEBUG, "%c esc_type:%d, qp:%d num:%d\n",
6091 av_get_pict_type_char(s->pict_type), s->h263_flv-1, s->qscale, s->picture_number);
6094 s->y_dc_scale_table=
6095 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;