• 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) 2006-2007 Konstantin Shishkov
00004  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "internal.h"
00029 #include "dsputil.h"
00030 #include "avcodec.h"
00031 #include "mpegvideo.h"
00032 #include "vc1.h"
00033 #include "vc1data.h"
00034 #include "msmpeg4data.h"
00035 #include "unary.h"
00036 #include "simple_idct.h"
00037 
00038 #undef NDEBUG
00039 #include <assert.h>
00040 
00041 /***********************************************************************/
00052 enum Imode {
00053     IMODE_RAW,
00054     IMODE_NORM2,
00055     IMODE_DIFF2,
00056     IMODE_NORM6,
00057     IMODE_DIFF6,
00058     IMODE_ROWSKIP,
00059     IMODE_COLSKIP
00060 }; //imode defines
00062 
00069 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
00070     int x, y;
00071 
00072     for (y=0; y<height; y++){
00073         if (!get_bits1(gb)) //rowskip
00074             memset(plane, 0, width);
00075         else
00076             for (x=0; x<width; x++)
00077                 plane[x] = get_bits1(gb);
00078         plane += stride;
00079     }
00080 }
00081 
00089 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
00090     int x, y;
00091 
00092     for (x=0; x<width; x++){
00093         if (!get_bits1(gb)) //colskip
00094             for (y=0; y<height; y++)
00095                 plane[y*stride] = 0;
00096         else
00097             for (y=0; y<height; y++)
00098                 plane[y*stride] = get_bits1(gb);
00099         plane ++;
00100     }
00101 }
00102 
00110 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
00111 {
00112     GetBitContext *gb = &v->s.gb;
00113 
00114     int imode, x, y, code, offset;
00115     uint8_t invert, *planep = data;
00116     int width, height, stride;
00117 
00118     width = v->s.mb_width;
00119     height = v->s.mb_height;
00120     stride = v->s.mb_stride;
00121     invert = get_bits1(gb);
00122     imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
00123 
00124     *raw_flag = 0;
00125     switch (imode)
00126     {
00127     case IMODE_RAW:
00128         //Data is actually read in the MB layer (same for all tests == "raw")
00129         *raw_flag = 1; //invert ignored
00130         return invert;
00131     case IMODE_DIFF2:
00132     case IMODE_NORM2:
00133         if ((height * width) & 1)
00134         {
00135             *planep++ = get_bits1(gb);
00136             offset = 1;
00137         }
00138         else offset = 0;
00139         // decode bitplane as one long line
00140         for (y = offset; y < height * width; y += 2) {
00141             code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
00142             *planep++ = code & 1;
00143             offset++;
00144             if(offset == width) {
00145                 offset = 0;
00146                 planep += stride - width;
00147             }
00148             *planep++ = code >> 1;
00149             offset++;
00150             if(offset == width) {
00151                 offset = 0;
00152                 planep += stride - width;
00153             }
00154         }
00155         break;
00156     case IMODE_DIFF6:
00157     case IMODE_NORM6:
00158         if(!(height % 3) && (width % 3)) { // use 2x3 decoding
00159             for(y = 0; y < height; y+= 3) {
00160                 for(x = width & 1; x < width; x += 2) {
00161                     code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
00162                     if(code < 0){
00163                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
00164                         return -1;
00165                     }
00166                     planep[x + 0] = (code >> 0) & 1;
00167                     planep[x + 1] = (code >> 1) & 1;
00168                     planep[x + 0 + stride] = (code >> 2) & 1;
00169                     planep[x + 1 + stride] = (code >> 3) & 1;
00170                     planep[x + 0 + stride * 2] = (code >> 4) & 1;
00171                     planep[x + 1 + stride * 2] = (code >> 5) & 1;
00172                 }
00173                 planep += stride * 3;
00174             }
00175             if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
00176         } else { // 3x2
00177             planep += (height & 1) * stride;
00178             for(y = height & 1; y < height; y += 2) {
00179                 for(x = width % 3; x < width; x += 3) {
00180                     code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
00181                     if(code < 0){
00182                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
00183                         return -1;
00184                     }
00185                     planep[x + 0] = (code >> 0) & 1;
00186                     planep[x + 1] = (code >> 1) & 1;
00187                     planep[x + 2] = (code >> 2) & 1;
00188                     planep[x + 0 + stride] = (code >> 3) & 1;
00189                     planep[x + 1 + stride] = (code >> 4) & 1;
00190                     planep[x + 2 + stride] = (code >> 5) & 1;
00191                 }
00192                 planep += stride * 2;
00193             }
00194             x = width % 3;
00195             if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
00196             if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
00197         }
00198         break;
00199     case IMODE_ROWSKIP:
00200         decode_rowskip(data, width, height, stride, &v->s.gb);
00201         break;
00202     case IMODE_COLSKIP:
00203         decode_colskip(data, width, height, stride, &v->s.gb);
00204         break;
00205     default: break;
00206     }
00207 
00208     /* Applying diff operator */
00209     if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
00210     {
00211         planep = data;
00212         planep[0] ^= invert;
00213         for (x=1; x<width; x++)
00214             planep[x] ^= planep[x-1];
00215         for (y=1; y<height; y++)
00216         {
00217             planep += stride;
00218             planep[0] ^= planep[-stride];
00219             for (x=1; x<width; x++)
00220             {
00221                 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
00222                 else                                 planep[x] ^= planep[x-1];
00223             }
00224         }
00225     }
00226     else if (invert)
00227     {
00228         planep = data;
00229         for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
00230     }
00231     return (imode<<1) + invert;
00232 }
00233  //Bitplane group
00235 
00236 /***********************************************************************/
00240 static int vop_dquant_decoding(VC1Context *v)
00241 {
00242     GetBitContext *gb = &v->s.gb;
00243     int pqdiff;
00244 
00245     //variable size
00246     if (v->dquant == 2)
00247     {
00248         pqdiff = get_bits(gb, 3);
00249         if (pqdiff == 7) v->altpq = get_bits(gb, 5);
00250         else v->altpq = v->pq + pqdiff + 1;
00251     }
00252     else
00253     {
00254         v->dquantfrm = get_bits1(gb);
00255         if ( v->dquantfrm )
00256         {
00257             v->dqprofile = get_bits(gb, 2);
00258             switch (v->dqprofile)
00259             {
00260             case DQPROFILE_SINGLE_EDGE:
00261             case DQPROFILE_DOUBLE_EDGES:
00262                 v->dqsbedge = get_bits(gb, 2);
00263                 break;
00264             case DQPROFILE_ALL_MBS:
00265                 v->dqbilevel = get_bits1(gb);
00266                 if(!v->dqbilevel)
00267                     v->halfpq = 0;
00268             default: break; //Forbidden ?
00269             }
00270             if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
00271             {
00272                 pqdiff = get_bits(gb, 3);
00273                 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
00274                 else v->altpq = v->pq + pqdiff + 1;
00275             }
00276         }
00277     }
00278     return 0;
00279 }
00280 
00281 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
00282 
00290 int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
00291 {
00292     av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
00293     v->profile = get_bits(gb, 2);
00294     if (v->profile == PROFILE_COMPLEX)
00295     {
00296         av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
00297     }
00298 
00299     if (v->profile == PROFILE_ADVANCED)
00300     {
00301         v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
00302         v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
00303         return decode_sequence_header_adv(v, gb);
00304     }
00305     else
00306     {
00307         v->zz_8x4 = wmv2_scantableA;
00308         v->zz_4x8 = wmv2_scantableB;
00309         v->res_sm = get_bits(gb, 2); //reserved
00310         if (v->res_sm)
00311         {
00312             av_log(avctx, AV_LOG_ERROR,
00313                    "Reserved RES_SM=%i is forbidden\n", v->res_sm);
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     {
00325         av_log(avctx, AV_LOG_ERROR,
00326                "LOOPFILTER shall not be enabled in Simple Profile\n");
00327     }
00328     if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
00329         v->s.loop_filter = 0;
00330 
00331     v->res_x8 = get_bits1(gb); //reserved
00332     v->multires = get_bits1(gb);
00333     v->res_fasttx = get_bits1(gb);
00334     if (!v->res_fasttx)
00335     {
00336         v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
00337         v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
00338         v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
00339         v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
00340         v->s.dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add;
00341         v->s.dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
00342         v->s.dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
00343         v->s.dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
00344     }
00345 
00346     v->fastuvmc =  get_bits1(gb); //common
00347     if (!v->profile && !v->fastuvmc)
00348     {
00349         av_log(avctx, AV_LOG_ERROR,
00350                "FASTUVMC unavailable in Simple Profile\n");
00351         return -1;
00352     }
00353     v->extended_mv =  get_bits1(gb); //common
00354     if (!v->profile && v->extended_mv)
00355     {
00356         av_log(avctx, AV_LOG_ERROR,
00357                "Extended MVs unavailable in Simple Profile\n");
00358         return -1;
00359     }
00360     v->dquant =  get_bits(gb, 2); //common
00361     v->vstransform =  get_bits1(gb); //common
00362 
00363     v->res_transtab = get_bits1(gb);
00364     if (v->res_transtab)
00365     {
00366         av_log(avctx, AV_LOG_ERROR,
00367                "1 for reserved RES_TRANSTAB is forbidden\n");
00368         return -1;
00369     }
00370 
00371     v->overlap = get_bits1(gb); //common
00372 
00373     v->s.resync_marker = get_bits1(gb);
00374     v->rangered = get_bits1(gb);
00375     if (v->rangered && v->profile == PROFILE_SIMPLE)
00376     {
00377         av_log(avctx, AV_LOG_INFO,
00378                "RANGERED should be set to 0 in Simple Profile\n");
00379     }
00380 
00381     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
00382     v->quantizer_mode = get_bits(gb, 2); //common
00383 
00384     v->finterpflag = get_bits1(gb); //common
00385     v->res_rtm_flag = get_bits1(gb); //reserved
00386     if (!v->res_rtm_flag)
00387     {
00388 //            av_log(avctx, AV_LOG_ERROR,
00389 //                   "0 for reserved RES_RTM_FLAG is forbidden\n");
00390         av_log(avctx, AV_LOG_ERROR,
00391                "Old WMV3 version detected, only I-frames will be decoded\n");
00392         //return -1;
00393     }
00394     //TODO: figure out what they mean (always 0x402F)
00395     if(!v->res_fasttx) skip_bits(gb, 16);
00396     av_log(avctx, AV_LOG_DEBUG,
00397                "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00398                "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
00399                "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
00400                "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
00401                v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
00402                v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
00403                v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
00404                v->dquant, v->quantizer_mode, avctx->max_b_frames
00405                );
00406     return 0;
00407 }
00408 
00409 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
00410 {
00411     v->res_rtm_flag = 1;
00412     v->level = get_bits(gb, 3);
00413     if(v->level >= 5)
00414     {
00415         av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
00416     }
00417     v->chromaformat = get_bits(gb, 2);
00418     if (v->chromaformat != 1)
00419     {
00420         av_log(v->s.avctx, AV_LOG_ERROR,
00421                "Only 4:2:0 chroma format supported\n");
00422         return -1;
00423     }
00424 
00425     // (fps-2)/4 (->30)
00426     v->frmrtq_postproc = get_bits(gb, 3); //common
00427     // (bitrate-32kbps)/64kbps
00428     v->bitrtq_postproc = get_bits(gb, 5); //common
00429     v->postprocflag = get_bits1(gb); //common
00430 
00431     v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
00432     v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
00433     v->s.avctx->width = v->s.avctx->coded_width;
00434     v->s.avctx->height = v->s.avctx->coded_height;
00435     v->broadcast = get_bits1(gb);
00436     v->interlace = get_bits1(gb);
00437     v->tfcntrflag = get_bits1(gb);
00438     v->finterpflag = get_bits1(gb);
00439     skip_bits1(gb); // reserved
00440 
00441     v->s.h_edge_pos = v->s.avctx->coded_width;
00442     v->s.v_edge_pos = v->s.avctx->coded_height;
00443 
00444     av_log(v->s.avctx, AV_LOG_DEBUG,
00445                "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00446                "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
00447                "TFCTRflag=%i, FINTERPflag=%i\n",
00448                v->level, v->frmrtq_postproc, v->bitrtq_postproc,
00449                v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
00450                v->tfcntrflag, v->finterpflag
00451                );
00452 
00453     v->psf = get_bits1(gb);
00454     if(v->psf) { //PsF, 6.1.13
00455         av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
00456         return -1;
00457     }
00458     v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
00459     if(get_bits1(gb)) { //Display Info - decoding is not affected by it
00460         int w, h, ar = 0;
00461         av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
00462         v->s.avctx->width  = w = get_bits(gb, 14) + 1;
00463         v->s.avctx->height = h = get_bits(gb, 14) + 1;
00464         av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
00465         if(get_bits1(gb))
00466             ar = get_bits(gb, 4);
00467         if(ar && ar < 14){
00468             v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
00469         }else if(ar == 15){
00470             w = get_bits(gb, 8);
00471             h = get_bits(gb, 8);
00472             v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
00473         }
00474         av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n", v->s.avctx->sample_aspect_ratio.num, v->s.avctx->sample_aspect_ratio.den);
00475 
00476         if(get_bits1(gb)){ //framerate stuff
00477             if(get_bits1(gb)) {
00478                 v->s.avctx->time_base.num = 32;
00479                 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
00480             } else {
00481                 int nr, dr;
00482                 nr = get_bits(gb, 8);
00483                 dr = get_bits(gb, 4);
00484                 if(nr && nr < 8 && dr && dr < 3){
00485                     v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
00486                     v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
00487                 }
00488             }
00489         }
00490 
00491         if(get_bits1(gb)){
00492             v->color_prim = get_bits(gb, 8);
00493             v->transfer_char = get_bits(gb, 8);
00494             v->matrix_coef = get_bits(gb, 8);
00495         }
00496     }
00497 
00498     v->hrd_param_flag = get_bits1(gb);
00499     if(v->hrd_param_flag) {
00500         int i;
00501         v->hrd_num_leaky_buckets = get_bits(gb, 5);
00502         skip_bits(gb, 4); //bitrate exponent
00503         skip_bits(gb, 4); //buffer size exponent
00504         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
00505             skip_bits(gb, 16); //hrd_rate[n]
00506             skip_bits(gb, 16); //hrd_buffer[n]
00507         }
00508     }
00509     return 0;
00510 }
00511 
00512 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
00513 {
00514     int i;
00515 
00516     av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
00517     v->broken_link = get_bits1(gb);
00518     v->closed_entry = get_bits1(gb);
00519     v->panscanflag = get_bits1(gb);
00520     v->refdist_flag = get_bits1(gb);
00521     v->s.loop_filter = get_bits1(gb);
00522     v->fastuvmc = get_bits1(gb);
00523     v->extended_mv = get_bits1(gb);
00524     v->dquant = get_bits(gb, 2);
00525     v->vstransform = get_bits1(gb);
00526     v->overlap = get_bits1(gb);
00527     v->quantizer_mode = get_bits(gb, 2);
00528 
00529     if(v->hrd_param_flag){
00530         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
00531             skip_bits(gb, 8); //hrd_full[n]
00532         }
00533     }
00534 
00535     if(get_bits1(gb)){
00536         avctx->coded_width = (get_bits(gb, 12)+1)<<1;
00537         avctx->coded_height = (get_bits(gb, 12)+1)<<1;
00538     }
00539     if(v->extended_mv)
00540         v->extended_dmv = get_bits1(gb);
00541     if((v->range_mapy_flag = get_bits1(gb))) {
00542         av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
00543         v->range_mapy = get_bits(gb, 3);
00544     }
00545     if((v->range_mapuv_flag = get_bits1(gb))) {
00546         av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
00547         v->range_mapuv = get_bits(gb, 3);
00548     }
00549 
00550     av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
00551         "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
00552         "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
00553         "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
00554         v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
00555         v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
00556 
00557     return 0;
00558 }
00559 
00560 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
00561 {
00562     int pqindex, lowquant, status;
00563 
00564     if(v->finterpflag) v->interpfrm = get_bits1(gb);
00565     skip_bits(gb, 2); //framecnt unused
00566     v->rangeredfrm = 0;
00567     if (v->rangered) v->rangeredfrm = get_bits1(gb);
00568     v->s.pict_type = get_bits1(gb);
00569     if (v->s.avctx->max_b_frames) {
00570         if (!v->s.pict_type) {
00571             if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
00572             else v->s.pict_type = FF_B_TYPE;
00573         } else v->s.pict_type = FF_P_TYPE;
00574     } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
00575 
00576     v->bi_type = 0;
00577     if(v->s.pict_type == FF_B_TYPE) {
00578         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00579         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00580         if(v->bfraction == 0) {
00581             v->s.pict_type = FF_BI_TYPE;
00582         }
00583     }
00584     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
00585         skip_bits(gb, 7); // skip buffer fullness
00586 
00587     if(v->parse_only)
00588         return 0;
00589 
00590     /* calculate RND */
00591     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
00592         v->rnd = 1;
00593     if(v->s.pict_type == FF_P_TYPE)
00594         v->rnd ^= 1;
00595 
00596     /* Quantizer stuff */
00597     pqindex = get_bits(gb, 5);
00598     if(!pqindex) return -1;
00599     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00600         v->pq = ff_vc1_pquant_table[0][pqindex];
00601     else
00602         v->pq = ff_vc1_pquant_table[1][pqindex];
00603 
00604     v->pquantizer = 1;
00605     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00606         v->pquantizer = pqindex < 9;
00607     if (v->quantizer_mode == QUANT_NON_UNIFORM)
00608         v->pquantizer = 0;
00609     v->pqindex = pqindex;
00610     if (pqindex < 9) v->halfpq = get_bits1(gb);
00611     else v->halfpq = 0;
00612     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00613         v->pquantizer = get_bits1(gb);
00614     v->dquantfrm = 0;
00615     if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
00616     v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00617     v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00618     v->range_x = 1 << (v->k_x - 1);
00619     v->range_y = 1 << (v->k_y - 1);
00620     if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
00621 
00622     if(v->res_x8 && (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)){
00623         v->x8_type = get_bits1(gb);
00624     }else v->x8_type = 0;
00625 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
00626 //        (v->s.pict_type == FF_P_TYPE) ? 'P' : ((v->s.pict_type == FF_I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
00627 
00628     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
00629 
00630     switch(v->s.pict_type) {
00631     case FF_P_TYPE:
00632         if (v->pq < 5) v->tt_index = 0;
00633         else if(v->pq < 13) v->tt_index = 1;
00634         else v->tt_index = 2;
00635 
00636         lowquant = (v->pq > 12) ? 0 : 1;
00637         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00638         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00639         {
00640             int scale, shift, i;
00641             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00642             v->lumscale = get_bits(gb, 6);
00643             v->lumshift = get_bits(gb, 6);
00644             v->use_ic = 1;
00645             /* fill lookup tables for intensity compensation */
00646             if(!v->lumscale) {
00647                 scale = -64;
00648                 shift = (255 - v->lumshift * 2) << 6;
00649                 if(v->lumshift > 31)
00650                     shift += 128 << 6;
00651             } else {
00652                 scale = v->lumscale + 32;
00653                 if(v->lumshift > 31)
00654                     shift = (v->lumshift - 64) << 6;
00655                 else
00656                     shift = v->lumshift << 6;
00657             }
00658             for(i = 0; i < 256; i++) {
00659                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
00660                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00661             }
00662         }
00663         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00664             v->s.quarter_sample = 0;
00665         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00666             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00667                 v->s.quarter_sample = 0;
00668             else
00669                 v->s.quarter_sample = 1;
00670         } else
00671             v->s.quarter_sample = 1;
00672         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));
00673 
00674         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00675                  v->mv_mode2 == MV_PMODE_MIXED_MV)
00676                 || v->mv_mode == MV_PMODE_MIXED_MV)
00677         {
00678             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00679             if (status < 0) return -1;
00680             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00681                    "Imode: %i, Invert: %i\n", status>>1, status&1);
00682         } else {
00683             v->mv_type_is_raw = 0;
00684             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00685         }
00686         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00687         if (status < 0) return -1;
00688         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00689                "Imode: %i, Invert: %i\n", status>>1, status&1);
00690 
00691         /* Hopefully this is correct for P frames */
00692         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
00693         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00694 
00695         if (v->dquant)
00696         {
00697             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00698             vop_dquant_decoding(v);
00699         }
00700 
00701         v->ttfrm = 0; //FIXME Is that so ?
00702         if (v->vstransform)
00703         {
00704             v->ttmbf = get_bits1(gb);
00705             if (v->ttmbf)
00706             {
00707                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00708             }
00709         } else {
00710             v->ttmbf = 1;
00711             v->ttfrm = TT_8X8;
00712         }
00713         break;
00714     case FF_B_TYPE:
00715         if (v->pq < 5) v->tt_index = 0;
00716         else if(v->pq < 13) v->tt_index = 1;
00717         else v->tt_index = 2;
00718 
00719         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00720         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
00721         v->s.mspel = v->s.quarter_sample;
00722 
00723         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
00724         if (status < 0) return -1;
00725         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
00726                "Imode: %i, Invert: %i\n", status>>1, status&1);
00727         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00728         if (status < 0) return -1;
00729         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00730                "Imode: %i, Invert: %i\n", status>>1, status&1);
00731 
00732         v->s.mv_table_index = get_bits(gb, 2);
00733         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00734 
00735         if (v->dquant)
00736         {
00737             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00738             vop_dquant_decoding(v);
00739         }
00740 
00741         v->ttfrm = 0;
00742         if (v->vstransform)
00743         {
00744             v->ttmbf = get_bits1(gb);
00745             if (v->ttmbf)
00746             {
00747                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00748             }
00749         } else {
00750             v->ttmbf = 1;
00751             v->ttfrm = TT_8X8;
00752         }
00753         break;
00754     }
00755 
00756     if(!v->x8_type)
00757     {
00758         /* AC Syntax */
00759         v->c_ac_table_index = decode012(gb);
00760         if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
00761         {
00762             v->y_ac_table_index = decode012(gb);
00763         }
00764         /* DC Syntax */
00765         v->s.dc_table_index = get_bits1(gb);
00766     }
00767 
00768     if(v->s.pict_type == FF_BI_TYPE) {
00769         v->s.pict_type = FF_B_TYPE;
00770         v->bi_type = 1;
00771     }
00772     return 0;
00773 }
00774 
00775 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
00776 {
00777     int pqindex, lowquant;
00778     int status;
00779 
00780     v->p_frame_skipped = 0;
00781 
00782     if(v->interlace){
00783         v->fcm = decode012(gb);
00784         if(v->fcm){
00785             if(!v->warn_interlaced++)
00786                 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
00787             return -1;
00788         }
00789     }
00790     switch(get_unary(gb, 0, 4)) {
00791     case 0:
00792         v->s.pict_type = FF_P_TYPE;
00793         break;
00794     case 1:
00795         v->s.pict_type = FF_B_TYPE;
00796         break;
00797     case 2:
00798         v->s.pict_type = FF_I_TYPE;
00799         break;
00800     case 3:
00801         v->s.pict_type = FF_BI_TYPE;
00802         break;
00803     case 4:
00804         v->s.pict_type = FF_P_TYPE; // skipped pic
00805         v->p_frame_skipped = 1;
00806         return 0;
00807     }
00808     if(v->tfcntrflag)
00809         skip_bits(gb, 8);
00810     if(v->broadcast) {
00811         if(!v->interlace || v->psf) {
00812             v->rptfrm = get_bits(gb, 2);
00813         } else {
00814             v->tff = get_bits1(gb);
00815             v->rptfrm = get_bits1(gb);
00816         }
00817     }
00818     if(v->panscanflag) {
00819         //...
00820     }
00821     v->rnd = get_bits1(gb);
00822     if(v->interlace)
00823         v->uvsamp = get_bits1(gb);
00824     if(v->finterpflag) v->interpfrm = get_bits1(gb);
00825     if(v->s.pict_type == FF_B_TYPE) {
00826         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00827         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00828         if(v->bfraction == 0) {
00829             v->s.pict_type = FF_BI_TYPE; /* XXX: should not happen here */
00830         }
00831     }
00832     pqindex = get_bits(gb, 5);
00833     if(!pqindex) return -1;
00834     v->pqindex = pqindex;
00835     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00836         v->pq = ff_vc1_pquant_table[0][pqindex];
00837     else
00838         v->pq = ff_vc1_pquant_table[1][pqindex];
00839 
00840     v->pquantizer = 1;
00841     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00842         v->pquantizer = pqindex < 9;
00843     if (v->quantizer_mode == QUANT_NON_UNIFORM)
00844         v->pquantizer = 0;
00845     v->pqindex = pqindex;
00846     if (pqindex < 9) v->halfpq = get_bits1(gb);
00847     else v->halfpq = 0;
00848     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00849         v->pquantizer = get_bits1(gb);
00850     if(v->postprocflag)
00851         v->postproc = get_bits(gb, 2);
00852 
00853     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
00854 
00855     if(v->parse_only)
00856         return 0;
00857 
00858     switch(v->s.pict_type) {
00859     case FF_I_TYPE:
00860     case FF_BI_TYPE:
00861         status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
00862         if (status < 0) return -1;
00863         av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
00864                 "Imode: %i, Invert: %i\n", status>>1, status&1);
00865         v->condover = CONDOVER_NONE;
00866         if(v->overlap && v->pq <= 8) {
00867             v->condover = decode012(gb);
00868             if(v->condover == CONDOVER_SELECT) {
00869                 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
00870                 if (status < 0) return -1;
00871                 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
00872                         "Imode: %i, Invert: %i\n", status>>1, status&1);
00873             }
00874         }
00875         break;
00876     case FF_P_TYPE:
00877         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
00878         else v->mvrange = 0;
00879         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00880         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00881         v->range_x = 1 << (v->k_x - 1);
00882         v->range_y = 1 << (v->k_y - 1);
00883 
00884         if (v->pq < 5) v->tt_index = 0;
00885         else if(v->pq < 13) v->tt_index = 1;
00886         else v->tt_index = 2;
00887 
00888         lowquant = (v->pq > 12) ? 0 : 1;
00889         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00890         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00891         {
00892             int scale, shift, i;
00893             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00894             v->lumscale = get_bits(gb, 6);
00895             v->lumshift = get_bits(gb, 6);
00896             /* fill lookup tables for intensity compensation */
00897             if(!v->lumscale) {
00898                 scale = -64;
00899                 shift = (255 - v->lumshift * 2) << 6;
00900                 if(v->lumshift > 31)
00901                     shift += 128 << 6;
00902             } else {
00903                 scale = v->lumscale + 32;
00904                 if(v->lumshift > 31)
00905                     shift = (v->lumshift - 64) << 6;
00906                 else
00907                     shift = v->lumshift << 6;
00908             }
00909             for(i = 0; i < 256; i++) {
00910                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
00911                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00912             }
00913             v->use_ic = 1;
00914         }
00915         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00916             v->s.quarter_sample = 0;
00917         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00918             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00919                 v->s.quarter_sample = 0;
00920             else
00921                 v->s.quarter_sample = 1;
00922         } else
00923             v->s.quarter_sample = 1;
00924         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));
00925 
00926         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00927                  v->mv_mode2 == MV_PMODE_MIXED_MV)
00928                 || v->mv_mode == MV_PMODE_MIXED_MV)
00929         {
00930             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00931             if (status < 0) return -1;
00932             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00933                    "Imode: %i, Invert: %i\n", status>>1, status&1);
00934         } else {
00935             v->mv_type_is_raw = 0;
00936             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00937         }
00938         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00939         if (status < 0) return -1;
00940         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00941                "Imode: %i, Invert: %i\n", status>>1, status&1);
00942 
00943         /* Hopefully this is correct for P frames */
00944         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
00945         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00946         if (v->dquant)
00947         {
00948             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00949             vop_dquant_decoding(v);
00950         }
00951 
00952         v->ttfrm = 0; //FIXME Is that so ?
00953         if (v->vstransform)
00954         {
00955             v->ttmbf = get_bits1(gb);
00956             if (v->ttmbf)
00957             {
00958                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00959             }
00960         } else {
00961             v->ttmbf = 1;
00962             v->ttfrm = TT_8X8;
00963         }
00964         break;
00965     case FF_B_TYPE:
00966         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
00967         else v->mvrange = 0;
00968         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00969         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00970         v->range_x = 1 << (v->k_x - 1);
00971         v->range_y = 1 << (v->k_y - 1);
00972 
00973         if (v->pq < 5) v->tt_index = 0;
00974         else if(v->pq < 13) v->tt_index = 1;
00975         else v->tt_index = 2;
00976 
00977         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00978         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
00979         v->s.mspel = v->s.quarter_sample;
00980 
00981         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
00982         if (status < 0) return -1;
00983         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
00984                "Imode: %i, Invert: %i\n", status>>1, status&1);
00985         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00986         if (status < 0) return -1;
00987         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00988                "Imode: %i, Invert: %i\n", status>>1, status&1);
00989 
00990         v->s.mv_table_index = get_bits(gb, 2);
00991         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00992 
00993         if (v->dquant)
00994         {
00995             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00996             vop_dquant_decoding(v);
00997         }
00998 
00999         v->ttfrm = 0;
01000         if (v->vstransform)
01001         {
01002             v->ttmbf = get_bits1(gb);
01003             if (v->ttmbf)
01004             {
01005                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
01006             }
01007         } else {
01008             v->ttmbf = 1;
01009             v->ttfrm = TT_8X8;
01010         }
01011         break;
01012     }
01013 
01014     /* AC Syntax */
01015     v->c_ac_table_index = decode012(gb);
01016     if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
01017     {
01018         v->y_ac_table_index = decode012(gb);
01019     }
01020     /* DC Syntax */
01021     v->s.dc_table_index = get_bits1(gb);
01022     if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
01023         av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01024         vop_dquant_decoding(v);
01025     }
01026 
01027     v->bi_type = 0;
01028     if(v->s.pict_type == FF_BI_TYPE) {
01029         v->s.pict_type = FF_B_TYPE;
01030         v->bi_type = 1;
01031     }
01032     return 0;
01033 }

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