LCOV - code coverage report
Current view: top level - libavcodec - ituh263dec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 510 749 68.1 %
Date: 2017-12-18 06:23:41 Functions: 13 16 81.2 %

          Line data    Source code
       1             : /*
       2             :  * ITU H.263 bitstream decoder
       3             :  * Copyright (c) 2000,2001 Fabrice Bellard
       4             :  * H.263+ support.
       5             :  * Copyright (c) 2001 Juan J. Sierralta P
       6             :  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
       7             :  *
       8             :  * This file is part of FFmpeg.
       9             :  *
      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.
      14             :  *
      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.
      19             :  *
      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
      23             :  */
      24             : 
      25             : /**
      26             :  * @file
      27             :  * H.263 decoder.
      28             :  */
      29             : 
      30             : #define UNCHECKED_BITSTREAM_READER 1
      31             : #include <limits.h>
      32             : 
      33             : #include "libavutil/attributes.h"
      34             : #include "libavutil/imgutils.h"
      35             : #include "libavutil/internal.h"
      36             : #include "libavutil/mathematics.h"
      37             : #include "avcodec.h"
      38             : #include "mpegvideo.h"
      39             : #include "h263.h"
      40             : #include "h263data.h"
      41             : #include "internal.h"
      42             : #include "mathops.h"
      43             : #include "mpegutils.h"
      44             : #include "unary.h"
      45             : #include "flv.h"
      46             : #include "rv10.h"
      47             : #include "mpeg4video.h"
      48             : #include "mpegvideodata.h"
      49             : 
      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.
      54             : #define MV_VLC_BITS 9
      55             : #define H263_MBTYPE_B_VLC_BITS 6
      56             : #define CBPC_B_VLC_BITS 3
      57             : 
      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,
      71             :     0, //stuffing
      72             :     MB_TYPE_INTRA4x4                | MB_TYPE_CBP,
      73             :     MB_TYPE_INTRA4x4                | MB_TYPE_CBP | MB_TYPE_QUANT,
      74             : };
      75             : 
      76         461 : void ff_h263_show_pict_info(MpegEncContext *s){
      77         461 :     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
      78           0 :     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           0 :          s->qscale, av_get_picture_type_char(s->pict_type),
      80           0 :          s->gb.size_in_bits, 1-s->no_rounding,
      81           0 :          s->obmc ? " AP" : "",
      82           0 :          s->umvplus ? " UMV" : "",
      83           0 :          s->h263_long_vectors ? " LONG" : "",
      84           0 :          s->h263_plus ? " +" : "",
      85           0 :          s->h263_aic ? " AIC" : "",
      86           0 :          s->alt_inter_vlc ? " AIV" : "",
      87           0 :          s->modified_quant ? " MQ" : "",
      88           0 :          s->loop_filter ? " LOOP" : "",
      89           0 :          s->h263_slice_structured ? " SS" : "",
      90           0 :          s->avctx->framerate.num, s->avctx->framerate.den
      91             :     );
      92             :     }
      93         461 : }
      94             : 
      95             : /***********************************************/
      96             : /* decoding */
      97             : 
      98             : VLC ff_h263_intra_MCBPC_vlc;
      99             : VLC ff_h263_inter_MCBPC_vlc;
     100             : VLC ff_h263_cbpy_vlc;
     101             : static VLC mv_vlc;
     102             : static VLC h263_mbtype_b_vlc;
     103             : static VLC cbpc_b_vlc;
     104             : 
     105             : /* init vlcs */
     106             : 
     107             : /* XXX: find a better solution to handle static init */
     108         296 : av_cold void ff_h263_decode_init_vlc(void)
     109             : {
     110             :     static volatile int done = 0;
     111             : 
     112         296 :     if (!done) {
     113         166 :         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         166 :         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         166 :         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         166 :         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         166 :         ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
     126         166 :         ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
     127         166 :         INIT_VLC_RL(ff_h263_rl_inter, 554);
     128         166 :         INIT_VLC_RL(ff_rl_intra_aic, 554);
     129         166 :         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         166 :         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);
     135         166 :         done = 1;
     136             :     }
     137         296 : }
     138             : 
     139         742 : int ff_h263_decode_mba(MpegEncContext *s)
     140             : {
     141             :     int i, mb_pos;
     142             : 
     143        2226 :     for (i = 0; i < 6; i++)
     144        2226 :         if (s->mb_num - 1 <= ff_mba_max[i])
     145         742 :             break;
     146         742 :     mb_pos  = get_bits(&s->gb, ff_mba_length[i]);
     147         742 :     s->mb_x = mb_pos % s->mb_width;
     148         742 :     s->mb_y = mb_pos / s->mb_width;
     149             : 
     150         742 :     return mb_pos;
     151             : }
     152             : 
     153             : /**
     154             :  * Decode the group of blocks header or slice header.
     155             :  * @return <0 if an error occurred
     156             :  */
     157        2544 : static int h263_decode_gob_header(MpegEncContext *s)
     158             : {
     159             :     unsigned int val, gob_number;
     160             :     int left;
     161             : 
     162             :     /* Check for GOB Start Code */
     163        2544 :     val = show_bits(&s->gb, 16);
     164        2544 :     if(val)
     165           0 :         return -1;
     166             : 
     167             :         /* We have a GBSC probably with GSTUFF */
     168        2544 :     skip_bits(&s->gb, 16); /* Drop the zeros */
     169        2544 :     left= get_bits_left(&s->gb);
     170        2544 :     left = FFMIN(left, 32);
     171             :     //MN: we must check the bits left or we might end in an infinite loop (or segfault)
     172       11538 :     for(;left>13; left--){
     173       11538 :         if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
     174             :     }
     175        2544 :     if(left<=13)
     176           0 :         return -1;
     177             : 
     178        2544 :     if(s->h263_slice_structured){
     179           0 :         if(check_marker(s->avctx, &s->gb, "before MBA")==0)
     180           0 :             return -1;
     181             : 
     182           0 :         ff_h263_decode_mba(s);
     183             : 
     184           0 :         if(s->mb_num > 1583)
     185           0 :             if(check_marker(s->avctx, &s->gb, "after MBA")==0)
     186           0 :                 return -1;
     187             : 
     188           0 :         s->qscale = get_bits(&s->gb, 5); /* SQUANT */
     189           0 :         if(check_marker(s->avctx, &s->gb, "after SQUANT")==0)
     190           0 :             return -1;
     191           0 :         skip_bits(&s->gb, 2); /* GFID */
     192             :     }else{
     193        2544 :         gob_number = get_bits(&s->gb, 5); /* GN */
     194        2544 :         s->mb_x= 0;
     195        2544 :         s->mb_y= s->gob_index* gob_number;
     196        2544 :         skip_bits(&s->gb, 2); /* GFID */
     197        2544 :         s->qscale = get_bits(&s->gb, 5); /* GQUANT */
     198             :     }
     199             : 
     200        2544 :     if(s->mb_y >= s->mb_height)
     201           0 :         return -1;
     202             : 
     203        2544 :     if(s->qscale==0)
     204           0 :         return -1;
     205             : 
     206        2544 :     return 0;
     207             : }
     208             : 
     209             : /**
     210             :  * Decode the group of blocks / video packet header.
     211             :  * @return bit position of the resync_marker, or <0 if none was found
     212             :  */
     213        6411 : int ff_h263_resync(MpegEncContext *s){
     214             :     int left, pos, ret;
     215             : 
     216        6411 :     if(s->codec_id==AV_CODEC_ID_MPEG4){
     217        3867 :         skip_bits1(&s->gb);
     218        3867 :         align_get_bits(&s->gb);
     219             :     }
     220             : 
     221        6411 :     if(show_bits(&s->gb, 16)==0){
     222        6411 :         pos= get_bits_count(&s->gb);
     223        6411 :         if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
     224        3867 :             ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
     225             :         else
     226        2544 :             ret= h263_decode_gob_header(s);
     227        6411 :         if(ret>=0)
     228        6411 :             return pos;
     229             :     }
     230             :     //OK, it's not where it is supposed to be ...
     231           0 :     s->gb= s->last_resync_gb;
     232           0 :     align_get_bits(&s->gb);
     233           0 :     left= get_bits_left(&s->gb);
     234             : 
     235           0 :     for(;left>16+1+5+5; left-=8){
     236           0 :         if(show_bits(&s->gb, 16)==0){
     237           0 :             GetBitContext bak= s->gb;
     238             : 
     239           0 :             pos= get_bits_count(&s->gb);
     240           0 :             if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
     241           0 :                 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
     242             :             else
     243           0 :                 ret= h263_decode_gob_header(s);
     244           0 :             if(ret>=0)
     245           0 :                 return pos;
     246             : 
     247           0 :             s->gb= bak;
     248             :         }
     249           0 :         skip_bits(&s->gb, 8);
     250             :     }
     251             : 
     252           0 :     return -1;
     253             : }
     254             : 
     255     3787838 : int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
     256             : {
     257             :     int code, val, sign, shift;
     258     3787838 :     code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
     259             : 
     260     3787838 :     if (code == 0)
     261     1862335 :         return pred;
     262     1925503 :     if (code < 0)
     263           0 :         return 0xffff;
     264             : 
     265     1925503 :     sign = get_bits1(&s->gb);
     266     1925503 :     shift = f_code - 1;
     267     1925503 :     val = code;
     268     1925503 :     if (shift) {
     269      222315 :         val = (val - 1) << shift;
     270      222315 :         val |= get_bits(&s->gb, shift);
     271      222315 :         val++;
     272             :     }
     273     1925503 :     if (sign)
     274      807460 :         val = -val;
     275     1925503 :     val += pred;
     276             : 
     277             :     /* modulo decoding */
     278     1925503 :     if (!s->h263_long_vectors) {
     279     1925503 :         val = sign_extend(val, 5 + f_code);
     280             :     } else {
     281             :         /* horrible H.263 long vector mode */
     282           0 :         if (pred < -31 && val < -63)
     283           0 :             val += 64;
     284           0 :         if (pred > 32 && val > 63)
     285           0 :             val -= 64;
     286             : 
     287             :     }
     288     1925503 :     return val;
     289             : }
     290             : 
     291             : 
     292             : /* Decode RVLC of H.263+ UMV */
     293      103298 : static int h263p_decode_umotion(MpegEncContext * s, int pred)
     294             : {
     295      103298 :    int code = 0, sign;
     296             : 
     297      103298 :    if (get_bits1(&s->gb)) /* Motion difference = 0 */
     298       51255 :       return pred;
     299             : 
     300       52043 :    code = 2 + get_bits1(&s->gb);
     301             : 
     302      132845 :    while (get_bits1(&s->gb))
     303             :    {
     304       28759 :       code <<= 1;
     305       28759 :       code += get_bits1(&s->gb);
     306       28759 :       if (code >= 32768) {
     307           0 :           avpriv_request_sample(s->avctx, "Huge DMV");
     308           0 :           return 0xffff;
     309             :       }
     310             :    }
     311       52043 :    sign = code & 1;
     312       52043 :    code >>= 1;
     313             : 
     314       52043 :    code = (sign) ? (pred - code) : (pred + code);
     315             :    ff_tlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
     316       52043 :    return code;
     317             : 
     318             : }
     319             : 
     320             : /**
     321             :  * read the next MVs for OBMC. yes this is an ugly hack, feel free to send a patch :)
     322             :  */
     323       48550 : static void preview_obmc(MpegEncContext *s){
     324       48550 :     GetBitContext gb= s->gb;
     325             : 
     326             :     int cbpc, i, pred_x, pred_y, mx, my;
     327             :     int16_t *mot_val;
     328       48550 :     const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
     329       48550 :     const int stride= s->b8_stride*2;
     330             : 
     331      242750 :     for(i=0; i<4; i++)
     332      194200 :         s->block_index[i]+= 2;
     333      145650 :     for(i=4; i<6; i++)
     334       97100 :         s->block_index[i]+= 1;
     335       48550 :     s->mb_x++;
     336             : 
     337             :     av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
     338             : 
     339             :     do{
     340       48550 :         if (get_bits1(&s->gb)) {
     341             :             /* skip mb */
     342         654 :             mot_val = s->current_picture.motion_val[0][s->block_index[0]];
     343        1308 :             mot_val[0       ]= mot_val[2       ]=
     344        1308 :             mot_val[0+stride]= mot_val[2+stride]= 0;
     345        1308 :             mot_val[1       ]= mot_val[3       ]=
     346        1308 :             mot_val[1+stride]= mot_val[3+stride]= 0;
     347             : 
     348         654 :             s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
     349         654 :             goto end;
     350             :         }
     351       47896 :         cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
     352       47896 :     }while(cbpc == 20);
     353             : 
     354       47896 :     if(cbpc & 4){
     355         957 :         s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
     356             :     }else{
     357       46939 :         get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     358       46939 :         if (cbpc & 8) {
     359           0 :             if(s->modified_quant){
     360           0 :                 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
     361           0 :                 else                  skip_bits(&s->gb, 5);
     362             :             }else
     363           0 :                 skip_bits(&s->gb, 2);
     364             :         }
     365             : 
     366       46939 :         if ((cbpc & 16) == 0) {
     367       46939 :                 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
     368             :                 /* 16x16 motion prediction */
     369       46939 :                 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
     370       46939 :                 if (s->umvplus)
     371           0 :                     mx = h263p_decode_umotion(s, pred_x);
     372             :                 else
     373       46939 :                     mx = ff_h263_decode_motion(s, pred_x, 1);
     374             : 
     375       46939 :                 if (s->umvplus)
     376           0 :                     my = h263p_decode_umotion(s, pred_y);
     377             :                 else
     378       46939 :                     my = ff_h263_decode_motion(s, pred_y, 1);
     379             : 
     380       93878 :                 mot_val[0       ]= mot_val[2       ]=
     381       93878 :                 mot_val[0+stride]= mot_val[2+stride]= mx;
     382       93878 :                 mot_val[1       ]= mot_val[3       ]=
     383       93878 :                 mot_val[1+stride]= mot_val[3+stride]= my;
     384             :         } else {
     385           0 :             s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
     386           0 :             for(i=0;i<4;i++) {
     387           0 :                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
     388           0 :                 if (s->umvplus)
     389           0 :                     mx = h263p_decode_umotion(s, pred_x);
     390             :                 else
     391           0 :                     mx = ff_h263_decode_motion(s, pred_x, 1);
     392             : 
     393           0 :                 if (s->umvplus)
     394           0 :                     my = h263p_decode_umotion(s, pred_y);
     395             :                 else
     396           0 :                     my = ff_h263_decode_motion(s, pred_y, 1);
     397           0 :                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
     398           0 :                     skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
     399           0 :                 mot_val[0] = mx;
     400           0 :                 mot_val[1] = my;
     401             :             }
     402             :         }
     403             :     }
     404           0 : end:
     405             : 
     406      242750 :     for(i=0; i<4; i++)
     407      194200 :         s->block_index[i]-= 2;
     408      145650 :     for(i=4; i<6; i++)
     409       97100 :         s->block_index[i]-= 1;
     410       48550 :     s->mb_x--;
     411             : 
     412       48550 :     s->gb= gb;
     413       48550 : }
     414             : 
     415           0 : static void h263_decode_dquant(MpegEncContext *s){
     416             :     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
     417             : 
     418           0 :     if(s->modified_quant){
     419           0 :         if(get_bits1(&s->gb))
     420           0 :             s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
     421             :         else
     422           0 :             s->qscale= get_bits(&s->gb, 5);
     423             :     }else
     424           0 :         s->qscale += quant_tab[get_bits(&s->gb, 2)];
     425           0 :     ff_set_qscale(s, s->qscale);
     426           0 : }
     427             : 
     428     2828502 : static int h263_decode_block(MpegEncContext * s, int16_t * block,
     429             :                              int n, int coded)
     430             : {
     431             :     int level, i, j, run;
     432     2828502 :     RLTable *rl = &ff_h263_rl_inter;
     433             :     const uint8_t *scan_table;
     434     2828502 :     GetBitContext gb= s->gb;
     435             : 
     436     2828502 :     scan_table = s->intra_scantable.permutated;
     437     2828502 :     if (s->h263_aic && s->mb_intra) {
     438       91098 :         rl = &ff_rl_intra_aic;
     439       91098 :         i = 0;
     440      182196 :         if (s->ac_pred) {
     441        4434 :             if (s->h263_aic_dir)
     442        1596 :                 scan_table = s->intra_v_scantable.permutated; /* left */
     443             :             else
     444        2838 :                 scan_table = s->intra_h_scantable.permutated; /* top */
     445             :         }
     446     2737404 :     } else if (s->mb_intra) {
     447             :         /* DC coef */
     448      327756 :         if (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
     449       46710 :           if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
     450             :             int component, diff;
     451           0 :             component = (n <= 3 ? 0 : n - 4 + 1);
     452           0 :             level = s->last_dc[component];
     453           0 :             if (s->rv10_first_dc_coded[component]) {
     454           0 :                 diff = ff_rv_decode_dc(s, n);
     455           0 :                 if (diff == 0xffff)
     456           0 :                     return -1;
     457           0 :                 level += diff;
     458           0 :                 level = level & 0xff; /* handle wrap round */
     459           0 :                 s->last_dc[component] = level;
     460             :             } else {
     461           0 :                 s->rv10_first_dc_coded[component] = 1;
     462             :             }
     463             :           } else {
     464       46710 :                 level = get_bits(&s->gb, 8);
     465       46710 :                 if (level == 255)
     466         542 :                     level = 128;
     467             :           }
     468             :         }else{
     469      281046 :             level = get_bits(&s->gb, 8);
     470      281046 :             if((level&0x7F) == 0){
     471           0 :                 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
     472           0 :                 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
     473           0 :                     return -1;
     474             :             }
     475      281046 :             if (level == 255)
     476        3309 :                 level = 128;
     477             :         }
     478      327756 :         block[0] = level;
     479      327756 :         i = 1;
     480             :     } else {
     481     2409648 :         i = 0;
     482             :     }
     483     2828502 :     if (!coded) {
     484     1340005 :         if (s->mb_intra && s->h263_aic)
     485        3823 :             goto not_coded;
     486     1336182 :         s->block_last_index[n] = i - 1;
     487     1336182 :         return 0;
     488             :     }
     489     1488497 : retry:
     490             :     {
     491     1554956 :     OPEN_READER(re, &s->gb);
     492     1554956 :     i--; // offset by -1 to allow direct indexing of scan_table
     493             :     for(;;) {
     494    29076944 :         UPDATE_CACHE(re, &s->gb);
     495    15315950 :         GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
     496    15315950 :         if (run == 66) {
     497      640501 :             if (level){
     498           0 :                 CLOSE_READER(re, &s->gb);
     499           0 :                 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
     500           0 :                 return -1;
     501             :             }
     502             :             /* escape */
     503      640501 :             if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
     504      170446 :                 int is11 = SHOW_UBITS(re, &s->gb, 1);
     505      170446 :                 SKIP_CACHE(re, &s->gb, 1);
     506      170446 :                 run = SHOW_UBITS(re, &s->gb, 7) + 1;
     507      170446 :                 if (is11) {
     508           0 :                     SKIP_COUNTER(re, &s->gb, 1 + 7);
     509           0 :                     UPDATE_CACHE(re, &s->gb);
     510           0 :                     level = SHOW_SBITS(re, &s->gb, 11);
     511           0 :                     SKIP_COUNTER(re, &s->gb, 11);
     512             :                 } else {
     513      170446 :                     SKIP_CACHE(re, &s->gb, 7);
     514      170446 :                     level = SHOW_SBITS(re, &s->gb, 7);
     515      170446 :                     SKIP_COUNTER(re, &s->gb, 1 + 7 + 7);
     516             :                 }
     517             :             } else {
     518      470055 :                 run = SHOW_UBITS(re, &s->gb, 7) + 1;
     519      470055 :                 SKIP_CACHE(re, &s->gb, 7);
     520      470055 :                 level = (int8_t)SHOW_UBITS(re, &s->gb, 8);
     521      470055 :                 SKIP_COUNTER(re, &s->gb, 7 + 8);
     522      470055 :                 if(level == -128){
     523        3157 :                     UPDATE_CACHE(re, &s->gb);
     524        3157 :                     if (s->codec_id == AV_CODEC_ID_RV10) {
     525             :                         /* XXX: should patch encoder too */
     526           0 :                         level = SHOW_SBITS(re, &s->gb, 12);
     527           0 :                         SKIP_COUNTER(re, &s->gb, 12);
     528             :                     }else{
     529        3157 :                         level = SHOW_UBITS(re, &s->gb, 5);
     530        3157 :                         SKIP_CACHE(re, &s->gb, 5);
     531        3157 :                         level |= SHOW_SBITS(re, &s->gb, 6) * (1<<5);
     532        3157 :                         SKIP_COUNTER(re, &s->gb, 5 + 6);
     533             :                     }
     534             :                 }
     535             :             }
     536             :         } else {
     537    14675449 :             if (SHOW_UBITS(re, &s->gb, 1))
     538     7316240 :                 level = -level;
     539    14675449 :             SKIP_COUNTER(re, &s->gb, 1);
     540             :         }
     541    15315950 :         i += run;
     542    15315950 :         if (i >= 64){
     543     1554956 :             CLOSE_READER(re, &s->gb);
     544             :             // redo update without last flag, revert -1 offset
     545     1554956 :             i = i - run + ((run-1)&63) + 1;
     546     1554956 :             if (i < 64) {
     547             :                 // only last marker, no overrun
     548     1488497 :                 block[scan_table[i]] = level;
     549     1488497 :                 break;
     550             :             }
     551       66459 :             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       66459 :                 rl = &ff_rl_intra_aic;
     554       66459 :                 i = 0;
     555       66459 :                 s->gb= gb;
     556       66459 :                 s->bdsp.clear_block(block);
     557       66459 :                 goto retry;
     558             :             }
     559           0 :             av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
     560           0 :             return -1;
     561             :         }
     562    13760994 :         j = scan_table[i];
     563    13760994 :         block[j] = level;
     564             :     }
     565             :     }
     566     1492320 : not_coded:
     567     1492320 :     if (s->mb_intra && s->h263_aic) {
     568       91098 :         ff_h263_pred_acdc(s, block, n);
     569       91098 :         i = 63;
     570             :     }
     571     1492320 :     s->block_last_index[n] = i;
     572     1492320 :     return 0;
     573             : }
     574             : 
     575           0 : static int h263_skip_b_part(MpegEncContext *s, int cbp)
     576             : {
     577           0 :     LOCAL_ALIGNED_32(int16_t, dblock, [64]);
     578             :     int i, mbi;
     579             :     int bli[6];
     580             : 
     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)
     583             :      */
     584           0 :     mbi = s->mb_intra;
     585           0 :     memcpy(bli, s->block_last_index, sizeof(bli));
     586           0 :     s->mb_intra = 0;
     587           0 :     for (i = 0; i < 6; i++) {
     588           0 :         if (h263_decode_block(s, dblock, i, cbp&32) < 0)
     589           0 :             return -1;
     590           0 :         cbp+=cbp;
     591             :     }
     592           0 :     s->mb_intra = mbi;
     593           0 :     memcpy(s->block_last_index, bli, sizeof(bli));
     594           0 :     return 0;
     595             : }
     596             : 
     597           0 : static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
     598             : {
     599           0 :     int c, mv = 1;
     600             : 
     601           0 :     if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
     602           0 :         c = get_bits1(gb);
     603           0 :         if (pb_frame == 2 && c)
     604           0 :             mv = !get_bits1(gb);
     605             :     } else { // h.263 Annex M improved PB-frame
     606           0 :         mv = get_unary(gb, 0, 4) + 1;
     607           0 :         c = mv & 1;
     608           0 :         mv = !!(mv & 2);
     609             :     }
     610           0 :     if(c)
     611           0 :         *cbpb = get_bits(gb, 6);
     612           0 :     return mv;
     613             : }
     614             : 
     615             : #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
     616             : #define tab_bias (tab_size / 2)
     617       15407 : static inline void set_one_direct_mv(MpegEncContext *s, Picture *p, int i)
     618             : {
     619       15407 :     int xy           = s->block_index[i];
     620       15407 :     uint16_t time_pp = s->pp_time;
     621       15407 :     uint16_t time_pb = s->pb_time;
     622             :     int p_mx, p_my;
     623             : 
     624       15407 :     p_mx = p->motion_val[0][xy][0];
     625       15407 :     if ((unsigned)(p_mx + tab_bias) < tab_size) {
     626       15407 :         s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias];
     627       15407 :         s->mv[1][i][0] = s->direct_scale_mv[1][p_mx + tab_bias];
     628             :     } else {
     629           0 :         s->mv[0][i][0] = p_mx * time_pb / time_pp;
     630           0 :         s->mv[1][i][0] = p_mx * (time_pb - time_pp) / time_pp;
     631             :     }
     632       15407 :     p_my = p->motion_val[0][xy][1];
     633       15407 :     if ((unsigned)(p_my + tab_bias) < tab_size) {
     634       15407 :         s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias];
     635       15407 :         s->mv[1][i][1] = s->direct_scale_mv[1][p_my + tab_bias];
     636             :     } else {
     637           0 :         s->mv[0][i][1] = p_my * time_pb / time_pp;
     638           0 :         s->mv[1][i][1] = p_my * (time_pb - time_pp) / time_pp;
     639             :     }
     640       15407 : }
     641             : 
     642             : /**
     643             :  * @return the mb_type
     644             :  */
     645       15266 : static int set_direct_mv(MpegEncContext *s)
     646             : {
     647       15266 :     const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
     648       15266 :     Picture *p = &s->next_picture;
     649       15266 :     int colocated_mb_type = p->mb_type[mb_index];
     650             :     int i;
     651             : 
     652       15266 :     if (s->codec_tag == AV_RL32("U263") && p->f->pict_type == AV_PICTURE_TYPE_I) {
     653           0 :         p = &s->last_picture;
     654           0 :         colocated_mb_type = p->mb_type[mb_index];
     655             :     }
     656             : 
     657       15266 :     if (IS_8X8(colocated_mb_type)) {
     658          47 :         s->mv_type = MV_TYPE_8X8;
     659         235 :         for (i = 0; i < 4; i++)
     660         188 :             set_one_direct_mv(s, p, i);
     661          47 :         return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
     662             :     } else {
     663       15219 :         set_one_direct_mv(s, p, 0);
     664       15219 :         s->mv[0][1][0] =
     665       15219 :         s->mv[0][2][0] =
     666       15219 :         s->mv[0][3][0] = s->mv[0][0][0];
     667       15219 :         s->mv[0][1][1] =
     668       15219 :         s->mv[0][2][1] =
     669       15219 :         s->mv[0][3][1] = s->mv[0][0][1];
     670       15219 :         s->mv[1][1][0] =
     671       15219 :         s->mv[1][2][0] =
     672       15219 :         s->mv[1][3][0] = s->mv[1][0][0];
     673       15219 :         s->mv[1][1][1] =
     674       15219 :         s->mv[1][2][1] =
     675       15219 :         s->mv[1][3][1] = s->mv[1][0][1];
     676       15219 :         s->mv_type = MV_TYPE_8X8;
     677             :         // Note see prev line
     678       15219 :         return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1;
     679             :     }
     680             : }
     681             : 
     682      480738 : int ff_h263_decode_mb(MpegEncContext *s,
     683             :                       int16_t block[6][64])
     684             : {
     685             :     int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
     686             :     int16_t *mot_val;
     687      480738 :     const int xy= s->mb_x + s->mb_y * s->mb_stride;
     688      480738 :     int cbpb = 0, pb_mv_count = 0;
     689             : 
     690             :     av_assert2(!s->h263_pred);
     691             : 
     692      480738 :     if (s->pict_type == AV_PICTURE_TYPE_P) {
     693             :         do{
     694      406113 :             if (get_bits1(&s->gb)) {
     695             :                 /* skip mb */
     696        9321 :                 s->mb_intra = 0;
     697       65247 :                 for(i=0;i<6;i++)
     698       55926 :                     s->block_last_index[i] = -1;
     699        9321 :                 s->mv_dir = MV_DIR_FORWARD;
     700        9321 :                 s->mv_type = MV_TYPE_16X16;
     701        9321 :                 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
     702        9321 :                 s->mv[0][0][0] = 0;
     703        9321 :                 s->mv[0][0][1] = 0;
     704        9321 :                 s->mb_skipped = !(s->obmc | s->loop_filter);
     705        9321 :                 goto end;
     706             :             }
     707      396792 :             cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
     708      396792 :             if (cbpc < 0){
     709           0 :                 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
     710           0 :                 return SLICE_ERROR;
     711             :             }
     712      396792 :         }while(cbpc == 20);
     713             : 
     714      396792 :         s->bdsp.clear_blocks(s->block[0]);
     715             : 
     716      396792 :         dquant = cbpc & 8;
     717      396792 :         s->mb_intra = ((cbpc & 4) != 0);
     718      396792 :         if (s->mb_intra) goto intra;
     719             : 
     720      380054 :         if(s->pb_frame && get_bits1(&s->gb))
     721           0 :             pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
     722      380054 :         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     723             : 
     724      380054 :         if (cbpy < 0) {
     725           0 :             av_log(s->avctx, AV_LOG_ERROR, "cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
     726           0 :             return SLICE_ERROR;
     727             :         }
     728             : 
     729      380054 :         if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
     730      342210 :             cbpy ^= 0xF;
     731             : 
     732      380054 :         cbp = (cbpc & 3) | (cbpy << 2);
     733      380054 :         if (dquant) {
     734           0 :             h263_decode_dquant(s);
     735             :         }
     736             : 
     737      380054 :         s->mv_dir = MV_DIR_FORWARD;
     738      380054 :         if ((cbpc & 16) == 0) {
     739      379011 :             s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
     740             :             /* 16x16 motion prediction */
     741      379011 :             s->mv_type = MV_TYPE_16X16;
     742      379011 :             ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
     743      379011 :             if (s->umvplus)
     744       51649 :                mx = h263p_decode_umotion(s, pred_x);
     745             :             else
     746      327362 :                mx = ff_h263_decode_motion(s, pred_x, 1);
     747             : 
     748      379011 :             if (mx >= 0xffff)
     749           0 :                 return SLICE_ERROR;
     750             : 
     751      379011 :             if (s->umvplus)
     752       51649 :                my = h263p_decode_umotion(s, pred_y);
     753             :             else
     754      327362 :                my = ff_h263_decode_motion(s, pred_y, 1);
     755             : 
     756      379011 :             if (my >= 0xffff)
     757           0 :                 return SLICE_ERROR;
     758      379011 :             s->mv[0][0][0] = mx;
     759      379011 :             s->mv[0][0][1] = my;
     760             : 
     761      379011 :             if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
     762         340 :                skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
     763             :         } else {
     764        1043 :             s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
     765        1043 :             s->mv_type = MV_TYPE_8X8;
     766        5215 :             for(i=0;i<4;i++) {
     767        4172 :                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
     768        4172 :                 if (s->umvplus)
     769           0 :                     mx = h263p_decode_umotion(s, pred_x);
     770             :                 else
     771        4172 :                     mx = ff_h263_decode_motion(s, pred_x, 1);
     772        4172 :                 if (mx >= 0xffff)
     773           0 :                     return SLICE_ERROR;
     774             : 
     775        4172 :                 if (s->umvplus)
     776           0 :                     my = h263p_decode_umotion(s, pred_y);
     777             :                 else
     778        4172 :                     my = ff_h263_decode_motion(s, pred_y, 1);
     779        4172 :                 if (my >= 0xffff)
     780           0 :                     return SLICE_ERROR;
     781        4172 :                 s->mv[0][i][0] = mx;
     782        4172 :                 s->mv[0][i][1] = my;
     783        4172 :                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
     784           0 :                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
     785        4172 :                 mot_val[0] = mx;
     786        4172 :                 mot_val[1] = my;
     787             :             }
     788             :         }
     789       74625 :     } else if(s->pict_type==AV_PICTURE_TYPE_B) {
     790             :         int mb_type;
     791       21600 :         const int stride= s->b8_stride;
     792       21600 :         int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
     793       21600 :         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;
     795             : 
     796             :         //FIXME ugly
     797       43200 :         mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
     798       64800 :         mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
     799       64800 :         mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
     800       43200 :         mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
     801             : 
     802             :         do{
     803       21600 :             mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
     804       21600 :             if (mb_type < 0){
     805           0 :                 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
     806           0 :                 return SLICE_ERROR;
     807             :             }
     808             : 
     809       21600 :             mb_type= h263_mb_type_b_map[ mb_type ];
     810       21600 :         }while(!mb_type);
     811             : 
     812       21600 :         s->mb_intra = IS_INTRA(mb_type);
     813       21600 :         if(HAS_CBP(mb_type)){
     814       11402 :             s->bdsp.clear_blocks(s->block[0]);
     815       11402 :             cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
     816       11402 :             if(s->mb_intra){
     817          46 :                 dquant = IS_QUANT(mb_type);
     818          46 :                 goto intra;
     819             :             }
     820             : 
     821       11356 :             cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     822             : 
     823       11356 :             if (cbpy < 0){
     824           0 :                 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
     825           0 :                 return SLICE_ERROR;
     826             :             }
     827             : 
     828       11356 :             if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
     829       11356 :                 cbpy ^= 0xF;
     830             : 
     831       11356 :             cbp = (cbpc & 3) | (cbpy << 2);
     832             :         }else
     833       10198 :             cbp=0;
     834             : 
     835             :         av_assert2(!s->mb_intra);
     836             : 
     837       21554 :         if(IS_QUANT(mb_type)){
     838           0 :             h263_decode_dquant(s);
     839             :         }
     840             : 
     841       21554 :         if(IS_DIRECT(mb_type)){
     842       15266 :             s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
     843       15266 :             mb_type |= set_direct_mv(s);
     844             :         }else{
     845        6288 :             s->mv_dir = 0;
     846        6288 :             s->mv_type= MV_TYPE_16X16;
     847             : //FIXME UMV
     848             : 
     849        6288 :             if(USES_LIST(mb_type, 0)){
     850        2594 :                 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
     851        2594 :                 s->mv_dir = MV_DIR_FORWARD;
     852             : 
     853        2594 :                 if (s->umvplus)
     854           0 :                     mx = h263p_decode_umotion(s, pred_x);
     855             :                 else
     856        2594 :                     mx = ff_h263_decode_motion(s, pred_x, 1);
     857        2594 :                 if (mx >= 0xffff)
     858           0 :                     return SLICE_ERROR;
     859             : 
     860        2594 :                 if (s->umvplus)
     861           0 :                     my = h263p_decode_umotion(s, pred_y);
     862             :                 else
     863        2594 :                     my = ff_h263_decode_motion(s, pred_y, 1);
     864        2594 :                 if (my >= 0xffff)
     865           0 :                     return SLICE_ERROR;
     866             : 
     867        2594 :                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
     868           0 :                     skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
     869             : 
     870        2594 :                 s->mv[0][0][0] = mx;
     871        2594 :                 s->mv[0][0][1] = my;
     872        2594 :                 mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
     873        2594 :                 mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
     874             :             }
     875             : 
     876        6288 :             if(USES_LIST(mb_type, 1)){
     877        3694 :                 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &pred_x, &pred_y);
     878        3694 :                 s->mv_dir |= MV_DIR_BACKWARD;
     879             : 
     880        3694 :                 if (s->umvplus)
     881           0 :                     mx = h263p_decode_umotion(s, pred_x);
     882             :                 else
     883        3694 :                     mx = ff_h263_decode_motion(s, pred_x, 1);
     884        3694 :                 if (mx >= 0xffff)
     885           0 :                     return SLICE_ERROR;
     886             : 
     887        3694 :                 if (s->umvplus)
     888           0 :                     my = h263p_decode_umotion(s, pred_y);
     889             :                 else
     890        3694 :                     my = ff_h263_decode_motion(s, pred_y, 1);
     891        3694 :                 if (my >= 0xffff)
     892           0 :                     return SLICE_ERROR;
     893             : 
     894        3694 :                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
     895           0 :                     skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
     896             : 
     897        3694 :                 s->mv[1][0][0] = mx;
     898        3694 :                 s->mv[1][0][1] = my;
     899        3694 :                 mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
     900        3694 :                 mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
     901             :             }
     902             :         }
     903             : 
     904       21554 :         s->current_picture.mb_type[xy] = mb_type;
     905             :     } else { /* I-Frame */
     906             :         do{
     907       53025 :             cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
     908       53025 :             if (cbpc < 0){
     909           0 :                 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
     910           0 :                 return SLICE_ERROR;
     911             :             }
     912       53025 :         }while(cbpc == 8);
     913             : 
     914       53025 :         s->bdsp.clear_blocks(s->block[0]);
     915             : 
     916       53025 :         dquant = cbpc & 4;
     917       53025 :         s->mb_intra = 1;
     918       69809 : intra:
     919       69809 :         s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
     920       69809 :         if (s->h263_aic) {
     921       15183 :             s->ac_pred = get_bits1(&s->gb);
     922       15183 :             if(s->ac_pred){
     923         739 :                 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
     924             : 
     925         739 :                 s->h263_aic_dir = get_bits1(&s->gb);
     926             :             }
     927             :         }else
     928       54626 :             s->ac_pred = 0;
     929             : 
     930       69809 :         if(s->pb_frame && get_bits1(&s->gb))
     931           0 :             pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
     932       69809 :         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     933       69809 :         if(cbpy<0){
     934           0 :             av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
     935           0 :             return SLICE_ERROR;
     936             :         }
     937       69809 :         cbp = (cbpc & 3) | (cbpy << 2);
     938       69809 :         if (dquant) {
     939           0 :             h263_decode_dquant(s);
     940             :         }
     941             : 
     942       69809 :         pb_mv_count += !!s->pb_frame;
     943             :     }
     944             : 
     945      942834 :     while(pb_mv_count--){
     946           0 :         ff_h263_decode_motion(s, 0, 1);
     947           0 :         ff_h263_decode_motion(s, 0, 1);
     948             :     }
     949             : 
     950             :     /* decode each block */
     951     3299919 :     for (i = 0; i < 6; i++) {
     952     2828502 :         if (h263_decode_block(s, block[i], i, cbp&32) < 0)
     953           0 :             return -1;
     954     2828502 :         cbp+=cbp;
     955             :     }
     956             : 
     957      471417 :     if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
     958           0 :         return -1;
     959      530001 :     if(s->obmc && !s->mb_intra){
     960      150160 :         if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
     961       48550 :             preview_obmc(s);
     962             :     }
     963      903605 : end:
     964             : 
     965      480738 :     if (get_bits_left(&s->gb) < 0)
     966           0 :         return AVERROR_INVALIDDATA;
     967             : 
     968             :         /* per-MB end of slice check */
     969             :     {
     970      480738 :         int v= show_bits(&s->gb, 16);
     971             : 
     972      480738 :         if (get_bits_left(&s->gb) < 16) {
     973        1716 :             v >>= 16 - get_bits_left(&s->gb);
     974             :         }
     975             : 
     976      480738 :         if(v==0)
     977        3842 :             return SLICE_END;
     978             :     }
     979             : 
     980      476896 :     return SLICE_OK;
     981             : }
     982             : 
     983             : /* Most is hardcoded; should extend to handle all H.263 streams. */
     984         461 : int ff_h263_decode_picture_header(MpegEncContext *s)
     985             : {
     986             :     int format, width, height, i, ret;
     987             :     uint32_t startcode;
     988             : 
     989         461 :     align_get_bits(&s->gb);
     990             : 
     991         461 :     if (show_bits(&s->gb, 2) == 2 && s->avctx->frame_number == 0) {
     992           0 :          av_log(s->avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
     993             :     }
     994             : 
     995         461 :     startcode= get_bits(&s->gb, 22-8);
     996             : 
     997         461 :     for(i= get_bits_left(&s->gb); i>24; i-=8) {
     998         461 :         startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
     999             : 
    1000         461 :         if(startcode == 0x20)
    1001         461 :             break;
    1002             :     }
    1003             : 
    1004         461 :     if (startcode != 0x20) {
    1005           0 :         av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
    1006           0 :         return -1;
    1007             :     }
    1008             :     /* temporal reference */
    1009         461 :     i = get_bits(&s->gb, 8); /* picture timestamp */
    1010             : 
    1011         461 :     i -= (i - (s->picture_number & 0xFF) + 128) & ~0xFF;
    1012             : 
    1013         461 :     s->picture_number= (s->picture_number&~0xFF) + i;
    1014             : 
    1015             :     /* PTYPE starts here */
    1016         461 :     if (check_marker(s->avctx, &s->gb, "in PTYPE") != 1) {
    1017           0 :         return -1;
    1018             :     }
    1019         461 :     if (get_bits1(&s->gb) != 0) {
    1020           0 :         av_log(s->avctx, AV_LOG_ERROR, "Bad H.263 id\n");
    1021           0 :         return -1;      /* H.263 id */
    1022             :     }
    1023         461 :     skip_bits1(&s->gb);         /* split screen off */
    1024         461 :     skip_bits1(&s->gb);         /* camera  off */
    1025         461 :     skip_bits1(&s->gb);         /* freeze picture release off */
    1026             : 
    1027         461 :     format = get_bits(&s->gb, 3);
    1028             :     /*
    1029             :         0    forbidden
    1030             :         1    sub-QCIF
    1031             :         10   QCIF
    1032             :         7       extended PTYPE (PLUSPTYPE)
    1033             :     */
    1034             : 
    1035         461 :     if (format != 7 && format != 6) {
    1036         307 :         s->h263_plus = 0;
    1037             :         /* H.263v1 */
    1038         307 :         width = ff_h263_format[format][0];
    1039         307 :         height = ff_h263_format[format][1];
    1040         307 :         if (!width)
    1041           0 :             return -1;
    1042             : 
    1043         307 :         s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
    1044             : 
    1045         307 :         s->h263_long_vectors = get_bits1(&s->gb);
    1046             : 
    1047         307 :         if (get_bits1(&s->gb) != 0) {
    1048           0 :             av_log(s->avctx, AV_LOG_ERROR, "H.263 SAC not supported\n");
    1049           0 :             return -1; /* SAC: off */
    1050             :         }
    1051         307 :         s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
    1052         307 :         s->unrestricted_mv = s->h263_long_vectors || s->obmc;
    1053             : 
    1054         307 :         s->pb_frame = get_bits1(&s->gb);
    1055         307 :         s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
    1056         307 :         skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
    1057             : 
    1058         307 :         s->width = width;
    1059         307 :         s->height = height;
    1060         307 :         s->avctx->sample_aspect_ratio= (AVRational){12,11};
    1061         307 :         s->avctx->framerate = (AVRational){ 30000, 1001 };
    1062             :     } else {
    1063             :         int ufep;
    1064             : 
    1065             :         /* H.263v2 */
    1066         154 :         s->h263_plus = 1;
    1067         154 :         ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
    1068             : 
    1069             :         /* ufep other than 0 and 1 are reserved */
    1070         154 :         if (ufep == 1) {
    1071             :             /* OPPTYPE */
    1072         154 :             format = get_bits(&s->gb, 3);
    1073             :             ff_dlog(s->avctx, "ufep=1, format: %d\n", format);
    1074         154 :             s->custom_pcf= get_bits1(&s->gb);
    1075         154 :             s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
    1076         154 :             if (get_bits1(&s->gb) != 0) {
    1077           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
    1078             :             }
    1079         154 :             s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
    1080         154 :             s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
    1081         154 :             s->loop_filter= get_bits1(&s->gb);
    1082         154 :             s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
    1083         154 :             if(s->avctx->lowres)
    1084           0 :                 s->loop_filter = 0;
    1085             : 
    1086         154 :             s->h263_slice_structured= get_bits1(&s->gb);
    1087         154 :             if (get_bits1(&s->gb) != 0) {
    1088           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
    1089             :             }
    1090         154 :             if (get_bits1(&s->gb) != 0) {
    1091           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
    1092             :             }
    1093         154 :             s->alt_inter_vlc= get_bits1(&s->gb);
    1094         154 :             s->modified_quant= get_bits1(&s->gb);
    1095         154 :             if(s->modified_quant)
    1096         154 :                 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
    1097             : 
    1098         154 :             skip_bits(&s->gb, 1); /* Prevent start code emulation */
    1099             : 
    1100         154 :             skip_bits(&s->gb, 3); /* Reserved */
    1101           0 :         } else if (ufep != 0) {
    1102           0 :             av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
    1103           0 :             return -1;
    1104             :         }
    1105             : 
    1106             :         /* MPPTYPE */
    1107         154 :         s->pict_type = get_bits(&s->gb, 3);
    1108         154 :         switch(s->pict_type){
    1109          19 :         case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
    1110         135 :         case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
    1111           0 :         case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
    1112           0 :         case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
    1113           0 :         case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
    1114           0 :         default:
    1115           0 :             return -1;
    1116             :         }
    1117         154 :         skip_bits(&s->gb, 2);
    1118         154 :         s->no_rounding = get_bits1(&s->gb);
    1119         154 :         skip_bits(&s->gb, 4);
    1120             : 
    1121             :         /* Get the picture dimensions */
    1122         154 :         if (ufep) {
    1123         154 :             if (format == 6) {
    1124             :                 /* Custom Picture Format (CPFMT) */
    1125           0 :                 s->aspect_ratio_info = get_bits(&s->gb, 4);
    1126             :                 ff_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
    1127             :                 /* aspect ratios:
    1128             :                 0 - forbidden
    1129             :                 1 - 1:1
    1130             :                 2 - 12:11 (CIF 4:3)
    1131             :                 3 - 10:11 (525-type 4:3)
    1132             :                 4 - 16:11 (CIF 16:9)
    1133             :                 5 - 40:33 (525-type 16:9)
    1134             :                 6-14 - reserved
    1135             :                 */
    1136           0 :                 width = (get_bits(&s->gb, 9) + 1) * 4;
    1137           0 :                 check_marker(s->avctx, &s->gb, "in dimensions");
    1138           0 :                 height = get_bits(&s->gb, 9) * 4;
    1139             :                 ff_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
    1140           0 :                 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
    1141             :                     /* expected dimensions */
    1142           0 :                     s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
    1143           0 :                     s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
    1144             :                 }else{
    1145           0 :                     s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
    1146             :                 }
    1147             :             } else {
    1148         154 :                 width = ff_h263_format[format][0];
    1149         154 :                 height = ff_h263_format[format][1];
    1150         154 :                 s->avctx->sample_aspect_ratio= (AVRational){12,11};
    1151             :             }
    1152         154 :             s->avctx->sample_aspect_ratio.den <<= s->ehc_mode;
    1153         154 :             if ((width == 0) || (height == 0))
    1154           0 :                 return -1;
    1155         154 :             s->width = width;
    1156         154 :             s->height = height;
    1157             : 
    1158         154 :             if(s->custom_pcf){
    1159             :                 int gcd;
    1160         154 :                 s->avctx->framerate.num  = 1800000;
    1161         154 :                 s->avctx->framerate.den  = 1000 + get_bits1(&s->gb);
    1162         154 :                 s->avctx->framerate.den *= get_bits(&s->gb, 7);
    1163         154 :                 if(s->avctx->framerate.den == 0){
    1164           0 :                     av_log(s, AV_LOG_ERROR, "zero framerate\n");
    1165           0 :                     return -1;
    1166             :                 }
    1167         154 :                 gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
    1168         154 :                 s->avctx->framerate.den /= gcd;
    1169         154 :                 s->avctx->framerate.num /= gcd;
    1170             :             }else{
    1171           0 :                 s->avctx->framerate = (AVRational){ 30000, 1001 };
    1172             :             }
    1173             :         }
    1174             : 
    1175         154 :         if(s->custom_pcf){
    1176         154 :             skip_bits(&s->gb, 2); //extended Temporal reference
    1177             :         }
    1178             : 
    1179         154 :         if (ufep) {
    1180         154 :             if (s->umvplus) {
    1181         154 :                 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
    1182         154 :                     skip_bits1(&s->gb);
    1183             :             }
    1184         154 :             if(s->h263_slice_structured){
    1185           0 :                 if (get_bits1(&s->gb) != 0) {
    1186           0 :                     av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
    1187             :                 }
    1188           0 :                 if (get_bits1(&s->gb) != 0) {
    1189           0 :                     av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
    1190             :                 }
    1191             :             }
    1192         154 :             if (s->pict_type == AV_PICTURE_TYPE_B) {
    1193           0 :                 skip_bits(&s->gb, 4); //ELNUM
    1194           0 :                 if (ufep == 1) {
    1195           0 :                     skip_bits(&s->gb, 4); // RLNUM
    1196             :                 }
    1197             :             }
    1198             :         }
    1199             : 
    1200         154 :         s->qscale = get_bits(&s->gb, 5);
    1201             :     }
    1202             : 
    1203         461 :     if ((ret = av_image_check_size(s->width, s->height, 0, s)) < 0)
    1204           0 :         return ret;
    1205             : 
    1206         461 :     s->mb_width = (s->width  + 15) / 16;
    1207         461 :     s->mb_height = (s->height  + 15) / 16;
    1208         461 :     s->mb_num = s->mb_width * s->mb_height;
    1209             : 
    1210         461 :     if (s->pb_frame) {
    1211           0 :         skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
    1212           0 :         if (s->custom_pcf)
    1213           0 :             skip_bits(&s->gb, 2); //extended Temporal reference
    1214           0 :         skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
    1215             :     }
    1216             : 
    1217         461 :     if (s->pict_type!=AV_PICTURE_TYPE_B) {
    1218         461 :         s->time            = s->picture_number;
    1219         461 :         s->pp_time         = s->time - s->last_non_b_time;
    1220         461 :         s->last_non_b_time = s->time;
    1221             :     }else{
    1222           0 :         s->time    = s->picture_number;
    1223           0 :         s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
    1224           0 :         if (s->pp_time <=s->pb_time ||
    1225           0 :             s->pp_time <= s->pp_time - s->pb_time ||
    1226           0 :             s->pp_time <= 0){
    1227           0 :             s->pp_time = 2;
    1228           0 :             s->pb_time = 1;
    1229             :         }
    1230           0 :         ff_mpeg4_init_direct_mv(s);
    1231             :     }
    1232             : 
    1233             :     /* PEI */
    1234         461 :     if (skip_1stop_8data_bits(&s->gb) < 0)
    1235           0 :         return AVERROR_INVALIDDATA;
    1236             : 
    1237         461 :     if(s->h263_slice_structured){
    1238           0 :         if (check_marker(s->avctx, &s->gb, "SEPB1") != 1) {
    1239           0 :             return -1;
    1240             :         }
    1241             : 
    1242           0 :         ff_h263_decode_mba(s);
    1243             : 
    1244           0 :         if (check_marker(s->avctx, &s->gb, "SEPB2") != 1) {
    1245           0 :             return -1;
    1246             :         }
    1247             :     }
    1248         461 :     s->f_code = 1;
    1249             : 
    1250         461 :     if (s->pict_type == AV_PICTURE_TYPE_B)
    1251           0 :         s->low_delay = 0;
    1252             : 
    1253         461 :     if(s->h263_aic){
    1254         154 :          s->y_dc_scale_table=
    1255         154 :          s->c_dc_scale_table= ff_aic_dc_scale_table;
    1256             :     }else{
    1257         307 :         s->y_dc_scale_table=
    1258         307 :         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
    1259             :     }
    1260             : 
    1261         461 :         ff_h263_show_pict_info(s);
    1262         461 :     if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
    1263             :         int i,j;
    1264           0 :         for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
    1265           0 :         av_log(s->avctx, AV_LOG_DEBUG, "\n");
    1266           0 :         for(i=0; i<13; i++){
    1267           0 :             for(j=0; j<3; j++){
    1268           0 :                 int v= get_bits(&s->gb, 8);
    1269           0 :                 v |= get_sbits(&s->gb, 8)<<8;
    1270           0 :                 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
    1271             :             }
    1272           0 :             av_log(s->avctx, AV_LOG_DEBUG, "\n");
    1273             :         }
    1274           0 :         for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
    1275             :     }
    1276             : 
    1277         461 :     return 0;
    1278             : }

Generated by: LCOV version 1.13