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"
37 #include "libavutil/mem_internal.h"
39 #include "mpegvideo.h"
44 #include "mpegutils.h"
48 #include "mpeg4video.h"
49 #include "mpegvideodata.h"
51 // The defines below define the number of bits that are read at once for
52 // reading vlc values. Changing these may improve speed and data cache needs
53 // be aware though that decreasing them may need the number of stages that is
54 // passed to get_vlc* to be increased.
56 #define H263_MBTYPE_B_VLC_BITS 6
57 #define CBPC_B_VLC_BITS 3
59 static const int h263_mb_type_b_map[15]= {
60 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
61 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
62 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
63 MB_TYPE_L0 | MB_TYPE_16x16,
64 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
65 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
66 MB_TYPE_L1 | MB_TYPE_16x16,
67 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
68 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
69 MB_TYPE_L0L1 | MB_TYPE_16x16,
70 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
71 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
73 MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
74 MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
77 void ff_h263_show_pict_info(MpegEncContext *s){
78 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
79 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",
80 s->qscale, av_get_picture_type_char(s->pict_type),
81 s->gb.size_in_bits, 1-s->no_rounding,
83 s->umvplus ? " UMV" : "",
84 s->h263_long_vectors ? " LONG" : "",
85 s->h263_plus ? " +" : "",
86 s->h263_aic ? " AIC" : "",
87 s->alt_inter_vlc ? " AIV" : "",
88 s->modified_quant ? " MQ" : "",
89 s->loop_filter ? " LOOP" : "",
90 s->h263_slice_structured ? " SS" : "",
91 s->avctx->framerate.num, s->avctx->framerate.den
96 /***********************************************/
99 VLC ff_h263_intra_MCBPC_vlc;
100 VLC ff_h263_inter_MCBPC_vlc;
101 VLC ff_h263_cbpy_vlc;
103 static VLC h263_mbtype_b_vlc;
104 static VLC cbpc_b_vlc;
108 /* XXX: find a better solution to handle static init */
109 av_cold void ff_h263_decode_init_vlc(void)
111 static volatile int done = 0;
114 INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
115 ff_h263_intra_MCBPC_bits, 1, 1,
116 ff_h263_intra_MCBPC_code, 1, 1, 72);
117 INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
118 ff_h263_inter_MCBPC_bits, 1, 1,
119 ff_h263_inter_MCBPC_code, 1, 1, 198);
120 INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
121 &ff_h263_cbpy_tab[0][1], 2, 1,
122 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
123 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
124 &ff_mvtab[0][1], 2, 1,
125 &ff_mvtab[0][0], 2, 1, 538);
126 ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
127 ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
128 INIT_VLC_RL(ff_h263_rl_inter, 554);
129 INIT_VLC_RL(ff_rl_intra_aic, 554);
130 INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
131 &ff_h263_mbtype_b_tab[0][1], 2, 1,
132 &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
133 INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
134 &ff_cbpc_b_tab[0][1], 2, 1,
135 &ff_cbpc_b_tab[0][0], 2, 1, 8);
140 int ff_h263_decode_mba(MpegEncContext *s)
144 for (i = 0; i < 6; i++)
145 if (s->mb_num - 1 <= ff_mba_max[i])
147 mb_pos = get_bits(&s->gb, ff_mba_length[i]);
148 s->mb_x = mb_pos % s->mb_width;
149 s->mb_y = mb_pos / s->mb_width;
155 * Decode the group of blocks header or slice header.
156 * @return <0 if an error occurred
158 static int h263_decode_gob_header(MpegEncContext *s)
160 unsigned int val, gob_number;
163 /* Check for GOB Start Code */
164 val = show_bits(&s->gb, 16);
168 /* We have a GBSC probably with GSTUFF */
169 skip_bits(&s->gb, 16); /* Drop the zeros */
170 left= get_bits_left(&s->gb);
171 left = FFMIN(left, 32);
172 //MN: we must check the bits left or we might end in an infinite loop (or segfault)
173 for(;left>13; left--){
174 if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
179 if(s->h263_slice_structured){
180 if(check_marker(s->avctx, &s->gb, "before MBA")==0)
183 ff_h263_decode_mba(s);
186 if(check_marker(s->avctx, &s->gb, "after MBA")==0)
189 s->qscale = get_bits(&s->gb, 5); /* SQUANT */
190 if(check_marker(s->avctx, &s->gb, "after SQUANT")==0)
192 skip_bits(&s->gb, 2); /* GFID */
194 gob_number = get_bits(&s->gb, 5); /* GN */
196 s->mb_y= s->gob_index* gob_number;
197 skip_bits(&s->gb, 2); /* GFID */
198 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
201 if(s->mb_y >= s->mb_height)
211 * Decode the group of blocks / video packet header / slice header (MPEG-4 Studio).
212 * @return bit position of the resync_marker, or <0 if none was found
214 int ff_h263_resync(MpegEncContext *s){
217 /* In MPEG-4 studio mode look for a new slice startcode
218 * and decode slice header */
219 if(s->codec_id==AV_CODEC_ID_MPEG4 && s->studio_profile) {
220 align_get_bits(&s->gb);
222 while (get_bits_left(&s->gb) >= 32 && show_bits_long(&s->gb, 32) != SLICE_START_CODE) {
226 if (get_bits_left(&s->gb) >= 32 && show_bits_long(&s->gb, 32) == SLICE_START_CODE)
227 return get_bits_count(&s->gb);
232 if(s->codec_id==AV_CODEC_ID_MPEG4){
234 align_get_bits(&s->gb);
237 if(show_bits(&s->gb, 16)==0){
238 pos= get_bits_count(&s->gb);
239 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
240 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
242 ret= h263_decode_gob_header(s);
246 //OK, it's not where it is supposed to be ...
247 s->gb= s->last_resync_gb;
248 align_get_bits(&s->gb);
249 left= get_bits_left(&s->gb);
251 for(;left>16+1+5+5; left-=8){
252 if(show_bits(&s->gb, 16)==0){
253 GetBitContext bak= s->gb;
255 pos= get_bits_count(&s->gb);
256 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
257 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
259 ret= h263_decode_gob_header(s);
265 skip_bits(&s->gb, 8);
271 int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
273 int code, val, sign, shift;
274 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
281 sign = get_bits1(&s->gb);
285 val = (val - 1) << shift;
286 val |= get_bits(&s->gb, shift);
293 /* modulo decoding */
294 if (!s->h263_long_vectors) {
295 val = sign_extend(val, 5 + f_code);
297 /* horrible H.263 long vector mode */
298 if (pred < -31 && val < -63)
300 if (pred > 32 && val > 63)
308 /* Decode RVLC of H.263+ UMV */
309 static int h263p_decode_umotion(MpegEncContext * s, int pred)
313 if (get_bits1(&s->gb)) /* Motion difference = 0 */
316 code = 2 + get_bits1(&s->gb);
318 while (get_bits1(&s->gb))
321 code += get_bits1(&s->gb);
323 avpriv_request_sample(s->avctx, "Huge DMV");
330 code = (sign) ? (pred - code) : (pred + code);
331 ff_tlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
337 * read the next MVs for OBMC. yes this is an ugly hack, feel free to send a patch :)
339 static void preview_obmc(MpegEncContext *s){
340 GetBitContext gb= s->gb;
342 int cbpc, i, pred_x, pred_y, mx, my;
344 const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
345 const int stride= s->b8_stride*2;
348 s->block_index[i]+= 2;
350 s->block_index[i]+= 1;
353 av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
356 if (get_bits1(&s->gb)) {
358 mot_val = s->current_picture.motion_val[0][s->block_index[0]];
359 mot_val[0 ]= mot_val[2 ]=
360 mot_val[0+stride]= mot_val[2+stride]= 0;
361 mot_val[1 ]= mot_val[3 ]=
362 mot_val[1+stride]= mot_val[3+stride]= 0;
364 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
367 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
371 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
373 get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
375 if(s->modified_quant){
376 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
377 else skip_bits(&s->gb, 5);
379 skip_bits(&s->gb, 2);
382 if ((cbpc & 16) == 0) {
383 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
384 /* 16x16 motion prediction */
385 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
387 mx = h263p_decode_umotion(s, pred_x);
389 mx = ff_h263_decode_motion(s, pred_x, 1);
392 my = h263p_decode_umotion(s, pred_y);
394 my = ff_h263_decode_motion(s, pred_y, 1);
396 mot_val[0 ]= mot_val[2 ]=
397 mot_val[0+stride]= mot_val[2+stride]= mx;
398 mot_val[1 ]= mot_val[3 ]=
399 mot_val[1+stride]= mot_val[3+stride]= my;
401 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
403 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
405 mx = h263p_decode_umotion(s, pred_x);
407 mx = ff_h263_decode_motion(s, pred_x, 1);
410 my = h263p_decode_umotion(s, pred_y);
412 my = ff_h263_decode_motion(s, pred_y, 1);
413 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
414 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
423 s->block_index[i]-= 2;
425 s->block_index[i]-= 1;
431 static void h263_decode_dquant(MpegEncContext *s){
432 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
434 if(s->modified_quant){
435 if(get_bits1(&s->gb))
436 s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
438 s->qscale= get_bits(&s->gb, 5);
440 s->qscale += quant_tab[get_bits(&s->gb, 2)];
441 ff_set_qscale(s, s->qscale);
444 static int h263_decode_block(MpegEncContext * s, int16_t * block,
447 int level, i, j, run;
448 RLTable *rl = &ff_h263_rl_inter;
449 const uint8_t *scan_table;
450 GetBitContext gb= s->gb;
452 scan_table = s->intra_scantable.permutated;
453 if (s->h263_aic && s->mb_intra) {
454 rl = &ff_rl_intra_aic;
458 scan_table = s->intra_v_scantable.permutated; /* left */
460 scan_table = s->intra_h_scantable.permutated; /* top */
462 } else if (s->mb_intra) {
464 if (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
465 if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
467 component = (n <= 3 ? 0 : n - 4 + 1);
468 level = s->last_dc[component];
469 if (s->rv10_first_dc_coded[component]) {
470 diff = ff_rv_decode_dc(s, n);
474 level = level & 0xff; /* handle wrap round */
475 s->last_dc[component] = level;
477 s->rv10_first_dc_coded[component] = 1;
480 level = get_bits(&s->gb, 8);
485 level = get_bits(&s->gb, 8);
486 if((level&0x7F) == 0){
487 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
488 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
500 if (s->mb_intra && s->h263_aic)
502 s->block_last_index[n] = i - 1;
507 OPEN_READER(re, &s->gb);
508 i--; // offset by -1 to allow direct indexing of scan_table
510 UPDATE_CACHE(re, &s->gb);
511 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
514 CLOSE_READER(re, &s->gb);
515 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
519 if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
520 int is11 = SHOW_UBITS(re, &s->gb, 1);
521 SKIP_CACHE(re, &s->gb, 1);
522 run = SHOW_UBITS(re, &s->gb, 7) + 1;
524 SKIP_COUNTER(re, &s->gb, 1 + 7);
525 UPDATE_CACHE(re, &s->gb);
526 level = SHOW_SBITS(re, &s->gb, 11);
527 SKIP_COUNTER(re, &s->gb, 11);
529 SKIP_CACHE(re, &s->gb, 7);
530 level = SHOW_SBITS(re, &s->gb, 7);
531 SKIP_COUNTER(re, &s->gb, 1 + 7 + 7);
534 run = SHOW_UBITS(re, &s->gb, 7) + 1;
535 SKIP_CACHE(re, &s->gb, 7);
536 level = (int8_t)SHOW_UBITS(re, &s->gb, 8);
537 SKIP_COUNTER(re, &s->gb, 7 + 8);
539 UPDATE_CACHE(re, &s->gb);
540 if (s->codec_id == AV_CODEC_ID_RV10) {
541 /* XXX: should patch encoder too */
542 level = SHOW_SBITS(re, &s->gb, 12);
543 SKIP_COUNTER(re, &s->gb, 12);
545 level = SHOW_UBITS(re, &s->gb, 5);
546 SKIP_CACHE(re, &s->gb, 5);
547 level |= SHOW_SBITS(re, &s->gb, 6) * (1<<5);
548 SKIP_COUNTER(re, &s->gb, 5 + 6);
553 if (SHOW_UBITS(re, &s->gb, 1))
555 SKIP_COUNTER(re, &s->gb, 1);
559 CLOSE_READER(re, &s->gb);
560 // redo update without last flag, revert -1 offset
561 i = i - run + ((run-1)&63) + 1;
563 // only last marker, no overrun
564 block[scan_table[i]] = level;
567 if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
568 //Looks like a hack but no, it's the way it is supposed to work ...
569 rl = &ff_rl_intra_aic;
572 s->bdsp.clear_block(block);
575 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
583 if (s->mb_intra && s->h263_aic) {
584 ff_h263_pred_acdc(s, block, n);
587 s->block_last_index[n] = i;
591 static int h263_skip_b_part(MpegEncContext *s, int cbp)
593 LOCAL_ALIGNED_32(int16_t, dblock, [64]);
597 /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
598 * but real value should be restored in order to be used later (in OBMC condition)
601 memcpy(bli, s->block_last_index, sizeof(bli));
603 for (i = 0; i < 6; i++) {
604 if (h263_decode_block(s, dblock, i, cbp&32) < 0)
609 memcpy(s->block_last_index, bli, sizeof(bli));
613 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
617 if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
619 if (pb_frame == 2 && c)
621 } else { // h.263 Annex M improved PB-frame
622 mv = get_unary(gb, 0, 4) + 1;
627 *cbpb = get_bits(gb, 6);
631 #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
632 #define tab_bias (tab_size / 2)
633 static inline void set_one_direct_mv(MpegEncContext *s, Picture *p, int i)
635 int xy = s->block_index[i];
636 uint16_t time_pp = s->pp_time;
637 uint16_t time_pb = s->pb_time;
640 p_mx = p->motion_val[0][xy][0];
641 if ((unsigned)(p_mx + tab_bias) < tab_size) {
642 s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias];
643 s->mv[1][i][0] = s->direct_scale_mv[1][p_mx + tab_bias];
645 s->mv[0][i][0] = p_mx * time_pb / time_pp;
646 s->mv[1][i][0] = p_mx * (time_pb - time_pp) / time_pp;
648 p_my = p->motion_val[0][xy][1];
649 if ((unsigned)(p_my + tab_bias) < tab_size) {
650 s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias];
651 s->mv[1][i][1] = s->direct_scale_mv[1][p_my + tab_bias];
653 s->mv[0][i][1] = p_my * time_pb / time_pp;
654 s->mv[1][i][1] = p_my * (time_pb - time_pp) / time_pp;
659 * @return the mb_type
661 static int set_direct_mv(MpegEncContext *s)
663 const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
664 Picture *p = &s->next_picture;
665 int colocated_mb_type = p->mb_type[mb_index];
668 if (s->codec_tag == AV_RL32("U263") && p->f->pict_type == AV_PICTURE_TYPE_I) {
669 p = &s->last_picture;
670 colocated_mb_type = p->mb_type[mb_index];
673 if (IS_8X8(colocated_mb_type)) {
674 s->mv_type = MV_TYPE_8X8;
675 for (i = 0; i < 4; i++)
676 set_one_direct_mv(s, p, i);
677 return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
679 set_one_direct_mv(s, p, 0);
682 s->mv[0][3][0] = s->mv[0][0][0];
685 s->mv[0][3][1] = s->mv[0][0][1];
688 s->mv[1][3][0] = s->mv[1][0][0];
691 s->mv[1][3][1] = s->mv[1][0][1];
692 s->mv_type = MV_TYPE_8X8;
693 // Note see prev line
694 return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1;
698 int ff_h263_decode_mb(MpegEncContext *s,
699 int16_t block[6][64])
701 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
703 const int xy= s->mb_x + s->mb_y * s->mb_stride;
704 int cbpb = 0, pb_mv_count = 0;
706 av_assert2(!s->h263_pred);
708 if (s->pict_type == AV_PICTURE_TYPE_P) {
710 if (get_bits1(&s->gb)) {
714 s->block_last_index[i] = -1;
715 s->mv_dir = MV_DIR_FORWARD;
716 s->mv_type = MV_TYPE_16X16;
717 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
720 s->mb_skipped = !(s->obmc | s->loop_filter);
723 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
725 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
730 s->bdsp.clear_blocks(s->block[0]);
733 s->mb_intra = ((cbpc & 4) != 0);
734 if (s->mb_intra) goto intra;
736 if(s->pb_frame && get_bits1(&s->gb))
737 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
738 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
741 av_log(s->avctx, AV_LOG_ERROR, "cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
745 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
748 cbp = (cbpc & 3) | (cbpy << 2);
750 h263_decode_dquant(s);
753 s->mv_dir = MV_DIR_FORWARD;
754 if ((cbpc & 16) == 0) {
755 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
756 /* 16x16 motion prediction */
757 s->mv_type = MV_TYPE_16X16;
758 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
760 mx = h263p_decode_umotion(s, pred_x);
762 mx = ff_h263_decode_motion(s, pred_x, 1);
768 my = h263p_decode_umotion(s, pred_y);
770 my = ff_h263_decode_motion(s, pred_y, 1);
777 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
778 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
780 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
781 s->mv_type = MV_TYPE_8X8;
783 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
785 mx = h263p_decode_umotion(s, pred_x);
787 mx = ff_h263_decode_motion(s, pred_x, 1);
792 my = h263p_decode_umotion(s, pred_y);
794 my = ff_h263_decode_motion(s, pred_y, 1);
799 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
800 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
805 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
807 const int stride= s->b8_stride;
808 int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
809 int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
810 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
813 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
814 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
815 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
816 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
819 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
821 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
825 mb_type= h263_mb_type_b_map[ mb_type ];
828 s->mb_intra = IS_INTRA(mb_type);
829 if(HAS_CBP(mb_type)){
830 s->bdsp.clear_blocks(s->block[0]);
831 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
833 dquant = IS_QUANT(mb_type);
837 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
840 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
844 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
847 cbp = (cbpc & 3) | (cbpy << 2);
851 av_assert2(!s->mb_intra);
853 if(IS_QUANT(mb_type)){
854 h263_decode_dquant(s);
857 if(IS_DIRECT(mb_type)){
858 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
859 mb_type |= set_direct_mv(s);
862 s->mv_type= MV_TYPE_16X16;
865 if(USES_LIST(mb_type, 0)){
866 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
867 s->mv_dir = MV_DIR_FORWARD;
870 mx = h263p_decode_umotion(s, pred_x);
872 mx = ff_h263_decode_motion(s, pred_x, 1);
877 my = h263p_decode_umotion(s, pred_y);
879 my = ff_h263_decode_motion(s, pred_y, 1);
883 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
884 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
888 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
889 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
892 if(USES_LIST(mb_type, 1)){
893 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &pred_x, &pred_y);
894 s->mv_dir |= MV_DIR_BACKWARD;
897 mx = h263p_decode_umotion(s, pred_x);
899 mx = ff_h263_decode_motion(s, pred_x, 1);
904 my = h263p_decode_umotion(s, pred_y);
906 my = ff_h263_decode_motion(s, pred_y, 1);
910 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
911 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
915 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
916 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
920 s->current_picture.mb_type[xy] = mb_type;
921 } else { /* I-Frame */
923 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
925 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
930 s->bdsp.clear_blocks(s->block[0]);
935 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
937 s->ac_pred = get_bits1(&s->gb);
939 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
941 s->h263_aic_dir = get_bits1(&s->gb);
946 if(s->pb_frame && get_bits1(&s->gb))
947 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
948 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
950 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
953 cbp = (cbpc & 3) | (cbpy << 2);
955 h263_decode_dquant(s);
958 pb_mv_count += !!s->pb_frame;
961 while(pb_mv_count--){
962 ff_h263_decode_motion(s, 0, 1);
963 ff_h263_decode_motion(s, 0, 1);
966 /* decode each block */
967 for (i = 0; i < 6; i++) {
968 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
973 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
975 if(s->obmc && !s->mb_intra){
976 if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
981 if (get_bits_left(&s->gb) < 0)
982 return AVERROR_INVALIDDATA;
984 /* per-MB end of slice check */
986 int v= show_bits(&s->gb, 16);
988 if (get_bits_left(&s->gb) < 16) {
989 v >>= 16 - get_bits_left(&s->gb);
999 /* Most is hardcoded; should extend to handle all H.263 streams. */
1000 int ff_h263_decode_picture_header(MpegEncContext *s)
1002 int format, width, height, i, ret;
1005 align_get_bits(&s->gb);
1007 if (show_bits(&s->gb, 2) == 2 && s->avctx->frame_number == 0) {
1008 av_log(s->avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
1011 startcode= get_bits(&s->gb, 22-8);
1013 for(i= get_bits_left(&s->gb); i>24; i-=8) {
1014 startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
1016 if(startcode == 0x20)
1020 if (startcode != 0x20) {
1021 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
1024 /* temporal reference */
1025 i = get_bits(&s->gb, 8); /* picture timestamp */
1027 i -= (i - (s->picture_number & 0xFF) + 128) & ~0xFF;
1029 s->picture_number= (s->picture_number&~0xFF) + i;
1031 /* PTYPE starts here */
1032 if (check_marker(s->avctx, &s->gb, "in PTYPE") != 1) {
1035 if (get_bits1(&s->gb) != 0) {
1036 av_log(s->avctx, AV_LOG_ERROR, "Bad H.263 id\n");
1037 return -1; /* H.263 id */
1039 skip_bits1(&s->gb); /* split screen off */
1040 skip_bits1(&s->gb); /* camera off */
1041 skip_bits1(&s->gb); /* freeze picture release off */
1043 format = get_bits(&s->gb, 3);
1048 7 extended PTYPE (PLUSPTYPE)
1051 if (format != 7 && format != 6) {
1054 width = ff_h263_format[format][0];
1055 height = ff_h263_format[format][1];
1059 s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
1061 s->h263_long_vectors = get_bits1(&s->gb);
1063 if (get_bits1(&s->gb) != 0) {
1064 av_log(s->avctx, AV_LOG_ERROR, "H.263 SAC not supported\n");
1065 return -1; /* SAC: off */
1067 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
1068 s->unrestricted_mv = s->h263_long_vectors || s->obmc;
1070 s->pb_frame = get_bits1(&s->gb);
1071 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
1072 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
1076 s->avctx->sample_aspect_ratio= (AVRational){12,11};
1077 s->avctx->framerate = (AVRational){ 30000, 1001 };
1083 ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
1085 /* ufep other than 0 and 1 are reserved */
1088 format = get_bits(&s->gb, 3);
1089 ff_dlog(s->avctx, "ufep=1, format: %d\n", format);
1090 s->custom_pcf= get_bits1(&s->gb);
1091 s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
1092 if (get_bits1(&s->gb) != 0) {
1093 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
1095 s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
1096 s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
1097 s->loop_filter= get_bits1(&s->gb);
1098 s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
1099 if(s->avctx->lowres)
1102 s->h263_slice_structured= get_bits1(&s->gb);
1103 if (get_bits1(&s->gb) != 0) {
1104 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
1106 if (get_bits1(&s->gb) != 0) {
1107 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
1109 s->alt_inter_vlc= get_bits1(&s->gb);
1110 s->modified_quant= get_bits1(&s->gb);
1111 if(s->modified_quant)
1112 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
1114 skip_bits(&s->gb, 1); /* Prevent start code emulation */
1116 skip_bits(&s->gb, 3); /* Reserved */
1117 } else if (ufep != 0) {
1118 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
1123 s->pict_type = get_bits(&s->gb, 3);
1124 switch(s->pict_type){
1125 case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
1126 case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
1127 case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
1128 case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
1129 case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
1133 skip_bits(&s->gb, 2);
1134 s->no_rounding = get_bits1(&s->gb);
1135 skip_bits(&s->gb, 4);
1137 /* Get the picture dimensions */
1140 /* Custom Picture Format (CPFMT) */
1141 s->aspect_ratio_info = get_bits(&s->gb, 4);
1142 ff_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
1147 3 - 10:11 (525-type 4:3)
1148 4 - 16:11 (CIF 16:9)
1149 5 - 40:33 (525-type 16:9)
1152 width = (get_bits(&s->gb, 9) + 1) * 4;
1153 check_marker(s->avctx, &s->gb, "in dimensions");
1154 height = get_bits(&s->gb, 9) * 4;
1155 ff_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1156 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1157 /* expected dimensions */
1158 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
1159 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
1161 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1164 width = ff_h263_format[format][0];
1165 height = ff_h263_format[format][1];
1166 s->avctx->sample_aspect_ratio= (AVRational){12,11};
1168 s->avctx->sample_aspect_ratio.den <<= s->ehc_mode;
1169 if ((width == 0) || (height == 0))
1176 s->avctx->framerate.num = 1800000;
1177 s->avctx->framerate.den = 1000 + get_bits1(&s->gb);
1178 s->avctx->framerate.den *= get_bits(&s->gb, 7);
1179 if(s->avctx->framerate.den == 0){
1180 av_log(s, AV_LOG_ERROR, "zero framerate\n");
1183 gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
1184 s->avctx->framerate.den /= gcd;
1185 s->avctx->framerate.num /= gcd;
1187 s->avctx->framerate = (AVRational){ 30000, 1001 };
1192 skip_bits(&s->gb, 2); //extended Temporal reference
1197 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1200 if(s->h263_slice_structured){
1201 if (get_bits1(&s->gb) != 0) {
1202 av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1204 if (get_bits1(&s->gb) != 0) {
1205 av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1208 if (s->pict_type == AV_PICTURE_TYPE_B) {
1209 skip_bits(&s->gb, 4); //ELNUM
1211 skip_bits(&s->gb, 4); // RLNUM
1216 s->qscale = get_bits(&s->gb, 5);
1219 if ((ret = av_image_check_size(s->width, s->height, 0, s)) < 0)
1222 if (!(s->avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1223 if ((s->width * s->height / 256 / 8) > get_bits_left(&s->gb))
1224 return AVERROR_INVALIDDATA;
1227 s->mb_width = (s->width + 15) / 16;
1228 s->mb_height = (s->height + 15) / 16;
1229 s->mb_num = s->mb_width * s->mb_height;
1232 skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
1234 skip_bits(&s->gb, 2); //extended Temporal reference
1235 skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
1238 if (s->pict_type!=AV_PICTURE_TYPE_B) {
1239 s->time = s->picture_number;
1240 s->pp_time = s->time - s->last_non_b_time;
1241 s->last_non_b_time = s->time;
1243 s->time = s->picture_number;
1244 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
1245 if (s->pp_time <=s->pb_time ||
1246 s->pp_time <= s->pp_time - s->pb_time ||
1251 ff_mpeg4_init_direct_mv(s);
1255 if (skip_1stop_8data_bits(&s->gb) < 0)
1256 return AVERROR_INVALIDDATA;
1258 if(s->h263_slice_structured){
1259 if (check_marker(s->avctx, &s->gb, "SEPB1") != 1) {
1263 ff_h263_decode_mba(s);
1265 if (check_marker(s->avctx, &s->gb, "SEPB2") != 1) {
1271 if (s->pict_type == AV_PICTURE_TYPE_B)
1275 s->y_dc_scale_table=
1276 s->c_dc_scale_table= ff_aic_dc_scale_table;
1278 s->y_dc_scale_table=
1279 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1282 ff_h263_show_pict_info(s);
1283 if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
1285 for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1286 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1287 for(i=0; i<13; i++){
1289 int v= get_bits(&s->gb, 8);
1290 v |= get_sbits(&s->gb, 8) * (1 << 8);
1291 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
1293 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1295 for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));