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 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,
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];
75 int h263_get_picture_format(int width, int height)
79 if (width == 128 && height == 96)
81 else if (width == 176 && height == 144)
83 else if (width == 352 && height == 288)
85 else if (width == 704 && height == 576)
87 else if (width == 1408 && height == 1152)
94 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
98 align_put_bits(&s->pb);
100 /* Update the pointer to last GOB */
101 s->ptr_lastgob = pbBufPtr(&s->pb);
104 put_bits(&s->pb, 22, 0x20); /* PSC */
105 put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) /
106 s->frame_rate) & 0xff);
108 put_bits(&s->pb, 1, 1); /* marker */
109 put_bits(&s->pb, 1, 0); /* h263 id */
110 put_bits(&s->pb, 1, 0); /* split screen off */
111 put_bits(&s->pb, 1, 0); /* camera off */
112 put_bits(&s->pb, 1, 0); /* freeze picture release off */
114 format = h263_get_picture_format(s->width, s->height);
117 put_bits(&s->pb, 3, format);
118 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
119 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
120 of H.263v1 UMV implies to check the predicted MV after
121 calculation of the current MB to see if we're on the limits */
122 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
123 put_bits(&s->pb, 1, 0); /* SAC: off */
124 put_bits(&s->pb, 1, 0); /* advanced prediction mode: off */
125 put_bits(&s->pb, 1, 0); /* not PB frame */
126 put_bits(&s->pb, 5, s->qscale);
127 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
130 /* H.263 Plus PTYPE */
131 put_bits(&s->pb, 3, 7);
132 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
134 put_bits(&s->pb,3,6); /* Custom Source Format */
136 put_bits(&s->pb, 3, format);
138 put_bits(&s->pb,1,0); /* Custom PCF: off */
139 s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
140 put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
141 put_bits(&s->pb,1,0); /* SAC: off */
142 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
143 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
144 put_bits(&s->pb,1,0); /* Deblocking Filter: off */
145 put_bits(&s->pb,1,0); /* Slice Structured: off */
146 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
147 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
148 put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
149 put_bits(&s->pb,1,0); /* Modified Quantization: off */
150 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
151 put_bits(&s->pb,3,0); /* Reserved */
153 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
155 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
156 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
157 if (s->pict_type == I_TYPE)
161 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
162 put_bits(&s->pb,2,0); /* Reserved */
163 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
165 /* This should be here if PLUSPTYPE */
166 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
169 /* Custom Picture Format (CPFMT) */
171 if (s->aspect_ratio_info)
172 put_bits(&s->pb,4,s->aspect_ratio_info);
174 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
175 put_bits(&s->pb,9,(s->width >> 2) - 1);
176 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
177 put_bits(&s->pb,9,(s->height >> 2));
180 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
182 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
183 put_bits(&s->pb, 5, s->qscale);
186 put_bits(&s->pb, 1, 0); /* no PEI */
190 s->c_dc_scale_table= h263_aic_dc_scale_table;
193 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
197 int h263_encode_gob_header(MpegEncContext * s, int mb_line)
201 /* Check to see if we need to put a new GBSC */
202 /* for RTP packetization */
204 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
205 if (pdif >= s->rtp_payload_size) {
206 /* Bad luck, packet must be cut before */
207 align_put_bits(&s->pb);
208 flush_put_bits(&s->pb);
209 /* Call the RTP callback to send the last GOB */
210 if (s->rtp_callback) {
211 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
212 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
214 s->ptr_lastgob = pbBufPtr(&s->pb);
215 put_bits(&s->pb, 17, 1); /* GBSC */
216 s->gob_number = mb_line / s->gob_index;
217 put_bits(&s->pb, 5, s->gob_number); /* GN */
218 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
219 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
220 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
222 } else if (pdif + s->mb_line_avgsize >= s->rtp_payload_size) {
223 /* Cut the packet before we can't */
224 align_put_bits(&s->pb);
225 flush_put_bits(&s->pb);
226 /* Call the RTP callback to send the last GOB */
227 if (s->rtp_callback) {
228 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
229 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
231 s->ptr_lastgob = pbBufPtr(&s->pb);
232 put_bits(&s->pb, 17, 1); /* GBSC */
233 s->gob_number = mb_line / s->gob_index;
234 put_bits(&s->pb, 5, s->gob_number); /* GN */
235 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
236 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
237 //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
244 static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6])
246 int score0=0, score1=0;
250 INT16 *ac_val, *ac_val1;
252 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
255 ac_val-= s->block_wrap[n]*16;
257 const int level= block[n][block_permute_op(i )];
259 score1+= ABS(level - ac_val[i+8]);
260 ac_val1[i ]= block[n][block_permute_op(i<<3)];
266 const int level= block[n][block_permute_op(i<<3)];
268 score1+= ABS(level - ac_val[i]);
270 ac_val1[i+8]= block[n][block_permute_op(i )];
275 return score0 > score1 ? 1 : 0;
278 void mpeg4_encode_mb(MpegEncContext * s,
279 DCTELEM block[6][64],
280 int motion_x, int motion_y)
282 int cbpc, cbpy, i, pred_x, pred_y;
284 PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
285 PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
286 PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2 : &s->pb;
287 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
289 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
293 for (i = 0; i < 6; i++) {
294 if (s->block_last_index[i] >= 0)
298 if(s->pict_type==B_TYPE){
299 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
300 int mb_type= mb_type_table[s->mv_dir];
306 s->last_mv[1][0][1]= 0;
309 /* nothing to do if this MB was skiped in the next P Frame */
310 if(s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]){
316 s->mv_dir= MV_DIR_FORWARD; //doesnt matter
320 if ((cbp | motion_x | motion_y | mb_type) ==0) {
321 /* direct MB with MV={0,0} */
322 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
324 if(interleaved_stats){
331 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
332 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
333 put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
334 if(cbp) put_bits(&s->pb, 6, cbp);
337 put_bits(&s->pb, 1, 0); /* no q-scale change */
339 if(interleaved_stats){
340 bits= get_bit_count(&s->pb);
341 s->misc_bits+= bits - s->last_bits;
348 h263_encode_motion(s, motion_x, 1);
349 h263_encode_motion(s, motion_y, 1);
352 h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
353 h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
354 h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
355 h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
356 s->last_mv[0][0][0]= s->mv[0][0][0];
357 s->last_mv[0][0][1]= s->mv[0][0][1];
358 s->last_mv[1][0][0]= s->mv[1][0][0];
359 s->last_mv[1][0][1]= s->mv[1][0][1];
361 case 2: /* backward */
362 h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code);
363 h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code);
364 s->last_mv[1][0][0]= motion_x;
365 s->last_mv[1][0][1]= motion_y;
367 case 3: /* forward */
368 h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);
369 h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);
370 s->last_mv[0][0][0]= motion_x;
371 s->last_mv[0][0][1]= motion_y;
374 printf("unknown mb type\n");
378 if(interleaved_stats){
379 bits= get_bit_count(&s->pb);
380 s->mv_bits+= bits - s->last_bits;
384 /* encode each block */
385 for (i = 0; i < 6; i++) {
386 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct, NULL, &s->pb);
389 if(interleaved_stats){
390 bits= get_bit_count(&s->pb);
391 s->p_tex_bits+= bits - s->last_bits;
394 }else{ /* s->pict_type==B_TYPE */
395 if ((cbp | motion_x | motion_y) == 0 && s->mv_type==MV_TYPE_16X16) {
396 /* check if the B frames can skip it too, as we must skip it if we skip here
397 why didnt they just compress the skip-mb bits instead of reusing them ?! */
398 if(s->max_b_frames>0){
405 if(x+16 > s->width) x= s->width-16;
406 if(y+16 > s->height) y= s->height-16;
408 offset= x + y*s->linesize;
409 p_pic= s->new_picture[0] + offset;
412 for(i=0; i<s->max_b_frames; i++){
416 if(s->coded_order[i+1].pict_type!=B_TYPE) break;
418 b_pic= s->coded_order[i+1].picture[0] + offset;
419 diff= pix_abs16x16(p_pic, b_pic, s->linesize);
420 if(diff>s->qscale*70){ //FIXME check that 70 is optimal
429 /* skip macroblock */
430 put_bits(&s->pb, 1, 1);
432 if(interleaved_stats){
441 put_bits(&s->pb, 1, 0); /* mb coded */
442 if(s->mv_type==MV_TYPE_16X16){
445 inter_MCBPC_bits[cbpc],
446 inter_MCBPC_code[cbpc]);
449 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
451 if(interleaved_stats){
452 bits= get_bit_count(&s->pb);
453 s->misc_bits+= bits - s->last_bits;
457 /* motion vectors: 16x16 mode */
458 h263_pred_motion(s, 0, &pred_x, &pred_y);
460 h263_encode_motion(s, motion_x - pred_x, s->f_code);
461 h263_encode_motion(s, motion_y - pred_y, s->f_code);
465 inter_MCBPC_bits[cbpc],
466 inter_MCBPC_code[cbpc]);
469 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
471 if(interleaved_stats){
472 bits= get_bit_count(&s->pb);
473 s->misc_bits+= bits - s->last_bits;
478 /* motion vectors: 8x8 mode*/
479 h263_pred_motion(s, i, &pred_x, &pred_y);
481 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);
482 h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);
486 if(interleaved_stats){
487 bits= get_bit_count(&s->pb);
488 s->mv_bits+= bits - s->last_bits;
492 /* encode each block */
493 for (i = 0; i < 6; i++) {
494 mpeg4_encode_block(s, block[i], i, 0, zigzag_direct, NULL, tex_pb);
497 if(interleaved_stats){
498 bits= get_bit_count(&s->pb);
499 s->p_tex_bits+= bits - s->last_bits;
506 int dc_diff[6]; //dc values with the dc prediction subtracted
507 int dir[6]; //prediction direction
508 int zigzag_last_index[6];
509 UINT8 *scan_table[6];
512 const int level= block[i][0];
515 dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
517 *dc_ptr = level * s->y_dc_scale;
519 *dc_ptr = level * s->c_dc_scale;
523 s->ac_pred= decide_ac_pred(s, block, dir);
530 mpeg4_inv_pred_ac(s, block[i], i, dir[i]);
531 if (dir[i]==0) st = ff_alternate_vertical_scan; /* left */
532 else st = ff_alternate_horizontal_scan; /* top */
534 for(last_index=63; last_index>=0; last_index--) //FIXME optimize
535 if(block[i][st[last_index]]) break;
536 zigzag_last_index[i]= s->block_last_index[i];
537 s->block_last_index[i]= last_index;
542 scan_table[i]= zigzag_direct;
547 for (i = 0; i < 6; i++) {
548 if (s->block_last_index[i] >= 1)
553 if (s->pict_type == I_TYPE) {
555 intra_MCBPC_bits[cbpc],
556 intra_MCBPC_code[cbpc]);
558 put_bits(&s->pb, 1, 0); /* mb coded */
560 inter_MCBPC_bits[cbpc + 4],
561 inter_MCBPC_code[cbpc + 4]);
563 put_bits(pb2, 1, s->ac_pred);
565 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
567 if(interleaved_stats){
568 bits= get_bit_count(&s->pb);
569 s->misc_bits+= bits - s->last_bits;
573 /* encode each block */
574 for (i = 0; i < 6; i++) {
575 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
578 if(interleaved_stats){
579 bits= get_bit_count(&s->pb);
580 s->i_tex_bits+= bits - s->last_bits;
585 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
591 ac_val = s->ac_val[0][0] + s->block_index[i] * 16;
595 block[i][block_permute_op(j )]= ac_val[j+8];
598 block[i][block_permute_op(j<<3)]= ac_val[j ];
600 s->block_last_index[i]= zigzag_last_index[i];
606 void h263_encode_mb(MpegEncContext * s,
607 DCTELEM block[6][64],
608 int motion_x, int motion_y)
610 int cbpc, cbpy, i, cbp, pred_x, pred_y;
612 INT16 rec_intradc[6];
615 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
619 for (i = 0; i < 6; i++) {
620 if (s->block_last_index[i] >= 0)
623 if ((cbp | motion_x | motion_y) == 0) {
624 /* skip macroblock */
625 put_bits(&s->pb, 1, 1);
628 put_bits(&s->pb, 1, 0); /* mb coded */
631 inter_MCBPC_bits[cbpc],
632 inter_MCBPC_code[cbpc]);
635 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
637 /* motion vectors: 16x16 mode only now */
638 h263_pred_motion(s, 0, &pred_x, &pred_y);
641 h263_encode_motion(s, motion_x - pred_x, s->f_code);
642 h263_encode_motion(s, motion_y - pred_y, s->f_code);
645 h263p_encode_umotion(s, motion_x - pred_x);
646 h263p_encode_umotion(s, motion_y - pred_y);
647 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
648 /* To prevent Start Code emulation */
649 put_bits(&s->pb,1,1);
652 int li = s->h263_aic ? 0 : 1;
657 if (s->h263_aic && s->mb_intra) {
658 INT16 level = block[i][0];
660 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
664 level = (level + (s->qscale >> 1))/(s->y_dc_scale);
666 level = (level - (s->qscale >> 1))/(s->y_dc_scale);
668 /* AIC can change CBP */
669 if (level == 0 && s->block_last_index[i] == 0)
670 s->block_last_index[i] = -1;
671 else if (level < -127)
673 else if (level > 127)
678 rec_intradc[i] = (s->y_dc_scale*level) + pred_dc;
681 //if ((rec_intradc[i] % 2) == 0)
684 if (rec_intradc[i] < 0)
686 else if (rec_intradc[i] > 2047)
687 rec_intradc[i] = 2047;
689 /* Update AC/DC tables */
690 *dc_ptr[i] = rec_intradc[i];
693 if (s->block_last_index[i] >= li)
698 if (s->pict_type == I_TYPE) {
700 intra_MCBPC_bits[cbpc],
701 intra_MCBPC_code[cbpc]);
703 put_bits(&s->pb, 1, 0); /* mb coded */
705 inter_MCBPC_bits[cbpc + 4],
706 inter_MCBPC_code[cbpc + 4]);
709 /* XXX: currently, we do not try to use ac prediction */
710 put_bits(&s->pb, 1, 0); /* no AC prediction */
713 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
717 /* encode each block */
718 h263_encode_block(s, block[i], i);
720 /* Update INTRADC for decoding */
721 if (s->h263_aic && s->mb_intra) {
722 block[i][0] = rec_intradc[i];
728 static int h263_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr)
730 int x, y, wrap, a, c, pred_dc, scale;
731 INT16 *dc_val, *ac_val;
733 /* find prediction */
735 x = 2 * s->mb_x + 1 + (n & 1);
736 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
737 wrap = s->mb_width * 2 + 2;
738 dc_val = s->dc_val[0];
739 ac_val = s->ac_val[0][0];
740 scale = s->y_dc_scale;
744 wrap = s->mb_width + 2;
745 dc_val = s->dc_val[n - 4 + 1];
746 ac_val = s->ac_val[n - 4 + 1][0];
747 scale = s->c_dc_scale;
752 a = dc_val[(x - 1) + (y) * wrap];
753 c = dc_val[(x) + (y - 1) * wrap];
755 /* No prediction outside GOB boundary */
756 if (s->first_slice_line && ((n < 2) || (n > 3)))
759 /* just DC prediction */
760 if (a != 1024 && c != 1024)
761 pred_dc = (a + c) >> 1;
767 /* we assume pred is positive */
768 //pred_dc = (pred_dc + (scale >> 1)) / scale;
769 *dc_val_ptr = &dc_val[x + y * wrap];
774 void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n)
776 int x, y, wrap, a, c, pred_dc, scale, i;
777 INT16 *dc_val, *ac_val, *ac_val1;
779 /* find prediction */
781 x = 2 * s->mb_x + 1 + (n & 1);
782 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
783 wrap = s->mb_width * 2 + 2;
784 dc_val = s->dc_val[0];
785 ac_val = s->ac_val[0][0];
786 scale = s->y_dc_scale;
790 wrap = s->mb_width + 2;
791 dc_val = s->dc_val[n - 4 + 1];
792 ac_val = s->ac_val[n - 4 + 1][0];
793 scale = s->c_dc_scale;
796 ac_val += ((y) * wrap + (x)) * 16;
802 a = dc_val[(x - 1) + (y) * wrap];
803 c = dc_val[(x) + (y - 1) * wrap];
805 /* No prediction outside GOB boundary */
806 if (s->first_slice_line && ((n < 2) || (n > 3)))
810 if (s->h263_aic_dir) {
811 /* left prediction */
815 block[block_permute_op(i*8)] += ac_val[i];
824 block[block_permute_op(i)] += ac_val[i + 8];
830 /* just DC prediction */
831 if (a != 1024 && c != 1024)
832 pred_dc = (a + c) >> 1;
839 /* we assume pred is positive */
840 block[0]=block[0]*scale + pred_dc;
844 else if (!(block[0] & 1))
847 /* Update AC/DC tables */
848 dc_val[(x) + (y) * wrap] = block[0];
852 ac_val1[i] = block[block_permute_op(i * 8)];
855 ac_val1[8 + i] = block[block_permute_op(i)];
858 INT16 *h263_pred_motion(MpegEncContext * s, int block,
862 INT16 *A, *B, *C, *mot_val;
863 static const int off[4]= {2, 1, 1, -1};
865 wrap = s->block_wrap[0];
866 xy = s->block_index[block];
868 mot_val = s->motion_val[xy];
870 A = s->motion_val[xy - 1];
871 /* special case for first (slice) line */
872 if ((s->mb_y == 0 || s->first_slice_line) && block<3) {
873 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
874 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
875 if(block==0){ //most common case
876 if(s->mb_x == s->resync_mb_x){ //rare
878 }else if(s->mb_x + 1 == s->resync_mb_x){ //rare
879 C = s->motion_val[xy + off[block] - wrap];
884 *px = mid_pred(A[0], 0, C[0]);
885 *py = mid_pred(A[1], 0, C[1]);
892 if(s->mb_x + 1 == s->resync_mb_x){ //rare
893 C = s->motion_val[xy + off[block] - wrap];
894 *px = mid_pred(A[0], 0, C[0]);
895 *py = mid_pred(A[1], 0, C[1]);
901 B = s->motion_val[xy - wrap];
902 C = s->motion_val[xy + off[block] - wrap];
903 if(s->mb_x == s->resync_mb_x) //rare
906 *px = mid_pred(A[0], B[0], C[0]);
907 *py = mid_pred(A[1], B[1], C[1]);
910 B = s->motion_val[xy - wrap];
911 C = s->motion_val[xy + off[block] - wrap];
912 *px = mid_pred(A[0], B[0], C[0]);
913 *py = mid_pred(A[1], B[1], C[1]);
918 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
920 int range, l, m, bit_size, sign, code, bits;
925 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
927 bit_size = f_code - 1;
928 range = 1 << bit_size;
929 /* modulo encoding */
934 } else if (val >= l) {
945 code = (val >> bit_size) + 1;
946 bits = val & (range - 1);
948 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
950 put_bits(&s->pb, bit_size, bits);
955 /* Encode MV differences on H.263+ with Unrestricted MV mode */
956 static void h263p_encode_umotion(MpegEncContext * s, int val)
966 put_bits(&s->pb, 1, 1);
968 put_bits(&s->pb, 3, 0);
970 put_bits(&s->pb, 3, 2);
973 sval = ((val < 0) ? (short)(-val):(short)val);
976 while (temp_val != 0) {
977 temp_val = temp_val >> 1;
983 tcode = (sval & (1 << (i-1))) >> (i-1);
984 tcode = (tcode << 1) | 1;
985 code = (code << 2) | tcode;
988 code = ((code << 1) | (val < 0)) << 1;
989 put_bits(&s->pb, (2*n_bits)+1, code);
990 //printf("\nVal = %d\tCode = %d", sval, code);
994 static void init_mv_penalty_and_fcode(MpegEncContext *s)
998 for(f_code=1; f_code<=MAX_FCODE; f_code++){
999 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1002 if(mv==0) len= mvtab[0][1];
1004 int val, bit_size, range, code;
1006 bit_size = s->f_code - 1;
1007 range = 1 << bit_size;
1013 code = (val >> bit_size) + 1;
1015 len= mvtab[code][1] + 1 + bit_size;
1017 len= mvtab[32][1] + 2 + bit_size;
1021 mv_penalty[f_code][mv+MAX_MV]= len;
1025 for(f_code=MAX_FCODE; f_code>0; f_code--){
1026 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1027 fcode_tab[mv+MAX_MV]= f_code;
1031 for(mv=0; mv<MAX_MV*2+1; mv++){
1032 umv_fcode_tab[mv]= 1;
1036 static void init_uni_dc_tab(void)
1038 int level, uni_code, uni_len;
1040 for(level=-256; level<256; level++){
1042 /* find number of bits */
1051 l= (-level) ^ ((1 << size) - 1);
1056 uni_code= DCtab_lum[size][0];
1057 uni_len = DCtab_lum[size][1];
1060 uni_code<<=size; uni_code|=l;
1063 uni_code<<=1; uni_code|=1;
1067 uni_DCtab_lum[level+256][0]= uni_code;
1068 uni_DCtab_lum[level+256][1]= uni_len;
1071 uni_code= DCtab_chrom[size][0];
1072 uni_len = DCtab_chrom[size][1];
1075 uni_code<<=size; uni_code|=l;
1078 uni_code<<=1; uni_code|=1;
1082 uni_DCtab_chrom[level+256][0]= uni_code;
1083 uni_DCtab_chrom[level+256][1]= uni_len;
1088 void h263_encode_init(MpegEncContext *s)
1090 static int done = 0;
1099 init_rl(&rl_intra_aic);
1101 init_mv_penalty_and_fcode(s);
1103 s->mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1105 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
1106 switch(s->codec_id){
1107 case CODEC_ID_MPEG4:
1108 s->fcode_tab= fcode_tab;
1109 s->min_qcoeff= -2048;
1110 s->max_qcoeff= 2047;
1112 case CODEC_ID_H263P:
1113 s->fcode_tab= umv_fcode_tab;
1114 s->min_qcoeff= -128;
1117 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
1118 default: //nothing needed default table allready set in mpegvideo.c
1119 s->min_qcoeff= -128;
1121 s->y_dc_scale_table=
1122 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1125 /* h263 type bias */
1126 //FIXME mpeg4 mpeg quantizer
1127 s->intra_quant_bias=0;
1128 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
1131 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1133 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1137 if (s->mb_intra && !s->h263_aic) {
1140 /* 255 cannot be represented, so we clamp */
1145 /* 0 cannot be represented also */
1151 put_bits(&s->pb, 8, 0xff);
1153 put_bits(&s->pb, 8, level & 0xff);
1157 if (s->h263_aic && s->mb_intra)
1162 last_index = s->block_last_index[n];
1163 last_non_zero = i - 1;
1164 for (; i <= last_index; i++) {
1165 j = zigzag_direct[i];
1168 run = i - last_non_zero - 1;
1169 last = (i == last_index);
1176 code = get_rl_index(rl, last, run, level);
1177 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1178 if (code == rl->n) {
1179 put_bits(&s->pb, 1, last);
1180 put_bits(&s->pb, 6, run);
1181 put_bits(&s->pb, 8, slevel & 0xff);
1183 put_bits(&s->pb, 1, sign);
1190 /***************************************************/
1192 void ff_mpeg4_stuffing(PutBitContext * pbc)
1195 put_bits(pbc, 1, 0);
1196 length= (-get_bit_count(pbc))&7;
1197 if(length) put_bits(pbc, length, (1<<length)-1);
1200 /* must be called before writing the header */
1201 void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
1202 int time_div, time_mod;
1204 if(s->pict_type==I_TYPE){ //we will encode a vol header
1205 s->time_increment_resolution= s->frame_rate/ff_gcd(s->frame_rate, FRAME_RATE_BASE);
1206 if(s->time_increment_resolution>=256*256) s->time_increment_resolution= 256*128;
1208 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
1211 s->time= picture_number*(INT64)FRAME_RATE_BASE*s->time_increment_resolution/s->frame_rate;
1212 time_div= s->time/s->time_increment_resolution;
1213 time_mod= s->time%s->time_increment_resolution;
1215 if(s->pict_type==B_TYPE){
1216 s->bp_time= s->last_non_b_time - s->time;
1218 s->last_time_base= s->time_base;
1219 s->time_base= time_div;
1220 s->pp_time= s->time - s->last_non_b_time;
1221 s->last_non_b_time= s->time;
1225 static void mpeg4_encode_vol_header(MpegEncContext * s)
1227 int vo_ver_id=1; //must be 2 if we want GMC or q-pel
1230 s->vo_type= s->has_b_frames ? CORE_VO_TYPE : SIMPLE_VO_TYPE;
1232 put_bits(&s->pb, 16, 0);
1233 put_bits(&s->pb, 16, 0x100); /* video obj */
1234 put_bits(&s->pb, 16, 0);
1235 put_bits(&s->pb, 16, 0x120); /* video obj layer */
1237 put_bits(&s->pb, 1, 0); /* random access vol */
1238 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
1239 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
1240 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
1241 put_bits(&s->pb, 3, 1); /* is obj layer priority */
1242 if(s->aspect_ratio_info)
1243 put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
1245 put_bits(&s->pb, 4, 1); /* aspect ratio info= sqare pixel */
1248 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1249 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1250 put_bits(&s->pb, 1, s->low_delay);
1251 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
1253 put_bits(&s->pb, 1, 0); /* vol control parameters= no */
1256 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
1257 put_bits(&s->pb, 1, 1); /* marker bit */
1259 put_bits(&s->pb, 16, s->time_increment_resolution);
1260 if (s->time_increment_bits < 1)
1261 s->time_increment_bits = 1;
1262 put_bits(&s->pb, 1, 1); /* marker bit */
1263 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
1264 put_bits(&s->pb, 1, 1); /* marker bit */
1265 put_bits(&s->pb, 13, s->width); /* vol width */
1266 put_bits(&s->pb, 1, 1); /* marker bit */
1267 put_bits(&s->pb, 13, s->height); /* vol height */
1268 put_bits(&s->pb, 1, 1); /* marker bit */
1269 put_bits(&s->pb, 1, 0); /* interlace */
1270 put_bits(&s->pb, 1, 1); /* obmc disable */
1271 if (vo_ver_id == 1) {
1272 put_bits(&s->pb, 1, s->vol_sprite_usage=0); /* sprite enable */
1273 }else{ /* vo_ver_id == 2 */
1274 put_bits(&s->pb, 2, s->vol_sprite_usage=0); /* sprite enable */
1276 put_bits(&s->pb, 1, 0); /* not 8 bit */
1277 put_bits(&s->pb, 1, 0); /* quant type= h263 style*/
1279 put_bits(&s->pb, 1, s->quarter_sample=0);
1280 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
1281 s->resync_marker= s->rtp_mode;
1282 put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
1283 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1284 if(s->data_partitioning){
1285 put_bits(&s->pb, 1, 0); /* no rvlc */
1288 if (vo_ver_id != 1){
1289 put_bits(&s->pb, 1, 0); /* newpred */
1290 put_bits(&s->pb, 1, 0); /* reduced res vop */
1292 put_bits(&s->pb, 1, 0); /* scalability */
1294 ff_mpeg4_stuffing(&s->pb);
1295 put_bits(&s->pb, 16, 0);
1296 put_bits(&s->pb, 16, 0x1B2); /* user_data */
1297 sprintf(buf, "FFmpeg%sb%s", FFMPEG_VERSION, LIBAVCODEC_BUILD_STR);
1298 put_string(&s->pb, buf);
1300 ff_mpeg4_stuffing(&s->pb);
1303 /* write mpeg4 VOP header */
1304 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1307 int time_div, time_mod;
1309 if(s->pict_type==I_TYPE){
1311 if(picture_number==0 || !s->strict_std_compliance)
1312 mpeg4_encode_vol_header(s);
1315 //printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
1317 put_bits(&s->pb, 16, 0); /* vop header */
1318 put_bits(&s->pb, 16, 0x1B6); /* vop header */
1319 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
1321 time_div= s->time/s->time_increment_resolution;
1322 time_mod= s->time%s->time_increment_resolution;
1323 time_incr= time_div - s->last_time_base;
1325 put_bits(&s->pb, 1, 1);
1327 put_bits(&s->pb, 1, 0);
1329 put_bits(&s->pb, 1, 1); /* marker */
1330 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1331 put_bits(&s->pb, 1, 1); /* marker */
1332 put_bits(&s->pb, 1, 1); /* vop coded */
1333 if ( s->pict_type == P_TYPE
1334 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
1335 s->no_rounding ^= 1;
1336 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
1338 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
1339 //FIXME sprite stuff
1341 put_bits(&s->pb, 5, s->qscale);
1343 if (s->pict_type != I_TYPE)
1344 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1345 if (s->pict_type == B_TYPE)
1346 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1347 // printf("****frame %d\n", picture_number);
1349 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
1350 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1353 static void h263_dc_scale(MpegEncContext * s)
1355 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
1356 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
1359 inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
1361 int a, b, c, wrap, pred, scale;
1365 /* find prediction */
1367 scale = s->y_dc_scale;
1369 scale = s->c_dc_scale;
1371 wrap= s->block_wrap[n];
1372 dc_val = s->dc_val[0] + s->block_index[n];
1378 b = dc_val[ - 1 - wrap];
1379 c = dc_val[ - wrap];
1381 if (abs(a - b) < abs(b - c)) {
1383 *dir_ptr = 1; /* top */
1386 *dir_ptr = 0; /* left */
1388 /* we assume pred is positive */
1391 "xorl %%edx, %%edx \n\t"
1393 : "=d" (pred), "=a"(dummy)
1394 : "a" (pred + (scale >> 1)), "c" (inverse[scale])
1397 pred = (pred + (scale >> 1)) / scale;
1400 /* prepare address for prediction update */
1401 *dc_val_ptr = &dc_val[0];
1406 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
1410 INT16 *ac_val, *ac_val1;
1412 /* find prediction */
1413 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1417 /* left prediction */
1420 block[block_permute_op(i*8)] += ac_val[i];
1423 /* top prediction */
1424 ac_val -= 16 * s->block_wrap[n];
1426 block[block_permute_op(i)] += ac_val[i + 8];
1432 ac_val1[i] = block[block_permute_op(i * 8)];
1435 ac_val1[8 + i] = block[block_permute_op(i)];
1438 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
1444 /* find prediction */
1445 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1448 /* left prediction */
1451 block[block_permute_op(i*8)] -= ac_val[i];
1454 /* top prediction */
1455 ac_val -= 16 * s->block_wrap[n];
1457 block[block_permute_op(i)] -= ac_val[i + 8];
1462 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
1465 // if(level<-255 || level>255) printf("dc overflow\n");
1469 put_bits(s, uni_DCtab_lum[level][1], uni_DCtab_lum[level][0]);
1472 put_bits(s, uni_DCtab_chrom[level][1], uni_DCtab_chrom[level][0]);
1476 /* find number of bits */
1486 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
1489 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
1492 /* encode remaining bits */
1495 level = (-level) ^ ((1 << size) - 1);
1496 put_bits(&s->pb, size, level);
1498 put_bits(&s->pb, 1, 1);
1503 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
1504 UINT8 *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
1506 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
1511 /* mpeg4 based DC predictor */
1512 mpeg4_encode_dc(dc_pb, intra_dc, n);
1521 last_index = s->block_last_index[n];
1522 last_non_zero = i - 1;
1523 for (; i <= last_index; i++) {
1527 run = i - last_non_zero - 1;
1528 last = (i == last_index);
1535 code = get_rl_index(rl, last, run, level);
1536 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1537 if (code == rl->n) {
1539 level1 = level - rl->max_level[last][run];
1542 code = get_rl_index(rl, last, run, level1);
1543 if (code == rl->n) {
1545 put_bits(ac_pb, 1, 1);
1546 if (level > MAX_LEVEL)
1548 run1 = run - rl->max_run[last][level] - 1;
1551 code = get_rl_index(rl, last, run1, level);
1552 if (code == rl->n) {
1555 put_bits(ac_pb, 1, 1);
1556 put_bits(ac_pb, 1, last);
1557 put_bits(ac_pb, 6, run);
1558 put_bits(ac_pb, 1, 1);
1559 put_bits(ac_pb, 12, slevel & 0xfff);
1560 put_bits(ac_pb, 1, 1);
1563 put_bits(ac_pb, 1, 0);
1564 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1565 put_bits(ac_pb, 1, sign);
1569 put_bits(ac_pb, 1, 0);
1570 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1571 put_bits(ac_pb, 1, sign);
1574 put_bits(ac_pb, 1, sign);
1583 /***********************************************/
1586 static VLC intra_MCBPC_vlc;
1587 static VLC inter_MCBPC_vlc;
1588 static VLC cbpy_vlc;
1590 static VLC dc_lum, dc_chrom;
1591 static VLC sprite_trajectory;
1592 static VLC mb_type_b_vlc;
1594 void init_rl(RLTable *rl)
1596 INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
1597 UINT8 index_run[MAX_RUN+1];
1598 int last, run, level, start, end, i;
1600 /* compute max_level[], max_run[] and index_run[] */
1601 for(last=0;last<2;last++) {
1610 memset(max_level, 0, MAX_RUN + 1);
1611 memset(max_run, 0, MAX_LEVEL + 1);
1612 memset(index_run, rl->n, MAX_RUN + 1);
1613 for(i=start;i<end;i++) {
1614 run = rl->table_run[i];
1615 level = rl->table_level[i];
1616 if (index_run[run] == rl->n)
1618 if (level > max_level[run])
1619 max_level[run] = level;
1620 if (run > max_run[level])
1621 max_run[level] = run;
1623 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1624 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1625 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1626 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1627 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1628 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1632 void init_vlc_rl(RLTable *rl)
1636 init_vlc(&rl->vlc, 9, rl->n + 1,
1637 &rl->table_vlc[0][1], 4, 2,
1638 &rl->table_vlc[0][0], 4, 2);
1641 for(q=0; q<32; q++){
1650 rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
1651 for(i=0; i<rl->vlc.table_size; i++){
1652 int code= rl->vlc.table[i][0];
1653 int len = rl->vlc.table[i][1];
1656 if(len==0){ // illegal code
1659 }else if(len<0){ //more bits needed
1663 if(code==rl->n){ //esc
1667 run= rl->table_run [code] + 1;
1668 level= rl->table_level[code] * qmul + qadd;
1669 if(code >= rl->last) run+=192;
1672 rl->rl_vlc[q][i].len= len;
1673 rl->rl_vlc[q][i].level= level;
1674 rl->rl_vlc[q][i].run= run;
1681 /* XXX: find a better solution to handle static init */
1682 void h263_decode_init_vlc(MpegEncContext *s)
1684 static int done = 0;
1689 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 8,
1690 intra_MCBPC_bits, 1, 1,
1691 intra_MCBPC_code, 1, 1);
1692 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 25,
1693 inter_MCBPC_bits, 1, 1,
1694 inter_MCBPC_code, 1, 1);
1695 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1696 &cbpy_tab[0][1], 2, 1,
1697 &cbpy_tab[0][0], 2, 1);
1698 init_vlc(&mv_vlc, MV_VLC_BITS, 33,
1700 &mvtab[0][0], 2, 1);
1703 init_rl(&rl_intra_aic);
1704 init_vlc_rl(&rl_inter);
1705 init_vlc_rl(&rl_intra);
1706 init_vlc_rl(&rl_intra_aic);
1707 init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
1708 &DCtab_lum[0][1], 2, 1,
1709 &DCtab_lum[0][0], 2, 1);
1710 init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
1711 &DCtab_chrom[0][1], 2, 1,
1712 &DCtab_chrom[0][0], 2, 1);
1713 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
1714 &sprite_trajectory_tab[0][1], 4, 2,
1715 &sprite_trajectory_tab[0][0], 4, 2);
1716 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
1717 &mb_type_b_tab[0][1], 2, 1,
1718 &mb_type_b_tab[0][0], 2, 1);
1722 int h263_decode_gob_header(MpegEncContext *s)
1724 unsigned int val, gfid;
1726 /* Check for GOB Start Code */
1727 val = show_bits(&s->gb, 16);
1729 /* We have a GBSC probably with GSTUFF */
1730 skip_bits(&s->gb, 16); /* Drop the zeros */
1731 while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
1733 fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
1735 s->gob_number = get_bits(&s->gb, 5); /* GN */
1736 gfid = get_bits(&s->gb, 2); /* GFID */
1737 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
1739 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
1747 static inline void memsetw(short *tab, int val, int n)
1754 void ff_mpeg4_init_partitions(MpegEncContext *s)
1756 init_put_bits(&s->tex_pb, s->tex_pb_buffer, PB_BUFFER_SIZE, NULL, NULL);
1757 init_put_bits(&s->pb2 , s->pb2_buffer , PB_BUFFER_SIZE, NULL, NULL);
1760 void ff_mpeg4_merge_partitions(MpegEncContext *s)
1762 const int pb2_len = get_bit_count(&s->pb2 );
1763 const int tex_pb_len= get_bit_count(&s->tex_pb);
1764 const int bits= get_bit_count(&s->pb);
1766 if(s->pict_type==I_TYPE){
1767 put_bits(&s->pb, 19, DC_MARKER);
1768 s->misc_bits+=19 + pb2_len + bits - s->last_bits;
1769 s->i_tex_bits+= tex_pb_len;
1771 put_bits(&s->pb, 17, MOTION_MARKER);
1772 s->misc_bits+=17 + pb2_len;;
1773 s->mv_bits+= bits - s->last_bits;
1774 s->p_tex_bits+= tex_pb_len;
1777 flush_put_bits(&s->pb2);
1778 flush_put_bits(&s->tex_pb);
1780 ff_copy_bits(&s->pb, s->pb2_buffer , pb2_len);
1781 ff_copy_bits(&s->pb, s->tex_pb_buffer, tex_pb_len);
1782 s->last_bits= get_bit_count(&s->pb);
1785 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1787 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1789 ff_mpeg4_stuffing(&s->pb);
1790 if(s->pict_type==I_TYPE)
1791 put_bits(&s->pb, 16, 0);
1792 else if(s->pict_type==B_TYPE)
1793 put_bits(&s->pb, MAX(MAX(s->f_code, s->b_code)+15, 17), 0);
1795 put_bits(&s->pb, s->f_code+15, 0);
1796 put_bits(&s->pb, 1, 1);
1798 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
1799 put_bits(&s->pb, 5, s->qscale);
1800 put_bits(&s->pb, 1, 0); /* no HEC */
1804 * decodes the next video packet and sets s->next_qscale
1805 * returns mb_num of the next packet or <0 if something went wrong
1807 static int decode_video_packet_header(MpegEncContext *s, GetBitContext *gb)
1810 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1811 int header_extension=0, mb_num;
1812 //printf("%X\n", show_bits(&gb, 24));
1813 //printf("parse_video_packet_header\n");
1814 // if(show_aligned_bits(gb, 1, 16) != 0) return -1;
1816 /* is there enough space left for a video packet + header */
1817 if( get_bits_count(gb) > gb->size*8-20) return -1;
1819 //printf("resync at %d %d\n", s->mb_x, s->mb_y);
1820 // skip_bits(gb, 1);
1821 // align_get_bits(gb);
1822 if(get_bits(gb, 16)!=0){
1823 printf("internal error while decoding video packet header\n");
1826 //printf("%X\n", show_bits(gb, 24));
1828 while(!get_bits1(gb) && bits<30) bits++;
1829 if((s->pict_type == P_TYPE || s->pict_type == S_TYPE) && bits != s->f_code-1){
1830 printf("marker does not match f_code (is: %d should be: %d pos: %d end %d x: %d y: %d)\n",
1831 bits+1, s->f_code, get_bits_count(gb), gb->size*8, s->mb_x, s->mb_y);
1833 }else if(s->pict_type == I_TYPE && bits != 0){
1834 printf("marker too long\n");
1836 }else if(s->pict_type == B_TYPE && bits != MAX(MAX(s->f_code, s->b_code)-1, 1)){
1837 printf("marker does not match f/b_code\n");
1840 //printf("%X\n", show_bits(gb, 24));
1842 if(s->shape != RECT_SHAPE){
1843 header_extension= get_bits1(gb);
1844 //FIXME more stuff here
1847 mb_num= get_bits(gb, mb_num_bits);
1848 if(mb_num < s->mb_x + s->mb_y*s->mb_width || mb_num>=s->mb_num){
1849 fprintf(stderr, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_x + s->mb_y*s->mb_width);
1853 if(s->shape != BIN_ONLY_SHAPE){
1854 s->next_resync_qscale= get_bits(gb, 5);
1855 if(s->next_resync_qscale==0)
1856 s->next_resync_qscale= s->qscale;
1857 if(s->next_resync_qscale==0){
1858 fprintf(stderr, "qscale==0\n");
1863 if(s->shape == RECT_SHAPE){
1864 header_extension= get_bits1(gb);
1866 if(header_extension){
1869 printf("header extension not supported\n");
1872 while (get_bits1(gb) != 0)
1875 check_marker(gb, "before time_increment in video packed header");
1876 time_increment= get_bits(gb, s->time_increment_bits);
1877 if(s->pict_type!=B_TYPE){
1878 s->last_time_base= s->time_base;
1879 s->time_base+= time_incr;
1880 s->time= s->time_base*s->time_increment_resolution + time_increment;
1881 s->pp_time= s->time - s->last_non_b_time;
1882 s->last_non_b_time= s->time;
1884 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
1885 s->bp_time= s->last_non_b_time - s->time;
1887 check_marker(gb, "before vop_coding_type in video packed header");
1889 skip_bits(gb, 2); /* vop coding type */
1890 //FIXME not rect stuff here
1892 if(s->shape != BIN_ONLY_SHAPE){
1893 skip_bits(gb, 3); /* intra dc vlc threshold */
1895 if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE && s->num_sprite_warping_points){
1896 mpeg4_decode_sprite_trajectory(s);
1899 //FIXME reduced res stuff here
1901 if (s->pict_type != I_TYPE) {
1902 s->f_code = get_bits(gb, 3); /* fcode_for */
1904 printf("Error, video packet header damaged or not MPEG4 header (f_code=0)\n");
1905 return -1; // makes no sense to continue, as the MV decoding will break very quickly
1908 if (s->pict_type == B_TYPE) {
1909 s->b_code = get_bits(gb, 3);
1913 //FIXME new-pred stuff
1915 //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));
1920 void ff_mpeg4_clean_buffers(MpegEncContext *s)
1922 int c_wrap, c_xy, l_wrap, l_xy;
1924 l_wrap= s->block_wrap[0];
1925 l_xy= s->mb_y*l_wrap*2 + s->mb_x*2;
1926 c_wrap= s->block_wrap[4];
1927 c_xy= s->mb_y*c_wrap + s->mb_x;
1930 memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
1931 memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
1932 memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
1935 memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(INT16));
1936 memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(INT16));
1937 memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(INT16));
1940 // we cant clear the MVs as they might be needed by a b frame
1941 // memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(INT16));
1942 // memset(s->motion_val, 0, 2*sizeof(INT16)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
1943 s->last_mv[0][0][0]=
1944 s->last_mv[0][0][1]=
1945 s->last_mv[1][0][0]=
1946 s->last_mv[1][0][1]= 0;
1949 /* searches for the next resync marker clears ac,dc,mc, and sets s->next_resync_gb, s->mb_num_left */
1950 int ff_mpeg4_resync(MpegEncContext *s)
1954 /* search & parse next resync marker */
1956 gb= s->next_resync_gb;
1957 align_get_bits(&gb);
1958 //printf("mpeg4_resync %d next:%d \n", get_bits_count(&gb), get_bits_count(&s->next_resync_gb));
1960 int v= show_bits(&gb, 24);
1961 if( get_bits_count(&gb) >= gb.size*8-24 || v == 1 /* start-code */){
1962 s->mb_num_left= s->mb_num - s->mb_x - s->mb_y*s->mb_width;
1963 //printf("mpeg4_resync end\n");
1964 s->gb= s->next_resync_gb; //continue at the next resync marker
1966 }else if(v>>8 == 0){
1968 s->next_resync_pos= get_bits_count(&gb);
1970 next= decode_video_packet_header(s, &gb);
1972 s->mb_num_left= next - s->mb_x - s->mb_y*s->mb_width;
1976 align_get_bits(&gb);
1980 s->next_resync_gb=gb;
1985 static inline void init_block_index(MpegEncContext *s)
1987 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1988 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
1989 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
1990 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
1991 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
1992 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;
1995 static inline void update_block_index(MpegEncContext *s)
1997 s->block_index[0]+=2;
1998 s->block_index[1]+=2;
1999 s->block_index[2]+=2;
2000 s->block_index[3]+=2;
2001 s->block_index[4]++;
2002 s->block_index[5]++;
2006 * decodes the first & second partition
2007 * returns error type or 0 if no error
2009 int ff_mpeg4_decode_partitions(MpegEncContext *s)
2011 static const INT8 quant_tab[4] = { -1, -2, 1, 2 };
2014 /* decode first partition */
2016 s->first_slice_line=1;
2017 s->mb_x= s->resync_mb_x;
2018 for(s->mb_y= s->resync_mb_y; mb_num < s->mb_num_left; s->mb_y++){
2019 init_block_index(s);
2020 for(; mb_num < s->mb_num_left && s->mb_x<s->mb_width; s->mb_x++){
2021 const int xy= s->mb_x + s->mb_y*s->mb_width;
2026 update_block_index(s);
2027 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
2028 s->first_slice_line=0;
2030 if(s->mb_x==0) PRINT_MB_TYPE("\n");
2032 if(s->pict_type==I_TYPE){
2036 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2038 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2039 return DECODING_DESYNC;
2041 s->cbp_table[xy]= cbpc & 3;
2042 s->mb_type[xy]= MB_TYPE_INTRA;
2046 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2049 else if (s->qscale > 31)
2053 s->qscale_table[xy]= s->qscale;
2055 s->mbintra_table[xy]= 1;
2058 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
2060 fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2061 return DECODING_DESYNC;
2064 if(dc_pred_dir) dir|=1;
2066 s->pred_dir_table[xy]= dir;
2067 }else{ /* P/S_TYPE */
2068 int mx, my, pred_x, pred_y;
2069 INT16 * const mot_val= s->motion_val[s->block_index[0]];
2070 const int stride= s->block_wrap[0]*2;
2072 if(get_bits1(&s->gb)){
2074 s->mb_type[xy]= MB_TYPE_SKIPED;
2075 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2076 const int a= s->sprite_warping_accuracy;
2078 if(s->divx_version==500 && s->divx_build==413){
2079 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2080 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2082 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2083 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2084 s->mb_type[xy]= MB_TYPE_GMC | MB_TYPE_SKIPED;
2091 mot_val[0 ]= mot_val[2 ]=
2092 mot_val[0+stride]= mot_val[2+stride]= mx;
2093 mot_val[1 ]= mot_val[3 ]=
2094 mot_val[1+stride]= mot_val[3+stride]= my;
2096 if(s->mbintra_table[xy])
2097 ff_clean_intra_table_entries(s);
2101 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
2103 fprintf(stderr, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
2104 return DECODING_DESYNC;
2108 else if (cbpc == 20)
2109 fprintf(stderr, "Stuffing !");
2110 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
2112 s->mb_intra = ((cbpc & 4) != 0);
2116 s->mbintra_table[xy]= 1;
2117 s->mb_type[xy]= MB_TYPE_INTRA;
2118 mot_val[0 ]= mot_val[2 ]=
2119 mot_val[0+stride]= mot_val[2+stride]= 0;
2120 mot_val[1 ]= mot_val[3 ]=
2121 mot_val[1+stride]= mot_val[3+stride]= 0;
2123 if(s->mbintra_table[xy])
2124 ff_clean_intra_table_entries(s);
2126 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
2127 s->mcsel= get_bits1(&s->gb);
2130 if ((cbpc & 16) == 0) {
2132 /* 16x16 motion prediction */
2133 s->mb_type[xy]= MB_TYPE_INTER;
2135 h263_pred_motion(s, 0, &pred_x, &pred_y);
2137 mx = h263_decode_motion(s, pred_x, s->f_code);
2139 const int a= s->sprite_warping_accuracy;
2140 if(s->divx_version==500 && s->divx_build==413){
2141 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2143 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2147 return DECODING_DESYNC;
2150 my = h263_decode_motion(s, pred_y, s->f_code);
2152 const int a= s->sprite_warping_accuracy;
2153 if(s->divx_version==500 && s->divx_build==413){
2154 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2156 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2160 return DECODING_DESYNC;
2161 mot_val[0 ]= mot_val[2 ] =
2162 mot_val[0+stride]= mot_val[2+stride]= mx;
2163 mot_val[1 ]= mot_val[3 ]=
2164 mot_val[1+stride]= mot_val[3+stride]= my;
2168 s->mb_type[xy]= MB_TYPE_INTER4V;
2170 INT16 *mot_val= h263_pred_motion(s, i, &pred_x, &pred_y);
2171 mx = h263_decode_motion(s, pred_x, s->f_code);
2173 return DECODING_DESYNC;
2175 my = h263_decode_motion(s, pred_y, s->f_code);
2177 return DECODING_DESYNC;
2188 if (s->pict_type==I_TYPE && get_bits(&s->gb, 19)!=DC_MARKER ) s->decoding_error= DECODING_DESYNC;
2189 else if(s->pict_type!=I_TYPE && get_bits(&s->gb, 17)!=MOTION_MARKER) s->decoding_error= DECODING_DESYNC;
2190 if(s->decoding_error== DECODING_DESYNC){
2191 fprintf(stderr, "marker missing after first partition at %d %d\n", s->mb_x, s->mb_y);
2192 return DECODING_DESYNC;
2195 /* decode second partition */
2197 s->mb_x= s->resync_mb_x;
2198 for(s->mb_y= s->resync_mb_y; mb_num < s->mb_num_left; s->mb_y++){
2199 init_block_index(s);
2200 for(; mb_num < s->mb_num_left && s->mb_x<s->mb_width; s->mb_x++){
2201 const int xy= s->mb_x + s->mb_y*s->mb_width;
2204 update_block_index(s);
2206 if(s->pict_type==I_TYPE){
2207 int ac_pred= get_bits1(&s->gb);
2208 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2210 fprintf(stderr, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2211 return DECODING_AC_LOST;
2214 s->cbp_table[xy]|= cbpy<<2;
2215 s->pred_dir_table[xy]|= ac_pred<<7;
2216 }else{ /* P || S_TYPE */
2217 if(s->mb_type[xy]&MB_TYPE_INTRA){
2219 int ac_pred = get_bits1(&s->gb);
2220 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2223 fprintf(stderr, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2224 return DECODING_ACDC_LOST;
2227 if(s->cbp_table[xy] & 8) {
2228 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2231 else if (s->qscale > 31)
2235 s->qscale_table[xy]= s->qscale;
2239 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
2241 fprintf(stderr, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
2242 return DECODING_ACDC_LOST;
2245 if(dc_pred_dir) dir|=1;
2247 s->cbp_table[xy]&= 3; //remove dquant
2248 s->cbp_table[xy]|= cbpy<<2;
2249 s->pred_dir_table[xy]= dir | (ac_pred<<7);
2250 }else if(s->mb_type[xy]&MB_TYPE_SKIPED){
2251 s->qscale_table[xy]= s->qscale;
2252 s->cbp_table[xy]= 0;
2254 int cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2257 fprintf(stderr, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
2258 return DECODING_ACDC_LOST;
2261 if(s->cbp_table[xy] & 8) {
2262 //fprintf(stderr, "dquant\n");
2263 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2266 else if (s->qscale > 31)
2270 s->qscale_table[xy]= s->qscale;
2272 s->cbp_table[xy]&= 3; //remove dquant
2273 s->cbp_table[xy]|= (cbpy^0xf)<<2;
2284 static int mpeg4_decode_partitioned_mb(MpegEncContext *s,
2285 DCTELEM block[6][64])
2288 const int xy= s->mb_x + s->mb_y*s->mb_width;
2290 if(s->mb_x==s->resync_mb_x && s->mb_y==s->resync_mb_y){ //Note resync_mb_{x,y}==0 at the start
2292 int block_index_backup[6];
2293 int qscale= s->qscale;
2295 for(i=0; i<6; i++) block_index_backup[i]= s->block_index[i];
2297 s->decoding_error= ff_mpeg4_decode_partitions(s);
2299 for(i=0; i<6; i++) s->block_index[i]= block_index_backup[i];
2300 s->first_slice_line=1;
2301 s->mb_x= s->resync_mb_x;
2302 s->mb_y= s->resync_mb_y;
2306 if(s->decoding_error==DECODING_DESYNC) return -1;
2309 mb_type= s->mb_type[xy];
2310 if(s->decoding_error)
2313 cbp = s->cbp_table[xy];
2315 if(s->decoding_error!=DECODING_ACDC_LOST && s->qscale_table[xy] != s->qscale){
2316 s->qscale= s->qscale_table[xy];
2320 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
2323 s->mv[0][i][0] = s->motion_val[ s->block_index[i] ][0];
2324 s->mv[0][i][1] = s->motion_val[ s->block_index[i] ][1];
2326 s->mb_intra = mb_type&MB_TYPE_INTRA;
2328 if (mb_type&MB_TYPE_SKIPED) {
2331 s->block_last_index[i] = -1;
2332 s->mv_dir = MV_DIR_FORWARD;
2333 s->mv_type = MV_TYPE_16X16;
2334 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2342 }else if(s->mb_intra && s->decoding_error!=DECODING_ACDC_LOST){
2343 s->ac_pred = s->pred_dir_table[xy]>>7;
2345 /* decode each block */
2346 for (i = 0; i < 6; i++) {
2347 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1);
2348 if(ret==DECODING_AC_LOST){
2349 fprintf(stderr, "texture corrupted at %d %d (trying to continue with mc/dc only)\n", s->mb_x, s->mb_y);
2350 s->decoding_error=DECODING_AC_LOST;
2352 }else if(ret==DECODING_ACDC_LOST){
2353 fprintf(stderr, "dc corrupted at %d %d (trying to continue with mc only)\n", s->mb_x, s->mb_y);
2354 s->decoding_error=DECODING_ACDC_LOST;
2358 }else if(!s->mb_intra){
2359 // s->mcsel= 0; //FIXME do we need to init that
2361 s->mv_dir = MV_DIR_FORWARD;
2362 if (mb_type&MB_TYPE_INTER4V) {
2363 s->mv_type = MV_TYPE_8X8;
2365 s->mv_type = MV_TYPE_16X16;
2367 if(s->decoding_error==0 && cbp){
2368 /* decode each block */
2369 for (i = 0; i < 6; i++) {
2370 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1);
2371 if(ret==DECODING_AC_LOST){
2372 fprintf(stderr, "texture corrupted at %d %d (trying to continue with mc/dc only)\n", s->mb_x, s->mb_y);
2373 s->decoding_error=DECODING_AC_LOST;
2379 } else { /* I-Frame */
2382 s->ac_pred = s->pred_dir_table[xy]>>7;
2384 /* decode each block */
2385 for (i = 0; i < 6; i++) {
2386 int ret= mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1);
2387 if(ret==DECODING_AC_LOST){
2388 fprintf(stderr, "texture corrupted at %d %d (trying to continue with dc only)\n", s->mb_x, s->mb_y);
2389 s->decoding_error=DECODING_AC_LOST;
2391 }else if(ret==DECODING_ACDC_LOST){
2392 fprintf(stderr, "dc corrupted at %d %d\n", s->mb_x, s->mb_y);
2402 int h263_decode_mb(MpegEncContext *s,
2403 DCTELEM block[6][64])
2405 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
2407 static INT8 quant_tab[4] = { -1, -2, 1, 2 };
2409 if(s->mb_x==0) PRINT_MB_TYPE("\n");
2411 if(s->resync_marker){
2412 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
2413 s->first_slice_line=0;
2417 if(s->data_partitioning && s->pict_type!=B_TYPE)
2418 return mpeg4_decode_partitioned_mb(s, block);
2420 if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
2421 if (get_bits1(&s->gb)) {
2425 s->block_last_index[i] = -1;
2426 s->mv_dir = MV_DIR_FORWARD;
2427 s->mv_type = MV_TYPE_16X16;
2428 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
2429 const int a= s->sprite_warping_accuracy;
2430 // int l = (1 << (s->f_code - 1)) * 32;
2433 if(s->divx_version==500 && s->divx_build==413){
2434 s->mv[0][0][0] = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2435 s->mv[0][0][1] = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2437 s->mv[0][0][0] = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2438 s->mv[0][0][1] = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2440 /* if (s->mv[0][0][0] < -l) s->mv[0][0][0]= -l;
2441 else if (s->mv[0][0][0] >= l) s->mv[0][0][0]= l-1;
2442 if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l;
2443 else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/
2455 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
2456 //fprintf(stderr, "\tCBPC: %d", cbpc);
2461 else if (cbpc == 20)
2462 fprintf(stderr, "Stuffing !");
2465 s->mb_intra = ((cbpc & 4) != 0);
2466 if (s->mb_intra) goto intra;
2468 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
2469 s->mcsel= get_bits1(&s->gb);
2471 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2472 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
2474 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2477 else if (s->qscale > 31)
2481 s->mv_dir = MV_DIR_FORWARD;
2482 if ((cbpc & 16) == 0) {
2484 /* 16x16 motion prediction */
2485 s->mv_type = MV_TYPE_16X16;
2486 h263_pred_motion(s, 0, &pred_x, &pred_y);
2488 mx = h263p_decode_umotion(s, pred_x);
2490 mx = h263_decode_motion(s, pred_x, s->f_code);
2492 const int a= s->sprite_warping_accuracy;
2493 // int l = (1 << (s->f_code - 1)) * 32;
2494 if(s->divx_version==500 && s->divx_build==413){
2495 mx = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
2497 mx = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
2499 // if (mx < -l) mx= -l, printf("C");
2500 // else if (mx >= l) mx= l-1, printf("C");
2506 my = h263p_decode_umotion(s, pred_y);
2508 my = h263_decode_motion(s, pred_y, s->f_code);
2510 const int a= s->sprite_warping_accuracy;
2511 // int l = (1 << (s->f_code - 1)) * 32;
2512 if(s->divx_version==500 && s->divx_build==413){
2513 my = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
2515 my = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
2517 // if (my < -l) my= -l, printf("C");
2518 // else if (my >= l) my= l-1, printf("C");
2522 s->mv[0][0][0] = mx;
2523 s->mv[0][0][1] = my;
2524 /*fprintf(stderr, "\n MB %d", (s->mb_y * s->mb_width) + s->mb_x);
2525 fprintf(stderr, "\n\tmvx: %d\t\tpredx: %d", mx, pred_x);
2526 fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
2527 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
2528 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2532 s->mv_type = MV_TYPE_8X8;
2534 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
2536 mx = h263p_decode_umotion(s, pred_x);
2538 mx = h263_decode_motion(s, pred_x, s->f_code);
2543 my = h263p_decode_umotion(s, pred_y);
2545 my = h263_decode_motion(s, pred_y, s->f_code);
2548 s->mv[0][i][0] = mx;
2549 s->mv[0][i][1] = my;
2550 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
2551 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
2556 } else if(s->pict_type==B_TYPE) {
2557 int modb1; // first bit of modb
2558 int modb2; // second bit of modb
2564 s->mb_intra = 0; //B-frames never contain intra blocks
2565 s->mcsel=0; // ... true gmc blocks
2568 s->last_mv[0][0][0]=
2569 s->last_mv[0][0][1]=
2570 s->last_mv[1][0][0]=
2571 s->last_mv[1][0][1]= 0;
2574 /* if we skipped it in the future P Frame than skip it now too */
2575 s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC
2580 s->block_last_index[i] = -1;
2582 s->mv_dir = MV_DIR_FORWARD;
2583 s->mv_type = MV_TYPE_16X16;
2588 //FIXME is this correct?
2589 /* s->last_mv[0][0][0]=
2590 s->last_mv[0][0][1]=0;*/
2595 modb1= get_bits1(&s->gb);
2597 modb2= get_bits1(&s->gb);
2598 mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
2599 if(modb2==0) cbp= get_bits(&s->gb, 6);
2601 if (mb_type && cbp) {
2602 if(get_bits1(&s->gb)){
2603 s->qscale +=get_bits1(&s->gb)*4 - 2;
2606 else if (s->qscale > 31)
2612 mb_type=4; //like 0 but no vectors coded
2615 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
2616 mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my
2619 case 0: /* direct */
2620 mx = h263_decode_motion(s, 0, 1);
2621 my = h263_decode_motion(s, 0, 1);
2622 case 4: /* direct with mx=my=0 */
2623 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2624 xy= s->block_index[0];
2625 time_pp= s->pp_time;
2626 time_pb= time_pp - s->bp_time;
2627 //if(time_pp>3000 )printf("%d %d ", time_pp, time_pb);
2629 //FIXME avoid divides
2630 s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
2631 s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
2632 s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
2633 : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx;
2634 s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1]
2635 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
2639 s->mv[1][0][1] = 1000;*/
2643 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2644 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2645 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2646 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2647 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2649 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2650 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2651 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2652 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2656 s->mv_dir = MV_DIR_BACKWARD;
2657 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2658 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2659 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2660 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2664 s->mv_dir = MV_DIR_FORWARD;
2665 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2666 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2667 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2668 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2672 printf("illegal MB_type\n");
2675 } else { /* I-Frame */
2676 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
2684 if (s->h263_pred || s->h263_aic) {
2685 s->ac_pred = get_bits1(&s->gb);
2686 if (s->ac_pred && s->h263_aic)
2687 s->h263_aic_dir = get_bits1(&s->gb);
2689 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
2690 if(cbpy<0) return -1;
2691 cbp = (cbpc & 3) | (cbpy << 2);
2693 s->qscale += quant_tab[get_bits(&s->gb, 2)];
2696 else if (s->qscale > 31)
2702 /* decode each block */
2704 for (i = 0; i < 6; i++) {
2705 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
2709 for (i = 0; i < 6; i++) {
2710 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
2717 static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
2719 int code, val, sign, shift, l, m;
2721 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
2727 sign = get_bits1(&s->gb);
2729 val = (code - 1) << shift;
2731 val |= get_bits(&s->gb, shift);
2737 /* modulo decoding */
2738 if (!s->h263_long_vectors) {
2739 l = (1 << (f_code - 1)) * 32;
2743 } else if (val >= l) {
2747 /* horrible h263 long vector mode */
2748 if (pred < -31 && val < -63)
2750 if (pred > 32 && val > 63)
2757 /* Decodes RVLC of H.263+ UMV */
2758 static int h263p_decode_umotion(MpegEncContext * s, int pred)
2762 if (get_bits1(&s->gb)) /* Motion difference = 0 */
2765 code = 2 + get_bits1(&s->gb);
2767 while (get_bits1(&s->gb))
2770 code += get_bits1(&s->gb);
2775 code = (sign) ? (pred - code) : (pred + code);
2777 fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
2783 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
2786 int code, level, i, j, last, run;
2787 RLTable *rl = &rl_inter;
2788 const UINT8 *scan_table;
2790 scan_table = zigzag_direct;
2791 if (s->h263_aic && s->mb_intra) {
2795 if (s->h263_aic_dir)
2796 scan_table = ff_alternate_vertical_scan; /* left */
2798 scan_table = ff_alternate_horizontal_scan; /* top */
2800 } else if (s->mb_intra) {
2802 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
2803 int component, diff;
2804 component = (n <= 3 ? 0 : n - 4 + 1);
2805 level = s->last_dc[component];
2806 if (s->rv10_first_dc_coded[component]) {
2807 diff = rv_decode_dc(s, n);
2811 level = level & 0xff; /* handle wrap round */
2812 s->last_dc[component] = level;
2814 s->rv10_first_dc_coded[component] = 1;
2817 level = get_bits(&s->gb, 8);
2827 if (s->mb_intra && s->h263_aic)
2829 s->block_last_index[n] = i - 1;
2834 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
2837 if (code == rl->n) {
2839 last = get_bits1(&s->gb);
2840 run = get_bits(&s->gb, 6);
2841 level = (INT8)get_bits(&s->gb, 8);
2842 if (s->h263_rv10 && level == -128) {
2843 /* XXX: should patch encoder too */
2844 level = get_bits(&s->gb, 12);
2845 level = (level << 20) >> 20;
2848 run = rl->table_run[code];
2849 level = rl->table_level[code];
2850 last = code >= rl->last;
2851 if (get_bits1(&s->gb))
2864 if (s->mb_intra && s->h263_aic) {
2865 h263_pred_acdc(s, block, n);
2868 s->block_last_index[n] = i;
2872 static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
2874 int level, pred, code;
2878 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
2880 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
2881 if (code < 0 || code > 9 /* && s->nbit<9 */){
2882 fprintf(stderr, "illegal dc vlc\n");
2888 level = get_bits(&s->gb, code);
2889 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
2890 level = - (level ^ ((1 << code) - 1));
2892 if(get_bits1(&s->gb)==0){ /* marker */
2893 fprintf(stderr, "dc marker bit missing\n");
2899 pred = ff_mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
2904 *dc_val = level * s->y_dc_scale;
2906 *dc_val = level * s->c_dc_scale;
2913 * returns 0 if everything went ok
2914 * returns DECODING_AC_LOST if an error was detected during AC decoding
2915 * returns DECODING_ACDC_LOST if an error was detected during DC decoding
2917 static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
2920 int level, i, last, run;
2923 RL_VLC_ELEM *rl_vlc;
2924 const UINT8 *scan_table;
2929 if(s->data_partitioning && s->pict_type!=B_TYPE){
2930 level = s->dc_val[0][ s->block_index[n] ];
2931 if(n<4) level= (level + (s->y_dc_scale>>1))/s->y_dc_scale; //FIXME optimizs
2932 else level= (level + (s->c_dc_scale>>1))/s->c_dc_scale;
2933 dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_width]<<n)&32;
2935 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
2937 return DECODING_ACDC_LOST;
2944 rl_vlc = rl_intra.rl_vlc[0];
2946 if (dc_pred_dir == 0)
2947 scan_table = ff_alternate_vertical_scan; /* left */
2949 scan_table = ff_alternate_horizontal_scan; /* top */
2951 scan_table = zigzag_direct;
2958 s->block_last_index[n] = i;
2962 rl_vlc = rl_inter.rl_vlc[s->qscale];
2963 scan_table = zigzag_direct;
2964 qmul = s->qscale << 1;
2965 qadd = (s->qscale - 1) | 1;
2968 OPEN_READER(re, &s->gb);
2970 UPDATE_CACHE(re, &s->gb);
2971 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
2974 cache= GET_CACHE(re, &s->gb);
2976 if (cache&0x80000000) {
2977 if (cache&0x40000000) {
2979 SKIP_CACHE(re, &s->gb, 2);
2980 last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
2981 run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
2982 SKIP_COUNTER(re, &s->gb, 2+1+6);
2983 UPDATE_CACHE(re, &s->gb);
2985 if(SHOW_UBITS(re, &s->gb, 1)==0){
2986 fprintf(stderr, "1. marker bit missing in 3. esc\n");
2987 return DECODING_AC_LOST;
2988 }; SKIP_CACHE(re, &s->gb, 1);
2990 level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
2992 if(SHOW_UBITS(re, &s->gb, 1)==0){
2993 fprintf(stderr, "2. marker bit missing in 3. esc\n");
2994 return DECODING_AC_LOST;
2995 }; LAST_SKIP_CACHE(re, &s->gb, 1);
2997 SKIP_COUNTER(re, &s->gb, 1+12+1);
2999 if(level>512 || level<-512){ //FIXME check that QP=1 is ok with this too
3000 fprintf(stderr, "|level| overflow in 3. esc\n");
3001 return DECODING_AC_LOST;
3005 const int abs_level= ABS(level);
3006 if(abs_level<=MAX_LEVEL && run<=MAX_RUN && s->error_resilience>=0){
3007 const int run1= run - rl->max_run[last][abs_level] - 1;
3008 if(abs_level <= rl->max_level[last][run]){
3009 fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
3010 return DECODING_AC_LOST;
3012 if(abs_level <= rl->max_level[last][run]*2){
3013 fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
3014 return DECODING_AC_LOST;
3016 if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
3017 fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
3018 return DECODING_AC_LOST;
3023 if (level>0) level= level * qmul + qadd;
3024 else level= level * qmul - qadd;
3030 #if MIN_CACHE_BITS < 20
3031 LAST_SKIP_BITS(re, &s->gb, 2);
3032 UPDATE_CACHE(re, &s->gb);
3034 SKIP_BITS(re, &s->gb, 2);
3036 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3037 i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
3038 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3039 LAST_SKIP_BITS(re, &s->gb, 1);
3043 #if MIN_CACHE_BITS < 19
3044 LAST_SKIP_BITS(re, &s->gb, 1);
3045 UPDATE_CACHE(re, &s->gb);
3047 SKIP_BITS(re, &s->gb, 1);
3049 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2);
3051 level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
3052 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3053 LAST_SKIP_BITS(re, &s->gb, 1);
3057 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
3058 LAST_SKIP_BITS(re, &s->gb, 1);
3063 fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3064 return DECODING_AC_LOST;
3067 block[scan_table[i]] = level;
3071 block[scan_table[i]] = level;
3073 CLOSE_READER(re, &s->gb);
3077 mpeg4_pred_ac(s, block, n, dc_pred_dir);
3079 i = 63; /* XXX: not optimal */
3082 s->block_last_index[n] = i;
3086 /* most is hardcoded. should extend to handle all h263 streams */
3087 int h263_decode_picture_header(MpegEncContext *s)
3089 int format, width, height;
3091 /* picture start code */
3092 if (get_bits(&s->gb, 22) != 0x20) {
3093 fprintf(stderr, "Bad picture start code\n");
3096 /* temporal reference */
3097 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
3099 /* PTYPE starts here */
3100 if (get_bits1(&s->gb) != 1) {
3102 fprintf(stderr, "Bad marker\n");
3105 if (get_bits1(&s->gb) != 0) {
3106 fprintf(stderr, "Bad H263 id\n");
3107 return -1; /* h263 id */
3109 skip_bits1(&s->gb); /* split screen off */
3110 skip_bits1(&s->gb); /* camera off */
3111 skip_bits1(&s->gb); /* freeze picture release off */
3113 /* Reset GOB number */
3116 format = get_bits(&s->gb, 3);
3121 7 extended PTYPE (PLUSPTYPE)
3124 if (format != 7 && format != 6) {
3127 width = h263_format[format][0];
3128 height = h263_format[format][1];
3134 s->pict_type = I_TYPE + get_bits1(&s->gb);
3136 s->unrestricted_mv = get_bits1(&s->gb);
3137 s->h263_long_vectors = s->unrestricted_mv;
3139 if (get_bits1(&s->gb) != 0) {
3140 fprintf(stderr, "H263 SAC not supported\n");
3141 return -1; /* SAC: off */
3143 if (get_bits1(&s->gb) != 0) {
3144 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3147 if (get_bits1(&s->gb) != 0) {
3148 fprintf(stderr, "H263 PB frame not supported\n");
3149 return -1; /* not PB frame */
3151 s->qscale = get_bits(&s->gb, 5);
3152 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
3158 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
3160 /* ufep other than 0 and 1 are reserved */
3163 format = get_bits(&s->gb, 3);
3164 dprintf("ufep=1, format: %d\n", format);
3165 skip_bits(&s->gb,1); /* Custom PCF */
3166 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
3167 skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
3168 if (get_bits1(&s->gb) != 0) {
3169 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
3171 if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
3175 skip_bits(&s->gb, 7);
3176 /* these are the 7 bits: (in order of appearence */
3177 /* Deblocking Filter */
3178 /* Slice Structured */
3179 /* Reference Picture Selection */
3180 /* Independent Segment Decoding */
3181 /* Alternative Inter VLC */
3182 /* Modified Quantization */
3183 /* Prevent start code emulation */
3185 skip_bits(&s->gb, 3); /* Reserved */
3186 } else if (ufep != 0) {
3187 fprintf(stderr, "Bad UFEP type (%d)\n", ufep);
3192 s->pict_type = get_bits(&s->gb, 3) + I_TYPE;
3193 dprintf("pict_type: %d\n", s->pict_type);
3194 if (s->pict_type != I_TYPE &&
3195 s->pict_type != P_TYPE)
3197 skip_bits(&s->gb, 2);
3198 s->no_rounding = get_bits1(&s->gb);
3199 dprintf("RTYPE: %d\n", s->no_rounding);
3200 skip_bits(&s->gb, 4);
3202 /* Get the picture dimensions */
3205 /* Custom Picture Format (CPFMT) */
3206 s->aspect_ratio_info = get_bits(&s->gb, 4);
3207 dprintf("aspect: %d\n", s->aspect_ratio_info);
3212 3 - 10:11 (525-type 4:3)
3213 4 - 16:11 (CIF 16:9)
3214 5 - 40:33 (525-type 16:9)
3217 width = (get_bits(&s->gb, 9) + 1) * 4;
3219 height = get_bits(&s->gb, 9) * 4;
3220 dprintf("\nH.263+ Custom picture: %dx%d\n",width,height);
3221 if (s->aspect_ratio_info == EXTENDED_PAR) {
3222 /* aspected dimensions */
3223 skip_bits(&s->gb, 8); /* width */
3224 skip_bits(&s->gb, 8); /* height */
3227 width = h263_format[format][0];
3228 height = h263_format[format][1];
3230 if ((width == 0) || (height == 0))
3234 if (s->umvplus_dec) {
3235 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
3239 s->qscale = get_bits(&s->gb, 5);
3242 while (get_bits1(&s->gb) != 0) {
3243 skip_bits(&s->gb, 8);
3248 s->y_dc_scale_table=
3249 s->c_dc_scale_table= h263_aic_dc_scale_table;
3251 s->y_dc_scale_table=
3252 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
3258 static void mpeg4_decode_sprite_trajectory(MpegEncContext * s)
3261 int a= 2<<s->sprite_warping_accuracy;
3262 int rho= 3-s->sprite_warping_accuracy;
3264 const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
3265 int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
3266 int sprite_ref[4][2];
3267 int virtual_ref[2][2];
3269 int alpha=0, beta=0;
3272 //printf("SP %d\n", s->sprite_warping_accuracy);
3273 for(i=0; i<s->num_sprite_warping_points; i++){
3277 length= get_vlc(&s->gb, &sprite_trajectory);
3279 x= get_bits(&s->gb, length);
3280 //printf("lx %d %d\n", length, x);
3281 if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
3282 x = - (x ^ ((1 << length) - 1));
3284 if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(&s->gb); /* marker bit */
3286 length= get_vlc(&s->gb, &sprite_trajectory);
3288 y=get_bits(&s->gb, length);
3289 //printf("ly %d %d\n", length, y);
3290 if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
3291 y = - (y ^ ((1 << length) - 1));
3293 skip_bits1(&s->gb); /* marker bit */
3294 //printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy);
3295 //if(i>0 && (x!=0 || y!=0)) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
3301 while((1<<alpha)<w) alpha++;
3302 while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
3306 // Note, the 4th point isnt used for GMC
3307 if(s->divx_version==500 && s->divx_build==413){
3308 sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
3309 sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
3310 sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
3311 sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
3312 sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
3313 sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
3315 sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
3316 sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
3317 sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
3318 sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
3319 sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
3320 sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
3322 /* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
3323 sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
3325 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
3326 // perhaps it should be reordered to be more readable ...
3327 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
3328 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
3329 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
3330 + 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);
3331 virtual_ref[0][1]= 16*vop_ref[0][1]
3332 + 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);
3333 virtual_ref[1][0]= 16*vop_ref[0][0]
3334 + 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);
3335 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
3336 + 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);
3338 switch(s->num_sprite_warping_points)
3341 s->sprite_offset[0][0]= 0;
3342 s->sprite_offset[0][1]= 0;
3343 s->sprite_offset[1][0]= 0;
3344 s->sprite_offset[1][1]= 0;
3345 s->sprite_delta[0][0][0]= a;
3346 s->sprite_delta[0][0][1]= 0;
3347 s->sprite_delta[0][1][0]= 0;
3348 s->sprite_delta[0][1][1]= a;
3349 s->sprite_delta[1][0][0]= a;
3350 s->sprite_delta[1][0][1]= 0;
3351 s->sprite_delta[1][1][0]= 0;
3352 s->sprite_delta[1][1][1]= a;
3353 s->sprite_shift[0][0]= 0;
3354 s->sprite_shift[0][1]= 0;
3355 s->sprite_shift[1][0]= 0;
3356 s->sprite_shift[1][1]= 0;
3359 s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
3360 s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
3361 s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
3362 s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
3363 s->sprite_delta[0][0][0]= a;
3364 s->sprite_delta[0][0][1]= 0;
3365 s->sprite_delta[0][1][0]= 0;
3366 s->sprite_delta[0][1][1]= a;
3367 s->sprite_delta[1][0][0]= a;
3368 s->sprite_delta[1][0][1]= 0;
3369 s->sprite_delta[1][1][0]= 0;
3370 s->sprite_delta[1][1][1]= a;
3371 s->sprite_shift[0][0]= 0;
3372 s->sprite_shift[0][1]= 0;
3373 s->sprite_shift[1][0]= 0;
3374 s->sprite_shift[1][1]= 0;
3378 s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
3379 + ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
3380 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1]));
3381 s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
3382 + ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
3383 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1]));
3384 s->sprite_offset[1][0]= ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
3385 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
3386 +2*w2*r*sprite_ref[0][0] - 16*w2);
3387 s->sprite_offset[1][1]= ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
3388 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
3389 +2*w2*r*sprite_ref[0][1] - 16*w2);
3390 s->sprite_delta[0][0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
3391 s->sprite_delta[0][0][1]= ( r*sprite_ref[0][1] - virtual_ref[0][1]);
3392 s->sprite_delta[0][1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
3393 s->sprite_delta[0][1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
3394 s->sprite_delta[1][0][0]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
3395 s->sprite_delta[1][0][1]= 4*( r*sprite_ref[0][1] - virtual_ref[0][1]);
3396 s->sprite_delta[1][1][0]= 4*(-r*sprite_ref[0][1] + virtual_ref[0][1]);
3397 s->sprite_delta[1][1][1]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
3398 s->sprite_shift[0][0]= alpha+rho;
3399 s->sprite_shift[0][1]= alpha+rho;
3400 s->sprite_shift[1][0]= alpha+rho+2;
3401 s->sprite_shift[1][1]= alpha+rho+2;
3406 /*printf("%d %d\n", s->sprite_delta[0][0][0], a<<s->sprite_shift[0][0]);
3407 printf("%d %d\n", s->sprite_delta[0][0][1], 0);
3408 printf("%d %d\n", s->sprite_delta[0][1][0], 0);
3409 printf("%d %d\n", s->sprite_delta[0][1][1], a<<s->sprite_shift[0][1]);
3410 printf("%d %d\n", s->sprite_delta[1][0][0], a<<s->sprite_shift[1][0]);
3411 printf("%d %d\n", s->sprite_delta[1][0][1], 0);
3412 printf("%d %d\n", s->sprite_delta[1][1][0], 0);
3413 printf("%d %d\n", s->sprite_delta[1][1][1], a<<s->sprite_shift[1][1]);*/
3414 /* try to simplify the situation */
3415 if( s->sprite_delta[0][0][0] == a<<s->sprite_shift[0][0]
3416 && s->sprite_delta[0][0][1] == 0
3417 && s->sprite_delta[0][1][0] == 0
3418 && s->sprite_delta[0][1][1] == a<<s->sprite_shift[0][1]
3419 && s->sprite_delta[1][0][0] == a<<s->sprite_shift[1][0]
3420 && s->sprite_delta[1][0][1] == 0
3421 && s->sprite_delta[1][1][0] == 0
3422 && s->sprite_delta[1][1][1] == a<<s->sprite_shift[1][1])
3424 s->sprite_offset[0][0]>>=s->sprite_shift[0][0];
3425 s->sprite_offset[0][1]>>=s->sprite_shift[0][1];
3426 s->sprite_offset[1][0]>>=s->sprite_shift[1][0];
3427 s->sprite_offset[1][1]>>=s->sprite_shift[1][1];
3428 s->sprite_delta[0][0][0]= a;
3429 s->sprite_delta[0][0][1]= 0;
3430 s->sprite_delta[0][1][0]= 0;
3431 s->sprite_delta[0][1][1]= a;
3432 s->sprite_delta[1][0][0]= a;
3433 s->sprite_delta[1][0][1]= 0;
3434 s->sprite_delta[1][1][0]= 0;
3435 s->sprite_delta[1][1][1]= a;
3436 s->sprite_shift[0][0]= 0;
3437 s->sprite_shift[0][1]= 0;
3438 s->sprite_shift[1][0]= 0;
3439 s->sprite_shift[1][1]= 0;
3440 s->real_sprite_warping_points=1;
3443 s->real_sprite_warping_points= s->num_sprite_warping_points;
3445 //printf("%d %d %d %d\n", d[0][0], d[0][1], s->sprite_offset[0][0], s->sprite_offset[0][1]);
3448 /* decode mpeg4 VOP header */
3449 int mpeg4_decode_picture_header(MpegEncContext * s)
3451 int time_incr, startcode, state, v;
3455 /* search next start code */
3456 align_get_bits(&s->gb);
3459 v = get_bits(&s->gb, 8);
3460 if (state == 0x000001) {
3461 state = ((state << 8) | v) & 0xffffff;
3465 state = ((state << 8) | v) & 0xffffff;
3466 if( get_bits_count(&s->gb) > s->gb.size*8-32){
3468 printf("no VOP startcode found, frame size was=%d\n", s->gb.size);
3471 printf("frame skip\n");
3472 return FRAME_SKIPED;
3476 //printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));
3477 if (startcode == 0x120) { // Video Object Layer
3478 int width, height, vo_ver_id;
3481 skip_bits(&s->gb, 1); /* random access */
3482 s->vo_type= get_bits(&s->gb, 8);
3483 if (get_bits1(&s->gb) != 0) { /* is_ol_id */
3484 vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
3485 skip_bits(&s->gb, 3); /* vo_priority */
3489 //printf("vo type:%d\n",s->vo_type);
3490 s->aspect_ratio_info= get_bits(&s->gb, 4);
3491 if(s->aspect_ratio_info == EXTENDED_PAR){
3492 skip_bits(&s->gb, 8); //par_width
3493 skip_bits(&s->gb, 8); // par_height
3496 if ((s->vol_control_parameters=get_bits1(&s->gb))) { /* vol control parameter */
3497 int chroma_format= get_bits(&s->gb, 2);
3498 if(chroma_format!=1){
3499 printf("illegal chroma format\n");
3501 s->low_delay= get_bits1(&s->gb);
3502 if(get_bits1(&s->gb)){ /* vbv parameters */
3503 printf("vbv parameters not supported\n");
3507 // set low delay flag only once so the smart? low delay detection wont be overriden
3508 if(s->picture_number==0)
3512 s->shape = get_bits(&s->gb, 2); /* vol shape */
3513 if(s->shape != RECT_SHAPE) printf("only rectangular vol supported\n");
3514 if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
3515 printf("Gray shape not supported\n");
3516 skip_bits(&s->gb, 4); //video_object_layer_shape_extension
3519 skip_bits1(&s->gb); /* marker */
3521 s->time_increment_resolution = get_bits(&s->gb, 16);
3522 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
3523 if (s->time_increment_bits < 1)
3524 s->time_increment_bits = 1;
3525 skip_bits1(&s->gb); /* marker */
3527 if (get_bits1(&s->gb) != 0) { /* fixed_vop_rate */
3528 skip_bits(&s->gb, s->time_increment_bits);
3531 if (s->shape != BIN_ONLY_SHAPE) {
3532 if (s->shape == RECT_SHAPE) {
3533 skip_bits1(&s->gb); /* marker */
3534 width = get_bits(&s->gb, 13);
3535 skip_bits1(&s->gb); /* marker */
3536 height = get_bits(&s->gb, 13);
3537 skip_bits1(&s->gb); /* marker */
3538 if(width && height){ /* they should be non zero but who knows ... */
3541 // printf("width/height: %d %d\n", width, height);
3545 if(get_bits1(&s->gb)) printf("interlaced not supported\n"); /* interlaced */
3546 if(!get_bits1(&s->gb)) printf("OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
3547 if (vo_ver_id == 1) {
3548 s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
3550 s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
3552 if(s->vol_sprite_usage==STATIC_SPRITE) printf("Static Sprites not supported\n");
3553 if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
3554 if(s->vol_sprite_usage==STATIC_SPRITE){
3555 s->sprite_width = get_bits(&s->gb, 13);
3556 skip_bits1(&s->gb); /* marker */
3557 s->sprite_height= get_bits(&s->gb, 13);
3558 skip_bits1(&s->gb); /* marker */
3559 s->sprite_left = get_bits(&s->gb, 13);
3560 skip_bits1(&s->gb); /* marker */
3561 s->sprite_top = get_bits(&s->gb, 13);
3562 skip_bits1(&s->gb); /* marker */
3564 s->num_sprite_warping_points= get_bits(&s->gb, 6);
3565 s->sprite_warping_accuracy = get_bits(&s->gb, 2);
3566 s->sprite_brightness_change= get_bits1(&s->gb);
3567 if(s->vol_sprite_usage==STATIC_SPRITE)
3568 s->low_latency_sprite= get_bits1(&s->gb);
3570 // FIXME sadct disable bit if verid!=1 && shape not rect
3572 if (get_bits1(&s->gb) == 1) { /* not_8_bit */
3573 s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
3574 if(get_bits(&s->gb, 4)!=8) printf("N-bit not supported\n"); /* bits_per_pixel */
3575 if(s->quant_precision!=5) printf("quant precission %d\n", s->quant_precision);
3577 s->quant_precision = 5;
3580 // FIXME a bunch of grayscale shape things
3582 if(get_bits1(&s->gb)){ /* vol_quant_type */
3584 /* load default matrixes */
3585 for(i=0; i<64; i++){
3586 v= ff_mpeg4_default_intra_matrix[i];
3587 s->intra_matrix[i]= v;
3588 s->chroma_intra_matrix[i]= v;
3590 v= ff_mpeg4_default_non_intra_matrix[i];
3591 s->inter_matrix[i]= v;
3592 s->chroma_inter_matrix[i]= v;
3595 /* load custom intra matrix */
3596 if(get_bits1(&s->gb)){
3597 for(i=0; i<64; i++){
3598 v= get_bits(&s->gb, 8);
3601 j= zigzag_direct[i];
3602 s->intra_matrix[j]= v;
3603 s->chroma_intra_matrix[j]= v;
3607 /* load custom non intra matrix */
3608 if(get_bits1(&s->gb)){
3609 for(i=0; i<64; i++){
3610 v= get_bits(&s->gb, 8);
3613 j= zigzag_direct[i];
3614 s->inter_matrix[j]= v;
3615 s->chroma_inter_matrix[j]= v;
3618 /* replicate last value */
3620 j= zigzag_direct[i];
3621 s->inter_matrix[j]= v;
3622 s->chroma_inter_matrix[j]= v;
3626 s->dct_unquantize= s->dct_unquantize_mpeg2;
3628 // FIXME a bunch of grayscale shape things
3630 s->dct_unquantize= s->dct_unquantize_h263;
3633 s->quarter_sample= get_bits1(&s->gb);
3634 else s->quarter_sample=0;
3636 if(!get_bits1(&s->gb)) printf("Complexity estimation not supported\n");
3638 s->resync_marker= !get_bits1(&s->gb); /* resync_marker_disabled */
3640 s->data_partitioning= get_bits1(&s->gb);
3641 if(s->data_partitioning){
3642 s->rvlc= get_bits1(&s->gb);
3644 printf("reversible vlc not supported\n");
3648 if(vo_ver_id != 1) {
3649 s->new_pred= get_bits1(&s->gb);
3651 printf("new pred not supported\n");
3652 skip_bits(&s->gb, 2); /* requested upstream message type */
3653 skip_bits1(&s->gb); /* newpred segment type */
3655 s->reduced_res_vop= get_bits1(&s->gb);
3656 if(s->reduced_res_vop) printf("reduced resolution VOP not supported\n");
3660 s->reduced_res_vop= 0;
3663 s->scalability= get_bits1(&s->gb);
3664 if (s->scalability) {
3665 printf("scalability not supported\n");
3668 //printf("end Data %X %d\n", show_bits(&s->gb, 32), get_bits_count(&s->gb)&0x7);
3670 } else if (startcode == 0x1b2) { //userdata
3676 //printf("user Data %X\n", show_bits(&s->gb, 32));
3677 buf[0]= show_bits(&s->gb, 8);
3678 for(i=1; i<256; i++){
3679 buf[i]= show_bits(&s->gb, 16)&0xFF;
3680 if(buf[i]==0) break;
3681 skip_bits(&s->gb, 8);
3684 e=sscanf(buf, "DivX%dBuild%d", &ver, &build);
3686 e=sscanf(buf, "DivX%db%d", &ver, &build);
3688 s->divx_version= ver;
3689 s->divx_build= build;
3690 if(s->picture_number==0){
3691 printf("This file was encoded with DivX%d Build%d\n", ver, build);
3692 if(ver==500 && build==413){
3693 printf("WARNING: this version of DivX is not MPEG4 compatible, trying to workaround these bugs...\n");
3696 printf("hmm, i havnt seen that version of divx yet, lets assume they fixed these bugs ...\n"
3697 "using mpeg4 decoder, if it fails contact the developers (of ffmpeg)\n");
3702 //printf("User Data: %s\n", buf);
3704 } else if (startcode != 0x1b6) { //VOP
3708 s->pict_type = get_bits(&s->gb, 2) + I_TYPE; /* pict type: I = 0 , P = 1 */
3709 //if(s->pict_type!=I_TYPE) return FRAME_SKIPED;
3710 if(s->pict_type==B_TYPE && s->low_delay && s->vol_control_parameters==0){
3711 printf("low_delay flag set, but shouldnt, clearing it\n");
3714 // printf("pic: %d, qpel:%d\n", s->pict_type, s->quarter_sample);
3715 //printf("%d", s->pict_type);
3717 while (get_bits1(&s->gb) != 0)
3720 check_marker(&s->gb, "before time_increment");
3721 time_increment= get_bits(&s->gb, s->time_increment_bits);
3722 //printf(" type:%d incr:%d increment:%d\n", s->pict_type, time_incr, time_increment);
3723 if(s->pict_type!=B_TYPE){
3724 s->last_time_base= s->time_base;
3725 s->time_base+= time_incr;
3726 s->time= s->time_base*s->time_increment_resolution + time_increment;
3727 s->pp_time= s->time - s->last_non_b_time;
3728 s->last_non_b_time= s->time;
3730 s->time= (s->last_time_base + time_incr)*s->time_increment_resolution + time_increment;
3731 s->bp_time= s->last_non_b_time - s->time;
3732 if(s->pp_time <=s->bp_time){
3733 // printf("messed up order, seeking?, skiping current b frame\n");
3734 return FRAME_SKIPED;
3738 if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){
3739 printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n");
3740 for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){
3741 if(get_bits1(&s->gb)) break;
3743 printf("my guess is %d bits ;)\n",s->time_increment_bits);
3746 if (get_bits1(&s->gb) != 1)
3748 //printf("time %d %d %d || %d %d %d\n", s->time_increment_bits, s->time_increment, s->time_base,
3749 //s->time, s->last_non_b_time[0], s->last_non_b_time[1]);
3750 if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE
3751 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {
3752 /* rounding type for motion estimation */
3753 s->no_rounding = get_bits1(&s->gb);
3757 //FIXME reduced res stuff
3759 if (s->shape != RECT_SHAPE) {
3760 if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
3761 int width, height, hor_spat_ref, ver_spat_ref;
3763 width = get_bits(&s->gb, 13);
3764 skip_bits1(&s->gb); /* marker */
3765 height = get_bits(&s->gb, 13);
3766 skip_bits1(&s->gb); /* marker */
3767 hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
3768 skip_bits1(&s->gb); /* marker */
3769 ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
3771 skip_bits1(&s->gb); /* change_CR_disable */
3773 if (get_bits1(&s->gb) != 0) {
3774 skip_bits(&s->gb, 8); /* constant_alpha_value */
3777 //FIXME complexity estimation stuff
3779 if (s->shape != BIN_ONLY_SHAPE) {
3781 t=get_bits(&s->gb, 3); /* intra dc VLC threshold */
3782 //printf("threshold %d\n", t);
3783 //FIXME interlaced specific bits
3786 if(s->pict_type == S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
3787 if(s->num_sprite_warping_points){
3788 mpeg4_decode_sprite_trajectory(s);
3790 if(s->sprite_brightness_change) printf("sprite_brightness_change not supported\n");
3791 if(s->vol_sprite_usage==STATIC_SPRITE) printf("static sprite not supported\n");
3794 if (s->shape != BIN_ONLY_SHAPE) {
3795 /* note: we do not use quant_precision to avoid problem if no
3796 MPEG4 vol header as it is found on some old opendivx
3798 s->qscale = get_bits(&s->gb, 5);
3800 printf("Error, header damaged or not MPEG4 header (qscale=0)\n");
3801 return -1; // makes no sense to continue, as there is nothing left from the image then
3804 if (s->pict_type != I_TYPE) {
3805 s->f_code = get_bits(&s->gb, 3); /* fcode_for */
3807 printf("Error, header damaged or not MPEG4 header (f_code=0)\n");
3808 return -1; // makes no sense to continue, as the MV decoding will break very quickly
3811 if (s->pict_type == B_TYPE) {
3812 s->b_code = get_bits(&s->gb, 3);
3813 //printf("b-code %d\n", s->b_code);
3815 //printf("quant:%d fcode:%d bcode:%d type:%d\n", s->qscale, s->f_code, s->b_code, s->pict_type);
3816 if(!s->scalability){
3817 if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) {
3818 skip_bits1(&s->gb); // vop shape coding type
3822 /* detect buggy encoders which dont set the low_delay flag (divx4/xvid/opendivx)*/
3823 // note we cannot detect divx5 without b-frames easyly (allthough its buggy too)
3824 if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==0 && s->picture_number==0){
3825 printf("looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3829 s->picture_number++; // better than pic number==0 allways ;)
3832 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
3833 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
3838 /* don't understand why they choose a different header ! */
3839 int intel_h263_decode_picture_header(MpegEncContext *s)
3843 /* picture header */
3844 if (get_bits(&s->gb, 22) != 0x20) {
3845 fprintf(stderr, "Bad picture start code\n");
3848 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
3850 if (get_bits1(&s->gb) != 1) {
3851 fprintf(stderr, "Bad marker\n");
3852 return -1; /* marker */
3854 if (get_bits1(&s->gb) != 0) {
3855 fprintf(stderr, "Bad H263 id\n");
3856 return -1; /* h263 id */
3858 skip_bits1(&s->gb); /* split screen off */
3859 skip_bits1(&s->gb); /* camera off */
3860 skip_bits1(&s->gb); /* freeze picture release off */
3862 format = get_bits(&s->gb, 3);
3864 fprintf(stderr, "Intel H263 free format not supported\n");
3869 s->pict_type = I_TYPE + get_bits1(&s->gb);
3871 s->unrestricted_mv = get_bits1(&s->gb);
3872 s->h263_long_vectors = s->unrestricted_mv;
3874 if (get_bits1(&s->gb) != 0) {
3875 fprintf(stderr, "SAC not supported\n");
3876 return -1; /* SAC: off */
3878 if (get_bits1(&s->gb) != 0) {
3879 fprintf(stderr, "Advanced Prediction Mode not supported\n");
3880 return -1; /* advanced prediction mode: off */
3882 if (get_bits1(&s->gb) != 0) {
3883 fprintf(stderr, "PB frame mode no supported\n");
3884 return -1; /* PB frame mode */
3887 /* skip unknown header garbage */
3888 skip_bits(&s->gb, 41);
3890 s->qscale = get_bits(&s->gb, 5);
3891 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
3894 while (get_bits1(&s->gb) != 0) {
3895 skip_bits(&s->gb, 8);