• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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_WARNING, "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_y411   = get_bits1(gb);
00310         v->res_sprite = get_bits1(gb);
00311         if (v->res_y411)
00312         {
00313             av_log(avctx, AV_LOG_ERROR,
00314                    "Old interlaced mode is not supported\n");
00315             return -1;
00316         }
00317         if (v->res_sprite) {
00318             av_log(avctx, AV_LOG_ERROR, "WMVP is not fully supported\n");
00319         }
00320     }
00321 
00322     // (fps-2)/4 (->30)
00323     v->frmrtq_postproc = get_bits(gb, 3); //common
00324     // (bitrate-32kbps)/64kbps
00325     v->bitrtq_postproc = get_bits(gb, 5); //common
00326     v->s.loop_filter = get_bits1(gb); //common
00327     if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
00328     {
00329         av_log(avctx, AV_LOG_ERROR,
00330                "LOOPFILTER shall not be enabled in Simple Profile\n");
00331     }
00332     if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
00333         v->s.loop_filter = 0;
00334 
00335     v->res_x8 = get_bits1(gb); //reserved
00336     v->multires = get_bits1(gb);
00337     v->res_fasttx = get_bits1(gb);
00338     if (!v->res_fasttx)
00339     {
00340         v->vc1dsp.vc1_inv_trans_8x8 = ff_simple_idct;
00341         v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
00342         v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
00343         v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
00344         v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add;
00345         v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
00346         v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
00347         v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
00348     }
00349 
00350     v->fastuvmc =  get_bits1(gb); //common
00351     if (!v->profile && !v->fastuvmc)
00352     {
00353         av_log(avctx, AV_LOG_ERROR,
00354                "FASTUVMC unavailable in Simple Profile\n");
00355         return -1;
00356     }
00357     v->extended_mv =  get_bits1(gb); //common
00358     if (!v->profile && v->extended_mv)
00359     {
00360         av_log(avctx, AV_LOG_ERROR,
00361                "Extended MVs unavailable in Simple Profile\n");
00362         return -1;
00363     }
00364     v->dquant =  get_bits(gb, 2); //common
00365     v->vstransform =  get_bits1(gb); //common
00366 
00367     v->res_transtab = get_bits1(gb);
00368     if (v->res_transtab)
00369     {
00370         av_log(avctx, AV_LOG_ERROR,
00371                "1 for reserved RES_TRANSTAB is forbidden\n");
00372         return -1;
00373     }
00374 
00375     v->overlap = get_bits1(gb); //common
00376 
00377     v->s.resync_marker = get_bits1(gb);
00378     v->rangered = get_bits1(gb);
00379     if (v->rangered && v->profile == PROFILE_SIMPLE)
00380     {
00381         av_log(avctx, AV_LOG_INFO,
00382                "RANGERED should be set to 0 in Simple Profile\n");
00383     }
00384 
00385     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
00386     v->quantizer_mode = get_bits(gb, 2); //common
00387 
00388     v->finterpflag = get_bits1(gb); //common
00389 
00390     if (v->res_sprite) {
00391         v->s.avctx->width  = v->s.avctx->coded_width  = get_bits(gb, 11);
00392         v->s.avctx->height = v->s.avctx->coded_height = get_bits(gb, 11);
00393         skip_bits(gb, 5); //frame rate
00394         v->res_x8 = get_bits1(gb);
00395         if (get_bits1(gb)) { // something to do with DC VLC selection
00396             av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
00397             return -1;
00398         }
00399         skip_bits(gb, 3); //slice code
00400         v->res_rtm_flag = 0;
00401     } else {
00402         v->res_rtm_flag = get_bits1(gb); //reserved
00403     }
00404     if (!v->res_rtm_flag)
00405     {
00406 //            av_log(avctx, AV_LOG_ERROR,
00407 //                   "0 for reserved RES_RTM_FLAG is forbidden\n");
00408         av_log(avctx, AV_LOG_ERROR,
00409                "Old WMV3 version detected, some frames may be decoded incorrectly\n");
00410         //return -1;
00411     }
00412     //TODO: figure out what they mean (always 0x402F)
00413     if(!v->res_fasttx) skip_bits(gb, 16);
00414     av_log(avctx, AV_LOG_DEBUG,
00415                "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00416                "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
00417                "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
00418                "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
00419                v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
00420                v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
00421                v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
00422                v->dquant, v->quantizer_mode, avctx->max_b_frames
00423                );
00424     return 0;
00425 }
00426 
00427 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
00428 {
00429     v->res_rtm_flag = 1;
00430     v->level = get_bits(gb, 3);
00431     if(v->level >= 5)
00432     {
00433         av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
00434     }
00435     v->chromaformat = get_bits(gb, 2);
00436     if (v->chromaformat != 1)
00437     {
00438         av_log(v->s.avctx, AV_LOG_ERROR,
00439                "Only 4:2:0 chroma format supported\n");
00440         return -1;
00441     }
00442 
00443     // (fps-2)/4 (->30)
00444     v->frmrtq_postproc = get_bits(gb, 3); //common
00445     // (bitrate-32kbps)/64kbps
00446     v->bitrtq_postproc = get_bits(gb, 5); //common
00447     v->postprocflag = get_bits1(gb); //common
00448 
00449     v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
00450     v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
00451     v->s.avctx->width = v->s.avctx->coded_width;
00452     v->s.avctx->height = v->s.avctx->coded_height;
00453     v->broadcast = get_bits1(gb);
00454     v->interlace = get_bits1(gb);
00455     v->tfcntrflag = get_bits1(gb);
00456     v->finterpflag = get_bits1(gb);
00457     skip_bits1(gb); // reserved
00458 
00459     v->s.h_edge_pos = v->s.avctx->coded_width;
00460     v->s.v_edge_pos = v->s.avctx->coded_height;
00461 
00462     av_log(v->s.avctx, AV_LOG_DEBUG,
00463                "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00464                "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
00465                "TFCTRflag=%i, FINTERPflag=%i\n",
00466                v->level, v->frmrtq_postproc, v->bitrtq_postproc,
00467                v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
00468                v->tfcntrflag, v->finterpflag
00469                );
00470 
00471     v->psf = get_bits1(gb);
00472     if(v->psf) { //PsF, 6.1.13
00473         av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
00474         return -1;
00475     }
00476     v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
00477     if(get_bits1(gb)) { //Display Info - decoding is not affected by it
00478         int w, h, ar = 0;
00479         av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
00480         v->s.avctx->width  = w = get_bits(gb, 14) + 1;
00481         v->s.avctx->height = h = get_bits(gb, 14) + 1;
00482         av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
00483         if(get_bits1(gb))
00484             ar = get_bits(gb, 4);
00485         if(ar && ar < 14){
00486             v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
00487         }else if(ar == 15){
00488             w = get_bits(gb, 8) + 1;
00489             h = get_bits(gb, 8) + 1;
00490             v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
00491         }
00492         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);
00493 
00494         if(get_bits1(gb)){ //framerate stuff
00495             if(get_bits1(gb)) {
00496                 v->s.avctx->time_base.num = 32;
00497                 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
00498             } else {
00499                 int nr, dr;
00500                 nr = get_bits(gb, 8);
00501                 dr = get_bits(gb, 4);
00502                 if(nr && nr < 8 && dr && dr < 3){
00503                     v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
00504                     v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
00505                 }
00506             }
00507         }
00508 
00509         if(get_bits1(gb)){
00510             v->color_prim = get_bits(gb, 8);
00511             v->transfer_char = get_bits(gb, 8);
00512             v->matrix_coef = get_bits(gb, 8);
00513         }
00514     }
00515 
00516     v->hrd_param_flag = get_bits1(gb);
00517     if(v->hrd_param_flag) {
00518         int i;
00519         v->hrd_num_leaky_buckets = get_bits(gb, 5);
00520         skip_bits(gb, 4); //bitrate exponent
00521         skip_bits(gb, 4); //buffer size exponent
00522         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
00523             skip_bits(gb, 16); //hrd_rate[n]
00524             skip_bits(gb, 16); //hrd_buffer[n]
00525         }
00526     }
00527     return 0;
00528 }
00529 
00530 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
00531 {
00532     int i;
00533 
00534     av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
00535     v->broken_link = get_bits1(gb);
00536     v->closed_entry = get_bits1(gb);
00537     v->panscanflag = get_bits1(gb);
00538     v->refdist_flag = get_bits1(gb);
00539     v->s.loop_filter = get_bits1(gb);
00540     v->fastuvmc = get_bits1(gb);
00541     v->extended_mv = get_bits1(gb);
00542     v->dquant = get_bits(gb, 2);
00543     v->vstransform = get_bits1(gb);
00544     v->overlap = get_bits1(gb);
00545     v->quantizer_mode = get_bits(gb, 2);
00546 
00547     if(v->hrd_param_flag){
00548         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
00549             skip_bits(gb, 8); //hrd_full[n]
00550         }
00551     }
00552 
00553     if(get_bits1(gb)){
00554         avctx->coded_width = (get_bits(gb, 12)+1)<<1;
00555         avctx->coded_height = (get_bits(gb, 12)+1)<<1;
00556     }
00557     if(v->extended_mv)
00558         v->extended_dmv = get_bits1(gb);
00559     if((v->range_mapy_flag = get_bits1(gb))) {
00560         av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
00561         v->range_mapy = get_bits(gb, 3);
00562     }
00563     if((v->range_mapuv_flag = get_bits1(gb))) {
00564         av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
00565         v->range_mapuv = get_bits(gb, 3);
00566     }
00567 
00568     av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
00569         "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
00570         "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
00571         "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
00572         v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
00573         v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
00574 
00575     return 0;
00576 }
00577 
00578 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
00579 {
00580     int pqindex, lowquant, status;
00581 
00582     if(v->finterpflag) v->interpfrm = get_bits1(gb);
00583     skip_bits(gb, 2); //framecnt unused
00584     v->rangeredfrm = 0;
00585     if (v->rangered) v->rangeredfrm = get_bits1(gb);
00586     v->s.pict_type = get_bits1(gb);
00587     if (v->s.avctx->max_b_frames) {
00588         if (!v->s.pict_type) {
00589             if (get_bits1(gb)) v->s.pict_type = AV_PICTURE_TYPE_I;
00590             else v->s.pict_type = AV_PICTURE_TYPE_B;
00591         } else v->s.pict_type = AV_PICTURE_TYPE_P;
00592     } else v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00593 
00594     v->bi_type = 0;
00595     if(v->s.pict_type == AV_PICTURE_TYPE_B) {
00596         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00597         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00598         if(v->bfraction == 0) {
00599             v->s.pict_type = AV_PICTURE_TYPE_BI;
00600         }
00601     }
00602     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
00603         skip_bits(gb, 7); // skip buffer fullness
00604 
00605     if(v->parse_only)
00606         return 0;
00607 
00608     /* calculate RND */
00609     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
00610         v->rnd = 1;
00611     if(v->s.pict_type == AV_PICTURE_TYPE_P)
00612         v->rnd ^= 1;
00613 
00614     /* Quantizer stuff */
00615     pqindex = get_bits(gb, 5);
00616     if(!pqindex) return -1;
00617     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00618         v->pq = ff_vc1_pquant_table[0][pqindex];
00619     else
00620         v->pq = ff_vc1_pquant_table[1][pqindex];
00621 
00622     v->pquantizer = 1;
00623     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00624         v->pquantizer = pqindex < 9;
00625     if (v->quantizer_mode == QUANT_NON_UNIFORM)
00626         v->pquantizer = 0;
00627     v->pqindex = pqindex;
00628     if (pqindex < 9) v->halfpq = get_bits1(gb);
00629     else v->halfpq = 0;
00630     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00631         v->pquantizer = get_bits1(gb);
00632     v->dquantfrm = 0;
00633     if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
00634     v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00635     v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00636     v->range_x = 1 << (v->k_x - 1);
00637     v->range_y = 1 << (v->k_y - 1);
00638     if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B) v->respic = get_bits(gb, 2);
00639 
00640     if(v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)){
00641         v->x8_type = get_bits1(gb);
00642     }else v->x8_type = 0;
00643 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
00644 //        (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);
00645 
00646     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
00647 
00648     switch(v->s.pict_type) {
00649     case AV_PICTURE_TYPE_P:
00650         if (v->pq < 5) v->tt_index = 0;
00651         else if(v->pq < 13) v->tt_index = 1;
00652         else v->tt_index = 2;
00653 
00654         lowquant = (v->pq > 12) ? 0 : 1;
00655         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00656         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00657         {
00658             int scale, shift, i;
00659             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00660             v->lumscale = get_bits(gb, 6);
00661             v->lumshift = get_bits(gb, 6);
00662             v->use_ic = 1;
00663             /* fill lookup tables for intensity compensation */
00664             if(!v->lumscale) {
00665                 scale = -64;
00666                 shift = (255 - v->lumshift * 2) << 6;
00667                 if(v->lumshift > 31)
00668                     shift += 128 << 6;
00669             } else {
00670                 scale = v->lumscale + 32;
00671                 if(v->lumshift > 31)
00672                     shift = (v->lumshift - 64) << 6;
00673                 else
00674                     shift = v->lumshift << 6;
00675             }
00676             for(i = 0; i < 256; i++) {
00677                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
00678                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00679             }
00680         }
00681         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00682             v->s.quarter_sample = 0;
00683         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00684             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00685                 v->s.quarter_sample = 0;
00686             else
00687                 v->s.quarter_sample = 1;
00688         } else
00689             v->s.quarter_sample = 1;
00690         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));
00691 
00692         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00693                  v->mv_mode2 == MV_PMODE_MIXED_MV)
00694                 || v->mv_mode == MV_PMODE_MIXED_MV)
00695         {
00696             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00697             if (status < 0) return -1;
00698             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00699                    "Imode: %i, Invert: %i\n", status>>1, status&1);
00700         } else {
00701             v->mv_type_is_raw = 0;
00702             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00703         }
00704         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00705         if (status < 0) return -1;
00706         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00707                "Imode: %i, Invert: %i\n", status>>1, status&1);
00708 
00709         /* Hopefully this is correct for P frames */
00710         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
00711         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00712 
00713         if (v->dquant)
00714         {
00715             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00716             vop_dquant_decoding(v);
00717         }
00718 
00719         v->ttfrm = 0; //FIXME Is that so ?
00720         if (v->vstransform)
00721         {
00722             v->ttmbf = get_bits1(gb);
00723             if (v->ttmbf)
00724             {
00725                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00726             }
00727         } else {
00728             v->ttmbf = 1;
00729             v->ttfrm = TT_8X8;
00730         }
00731         break;
00732     case AV_PICTURE_TYPE_B:
00733         if (v->pq < 5) v->tt_index = 0;
00734         else if(v->pq < 13) v->tt_index = 1;
00735         else v->tt_index = 2;
00736 
00737         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00738         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
00739         v->s.mspel = v->s.quarter_sample;
00740 
00741         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
00742         if (status < 0) return -1;
00743         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
00744                "Imode: %i, Invert: %i\n", status>>1, status&1);
00745         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00746         if (status < 0) return -1;
00747         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00748                "Imode: %i, Invert: %i\n", status>>1, status&1);
00749 
00750         v->s.mv_table_index = get_bits(gb, 2);
00751         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00752 
00753         if (v->dquant)
00754         {
00755             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00756             vop_dquant_decoding(v);
00757         }
00758 
00759         v->ttfrm = 0;
00760         if (v->vstransform)
00761         {
00762             v->ttmbf = get_bits1(gb);
00763             if (v->ttmbf)
00764             {
00765                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00766             }
00767         } else {
00768             v->ttmbf = 1;
00769             v->ttfrm = TT_8X8;
00770         }
00771         break;
00772     }
00773 
00774     if(!v->x8_type)
00775     {
00776         /* AC Syntax */
00777         v->c_ac_table_index = decode012(gb);
00778         if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
00779         {
00780             v->y_ac_table_index = decode012(gb);
00781         }
00782         /* DC Syntax */
00783         v->s.dc_table_index = get_bits1(gb);
00784     }
00785 
00786     if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
00787         v->s.pict_type = AV_PICTURE_TYPE_B;
00788         v->bi_type = 1;
00789     }
00790     return 0;
00791 }
00792 
00793 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
00794 {
00795     int pqindex, lowquant;
00796     int status;
00797 
00798     v->p_frame_skipped = 0;
00799 
00800     if(v->interlace){
00801         v->fcm = decode012(gb);
00802         if(v->fcm){
00803             if(!v->warn_interlaced++)
00804                 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
00805             return -1;
00806         }
00807     }
00808     switch(get_unary(gb, 0, 4)) {
00809     case 0:
00810         v->s.pict_type = AV_PICTURE_TYPE_P;
00811         break;
00812     case 1:
00813         v->s.pict_type = AV_PICTURE_TYPE_B;
00814         break;
00815     case 2:
00816         v->s.pict_type = AV_PICTURE_TYPE_I;
00817         break;
00818     case 3:
00819         v->s.pict_type = AV_PICTURE_TYPE_BI;
00820         break;
00821     case 4:
00822         v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
00823         v->p_frame_skipped = 1;
00824         return 0;
00825     }
00826     if(v->tfcntrflag)
00827         skip_bits(gb, 8);
00828     if(v->broadcast) {
00829         if(!v->interlace || v->psf) {
00830             v->rptfrm = get_bits(gb, 2);
00831         } else {
00832             v->tff = get_bits1(gb);
00833             v->rff = get_bits1(gb);
00834         }
00835     }
00836     if(v->panscanflag) {
00837         av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
00838         //...
00839     }
00840     v->rnd = get_bits1(gb);
00841     if(v->interlace)
00842         v->uvsamp = get_bits1(gb);
00843     if(v->finterpflag) v->interpfrm = get_bits1(gb);
00844     if(v->s.pict_type == AV_PICTURE_TYPE_B) {
00845         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00846         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00847         if(v->bfraction == 0) {
00848             v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
00849         }
00850     }
00851     pqindex = get_bits(gb, 5);
00852     if(!pqindex) return -1;
00853     v->pqindex = pqindex;
00854     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00855         v->pq = ff_vc1_pquant_table[0][pqindex];
00856     else
00857         v->pq = ff_vc1_pquant_table[1][pqindex];
00858 
00859     v->pquantizer = 1;
00860     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00861         v->pquantizer = pqindex < 9;
00862     if (v->quantizer_mode == QUANT_NON_UNIFORM)
00863         v->pquantizer = 0;
00864     v->pqindex = pqindex;
00865     if (pqindex < 9) v->halfpq = get_bits1(gb);
00866     else v->halfpq = 0;
00867     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00868         v->pquantizer = get_bits1(gb);
00869     if(v->postprocflag)
00870         v->postproc = get_bits(gb, 2);
00871 
00872     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
00873 
00874     if(v->parse_only)
00875         return 0;
00876 
00877     switch(v->s.pict_type) {
00878     case AV_PICTURE_TYPE_I:
00879     case AV_PICTURE_TYPE_BI:
00880         status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
00881         if (status < 0) return -1;
00882         av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
00883                 "Imode: %i, Invert: %i\n", status>>1, status&1);
00884         v->condover = CONDOVER_NONE;
00885         if(v->overlap && v->pq <= 8) {
00886             v->condover = decode012(gb);
00887             if(v->condover == CONDOVER_SELECT) {
00888                 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
00889                 if (status < 0) return -1;
00890                 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
00891                         "Imode: %i, Invert: %i\n", status>>1, status&1);
00892             }
00893         }
00894         break;
00895     case AV_PICTURE_TYPE_P:
00896         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
00897         else v->mvrange = 0;
00898         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00899         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00900         v->range_x = 1 << (v->k_x - 1);
00901         v->range_y = 1 << (v->k_y - 1);
00902 
00903         if (v->pq < 5) v->tt_index = 0;
00904         else if(v->pq < 13) v->tt_index = 1;
00905         else v->tt_index = 2;
00906 
00907         lowquant = (v->pq > 12) ? 0 : 1;
00908         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00909         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00910         {
00911             int scale, shift, i;
00912             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00913             v->lumscale = get_bits(gb, 6);
00914             v->lumshift = get_bits(gb, 6);
00915             /* fill lookup tables for intensity compensation */
00916             if(!v->lumscale) {
00917                 scale = -64;
00918                 shift = (255 - v->lumshift * 2) << 6;
00919                 if(v->lumshift > 31)
00920                     shift += 128 << 6;
00921             } else {
00922                 scale = v->lumscale + 32;
00923                 if(v->lumshift > 31)
00924                     shift = (v->lumshift - 64) << 6;
00925                 else
00926                     shift = v->lumshift << 6;
00927             }
00928             for(i = 0; i < 256; i++) {
00929                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
00930                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00931             }
00932             v->use_ic = 1;
00933         }
00934         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00935             v->s.quarter_sample = 0;
00936         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00937             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00938                 v->s.quarter_sample = 0;
00939             else
00940                 v->s.quarter_sample = 1;
00941         } else
00942             v->s.quarter_sample = 1;
00943         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));
00944 
00945         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00946                  v->mv_mode2 == MV_PMODE_MIXED_MV)
00947                 || v->mv_mode == MV_PMODE_MIXED_MV)
00948         {
00949             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00950             if (status < 0) return -1;
00951             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00952                    "Imode: %i, Invert: %i\n", status>>1, status&1);
00953         } else {
00954             v->mv_type_is_raw = 0;
00955             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00956         }
00957         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00958         if (status < 0) return -1;
00959         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00960                "Imode: %i, Invert: %i\n", status>>1, status&1);
00961 
00962         /* Hopefully this is correct for P frames */
00963         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
00964         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00965         if (v->dquant)
00966         {
00967             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00968             vop_dquant_decoding(v);
00969         }
00970 
00971         v->ttfrm = 0; //FIXME Is that so ?
00972         if (v->vstransform)
00973         {
00974             v->ttmbf = get_bits1(gb);
00975             if (v->ttmbf)
00976             {
00977                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00978             }
00979         } else {
00980             v->ttmbf = 1;
00981             v->ttfrm = TT_8X8;
00982         }
00983         break;
00984     case AV_PICTURE_TYPE_B:
00985         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
00986         else v->mvrange = 0;
00987         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
00988         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
00989         v->range_x = 1 << (v->k_x - 1);
00990         v->range_y = 1 << (v->k_y - 1);
00991 
00992         if (v->pq < 5) v->tt_index = 0;
00993         else if(v->pq < 13) v->tt_index = 1;
00994         else v->tt_index = 2;
00995 
00996         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00997         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
00998         v->s.mspel = v->s.quarter_sample;
00999 
01000         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
01001         if (status < 0) return -1;
01002         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
01003                "Imode: %i, Invert: %i\n", status>>1, status&1);
01004         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
01005         if (status < 0) return -1;
01006         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
01007                "Imode: %i, Invert: %i\n", status>>1, status&1);
01008 
01009         v->s.mv_table_index = get_bits(gb, 2);
01010         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
01011 
01012         if (v->dquant)
01013         {
01014             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01015             vop_dquant_decoding(v);
01016         }
01017 
01018         v->ttfrm = 0;
01019         if (v->vstransform)
01020         {
01021             v->ttmbf = get_bits1(gb);
01022             if (v->ttmbf)
01023             {
01024                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
01025             }
01026         } else {
01027             v->ttmbf = 1;
01028             v->ttfrm = TT_8X8;
01029         }
01030         break;
01031     }
01032 
01033     /* AC Syntax */
01034     v->c_ac_table_index = decode012(gb);
01035     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
01036     {
01037         v->y_ac_table_index = decode012(gb);
01038     }
01039     /* DC Syntax */
01040     v->s.dc_table_index = get_bits1(gb);
01041     if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) && v->dquant) {
01042         av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01043         vop_dquant_decoding(v);
01044     }
01045 
01046     v->bi_type = 0;
01047     if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
01048         v->s.pict_type = AV_PICTURE_TYPE_B;
01049         v->bi_type = 1;
01050     }
01051     return 0;
01052 }

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1