2 * H263/MPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * Copyright (c) 2001 Juan J. Sierralta P.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ac prediction encoding & b-frame support by Michael Niedermayer <michaelni@gmx.at>
28 #include "mpegvideo.h"
30 #include "mpeg4data.h"
32 //rounded divison & shift
33 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
35 #define PRINT_MB_TYPE(a) {}
36 //#define PRINT_MB_TYPE(a) printf(a)
38 #define INTRA_MCBPC_VLC_BITS 6
39 #define INTER_MCBPC_VLC_BITS 6
40 #define CBPY_VLC_BITS 6
43 #define SPRITE_TRAJ_VLC_BITS 6
44 #define MB_TYPE_B_VLC_BITS 4
45 #define TEX_VLC_BITS 9
47 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
49 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
50 static void h263p_encode_umotion(MpegEncContext * s, int val);
51 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
52 int n, int dc, UINT8 *scan_table,
53 PutBitContext *dc_pb, PutBitContext *ac_pb);
54 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
55 static int h263p_decode_umotion(MpegEncContext * s, int pred);
56 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
58 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
59 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
60 int n, int coded, int intra);
61 static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr);
62 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
64 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
66 extern UINT32 inverse[256];
68 static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
69 static UINT8 fcode_tab[MAX_MV*2+1];
70 static UINT8 umv_fcode_tab[MAX_MV*2+1];
72 static UINT16 uni_DCtab_lum [512][2];
73 static UINT16 uni_DCtab_chrom[512][2];
74 static UINT32 uni_mpeg4_intra_rl_bits[64*64*2*2];
75 static UINT8 uni_mpeg4_intra_rl_len [64*64*2*2];
76 static UINT32 uni_mpeg4_inter_rl_bits[64*64*2*2];
77 static UINT8 uni_mpeg4_inter_rl_len [64*64*2*2];
78 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
79 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
91 int h263_get_picture_format(int width, int height)
95 if (width == 128 && height == 96)
97 else if (width == 176 && height == 144)
99 else if (width == 352 && height == 288)
101 else if (width == 704 && height == 576)
103 else if (width == 1408 && height == 1152)
110 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
114 align_put_bits(&s->pb);
116 /* Update the pointer to last GOB */
117 s->ptr_lastgob = pbBufPtr(&s->pb);
120 put_bits(&s->pb, 22, 0x20); /* PSC */
121 put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) /
122 s->frame_rate) & 0xff);
124 put_bits(&s->pb, 1, 1); /* marker */
125 put_bits(&s->pb, 1, 0); /* h263 id */
126 put_bits(&s->pb, 1, 0); /* split screen off */
127 put_bits(&s->pb, 1, 0); /* camera off */
128 put_bits(&s->pb, 1, 0); /* freeze picture release off */
130 format = h263_get_picture_format(s->width, s->height);
133 put_bits(&s->pb, 3, format);
134 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
135 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
136 of H.263v1 UMV implies to check the predicted MV after
137 calculation of the current MB to see if we're on the limits */
138 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
139 put_bits(&s->pb, 1, 0); /* SAC: off */
140 put_bits(&s->pb, 1, 0); /* advanced prediction mode: off */
141 put_bits(&s->pb, 1, 0); /* not PB frame */
142 put_bits(&s->pb, 5, s->qscale);
143 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
146 /* H.263 Plus PTYPE */
147 put_bits(&s->pb, 3, 7);
148 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
150 put_bits(&s->pb,3,6); /* Custom Source Format */
152 put_bits(&s->pb, 3, format);
154 put_bits(&s->pb,1,0); /* Custom PCF: off */
155 s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
156 put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
157 put_bits(&s->pb,1,0); /* SAC: off */
158 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
159 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
160 put_bits(&s->pb,1,0); /* Deblocking Filter: off */
161 put_bits(&s->pb,1,0); /* Slice Structured: off */
162 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
163 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
164 put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
165 put_bits(&s->pb,1,0); /* Modified Quantization: off */
166 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
167 put_bits(&s->pb,3,0); /* Reserved */
169 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
171 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
172 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
173 if (s->pict_type == I_TYPE)
177 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
178 put_bits(&s->pb,2,0); /* Reserved */
179 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
181 /* This should be here if PLUSPTYPE */
182 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
185 /* Custom Picture Format (CPFMT) */
187 if (s->aspect_ratio_info)
188 put_bits(&s->pb,4,s->aspect_ratio_info);
190 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
191 put_bits(&s->pb,9,(s->width >> 2) - 1);
192 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
193 put_bits(&s->pb,9,(s->height >> 2));
196 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
198 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
199 put_bits(&s->pb, 5, s->qscale);
202 put_bits(&s->pb, 1, 0); /* no PEI */
206 s->c_dc_scale_table= h263_aic_dc_scale_table;
209 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
213 int h263_encode_gob_header(MpegEncContext * s, int mb_line)
217 /* Check to see if we need to put a new GBSC */
218 /* for RTP packetization */
220 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
221 if (pdif >= s->rtp_payload_size) {
222 /* Bad luck, packet must be cut before */
223 align_put_bits(&s->pb);
224 flush_put_bits(&s->pb);
225 /* Call the RTP callback to send the last GOB */
226 if (s->rtp_callback) {
227 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
228 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
230 s->ptr_lastgob = pbBufPtr(&s->pb);
231 put_bits(&s->pb, 17, 1); /* GBSC */
232 s->gob_number = mb_line / s->gob_index;
233 put_bits(&s->pb, 5, s->gob_number); /* GN */
234 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
235 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
236 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
238 } else if (pdif + s->mb_line_avgsize >= s->rtp_payload_size) {
239 /* Cut the packet before we can't */
240 align_put_bits(&s->pb);
241 flush_put_bits(&s->pb);
242 /* Call the RTP callback to send the last GOB */
243 if (s->rtp_callback) {
244 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
245 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
247 s->ptr_lastgob = pbBufPtr(&s->pb);
248 put_bits(&s->pb, 17, 1); /* GBSC */
249 s->gob_number = mb_line / s->gob_index;
250 put_bits(&s->pb, 5, s->gob_number); /* GN */
251 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
252 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
253 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
260 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6])
262 int score0=0, score1=0;
266 INT16 *ac_val, *ac_val1;
268 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
271 ac_val-= s->block_wrap[n]*16;
273 const int level= block[n][block_permute_op(i )];
275 score1+= ABS(level - ac_val[i+8]);
276 ac_val1[i ]= block[n][block_permute_op(i<<3)];
282 const int level= block[n][block_permute_op(i<<3)];
284 score1+= ABS(level - ac_val[i]);
286 ac_val1[i+8]= block[n][block_permute_op(i )];
291 return score0 > score1 ? 1 : 0;
294 void mpeg4_encode_mb(MpegEncContext * s,
295 DCTELEM block[6][64],
296 int motion_x, int motion_y)
298 int cbpc, cbpy, i, pred_x, pred_y;
300 PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
301 PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
302 PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2 : &s->pb;
303 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
305 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
309 for (i = 0; i < 6; i++) {
310 if (s->block_last_index[i] >= 0)
314 if(s->pict_type==B_TYPE){
315 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
316 int mb_type= mb_type_table[s->mv_dir];
322 s->last_mv[1][0][1]= 0;
325 /* nothing to do if this MB was skiped in the next P Frame */
326 if(s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]){
332 s->mv_dir= MV_DIR_FORWARD; //doesnt matter
336 if ((cbp | motion_x | motion_y | mb_type) ==0) {
337 /* direct MB with MV={0,0} */
338 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
340 if(interleaved_stats){
347 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
348 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
349 put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
350 if(cbp) put_bits(&s->pb, 6, cbp);
353 put_bits(&s->pb, 1, 0); /* no q-scale change */
355 if(interleaved_stats){
356 bits= get_bit_count(&s->pb);
357 s->misc_bits+= bits - s->last_bits;
364 h263_encode_motion(s, motion_x, 1);
365 h263_encode_motion(s, motion_y, 1);
368 h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
369 h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
370 h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
371 h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
372 s->last_mv[0][0][0]= s->mv[0][0][0];
373 s->last_mv[0][0][1]= s->mv[0][0][1];
374 s->last_mv[1][0][0]= s->mv[1][0][0];
375 s->last_mv[1][0][1]= s->mv[1][0][1];
377 case 2: /* backward */
378 h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
379 h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
380 s->last_mv[1][0][0]= motion_x;
381 s->last_mv[1][0][1]= motion_y;
383 case 3: /* forward */
384 h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
385 h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
386 s->last_mv[0][0][0]= motion_x;
387 s->last_mv[0][0][1]= motion_y;
390 printf("unknown mb type\n");
394 if(interleaved_stats){
395 bits= get_bit_count(&s->pb);
396 s->mv_bits+= bits - s->last_bits;
400 /* encode each block */
401 for (i = 0; i < 6; i++) {
402 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct, NULL, &s->pb);
405 if(interleaved_stats){
406 bits= get_bit_count(&s->pb);
407 s->p_tex_bits+= bits - s->last_bits;
410 }else{ /* s->pict_type==B_TYPE */
411 if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) {
412 /* check if the B frames can skip it too, as we must skip it if we skip here
413 why didnt they just compress the skip-mb bits instead of reusing them ?! */
414 if(s->max_b_frames>0){
421 if(x+16 > s->width) x= s->width-16;
422 if(y+16 > s->height) y= s->height-16;
424 offset= x + y*s->linesize;
425 p_pic= s->new_picture[0] + offset;
428 for(i=0; i<s->max_b_frames; i++){
432 if(s->coded_order[i+1].pict_type!=B_TYPE) break;
434 b_pic= s->coded_order[i+1].picture[0] + offset;
435 diff= pix_abs16x16(p_pic, b_pic, s->linesize);
436 if(diff>s->qscale*70){ //FIXME check that 70 is optimal
445 /* skip macroblock */
446 put_bits(&s->pb, 1, 1);
448 if(interleaved_stats){
457 put_bits(&s->pb, 1, 0); /* mb coded */
458 if(s->mv_type==MV_TYPE_16X16){
461 inter_MCBPC_bits[cbpc],
462 inter_MCBPC_code[cbpc]);
465 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
467 if(interleaved_stats){
468 bits= get_bit_count(&s->pb);
469 s->misc_bits+= bits - s->last_bits;
473 /* motion vectors: 16x16 mode */
474 h263_pred_motion(s, 0, &pred_x, &pred_y);
476 h263_encode_motion(s, motion_x - pred_x, s->f_code);
477 h263_encode_motion(s, motion_y - pred_y, s->f_code);
481 inter_MCBPC_bits[cbpc],
482 inter_MCBPC_code[cbpc]);
485 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
487 if(interleaved_stats){
488 bits= get_bit_count(&s->pb);
489 s->misc_bits+= bits - s->last_bits;
494 /* motion vectors: 8x8 mode*/
495 h263_pred_motion(s, i, &pred_x, &pred_y);
497 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);
498 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);
502 if(interleaved_stats){
503 bits= get_bit_count(&s->pb);
504 s->mv_bits+= bits - s->last_bits;
508 /* encode each block */
509 for (i = 0; i < 6; i++) {
510 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct, NULL, tex_pb);
513 if(interleaved_stats){
514 bits= get_bit_count(&s->pb);
515 s->p_tex_bits+= bits - s->last_bits;
522 int dc_diff[6]; //dc values with the dc prediction subtracted
523 int dir[6]; //prediction direction
524 int zigzag_last_index[6];
525 UINT8 *scan_table[6];
528 const int level= block[i][0];
531 dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
533 *dc_ptr = level * s->y_dc_scale;
535 *dc_ptr = level * s->c_dc_scale;
539 s->ac_pred= decide_ac_pred(s, block, dir);
546 mpeg4_inv_pred_ac(s, block[i], i, dir[i]);
547 if (dir[i]==0) st = ff_alternate_vertical_scan; /* left */
548 else st = ff_alternate_horizontal_scan; /* top */
550 for(last_index=63; last_index>=0; last_index--) //FIXME optimize
551 if(block[i][st[last_index]]) break;
552 zigzag_last_index[i]= s->block_last_index[i];
553 s->block_last_index[i]= last_index;
558 scan_table[i]= zigzag_direct;
563 for (i = 0; i < 6; i++) {
564 if (s->block_last_index[i] >= 1)
569 if (s->pict_type == I_TYPE) {
571 intra_MCBPC_bits[cbpc],
572 intra_MCBPC_code[cbpc]);
574 put_bits(&s->pb, 1, 0); /* mb coded */
576 inter_MCBPC_bits[cbpc + 4],
577 inter_MCBPC_code[cbpc + 4]);
579 put_bits(pb2, 1, s->ac_pred);
581 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
583 if(interleaved_stats){
584 bits= get_bit_count(&s->pb);
585 s->misc_bits+= bits - s->last_bits;
589 /* encode each block */
590 for (i = 0; i < 6; i++) {
591 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
594 if(interleaved_stats){
595 bits= get_bit_count(&s->pb);
596 s->i_tex_bits+= bits - s->last_bits;
601 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
607 ac_val = s->ac_val[0][0] + s->block_index[i] * 16;
611 block[i][block_permute_op(j )]= ac_val[j+8];
614 block[i][block_permute_op(j<<3)]= ac_val[j ];
616 s->block_last_index[i]= zigzag_last_index[i];
622 void h263_encode_mb(MpegEncContext * s,
623 DCTELEM block[6][64],
624 int motion_x, int motion_y)
626 int cbpc, cbpy, i, cbp, pred_x, pred_y;
628 INT16 rec_intradc[6];
631 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
635 for (i = 0; i < 6; i++) {
636 if (s->block_last_index[i] >= 0)
639 if ((cbp | motion_x | motion_y) == 0) {
640 /* skip macroblock */
641 put_bits(&s->pb, 1, 1);
644 put_bits(&s->pb, 1, 0); /* mb coded */
647 inter_MCBPC_bits[cbpc],
648 inter_MCBPC_code[cbpc]);
651 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
653 /* motion vectors: 16x16 mode only now */
654 h263_pred_motion(s, 0, &pred_x, &pred_y);
657 h263_encode_motion(s, motion_x - pred_x, s->f_code);
658 h263_encode_motion(s, motion_y - pred_y, s->f_code);
661 h263p_encode_umotion(s, motion_x - pred_x);
662 h263p_encode_umotion(s, motion_y - pred_y);
663 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
664 /* To prevent Start Code emulation */
665 put_bits(&s->pb,1,1);
668 int li = s->h263_aic ? 0 : 1;
673 if (s->h263_aic && s->mb_intra) {
674 INT16 level = block[i][0];
676 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
680 level = (level + (s->qscale >> 1))/(s->y_dc_scale);
682 level = (level - (s->qscale >> 1))/(s->y_dc_scale);
684 /* AIC can change CBP */
685 if (level == 0 && s->block_last_index[i] == 0)
686 s->block_last_index[i] = -1;
687 else if (level < -127)
689 else if (level > 127)
694 rec_intradc[i] = (s->y_dc_scale*level) + pred_dc;
697 //if ((rec_intradc[i] % 2) == 0)
700 if (rec_intradc[i] < 0)
702 else if (rec_intradc[i] > 2047)
703 rec_intradc[i] = 2047;
705 /* Update AC/DC tables */
706 *dc_ptr[i] = rec_intradc[i];
709 if (s->block_last_index[i] >= li)
714 if (s->pict_type == I_TYPE) {
716 intra_MCBPC_bits[cbpc],
717 intra_MCBPC_code[cbpc]);
719 put_bits(&s->pb, 1, 0); /* mb coded */
721 inter_MCBPC_bits[cbpc + 4],
722 inter_MCBPC_code[cbpc + 4]);
725 /* XXX: currently, we do not try to use ac prediction */
726 put_bits(&s->pb, 1, 0); /* no AC prediction */
729 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
733 /* encode each block */
734 h263_encode_block(s, block[i], i);
736 /* Update INTRADC for decoding */
737 if (s->h263_aic && s->mb_intra) {
738 block[i][0] = rec_intradc[i];
744 static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr)
746 int x, y, wrap, a, c, pred_dc, scale;
747 INT16 *dc_val, *ac_val;
749 /* find prediction */
751 x = 2 * s->mb_x + 1 + (n & 1);
752 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
753 wrap = s->mb_width * 2 + 2;
754 dc_val = s->dc_val[0];
755 ac_val = s->ac_val[0][0];
756 scale = s->y_dc_scale;
760 wrap = s->mb_width + 2;
761 dc_val = s->dc_val[n - 4 + 1];
762 ac_val = s->ac_val[n - 4 + 1][0];
763 scale = s->c_dc_scale;
768 a = dc_val[(x - 1) + (y) * wrap];
769 c = dc_val[(x) + (y - 1) * wrap];
771 /* No prediction outside GOB boundary */
772 if (s->first_slice_line && ((n < 2) || (n > 3)))
775 /* just DC prediction */
776 if (a != 1024 && c != 1024)
777 pred_dc = (a + c) >> 1;
783 /* we assume pred is positive */
784 //pred_dc = (pred_dc + (scale >> 1)) / scale;
785 *dc_val_ptr = &dc_val[x + y * wrap];
790 void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n)
792 int x, y, wrap, a, c, pred_dc, scale, i;
793 INT16 *dc_val, *ac_val, *ac_val1;
795 /* find prediction */
797 x = 2 * s->mb_x + 1 + (n & 1);
798 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
799 wrap = s->mb_width * 2 + 2;
800 dc_val = s->dc_val[0];
801 ac_val = s->ac_val[0][0];
802 scale = s->y_dc_scale;
806 wrap = s->mb_width + 2;
807 dc_val = s->dc_val[n - 4 + 1];
808 ac_val = s->ac_val[n - 4 + 1][0];
809 scale = s->c_dc_scale;
812 ac_val += ((y) * wrap + (x)) * 16;
818 a = dc_val[(x - 1) + (y) * wrap];
819 c = dc_val[(x) + (y - 1) * wrap];
821 /* No prediction outside GOB boundary */
822 if (s->first_slice_line && ((n < 2) || (n > 3)))
826 if (s->h263_aic_dir) {
827 /* left prediction */
831 block[block_permute_op(i*8)] += ac_val[i];
840 block[block_permute_op(i)] += ac_val[i + 8];
846 /* just DC prediction */
847 if (a != 1024 && c != 1024)
848 pred_dc = (a + c) >> 1;
855 /* we assume pred is positive */
856 block[0]=block[0]*scale + pred_dc;
860 else if (!(block[0] & 1))
863 /* Update AC/DC tables */
864 dc_val[(x) + (y) * wrap] = block[0];
868 ac_val1[i] = block[block_permute_op(i * 8)];
871 ac_val1[8 + i] = block[block_permute_op(i)];
874 INT16 *h263_pred_motion(MpegEncContext * s, int block,
878 INT16 *A, *B, *C, *mot_val;
879 static const int off[4]= {2, 1, 1, -1};
881 wrap = s->block_wrap[0];
882 xy = s->block_index[block];
884 mot_val = s->motion_val[xy];
886 A = s->motion_val[xy - 1];
887 /* special case for first (slice) line */
888 if ((s->mb_y == 0 || s->first_slice_line) && block<3) {
889 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
890 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
891 if(block==0){ //most common case
892 if(s->mb_x == s->resync_mb_x){ //rare
894 }else if(s->mb_x + 1 == s->resync_mb_x){ //rare
895 C = s->motion_val[xy + off[block] - wrap];
900 *px = mid_pred(A[0], 0, C[0]);
901 *py = mid_pred(A[1], 0, C[1]);
908 if(s->mb_x + 1 == s->resync_mb_x){ //rare
909 C = s->motion_val[xy + off[block] - wrap];
910 *px = mid_pred(A[0], 0, C[0]);
911 *py = mid_pred(A[1], 0, C[1]);
917 B = s->motion_val[xy - wrap];
918 C = s->motion_val[xy + off[block] - wrap];
919 if(s->mb_x == s->resync_mb_x) //rare
922 *px = mid_pred(A[0], B[0], C[0]);
923 *py = mid_pred(A[1], B[1], C[1]);
926 B = s->motion_val[xy - wrap];
927 C = s->motion_val[xy + off[block] - wrap];
928 *px = mid_pred(A[0], B[0], C[0]);
929 *py = mid_pred(A[1], B[1], C[1]);
934 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
936 int range, l, m, bit_size, sign, code, bits;
941 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
943 bit_size = f_code - 1;
944 range = 1 << bit_size;
945 /* modulo encoding */
950 } else if (val >= l) {
961 code = (val >> bit_size) + 1;
962 bits = val & (range - 1);
964 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
966 put_bits(&s->pb, bit_size, bits);
971 /* Encode MV differences on H.263+ with Unrestricted MV mode */
972 static void h263p_encode_umotion(MpegEncContext * s, int val)
982 put_bits(&s->pb, 1, 1);
984 put_bits(&s->pb, 3, 0);
986 put_bits(&s->pb, 3, 2);
989 sval = ((val < 0) ? (short)(-val):(short)val);
992 while (temp_val != 0) {
993 temp_val = temp_val >> 1;
999 tcode = (sval & (1 << (i-1))) >> (i-1);
1000 tcode = (tcode << 1) | 1;
1001 code = (code << 2) | tcode;
1004 code = ((code << 1) | (val < 0)) << 1;
1005 put_bits(&s->pb, (2*n_bits)+1, code);
1006 //printf("\nVal = %d\tCode = %d", sval, code);
1010 static void init_mv_penalty_and_fcode(MpegEncContext *s)
1014 for(f_code=1; f_code<=MAX_FCODE; f_code++){
1015 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1018 if(mv==0) len= mvtab[0][1];
1020 int val, bit_size, range, code;
1022 bit_size = s->f_code - 1;
1023 range = 1 << bit_size;
1029 code = (val >> bit_size) + 1;
1031 len= mvtab[code][1] + 1 + bit_size;
1033 len= mvtab[32][1] + 2 + bit_size;
1037 mv_penalty[f_code][mv+MAX_MV]= len;
1041 for(f_code=MAX_FCODE; f_code>0; f_code--){
1042 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1043 fcode_tab[mv+MAX_MV]= f_code;
1047 for(mv=0; mv<MAX_MV*2+1; mv++){
1048 umv_fcode_tab[mv]= 1;
1052 static void init_uni_dc_tab(void)
1054 int level, uni_code, uni_len;
1056 for(level=-256; level<256; level++){
1058 /* find number of bits */
1067 l= (-level) ^ ((1 << size) - 1);
1072 uni_code= DCtab_lum[size][0];
1073 uni_len = DCtab_lum[size][1];
1076 uni_code<<=size; uni_code|=l;
1079 uni_code<<=1; uni_code|=1;
1083 uni_DCtab_lum[level+256][0]= uni_code;
1084 uni_DCtab_lum[level+256][1]= uni_len;
1087 uni_code= DCtab_chrom[size][0];
1088 uni_len = DCtab_chrom[size][1];
1091 uni_code<<=size; uni_code|=l;
1094 uni_code<<=1; uni_code|=1;
1098 uni_DCtab_chrom[level+256][0]= uni_code;
1099 uni_DCtab_chrom[level+256][1]= uni_len;
1104 static void init_uni_mpeg4_rl_tab(RLTable *rl, UINT32 *bits_tab, UINT8 *len_tab){
1105 int slevel, run, last;
1107 assert(MAX_LEVEL >= 64);
1108 assert(MAX_RUN >= 63);
1110 for(slevel=-64; slevel<64; slevel++){
1111 if(slevel==0) continue;
1112 for(run=0; run<64; run++){
1113 for(last=0; last<=1; last++){
1114 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1115 int level= slevel < 0 ? -slevel : slevel;
1116 int sign= slevel < 0 ? 1 : 0;
1117 int bits, len, code;
1120 len_tab[index]= 100;
1123 code= get_rl_index(rl, last, run, level);
1124 bits= rl->table_vlc[code][0];
1125 len= rl->table_vlc[code][1];
1126 bits=bits*2+sign; len++;
1128 if(code!=rl->n && len < len_tab[index]){
1129 bits_tab[index]= bits;
1130 len_tab [index]= len;
1134 bits= rl->table_vlc[rl->n][0];
1135 len= rl->table_vlc[rl->n][1];
1136 bits=bits*2; len++; //esc1
1137 level1= level - rl->max_level[last][run];
1139 code= get_rl_index(rl, last, run, level1);
1140 bits<<= rl->table_vlc[code][1];
1141 len += rl->table_vlc[code][1];
1142 bits += rl->table_vlc[code][0];
1143 bits=bits*2+sign; len++;
1145 if(code!=rl->n && len < len_tab[index]){
1146 bits_tab[index]= bits;
1147 len_tab [index]= len;
1153 bits= rl->table_vlc[rl->n][0];
1154 len= rl->table_vlc[rl->n][1];
1155 bits=bits*4+2; len+=2; //esc2
1156 run1 = run - rl->max_run[last][level] - 1;
1158 code= get_rl_index(rl, last, run1, level);
1159 bits<<= rl->table_vlc[code][1];
1160 len += rl->table_vlc[code][1];
1161 bits += rl->table_vlc[code][0];
1162 bits=bits*2+sign; len++;
1164 if(code!=rl->n && len < len_tab[index]){
1165 bits_tab[index]= bits;
1166 len_tab [index]= len;
1171 bits= rl->table_vlc[rl->n][0];
1172 len = rl->table_vlc[rl->n][1];
1173 bits=bits*4+3; len+=2; //esc3
1174 bits=bits*2+last; len++;
1175 bits=bits*64+run; len+=6;
1176 bits=bits*2+1; len++; //marker
1177 bits=bits*4096+(slevel&0xfff); len+=12;
1178 bits=bits*2+1; len++; //marker
1180 if(len < len_tab[index]){
1181 bits_tab[index]= bits;
1182 len_tab [index]= len;
1189 void h263_encode_init(MpegEncContext *s)
1191 static int done = 0;
1200 init_rl(&rl_intra_aic);
1202 init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1203 init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1205 init_mv_penalty_and_fcode(s);
1207 s->mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1209 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
1210 switch(s->codec_id){
1211 case CODEC_ID_MPEG4:
1212 s->fcode_tab= fcode_tab;
1213 s->min_qcoeff= -2048;
1214 s->max_qcoeff= 2047;
1216 case CODEC_ID_H263P:
1217 s->fcode_tab= umv_fcode_tab;
1218 s->min_qcoeff= -128;
1221 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
1222 default: //nothing needed default table allready set in mpegvideo.c
1223 s->min_qcoeff= -128;
1225 s->y_dc_scale_table=
1226 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1230 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
1231 s->inter_quant_bias= 0;
1233 s->intra_quant_bias=0;
1234 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
1238 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1240 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1244 if (s->mb_intra && !s->h263_aic) {
1247 /* 255 cannot be represented, so we clamp */
1252 /* 0 cannot be represented also */
1258 put_bits(&s->pb, 8, 0xff);
1260 put_bits(&s->pb, 8, level & 0xff);
1264 if (s->h263_aic && s->mb_intra)
1269 last_index = s->block_last_index[n];
1270 last_non_zero = i - 1;
1271 for (; i <= last_index; i++) {
1272 j = zigzag_direct[i];
1275 run = i - last_non_zero - 1;
1276 last = (i == last_index);
1283 code = get_rl_index(rl, last, run, level);
1284 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1285 if (code == rl->n) {
1286 put_bits(&s->pb, 1, last);
1287 put_bits(&s->pb, 6, run);
1288 put_bits(&s->pb, 8, slevel & 0xff);
1290 put_bits(&s->pb, 1, sign);
1297 /***************************************************/
1299 void ff_mpeg4_stuffing(PutBitContext * pbc)
1302 put_bits(pbc, 1, 0);
1303 length= (-get_bit_count(pbc))&7;
1304 if(length) put_bits(pbc, length, (1<<length)-1);
1307 /* must be called before writing the header */
1308 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
1309 int time_div, time_mod;
1311 if(s->pict_type==I_TYPE){ //we will encode a vol header
1312 s->time_increment_resolution= s->frame_rate/ff_gcd(s->frame_rate, FRAME_RATE_BASE);
1313 if(s->time_increment_resolution>=256*256) s->time_increment_resolution= 256*128;
1315 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
1318 s->time= picture_number*(INT64)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate;
1319 time_div= s->time/s->time_increment_resolution;
1320 time_mod= s->time%s->time_increment_resolution;
1322 if(s->pict_type==B_TYPE){
1323 s->bp_time= s->last_non_b_time - s->time;
1325 s->last_time_base= s->time_base;
1326 s->time_base= time_div;
1327 s->pp_time= s->time - s->last_non_b_time;
1328 s->last_non_b_time= s->time;
1332 static void mpeg4_encode_vol_header(MpegEncContext * s)
1334 int vo_ver_id=1; //must be 2 if we want GMC or q-pel
1337 s->vo_type= s->has_b_frames ? CORE_VO_TYPE : SIMPLE_VO_TYPE;
1339 put_bits(&s->pb, 16, 0);
1340 put_bits(&s->pb, 16, 0x100); /* video obj */
1341 put_bits(&s->pb, 16, 0);
1342 put_bits(&s->pb, 16, 0x120); /* video obj layer */
1344 put_bits(&s->pb, 1, 0); /* random access vol */
1345 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
1346 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
1347 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
1348 put_bits(&s->pb, 3, 1); /* is obj layer priority */
1349 if(s->aspect_ratio_info)
1350 put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
1352 put_bits(&s->pb, 4, 1); /* aspect ratio info= sqare pixel */
1355 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1356 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1357 put_bits(&s->pb, 1, s->low_delay);
1358 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
1360 put_bits(&s->pb, 1, 0); /* vol control parameters= no */
1363 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1364 put_bits(&s->pb, 1, 1); /* marker bit */
1366 put_bits(&s->pb, 16, s->time_increment_resolution);
1367 if (s->time_increment_bits < 1)
1368 s->time_increment_bits = 1;
1369 put_bits(&s->pb, 1, 1); /* marker bit */
1370 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
1371 put_bits(&s->pb, 1, 1); /* marker bit */
1372 put_bits(&s->pb, 13, s->width); /* vol width */
1373 put_bits(&s->pb, 1, 1); /* marker bit */
1374 put_bits(&s->pb, 13, s->height); /* vol height */
1375 put_bits(&s->pb, 1, 1); /* marker bit */
1376 put_bits(&s->pb, 1, 0); /* interlace */
1377 put_bits(&s->pb, 1, 1); /* obmc disable */
1378 if (vo_ver_id == 1) {
1379 put_bits(&s->pb, 1, s->vol_sprite_usage=0); /* sprite enable */
1380 }else{ /* vo_ver_id == 2 */
1381 put_bits(&s->pb, 2, s->vol_sprite_usage=0); /* sprite enable */
1383 put_bits(&s->pb, 1, 0); /* not 8 bit */
1384 put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
1385 if(s->mpeg_quant) put_bits(&s->pb, 2, 0); /* no custom matrixes */
1388 put_bits(&s->pb, 1, s->quarter_sample=0);
1389 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
1390 s->resync_marker= s->rtp_mode;
1391 put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
1392 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1393 if(s->data_partitioning){
1394 put_bits(&s->pb, 1, 0); /* no rvlc */
1397 if (vo_ver_id != 1){
1398 put_bits(&s->pb, 1, 0); /* newpred */
1399 put_bits(&s->pb, 1, 0); /* reduced res vop */
1401 put_bits(&s->pb, 1, 0); /* scalability */
1403 ff_mpeg4_stuffing(&s->pb);
1404 put_bits(&s->pb, 16, 0);
1405 put_bits(&s->pb, 16, 0x1B2); /* user_data */
1406 sprintf(buf, "FFmpeg%sb%s", FFMPEG_VERSION, LIBAVCODEC_BUILD_STR);
1407 put_string(&s->pb, buf);
1409 ff_mpeg4_stuffing(&s->pb);
1412 /* write mpeg4 VOP header */
1413 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1416 int time_div, time_mod;
1418 if(s->pict_type==I_TYPE){
1420 if(picture_number==0 || !s->strict_std_compliance)
1421 mpeg4_encode_vol_header(s);
1424 //printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
1426 put_bits(&s->pb, 16, 0); /* vop header */
1427 put_bits(&s->pb, 16, 0x1B6); /* vop header */
1428 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
1430 time_div= s->time/s->time_increment_resolution;
1431 time_mod= s->time%s->time_increment_resolution;
1432 time_incr= time_div - s->last_time_base;
1434 put_bits(&s->pb, 1, 1);
1436 put_bits(&s->pb, 1, 0);
1438 put_bits(&s->pb, 1, 1); /* marker */
1439 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1440 put_bits(&s->pb, 1, 1); /* marker */
1441 put_bits(&s->pb, 1, 1); /* vop coded */
1442 if ( s->pict_type == P_TYPE
1443 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
1444 s->no_rounding ^= 1;
1445 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1447 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1448 //FIXME sprite stuff
1450 put_bits(&s->pb, 5, s->qscale);
1452 if (s->pict_type != I_TYPE)
1453 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1454 if (s->pict_type == B_TYPE)
1455 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1456 // printf("****frame %d\n", picture_number);
1458 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
1459 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1460 s->h_edge_pos= s->width;
1461 s->v_edge_pos= s->height;
1464 static void h263_dc_scale(MpegEncContext * s)
1466 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
1467 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
1470 inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
1472 int a, b, c, wrap, pred, scale;
1476 /* find prediction */
1478 scale = s->y_dc_scale;
1480 scale = s->c_dc_scale;
1482 wrap= s->block_wrap[n];
1483 dc_val = s->dc_val[0] + s->block_index[n];
1489 b = dc_val[ - 1 - wrap];
1490 c = dc_val[ - wrap];
1492 if (abs(a - b) < abs(b - c)) {
1494 *dir_ptr = 1; /* top */
1497 *dir_ptr = 0; /* left */
1499 /* we assume pred is positive */
1502 "xorl %%edx, %%edx \n\t"
1504 : "=d" (pred), "=a"(dummy)
1505 : "a" (pred + (scale >> 1)), "c" (inverse[scale])
1508 pred = (pred + (scale >> 1)) / scale;
1511 /* prepare address for prediction update */
1512 *dc_val_ptr = &dc_val[0];
1517 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
1521 INT16 *ac_val, *ac_val1;
1523 /* find prediction */
1524 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1528 const int xy= s->mb_x-1 + s->mb_y*s->mb_width;
1529 /* left prediction */
1532 if(s->mb_x==0 || s->qscale == s->qscale_table[xy] || n==1 || n==3){
1535 block[block_permute_op(i*8)] += ac_val[i];
1538 /* different qscale, we must rescale */
1540 block[block_permute_op(i*8)] += ROUNDED_DIV(ac_val[i]*s->qscale_table[xy], s->qscale);
1544 const int xy= s->mb_x + s->mb_y*s->mb_width - s->mb_width;
1545 /* top prediction */
1546 ac_val -= 16 * s->block_wrap[n];
1548 if(s->mb_y==0 || s->qscale == s->qscale_table[xy] || n==2 || n==3){
1551 block[block_permute_op(i)] += ac_val[i + 8];
1554 /* different qscale, we must rescale */
1556 block[block_permute_op(i)] += ROUNDED_DIV(ac_val[i + 8]*s->qscale_table[xy], s->qscale);
1563 ac_val1[i] = block[block_permute_op(i * 8)];
1567 ac_val1[8 + i] = block[block_permute_op(i)];
1571 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
1577 /* find prediction */
1578 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1581 /* left prediction */
1584 block[block_permute_op(i*8)] -= ac_val[i];
1587 /* top prediction */
1588 ac_val -= 16 * s->block_wrap[n];
1590 block[block_permute_op(i)] -= ac_val[i + 8];
1595 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
1598 // if(level<-255 || level>255) printf("dc overflow\n");
1602 put_bits(s, uni_DCtab_lum[level][1], uni_DCtab_lum[level][0]);
1605 put_bits(s, uni_DCtab_chrom[level][1], uni_DCtab_chrom[level][0]);
1609 /* find number of bits */
1619 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
1622 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
1625 /* encode remaining bits */
1628 level = (-level) ^ ((1 << size) - 1);
1629 put_bits(&s->pb, size, level);
1631 put_bits(&s->pb, 1, 1);
1636 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
1637 UINT8 *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
1639 int last, i, last_non_zero, sign;
1644 const int last_index = s->block_last_index[n];
1646 if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
1647 /* mpeg4 based DC predictor */
1648 mpeg4_encode_dc(dc_pb, intra_dc, n);
1649 if(last_index<1) return;
1652 bits_tab= uni_mpeg4_intra_rl_bits;
1653 len_tab = uni_mpeg4_intra_rl_len;
1655 if(last_index<0) return;
1658 bits_tab= uni_mpeg4_inter_rl_bits;
1659 len_tab = uni_mpeg4_inter_rl_len;
1663 last_non_zero = i - 1;
1665 for (; i < last_index; i++) {
1666 int level = block[ scan_table[i] ];
1668 int run = i - last_non_zero - 1;
1670 if((level&(~127)) == 0){
1671 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
1672 put_bits(ac_pb, len_tab[index], bits_tab[index]);
1674 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);
1679 /*if(i<=last_index)*/{
1680 int level = block[ scan_table[i] ];
1681 int run = i - last_non_zero - 1;
1683 if((level&(~127)) == 0){
1684 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
1685 put_bits(ac_pb, len_tab[index], bits_tab[index]);
1687 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);
1691 for (; i <= last_index; i++) {
1692 const int slevel = block[ scan_table[i] ];
1695 int run = i - last_non_zero - 1;
1696 last = (i == last_index);
1703 code = get_rl_index(rl, last, run, level);
1704 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1705 if (code == rl->n) {
1707 level1 = level - rl->max_level[last][run];
1710 code = get_rl_index(rl, last, run, level1);
1711 if (code == rl->n) {
1713 put_bits(ac_pb, 1, 1);
1714 if (level > MAX_LEVEL)
1716 run1 = run - rl->max_run[last][level] - 1;
1719 code = get_rl_index(rl, last, run1, level);
1720 if (code == rl->n) {
1723 put_bits(ac_pb, 1, 1);
1724 put_bits(ac_pb, 1, last);
1725 put_bits(ac_pb, 6, run);
1726 put_bits(ac_pb, 1, 1);
1727 put_bits(ac_pb, 12, slevel & 0xfff);
1728 put_bits(ac_pb, 1, 1);
1731 put_bits(ac_pb, 1, 0);
1732 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1733 put_bits(ac_pb, 1, sign);
1737 put_bits(ac_pb, 1, 0);
1738 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1739 put_bits(ac_pb, 1, sign);
1742 put_bits(ac_pb, 1, sign);
1752 /***********************************************/
1755 static VLC intra_MCBPC_vlc;
1756 static VLC inter_MCBPC_vlc;
1757 static VLC cbpy_vlc;
1759 static VLC dc_lum, dc_chrom;
1760 static VLC sprite_trajectory;
1761 static VLC mb_type_b_vlc;
1763 void init_rl(RLTable *rl)
1765 INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
1766 UINT8 index_run[MAX_RUN+1];
1767 int last, run, level, start, end, i;
1769 /* compute max_level[], max_run[] and index_run[] */
1770 for(last=0;last<2;last++) {
1779 memset(max_level, 0, MAX_RUN + 1);
1780 memset(max_run, 0, MAX_LEVEL + 1);
1781 memset(index_run, rl->n, MAX_RUN + 1);
1782 for(i=start;i<end;i++) {
1783 run = rl->table_run[i];
1784 level = rl->table_level[i];
1785 if (index_run[run] == rl->n)
1787 if (level > max_level[run])
1788 max_level[run] = level;
1789 if (run > max_run[level])
1790 max_run[level] = run;
1792 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1793 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1794 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1795 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1796 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1797 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1801 void init_vlc_rl(RLTable *rl)
1805 init_vlc(&rl->vlc, 9, rl->n + 1,
1806 &rl->table_vlc[0][1], 4, 2,
1807 &rl->table_vlc[0][0], 4, 2);
1810 for(q=0; q<32; q++){
1819 rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
1820 for(i=0; i<rl->vlc.table_size; i++){
1821 int code= rl->vlc.table[i][0];
1822 int len = rl->vlc.table[i][1];
1825 if(len==0){ // illegal code
1828 }else if(len<0){ //more bits needed
1832 if(code==rl->n){ //esc
1836 run= rl->table_run [code] + 1;
1837 level= rl->table_level[code] * qmul + qadd;
1838 if(code >= rl->last) run+=192;
1841 rl->rl_vlc[q][i].len= len;
1842 rl->rl_vlc[q][i].level= level;
1843 rl->rl_vlc[q][i].run= run;
1850 /* XXX: find a better solution to handle static init */
1851 void h263_decode_init_vlc(MpegEncContext *s)
1853 static int done = 0;
1858 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 8,
1859 intra_MCBPC_bits, 1, 1,
1860 intra_MCBPC_code, 1, 1);
1861 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 25,
1862 inter_MCBPC_bits, 1, 1,
1863 inter_MCBPC_code, 1, 1);
1864 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1865 &cbpy_tab[0][1], 2, 1,
1866 &cbpy_tab[0][0], 2, 1);
1867 init_vlc(&mv_vlc, MV_VLC_BITS, 33,
1869 &mvtab[0][0], 2, 1);
1872 init_rl(&rl_intra_aic);
1873 init_vlc_rl(&rl_inter);
1874 init_vlc_rl(&rl_intra);
1875 init_vlc_rl(&rl_intra_aic);
1876 init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
1877 &DCtab_lum[0][1], 2, 1,
1878 &DCtab_lum[0][0], 2, 1);
1879 init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
1880 &DCtab_chrom[0][1], 2, 1,
1881 &DCtab_chrom[0][0], 2, 1);
1882 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
1883 &sprite_trajectory_tab[0][1], 4, 2,
1884 &sprite_trajectory_tab[0][0], 4, 2);
1885 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
1886 &mb_type_b_tab[0][1], 2, 1,
1887 &mb_type_b_tab[0][0], 2, 1);
1890 s->progressive_sequence=1; // set to most likely for the case of incomplete headers
1893 int h263_decode_gob_header(MpegEncContext *s)
1895 unsigned int val, gfid;
1897 /* Check for GOB Start Code */
1898 val = show_bits(&s->gb, 16);
1900 /* We have a GBSC probably with GSTUFF */
1901 skip_bits(&s->gb, 16); /* Drop the zeros */
1902 while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
1904 fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
1906 s->gob_number = get_bits(&s->gb, 5); /* GN */
1907 gfid = get_bits(&s->gb, 2); /* GFID */
1908 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
1910 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
1918 static inline void memsetw(short *tab, int val, int n)
1925 void ff_mpeg4_init_partitions(MpegEncContext *s)
1927 init_put_bits(&s->tex_pb, s->tex_pb_buffer, PB_BUFFER_SIZE, NULL, NULL);
1928 init_put_bits(&s->pb2 , s->pb2_buffer , PB_BUFFER_SIZE, NULL, NULL);
1931 void ff_mpeg4_merge_partitions(MpegEncContext *s)
1933 const int pb2_len = get_bit_count(&s->pb2 );
1934 const int tex_pb_len= get_bit_count(&s->tex_pb);
1935 const int bits= get_bit_count(&s->pb);
1937 if(s->pict_type==I_TYPE){
1938 put_bits(&s->pb, 19, DC_MARKER);
1939 s->misc_bits+=19 + pb2_len + bits - s->last_bits;
1940 s->i_tex_bits+= tex_pb_len;
1942 put_bits(&s->pb, 17, MOTION_MARKER);
1943 s->misc_bits+=17 + pb2_len;;
1944 s->mv_bits+= bits - s->last_bits;
1945 s->p_tex_bits+= tex_pb_len;
1948 flush_put_bits(&s->pb2);
1949 flush_put_bits(&s->tex_pb);
1951 ff_copy_bits(&s->pb, s->pb2_buffer , pb2_len);
1952 ff_copy_bits(&s->pb, s->tex_pb_buffer, tex_pb_len);
1953 s->last_bits= get_bit_count(&s->pb);
1956 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1958 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1960 ff_mpeg4_stuffing(&s->pb);
1961 if(s->pict_type==I_TYPE)
1962 put_bits(&s->pb, 16, 0);
1963 else if(s->pict_type==B_TYPE)
1964 put_bits(&s->pb, MAX(MAX(s->f_code, s->b_code)+15, 17), 0);
1966 put_bits(&s->pb, s->f_code+15, 0);
1967 put_bits(&s->pb, 1, 1);
1969 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
1970 put_bits(&s->pb, 5, s->qscale);
1971 put_bits(&s->pb, 1, 0); /* no HEC */
1975 * decodes the next video packet and sets s->next_qscale
1976 * returns mb_num of the next packet or <0 if something went wrong
1978 static int decode_video_packet_header(MpegEncContext *s, GetBitContext *gb)
1981 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1982 int header_extension=0, mb_num;
1983 //printf("%X\n", show_bits(&gb, 24));
1984 //printf("parse_video_packet_header\n");
1985 // if(show_aligned_bits(gb, 1, 16) != 0) return -1;
1987 /* is there enough space left for a video packet + header */
1988 if( get_bits_count(gb) > gb->size*8-20) return -1;
1990 //printf("resync at %d %d\n", s->mb_x, s->mb_y);
1991 // skip_bits(gb, 1);
1992 // align_get_bits(gb);
1993 if(get_bits(gb, 16)!=0){
1994 printf("internal error while decoding video packet header\n");
1997 //printf("%X\n", show_bits(gb, 24));
1999 while(!get_bits1(gb) && bits<30) bits++;
2000 if((s->pict_type == P_TYPE || s->pict_type == S_TYPE) && bits != s->f_code-1){
2001 printf("marker does not match f_code (is: %d should be: %d pos: %d end %d x: %d y: %d)\n",
2002 bits+1, s->f_code, get_bits_count(gb), gb->size*8, s->mb_x, s->mb_y);
2004 }else if(s->pict_type == I_TYPE && bits != 0){
2005 printf("marker too long\n");
2007 }else if(s->pict_type == B_TYPE && bits != MAX(MAX(s->f_code, s->b_code)-1, 1)){
2008 printf("marker does not match f/b_code\n");
2011 //printf("%X\n", show_bits(gb, 24));
2013 if(s->shape != RECT_SHAPE){
2014 header_extension= get_bits1(gb);
2015 //FIXME more stuff here
2018 mb_num= get_bits(gb, mb_num_bits);
2019 if(mb_num < s->mb_x + s->mb_y*s->mb_width || mb_num>=s->mb_num){
2020 fprintf(stderr, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_x + s->mb_y*s->mb_width);
2024 if(s->shape != BIN_ONLY_SHAPE){
2025 s->next_resync_qscale= get_bits(gb, 5);
2026 if(s->next_resync_qscale==0)
2027 s->next_resync_qscale= s->qscale;
2028 if(s->next_resync_qscale==0){
2029 fprintf(stderr, "qscale==0\n");
2034 if(s->shape == RECT_SHAPE){
2035 header_extension= get_bits1(gb);
2037 if(header_extension){
2040 printf("header extension not supported\n");
2043 while (get_bits1(gb) != 0)
2046 check_marker(gb, "before time_increment in video packed header");
2047 time_increment= get_bits(gb, s->time_increment_bits);
2048 if(s->pict_type!=B_TYPE){
2049 s->last_time_base= s->time_base;
2050 s->time_base+= time_incr;
2051 s->time= s->time_base*s->time_increment_resolution + time_increment;
2052 s->pp_time= s->time - s->last_non_b_time;
2053 s->last_non_b_time= s->time;
2055 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
2056 s->bp_time= s->last_non_b_time - s->time;
2058 check_marker(gb, "before vop_coding_type in video packed header");
2060 skip_bits(gb, 2); /* vop coding type */
2061 //FIXME not rect stuff here
2063 if(s->shape != BIN_ONLY_SHAPE){
2064 skip_bits(gb, 3); /* intra dc vlc threshold */
2066 if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE && s->num_sprite_warping_points){
2067 mpeg4_decode_sprite_trajectory(s);
2070 //FIXME reduced res stuff here
2072 if (s->pict_type != I_TYPE) {
2073 s->f_code = get_bits(gb, 3); /* fcode_for */
2075 printf("Error, video packet header damaged or not MPEG4 header (f_code=0)\n");
2076 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2079 if (s->pict_type == B_TYPE) {
2080 s->b_code = get_bits(gb, 3);
2084 //FIXME new-pred stuff
2086 //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));
2091 void ff_mpeg4_clean_buffers(MpegEncContext *s)
2093 int c_wrap, c_xy, l_wrap, l_xy;
2095 l_wrap= s->block_wrap[0];
2096 l_xy= s->mb_y*l_wrap*2 + s->mb_x*2;
2097 c_wrap= s->block_wrap[4];
2098 c_xy= s->mb_y*c_wrap + s->mb_x;
2101 memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
2102 memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
2103 memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
2106 memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(INT16));
2107 memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(INT16));
2108 memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(INT16));
2111 // we cant clear the MVs as they might be needed by a b frame
2112 // memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(INT16));
2113 // memset(s->motion_val, 0, 2*sizeof(INT16)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
2114 s->last_mv[0][0][0]=
2115 s->last_mv[0][0][1]=
2116 s->last_mv[1][0][0]=
2117 s->last_mv[1][0][1]= 0;
2120 /* searches for the next resync marker clears ac,dc,mc, and sets s->next_resync_gb, s->mb_num_left */
2121 int ff_mpeg4_resync(MpegEncContext *s)
2125 /* search & parse next resync marker */
2127 gb= s->next_resync_gb;
2128 align_get_bits(&gb);
2129 //printf("mpeg4_resync %d next:%d \n", get_bits_count(&gb), get_bits_count(&s->next_resync_gb));
2131 int v= show_bits(&gb, 24);
2132 if( get_bits_count(&gb) >= gb.size*8-24 || v == 1 /* start-code */){
2133 s->mb_num_left= s->mb_num - s->mb_x - s->mb_y*s->mb_width;
2134 //printf("mpeg4_resync end\n");
2135 s->gb= s->next_resync_gb; //continue at the next resync marker
2137 }else if(v>>8 == 0){
2139 s->next_resync_pos= get_bits_count(&gb);
2141 next= decode_video_packet_header(s, &gb);
2143 s->mb_num_left= next - s->mb_x - s->mb_y*s->mb_width;
2147 align_get_bits(&gb);
2151 s->next_resync_gb=gb;
2156 static inline void init_block_index(MpegEncContext *s)
2158 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2159 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
2160 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
2161 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
2162 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2163 s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2166 static inline void update_block_index(MpegEncContext *s)
2168 s->block_index[0]+=2;
2169 s->block_index[1]+=2;
2170 s->block_index[2]+=2;
2171 s->block_index[3]+=2;
2172 s->block_index[4]++;
2173 s->block_index[5]++;
2177 * decodes the first & second partition
2178 * returns error type or 0 if no error
2180 int ff_mpeg4_decode_partitions(MpegEncContext *s)
2182 static const INT8 quant_tab[4] = { -1, -2, 1, 2 };
2185 /* decode first partition */
2187 s->first_slice_line=1;
2188 s->mb_x= s->resync_mb_x;
2189 for(s->mb_y= s->resync_mb_y; mb_num < s->mb_num_left; s->mb_y++){
2190 init_block_index(s);
2191 for(; mb_num < s->mb_num_left && s->mb_x<s->mb_width; s->mb_x++){
2192 const int xy= s->mb_x + s->mb_y*s->mb_width;
2197 update_block_index(s);
2198 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
2199 s->first_slice_line=0;
2201 if(s->mb_x==0) PRINT_MB_TYPE("\n");
2203 if(s->pict_type==I_TYPE){
2207 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2209 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2210 return DECODING_DESYNC;
2212 s->cbp_table[xy]= cbpc & 3;
2213 s->mb_type[xy]= MB_TYPE_INTRA;
2217 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2220 else if (s->qscale > 31)
2224 s->qscale_table[xy]= s->qscale;
2226 s->mbintra_table[xy]= 1;
2229 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
2231 fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2232 return DECODING_DESYNC;
2235 if(dc_pred_dir) dir|=1;
2237 s->pred_dir_table[xy]= dir;
2238 }else{ /* P/S_TYPE */
2239 int mx, my, pred_x, pred_y;
2240 INT16 * const mot_val= s->motion_val[s->block_index[0]];
2241 const int stride= s->block_wrap[0]*2;
2243 if(get_bits1(&s->gb)){
2245 s->mb_type[xy]= MB_TYPE_SKIPED;
2246 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2247 const int a= s->sprite_warping_accuracy;
2249 if(s->divx_version==500 && s->divx_build==413){
2250 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2251 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2253 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2254 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2255 s->mb_type[xy]= MB_TYPE_GMC | MB_TYPE_SKIPED;
2262 mot_val[0 ]= mot_val[2 ]=
2263 mot_val[0+stride]= mot_val[2+stride]= mx;
2264 mot_val[1 ]= mot_val[3 ]=
2265 mot_val[1+stride]= mot_val[3+stride]= my;
2267 if(s->mbintra_table[xy])
2268 ff_clean_intra_table_entries(s);
2272 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
2274 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2275 return DECODING_DESYNC;
2279 else if (cbpc == 20)
2280 fprintf(stderr, "Stuffing !");
2281 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
2283 s->mb_intra = ((cbpc & 4) != 0);
2287 s->mbintra_table[xy]= 1;
2288 s->mb_type[xy]= MB_TYPE_INTRA;
2289 mot_val[0 ]= mot_val[2 ]=
2290 mot_val[0+stride]= mot_val[2+stride]= 0;
2291 mot_val[1 ]= mot_val[3 ]=
2292 mot_val[1+stride]= mot_val[3+stride]= 0;
2294 if(s->mbintra_table[xy])
2295 ff_clean_intra_table_entries(s);
2297 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
2298 s->mcsel= get_bits1(&s->gb);
2301 if ((cbpc & 16) == 0) {
2303 /* 16x16 motion prediction */
2304 s->mb_type[xy]= MB_TYPE_INTER;
2306 h263_pred_motion(s, 0, &pred_x, &pred_y);
2308 mx = h263_decode_motion(s, pred_x, s->f_code);
2310 const int a= s->sprite_warping_accuracy;
2311 if(s->divx_version==500 && s->divx_build==413){
2312 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2314 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2318 return DECODING_DESYNC;
2321 my = h263_decode_motion(s, pred_y, s->f_code);
2323 const int a= s->sprite_warping_accuracy;
2324 if(s->divx_version==500 && s->divx_build==413){
2325 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2327 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2331 return DECODING_DESYNC;
2332 mot_val[0 ]= mot_val[2 ] =
2333 mot_val[0+stride]= mot_val[2+stride]= mx;
2334 mot_val[1 ]= mot_val[3 ]=
2335 mot_val[1+stride]= mot_val[3+stride]= my;
2339 s->mb_type[xy]= MB_TYPE_INTER4V;
2341 INT16 *mot_val= h263_pred_motion(s, i, &pred_x, &pred_y);
2342 mx = h263_decode_motion(s, pred_x, s->f_code);
2344 return DECODING_DESYNC;
2346 my = h263_decode_motion(s, pred_y, s->f_code);
2348 return DECODING_DESYNC;
2359 if (s->pict_type==I_TYPE && get_bits(&s->gb, 19)!=DC_MARKER ) s->decoding_error= DECODING_DESYNC;
2360 else if(s->pict_type!=I_TYPE && get_bits(&s->gb, 17)!=MOTION_MARKER) s->decoding_error= DECODING_DESYNC;
2361 if(s->decoding_error== DECODING_DESYNC){
2362 fprintf(stderr, "marker missing after first partition at %d %d\n", s->mb_x, s->mb_y);
2363 return DECODING_DESYNC;
2366 /* decode second partition */
2368 s->mb_x= s->resync_mb_x;
2369 for(s->mb_y= s->resync_mb_y; mb_num < s->mb_num_left; s->mb_y++){
2370 init_block_index(s);
2371 for(; mb_num < s->mb_num_left && s->mb_x<s->mb_width; s->mb_x++){
2372 const int xy= s->mb_x + s->mb_y*s->mb_width;
2375 update_block_index(s);
2377 if(s->pict_type==I_TYPE){
2378 int ac_pred= get_bits1(&s->gb);
2379 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2381 fprintf(stderr, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2382 return DECODING_AC_LOST;
2385 s->cbp_table[xy]|= cbpy<<2;
2386 s->pred_dir_table[xy]|= ac_pred<<7;
2387 }else{ /* P || S_TYPE */
2388 if(s->mb_type[xy]&MB_TYPE_INTRA){
2390 int ac_pred = get_bits1(&s->gb);
2391 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2394 fprintf(stderr, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2395 return DECODING_ACDC_LOST;
2398 if(s->cbp_table[xy] & 8) {
2399 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2402 else if (s->qscale > 31)
2406 s->qscale_table[xy]= s->qscale;
2410 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
2412 fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2413 return DECODING_ACDC_LOST;
2416 if(dc_pred_dir) dir|=1;
2418 s->cbp_table[xy]&= 3; //remove dquant
2419 s->cbp_table[xy]|= cbpy<<2;
2420 s->pred_dir_table[xy]= dir | (ac_pred<<7);
2421 }else if(s->mb_type[xy]&MB_TYPE_SKIPED){
2422 s->qscale_table[xy]= s->qscale;
2423 s->cbp_table[xy]= 0;
2425 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2428 fprintf(stderr, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2429 return DECODING_ACDC_LOST;
2432 if(s->cbp_table[xy] & 8) {
2433 //fprintf(stderr, "dquant\n");
2434 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2437 else if (s->qscale > 31)
2441 s->qscale_table[xy]= s->qscale;
2443 s->cbp_table[xy]&= 3; //remove dquant
2444 s->cbp_table[xy]|= (cbpy^0xf)<<2;
2455 static int mpeg4_decode_partitioned_mb(MpegEncContext *s,
2456 DCTELEM block[6][64])
2459 const int xy= s->mb_x + s->mb_y*s->mb_width;
2461 if(s->mb_x==s->resync_mb_x && s->mb_y==s->resync_mb_y){ //Note resync_mb_{x,y}==0 at the start
2463 int block_index_backup[6];
2464 int qscale= s->qscale;
2466 for(i=0; i<6; i++) block_index_backup[i]= s->block_index[i];
2468 s->decoding_error= ff_mpeg4_decode_partitions(s);
2470 for(i=0; i<6; i++) s->block_index[i]= block_index_backup[i];
2471 s->first_slice_line=1;
2472 s->mb_x= s->resync_mb_x;
2473 s->mb_y= s->resync_mb_y;
2477 if(s->decoding_error==DECODING_DESYNC) return -1;
2480 mb_type= s->mb_type[xy];
2481 if(s->decoding_error)
2484 cbp = s->cbp_table[xy];
2486 if(s->decoding_error!=DECODING_ACDC_LOST && s->qscale_table[xy] != s->qscale){
2487 s->qscale= s->qscale_table[xy];
2491 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
2494 s->mv[0][i][0] = s->motion_val[ s->block_index[i] ][0];
2495 s->mv[0][i][1] = s->motion_val[ s->block_index[i] ][1];
2497 s->mb_intra = mb_type&MB_TYPE_INTRA;
2499 if (mb_type&MB_TYPE_SKIPED) {
2502 s->block_last_index[i] = -1;
2503 s->mv_dir = MV_DIR_FORWARD;
2504 s->mv_type = MV_TYPE_16X16;
2505 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2513 }else if(s->mb_intra && s->decoding_error!=DECODING_ACDC_LOST){
2514 s->ac_pred = s->pred_dir_table[xy]>>7;
2516 /* decode each block */
2517 for (i = 0; i < 6; i++) {
2518 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, 1);
2519 if(ret==DECODING_AC_LOST){
2520 fprintf(stderr, "texture corrupted at %d %d (trying to continue with mc/dc only)\n", s->mb_x, s->mb_y);
2521 s->decoding_error=DECODING_AC_LOST;
2523 }else if(ret==DECODING_ACDC_LOST){
2524 fprintf(stderr, "dc corrupted at %d %d (trying to continue with mc only)\n", s->mb_x, s->mb_y);
2525 s->decoding_error=DECODING_ACDC_LOST;
2529 }else if(!s->mb_intra){
2530 // s->mcsel= 0; //FIXME do we need to init that
2532 s->mv_dir = MV_DIR_FORWARD;
2533 if (mb_type&MB_TYPE_INTER4V) {
2534 s->mv_type = MV_TYPE_8X8;
2536 s->mv_type = MV_TYPE_16X16;
2538 if(s->decoding_error==0 && cbp){
2539 /* decode each block */
2540 for (i = 0; i < 6; i++) {
2541 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, 0);
2542 if(ret==DECODING_AC_LOST){
2543 fprintf(stderr, "texture corrupted at %d %d (trying to continue with mc/dc only)\n", s->mb_x, s->mb_y);
2544 s->decoding_error=DECODING_AC_LOST;
2550 } else { /* I-Frame */
2553 s->ac_pred = s->pred_dir_table[xy]>>7;
2555 /* decode each block */
2556 for (i = 0; i < 6; i++) {
2557 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, 1);
2558 if(ret==DECODING_AC_LOST){
2559 fprintf(stderr, "texture corrupted at %d %d (trying to continue with dc only)\n", s->mb_x, s->mb_y);
2560 s->decoding_error=DECODING_AC_LOST;
2562 }else if(ret==DECODING_ACDC_LOST){
2563 fprintf(stderr, "dc corrupted at %d %d\n", s->mb_x, s->mb_y);
2572 static inline void decode_interlaced_info(MpegEncContext *s, int cbp, int mb_type){
2574 if(!s->progressive_sequence){
2575 if(cbp || s->mb_intra)
2576 s->interlaced_dct= get_bits1(&s->gb);
2579 if( s->pict_type==P_TYPE //FIXME check that 4MV is forbidden
2580 || (s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && !s->mcsel)
2581 || (s->pict_type==B_TYPE && mb_type!=0) ){
2583 if(get_bits1(&s->gb)){
2584 s->mv_type= MV_TYPE_FIELD;
2586 if( s->pict_type==P_TYPE
2587 || (s->pict_type==B_TYPE && mb_type!=2)){
2588 s->field_select[0][0]= get_bits1(&s->gb);
2589 s->field_select[0][1]= get_bits1(&s->gb);
2591 if(s->pict_type==B_TYPE && mb_type!=3){
2592 s->field_select[1][0]= get_bits1(&s->gb);
2593 s->field_select[1][1]= get_bits1(&s->gb);
2603 int h263_decode_mb(MpegEncContext *s,
2604 DCTELEM block[6][64])
2606 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
2608 static INT8 quant_tab[4] = { -1, -2, 1, 2 };
2610 if(s->mb_x==0) PRINT_MB_TYPE("\n");
2612 if(s->resync_marker){
2613 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
2614 s->first_slice_line=0;
2618 if(s->data_partitioning && s->pict_type!=B_TYPE)
2619 return mpeg4_decode_partitioned_mb(s, block);
2621 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
2622 if (get_bits1(&s->gb)) {
2626 s->block_last_index[i] = -1;
2627 s->mv_dir = MV_DIR_FORWARD;
2628 s->mv_type = MV_TYPE_16X16;
2629 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2630 const int a= s->sprite_warping_accuracy;
2631 // int l = (1 << (s->f_code - 1)) * 32;
2634 if(s->divx_version==500 && s->divx_build==413){
2635 s->mv[0][0][0] = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2636 s->mv[0][0][1] = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2638 s->mv[0][0][0] = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2639 s->mv[0][0][1] = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2641 /* if (s->mv[0][0][0] < -l) s->mv[0][0][0]= -l;
2642 else if (s->mv[0][0][0] >= l) s->mv[0][0][0]= l-1;
2643 if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l;
2644 else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/
2656 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
2657 //fprintf(stderr, "\tCBPC: %d", cbpc);
2662 else if (cbpc == 20)
2663 fprintf(stderr, "Stuffing !");
2666 s->mb_intra = ((cbpc & 4) != 0);
2667 if (s->mb_intra) goto intra;
2669 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
2670 s->mcsel= get_bits1(&s->gb);
2672 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2673 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
2675 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2678 else if (s->qscale > 31)
2682 if((!s->progressive_sequence) && (cbp || s->workaround_bugs==2))
2683 s->interlaced_dct= get_bits1(&s->gb);
2685 s->mv_dir = MV_DIR_FORWARD;
2686 if ((cbpc & 16) == 0) {
2688 const int a= s->sprite_warping_accuracy;
2690 /* 16x16 global motion prediction */
2691 s->mv_type = MV_TYPE_16X16;
2692 // int l = (1 << (s->f_code - 1)) * 32;
2693 if(s->divx_version==500 && s->divx_build==413){
2694 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2695 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2697 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2698 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2700 // int l = (1 << (s->f_code - 1)) * 32;
2701 s->mv[0][0][0] = mx;
2702 s->mv[0][0][1] = my;
2703 }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
2705 /* 16x8 field motion prediction */
2706 s->mv_type= MV_TYPE_FIELD;
2708 s->field_select[0][0]= get_bits1(&s->gb);
2709 s->field_select[0][1]= get_bits1(&s->gb);
2711 h263_pred_motion(s, 0, &pred_x, &pred_y);
2714 mx = h263_decode_motion(s, pred_x, s->f_code);
2718 my = h263_decode_motion(s, pred_y/2, s->f_code);
2722 s->mv[0][i][0] = mx;
2723 s->mv[0][i][1] = my*2;
2727 /* 16x16 motion prediction */
2728 s->mv_type = MV_TYPE_16X16;
2729 h263_pred_motion(s, 0, &pred_x, &pred_y);
2731 mx = h263p_decode_umotion(s, pred_x);
2733 mx = h263_decode_motion(s, pred_x, s->f_code);
2739 my = h263p_decode_umotion(s, pred_y);
2741 my = h263_decode_motion(s, pred_y, s->f_code);
2745 s->mv[0][0][0] = mx;
2746 s->mv[0][0][1] = my;
2748 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
2749 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2753 s->mv_type = MV_TYPE_8X8;
2755 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
2757 mx = h263p_decode_umotion(s, pred_x);
2759 mx = h263_decode_motion(s, pred_x, s->f_code);
2764 my = h263p_decode_umotion(s, pred_y);
2766 my = h263_decode_motion(s, pred_y, s->f_code);
2769 s->mv[0][i][0] = mx;
2770 s->mv[0][i][1] = my;
2771 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
2772 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2777 } else if(s->pict_type==B_TYPE) {
2778 int modb1; // first bit of modb
2779 int modb2; // second bit of modb
2785 s->mb_intra = 0; //B-frames never contain intra blocks
2786 s->mcsel=0; // ... true gmc blocks
2789 s->last_mv[0][0][0]=
2790 s->last_mv[0][0][1]=
2791 s->last_mv[1][0][0]=
2792 s->last_mv[1][0][1]= 0;
2795 /* if we skipped it in the future P Frame than skip it now too */
2796 s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC
2801 s->block_last_index[i] = -1;
2803 s->mv_dir = MV_DIR_FORWARD;
2804 s->mv_type = MV_TYPE_16X16;
2813 modb1= get_bits1(&s->gb);
2815 modb2= get_bits1(&s->gb);
2816 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
2817 if(modb2==0) cbp= get_bits(&s->gb, 6);
2819 if (mb_type && cbp) {
2820 if(get_bits1(&s->gb)){
2821 s->qscale +=get_bits1(&s->gb)*4 - 2;
2824 else if (s->qscale > 31)
2829 // decode_interlaced_info(s, cbp, mb_type);
2831 mb_type=4; //like 0 but no vectors coded
2834 s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here
2835 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my
2838 case 0: /* direct */
2839 mx = h263_decode_motion(s, 0, 1);
2840 my = h263_decode_motion(s, 0, 1);
2841 case 4: /* direct with mx=my=0 */
2842 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2843 xy= s->block_index[0];
2844 time_pp= s->pp_time;
2845 time_pb= time_pp - s->bp_time;
2846 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb);
2847 //FIXME avoid divides
2848 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
2849 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
2850 s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
2851 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx;
2852 s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1]
2853 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
2854 if(s->non_b_mv4_table[xy]){
2856 s->mv_type = MV_TYPE_8X8;
2858 xy= s->block_index[i];
2859 s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
2860 s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
2861 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0]
2862 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx;
2863 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1]
2864 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
2868 PRINT_MB_TYPE(mb_type==4 ? "D" : "S");
2873 s->mv[1][0][1] = 1000;*/
2875 //FIXME additional MVs for interlaced stuff
2877 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2878 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2879 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2880 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2881 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2883 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2884 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2885 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2886 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2890 s->mv_dir = MV_DIR_BACKWARD;
2891 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2892 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2893 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2894 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2898 s->mv_dir = MV_DIR_FORWARD;
2899 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2900 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2901 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2902 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2906 printf("illegal MB_type\n");
2909 } else { /* I-Frame */
2910 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2917 if (s->h263_pred || s->h263_aic) {
2918 s->ac_pred = get_bits1(&s->gb);
2919 if (s->ac_pred && s->h263_aic)
2920 s->h263_aic_dir = get_bits1(&s->gb);
2922 PRINT_MB_TYPE(s->ac_pred ? "A" : "I");
2924 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2925 if(cbpy<0) return -1;
2926 cbp = (cbpc & 3) | (cbpy << 2);
2928 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2931 else if (s->qscale > 31)
2935 if(!s->progressive_sequence)
2936 s->interlaced_dct= get_bits1(&s->gb);
2938 /* decode each block */
2940 for (i = 0; i < 6; i++) {
2941 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, 1) < 0)
2945 for (i = 0; i < 6; i++) {
2946 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
2953 /* decode each block */
2955 for (i = 0; i < 6; i++) {
2956 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, 0) < 0)
2960 for (i = 0; i < 6; i++) {
2961 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
2968 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
2970 int code, val, sign, shift, l, m;
2972 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
2978 sign = get_bits1(&s->gb);
2980 val = (code - 1) << shift;
2982 val |= get_bits(&s->gb, shift);
2988 /* modulo decoding */
2989 if (!s->h263_long_vectors) {
2990 l = (1 << (f_code - 1)) * 32;
2994 } else if (val >= l) {
2998 /* horrible h263 long vector mode */
2999 if (pred < -31 && val < -63)
3001 if (pred > 32 && val > 63)
3008 /* Decodes RVLC of H.263+ UMV */
3009 static int h263p_decode_umotion(MpegEncContext * s, int pred)
3013 if (get_bits1(&s->gb)) /* Motion difference = 0 */
3016 code = 2 + get_bits1(&s->gb);
3018 while (get_bits1(&s->gb))
3021 code += get_bits1(&s->gb);
3026 code = (sign) ? (pred - code) : (pred + code);
3028 fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
3034 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
3037 int code, level, i, j, last, run;
3038 RLTable *rl = &rl_inter;
3039 const UINT8 *scan_table;
3041 scan_table = zigzag_direct;
3042 if (s->h263_aic && s->mb_intra) {
3046 if (s->h263_aic_dir)
3047 scan_table = ff_alternate_vertical_scan; /* left */
3049 scan_table = ff_alternate_horizontal_scan; /* top */
3051 } else if (s->mb_intra) {
3053 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
3054 int component, diff;
3055 component = (n <= 3 ? 0 : n - 4 + 1);
3056 level = s->last_dc[component];
3057 if (s->rv10_first_dc_coded[component]) {
3058 diff = rv_decode_dc(s, n);
3062 level = level & 0xff; /* handle wrap round */
3063 s->last_dc[component] = level;
3065 s->rv10_first_dc_coded[component] = 1;
3068 level = get_bits(&s->gb, 8);
3078 if (s->mb_intra && s->h263_aic)
3080 s->block_last_index[n] = i - 1;
3085 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
3088 if (code == rl->n) {
3090 last = get_bits1(&s->gb);
3091 run = get_bits(&s->gb, 6);
3092 level = (INT8)get_bits(&s->gb, 8);
3093 if (s->h263_rv10 && level == -128) {
3094 /* XXX: should patch encoder too */
3095 level = get_bits(&s->gb, 12);
3096 level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension
3099 run = rl->table_run[code];
3100 level = rl->table_level[code];
3101 last = code >= rl->last;
3102 if (get_bits1(&s->gb))
3115 if (s->mb_intra && s->h263_aic) {
3116 h263_pred_acdc(s, block, n);
3119 s->block_last_index[n] = i;
3123 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
3125 int level, pred, code;
3129 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
3131 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
3132 if (code < 0 || code > 9 /* && s->nbit<9 */){
3133 fprintf(stderr, "illegal dc vlc\n");
3139 level = get_bits(&s->gb, code);
3140 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
3141 level = - (level ^ ((1 << code) - 1));
3143 if(get_bits1(&s->gb)==0){ /* marker */
3144 fprintf(stderr, "dc marker bit missing\n");
3150 pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
3155 *dc_val = level * s->y_dc_scale;
3157 *dc_val = level * s->c_dc_scale;
3164 * returns 0 if everything went ok
3165 * returns DECODING_AC_LOST if an error was detected during AC decoding
3166 * returns DECODING_ACDC_LOST if an error was detected during DC decoding
3168 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
3169 int n, int coded, int intra)
3171 int level, i, last, run;
3174 RL_VLC_ELEM * rl_vlc;
3175 const UINT8 * scan_table;
3180 if(s->data_partitioning && s->pict_type!=B_TYPE){
3181 level = s->dc_val[0][ s->block_index[n] ];
3182 if(n<4) level= (level + (s->y_dc_scale>>1))/s->y_dc_scale; //FIXME optimizs
3183 else level= (level + (s->c_dc_scale>>1))/s->c_dc_scale;
3184 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_width]<<n)&32;
3186 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
3188 return DECODING_ACDC_LOST;
3195 rl_vlc = rl_intra.rl_vlc[0];
3197 if (dc_pred_dir == 0)
3198 scan_table = ff_alternate_vertical_scan; /* left */
3200 scan_table = ff_alternate_horizontal_scan; /* top */
3202 scan_table = zigzag_direct;
3209 s->block_last_index[n] = i;
3213 scan_table = zigzag_direct;
3217 rl_vlc = rl_inter.rl_vlc[0];
3219 qmul = s->qscale << 1;
3220 qadd = (s->qscale - 1) | 1;
3221 rl_vlc = rl_inter.rl_vlc[s->qscale];
3225 OPEN_READER(re, &s->gb);
3227 UPDATE_CACHE(re, &s->gb);
3228 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3231 cache= GET_CACHE(re, &s->gb);
3233 if (cache&0x80000000) {
3234 if (cache&0x40000000) {
3236 SKIP_CACHE(re, &s->gb, 2);
3237 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
3238 run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
3239 SKIP_COUNTER(re, &s->gb, 2+1+6);
3240 UPDATE_CACHE(re, &s->gb);
3242 if(SHOW_UBITS(re, &s->gb, 1)==0){
3243 fprintf(stderr, "1. marker bit missing in 3. esc\n");
3244 return DECODING_AC_LOST;
3245 }; SKIP_CACHE(re, &s->gb, 1);
3247 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
3249 if(SHOW_UBITS(re, &s->gb, 1)==0){
3250 fprintf(stderr, "2. marker bit missing in 3. esc\n");
3251 return DECODING_AC_LOST;
3252 }; LAST_SKIP_CACHE(re, &s->gb, 1);
3254 SKIP_COUNTER(re, &s->gb, 1+12+1);
3256 if(level*s->qscale>1024 || level*s->qscale<-1024){
3257 fprintf(stderr, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
3258 return DECODING_AC_LOST;
3262 const int abs_level= ABS(level);
3263 if(abs_level<=MAX_LEVEL && run<=MAX_RUN && s->error_resilience>=0){
3264 const int run1= run - rl->max_run[last][abs_level] - 1;
3265 if(abs_level <= rl->max_level[last][run]){
3266 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
3267 return DECODING_AC_LOST;
3269 if(abs_level <= rl->max_level[last][run]*2){
3270 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
3271 return DECODING_AC_LOST;
3273 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
3274 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
3275 return DECODING_AC_LOST;
3280 if (level>0) level= level * qmul + qadd;
3281 else level= level * qmul - qadd;
3287 #if MIN_CACHE_BITS < 20
3288 LAST_SKIP_BITS(re, &s->gb, 2);
3289 UPDATE_CACHE(re, &s->gb);
3291 SKIP_BITS(re, &s->gb, 2);
3293 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3294 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
3295 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3296 LAST_SKIP_BITS(re, &s->gb, 1);
3300 #if MIN_CACHE_BITS < 19
3301 LAST_SKIP_BITS(re, &s->gb, 1);
3302 UPDATE_CACHE(re, &s->gb);
3304 SKIP_BITS(re, &s->gb, 1);
3306 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3308 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
3309 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3310 LAST_SKIP_BITS(re, &s->gb, 1);
3314 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3315 LAST_SKIP_BITS(re, &s->gb, 1);
3320 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3321 return DECODING_AC_LOST;
3324 block[scan_table[i]] = level;
3328 block[scan_table[i]] = level;
3330 CLOSE_READER(re, &s->gb);
3334 mpeg4_pred_ac(s, block, n, dc_pred_dir);
3336 i = 63; /* XXX: not optimal */
3339 s->block_last_index[n] = i;
3343 /* most is hardcoded. should extend to handle all h263 streams */
3344 int h263_decode_picture_header(MpegEncContext *s)
3346 int format, width, height;
3348 /* picture start code */
3349 if (get_bits(&s->gb, 22) != 0x20) {
3350 fprintf(stderr, "Bad picture start code\n");
3353 /* temporal reference */
3354 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
3356 /* PTYPE starts here */
3357 if (get_bits1(&s->gb) != 1) {
3359 fprintf(stderr, "Bad marker\n");
3362 if (get_bits1(&s->gb) != 0) {
3363 fprintf(stderr, "Bad H263 id\n");
3364 return -1; /* h263 id */
3366 skip_bits1(&s->gb); /* split screen off */
3367 skip_bits1(&s->gb); /* camera off */
3368 skip_bits1(&s->gb); /* freeze picture release off */
3370 /* Reset GOB number */
3373 format = get_bits(&s->gb, 3);
3378 7 extended PTYPE (PLUSPTYPE)
3381 if (format != 7 && format != 6) {
3384 width = h263_format[format][0];
3385 height = h263_format[format][1];
3391 s->pict_type = I_TYPE + get_bits1(&s->gb);
3393 s->unrestricted_mv = get_bits1(&s->gb);
3394 s->h263_long_vectors = s->unrestricted_mv;
3396 if (get_bits1(&s->gb) != 0) {
3397 fprintf(stderr, "H263 SAC not supported\n");
3398 return -1; /* SAC: off */
3400 if (get_bits1(&s->gb) != 0) {
3401 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3404 if (get_bits1(&s->gb) != 0) {
3405 fprintf(stderr, "H263 PB frame not supported\n");
3406 return -1; /* not PB frame */
3408 s->qscale = get_bits(&s->gb, 5);
3409 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
3415 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
3417 /* ufep other than 0 and 1 are reserved */
3420 format = get_bits(&s->gb, 3);
3421 dprintf("ufep=1, format: %d\n", format);
3422 skip_bits(&s->gb,1); /* Custom PCF */
3423 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
3424 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
3425 if (get_bits1(&s->gb) != 0) {
3426 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3428 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
3432 skip_bits(&s->gb, 7);
3433 /* these are the 7 bits: (in order of appearence */
3434 /* Deblocking Filter */
3435 /* Slice Structured */
3436 /* Reference Picture Selection */
3437 /* Independent Segment Decoding */
3438 /* Alternative Inter VLC */
3439 /* Modified Quantization */
3440 /* Prevent start code emulation */
3442 skip_bits(&s->gb, 3); /* Reserved */
3443 } else if (ufep != 0) {
3444 fprintf(stderr, "Bad UFEP type (%d)\n", ufep);
3449 s->pict_type = get_bits(&s->gb, 3) + I_TYPE;
3450 dprintf("pict_type: %d\n", s->pict_type);
3451 if (s->pict_type != I_TYPE &&
3452 s->pict_type != P_TYPE)
3454 skip_bits(&s->gb, 2);
3455 s->no_rounding = get_bits1(&s->gb);
3456 dprintf("RTYPE: %d\n", s->no_rounding);
3457 skip_bits(&s->gb, 4);
3459 /* Get the picture dimensions */
3462 /* Custom Picture Format (CPFMT) */
3463 s->aspect_ratio_info = get_bits(&s->gb, 4);
3464 dprintf("aspect: %d\n", s->aspect_ratio_info);
3469 3 - 10:11 (525-type 4:3)
3470 4 - 16:11 (CIF 16:9)
3471 5 - 40:33 (525-type 16:9)
3474 width = (get_bits(&s->gb, 9) + 1) * 4;
3476 height = get_bits(&s->gb, 9) * 4;
3477 dprintf("\nH.263+ Custom picture: %dx%d\n",width,height);
3478 if (s->aspect_ratio_info == EXTENDED_PAR) {
3479 /* aspected dimensions */
3480 skip_bits(&s->gb, 8); /* width */
3481 skip_bits(&s->gb, 8); /* height */
3484 width = h263_format[format][0];
3485 height = h263_format[format][1];
3487 if ((width == 0) || (height == 0))
3491 if (s->umvplus_dec) {
3492 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
3496 s->qscale = get_bits(&s->gb, 5);
3499 while (get_bits1(&s->gb) != 0) {
3500 skip_bits(&s->gb, 8);
3505 s->y_dc_scale_table=
3506 s->c_dc_scale_table= h263_aic_dc_scale_table;
3508 s->y_dc_scale_table=
3509 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
3515 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s)
3518 int a= 2<<s->sprite_warping_accuracy;
3519 int rho= 3-s->sprite_warping_accuracy;
3521 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
3522 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
3523 int sprite_ref[4][2];
3524 int virtual_ref[2][2];
3526 int alpha=0, beta=0;
3529 //printf("SP %d\n", s->sprite_warping_accuracy);
3530 for(i=0; i<s->num_sprite_warping_points; i++){
3534 length= get_vlc(&s->gb, &sprite_trajectory);
3536 x= get_bits(&s->gb, length);
3537 //printf("lx %d %d\n", length, x);
3538 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
3539 x = - (x ^ ((1 << length) - 1));
3541 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(&s->gb); /* marker bit */
3543 length= get_vlc(&s->gb, &sprite_trajectory);
3545 y=get_bits(&s->gb, length);
3546 //printf("ly %d %d\n", length, y);
3547 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
3548 y = - (y ^ ((1 << length) - 1));
3550 skip_bits1(&s->gb); /* marker bit */
3551 //printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy);
3552 //if(i>0 && (x!=0 || y!=0)) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
3558 while((1<<alpha)<w) alpha++;
3559 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
3563 // Note, the 4th point isnt used for GMC
3564 if(s->divx_version==500 && s->divx_build==413){
3565 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
3566 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
3567 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
3568 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
3569 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
3570 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
3572 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
3573 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
3574 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
3575 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
3576 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
3577 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
3579 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
3580 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
3582 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
3583 // perhaps it should be reordered to be more readable ...
3584 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
3585 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
3586 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
3587 + 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);
3588 virtual_ref[0][1]= 16*vop_ref[0][1]
3589 + 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);
3590 virtual_ref[1][0]= 16*vop_ref[0][0]
3591 + 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);
3592 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
3593 + 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);
3595 switch(s->num_sprite_warping_points)
3598 s->sprite_offset[0][0]= 0;
3599 s->sprite_offset[0][1]= 0;
3600 s->sprite_offset[1][0]= 0;
3601 s->sprite_offset[1][1]= 0;
3602 s->sprite_delta[0][0][0]= a;
3603 s->sprite_delta[0][0][1]= 0;
3604 s->sprite_delta[0][1][0]= 0;
3605 s->sprite_delta[0][1][1]= a;
3606 s->sprite_delta[1][0][0]= a;
3607 s->sprite_delta[1][0][1]= 0;
3608 s->sprite_delta[1][1][0]= 0;
3609 s->sprite_delta[1][1][1]= a;
3610 s->sprite_shift[0][0]= 0;
3611 s->sprite_shift[0][1]= 0;
3612 s->sprite_shift[1][0]= 0;
3613 s->sprite_shift[1][1]= 0;
3616 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
3617 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
3618 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
3619 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
3620 s->sprite_delta[0][0][0]= a;
3621 s->sprite_delta[0][0][1]= 0;
3622 s->sprite_delta[0][1][0]= 0;
3623 s->sprite_delta[0][1][1]= a;
3624 s->sprite_delta[1][0][0]= a;
3625 s->sprite_delta[1][0][1]= 0;
3626 s->sprite_delta[1][1][0]= 0;
3627 s->sprite_delta[1][1][1]= a;
3628 s->sprite_shift[0][0]= 0;
3629 s->sprite_shift[0][1]= 0;
3630 s->sprite_shift[1][0]= 0;
3631 s->sprite_shift[1][1]= 0;
3635 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
3636 + ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
3637 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1]));
3638 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
3639 + ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
3640 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1]));
3641 s->sprite_offset[1][0]= ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
3642 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
3643 +2*w2*r*sprite_ref[0][0] - 16*w2);
3644 s->sprite_offset[1][1]= ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
3645 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
3646 +2*w2*r*sprite_ref[0][1] - 16*w2);
3647 s->sprite_delta[0][0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
3648 s->sprite_delta[0][0][1]= ( r*sprite_ref[0][1] - virtual_ref[0][1]);
3649 s->sprite_delta[0][1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
3650 s->sprite_delta[0][1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
3651 s->sprite_delta[1][0][0]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
3652 s->sprite_delta[1][0][1]= 4*( r*sprite_ref[0][1] - virtual_ref[0][1]);
3653 s->sprite_delta[1][1][0]= 4*(-r*sprite_ref[0][1] + virtual_ref[0][1]);
3654 s->sprite_delta[1][1][1]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
3655 s->sprite_shift[0][0]= alpha+rho;
3656 s->sprite_shift[0][1]= alpha+rho;
3657 s->sprite_shift[1][0]= alpha+rho+2;
3658 s->sprite_shift[1][1]= alpha+rho+2;
3663 /*printf("%d %d\n", s->sprite_delta[0][0][0], a<<s->sprite_shift[0][0]);
3664 printf("%d %d\n", s->sprite_delta[0][0][1], 0);
3665 printf("%d %d\n", s->sprite_delta[0][1][0], 0);
3666 printf("%d %d\n", s->sprite_delta[0][1][1], a<<s->sprite_shift[0][1]);
3667 printf("%d %d\n", s->sprite_delta[1][0][0], a<<s->sprite_shift[1][0]);
3668 printf("%d %d\n", s->sprite_delta[1][0][1], 0);
3669 printf("%d %d\n", s->sprite_delta[1][1][0], 0);
3670 printf("%d %d\n", s->sprite_delta[1][1][1], a<<s->sprite_shift[1][1]);*/
3671 /* try to simplify the situation */
3672 if( s->sprite_delta[0][0][0] == a<<s->sprite_shift[0][0]
3673 && s->sprite_delta[0][0][1] == 0
3674 && s->sprite_delta[0][1][0] == 0
3675 && s->sprite_delta[0][1][1] == a<<s->sprite_shift[0][1]
3676 && s->sprite_delta[1][0][0] == a<<s->sprite_shift[1][0]
3677 && s->sprite_delta[1][0][1] == 0
3678 && s->sprite_delta[1][1][0] == 0
3679 && s->sprite_delta[1][1][1] == a<<s->sprite_shift[1][1])
3681 s->sprite_offset[0][0]>>=s->sprite_shift[0][0];
3682 s->sprite_offset[0][1]>>=s->sprite_shift[0][1];
3683 s->sprite_offset[1][0]>>=s->sprite_shift[1][0];
3684 s->sprite_offset[1][1]>>=s->sprite_shift[1][1];
3685 s->sprite_delta[0][0][0]= a;
3686 s->sprite_delta[0][0][1]= 0;
3687 s->sprite_delta[0][1][0]= 0;
3688 s->sprite_delta[0][1][1]= a;
3689 s->sprite_delta[1][0][0]= a;
3690 s->sprite_delta[1][0][1]= 0;
3691 s->sprite_delta[1][1][0]= 0;
3692 s->sprite_delta[1][1][1]= a;
3693 s->sprite_shift[0][0]= 0;
3694 s->sprite_shift[0][1]= 0;
3695 s->sprite_shift[1][0]= 0;
3696 s->sprite_shift[1][1]= 0;
3697 s->real_sprite_warping_points=1;
3700 s->real_sprite_warping_points= s->num_sprite_warping_points;
3702 //printf("%d %d %d %d\n", d[0][0], d[0][1], s->sprite_offset[0][0], s->sprite_offset[0][1]);
3705 /* decode mpeg4 VOP header */
3706 int mpeg4_decode_picture_header(MpegEncContext * s)
3708 int time_incr, startcode, state, v;
3712 /* search next start code */
3713 align_get_bits(&s->gb);
3716 v = get_bits(&s->gb, 8);
3717 if (state == 0x000001) {
3718 state = ((state << 8) | v) & 0xffffff;
3722 state = ((state << 8) | v) & 0xffffff;
3723 if( get_bits_count(&s->gb) > s->gb.size*8-32){
3725 printf("no VOP startcode found, frame size was=%d\n", s->gb.size);
3728 printf("frame skip\n");
3729 return FRAME_SKIPED;
3733 //printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));
3734 if (startcode == 0x120) { // Video Object Layer
3735 int width, height, vo_ver_id;
3738 skip_bits(&s->gb, 1); /* random access */
3739 s->vo_type= get_bits(&s->gb, 8);
3740 if (get_bits1(&s->gb) != 0) { /* is_ol_id */
3741 vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
3742 skip_bits(&s->gb, 3); /* vo_priority */
3746 //printf("vo type:%d\n",s->vo_type);
3747 s->aspect_ratio_info= get_bits(&s->gb, 4);
3748 if(s->aspect_ratio_info == EXTENDED_PAR){
3749 skip_bits(&s->gb, 8); //par_width
3750 skip_bits(&s->gb, 8); // par_height
3753 if ((s->vol_control_parameters=get_bits1(&s->gb))) { /* vol control parameter */
3754 int chroma_format= get_bits(&s->gb, 2);
3755 if(chroma_format!=1){
3756 printf("illegal chroma format\n");
3758 s->low_delay= get_bits1(&s->gb);
3759 if(get_bits1(&s->gb)){ /* vbv parameters */
3760 printf("vbv parameters not supported\n");
3764 // set low delay flag only once so the smart? low delay detection wont be overriden
3765 if(s->picture_number==0)
3769 s->shape = get_bits(&s->gb, 2); /* vol shape */
3770 if(s->shape != RECT_SHAPE) printf("only rectangular vol supported\n");
3771 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
3772 printf("Gray shape not supported\n");
3773 skip_bits(&s->gb, 4); //video_object_layer_shape_extension
3776 skip_bits1(&s->gb); /* marker */
3778 s->time_increment_resolution = get_bits(&s->gb, 16);
3779 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
3780 if (s->time_increment_bits < 1)
3781 s->time_increment_bits = 1;
3782 skip_bits1(&s->gb); /* marker */
3784 if (get_bits1(&s->gb) != 0) { /* fixed_vop_rate */
3785 skip_bits(&s->gb, s->time_increment_bits);
3788 if (s->shape != BIN_ONLY_SHAPE) {
3789 if (s->shape == RECT_SHAPE) {
3790 skip_bits1(&s->gb); /* marker */
3791 width = get_bits(&s->gb, 13);
3792 skip_bits1(&s->gb); /* marker */
3793 height = get_bits(&s->gb, 13);
3794 skip_bits1(&s->gb); /* marker */
3795 if(width && height){ /* they should be non zero but who knows ... */
3798 // printf("width/height: %d %d\n", width, height);
3802 s->progressive_sequence= get_bits1(&s->gb)^1;
3803 if(!get_bits1(&s->gb)) printf("OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
3804 if (vo_ver_id == 1) {
3805 s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
3807 s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
3809 if(s->vol_sprite_usage==STATIC_SPRITE) printf("Static Sprites not supported\n");
3810 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
3811 if(s->vol_sprite_usage==STATIC_SPRITE){
3812 s->sprite_width = get_bits(&s->gb, 13);
3813 skip_bits1(&s->gb); /* marker */
3814 s->sprite_height= get_bits(&s->gb, 13);
3815 skip_bits1(&s->gb); /* marker */
3816 s->sprite_left = get_bits(&s->gb, 13);
3817 skip_bits1(&s->gb); /* marker */
3818 s->sprite_top = get_bits(&s->gb, 13);
3819 skip_bits1(&s->gb); /* marker */
3821 s->num_sprite_warping_points= get_bits(&s->gb, 6);
3822 s->sprite_warping_accuracy = get_bits(&s->gb, 2);
3823 s->sprite_brightness_change= get_bits1(&s->gb);
3824 if(s->vol_sprite_usage==STATIC_SPRITE)
3825 s->low_latency_sprite= get_bits1(&s->gb);
3827 // FIXME sadct disable bit if verid!=1 && shape not rect
3829 if (get_bits1(&s->gb) == 1) { /* not_8_bit */
3830 s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
3831 if(get_bits(&s->gb, 4)!=8) printf("N-bit not supported\n"); /* bits_per_pixel */
3832 if(s->quant_precision!=5) printf("quant precission %d\n", s->quant_precision);
3834 s->quant_precision = 5;
3837 // FIXME a bunch of grayscale shape things
3839 if((s->mpeg_quant=get_bits1(&s->gb))){ /* vol_quant_type */
3842 /* load default matrixes */
3843 for(i=0; i<64; i++){
3844 v= ff_mpeg4_default_intra_matrix[i];
3845 s->intra_matrix[i]= v;
3846 s->chroma_intra_matrix[i]= v;
3848 v= ff_mpeg4_default_non_intra_matrix[i];
3849 s->inter_matrix[i]= v;
3850 s->chroma_inter_matrix[i]= v;
3853 /* load custom intra matrix */
3854 if(get_bits1(&s->gb)){
3855 for(i=0; i<64; i++){
3856 v= get_bits(&s->gb, 8);
3859 j= zigzag_direct[i];
3860 s->intra_matrix[j]= v;
3861 s->chroma_intra_matrix[j]= v;
3865 /* load custom non intra matrix */
3866 if(get_bits1(&s->gb)){
3867 for(i=0; i<64; i++){
3868 v= get_bits(&s->gb, 8);
3871 j= zigzag_direct[i];
3872 s->inter_matrix[j]= v;
3873 s->chroma_inter_matrix[j]= v;
3876 /* replicate last value */
3878 j= zigzag_direct[i];
3879 s->inter_matrix[j]= v;
3880 s->chroma_inter_matrix[j]= v;
3884 // FIXME a bunch of grayscale shape things
3888 s->quarter_sample= get_bits1(&s->gb);
3889 else s->quarter_sample=0;
3891 if(!get_bits1(&s->gb)) printf("Complexity estimation not supported\n");
3893 s->resync_marker= !get_bits1(&s->gb); /* resync_marker_disabled */
3895 s->data_partitioning= get_bits1(&s->gb);
3896 if(s->data_partitioning){
3897 s->rvlc= get_bits1(&s->gb);
3899 printf("reversible vlc not supported\n");
3903 if(vo_ver_id != 1) {
3904 s->new_pred= get_bits1(&s->gb);
3906 printf("new pred not supported\n");
3907 skip_bits(&s->gb, 2); /* requested upstream message type */
3908 skip_bits1(&s->gb); /* newpred segment type */
3910 s->reduced_res_vop= get_bits1(&s->gb);
3911 if(s->reduced_res_vop) printf("reduced resolution VOP not supported\n");
3915 s->reduced_res_vop= 0;
3918 s->scalability= get_bits1(&s->gb);
3919 if(s->workaround_bugs==1) s->scalability=0;
3920 if (s->scalability) {
3921 int dummy= s->hierachy_type= get_bits1(&s->gb);
3922 int ref_layer_id= get_bits(&s->gb, 4);
3923 int ref_layer_sampling_dir= get_bits1(&s->gb);
3924 int h_sampling_factor_n= get_bits(&s->gb, 5);
3925 int h_sampling_factor_m= get_bits(&s->gb, 5);
3926 int v_sampling_factor_n= get_bits(&s->gb, 5);
3927 int v_sampling_factor_m= get_bits(&s->gb, 5);
3928 s->enhancement_type= get_bits1(&s->gb);
3929 // bin shape stuff FIXME
3930 printf("scalability not supported\n");
3933 //printf("end Data %X %d\n", show_bits(&s->gb, 32), get_bits_count(&s->gb)&0x7);
3935 } else if (startcode == 0x1b2) { //userdata
3941 //printf("user Data %X\n", show_bits(&s->gb, 32));
3942 buf[0]= show_bits(&s->gb, 8);
3943 for(i=1; i<256; i++){
3944 buf[i]= show_bits(&s->gb, 16)&0xFF;
3945 if(buf[i]==0) break;
3946 skip_bits(&s->gb, 8);
3949 e=sscanf(buf, "DivX%dBuild%d", &ver, &build);
3951 e=sscanf(buf, "DivX%db%d", &ver, &build);
3953 s->divx_version= ver;
3954 s->divx_build= build;
3955 if(s->picture_number==0){
3956 printf("This file was encoded with DivX%d Build%d\n", ver, build);
3957 if(ver==500 && build==413){
3958 printf("WARNING: this version of DivX is not MPEG4 compatible, trying to workaround these bugs...\n");
3962 //printf("User Data: %s\n", buf);
3964 } else if (startcode != 0x1b6) { //VOP
3968 s->pict_type = get_bits(&s->gb, 2) + I_TYPE; /* pict type: I = 0 , P = 1 */
3969 //if(s->pict_type!=I_TYPE) return FRAME_SKIPED;
3970 if(s->pict_type==B_TYPE && s->low_delay && s->vol_control_parameters==0){
3971 printf("low_delay flag set, but shouldnt, clearing it\n");
3974 // printf("pic: %d, qpel:%d part:%d resync:%d\n", s->pict_type, s->quarter_sample, s->data_partitioning, s->resync_marker);
3976 while (get_bits1(&s->gb) != 0)
3979 check_marker(&s->gb, "before time_increment");
3980 time_increment= get_bits(&s->gb, s->time_increment_bits);
3981 //printf(" type:%d incr:%d increment:%d\n", s->pict_type, time_incr, time_increment);
3982 if(s->pict_type!=B_TYPE){
3983 s->last_time_base= s->time_base;
3984 s->time_base+= time_incr;
3985 s->time= s->time_base*s->time_increment_resolution + time_increment;
3986 s->pp_time= s->time - s->last_non_b_time;
3987 s->last_non_b_time= s->time;
3989 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
3990 s->bp_time= s->last_non_b_time - s->time;
3991 if(s->pp_time <=s->bp_time){
3992 // printf("messed up order, seeking?, skiping current b frame\n");
3993 return FRAME_SKIPED;
3997 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){
3998 printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n");
3999 for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){
4000 if(get_bits1(&s->gb)) break;
4002 printf("my guess is %d bits ;)\n",s->time_increment_bits);
4005 if (get_bits1(&s->gb) != 1)
4007 //printf("time %d %d %d || %d %d %d\n", s->time_increment_bits, s->time_increment, s->time_base,
4008 //s->time, s->last_non_b_time[0], s->last_non_b_time[1]);
4009 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE
4010 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {
4011 /* rounding type for motion estimation */
4012 s->no_rounding = get_bits1(&s->gb);
4016 //FIXME reduced res stuff
4018 if (s->shape != RECT_SHAPE) {
4019 if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
4020 int width, height, hor_spat_ref, ver_spat_ref;
4022 width = get_bits(&s->gb, 13);
4023 skip_bits1(&s->gb); /* marker */
4024 height = get_bits(&s->gb, 13);
4025 skip_bits1(&s->gb); /* marker */
4026 hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
4027 skip_bits1(&s->gb); /* marker */
4028 ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
4030 skip_bits1(&s->gb); /* change_CR_disable */
4032 if (get_bits1(&s->gb) != 0) {
4033 skip_bits(&s->gb, 8); /* constant_alpha_value */
4036 //FIXME complexity estimation stuff
4038 if (s->shape != BIN_ONLY_SHAPE) {
4040 t=get_bits(&s->gb, 3); /* intra dc VLC threshold */
4041 //printf("threshold %d\n", t);
4042 if(!s->progressive_sequence){
4043 s->top_field_first= get_bits1(&s->gb);
4044 s->alternate_scan= get_bits1(&s->gb);
4045 //printf("top:%d alt:%d\n", s->top_field_first, s->alternate_scan);
4049 if(s->pict_type == S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
4050 if(s->num_sprite_warping_points){
4051 mpeg4_decode_sprite_trajectory(s);
4053 if(s->sprite_brightness_change) printf("sprite_brightness_change not supported\n");
4054 if(s->vol_sprite_usage==STATIC_SPRITE) printf("static sprite not supported\n");
4057 if (s->shape != BIN_ONLY_SHAPE) {
4058 /* note: we do not use quant_precision to avoid problem if no
4059 MPEG4 vol header as it is found on some old opendivx
4061 s->qscale = get_bits(&s->gb, 5);
4063 printf("Error, header damaged or not MPEG4 header (qscale=0)\n");
4064 return -1; // makes no sense to continue, as there is nothing left from the image then
4067 if (s->pict_type != I_TYPE) {
4068 s->f_code = get_bits(&s->gb, 3); /* fcode_for */
4070 printf("Error, header damaged or not MPEG4 header (f_code=0)\n");
4071 return -1; // makes no sense to continue, as the MV decoding will break very quickly
4076 if (s->pict_type == B_TYPE) {
4077 s->b_code = get_bits(&s->gb, 3);
4081 //printf("quant:%d fcode:%d bcode:%d type:%d size:%d\n", s->qscale, s->f_code, s->b_code, s->pict_type, s->gb.size);
4082 if(!s->scalability){
4083 if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) {
4084 skip_bits1(&s->gb); // vop shape coding type
4087 if(s->enhancement_type){
4088 int load_backward_shape= get_bits1(&s->gb);
4089 if(load_backward_shape){
4090 printf("load backward shape isnt supported\n");
4093 skip_bits(&s->gb, 2); //ref_select_code
4096 /* detect buggy encoders which dont set the low_delay flag (divx4/xvid/opendivx)*/
4097 // note we cannot detect divx5 without b-frames easyly (allthough its buggy too)
4098 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==0 && s->picture_number==0){
4099 printf("looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
4103 s->picture_number++; // better than pic number==0 allways ;)
4105 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
4106 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
4108 if(s->divx_version==0 || s->divx_version < 500){
4109 s->h_edge_pos= s->width;
4110 s->v_edge_pos= s->height;
4115 /* don't understand why they choose a different header ! */
4116 int intel_h263_decode_picture_header(MpegEncContext *s)
4120 /* picture header */
4121 if (get_bits(&s->gb, 22) != 0x20) {
4122 fprintf(stderr, "Bad picture start code\n");
4125 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
4127 if (get_bits1(&s->gb) != 1) {
4128 fprintf(stderr, "Bad marker\n");
4129 return -1; /* marker */
4131 if (get_bits1(&s->gb) != 0) {
4132 fprintf(stderr, "Bad H263 id\n");
4133 return -1; /* h263 id */
4135 skip_bits1(&s->gb); /* split screen off */
4136 skip_bits1(&s->gb); /* camera off */
4137 skip_bits1(&s->gb); /* freeze picture release off */
4139 format = get_bits(&s->gb, 3);
4141 fprintf(stderr, "Intel H263 free format not supported\n");
4146 s->pict_type = I_TYPE + get_bits1(&s->gb);
4148 s->unrestricted_mv = get_bits1(&s->gb);
4149 s->h263_long_vectors = s->unrestricted_mv;
4151 if (get_bits1(&s->gb) != 0) {
4152 fprintf(stderr, "SAC not supported\n");
4153 return -1; /* SAC: off */
4155 if (get_bits1(&s->gb) != 0) {
4156 fprintf(stderr, "Advanced Prediction Mode not supported\n");
4157 return -1; /* advanced prediction mode: off */
4159 if (get_bits1(&s->gb) != 0) {
4160 fprintf(stderr, "PB frame mode no supported\n");
4161 return -1; /* PB frame mode */
4164 /* skip unknown header garbage */
4165 skip_bits(&s->gb, 41);
4167 s->qscale = get_bits(&s->gb, 5);
4168 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
4171 while (get_bits1(&s->gb) != 0) {
4172 skip_bits(&s->gb, 8);