2 * ITU H263 bitstream decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * Copyright (c) 2001 Juan J. Sierralta P
6 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #define UNCHECKED_BITSTREAM_READER 1
33 #include "libavutil/attributes.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mathematics.h"
37 #include "mpegvideo.h"
42 #include "mpeg4video.h"
44 // The defines below define the number of bits that are read at once for
45 // reading vlc values. Changing these may improve speed and data cache needs
46 // be aware though that decreasing them may need the number of stages that is
47 // passed to get_vlc* to be increased.
49 #define H263_MBTYPE_B_VLC_BITS 6
50 #define CBPC_B_VLC_BITS 3
52 static const int h263_mb_type_b_map[15]= {
53 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
54 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
55 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
56 MB_TYPE_L0 | MB_TYPE_16x16,
57 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
58 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
59 MB_TYPE_L1 | MB_TYPE_16x16,
60 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
61 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
62 MB_TYPE_L0L1 | MB_TYPE_16x16,
63 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
64 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
66 MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
67 MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
70 void ff_h263_show_pict_info(MpegEncContext *s){
71 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
72 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
73 s->qscale, av_get_picture_type_char(s->pict_type),
74 s->gb.size_in_bits, 1-s->no_rounding,
76 s->umvplus ? " UMV" : "",
77 s->h263_long_vectors ? " LONG" : "",
78 s->h263_plus ? " +" : "",
79 s->h263_aic ? " AIC" : "",
80 s->alt_inter_vlc ? " AIV" : "",
81 s->modified_quant ? " MQ" : "",
82 s->loop_filter ? " LOOP" : "",
83 s->h263_slice_structured ? " SS" : "",
84 s->avctx->time_base.den, s->avctx->time_base.num
89 /***********************************************/
92 VLC ff_h263_intra_MCBPC_vlc;
93 VLC ff_h263_inter_MCBPC_vlc;
96 static VLC h263_mbtype_b_vlc;
97 static VLC cbpc_b_vlc;
101 /* XXX: find a better solution to handle static init */
102 av_cold void ff_h263_decode_init_vlc(void)
104 static volatile int done = 0;
107 INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
108 ff_h263_intra_MCBPC_bits, 1, 1,
109 ff_h263_intra_MCBPC_code, 1, 1, 72);
110 INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
111 ff_h263_inter_MCBPC_bits, 1, 1,
112 ff_h263_inter_MCBPC_code, 1, 1, 198);
113 INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
114 &ff_h263_cbpy_tab[0][1], 2, 1,
115 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
116 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
117 &ff_mvtab[0][1], 2, 1,
118 &ff_mvtab[0][0], 2, 1, 538);
119 ff_init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
120 ff_init_rl(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
121 INIT_VLC_RL(ff_h263_rl_inter, 554);
122 INIT_VLC_RL(ff_rl_intra_aic, 554);
123 INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
124 &ff_h263_mbtype_b_tab[0][1], 2, 1,
125 &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
126 INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
127 &ff_cbpc_b_tab[0][1], 2, 1,
128 &ff_cbpc_b_tab[0][0], 2, 1, 8);
133 int ff_h263_decode_mba(MpegEncContext *s)
138 if(s->mb_num-1 <= ff_mba_max[i]) break;
140 mb_pos= get_bits(&s->gb, ff_mba_length[i]);
141 s->mb_x= mb_pos % s->mb_width;
142 s->mb_y= mb_pos / s->mb_width;
148 * Decode the group of blocks header or slice header.
149 * @return <0 if an error occurred
151 static int h263_decode_gob_header(MpegEncContext *s)
153 unsigned int val, gob_number;
156 /* Check for GOB Start Code */
157 val = show_bits(&s->gb, 16);
161 /* We have a GBSC probably with GSTUFF */
162 skip_bits(&s->gb, 16); /* Drop the zeros */
163 left= get_bits_left(&s->gb);
164 //MN: we must check the bits left or we might end in a infinite loop (or segfault)
165 for(;left>13; left--){
166 if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
171 if(s->h263_slice_structured){
172 if(get_bits1(&s->gb)==0)
175 ff_h263_decode_mba(s);
178 if(get_bits1(&s->gb)==0)
181 s->qscale = get_bits(&s->gb, 5); /* SQUANT */
182 if(get_bits1(&s->gb)==0)
184 skip_bits(&s->gb, 2); /* GFID */
186 gob_number = get_bits(&s->gb, 5); /* GN */
188 s->mb_y= s->gob_index* gob_number;
189 skip_bits(&s->gb, 2); /* GFID */
190 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
193 if(s->mb_y >= s->mb_height)
203 * Decode the group of blocks / video packet header.
204 * @return bit position of the resync_marker, or <0 if none was found
206 int ff_h263_resync(MpegEncContext *s){
209 if(s->codec_id==AV_CODEC_ID_MPEG4){
211 align_get_bits(&s->gb);
214 if(show_bits(&s->gb, 16)==0){
215 pos= get_bits_count(&s->gb);
216 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
217 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
219 ret= h263_decode_gob_header(s);
223 //OK, it's not where it is supposed to be ...
224 s->gb= s->last_resync_gb;
225 align_get_bits(&s->gb);
226 left= get_bits_left(&s->gb);
228 for(;left>16+1+5+5; left-=8){
229 if(show_bits(&s->gb, 16)==0){
230 GetBitContext bak= s->gb;
232 pos= get_bits_count(&s->gb);
233 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
234 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
236 ret= h263_decode_gob_header(s);
242 skip_bits(&s->gb, 8);
248 int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
250 int code, val, sign, shift;
251 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
258 sign = get_bits1(&s->gb);
262 val = (val - 1) << shift;
263 val |= get_bits(&s->gb, shift);
270 /* modulo decoding */
271 if (!s->h263_long_vectors) {
272 val = sign_extend(val, 5 + f_code);
274 /* horrible h263 long vector mode */
275 if (pred < -31 && val < -63)
277 if (pred > 32 && val > 63)
285 /* Decode RVLC of H.263+ UMV */
286 static int h263p_decode_umotion(MpegEncContext * s, int pred)
290 if (get_bits1(&s->gb)) /* Motion difference = 0 */
293 code = 2 + get_bits1(&s->gb);
295 while (get_bits1(&s->gb))
298 code += get_bits1(&s->gb);
303 code = (sign) ? (pred - code) : (pred + code);
304 av_dlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
310 * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
312 static void preview_obmc(MpegEncContext *s){
313 GetBitContext gb= s->gb;
315 int cbpc, i, pred_x, pred_y, mx, my;
317 const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
318 const int stride= s->b8_stride*2;
321 s->block_index[i]+= 2;
323 s->block_index[i]+= 1;
326 av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
329 if (get_bits1(&s->gb)) {
331 mot_val = s->current_picture.motion_val[0][s->block_index[0]];
332 mot_val[0 ]= mot_val[2 ]=
333 mot_val[0+stride]= mot_val[2+stride]= 0;
334 mot_val[1 ]= mot_val[3 ]=
335 mot_val[1+stride]= mot_val[3+stride]= 0;
337 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
340 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
344 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
346 get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
348 if(s->modified_quant){
349 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
350 else skip_bits(&s->gb, 5);
352 skip_bits(&s->gb, 2);
355 if ((cbpc & 16) == 0) {
356 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
357 /* 16x16 motion prediction */
358 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
360 mx = h263p_decode_umotion(s, pred_x);
362 mx = ff_h263_decode_motion(s, pred_x, 1);
365 my = h263p_decode_umotion(s, pred_y);
367 my = ff_h263_decode_motion(s, pred_y, 1);
369 mot_val[0 ]= mot_val[2 ]=
370 mot_val[0+stride]= mot_val[2+stride]= mx;
371 mot_val[1 ]= mot_val[3 ]=
372 mot_val[1+stride]= mot_val[3+stride]= my;
374 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
376 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
378 mx = h263p_decode_umotion(s, pred_x);
380 mx = ff_h263_decode_motion(s, pred_x, 1);
383 my = h263p_decode_umotion(s, pred_y);
385 my = ff_h263_decode_motion(s, pred_y, 1);
386 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
387 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
396 s->block_index[i]-= 2;
398 s->block_index[i]-= 1;
404 static void h263_decode_dquant(MpegEncContext *s){
405 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
407 if(s->modified_quant){
408 if(get_bits1(&s->gb))
409 s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
411 s->qscale= get_bits(&s->gb, 5);
413 s->qscale += quant_tab[get_bits(&s->gb, 2)];
414 ff_set_qscale(s, s->qscale);
417 static int h263_decode_block(MpegEncContext * s, int16_t * block,
420 int code, level, i, j, last, run;
421 RLTable *rl = &ff_h263_rl_inter;
422 const uint8_t *scan_table;
423 GetBitContext gb= s->gb;
425 scan_table = s->intra_scantable.permutated;
426 if (s->h263_aic && s->mb_intra) {
427 rl = &ff_rl_intra_aic;
431 scan_table = s->intra_v_scantable.permutated; /* left */
433 scan_table = s->intra_h_scantable.permutated; /* top */
435 } else if (s->mb_intra) {
437 if(s->codec_id == AV_CODEC_ID_RV10){
438 #if CONFIG_RV10_DECODER
439 if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
441 component = (n <= 3 ? 0 : n - 4 + 1);
442 level = s->last_dc[component];
443 if (s->rv10_first_dc_coded[component]) {
444 diff = ff_rv_decode_dc(s, n);
448 level = level & 0xff; /* handle wrap round */
449 s->last_dc[component] = level;
451 s->rv10_first_dc_coded[component] = 1;
454 level = get_bits(&s->gb, 8);
460 level = get_bits(&s->gb, 8);
461 if((level&0x7F) == 0){
462 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
463 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
475 if (s->mb_intra && s->h263_aic)
477 s->block_last_index[n] = i - 1;
482 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
484 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
489 if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
490 ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
492 last = get_bits1(&s->gb);
493 run = get_bits(&s->gb, 6);
494 level = (int8_t)get_bits(&s->gb, 8);
496 if (s->codec_id == AV_CODEC_ID_RV10) {
497 /* XXX: should patch encoder too */
498 level = get_sbits(&s->gb, 12);
500 level = get_bits(&s->gb, 5);
501 level |= get_sbits(&s->gb, 6)<<5;
506 run = rl->table_run[code];
507 level = rl->table_level[code];
508 last = code >= rl->last;
509 if (get_bits1(&s->gb))
514 if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
515 //Looks like a hack but no, it's the way it is supposed to work ...
516 rl = &ff_rl_intra_aic;
519 s->dsp.clear_block(block);
522 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
532 if (s->mb_intra && s->h263_aic) {
533 ff_h263_pred_acdc(s, block, n);
536 s->block_last_index[n] = i;
540 static int h263_skip_b_part(MpegEncContext *s, int cbp)
542 LOCAL_ALIGNED_16(int16_t, dblock, [64]);
546 /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
547 * but real value should be restored in order to be used later (in OBMC condition)
550 memcpy(bli, s->block_last_index, sizeof(bli));
552 for (i = 0; i < 6; i++) {
553 if (h263_decode_block(s, dblock, i, cbp&32) < 0)
558 memcpy(s->block_last_index, bli, sizeof(bli));
562 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
566 if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
568 if (pb_frame == 2 && c)
570 } else { // h.263 Annex M improved PB-frame
571 mv = get_unary(gb, 0, 4) + 1;
576 *cbpb = get_bits(gb, 6);
580 int ff_h263_decode_mb(MpegEncContext *s,
581 int16_t block[6][64])
583 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
585 const int xy= s->mb_x + s->mb_y * s->mb_stride;
586 int cbpb = 0, pb_mv_count = 0;
588 av_assert2(!s->h263_pred);
590 if (s->pict_type == AV_PICTURE_TYPE_P) {
592 if (get_bits1(&s->gb)) {
596 s->block_last_index[i] = -1;
597 s->mv_dir = MV_DIR_FORWARD;
598 s->mv_type = MV_TYPE_16X16;
599 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
602 s->mb_skipped = !(s->obmc | s->loop_filter);
605 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
607 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
612 s->dsp.clear_blocks(s->block[0]);
615 s->mb_intra = ((cbpc & 4) != 0);
616 if (s->mb_intra) goto intra;
618 if(s->pb_frame && get_bits1(&s->gb))
619 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
620 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
622 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
625 cbp = (cbpc & 3) | (cbpy << 2);
627 h263_decode_dquant(s);
630 s->mv_dir = MV_DIR_FORWARD;
631 if ((cbpc & 16) == 0) {
632 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
633 /* 16x16 motion prediction */
634 s->mv_type = MV_TYPE_16X16;
635 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
637 mx = h263p_decode_umotion(s, pred_x);
639 mx = ff_h263_decode_motion(s, pred_x, 1);
645 my = h263p_decode_umotion(s, pred_y);
647 my = ff_h263_decode_motion(s, pred_y, 1);
654 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
655 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
657 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
658 s->mv_type = MV_TYPE_8X8;
660 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
662 mx = h263p_decode_umotion(s, pred_x);
664 mx = ff_h263_decode_motion(s, pred_x, 1);
669 my = h263p_decode_umotion(s, pred_y);
671 my = ff_h263_decode_motion(s, pred_y, 1);
676 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
677 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
682 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
684 const int stride= s->b8_stride;
685 int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
686 int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
687 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
690 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
691 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
692 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
693 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
696 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
698 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
702 mb_type= h263_mb_type_b_map[ mb_type ];
705 s->mb_intra = IS_INTRA(mb_type);
706 if(HAS_CBP(mb_type)){
707 s->dsp.clear_blocks(s->block[0]);
708 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
710 dquant = IS_QUANT(mb_type);
714 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
717 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
721 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
724 cbp = (cbpc & 3) | (cbpy << 2);
728 av_assert2(!s->mb_intra);
730 if(IS_QUANT(mb_type)){
731 h263_decode_dquant(s);
734 if(IS_DIRECT(mb_type)){
735 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
736 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
739 s->mv_type= MV_TYPE_16X16;
742 if(USES_LIST(mb_type, 0)){
743 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
744 s->mv_dir = MV_DIR_FORWARD;
746 mx = ff_h263_decode_motion(s, mx, 1);
747 my = ff_h263_decode_motion(s, my, 1);
751 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
752 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
755 if(USES_LIST(mb_type, 1)){
756 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
757 s->mv_dir |= MV_DIR_BACKWARD;
759 mx = ff_h263_decode_motion(s, mx, 1);
760 my = ff_h263_decode_motion(s, my, 1);
764 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
765 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
769 s->current_picture.mb_type[xy] = mb_type;
770 } else { /* I-Frame */
772 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
774 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
779 s->dsp.clear_blocks(s->block[0]);
784 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
786 s->ac_pred = get_bits1(&s->gb);
788 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
790 s->h263_aic_dir = get_bits1(&s->gb);
795 if(s->pb_frame && get_bits1(&s->gb))
796 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
797 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
799 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
802 cbp = (cbpc & 3) | (cbpy << 2);
804 h263_decode_dquant(s);
807 pb_mv_count += !!s->pb_frame;
810 while(pb_mv_count--){
811 ff_h263_decode_motion(s, 0, 1);
812 ff_h263_decode_motion(s, 0, 1);
815 /* decode each block */
816 for (i = 0; i < 6; i++) {
817 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
822 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
824 if(s->obmc && !s->mb_intra){
825 if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
830 /* per-MB end of slice check */
832 int v= show_bits(&s->gb, 16);
834 if (get_bits_left(&s->gb) < 16) {
835 v >>= 16 - get_bits_left(&s->gb);
845 /* most is hardcoded. should extend to handle all h263 streams */
846 int ff_h263_decode_picture_header(MpegEncContext *s)
848 int format, width, height, i;
851 align_get_bits(&s->gb);
853 if (show_bits(&s->gb, 2) == 2 && s->avctx->frame_number == 0) {
854 av_log(s->avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
857 startcode= get_bits(&s->gb, 22-8);
859 for(i= get_bits_left(&s->gb); i>24; i-=8) {
860 startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
862 if(startcode == 0x20)
866 if (startcode != 0x20) {
867 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
870 /* temporal reference */
871 i = get_bits(&s->gb, 8); /* picture timestamp */
872 if( (s->picture_number&~0xFF)+i < s->picture_number)
874 s->picture_number= (s->picture_number&~0xFF) + i;
876 /* PTYPE starts here */
877 if (get_bits1(&s->gb) != 1) {
879 av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
882 if (get_bits1(&s->gb) != 0) {
883 av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
884 return -1; /* h263 id */
886 skip_bits1(&s->gb); /* split screen off */
887 skip_bits1(&s->gb); /* camera off */
888 skip_bits1(&s->gb); /* freeze picture release off */
890 format = get_bits(&s->gb, 3);
895 7 extended PTYPE (PLUSPTYPE)
898 if (format != 7 && format != 6) {
901 width = ff_h263_format[format][0];
902 height = ff_h263_format[format][1];
906 s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
908 s->h263_long_vectors = get_bits1(&s->gb);
910 if (get_bits1(&s->gb) != 0) {
911 av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
912 return -1; /* SAC: off */
914 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
915 s->unrestricted_mv = s->h263_long_vectors || s->obmc;
917 s->pb_frame = get_bits1(&s->gb);
918 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
919 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
923 s->avctx->sample_aspect_ratio= (AVRational){12,11};
924 s->avctx->time_base= (AVRational){1001, 30000};
930 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
932 /* ufep other than 0 and 1 are reserved */
935 format = get_bits(&s->gb, 3);
936 av_dlog(s->avctx, "ufep=1, format: %d\n", format);
937 s->custom_pcf= get_bits1(&s->gb);
938 s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
939 if (get_bits1(&s->gb) != 0) {
940 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
942 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
943 s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
944 s->loop_filter= get_bits1(&s->gb);
945 s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
949 s->h263_slice_structured= get_bits1(&s->gb);
950 if (get_bits1(&s->gb) != 0) {
951 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
953 if (get_bits1(&s->gb) != 0) {
954 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
956 s->alt_inter_vlc= get_bits1(&s->gb);
957 s->modified_quant= get_bits1(&s->gb);
958 if(s->modified_quant)
959 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
961 skip_bits(&s->gb, 1); /* Prevent start code emulation */
963 skip_bits(&s->gb, 3); /* Reserved */
964 } else if (ufep != 0) {
965 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
970 s->pict_type = get_bits(&s->gb, 3);
971 switch(s->pict_type){
972 case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
973 case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
974 case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
975 case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
976 case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
980 skip_bits(&s->gb, 2);
981 s->no_rounding = get_bits1(&s->gb);
982 skip_bits(&s->gb, 4);
984 /* Get the picture dimensions */
987 /* Custom Picture Format (CPFMT) */
988 s->aspect_ratio_info = get_bits(&s->gb, 4);
989 av_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
994 3 - 10:11 (525-type 4:3)
996 5 - 40:33 (525-type 16:9)
999 width = (get_bits(&s->gb, 9) + 1) * 4;
1001 height = get_bits(&s->gb, 9) * 4;
1002 av_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1003 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1004 /* aspected dimensions */
1005 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
1006 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
1008 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1011 width = ff_h263_format[format][0];
1012 height = ff_h263_format[format][1];
1013 s->avctx->sample_aspect_ratio= (AVRational){12,11};
1015 s->avctx->sample_aspect_ratio.den <<= s->ehc_mode;
1016 if ((width == 0) || (height == 0))
1023 s->avctx->time_base.den= 1800000;
1024 s->avctx->time_base.num= 1000 + get_bits1(&s->gb);
1025 s->avctx->time_base.num*= get_bits(&s->gb, 7);
1026 if(s->avctx->time_base.num == 0){
1027 av_log(s, AV_LOG_ERROR, "zero framerate\n");
1030 gcd= av_gcd(s->avctx->time_base.den, s->avctx->time_base.num);
1031 s->avctx->time_base.den /= gcd;
1032 s->avctx->time_base.num /= gcd;
1034 s->avctx->time_base= (AVRational){1001, 30000};
1039 skip_bits(&s->gb, 2); //extended Temporal reference
1044 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1047 if(s->h263_slice_structured){
1048 if (get_bits1(&s->gb) != 0) {
1049 av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1051 if (get_bits1(&s->gb) != 0) {
1052 av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1057 s->qscale = get_bits(&s->gb, 5);
1060 if (s->width == 0 || s->height == 0) {
1061 av_log(s->avctx, AV_LOG_ERROR, "dimensions 0\n");
1064 s->mb_width = (s->width + 15) / 16;
1065 s->mb_height = (s->height + 15) / 16;
1066 s->mb_num = s->mb_width * s->mb_height;
1069 skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
1071 skip_bits(&s->gb, 2); //extended Temporal reference
1072 skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
1075 if (s->pict_type!=AV_PICTURE_TYPE_B) {
1076 s->time = s->picture_number;
1077 s->pp_time = s->time - s->last_non_b_time;
1078 s->last_non_b_time = s->time;
1080 s->time = s->picture_number;
1081 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
1082 if (s->pp_time <=s->pb_time ||
1083 s->pp_time <= s->pp_time - s->pb_time ||
1088 ff_mpeg4_init_direct_mv(s);
1092 if (skip_1stop_8data_bits(&s->gb) < 0)
1093 return AVERROR_INVALIDDATA;
1095 if(s->h263_slice_structured){
1096 if (get_bits1(&s->gb) != 1) {
1097 av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
1101 ff_h263_decode_mba(s);
1103 if (get_bits1(&s->gb) != 1) {
1104 av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
1111 s->y_dc_scale_table=
1112 s->c_dc_scale_table= ff_aic_dc_scale_table;
1114 s->y_dc_scale_table=
1115 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1118 ff_h263_show_pict_info(s);
1119 if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
1121 for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1122 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1123 for(i=0; i<13; i++){
1125 int v= get_bits(&s->gb, 8);
1126 v |= get_sbits(&s->gb, 8)<<8;
1127 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
1129 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1131 for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));