2 * ITU H.263 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/imgutils.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
38 #include "mpegvideo.h"
43 #include "mpegutils.h"
47 #include "mpeg4video.h"
48 #include "mpegvideodata.h"
50 // The defines below define the number of bits that are read at once for
51 // reading vlc values. Changing these may improve speed and data cache needs
52 // be aware though that decreasing them may need the number of stages that is
53 // passed to get_vlc* to be increased.
55 #define H263_MBTYPE_B_VLC_BITS 6
56 #define CBPC_B_VLC_BITS 3
58 static const int h263_mb_type_b_map[15]= {
59 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
60 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
61 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
62 MB_TYPE_L0 | MB_TYPE_16x16,
63 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
64 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
65 MB_TYPE_L1 | MB_TYPE_16x16,
66 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
67 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
68 MB_TYPE_L0L1 | MB_TYPE_16x16,
69 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
70 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
72 MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
73 MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
76 void ff_h263_show_pict_info(MpegEncContext *s){
77 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
78 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",
79 s->qscale, av_get_picture_type_char(s->pict_type),
80 s->gb.size_in_bits, 1-s->no_rounding,
82 s->umvplus ? " UMV" : "",
83 s->h263_long_vectors ? " LONG" : "",
84 s->h263_plus ? " +" : "",
85 s->h263_aic ? " AIC" : "",
86 s->alt_inter_vlc ? " AIV" : "",
87 s->modified_quant ? " MQ" : "",
88 s->loop_filter ? " LOOP" : "",
89 s->h263_slice_structured ? " SS" : "",
90 s->avctx->framerate.num, s->avctx->framerate.den
95 /***********************************************/
98 VLC ff_h263_intra_MCBPC_vlc;
99 VLC ff_h263_inter_MCBPC_vlc;
100 VLC ff_h263_cbpy_vlc;
102 static VLC h263_mbtype_b_vlc;
103 static VLC cbpc_b_vlc;
107 /* XXX: find a better solution to handle static init */
108 av_cold void ff_h263_decode_init_vlc(void)
110 static volatile int done = 0;
113 INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
114 ff_h263_intra_MCBPC_bits, 1, 1,
115 ff_h263_intra_MCBPC_code, 1, 1, 72);
116 INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
117 ff_h263_inter_MCBPC_bits, 1, 1,
118 ff_h263_inter_MCBPC_code, 1, 1, 198);
119 INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
120 &ff_h263_cbpy_tab[0][1], 2, 1,
121 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
122 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
123 &ff_mvtab[0][1], 2, 1,
124 &ff_mvtab[0][0], 2, 1, 538);
125 ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
126 ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
127 INIT_VLC_RL(ff_h263_rl_inter, 554);
128 INIT_VLC_RL(ff_rl_intra_aic, 554);
129 INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
130 &ff_h263_mbtype_b_tab[0][1], 2, 1,
131 &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
132 INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
133 &ff_cbpc_b_tab[0][1], 2, 1,
134 &ff_cbpc_b_tab[0][0], 2, 1, 8);
139 int ff_h263_decode_mba(MpegEncContext *s)
143 for (i = 0; i < 6; i++)
144 if (s->mb_num - 1 <= ff_mba_max[i])
146 mb_pos = get_bits(&s->gb, ff_mba_length[i]);
147 s->mb_x = mb_pos % s->mb_width;
148 s->mb_y = mb_pos / s->mb_width;
154 * Decode the group of blocks header or slice header.
155 * @return <0 if an error occurred
157 static int h263_decode_gob_header(MpegEncContext *s)
159 unsigned int val, gob_number;
162 /* Check for GOB Start Code */
163 val = show_bits(&s->gb, 16);
167 /* We have a GBSC probably with GSTUFF */
168 skip_bits(&s->gb, 16); /* Drop the zeros */
169 left= get_bits_left(&s->gb);
170 left = FFMIN(left, 32);
171 //MN: we must check the bits left or we might end in an infinite loop (or segfault)
172 for(;left>13; left--){
173 if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
178 if(s->h263_slice_structured){
179 if(check_marker(s->avctx, &s->gb, "before MBA")==0)
182 ff_h263_decode_mba(s);
185 if(check_marker(s->avctx, &s->gb, "after MBA")==0)
188 s->qscale = get_bits(&s->gb, 5); /* SQUANT */
189 if(check_marker(s->avctx, &s->gb, "after SQUANT")==0)
191 skip_bits(&s->gb, 2); /* GFID */
193 gob_number = get_bits(&s->gb, 5); /* GN */
195 s->mb_y= s->gob_index* gob_number;
196 skip_bits(&s->gb, 2); /* GFID */
197 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
200 if(s->mb_y >= s->mb_height)
210 * Decode the group of blocks / video packet header.
211 * @return bit position of the resync_marker, or <0 if none was found
213 int ff_h263_resync(MpegEncContext *s){
216 if(s->codec_id==AV_CODEC_ID_MPEG4){
218 align_get_bits(&s->gb);
221 if(show_bits(&s->gb, 16)==0){
222 pos= get_bits_count(&s->gb);
223 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
224 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
226 ret= h263_decode_gob_header(s);
230 //OK, it's not where it is supposed to be ...
231 s->gb= s->last_resync_gb;
232 align_get_bits(&s->gb);
233 left= get_bits_left(&s->gb);
235 for(;left>16+1+5+5; left-=8){
236 if(show_bits(&s->gb, 16)==0){
237 GetBitContext bak= s->gb;
239 pos= get_bits_count(&s->gb);
240 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
241 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
243 ret= h263_decode_gob_header(s);
249 skip_bits(&s->gb, 8);
255 int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
257 int code, val, sign, shift;
258 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
265 sign = get_bits1(&s->gb);
269 val = (val - 1) << shift;
270 val |= get_bits(&s->gb, shift);
277 /* modulo decoding */
278 if (!s->h263_long_vectors) {
279 val = sign_extend(val, 5 + f_code);
281 /* horrible H.263 long vector mode */
282 if (pred < -31 && val < -63)
284 if (pred > 32 && val > 63)
292 /* Decode RVLC of H.263+ UMV */
293 static int h263p_decode_umotion(MpegEncContext * s, int pred)
297 if (get_bits1(&s->gb)) /* Motion difference = 0 */
300 code = 2 + get_bits1(&s->gb);
302 while (get_bits1(&s->gb))
305 code += get_bits1(&s->gb);
307 avpriv_request_sample(s->avctx, "Huge DMV");
314 code = (sign) ? (pred - code) : (pred + code);
315 ff_tlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
321 * read the next MVs for OBMC. yes this is an ugly hack, feel free to send a patch :)
323 static void preview_obmc(MpegEncContext *s){
324 GetBitContext gb= s->gb;
326 int cbpc, i, pred_x, pred_y, mx, my;
328 const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
329 const int stride= s->b8_stride*2;
332 s->block_index[i]+= 2;
334 s->block_index[i]+= 1;
337 av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
340 if (get_bits1(&s->gb)) {
342 mot_val = s->current_picture.motion_val[0][s->block_index[0]];
343 mot_val[0 ]= mot_val[2 ]=
344 mot_val[0+stride]= mot_val[2+stride]= 0;
345 mot_val[1 ]= mot_val[3 ]=
346 mot_val[1+stride]= mot_val[3+stride]= 0;
348 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
351 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
355 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
357 get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
359 if(s->modified_quant){
360 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
361 else skip_bits(&s->gb, 5);
363 skip_bits(&s->gb, 2);
366 if ((cbpc & 16) == 0) {
367 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
368 /* 16x16 motion prediction */
369 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
371 mx = h263p_decode_umotion(s, pred_x);
373 mx = ff_h263_decode_motion(s, pred_x, 1);
376 my = h263p_decode_umotion(s, pred_y);
378 my = ff_h263_decode_motion(s, pred_y, 1);
380 mot_val[0 ]= mot_val[2 ]=
381 mot_val[0+stride]= mot_val[2+stride]= mx;
382 mot_val[1 ]= mot_val[3 ]=
383 mot_val[1+stride]= mot_val[3+stride]= my;
385 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
387 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
389 mx = h263p_decode_umotion(s, pred_x);
391 mx = ff_h263_decode_motion(s, pred_x, 1);
394 my = h263p_decode_umotion(s, pred_y);
396 my = ff_h263_decode_motion(s, pred_y, 1);
397 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
398 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
407 s->block_index[i]-= 2;
409 s->block_index[i]-= 1;
415 static void h263_decode_dquant(MpegEncContext *s){
416 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
418 if(s->modified_quant){
419 if(get_bits1(&s->gb))
420 s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
422 s->qscale= get_bits(&s->gb, 5);
424 s->qscale += quant_tab[get_bits(&s->gb, 2)];
425 ff_set_qscale(s, s->qscale);
428 static int h263_decode_block(MpegEncContext * s, int16_t * block,
431 int level, i, j, run;
432 RLTable *rl = &ff_h263_rl_inter;
433 const uint8_t *scan_table;
434 GetBitContext gb= s->gb;
436 scan_table = s->intra_scantable.permutated;
437 if (s->h263_aic && s->mb_intra) {
438 rl = &ff_rl_intra_aic;
442 scan_table = s->intra_v_scantable.permutated; /* left */
444 scan_table = s->intra_h_scantable.permutated; /* top */
446 } else if (s->mb_intra) {
448 if (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
449 if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
451 component = (n <= 3 ? 0 : n - 4 + 1);
452 level = s->last_dc[component];
453 if (s->rv10_first_dc_coded[component]) {
454 diff = ff_rv_decode_dc(s, n);
458 level = level & 0xff; /* handle wrap round */
459 s->last_dc[component] = level;
461 s->rv10_first_dc_coded[component] = 1;
464 level = get_bits(&s->gb, 8);
469 level = get_bits(&s->gb, 8);
470 if((level&0x7F) == 0){
471 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
472 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
484 if (s->mb_intra && s->h263_aic)
486 s->block_last_index[n] = i - 1;
491 OPEN_READER(re, &s->gb);
492 i--; // offset by -1 to allow direct indexing of scan_table
494 UPDATE_CACHE(re, &s->gb);
495 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
498 CLOSE_READER(re, &s->gb);
499 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
503 if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
504 int is11 = SHOW_UBITS(re, &s->gb, 1);
505 SKIP_CACHE(re, &s->gb, 1);
506 run = SHOW_UBITS(re, &s->gb, 7) + 1;
508 SKIP_COUNTER(re, &s->gb, 1 + 7);
509 UPDATE_CACHE(re, &s->gb);
510 level = SHOW_SBITS(re, &s->gb, 11);
511 SKIP_COUNTER(re, &s->gb, 11);
513 SKIP_CACHE(re, &s->gb, 7);
514 level = SHOW_SBITS(re, &s->gb, 7);
515 SKIP_COUNTER(re, &s->gb, 1 + 7 + 7);
518 run = SHOW_UBITS(re, &s->gb, 7) + 1;
519 SKIP_CACHE(re, &s->gb, 7);
520 level = (int8_t)SHOW_UBITS(re, &s->gb, 8);
521 SKIP_COUNTER(re, &s->gb, 7 + 8);
523 UPDATE_CACHE(re, &s->gb);
524 if (s->codec_id == AV_CODEC_ID_RV10) {
525 /* XXX: should patch encoder too */
526 level = SHOW_SBITS(re, &s->gb, 12);
527 SKIP_COUNTER(re, &s->gb, 12);
529 level = SHOW_UBITS(re, &s->gb, 5);
530 SKIP_CACHE(re, &s->gb, 5);
531 level |= SHOW_SBITS(re, &s->gb, 6) * (1<<5);
532 SKIP_COUNTER(re, &s->gb, 5 + 6);
537 if (SHOW_UBITS(re, &s->gb, 1))
539 SKIP_COUNTER(re, &s->gb, 1);
543 CLOSE_READER(re, &s->gb);
544 // redo update without last flag, revert -1 offset
545 i = i - run + ((run-1)&63) + 1;
547 // only last marker, no overrun
548 block[scan_table[i]] = level;
551 if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
552 //Looks like a hack but no, it's the way it is supposed to work ...
553 rl = &ff_rl_intra_aic;
556 s->bdsp.clear_block(block);
559 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
567 if (s->mb_intra && s->h263_aic) {
568 ff_h263_pred_acdc(s, block, n);
571 s->block_last_index[n] = i;
575 static int h263_skip_b_part(MpegEncContext *s, int cbp)
577 LOCAL_ALIGNED_32(int16_t, dblock, [64]);
581 /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
582 * but real value should be restored in order to be used later (in OBMC condition)
585 memcpy(bli, s->block_last_index, sizeof(bli));
587 for (i = 0; i < 6; i++) {
588 if (h263_decode_block(s, dblock, i, cbp&32) < 0)
593 memcpy(s->block_last_index, bli, sizeof(bli));
597 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
601 if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
603 if (pb_frame == 2 && c)
605 } else { // h.263 Annex M improved PB-frame
606 mv = get_unary(gb, 0, 4) + 1;
611 *cbpb = get_bits(gb, 6);
615 #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
616 #define tab_bias (tab_size / 2)
617 static inline void set_one_direct_mv(MpegEncContext *s, Picture *p, int i)
619 int xy = s->block_index[i];
620 uint16_t time_pp = s->pp_time;
621 uint16_t time_pb = s->pb_time;
624 p_mx = p->motion_val[0][xy][0];
625 if ((unsigned)(p_mx + tab_bias) < tab_size) {
626 s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias];
627 s->mv[1][i][0] = s->direct_scale_mv[1][p_mx + tab_bias];
629 s->mv[0][i][0] = p_mx * time_pb / time_pp;
630 s->mv[1][i][0] = p_mx * (time_pb - time_pp) / time_pp;
632 p_my = p->motion_val[0][xy][1];
633 if ((unsigned)(p_my + tab_bias) < tab_size) {
634 s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias];
635 s->mv[1][i][1] = s->direct_scale_mv[1][p_my + tab_bias];
637 s->mv[0][i][1] = p_my * time_pb / time_pp;
638 s->mv[1][i][1] = p_my * (time_pb - time_pp) / time_pp;
643 * @return the mb_type
645 static int set_direct_mv(MpegEncContext *s)
647 const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
648 Picture *p = &s->next_picture;
649 int colocated_mb_type = p->mb_type[mb_index];
652 if (s->codec_tag == AV_RL32("U263") && p->f->pict_type == AV_PICTURE_TYPE_I) {
653 p = &s->last_picture;
654 colocated_mb_type = p->mb_type[mb_index];
657 if (IS_8X8(colocated_mb_type)) {
658 s->mv_type = MV_TYPE_8X8;
659 for (i = 0; i < 4; i++)
660 set_one_direct_mv(s, p, i);
661 return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
663 set_one_direct_mv(s, p, 0);
666 s->mv[0][3][0] = s->mv[0][0][0];
669 s->mv[0][3][1] = s->mv[0][0][1];
672 s->mv[1][3][0] = s->mv[1][0][0];
675 s->mv[1][3][1] = s->mv[1][0][1];
676 s->mv_type = MV_TYPE_8X8;
677 // Note see prev line
678 return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1;
682 int ff_h263_decode_mb(MpegEncContext *s,
683 int16_t block[6][64])
685 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
687 const int xy= s->mb_x + s->mb_y * s->mb_stride;
688 int cbpb = 0, pb_mv_count = 0;
690 av_assert2(!s->h263_pred);
692 if (s->pict_type == AV_PICTURE_TYPE_P) {
694 if (get_bits1(&s->gb)) {
698 s->block_last_index[i] = -1;
699 s->mv_dir = MV_DIR_FORWARD;
700 s->mv_type = MV_TYPE_16X16;
701 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
704 s->mb_skipped = !(s->obmc | s->loop_filter);
707 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
709 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
714 s->bdsp.clear_blocks(s->block[0]);
717 s->mb_intra = ((cbpc & 4) != 0);
718 if (s->mb_intra) goto intra;
720 if(s->pb_frame && get_bits1(&s->gb))
721 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
722 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
725 av_log(s->avctx, AV_LOG_ERROR, "cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
729 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
732 cbp = (cbpc & 3) | (cbpy << 2);
734 h263_decode_dquant(s);
737 s->mv_dir = MV_DIR_FORWARD;
738 if ((cbpc & 16) == 0) {
739 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
740 /* 16x16 motion prediction */
741 s->mv_type = MV_TYPE_16X16;
742 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
744 mx = h263p_decode_umotion(s, pred_x);
746 mx = ff_h263_decode_motion(s, pred_x, 1);
752 my = h263p_decode_umotion(s, pred_y);
754 my = ff_h263_decode_motion(s, pred_y, 1);
761 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
762 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
764 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
765 s->mv_type = MV_TYPE_8X8;
767 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
769 mx = h263p_decode_umotion(s, pred_x);
771 mx = ff_h263_decode_motion(s, pred_x, 1);
776 my = h263p_decode_umotion(s, pred_y);
778 my = ff_h263_decode_motion(s, pred_y, 1);
783 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
784 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
789 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
791 const int stride= s->b8_stride;
792 int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
793 int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
794 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
797 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
798 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
799 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
800 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
803 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
805 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
809 mb_type= h263_mb_type_b_map[ mb_type ];
812 s->mb_intra = IS_INTRA(mb_type);
813 if(HAS_CBP(mb_type)){
814 s->bdsp.clear_blocks(s->block[0]);
815 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
817 dquant = IS_QUANT(mb_type);
821 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
824 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
828 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
831 cbp = (cbpc & 3) | (cbpy << 2);
835 av_assert2(!s->mb_intra);
837 if(IS_QUANT(mb_type)){
838 h263_decode_dquant(s);
841 if(IS_DIRECT(mb_type)){
842 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
843 mb_type |= set_direct_mv(s);
846 s->mv_type= MV_TYPE_16X16;
849 if(USES_LIST(mb_type, 0)){
850 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
851 s->mv_dir = MV_DIR_FORWARD;
854 mx = h263p_decode_umotion(s, pred_x);
856 mx = ff_h263_decode_motion(s, pred_x, 1);
861 my = h263p_decode_umotion(s, pred_y);
863 my = ff_h263_decode_motion(s, pred_y, 1);
867 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
868 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
872 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
873 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
876 if(USES_LIST(mb_type, 1)){
877 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &pred_x, &pred_y);
878 s->mv_dir |= MV_DIR_BACKWARD;
881 mx = h263p_decode_umotion(s, pred_x);
883 mx = ff_h263_decode_motion(s, pred_x, 1);
888 my = h263p_decode_umotion(s, pred_y);
890 my = ff_h263_decode_motion(s, pred_y, 1);
894 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
895 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
899 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
900 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
904 s->current_picture.mb_type[xy] = mb_type;
905 } else { /* I-Frame */
907 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
909 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
914 s->bdsp.clear_blocks(s->block[0]);
919 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
921 s->ac_pred = get_bits1(&s->gb);
923 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
925 s->h263_aic_dir = get_bits1(&s->gb);
930 if(s->pb_frame && get_bits1(&s->gb))
931 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
932 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
934 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
937 cbp = (cbpc & 3) | (cbpy << 2);
939 h263_decode_dquant(s);
942 pb_mv_count += !!s->pb_frame;
945 while(pb_mv_count--){
946 ff_h263_decode_motion(s, 0, 1);
947 ff_h263_decode_motion(s, 0, 1);
950 /* decode each block */
951 for (i = 0; i < 6; i++) {
952 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
957 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
959 if(s->obmc && !s->mb_intra){
960 if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
965 if (get_bits_left(&s->gb) < 0)
966 return AVERROR_INVALIDDATA;
968 /* per-MB end of slice check */
970 int v= show_bits(&s->gb, 16);
972 if (get_bits_left(&s->gb) < 16) {
973 v >>= 16 - get_bits_left(&s->gb);
983 /* Most is hardcoded; should extend to handle all H.263 streams. */
984 int ff_h263_decode_picture_header(MpegEncContext *s)
986 int format, width, height, i, ret;
989 align_get_bits(&s->gb);
991 if (show_bits(&s->gb, 2) == 2 && s->avctx->frame_number == 0) {
992 av_log(s->avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
995 startcode= get_bits(&s->gb, 22-8);
997 for(i= get_bits_left(&s->gb); i>24; i-=8) {
998 startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
1000 if(startcode == 0x20)
1004 if (startcode != 0x20) {
1005 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
1008 /* temporal reference */
1009 i = get_bits(&s->gb, 8); /* picture timestamp */
1011 i -= (i - (s->picture_number & 0xFF) + 128) & ~0xFF;
1013 s->picture_number= (s->picture_number&~0xFF) + i;
1015 /* PTYPE starts here */
1016 if (check_marker(s->avctx, &s->gb, "in PTYPE") != 1) {
1019 if (get_bits1(&s->gb) != 0) {
1020 av_log(s->avctx, AV_LOG_ERROR, "Bad H.263 id\n");
1021 return -1; /* H.263 id */
1023 skip_bits1(&s->gb); /* split screen off */
1024 skip_bits1(&s->gb); /* camera off */
1025 skip_bits1(&s->gb); /* freeze picture release off */
1027 format = get_bits(&s->gb, 3);
1032 7 extended PTYPE (PLUSPTYPE)
1035 if (format != 7 && format != 6) {
1038 width = ff_h263_format[format][0];
1039 height = ff_h263_format[format][1];
1043 s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
1045 s->h263_long_vectors = get_bits1(&s->gb);
1047 if (get_bits1(&s->gb) != 0) {
1048 av_log(s->avctx, AV_LOG_ERROR, "H.263 SAC not supported\n");
1049 return -1; /* SAC: off */
1051 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
1052 s->unrestricted_mv = s->h263_long_vectors || s->obmc;
1054 s->pb_frame = get_bits1(&s->gb);
1055 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
1056 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
1060 s->avctx->sample_aspect_ratio= (AVRational){12,11};
1061 s->avctx->framerate = (AVRational){ 30000, 1001 };
1067 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
1069 /* ufep other than 0 and 1 are reserved */
1072 format = get_bits(&s->gb, 3);
1073 ff_dlog(s->avctx, "ufep=1, format: %d\n", format);
1074 s->custom_pcf= get_bits1(&s->gb);
1075 s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
1076 if (get_bits1(&s->gb) != 0) {
1077 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
1079 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
1080 s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
1081 s->loop_filter= get_bits1(&s->gb);
1082 s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
1083 if(s->avctx->lowres)
1086 s->h263_slice_structured= get_bits1(&s->gb);
1087 if (get_bits1(&s->gb) != 0) {
1088 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
1090 if (get_bits1(&s->gb) != 0) {
1091 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
1093 s->alt_inter_vlc= get_bits1(&s->gb);
1094 s->modified_quant= get_bits1(&s->gb);
1095 if(s->modified_quant)
1096 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
1098 skip_bits(&s->gb, 1); /* Prevent start code emulation */
1100 skip_bits(&s->gb, 3); /* Reserved */
1101 } else if (ufep != 0) {
1102 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
1107 s->pict_type = get_bits(&s->gb, 3);
1108 switch(s->pict_type){
1109 case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
1110 case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
1111 case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
1112 case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
1113 case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
1117 skip_bits(&s->gb, 2);
1118 s->no_rounding = get_bits1(&s->gb);
1119 skip_bits(&s->gb, 4);
1121 /* Get the picture dimensions */
1124 /* Custom Picture Format (CPFMT) */
1125 s->aspect_ratio_info = get_bits(&s->gb, 4);
1126 ff_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
1131 3 - 10:11 (525-type 4:3)
1132 4 - 16:11 (CIF 16:9)
1133 5 - 40:33 (525-type 16:9)
1136 width = (get_bits(&s->gb, 9) + 1) * 4;
1137 check_marker(s->avctx, &s->gb, "in dimensions");
1138 height = get_bits(&s->gb, 9) * 4;
1139 ff_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1140 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1141 /* expected dimensions */
1142 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
1143 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
1145 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1148 width = ff_h263_format[format][0];
1149 height = ff_h263_format[format][1];
1150 s->avctx->sample_aspect_ratio= (AVRational){12,11};
1152 s->avctx->sample_aspect_ratio.den <<= s->ehc_mode;
1153 if ((width == 0) || (height == 0))
1160 s->avctx->framerate.num = 1800000;
1161 s->avctx->framerate.den = 1000 + get_bits1(&s->gb);
1162 s->avctx->framerate.den *= get_bits(&s->gb, 7);
1163 if(s->avctx->framerate.den == 0){
1164 av_log(s, AV_LOG_ERROR, "zero framerate\n");
1167 gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
1168 s->avctx->framerate.den /= gcd;
1169 s->avctx->framerate.num /= gcd;
1171 s->avctx->framerate = (AVRational){ 30000, 1001 };
1176 skip_bits(&s->gb, 2); //extended Temporal reference
1181 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1184 if(s->h263_slice_structured){
1185 if (get_bits1(&s->gb) != 0) {
1186 av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1188 if (get_bits1(&s->gb) != 0) {
1189 av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1192 if (s->pict_type == AV_PICTURE_TYPE_B) {
1193 skip_bits(&s->gb, 4); //ELNUM
1195 skip_bits(&s->gb, 4); // RLNUM
1200 s->qscale = get_bits(&s->gb, 5);
1203 if ((ret = av_image_check_size(s->width, s->height, 0, s)) < 0)
1206 s->mb_width = (s->width + 15) / 16;
1207 s->mb_height = (s->height + 15) / 16;
1208 s->mb_num = s->mb_width * s->mb_height;
1211 skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
1213 skip_bits(&s->gb, 2); //extended Temporal reference
1214 skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
1217 if (s->pict_type!=AV_PICTURE_TYPE_B) {
1218 s->time = s->picture_number;
1219 s->pp_time = s->time - s->last_non_b_time;
1220 s->last_non_b_time = s->time;
1222 s->time = s->picture_number;
1223 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
1224 if (s->pp_time <=s->pb_time ||
1225 s->pp_time <= s->pp_time - s->pb_time ||
1230 ff_mpeg4_init_direct_mv(s);
1234 if (skip_1stop_8data_bits(&s->gb) < 0)
1235 return AVERROR_INVALIDDATA;
1237 if(s->h263_slice_structured){
1238 if (check_marker(s->avctx, &s->gb, "SEPB1") != 1) {
1242 ff_h263_decode_mba(s);
1244 if (check_marker(s->avctx, &s->gb, "SEPB2") != 1) {
1250 if (s->pict_type == AV_PICTURE_TYPE_B)
1254 s->y_dc_scale_table=
1255 s->c_dc_scale_table= ff_aic_dc_scale_table;
1257 s->y_dc_scale_table=
1258 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1261 ff_h263_show_pict_info(s);
1262 if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
1264 for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1265 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1266 for(i=0; i<13; i++){
1268 int v= get_bits(&s->gb, 8);
1269 v |= get_sbits(&s->gb, 8)<<8;
1270 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
1272 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1274 for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));