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

libavcodec/vc1.c

Go to the documentation of this file.
00001 /*
00002  * VC-1 and WMV3 decoder common code
00003  * Copyright (c) 2011 Mashiat Sarker Shakkhar
00004  * Copyright (c) 2006-2007 Konstantin Shishkov
00005  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 
00030 #include "internal.h"
00031 #include "dsputil.h"
00032 #include "avcodec.h"
00033 #include "mpegvideo.h"
00034 #include "vc1.h"
00035 #include "vc1data.h"
00036 #include "msmpeg4data.h"
00037 #include "unary.h"
00038 #include "simple_idct.h"
00039 
00040 #undef NDEBUG
00041 #include <assert.h>
00042 
00043 /***********************************************************************/
00054 enum Imode {
00055     IMODE_RAW,
00056     IMODE_NORM2,
00057     IMODE_DIFF2,
00058     IMODE_NORM6,
00059     IMODE_DIFF6,
00060     IMODE_ROWSKIP,
00061     IMODE_COLSKIP
00062 }; //imode defines
00064 
00071 static void decode_rowskip(uint8_t* plane, int width, int height, int stride,
00072                            GetBitContext *gb)
00073 {
00074     int x, y;
00075 
00076     for (y = 0; y < height; y++) {
00077         if (!get_bits1(gb)) //rowskip
00078             memset(plane, 0, width);
00079         else
00080             for (x = 0; x < width; x++)
00081                 plane[x] = get_bits1(gb);
00082         plane += stride;
00083     }
00084 }
00085 
00093 static void decode_colskip(uint8_t* plane, int width, int height, int stride,
00094                            GetBitContext *gb)
00095 {
00096     int x, y;
00097 
00098     for (x = 0; x < width; x++) {
00099         if (!get_bits1(gb)) //colskip
00100             for (y = 0; y < height; y++)
00101                 plane[y*stride] = 0;
00102         else
00103             for (y = 0; y < height; y++)
00104                 plane[y*stride] = get_bits1(gb);
00105         plane ++;
00106     }
00107 }
00108 
00116 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
00117 {
00118     GetBitContext *gb = &v->s.gb;
00119 
00120     int imode, x, y, code, offset;
00121     uint8_t invert, *planep = data;
00122     int width, height, stride;
00123 
00124     width  = v->s.mb_width;
00125     height = v->s.mb_height >> v->field_mode;
00126     stride = v->s.mb_stride;
00127     invert = get_bits1(gb);
00128     imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
00129 
00130     *raw_flag = 0;
00131     switch (imode) {
00132     case IMODE_RAW:
00133         //Data is actually read in the MB layer (same for all tests == "raw")
00134         *raw_flag = 1; //invert ignored
00135         return invert;
00136     case IMODE_DIFF2:
00137     case IMODE_NORM2:
00138         if ((height * width) & 1) {
00139             *planep++ = get_bits1(gb);
00140             offset    = 1;
00141         }
00142         else
00143             offset = 0;
00144         // decode bitplane as one long line
00145         for (y = offset; y < height * width; y += 2) {
00146             code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
00147             *planep++ = code & 1;
00148             offset++;
00149             if (offset == width) {
00150                 offset  = 0;
00151                 planep += stride - width;
00152             }
00153             *planep++ = code >> 1;
00154             offset++;
00155             if (offset == width) {
00156                 offset  = 0;
00157                 planep += stride - width;
00158             }
00159         }
00160         break;
00161     case IMODE_DIFF6:
00162     case IMODE_NORM6:
00163         if (!(height % 3) && (width % 3)) { // use 2x3 decoding
00164             for (y = 0; y < height; y += 3) {
00165                 for (x = width & 1; x < width; x += 2) {
00166                     code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
00167                     if (code < 0) {
00168                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
00169                         return -1;
00170                     }
00171                     planep[x + 0]              = (code >> 0) & 1;
00172                     planep[x + 1]              = (code >> 1) & 1;
00173                     planep[x + 0 + stride]     = (code >> 2) & 1;
00174                     planep[x + 1 + stride]     = (code >> 3) & 1;
00175                     planep[x + 0 + stride * 2] = (code >> 4) & 1;
00176                     planep[x + 1 + stride * 2] = (code >> 5) & 1;
00177                 }
00178                 planep += stride * 3;
00179             }
00180             if (width & 1)
00181                 decode_colskip(data, 1, height, stride, &v->s.gb);
00182         } else { // 3x2
00183             planep += (height & 1) * stride;
00184             for (y = height & 1; y < height; y += 2) {
00185                 for (x = width % 3; x < width; x += 3) {
00186                     code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
00187                     if (code < 0) {
00188                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
00189                         return -1;
00190                     }
00191                     planep[x + 0]          = (code >> 0) & 1;
00192                     planep[x + 1]          = (code >> 1) & 1;
00193                     planep[x + 2]          = (code >> 2) & 1;
00194                     planep[x + 0 + stride] = (code >> 3) & 1;
00195                     planep[x + 1 + stride] = (code >> 4) & 1;
00196                     planep[x + 2 + stride] = (code >> 5) & 1;
00197                 }
00198                 planep += stride * 2;
00199             }
00200             x = width % 3;
00201             if (x)
00202                 decode_colskip(data,             x, height, stride, &v->s.gb);
00203             if (height & 1)
00204                 decode_rowskip(data + x, width - x,      1, stride, &v->s.gb);
00205         }
00206         break;
00207     case IMODE_ROWSKIP:
00208         decode_rowskip(data, width, height, stride, &v->s.gb);
00209         break;
00210     case IMODE_COLSKIP:
00211         decode_colskip(data, width, height, stride, &v->s.gb);
00212         break;
00213     default:
00214         break;
00215     }
00216 
00217     /* Applying diff operator */
00218     if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6) {
00219         planep = data;
00220         planep[0] ^= invert;
00221         for (x = 1; x < width; x++)
00222             planep[x] ^= planep[x-1];
00223         for (y = 1; y < height; y++) {
00224             planep += stride;
00225             planep[0] ^= planep[-stride];
00226             for (x = 1; x < width; x++) {
00227                 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
00228                 else                                 planep[x] ^= planep[x-1];
00229             }
00230         }
00231     } else if (invert) {
00232         planep = data;
00233         for (x = 0; x < stride * height; x++)
00234             planep[x] = !planep[x]; //FIXME stride
00235     }
00236     return (imode << 1) + invert;
00237 }
00238  //Bitplane group
00240 
00241 /***********************************************************************/
00245 static int vop_dquant_decoding(VC1Context *v)
00246 {
00247     GetBitContext *gb = &v->s.gb;
00248     int pqdiff;
00249 
00250     //variable size
00251     if (v->dquant == 2) {
00252         pqdiff = get_bits(gb, 3);
00253         if (pqdiff == 7)
00254             v->altpq = get_bits(gb, 5);
00255         else
00256             v->altpq = v->pq + pqdiff + 1;
00257     } else {
00258         v->dquantfrm = get_bits1(gb);
00259         if (v->dquantfrm) {
00260             v->dqprofile = get_bits(gb, 2);
00261             switch (v->dqprofile) {
00262             case DQPROFILE_SINGLE_EDGE:
00263             case DQPROFILE_DOUBLE_EDGES:
00264                 v->dqsbedge = get_bits(gb, 2);
00265                 break;
00266             case DQPROFILE_ALL_MBS:
00267                 v->dqbilevel = get_bits1(gb);
00268                 if (!v->dqbilevel)
00269                     v->halfpq = 0;
00270             default:
00271                 break; //Forbidden ?
00272             }
00273             if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS) {
00274                 pqdiff = get_bits(gb, 3);
00275                 if (pqdiff == 7)
00276                     v->altpq = get_bits(gb, 5);
00277                 else
00278                     v->altpq = v->pq + pqdiff + 1;
00279             }
00280         }
00281     }
00282     return 0;
00283 }
00284 
00285 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
00286 
00294 int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
00295 {
00296     av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
00297     v->profile = get_bits(gb, 2);
00298     if (v->profile == PROFILE_COMPLEX) {
00299         av_log(avctx, AV_LOG_WARNING, "WMV3 Complex Profile is not fully supported\n");
00300     }
00301 
00302     if (v->profile == PROFILE_ADVANCED) {
00303         v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
00304         v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
00305         return decode_sequence_header_adv(v, gb);
00306     } else {
00307         v->zz_8x4 = wmv2_scantableA;
00308         v->zz_4x8 = wmv2_scantableB;
00309         v->res_y411   = get_bits1(gb);
00310         v->res_sprite = get_bits1(gb);
00311         if (v->res_y411) {
00312             av_log(avctx, AV_LOG_ERROR,
00313                    "Old interlaced mode is not supported\n");
00314             return -1;
00315         }
00316     }
00317 
00318     // (fps-2)/4 (->30)
00319     v->frmrtq_postproc = get_bits(gb, 3); //common
00320     // (bitrate-32kbps)/64kbps
00321     v->bitrtq_postproc = get_bits(gb, 5); //common
00322     v->s.loop_filter   = get_bits1(gb); //common
00323     if (v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE) {
00324         av_log(avctx, AV_LOG_ERROR,
00325                "LOOPFILTER shall not be enabled in Simple Profile\n");
00326     }
00327     if (v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
00328         v->s.loop_filter = 0;
00329 
00330     v->res_x8          = get_bits1(gb); //reserved
00331     v->multires        = get_bits1(gb);
00332     v->res_fasttx      = get_bits1(gb);
00333     if (!v->res_fasttx) {
00334         v->vc1dsp.vc1_inv_trans_8x8    = ff_simple_idct_8;
00335         v->vc1dsp.vc1_inv_trans_8x4    = ff_simple_idct84_add;
00336         v->vc1dsp.vc1_inv_trans_4x8    = ff_simple_idct48_add;
00337         v->vc1dsp.vc1_inv_trans_4x4    = ff_simple_idct44_add;
00338         v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add_8;
00339         v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
00340         v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
00341         v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
00342     }
00343 
00344     v->fastuvmc        = get_bits1(gb); //common
00345     if (!v->profile && !v->fastuvmc) {
00346         av_log(avctx, AV_LOG_ERROR,
00347                "FASTUVMC unavailable in Simple Profile\n");
00348         return -1;
00349     }
00350     v->extended_mv     = get_bits1(gb); //common
00351     if (!v->profile && v->extended_mv)
00352     {
00353         av_log(avctx, AV_LOG_ERROR,
00354                "Extended MVs unavailable in Simple Profile\n");
00355         return -1;
00356     }
00357     v->dquant          = get_bits(gb, 2); //common
00358     v->vstransform     = get_bits1(gb); //common
00359 
00360     v->res_transtab    = get_bits1(gb);
00361     if (v->res_transtab)
00362     {
00363         av_log(avctx, AV_LOG_ERROR,
00364                "1 for reserved RES_TRANSTAB is forbidden\n");
00365         return -1;
00366     }
00367 
00368     v->overlap         = get_bits1(gb); //common
00369 
00370     v->s.resync_marker = get_bits1(gb);
00371     v->rangered        = get_bits1(gb);
00372     if (v->rangered && v->profile == PROFILE_SIMPLE) {
00373         av_log(avctx, AV_LOG_INFO,
00374                "RANGERED should be set to 0 in Simple Profile\n");
00375     }
00376 
00377     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
00378     v->quantizer_mode = get_bits(gb, 2); //common
00379 
00380     v->finterpflag = get_bits1(gb); //common
00381 
00382     if (v->res_sprite) {
00383         int w = get_bits(gb, 11);
00384         int h = get_bits(gb, 11);
00385         avcodec_set_dimensions(v->s.avctx, w, h);
00386         skip_bits(gb, 5); //frame rate
00387         v->res_x8 = get_bits1(gb);
00388         if (get_bits1(gb)) { // something to do with DC VLC selection
00389             av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
00390             return -1;
00391         }
00392         skip_bits(gb, 3); //slice code
00393         v->res_rtm_flag = 0;
00394     } else {
00395         v->res_rtm_flag = get_bits1(gb); //reserved
00396     }
00397     if (!v->res_rtm_flag) {
00398 //            av_log(avctx, AV_LOG_ERROR,
00399 //                   "0 for reserved RES_RTM_FLAG is forbidden\n");
00400         av_log(avctx, AV_LOG_ERROR,
00401                "Old WMV3 version detected, some frames may be decoded incorrectly\n");
00402         //return -1;
00403     }
00404     //TODO: figure out what they mean (always 0x402F)
00405     if (!v->res_fasttx)
00406         skip_bits(gb, 16);
00407     av_log(avctx, AV_LOG_DEBUG,
00408            "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00409            "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
00410            "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
00411            "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
00412            v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
00413            v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
00414            v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
00415            v->dquant, v->quantizer_mode, avctx->max_b_frames);
00416     return 0;
00417 }
00418 
00419 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
00420 {
00421     int w, h;
00422     v->res_rtm_flag = 1;
00423     v->level = get_bits(gb, 3);
00424     if (v->level >= 5) {
00425         av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
00426     }
00427     v->chromaformat = get_bits(gb, 2);
00428     if (v->chromaformat != 1) {
00429         av_log(v->s.avctx, AV_LOG_ERROR,
00430                "Only 4:2:0 chroma format supported\n");
00431         return -1;
00432     }
00433 
00434     // (fps-2)/4 (->30)
00435     v->frmrtq_postproc       = get_bits(gb, 3); //common
00436     // (bitrate-32kbps)/64kbps
00437     v->bitrtq_postproc       = get_bits(gb, 5); //common
00438     v->postprocflag          = get_bits1(gb);   //common
00439 
00440     w = (get_bits(gb, 12) + 1) << 1;
00441     h = (get_bits(gb, 12) + 1) << 1;
00442     avcodec_set_dimensions(v->s.avctx, w, h);
00443     v->broadcast             = get_bits1(gb);
00444     v->interlace             = get_bits1(gb);
00445     v->tfcntrflag            = get_bits1(gb);
00446     v->finterpflag           = get_bits1(gb);
00447     skip_bits1(gb); // reserved
00448 
00449     av_log(v->s.avctx, AV_LOG_DEBUG,
00450            "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00451            "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
00452            "TFCTRflag=%i, FINTERPflag=%i\n",
00453            v->level, v->frmrtq_postproc, v->bitrtq_postproc,
00454            v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
00455            v->tfcntrflag, v->finterpflag);
00456 
00457     v->psf = get_bits1(gb);
00458     if (v->psf) { //PsF, 6.1.13
00459         av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
00460         return -1;
00461     }
00462     v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
00463     if (get_bits1(gb)) { //Display Info - decoding is not affected by it
00464         int w, h, ar = 0;
00465         av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
00466         w = get_bits(gb, 14) + 1;
00467         h = get_bits(gb, 14) + 1;
00468         av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
00469         if (get_bits1(gb))
00470             ar = get_bits(gb, 4);
00471         if (ar && ar < 14) {
00472             v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
00473         } else if (ar == 15) {
00474             w = get_bits(gb, 8) + 1;
00475             h = get_bits(gb, 8) + 1;
00476             v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
00477         } else {
00478             av_reduce(&v->s.avctx->sample_aspect_ratio.num,
00479                       &v->s.avctx->sample_aspect_ratio.den,
00480                       v->s.avctx->height * w,
00481                       v->s.avctx->width * h,
00482                       1 << 30);
00483         }
00484         av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",
00485                v->s.avctx->sample_aspect_ratio.num,
00486                v->s.avctx->sample_aspect_ratio.den);
00487 
00488         if (get_bits1(gb)) { //framerate stuff
00489             if (get_bits1(gb)) {
00490                 v->s.avctx->time_base.num = 32;
00491                 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
00492             } else {
00493                 int nr, dr;
00494                 nr = get_bits(gb, 8);
00495                 dr = get_bits(gb, 4);
00496                 if (nr > 0 && nr < 8 && dr > 0 && dr < 3) {
00497                     v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
00498                     v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
00499                 }
00500             }
00501             if (v->broadcast) { // Pulldown may be present
00502                 v->s.avctx->time_base.den  *= 2;
00503                 v->s.avctx->ticks_per_frame = 2;
00504             }
00505         }
00506 
00507         if (get_bits1(gb)) {
00508             v->color_prim    = get_bits(gb, 8);
00509             v->transfer_char = get_bits(gb, 8);
00510             v->matrix_coef   = get_bits(gb, 8);
00511         }
00512     }
00513 
00514     v->hrd_param_flag = get_bits1(gb);
00515     if (v->hrd_param_flag) {
00516         int i;
00517         v->hrd_num_leaky_buckets = get_bits(gb, 5);
00518         skip_bits(gb, 4); //bitrate exponent
00519         skip_bits(gb, 4); //buffer size exponent
00520         for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
00521             skip_bits(gb, 16); //hrd_rate[n]
00522             skip_bits(gb, 16); //hrd_buffer[n]
00523         }
00524     }
00525     return 0;
00526 }
00527 
00528 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
00529 {
00530     int i;
00531 
00532     av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
00533     v->broken_link    = get_bits1(gb);
00534     v->closed_entry   = get_bits1(gb);
00535     v->panscanflag    = get_bits1(gb);
00536     v->refdist_flag   = get_bits1(gb);
00537     v->s.loop_filter  = get_bits1(gb);
00538     v->fastuvmc       = get_bits1(gb);
00539     v->extended_mv    = get_bits1(gb);
00540     v->dquant         = get_bits(gb, 2);
00541     v->vstransform    = get_bits1(gb);
00542     v->overlap        = get_bits1(gb);
00543     v->quantizer_mode = get_bits(gb, 2);
00544 
00545     if (v->hrd_param_flag) {
00546         for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
00547             skip_bits(gb, 8); //hrd_full[n]
00548         }
00549     }
00550 
00551     if(get_bits1(gb)){
00552         int w = (get_bits(gb, 12)+1)<<1;
00553         int h = (get_bits(gb, 12)+1)<<1;
00554         avcodec_set_dimensions(avctx, w, h);
00555     }
00556     if (v->extended_mv)
00557         v->extended_dmv = get_bits1(gb);
00558     if ((v->range_mapy_flag = get_bits1(gb))) {
00559         av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
00560         v->range_mapy = get_bits(gb, 3);
00561     }
00562     if ((v->range_mapuv_flag = get_bits1(gb))) {
00563         av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
00564         v->range_mapuv = get_bits(gb, 3);
00565     }
00566 
00567     av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
00568            "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
00569            "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
00570            "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
00571            v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
00572            v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
00573 
00574     return 0;
00575 }
00576 
00577 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
00578 {
00579     int pqindex, lowquant, status;
00580 
00581     if (v->finterpflag)
00582         v->interpfrm = get_bits1(gb);
00583     skip_bits(gb, 2); //framecnt unused
00584     v->rangeredfrm = 0;
00585     if (v->rangered)
00586         v->rangeredfrm = get_bits1(gb);
00587     v->s.pict_type = get_bits1(gb);
00588     if (v->s.avctx->max_b_frames) {
00589         if (!v->s.pict_type) {
00590             if (get_bits1(gb))
00591                 v->s.pict_type = AV_PICTURE_TYPE_I;
00592             else
00593                 v->s.pict_type = AV_PICTURE_TYPE_B;
00594         } else
00595             v->s.pict_type = AV_PICTURE_TYPE_P;
00596     } else
00597         v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00598 
00599     v->bi_type = 0;
00600     if (v->s.pict_type == AV_PICTURE_TYPE_B) {
00601         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00602         v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00603         if (v->bfraction == 0) {
00604             v->s.pict_type = AV_PICTURE_TYPE_BI;
00605         }
00606     }
00607     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
00608         skip_bits(gb, 7); // skip buffer fullness
00609 
00610     if (v->parse_only)
00611         return 0;
00612 
00613     /* calculate RND */
00614     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
00615         v->rnd = 1;
00616     if (v->s.pict_type == AV_PICTURE_TYPE_P)
00617         v->rnd ^= 1;
00618 
00619     /* Quantizer stuff */
00620     pqindex = get_bits(gb, 5);
00621     if (!pqindex)
00622         return -1;
00623     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00624         v->pq = ff_vc1_pquant_table[0][pqindex];
00625     else
00626         v->pq = ff_vc1_pquant_table[1][pqindex];
00627 
00628     v->pquantizer = 1;
00629     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00630         v->pquantizer = pqindex < 9;
00631     if (v->quantizer_mode == QUANT_NON_UNIFORM)
00632         v->pquantizer = 0;
00633     v->pqindex = pqindex;
00634     if (pqindex < 9)
00635         v->halfpq = get_bits1(gb);
00636     else
00637         v->halfpq = 0;
00638     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00639         v->pquantizer = get_bits1(gb);
00640     v->dquantfrm = 0;
00641     if (v->extended_mv == 1)
00642         v->mvrange = get_unary(gb, 0, 3);
00643     v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00644     v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00645     v->range_x = 1 << (v->k_x - 1);
00646     v->range_y = 1 << (v->k_y - 1);
00647     if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B)
00648         v->respic = get_bits(gb, 2);
00649 
00650     if (v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)) {
00651         v->x8_type = get_bits1(gb);
00652     } else
00653         v->x8_type = 0;
00654 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
00655 //        (v->s.pict_type == AV_PICTURE_TYPE_P) ? 'P' : ((v->s.pict_type == AV_PICTURE_TYPE_I) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
00656 
00657     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
00658         v->use_ic = 0;
00659 
00660     switch (v->s.pict_type) {
00661     case AV_PICTURE_TYPE_P:
00662         if (v->pq < 5)       v->tt_index = 0;
00663         else if (v->pq < 13) v->tt_index = 1;
00664         else                 v->tt_index = 2;
00665 
00666         lowquant = (v->pq > 12) ? 0 : 1;
00667         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00668         if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00669             int scale, shift, i;
00670             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00671             v->lumscale = get_bits(gb, 6);
00672             v->lumshift = get_bits(gb, 6);
00673             v->use_ic   = 1;
00674             /* fill lookup tables for intensity compensation */
00675             if (!v->lumscale) {
00676                 scale = -64;
00677                 shift = (255 - v->lumshift * 2) << 6;
00678                 if (v->lumshift > 31)
00679                     shift += 128 << 6;
00680             } else {
00681                 scale = v->lumscale + 32;
00682                 if (v->lumshift > 31)
00683                     shift = (v->lumshift - 64) << 6;
00684                 else
00685                     shift = v->lumshift << 6;
00686             }
00687             for (i = 0; i < 256; i++) {
00688                 v->luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);
00689                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00690             }
00691         }
00692         v->qs_last = v->s.quarter_sample;
00693         if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00694             v->s.quarter_sample = 0;
00695         else if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00696             if (v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00697                 v->s.quarter_sample = 0;
00698             else
00699                 v->s.quarter_sample = 1;
00700         } else
00701             v->s.quarter_sample = 1;
00702         v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
00703 
00704         if ((v->mv_mode  == MV_PMODE_INTENSITY_COMP &&
00705              v->mv_mode2 == MV_PMODE_MIXED_MV)      ||
00706             v->mv_mode   == MV_PMODE_MIXED_MV) {
00707             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00708             if (status < 0)
00709                 return -1;
00710             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00711                    "Imode: %i, Invert: %i\n", status>>1, status&1);
00712         } else {
00713             v->mv_type_is_raw = 0;
00714             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00715         }
00716         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00717         if (status < 0)
00718             return -1;
00719         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00720                "Imode: %i, Invert: %i\n", status>>1, status&1);
00721 
00722         /* Hopefully this is correct for P frames */
00723         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
00724         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00725 
00726         if (v->dquant) {
00727             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00728             vop_dquant_decoding(v);
00729         }
00730 
00731         v->ttfrm = 0; //FIXME Is that so ?
00732         if (v->vstransform) {
00733             v->ttmbf = get_bits1(gb);
00734             if (v->ttmbf) {
00735                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00736             }
00737         } else {
00738             v->ttmbf = 1;
00739             v->ttfrm = TT_8X8;
00740         }
00741         break;
00742     case AV_PICTURE_TYPE_B:
00743         if (v->pq < 5)       v->tt_index = 0;
00744         else if (v->pq < 13) v->tt_index = 1;
00745         else                 v->tt_index = 2;
00746 
00747         v->mv_mode          = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00748         v->qs_last          = v->s.quarter_sample;
00749         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
00750         v->s.mspel          = v->s.quarter_sample;
00751 
00752         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
00753         if (status < 0)
00754             return -1;
00755         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
00756                "Imode: %i, Invert: %i\n", status>>1, status&1);
00757         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00758         if (status < 0)
00759             return -1;
00760         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00761                "Imode: %i, Invert: %i\n", status>>1, status&1);
00762 
00763         v->s.mv_table_index = get_bits(gb, 2);
00764         v->cbpcy_vlc        = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00765 
00766         if (v->dquant) {
00767             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00768             vop_dquant_decoding(v);
00769         }
00770 
00771         v->ttfrm = 0;
00772         if (v->vstransform) {
00773             v->ttmbf = get_bits1(gb);
00774             if (v->ttmbf) {
00775                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00776             }
00777         } else {
00778             v->ttmbf = 1;
00779             v->ttfrm = TT_8X8;
00780         }
00781         break;
00782     }
00783 
00784     if (!v->x8_type) {
00785         /* AC Syntax */
00786         v->c_ac_table_index = decode012(gb);
00787         if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
00788             v->y_ac_table_index = decode012(gb);
00789         }
00790         /* DC Syntax */
00791         v->s.dc_table_index = get_bits1(gb);
00792     }
00793 
00794     if (v->s.pict_type == AV_PICTURE_TYPE_BI) {
00795         v->s.pict_type = AV_PICTURE_TYPE_B;
00796         v->bi_type     = 1;
00797     }
00798     return 0;
00799 }
00800 
00801 /* fill lookup tables for intensity compensation */
00802 #define INIT_LUT(lumscale, lumshift, luty, lutuv)   \
00803     if (!lumscale) {                                \
00804         scale = -64;                                \
00805         shift = (255 - lumshift * 2) << 6;          \
00806         if (lumshift > 31)                          \
00807             shift += 128 << 6;                      \
00808     } else {                                        \
00809         scale = lumscale + 32;                      \
00810         if (lumshift > 31)                          \
00811             shift = (lumshift - 64) << 6;           \
00812         else                                        \
00813             shift = lumshift << 6;                  \
00814     }                                               \
00815     for (i = 0; i < 256; i++) {                     \
00816         luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);           \
00817         lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);  \
00818     }
00819 
00820 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
00821 {
00822     int pqindex, lowquant;
00823     int status;
00824     int mbmodetab, imvtab, icbptab, twomvbptab, fourmvbptab; /* useful only for debugging */
00825     int scale, shift, i; /* for initializing LUT for intensity compensation */
00826 
00827     v->numref=0;
00828     v->p_frame_skipped = 0;
00829     if (v->second_field) {
00830         if(v->fcm!=2 || v->field_mode!=1)
00831             return -1;
00832         v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00833         if (v->fptype & 4)
00834             v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
00835         v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
00836         if (!v->pic_header_flag)
00837             goto parse_common_info;
00838     }
00839 
00840     v->field_mode = 0;
00841     if (v->interlace) {
00842         v->fcm = decode012(gb);
00843         if (v->fcm) {
00844             if (v->fcm == ILACE_FIELD)
00845                 v->field_mode = 1;
00846             if (!v->warn_interlaced++)
00847                 av_log(v->s.avctx, AV_LOG_ERROR,
00848                        "Interlaced frames/fields support is incomplete\n");
00849         }
00850     } else {
00851         v->fcm = PROGRESSIVE;
00852     }
00853 
00854     if (v->field_mode) {
00855         v->fptype = get_bits(gb, 3);
00856         v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00857         if (v->fptype & 4) // B-picture
00858             v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
00859     } else {
00860         switch (get_unary(gb, 0, 4)) {
00861         case 0:
00862             v->s.pict_type = AV_PICTURE_TYPE_P;
00863             break;
00864         case 1:
00865             v->s.pict_type = AV_PICTURE_TYPE_B;
00866             break;
00867         case 2:
00868             v->s.pict_type = AV_PICTURE_TYPE_I;
00869             break;
00870         case 3:
00871             v->s.pict_type = AV_PICTURE_TYPE_BI;
00872             break;
00873         case 4:
00874             v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
00875             v->p_frame_skipped = 1;
00876             break;
00877         }
00878     }
00879     if (v->tfcntrflag)
00880         skip_bits(gb, 8);
00881     if (v->broadcast) {
00882         if (!v->interlace || v->psf) {
00883             v->rptfrm = get_bits(gb, 2);
00884         } else {
00885             v->tff = get_bits1(gb);
00886             v->rff = get_bits1(gb);
00887         }
00888     }
00889     if (v->panscanflag) {
00890         av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
00891         //...
00892     }
00893     if (v->p_frame_skipped) {
00894         return 0;
00895     }
00896     v->rnd = get_bits1(gb);
00897     if (v->interlace)
00898         v->uvsamp = get_bits1(gb);
00899     if(!ff_vc1_bfraction_vlc.table)
00900         return 0; //parsing only, vlc tables havnt been allocated
00901     if (v->field_mode) {
00902         if (!v->refdist_flag)
00903             v->refdist = 0;
00904         else {
00905             if ((v->s.pict_type != AV_PICTURE_TYPE_B)
00906                 && (v->s.pict_type != AV_PICTURE_TYPE_BI)) {
00907                 v->refdist = get_bits(gb, 2);
00908                 if (v->refdist == 3)
00909                     v->refdist += get_unary(gb, 0, 16);
00910             } else {
00911                 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00912                 v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00913                 v->frfd = (v->bfraction * v->refdist) >> 8;
00914                 v->brfd = v->refdist - v->frfd - 1;
00915                 if (v->brfd < 0)
00916                     v->brfd = 0;
00917             }
00918         }
00919         goto parse_common_info;
00920     }
00921     if (v->fcm == PROGRESSIVE) {
00922         if (v->finterpflag)
00923             v->interpfrm = get_bits1(gb);
00924         if (v->s.pict_type == AV_PICTURE_TYPE_B) {
00925             v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00926             v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00927             if (v->bfraction == 0) {
00928                 v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
00929             }
00930         }
00931     }
00932 
00933     parse_common_info:
00934     if (v->field_mode)
00935         v->cur_field_type = !(v->tff ^ v->second_field);
00936     pqindex = get_bits(gb, 5);
00937     if (!pqindex)
00938         return -1;
00939     v->pqindex = pqindex;
00940     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00941         v->pq = ff_vc1_pquant_table[0][pqindex];
00942     else
00943         v->pq = ff_vc1_pquant_table[1][pqindex];
00944 
00945     v->pquantizer = 1;
00946     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00947         v->pquantizer = pqindex < 9;
00948     if (v->quantizer_mode == QUANT_NON_UNIFORM)
00949         v->pquantizer = 0;
00950     v->pqindex = pqindex;
00951     if (pqindex < 9)
00952         v->halfpq = get_bits1(gb);
00953     else
00954         v->halfpq = 0;
00955     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00956         v->pquantizer = get_bits1(gb);
00957     if (v->postprocflag)
00958         v->postproc = get_bits(gb, 2);
00959 
00960     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
00961         v->use_ic = 0;
00962 
00963     if (v->parse_only)
00964         return 0;
00965 
00966     switch (v->s.pict_type) {
00967     case AV_PICTURE_TYPE_I:
00968     case AV_PICTURE_TYPE_BI:
00969         if (v->fcm == ILACE_FRAME) { //interlace frame picture
00970             status = bitplane_decoding(v->fieldtx_plane, &v->fieldtx_is_raw, v);
00971             if (status < 0)
00972                 return -1;
00973             av_log(v->s.avctx, AV_LOG_DEBUG, "FIELDTX plane encoding: "
00974                    "Imode: %i, Invert: %i\n", status>>1, status&1);
00975         }
00976         status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
00977         if (status < 0)
00978             return -1;
00979         av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
00980                "Imode: %i, Invert: %i\n", status>>1, status&1);
00981         v->condover = CONDOVER_NONE;
00982         if (v->overlap && v->pq <= 8) {
00983             v->condover = decode012(gb);
00984             if (v->condover == CONDOVER_SELECT) {
00985                 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
00986                 if (status < 0)
00987                     return -1;
00988                 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
00989                        "Imode: %i, Invert: %i\n", status>>1, status&1);
00990             }
00991         }
00992         break;
00993     case AV_PICTURE_TYPE_P:
00994         if (v->field_mode) {
00995             v->numref = get_bits1(gb);
00996             if (!v->numref) {
00997                 v->reffield          = get_bits1(gb);
00998                 v->ref_field_type[0] = v->reffield ^ !v->cur_field_type;
00999             }
01000         }
01001         if (v->extended_mv)
01002             v->mvrange = get_unary(gb, 0, 3);
01003         else
01004             v->mvrange = 0;
01005         if (v->interlace) {
01006             if (v->extended_dmv)
01007                 v->dmvrange = get_unary(gb, 0, 3);
01008             else
01009                 v->dmvrange = 0;
01010             if (v->fcm == ILACE_FRAME) { // interlaced frame picture
01011                 v->fourmvswitch = get_bits1(gb);
01012                 v->intcomp      = get_bits1(gb);
01013                 if (v->intcomp) {
01014                     v->lumscale = get_bits(gb, 6);
01015                     v->lumshift = get_bits(gb, 6);
01016                     INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
01017                 }
01018                 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
01019                 av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
01020                        "Imode: %i, Invert: %i\n", status>>1, status&1);
01021                 mbmodetab = get_bits(gb, 2);
01022                 if (v->fourmvswitch)
01023                     v->mbmode_vlc = &ff_vc1_intfr_4mv_mbmode_vlc[mbmodetab];
01024                 else
01025                     v->mbmode_vlc = &ff_vc1_intfr_non4mv_mbmode_vlc[mbmodetab];
01026                 imvtab         = get_bits(gb, 2);
01027                 v->imv_vlc     = &ff_vc1_1ref_mvdata_vlc[imvtab];
01028                 // interlaced p-picture cbpcy range is [1, 63]
01029                 icbptab        = get_bits(gb, 3);
01030                 v->cbpcy_vlc   = &ff_vc1_icbpcy_vlc[icbptab];
01031                 twomvbptab     = get_bits(gb, 2);
01032                 v->twomvbp_vlc = &ff_vc1_2mv_block_pattern_vlc[twomvbptab];
01033                 if (v->fourmvswitch) {
01034                     fourmvbptab     = get_bits(gb, 2);
01035                     v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
01036                 }
01037             }
01038         }
01039         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
01040         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
01041         v->range_x = 1 << (v->k_x - 1);
01042         v->range_y = 1 << (v->k_y - 1);
01043 
01044         if (v->pq < 5)
01045             v->tt_index = 0;
01046         else if (v->pq < 13)
01047             v->tt_index = 1;
01048         else
01049             v->tt_index = 2;
01050         if (v->fcm != ILACE_FRAME) {
01051             int mvmode;
01052             mvmode     = get_unary(gb, 1, 4);
01053             lowquant   = (v->pq > 12) ? 0 : 1;
01054             v->mv_mode = ff_vc1_mv_pmode_table[lowquant][mvmode];
01055             if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
01056                 int mvmode2;
01057                 mvmode2 = get_unary(gb, 1, 3);
01058                 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
01059                 if (v->field_mode)
01060                     v->intcompfield = decode210(gb);
01061                 v->lumscale = get_bits(gb, 6);
01062                 v->lumshift = get_bits(gb, 6);
01063                 INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
01064                 if ((v->field_mode) && !v->intcompfield) {
01065                     v->lumscale2 = get_bits(gb, 6);
01066                     v->lumshift2 = get_bits(gb, 6);
01067                     INIT_LUT(v->lumscale2, v->lumshift2, v->luty2, v->lutuv2);
01068                 }
01069                 v->use_ic = 1;
01070             }
01071             v->qs_last = v->s.quarter_sample;
01072             if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
01073                 v->s.quarter_sample = 0;
01074             else if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
01075                 if (v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
01076                     v->s.quarter_sample = 0;
01077                 else
01078                     v->s.quarter_sample = 1;
01079             } else
01080                 v->s.quarter_sample = 1;
01081             v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN
01082                            || (v->mv_mode == MV_PMODE_INTENSITY_COMP
01083                                && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
01084         }
01085         if (v->fcm == PROGRESSIVE) { // progressive
01086             if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
01087                  v->mv_mode2 == MV_PMODE_MIXED_MV)
01088                 || v->mv_mode == MV_PMODE_MIXED_MV) {
01089                 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
01090                 if (status < 0)
01091                     return -1;
01092                 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
01093                        "Imode: %i, Invert: %i\n", status>>1, status&1);
01094             } else {
01095                 v->mv_type_is_raw = 0;
01096                 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
01097             }
01098             status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
01099             if (status < 0)
01100                 return -1;
01101             av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
01102                    "Imode: %i, Invert: %i\n", status>>1, status&1);
01103 
01104             /* Hopefully this is correct for P frames */
01105             v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
01106             v->cbpcy_vlc        = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
01107         } else if (v->fcm == ILACE_FRAME) { // frame interlaced
01108             v->qs_last          = v->s.quarter_sample;
01109             v->s.quarter_sample = 1;
01110             v->s.mspel          = 1;
01111         } else {    // field interlaced
01112             mbmodetab = get_bits(gb, 3);
01113             imvtab = get_bits(gb, 2 + v->numref);
01114             if (!v->numref)
01115                 v->imv_vlc = &ff_vc1_1ref_mvdata_vlc[imvtab];
01116             else
01117                 v->imv_vlc = &ff_vc1_2ref_mvdata_vlc[imvtab];
01118             icbptab = get_bits(gb, 3);
01119             v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
01120             if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
01121                 v->mv_mode2 == MV_PMODE_MIXED_MV) || v->mv_mode == MV_PMODE_MIXED_MV) {
01122                 fourmvbptab     = get_bits(gb, 2);
01123                 v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
01124                 v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
01125             } else {
01126                 v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
01127             }
01128         }
01129         if (v->dquant) {
01130             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01131             vop_dquant_decoding(v);
01132         }
01133 
01134         v->ttfrm = 0; //FIXME Is that so ?
01135         if (v->vstransform) {
01136             v->ttmbf = get_bits1(gb);
01137             if (v->ttmbf) {
01138                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
01139             }
01140         } else {
01141             v->ttmbf = 1;
01142             v->ttfrm = TT_8X8;
01143         }
01144         break;
01145     case AV_PICTURE_TYPE_B:
01146         // TODO: implement interlaced frame B picture decoding
01147         if (v->fcm == ILACE_FRAME)
01148             return -1;
01149         if (v->extended_mv)
01150             v->mvrange = get_unary(gb, 0, 3);
01151         else
01152             v->mvrange = 0;
01153         v->k_x     = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
01154         v->k_y     = v->mvrange + 8; //k_y can be 8 9 10 11
01155         v->range_x = 1 << (v->k_x - 1);
01156         v->range_y = 1 << (v->k_y - 1);
01157 
01158         if (v->pq < 5)
01159             v->tt_index = 0;
01160         else if (v->pq < 13)
01161             v->tt_index = 1;
01162         else
01163             v->tt_index = 2;
01164 
01165         if (v->field_mode) {
01166             int mvmode;
01167             av_log(v->s.avctx, AV_LOG_ERROR, "B Fields do not work currently\n");
01168             return -1;
01169             if (v->extended_dmv)
01170                 v->dmvrange = get_unary(gb, 0, 3);
01171             mvmode = get_unary(gb, 1, 3);
01172             lowquant = (v->pq > 12) ? 0 : 1;
01173             v->mv_mode          = ff_vc1_mv_pmode_table2[lowquant][mvmode];
01174             v->qs_last          = v->s.quarter_sample;
01175             v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV || v->mv_mode == MV_PMODE_MIXED_MV);
01176             v->s.mspel          = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || v->mv_mode == MV_PMODE_1MV_HPEL);
01177             status = bitplane_decoding(v->forward_mb_plane, &v->fmb_is_raw, v);
01178             if (status < 0)
01179                 return -1;
01180             av_log(v->s.avctx, AV_LOG_DEBUG, "MB Forward Type plane encoding: "
01181                    "Imode: %i, Invert: %i\n", status>>1, status&1);
01182             mbmodetab = get_bits(gb, 3);
01183             if (v->mv_mode == MV_PMODE_MIXED_MV)
01184                 v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
01185             else
01186                 v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
01187             imvtab       = get_bits(gb, 3);
01188             v->imv_vlc   = &ff_vc1_2ref_mvdata_vlc[imvtab];
01189             icbptab      = get_bits(gb, 3);
01190             v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
01191             if (v->mv_mode == MV_PMODE_MIXED_MV) {
01192                 fourmvbptab     = get_bits(gb, 2);
01193                 v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
01194             }
01195             v->numref = 1; // interlaced field B pictures are always 2-ref
01196         } else {
01197             v->mv_mode          = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
01198             v->qs_last          = v->s.quarter_sample;
01199             v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
01200             v->s.mspel          = v->s.quarter_sample;
01201             status              = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
01202             if (status < 0)
01203                 return -1;
01204             av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
01205                    "Imode: %i, Invert: %i\n", status>>1, status&1);
01206             status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
01207             if (status < 0)
01208                 return -1;
01209             av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
01210                    "Imode: %i, Invert: %i\n", status>>1, status&1);
01211             v->s.mv_table_index = get_bits(gb, 2);
01212             v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
01213         }
01214 
01215         if (v->dquant) {
01216             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01217             vop_dquant_decoding(v);
01218         }
01219 
01220         v->ttfrm = 0;
01221         if (v->vstransform) {
01222             v->ttmbf = get_bits1(gb);
01223             if (v->ttmbf) {
01224                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
01225             }
01226         } else {
01227             v->ttmbf = 1;
01228             v->ttfrm = TT_8X8;
01229         }
01230         break;
01231     }
01232 
01233     /* AC Syntax */
01234     v->c_ac_table_index = decode012(gb);
01235     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
01236         v->y_ac_table_index = decode012(gb);
01237     }
01238     /* DC Syntax */
01239     v->s.dc_table_index = get_bits1(gb);
01240     if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
01241         && v->dquant) {
01242         av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01243         vop_dquant_decoding(v);
01244     }
01245 
01246     v->bi_type = 0;
01247     if (v->s.pict_type == AV_PICTURE_TYPE_BI) {
01248         v->s.pict_type = AV_PICTURE_TYPE_B;
01249         v->bi_type = 1;
01250     }
01251     return 0;
01252 }
Generated on Fri Feb 1 2013 14:34:45 for FFmpeg by doxygen 1.7.1