• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/ituh263dec.c

Go to the documentation of this file.
00001 /*
00002  * ITU H263 bitstream decoder
00003  * Copyright (c) 2000,2001 Fabrice Bellard
00004  * H263+ support.
00005  * Copyright (c) 2001 Juan J. Sierralta P
00006  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00030 //#define DEBUG
00031 #include <limits.h>
00032 
00033 #include "dsputil.h"
00034 #include "avcodec.h"
00035 #include "mpegvideo.h"
00036 #include "h263.h"
00037 #include "mathops.h"
00038 #include "unary.h"
00039 #include "flv.h"
00040 #include "mpeg4video.h"
00041 
00042 //#undef NDEBUG
00043 //#include <assert.h>
00044 
00045 // The defines below define the number of bits that are read at once for
00046 // reading vlc values. Changing these may improve speed and data cache needs
00047 // be aware though that decreasing them may need the number of stages that is
00048 // passed to get_vlc* to be increased.
00049 #define MV_VLC_BITS 9
00050 #define H263_MBTYPE_B_VLC_BITS 6
00051 #define CBPC_B_VLC_BITS 3
00052 
00053 static const int h263_mb_type_b_map[15]= {
00054     MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
00055     MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
00056     MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
00057                       MB_TYPE_L0                                 | MB_TYPE_16x16,
00058                       MB_TYPE_L0   | MB_TYPE_CBP                 | MB_TYPE_16x16,
00059                       MB_TYPE_L0   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00060                       MB_TYPE_L1                                 | MB_TYPE_16x16,
00061                       MB_TYPE_L1   | MB_TYPE_CBP                 | MB_TYPE_16x16,
00062                       MB_TYPE_L1   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00063                       MB_TYPE_L0L1                               | MB_TYPE_16x16,
00064                       MB_TYPE_L0L1 | MB_TYPE_CBP                 | MB_TYPE_16x16,
00065                       MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00066     0, //stuffing
00067     MB_TYPE_INTRA4x4                | MB_TYPE_CBP,
00068     MB_TYPE_INTRA4x4                | MB_TYPE_CBP | MB_TYPE_QUANT,
00069 };
00070 
00071 void ff_h263_show_pict_info(MpegEncContext *s){
00072     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00073     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",
00074          s->qscale, av_get_pict_type_char(s->pict_type),
00075          s->gb.size_in_bits, 1-s->no_rounding,
00076          s->obmc ? " AP" : "",
00077          s->umvplus ? " UMV" : "",
00078          s->h263_long_vectors ? " LONG" : "",
00079          s->h263_plus ? " +" : "",
00080          s->h263_aic ? " AIC" : "",
00081          s->alt_inter_vlc ? " AIV" : "",
00082          s->modified_quant ? " MQ" : "",
00083          s->loop_filter ? " LOOP" : "",
00084          s->h263_slice_structured ? " SS" : "",
00085          s->avctx->time_base.den, s->avctx->time_base.num
00086     );
00087     }
00088 }
00089 
00090 /***********************************************/
00091 /* decoding */
00092 
00093 VLC ff_h263_intra_MCBPC_vlc;
00094 VLC ff_h263_inter_MCBPC_vlc;
00095 VLC ff_h263_cbpy_vlc;
00096 static VLC mv_vlc;
00097 static VLC h263_mbtype_b_vlc;
00098 static VLC cbpc_b_vlc;
00099 
00100 /* init vlcs */
00101 
00102 /* XXX: find a better solution to handle static init */
00103 void h263_decode_init_vlc(MpegEncContext *s)
00104 {
00105     static int done = 0;
00106 
00107     if (!done) {
00108         done = 1;
00109 
00110         INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
00111                  ff_h263_intra_MCBPC_bits, 1, 1,
00112                  ff_h263_intra_MCBPC_code, 1, 1, 72);
00113         INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
00114                  ff_h263_inter_MCBPC_bits, 1, 1,
00115                  ff_h263_inter_MCBPC_code, 1, 1, 198);
00116         INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
00117                  &ff_h263_cbpy_tab[0][1], 2, 1,
00118                  &ff_h263_cbpy_tab[0][0], 2, 1, 64);
00119         INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
00120                  &mvtab[0][1], 2, 1,
00121                  &mvtab[0][0], 2, 1, 538);
00122         init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
00123         init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]);
00124         INIT_VLC_RL(ff_h263_rl_inter, 554);
00125         INIT_VLC_RL(rl_intra_aic, 554);
00126         INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
00127                  &h263_mbtype_b_tab[0][1], 2, 1,
00128                  &h263_mbtype_b_tab[0][0], 2, 1, 80);
00129         INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
00130                  &cbpc_b_tab[0][1], 2, 1,
00131                  &cbpc_b_tab[0][0], 2, 1, 8);
00132     }
00133 }
00134 
00135 int ff_h263_decode_mba(MpegEncContext *s)
00136 {
00137     int i, mb_pos;
00138 
00139     for(i=0; i<6; i++){
00140         if(s->mb_num-1 <= ff_mba_max[i]) break;
00141     }
00142     mb_pos= get_bits(&s->gb, ff_mba_length[i]);
00143     s->mb_x= mb_pos % s->mb_width;
00144     s->mb_y= mb_pos / s->mb_width;
00145 
00146     return mb_pos;
00147 }
00148 
00153 static int h263_decode_gob_header(MpegEncContext *s)
00154 {
00155     unsigned int val, gfid, gob_number;
00156     int left;
00157 
00158     /* Check for GOB Start Code */
00159     val = show_bits(&s->gb, 16);
00160     if(val)
00161         return -1;
00162 
00163         /* We have a GBSC probably with GSTUFF */
00164     skip_bits(&s->gb, 16); /* Drop the zeros */
00165     left= get_bits_left(&s->gb);
00166     //MN: we must check the bits left or we might end in a infinite loop (or segfault)
00167     for(;left>13; left--){
00168         if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
00169     }
00170     if(left<=13)
00171         return -1;
00172 
00173     if(s->h263_slice_structured){
00174         if(get_bits1(&s->gb)==0)
00175             return -1;
00176 
00177         ff_h263_decode_mba(s);
00178 
00179         if(s->mb_num > 1583)
00180             if(get_bits1(&s->gb)==0)
00181                 return -1;
00182 
00183         s->qscale = get_bits(&s->gb, 5); /* SQUANT */
00184         if(get_bits1(&s->gb)==0)
00185             return -1;
00186         gfid = get_bits(&s->gb, 2); /* GFID */
00187     }else{
00188         gob_number = get_bits(&s->gb, 5); /* GN */
00189         s->mb_x= 0;
00190         s->mb_y= s->gob_index* gob_number;
00191         gfid = get_bits(&s->gb, 2); /* GFID */
00192         s->qscale = get_bits(&s->gb, 5); /* GQUANT */
00193     }
00194 
00195     if(s->mb_y >= s->mb_height)
00196         return -1;
00197 
00198     if(s->qscale==0)
00199         return -1;
00200 
00201     return 0;
00202 }
00203 
00210 const uint8_t *ff_h263_find_resync_marker(const uint8_t *restrict p, const uint8_t * restrict end)
00211 {
00212     assert(p < end);
00213 
00214     end-=2;
00215     p++;
00216     for(;p<end; p+=2){
00217         if(!*p){
00218             if     (!p[-1] && p[1]) return p - 1;
00219             else if(!p[ 1] && p[2]) return p;
00220         }
00221     }
00222     return end+2;
00223 }
00224 
00229 int ff_h263_resync(MpegEncContext *s){
00230     int left, pos, ret;
00231 
00232     if(s->codec_id==CODEC_ID_MPEG4){
00233         skip_bits1(&s->gb);
00234         align_get_bits(&s->gb);
00235     }
00236 
00237     if(show_bits(&s->gb, 16)==0){
00238         pos= get_bits_count(&s->gb);
00239         if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
00240             ret= mpeg4_decode_video_packet_header(s);
00241         else
00242             ret= h263_decode_gob_header(s);
00243         if(ret>=0)
00244             return pos;
00245     }
00246     //OK, it's not where it is supposed to be ...
00247     s->gb= s->last_resync_gb;
00248     align_get_bits(&s->gb);
00249     left= get_bits_left(&s->gb);
00250 
00251     for(;left>16+1+5+5; left-=8){
00252         if(show_bits(&s->gb, 16)==0){
00253             GetBitContext bak= s->gb;
00254 
00255             pos= get_bits_count(&s->gb);
00256             if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
00257                 ret= mpeg4_decode_video_packet_header(s);
00258             else
00259                 ret= h263_decode_gob_header(s);
00260             if(ret>=0)
00261                 return pos;
00262 
00263             s->gb= bak;
00264         }
00265         skip_bits(&s->gb, 8);
00266     }
00267 
00268     return -1;
00269 }
00270 
00271 int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
00272 {
00273     int code, val, sign, shift, l;
00274     code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
00275 
00276     if (code == 0)
00277         return pred;
00278     if (code < 0)
00279         return 0xffff;
00280 
00281     sign = get_bits1(&s->gb);
00282     shift = f_code - 1;
00283     val = code;
00284     if (shift) {
00285         val = (val - 1) << shift;
00286         val |= get_bits(&s->gb, shift);
00287         val++;
00288     }
00289     if (sign)
00290         val = -val;
00291     val += pred;
00292 
00293     /* modulo decoding */
00294     if (!s->h263_long_vectors) {
00295         l = INT_BIT - 5 - f_code;
00296         val = (val<<l)>>l;
00297     } else {
00298         /* horrible h263 long vector mode */
00299         if (pred < -31 && val < -63)
00300             val += 64;
00301         if (pred > 32 && val > 63)
00302             val -= 64;
00303 
00304     }
00305     return val;
00306 }
00307 
00308 
00309 /* Decodes RVLC of H.263+ UMV */
00310 static int h263p_decode_umotion(MpegEncContext * s, int pred)
00311 {
00312    int code = 0, sign;
00313 
00314    if (get_bits1(&s->gb)) /* Motion difference = 0 */
00315       return pred;
00316 
00317    code = 2 + get_bits1(&s->gb);
00318 
00319    while (get_bits1(&s->gb))
00320    {
00321       code <<= 1;
00322       code += get_bits1(&s->gb);
00323    }
00324    sign = code & 1;
00325    code >>= 1;
00326 
00327    code = (sign) ? (pred - code) : (pred + code);
00328    dprintf(s->avctx,"H.263+ UMV Motion = %d\n", code);
00329    return code;
00330 
00331 }
00332 
00336 static void preview_obmc(MpegEncContext *s){
00337     GetBitContext gb= s->gb;
00338 
00339     int cbpc, i, pred_x, pred_y, mx, my;
00340     int16_t *mot_val;
00341     const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
00342     const int stride= s->b8_stride*2;
00343 
00344     for(i=0; i<4; i++)
00345         s->block_index[i]+= 2;
00346     for(i=4; i<6; i++)
00347         s->block_index[i]+= 1;
00348     s->mb_x++;
00349 
00350     assert(s->pict_type == FF_P_TYPE);
00351 
00352     do{
00353         if (get_bits1(&s->gb)) {
00354             /* skip mb */
00355             mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
00356             mot_val[0       ]= mot_val[2       ]=
00357             mot_val[0+stride]= mot_val[2+stride]= 0;
00358             mot_val[1       ]= mot_val[3       ]=
00359             mot_val[1+stride]= mot_val[3+stride]= 0;
00360 
00361             s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00362             goto end;
00363         }
00364         cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
00365     }while(cbpc == 20);
00366 
00367     if(cbpc & 4){
00368         s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
00369     }else{
00370         get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00371         if (cbpc & 8) {
00372             if(s->modified_quant){
00373                 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
00374                 else                  skip_bits(&s->gb, 5);
00375             }else
00376                 skip_bits(&s->gb, 2);
00377         }
00378 
00379         if ((cbpc & 16) == 0) {
00380                 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
00381                 /* 16x16 motion prediction */
00382                 mot_val= h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00383                 if (s->umvplus)
00384                    mx = h263p_decode_umotion(s, pred_x);
00385                 else
00386                    mx = h263_decode_motion(s, pred_x, 1);
00387 
00388                 if (s->umvplus)
00389                    my = h263p_decode_umotion(s, pred_y);
00390                 else
00391                    my = h263_decode_motion(s, pred_y, 1);
00392 
00393                 mot_val[0       ]= mot_val[2       ]=
00394                 mot_val[0+stride]= mot_val[2+stride]= mx;
00395                 mot_val[1       ]= mot_val[3       ]=
00396                 mot_val[1+stride]= mot_val[3+stride]= my;
00397         } else {
00398             s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
00399             for(i=0;i<4;i++) {
00400                 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00401                 if (s->umvplus)
00402                   mx = h263p_decode_umotion(s, pred_x);
00403                 else
00404                   mx = h263_decode_motion(s, pred_x, 1);
00405 
00406                 if (s->umvplus)
00407                   my = h263p_decode_umotion(s, pred_y);
00408                 else
00409                   my = h263_decode_motion(s, pred_y, 1);
00410                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00411                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
00412                 mot_val[0] = mx;
00413                 mot_val[1] = my;
00414             }
00415         }
00416     }
00417 end:
00418 
00419     for(i=0; i<4; i++)
00420         s->block_index[i]-= 2;
00421     for(i=4; i<6; i++)
00422         s->block_index[i]-= 1;
00423     s->mb_x--;
00424 
00425     s->gb= gb;
00426 }
00427 
00428 static void h263_decode_dquant(MpegEncContext *s){
00429     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
00430 
00431     if(s->modified_quant){
00432         if(get_bits1(&s->gb))
00433             s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
00434         else
00435             s->qscale= get_bits(&s->gb, 5);
00436     }else
00437         s->qscale += quant_tab[get_bits(&s->gb, 2)];
00438     ff_set_qscale(s, s->qscale);
00439 }
00440 
00441 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
00442                              int n, int coded)
00443 {
00444     int code, level, i, j, last, run;
00445     RLTable *rl = &ff_h263_rl_inter;
00446     const uint8_t *scan_table;
00447     GetBitContext gb= s->gb;
00448 
00449     scan_table = s->intra_scantable.permutated;
00450     if (s->h263_aic && s->mb_intra) {
00451         rl = &rl_intra_aic;
00452         i = 0;
00453         if (s->ac_pred) {
00454             if (s->h263_aic_dir)
00455                 scan_table = s->intra_v_scantable.permutated; /* left */
00456             else
00457                 scan_table = s->intra_h_scantable.permutated; /* top */
00458         }
00459     } else if (s->mb_intra) {
00460         /* DC coef */
00461         if(s->codec_id == CODEC_ID_RV10){
00462 #if CONFIG_RV10_DECODER
00463           if (s->rv10_version == 3 && s->pict_type == FF_I_TYPE) {
00464             int component, diff;
00465             component = (n <= 3 ? 0 : n - 4 + 1);
00466             level = s->last_dc[component];
00467             if (s->rv10_first_dc_coded[component]) {
00468                 diff = rv_decode_dc(s, n);
00469                 if (diff == 0xffff)
00470                     return -1;
00471                 level += diff;
00472                 level = level & 0xff; /* handle wrap round */
00473                 s->last_dc[component] = level;
00474             } else {
00475                 s->rv10_first_dc_coded[component] = 1;
00476             }
00477           } else {
00478                 level = get_bits(&s->gb, 8);
00479                 if (level == 255)
00480                     level = 128;
00481           }
00482 #endif
00483         }else{
00484             level = get_bits(&s->gb, 8);
00485             if((level&0x7F) == 0){
00486                 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
00487                 if(s->error_recognition >= FF_ER_COMPLIANT)
00488                     return -1;
00489             }
00490             if (level == 255)
00491                 level = 128;
00492         }
00493         block[0] = level;
00494         i = 1;
00495     } else {
00496         i = 0;
00497     }
00498     if (!coded) {
00499         if (s->mb_intra && s->h263_aic)
00500             goto not_coded;
00501         s->block_last_index[n] = i - 1;
00502         return 0;
00503     }
00504 retry:
00505     for(;;) {
00506         code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
00507         if (code < 0){
00508             av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
00509             return -1;
00510         }
00511         if (code == rl->n) {
00512             /* escape */
00513             if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
00514                 ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
00515             } else {
00516                 last = get_bits1(&s->gb);
00517                 run = get_bits(&s->gb, 6);
00518                 level = (int8_t)get_bits(&s->gb, 8);
00519                 if(level == -128){
00520                     if (s->codec_id == CODEC_ID_RV10) {
00521                         /* XXX: should patch encoder too */
00522                         level = get_sbits(&s->gb, 12);
00523                     }else{
00524                         level = get_bits(&s->gb, 5);
00525                         level |= get_sbits(&s->gb, 6)<<5;
00526                     }
00527                 }
00528             }
00529         } else {
00530             run = rl->table_run[code];
00531             level = rl->table_level[code];
00532             last = code >= rl->last;
00533             if (get_bits1(&s->gb))
00534                 level = -level;
00535         }
00536         i += run;
00537         if (i >= 64){
00538             if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
00539                 //Looks like a hack but no, it's the way it is supposed to work ...
00540                 rl = &rl_intra_aic;
00541                 i = 0;
00542                 s->gb= gb;
00543                 s->dsp.clear_block(block);
00544                 goto retry;
00545             }
00546             av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
00547             return -1;
00548         }
00549         j = scan_table[i];
00550         block[j] = level;
00551         if (last)
00552             break;
00553         i++;
00554     }
00555 not_coded:
00556     if (s->mb_intra && s->h263_aic) {
00557         h263_pred_acdc(s, block, n);
00558         i = 63;
00559     }
00560     s->block_last_index[n] = i;
00561     return 0;
00562 }
00563 
00564 static int h263_skip_b_part(MpegEncContext *s, int cbp)
00565 {
00566     LOCAL_ALIGNED_16(DCTELEM, dblock, [64]);
00567     int i, mbi;
00568 
00569     /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
00570      * but real value should be restored in order to be used later (in OBMC condition)
00571      */
00572     mbi = s->mb_intra;
00573     s->mb_intra = 0;
00574     for (i = 0; i < 6; i++) {
00575         if (h263_decode_block(s, dblock, i, cbp&32) < 0)
00576             return -1;
00577         cbp+=cbp;
00578     }
00579     s->mb_intra = mbi;
00580     return 0;
00581 }
00582 
00583 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
00584 {
00585     int c, mv = 1;
00586 
00587     if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
00588         c = get_bits1(gb);
00589         if (pb_frame == 2 && c)
00590             mv = !get_bits1(gb);
00591     } else { // h.263 Annex M improved PB-frame
00592         mv = get_unary(gb, 0, 4) + 1;
00593         c = mv & 1;
00594         mv = !!(mv & 2);
00595     }
00596     if(c)
00597         *cbpb = get_bits(gb, 6);
00598     return mv;
00599 }
00600 
00601 int ff_h263_decode_mb(MpegEncContext *s,
00602                       DCTELEM block[6][64])
00603 {
00604     int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
00605     int16_t *mot_val;
00606     const int xy= s->mb_x + s->mb_y * s->mb_stride;
00607     int cbpb = 0, pb_mv_count = 0;
00608 
00609     assert(!s->h263_pred);
00610 
00611     if (s->pict_type == FF_P_TYPE) {
00612         do{
00613             if (get_bits1(&s->gb)) {
00614                 /* skip mb */
00615                 s->mb_intra = 0;
00616                 for(i=0;i<6;i++)
00617                     s->block_last_index[i] = -1;
00618                 s->mv_dir = MV_DIR_FORWARD;
00619                 s->mv_type = MV_TYPE_16X16;
00620                 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00621                 s->mv[0][0][0] = 0;
00622                 s->mv[0][0][1] = 0;
00623                 s->mb_skipped = !(s->obmc | s->loop_filter);
00624                 goto end;
00625             }
00626             cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
00627             if (cbpc < 0){
00628                 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
00629                 return -1;
00630             }
00631         }while(cbpc == 20);
00632 
00633         s->dsp.clear_blocks(s->block[0]);
00634 
00635         dquant = cbpc & 8;
00636         s->mb_intra = ((cbpc & 4) != 0);
00637         if (s->mb_intra) goto intra;
00638 
00639         if(s->pb_frame && get_bits1(&s->gb))
00640             pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
00641         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00642 
00643         if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
00644             cbpy ^= 0xF;
00645 
00646         cbp = (cbpc & 3) | (cbpy << 2);
00647         if (dquant) {
00648             h263_decode_dquant(s);
00649         }
00650 
00651         s->mv_dir = MV_DIR_FORWARD;
00652         if ((cbpc & 16) == 0) {
00653             s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
00654             /* 16x16 motion prediction */
00655             s->mv_type = MV_TYPE_16X16;
00656             h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00657             if (s->umvplus)
00658                mx = h263p_decode_umotion(s, pred_x);
00659             else
00660                mx = h263_decode_motion(s, pred_x, 1);
00661 
00662             if (mx >= 0xffff)
00663                 return -1;
00664 
00665             if (s->umvplus)
00666                my = h263p_decode_umotion(s, pred_y);
00667             else
00668                my = h263_decode_motion(s, pred_y, 1);
00669 
00670             if (my >= 0xffff)
00671                 return -1;
00672             s->mv[0][0][0] = mx;
00673             s->mv[0][0][1] = my;
00674 
00675             if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00676                skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
00677         } else {
00678             s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
00679             s->mv_type = MV_TYPE_8X8;
00680             for(i=0;i<4;i++) {
00681                 mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00682                 if (s->umvplus)
00683                   mx = h263p_decode_umotion(s, pred_x);
00684                 else
00685                   mx = h263_decode_motion(s, pred_x, 1);
00686                 if (mx >= 0xffff)
00687                     return -1;
00688 
00689                 if (s->umvplus)
00690                   my = h263p_decode_umotion(s, pred_y);
00691                 else
00692                   my = h263_decode_motion(s, pred_y, 1);
00693                 if (my >= 0xffff)
00694                     return -1;
00695                 s->mv[0][i][0] = mx;
00696                 s->mv[0][i][1] = my;
00697                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00698                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
00699                 mot_val[0] = mx;
00700                 mot_val[1] = my;
00701             }
00702         }
00703     } else if(s->pict_type==FF_B_TYPE) {
00704         int mb_type;
00705         const int stride= s->b8_stride;
00706         int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ];
00707         int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ];
00708 //        const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
00709 
00710         //FIXME ugly
00711         mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
00712         mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
00713         mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
00714         mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
00715 
00716         do{
00717             mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
00718             if (mb_type < 0){
00719                 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
00720                 return -1;
00721             }
00722 
00723             mb_type= h263_mb_type_b_map[ mb_type ];
00724         }while(!mb_type);
00725 
00726         s->mb_intra = IS_INTRA(mb_type);
00727         if(HAS_CBP(mb_type)){
00728             s->dsp.clear_blocks(s->block[0]);
00729             cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
00730             if(s->mb_intra){
00731                 dquant = IS_QUANT(mb_type);
00732                 goto intra;
00733             }
00734 
00735             cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00736 
00737             if (cbpy < 0){
00738                 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
00739                 return -1;
00740             }
00741 
00742             if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
00743                 cbpy ^= 0xF;
00744 
00745             cbp = (cbpc & 3) | (cbpy << 2);
00746         }else
00747             cbp=0;
00748 
00749         assert(!s->mb_intra);
00750 
00751         if(IS_QUANT(mb_type)){
00752             h263_decode_dquant(s);
00753         }
00754 
00755         if(IS_DIRECT(mb_type)){
00756             s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
00757             mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
00758         }else{
00759             s->mv_dir = 0;
00760             s->mv_type= MV_TYPE_16X16;
00761 //FIXME UMV
00762 
00763             if(USES_LIST(mb_type, 0)){
00764                 int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my);
00765                 s->mv_dir = MV_DIR_FORWARD;
00766 
00767                 mx = h263_decode_motion(s, mx, 1);
00768                 my = h263_decode_motion(s, my, 1);
00769 
00770                 s->mv[0][0][0] = mx;
00771                 s->mv[0][0][1] = my;
00772                 mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
00773                 mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
00774             }
00775 
00776             if(USES_LIST(mb_type, 1)){
00777                 int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my);
00778                 s->mv_dir |= MV_DIR_BACKWARD;
00779 
00780                 mx = h263_decode_motion(s, mx, 1);
00781                 my = h263_decode_motion(s, my, 1);
00782 
00783                 s->mv[1][0][0] = mx;
00784                 s->mv[1][0][1] = my;
00785                 mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
00786                 mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
00787             }
00788         }
00789 
00790         s->current_picture.mb_type[xy]= mb_type;
00791     } else { /* I-Frame */
00792         do{
00793             cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
00794             if (cbpc < 0){
00795                 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
00796                 return -1;
00797             }
00798         }while(cbpc == 8);
00799 
00800         s->dsp.clear_blocks(s->block[0]);
00801 
00802         dquant = cbpc & 4;
00803         s->mb_intra = 1;
00804 intra:
00805         s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
00806         if (s->h263_aic) {
00807             s->ac_pred = get_bits1(&s->gb);
00808             if(s->ac_pred){
00809                 s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
00810 
00811                 s->h263_aic_dir = get_bits1(&s->gb);
00812             }
00813         }else
00814             s->ac_pred = 0;
00815 
00816         if(s->pb_frame && get_bits1(&s->gb))
00817             pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
00818         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00819         if(cbpy<0){
00820             av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
00821             return -1;
00822         }
00823         cbp = (cbpc & 3) | (cbpy << 2);
00824         if (dquant) {
00825             h263_decode_dquant(s);
00826         }
00827 
00828         pb_mv_count += !!s->pb_frame;
00829     }
00830 
00831     while(pb_mv_count--){
00832         h263_decode_motion(s, 0, 1);
00833         h263_decode_motion(s, 0, 1);
00834     }
00835 
00836     /* decode each block */
00837     for (i = 0; i < 6; i++) {
00838         if (h263_decode_block(s, block[i], i, cbp&32) < 0)
00839             return -1;
00840         cbp+=cbp;
00841     }
00842 
00843     if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
00844         return -1;
00845     if(s->obmc && !s->mb_intra){
00846         if(s->pict_type == FF_P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
00847             preview_obmc(s);
00848     }
00849 end:
00850 
00851         /* per-MB end of slice check */
00852     {
00853         int v= show_bits(&s->gb, 16);
00854 
00855         if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
00856             v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
00857         }
00858 
00859         if(v==0)
00860             return SLICE_END;
00861     }
00862 
00863     return SLICE_OK;
00864 }
00865 
00866 /* most is hardcoded. should extend to handle all h263 streams */
00867 int h263_decode_picture_header(MpegEncContext *s)
00868 {
00869     int format, width, height, i;
00870     uint32_t startcode;
00871 
00872     align_get_bits(&s->gb);
00873 
00874     startcode= get_bits(&s->gb, 22-8);
00875 
00876     for(i= get_bits_left(&s->gb); i>24; i-=8) {
00877         startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
00878 
00879         if(startcode == 0x20)
00880             break;
00881     }
00882 
00883     if (startcode != 0x20) {
00884         av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00885         return -1;
00886     }
00887     /* temporal reference */
00888     i = get_bits(&s->gb, 8); /* picture timestamp */
00889     if( (s->picture_number&~0xFF)+i < s->picture_number)
00890         i+= 256;
00891     s->current_picture_ptr->pts=
00892     s->picture_number= (s->picture_number&~0xFF) + i;
00893 
00894     /* PTYPE starts here */
00895     if (get_bits1(&s->gb) != 1) {
00896         /* marker */
00897         av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
00898         return -1;
00899     }
00900     if (get_bits1(&s->gb) != 0) {
00901         av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
00902         return -1;      /* h263 id */
00903     }
00904     skip_bits1(&s->gb);         /* split screen off */
00905     skip_bits1(&s->gb);         /* camera  off */
00906     skip_bits1(&s->gb);         /* freeze picture release off */
00907 
00908     format = get_bits(&s->gb, 3);
00909     /*
00910         0    forbidden
00911         1    sub-QCIF
00912         10   QCIF
00913         7       extended PTYPE (PLUSPTYPE)
00914     */
00915 
00916     if (format != 7 && format != 6) {
00917         s->h263_plus = 0;
00918         /* H.263v1 */
00919         width = h263_format[format][0];
00920         height = h263_format[format][1];
00921         if (!width)
00922             return -1;
00923 
00924         s->pict_type = FF_I_TYPE + get_bits1(&s->gb);
00925 
00926         s->h263_long_vectors = get_bits1(&s->gb);
00927 
00928         if (get_bits1(&s->gb) != 0) {
00929             av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
00930             return -1; /* SAC: off */
00931         }
00932         s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
00933         s->unrestricted_mv = s->h263_long_vectors || s->obmc;
00934 
00935         s->pb_frame = get_bits1(&s->gb);
00936         s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
00937         skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
00938 
00939         s->width = width;
00940         s->height = height;
00941         s->avctx->sample_aspect_ratio= (AVRational){12,11};
00942         s->avctx->time_base= (AVRational){1001, 30000};
00943     } else {
00944         int ufep;
00945 
00946         /* H.263v2 */
00947         s->h263_plus = 1;
00948         ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
00949 
00950         /* ufep other than 0 and 1 are reserved */
00951         if (ufep == 1) {
00952             /* OPPTYPE */
00953             format = get_bits(&s->gb, 3);
00954             dprintf(s->avctx, "ufep=1, format: %d\n", format);
00955             s->custom_pcf= get_bits1(&s->gb);
00956             s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
00957             if (get_bits1(&s->gb) != 0) {
00958                 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
00959             }
00960             s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
00961             s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
00962             s->loop_filter= get_bits1(&s->gb);
00963             s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
00964 
00965             s->h263_slice_structured= get_bits1(&s->gb);
00966             if (get_bits1(&s->gb) != 0) {
00967                 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
00968             }
00969             if (get_bits1(&s->gb) != 0) {
00970                 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
00971             }
00972             s->alt_inter_vlc= get_bits1(&s->gb);
00973             s->modified_quant= get_bits1(&s->gb);
00974             if(s->modified_quant)
00975                 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
00976 
00977             skip_bits(&s->gb, 1); /* Prevent start code emulation */
00978 
00979             skip_bits(&s->gb, 3); /* Reserved */
00980         } else if (ufep != 0) {
00981             av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
00982             return -1;
00983         }
00984 
00985         /* MPPTYPE */
00986         s->pict_type = get_bits(&s->gb, 3);
00987         switch(s->pict_type){
00988         case 0: s->pict_type= FF_I_TYPE;break;
00989         case 1: s->pict_type= FF_P_TYPE;break;
00990         case 2: s->pict_type= FF_P_TYPE;s->pb_frame = 3;break;
00991         case 3: s->pict_type= FF_B_TYPE;break;
00992         case 7: s->pict_type= FF_I_TYPE;break; //ZYGO
00993         default:
00994             return -1;
00995         }
00996         skip_bits(&s->gb, 2);
00997         s->no_rounding = get_bits1(&s->gb);
00998         skip_bits(&s->gb, 4);
00999 
01000         /* Get the picture dimensions */
01001         if (ufep) {
01002             if (format == 6) {
01003                 /* Custom Picture Format (CPFMT) */
01004                 s->aspect_ratio_info = get_bits(&s->gb, 4);
01005                 dprintf(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
01006                 /* aspect ratios:
01007                 0 - forbidden
01008                 1 - 1:1
01009                 2 - 12:11 (CIF 4:3)
01010                 3 - 10:11 (525-type 4:3)
01011                 4 - 16:11 (CIF 16:9)
01012                 5 - 40:33 (525-type 16:9)
01013                 6-14 - reserved
01014                 */
01015                 width = (get_bits(&s->gb, 9) + 1) * 4;
01016                 skip_bits1(&s->gb);
01017                 height = get_bits(&s->gb, 9) * 4;
01018                 dprintf(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
01019                 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
01020                     /* aspected dimensions */
01021                     s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
01022                     s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
01023                 }else{
01024                     s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
01025                 }
01026             } else {
01027                 width = h263_format[format][0];
01028                 height = h263_format[format][1];
01029                 s->avctx->sample_aspect_ratio= (AVRational){12,11};
01030             }
01031             if ((width == 0) || (height == 0))
01032                 return -1;
01033             s->width = width;
01034             s->height = height;
01035 
01036             if(s->custom_pcf){
01037                 int gcd;
01038                 s->avctx->time_base.den= 1800000;
01039                 s->avctx->time_base.num= 1000 + get_bits1(&s->gb);
01040                 s->avctx->time_base.num*= get_bits(&s->gb, 7);
01041                 if(s->avctx->time_base.num == 0){
01042                     av_log(s, AV_LOG_ERROR, "zero framerate\n");
01043                     return -1;
01044                 }
01045                 gcd= av_gcd(s->avctx->time_base.den, s->avctx->time_base.num);
01046                 s->avctx->time_base.den /= gcd;
01047                 s->avctx->time_base.num /= gcd;
01048             }else{
01049                 s->avctx->time_base= (AVRational){1001, 30000};
01050             }
01051         }
01052 
01053         if(s->custom_pcf){
01054             skip_bits(&s->gb, 2); //extended Temporal reference
01055         }
01056 
01057         if (ufep) {
01058             if (s->umvplus) {
01059                 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
01060                     skip_bits1(&s->gb);
01061             }
01062             if(s->h263_slice_structured){
01063                 if (get_bits1(&s->gb) != 0) {
01064                     av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
01065                 }
01066                 if (get_bits1(&s->gb) != 0) {
01067                     av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
01068                 }
01069             }
01070         }
01071 
01072         s->qscale = get_bits(&s->gb, 5);
01073     }
01074 
01075     s->mb_width = (s->width  + 15) / 16;
01076     s->mb_height = (s->height  + 15) / 16;
01077     s->mb_num = s->mb_width * s->mb_height;
01078 
01079     if (s->pb_frame) {
01080         skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
01081         if (s->custom_pcf)
01082             skip_bits(&s->gb, 2); //extended Temporal reference
01083         skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
01084     }
01085 
01086     /* PEI */
01087     while (get_bits1(&s->gb) != 0) {
01088         skip_bits(&s->gb, 8);
01089     }
01090 
01091     if(s->h263_slice_structured){
01092         if (get_bits1(&s->gb) != 1) {
01093             av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
01094             return -1;
01095         }
01096 
01097         ff_h263_decode_mba(s);
01098 
01099         if (get_bits1(&s->gb) != 1) {
01100             av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
01101             return -1;
01102         }
01103     }
01104     s->f_code = 1;
01105 
01106     if(s->h263_aic){
01107          s->y_dc_scale_table=
01108          s->c_dc_scale_table= ff_aic_dc_scale_table;
01109     }else{
01110         s->y_dc_scale_table=
01111         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
01112     }
01113 
01114         ff_h263_show_pict_info(s);
01115     if (s->pict_type == FF_I_TYPE && s->codec_tag == AV_RL32("ZYGO")){
01116         int i,j;
01117         for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
01118         av_log(s->avctx, AV_LOG_DEBUG, "\n");
01119         for(i=0; i<13; i++){
01120             for(j=0; j<3; j++){
01121                 int v= get_bits(&s->gb, 8);
01122                 v |= get_sbits(&s->gb, 8)<<8;
01123                 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
01124             }
01125             av_log(s->avctx, AV_LOG_DEBUG, "\n");
01126         }
01127         for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
01128     }
01129 
01130     return 0;
01131 }

Generated on Fri Sep 16 2011 17:17:38 for FFmpeg by  doxygen 1.7.1