• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files

hvirtual/quicktime/ffmpeg/libavcodec/vc9.c

Go to the documentation of this file.
00001 /*
00002  * VC-9 and WMV3 decoder
00003  * Copyright (c) 2005 Anonymous
00004  * Copyright (c) 2005 Alex Beregszaszi
00005  * Copyright (c) 2005 Michael Niedermayer
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00030 #include "common.h"
00031 #include "dsputil.h"
00032 #include "avcodec.h"
00033 #include "mpegvideo.h"
00034 #include "vc9data.h"
00035 
00036 #undef NDEBUG
00037 #include <assert.h>
00038 
00039 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
00040 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
00041 extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
00042 #define MB_INTRA_VLC_BITS 9
00043 extern VLC ff_msmp4_mb_i_vlc;
00044 #define DC_VLC_BITS 9
00045 static const uint16_t table_mb_intra[64][2];
00046 
00047 /* Some inhibiting stuff */
00048 #define HAS_ADVANCED_PROFILE   0
00049 #define TRACE                  1
00050 
00051 #if TRACE
00052 #  define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
00053                    codes, codes_wrap, codes_size, use_static)          \
00054   if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,     \
00055                codes, codes_wrap, codes_size, use_static) < 0)         \
00056   {                                                                    \
00057     av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i);   \
00058     return -1;                                                         \
00059   }
00060 #else
00061 #  define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
00062                    codes, codes_wrap, codes_size, use_static)          \
00063   init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,         \
00064            codes, codes_wrap, codes_size, use_static)
00065 #endif
00066 
00069 #define PROFILE_SIMPLE   0
00070 #define PROFILE_MAIN     1
00071 #define PROFILE_COMPLEX  2 
00072 #define PROFILE_ADVANCED 3
00073 
00074 
00077 #define QUANT_FRAME_IMPLICIT   0 
00078 #define QUANT_FRAME_EXPLICIT   1 
00079 #define QUANT_NON_UNIFORM      2 
00080 #define QUANT_UNIFORM          3 
00081 
00082 
00083 
00085 #define DQPROFILE_FOUR_EDGES   0
00086 #define DQPROFILE_DOUBLE_EDGES 1
00087 #define DQPROFILE_SINGLE_EDGE  2
00088 #define DQPROFILE_ALL_MBS      3
00089 
00090 
00094 #define DQPROFILE_FOUR_EDGES   0
00095 #define DQSINGLE_BEDGE_LEFT   0
00096 #define DQSINGLE_BEDGE_TOP    1
00097 #define DQSINGLE_BEDGE_RIGHT  2
00098 #define DQSINGLE_BEDGE_BOTTOM 3
00099 
00100 
00103 #define DQDOUBLE_BEDGE_TOPLEFT     0
00104 #define DQDOUBLE_BEDGE_TOPRIGHT    1
00105 #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
00106 #define DQDOUBLE_BEDGE_BOTTOMLEFT  3
00107 
00108 
00111 #define MV_PMODE_1MV_HPEL_BILIN   0
00112 #define MV_PMODE_1MV              1
00113 #define MV_PMODE_1MV_HPEL         2
00114 #define MV_PMODE_MIXED_MV         3
00115 #define MV_PMODE_INTENSITY_COMP   4
00116 
00117 
00120 #define BMV_TYPE_BACKWARD          0
00121 #define BMV_TYPE_BACKWARD          0
00122 #define BMV_TYPE_FORWARD           1
00123 #define BMV_TYPE_INTERPOLATED      3
00124 
00125 
00127 static const uint8_t mv_pmode_table[2][5] = {
00128   { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
00129   { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
00130 };
00131 
00133 #define BI_TYPE 7
00134 
00135 static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
00136   fps_dr[2] = { 1000, 1001 };
00137 static const uint8_t pquant_table[3][32] = {
00138   {  /* Implicit quantizer */
00139      0,  1,  2,  3,  4,  5,  6,  7,  8,  6,  7,  8,  9, 10, 11, 12,
00140     13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
00141   },
00142   {  /* Explicit quantizer, pquantizer uniform */
00143      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
00144     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
00145   },
00146   {  /* Explicit quantizer, pquantizer non-uniform */
00147      0,  1,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
00148     14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
00149   }
00150 };
00151 
00156 #define VC9_BFRACTION_VLC_BITS 7
00157 static VLC vc9_bfraction_vlc;
00158 #define VC9_IMODE_VLC_BITS 4
00159 static VLC vc9_imode_vlc;
00160 #define VC9_NORM2_VLC_BITS 3
00161 static VLC vc9_norm2_vlc;
00162 #define VC9_NORM6_VLC_BITS 9
00163 static VLC vc9_norm6_vlc;
00164 /* Could be optimized, one table only needs 8 bits */
00165 #define VC9_TTMB_VLC_BITS 9 //12
00166 static VLC vc9_ttmb_vlc[3];
00167 #define VC9_MV_DIFF_VLC_BITS 9 //15
00168 static VLC vc9_mv_diff_vlc[4];
00169 #define VC9_CBPCY_P_VLC_BITS 9 //14
00170 static VLC vc9_cbpcy_p_vlc[4];
00171 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
00172 static VLC vc9_4mv_block_pattern_vlc[4];
00173 #define VC9_TTBLK_VLC_BITS 5
00174 static VLC vc9_ttblk_vlc[3];
00175 #define VC9_SUBBLKPAT_VLC_BITS 6
00176 static VLC vc9_subblkpat_vlc[3];
00178 
00185 typedef struct BitPlane {
00186     uint8_t *data;      
00187     int width;          
00188     int stride;         
00189     int height;         
00190     uint8_t is_raw;     
00191 } BitPlane;
00192 
00197 typedef struct VC9Context{
00198     MpegEncContext s;
00199 
00202     int res_sm;           
00203     int res_x8;           
00204     int multires;         
00205     int res_fasttx;       
00206     int res_transtab;     
00207     int rangered;         
00208 
00209     int res_rtm_flag;     
00210     int reserved;         
00211 
00212 
00213 #if HAS_ADVANCED_PROFILE
00214 
00216     int level;            
00217     int chromaformat;     
00218     int postprocflag;     
00219     int broadcast;        
00220     int interlace;        
00221     int tfcntrflag;       
00222     int panscanflag;      
00223     int extended_dmv;     
00224     int color_prim;       
00225     int transfer_char;    
00226     int matrix_coef;      
00227     int hrd_param_flag;   
00228 
00229 
00230 #endif
00231 
00232 
00237     int profile;          
00238     int frmrtq_postproc;  
00239     int bitrtq_postproc;  
00240     int fastuvmc;         
00241     int extended_mv;      
00242     int dquant;           
00243     int vstransform;      
00244     int overlap;          
00245     int quantizer_mode;   
00246     int finterpflag;      
00247 
00248 
00251     uint8_t mv_mode;      
00252     uint8_t mv_mode2;     
00253     int k_x;              
00254     int k_y;              
00255     uint8_t pq, altpq;    
00256 
00258     uint8_t dquantfrm;
00259     uint8_t dqprofile;
00260     uint8_t dqsbedge;
00261     uint8_t dqbilevel;
00263 
00267     int c_ac_table_index; 
00268     int y_ac_table_index; 
00269 
00270     int ttfrm;            
00271     uint8_t ttmbf;        
00272     int ttmb;             
00273     uint8_t ttblk4x4;     
00274 
00276     uint8_t lumscale;
00277     uint8_t lumshift;
00279     int16_t bfraction;    
00280     uint8_t halfpq;       
00281     uint8_t respic;       
00282     int buffer_fullness;  
00283 
00289     uint8_t mvrange;
00290     uint8_t pquantizer;           
00291     uint8_t *previous_line_cbpcy; 
00292     VLC *cbpcy_vlc;               
00293     int tt_index;                 
00294     BitPlane mv_type_mb_plane;    
00295     BitPlane skip_mb_plane;       
00296     BitPlane direct_mb_plane;     
00297 
00300     uint8_t rangeredfrm; 
00301     uint8_t interpfrm;
00303 
00304 #if HAS_ADVANCED_PROFILE
00305 
00307     uint8_t fcm; 
00308     uint8_t numpanscanwin;
00309     uint8_t tfcntr;
00310     uint8_t rptfrm, tff, rff;
00311     uint16_t topleftx;
00312     uint16_t toplefty;
00313     uint16_t bottomrightx;
00314     uint16_t bottomrighty;
00315     uint8_t uvsamp;
00316     uint8_t postproc;
00317     int hrd_num_leaky_buckets;
00318     uint8_t bit_rate_exponent;
00319     uint8_t buffer_size_exponent;
00320     BitPlane ac_pred_plane;       
00321     BitPlane over_flags_plane;    
00322     uint8_t condover;
00323     uint16_t *hrd_rate, *hrd_buffer;
00324     uint8_t *hrd_fullness;
00325     uint8_t range_mapy_flag;
00326     uint8_t range_mapuv_flag;
00327     uint8_t range_mapy;
00328     uint8_t range_mapuv;
00330 #endif
00331 } VC9Context;
00332 
00341 static int get_prefix(GetBitContext *gb, int stop, int len)
00342 {
00343 #if 1
00344   int i = 0, tmp = !stop;
00345 
00346   while (i != len && tmp != stop)
00347   {
00348     tmp = get_bits(gb, 1);
00349     i++;
00350   }
00351   if (i == len && tmp != stop) return len+1;
00352   return i;
00353 #else
00354   unsigned int buf;
00355   int log;
00356 
00357   OPEN_READER(re, gb);
00358   UPDATE_CACHE(re, gb);
00359   buf=GET_CACHE(re, gb); //Still not sure
00360   if (stop) buf = ~buf;
00361 
00362   log= av_log2(-buf); //FIXME: -?
00363   if (log < limit){
00364     LAST_SKIP_BITS(re, gb, log+1);
00365     CLOSE_READER(re, gb);
00366     return log;
00367   }
00368 
00369   LAST_SKIP_BITS(re, gb, limit);
00370   CLOSE_READER(re, gb);
00371   return limit;
00372 #endif
00373 }
00374 
00380 static int vc9_init_common(VC9Context *v)
00381 {
00382     static int done = 0;
00383     int i = 0;
00384 
00385     /* Set the bit planes */
00386     v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
00387     v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
00388     v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
00389 #if HAS_ADVANCED_PROFILE
00390     v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
00391     v->hrd_rate = v->hrd_buffer = NULL;
00392 #endif
00393 
00394     /* VLC tables */
00395 #if 0 // spec -> actual tables converter
00396     for(i=0; i<64; i++){
00397         int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
00398         av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
00399         if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
00400     }
00401     for(i=0; i<64; i++){
00402         int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4];
00403         av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
00404         if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
00405     }
00406 #endif
00407     if(!done)
00408     {
00409         done = 1;
00410         INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23,
00411                  vc9_bfraction_bits, 1, 1,
00412                  vc9_bfraction_codes, 1, 1, 1);
00413         INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
00414                  vc9_norm2_bits, 1, 1,
00415                  vc9_norm2_codes, 1, 1, 1);
00416         INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
00417                  vc9_norm6_bits, 1, 1,
00418                  vc9_norm6_codes, 2, 2, 1);
00419         INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
00420                  vc9_imode_bits, 1, 1,
00421                  vc9_imode_codes, 1, 1, 1);
00422         for (i=0; i<3; i++)
00423         {
00424             INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
00425                      vc9_ttmb_bits[i], 1, 1,
00426                      vc9_ttmb_codes[i], 2, 2, 1);
00427             INIT_VLC(&vc9_ttblk_vlc[i], VC9_TTBLK_VLC_BITS, 8,
00428                      vc9_ttblk_bits[i], 1, 1,
00429                      vc9_ttblk_codes[i], 1, 1, 1);
00430             INIT_VLC(&vc9_subblkpat_vlc[i], VC9_SUBBLKPAT_VLC_BITS, 15,
00431                      vc9_subblkpat_bits[i], 1, 1,
00432                      vc9_subblkpat_codes[i], 1, 1, 1);
00433         }
00434         for(i=0; i<4; i++)
00435         {
00436             INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
00437                      vc9_4mv_block_pattern_bits[i], 1, 1,
00438                      vc9_4mv_block_pattern_codes[i], 1, 1, 1);
00439             INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64,
00440                      vc9_cbpcy_p_bits[i], 1, 1,
00441                      vc9_cbpcy_p_codes[i], 2, 2, 1);
00442             INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73,
00443                      vc9_mv_diff_bits[i], 1, 1,
00444                      vc9_mv_diff_codes[i], 2, 2, 1);
00445         }
00446     }
00447 
00448     /* Other defaults */
00449     v->pq = -1;
00450     v->mvrange = 0; /* 7.1.1.18, p80 */
00451 
00452     return 0;
00453 }
00454 
00455 #if HAS_ADVANCED_PROFILE
00456 
00463 static int decode_hrd(VC9Context *v, GetBitContext *gb)
00464 {
00465     int i, num;
00466 
00467     num = 1 + get_bits(gb, 5);
00468 
00469     /*hrd rate*/
00470     if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
00471     {
00472         av_freep(&v->hrd_rate);
00473     }
00474     if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
00475     if (!v->hrd_rate) return -1;
00476 
00477     /*hrd buffer*/
00478     if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
00479     {
00480         av_freep(&v->hrd_buffer);
00481     }
00482     if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
00483     if (!v->hrd_buffer)
00484     {
00485         av_freep(&v->hrd_rate);
00486         return -1;
00487     }
00488 
00489     /*hrd fullness*/
00490     if (v->hrd_fullness || num != v->hrd_num_leaky_buckets)
00491     {
00492         av_freep(&v->hrd_buffer);
00493     }
00494     if (!v->hrd_fullness) v->hrd_fullness = av_malloc(num*sizeof(uint8_t));
00495     if (!v->hrd_fullness)
00496     {
00497         av_freep(&v->hrd_rate);
00498         av_freep(&v->hrd_buffer);
00499         return -1;
00500     }
00501     v->hrd_num_leaky_buckets = num;
00502 
00503     //exponent in base-2 for rate
00504     v->bit_rate_exponent = 6 + get_bits(gb, 4);
00505     //exponent in base-2 for buffer_size
00506     v->buffer_size_exponent = 4 + get_bits(gb, 4);
00507 
00508     for (i=0; i<num; i++)
00509     {
00510         //mantissae, ordered (if not, use a function ?
00511         v->hrd_rate[i] = 1 + get_bits(gb, 16);
00512         if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
00513         {
00514             av_log(v->s.avctx, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
00515                    "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
00516             return -1;
00517         }
00518         v->hrd_buffer[i] = 1 + get_bits(gb, 16);
00519         if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
00520         {
00521             av_log(v->s.avctx, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
00522                    "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
00523             return -1;
00524         }
00525     }
00526     return 0;
00527 }
00528 
00537 static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
00538 {
00539     VC9Context *v = avctx->priv_data;
00540     int nr, dr, aspect_ratio;
00541 
00542     v->postprocflag = get_bits(gb, 1);
00543     v->broadcast = get_bits(gb, 1);
00544     v->interlace = get_bits(gb, 1);
00545 
00546     v->tfcntrflag = get_bits(gb, 1);
00547     v->finterpflag = get_bits(gb, 1); //common
00548     v->panscanflag = get_bits(gb, 1);
00549     v->reserved = get_bits(gb, 1);
00550     if (v->reserved)
00551     {
00552         av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
00553                v->reserved);
00554         return -1;
00555     }
00556     if (v->extended_mv)
00557         v->extended_dmv = get_bits(gb, 1);
00558 
00559     /* 6.1.7, p21 */
00560     if (get_bits(gb, 1) /* pic_size_flag */)
00561     {
00562         avctx->coded_width = get_bits(gb, 12) << 1;
00563         avctx->coded_height = get_bits(gb, 12) << 1;
00564         if ( get_bits(gb, 1) /* disp_size_flag */)
00565         {
00566             avctx->width = get_bits(gb, 14);
00567             avctx->height = get_bits(gb, 14);
00568         }
00569 
00570         /* 6.1.7.4, p23 */
00571         if ( get_bits(gb, 1) /* aspect_ratio_flag */)
00572         {
00573             aspect_ratio = get_bits(gb, 4); //SAR
00574             if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
00575             {
00576                 avctx->sample_aspect_ratio.num = 1 + get_bits(gb, 8);
00577                 avctx->sample_aspect_ratio.den = 1 + get_bits(gb, 8);
00578             }
00579             else if (aspect_ratio == 0x0E)
00580             {
00581                 av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
00582             }
00583             else
00584             {
00585               avctx->sample_aspect_ratio = vc9_pixel_aspect[aspect_ratio];
00586             }
00587         }
00588     }
00589     else
00590     {
00591         avctx->coded_width = avctx->width;
00592         avctx->coded_height = avctx->height;
00593     }
00594 
00595     /* 6.1.8, p23 */
00596     if ( get_bits(gb, 1) /* framerateflag */)
00597     {
00598         if ( !get_bits(gb, 1) /* framerateind */)
00599         {
00600             nr = get_bits(gb, 8);
00601             dr = get_bits(gb, 4);
00602             if (nr<1)
00603             {
00604                 av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
00605                 return -1;
00606             }
00607             if (nr>5)
00608             {
00609                 av_log(avctx, AV_LOG_ERROR,
00610                        "Reserved FRAMERATENR %i not handled\n", nr);
00611                 nr = 5; /* overflow protection */
00612             }
00613             if (dr<1)
00614             {
00615                 av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
00616                 return -1;
00617             }
00618             if (dr>2)
00619             {
00620                 av_log(avctx, AV_LOG_ERROR,
00621                        "Reserved FRAMERATEDR %i not handled\n", dr);
00622                 dr = 2; /* overflow protection */
00623             }
00624             avctx->time_base.num = fps_nr[dr - 1];
00625             avctx->time_base.den = fps_nr[nr - 1];
00626         }
00627         else
00628         {
00629             nr = get_bits(gb, 16);
00630             // 0.03125->2048Hz / 0.03125Hz
00631             avctx->time_base.den = 1000000;
00632             avctx->time_base.num = 31250*(1+nr);
00633         }
00634     }
00635 
00636     /* 6.1.9, p25 */
00637     if ( get_bits(gb, 1) /* color_format_flag */)
00638     {
00639         //Chromacity coordinates of color primaries
00640         //like ITU-R BT.709-2, BT.470-2, ...
00641         v->color_prim = get_bits(gb, 8);
00642         if (v->color_prim<1)
00643         {
00644             av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is forbidden\n");
00645             return -1;
00646         }
00647         if (v->color_prim == 3 || v->color_prim>6)
00648         {
00649             av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
00650                    v->color_prim);
00651             return -1;
00652         }
00653 
00654         //Opto-electronic transfer characteristics
00655         v->transfer_char = get_bits(gb, 8);
00656         if (v->transfer_char < 1)
00657         {
00658             av_log(avctx, AV_LOG_ERROR, "0 for TRAMSFER_CHAR is forbidden\n");
00659             return -1;
00660         }
00661         if (v->transfer_char == 3 || v->transfer_char>8)
00662         {
00663             av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
00664                    v->color_prim);
00665             return -1;
00666         }
00667 
00668         //Matrix coefficient for primariev->YCbCr
00669         v->matrix_coef = get_bits(gb, 8);
00670         if (v->matrix_coef < 1)
00671         {
00672             av_log(avctx, AV_LOG_ERROR, "0 for MATRIX_COEF is forbidden\n");
00673             return -1;
00674         }
00675         if ((v->matrix_coef > 2 && v->matrix_coef < 6) || v->matrix_coef > 7)
00676         {
00677             av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
00678                    v->color_prim);
00679             return -1;
00680         }
00681     }
00682 
00683     //Hypothetical reference decoder indicator flag
00684     v->hrd_param_flag = get_bits(gb, 1);
00685     if (v->hrd_param_flag)
00686     {
00687       if (decode_hrd(v, gb) < 0) return -1;
00688     }
00689 
00690     /*reset scaling ranges, 6.2.2 & 6.2.3, p33*/
00691     v->range_mapy_flag = 0;
00692     v->range_mapuv_flag = 0;
00693 
00694     av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
00695     return -1;
00696 }
00697 #endif
00698 
00706 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
00707 {
00708     VC9Context *v = avctx->priv_data;
00709 
00710     av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
00711     v->profile = get_bits(gb, 2);
00712     if (v->profile == 2)
00713     {
00714         av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden\n");
00715         return -1;
00716     }
00717 
00718 #if HAS_ADVANCED_PROFILE
00719     if (v->profile == PROFILE_ADVANCED)
00720     {
00721         v->level = get_bits(gb, 3);
00722         if(v->level >= 5)
00723         {
00724             av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
00725         }
00726         v->chromaformat = get_bits(gb, 2);
00727         if (v->chromaformat != 1)
00728         {
00729             av_log(avctx, AV_LOG_ERROR,
00730                    "Only 4:2:0 chroma format supported\n");
00731             return -1;
00732         }
00733     }
00734     else
00735 #endif
00736     {
00737         v->res_sm = get_bits(gb, 2); //reserved
00738         if (v->res_sm)
00739         {
00740             av_log(avctx, AV_LOG_ERROR,
00741                    "Reserved RES_SM=%i is forbidden\n", v->res_sm);
00742             return -1;
00743         }
00744     }
00745 
00746     // (fps-2)/4 (->30)
00747     v->frmrtq_postproc = get_bits(gb, 3); //common
00748     // (bitrate-32kbps)/64kbps
00749     v->bitrtq_postproc = get_bits(gb, 5); //common
00750     v->s.loop_filter = get_bits(gb, 1); //common
00751     if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
00752     {
00753         av_log(avctx, AV_LOG_ERROR,
00754                "LOOPFILTER shell not be enabled in simple profile\n");
00755     }
00756 
00757 #if HAS_ADVANCED_PROFILE
00758     if (v->profile < PROFILE_ADVANCED)
00759 #endif
00760     {
00761         v->res_x8 = get_bits(gb, 1); //reserved
00762         if (v->res_x8)
00763         {
00764             av_log(avctx, AV_LOG_ERROR,
00765                    "1 for reserved RES_X8 is forbidden\n");
00766             //return -1;
00767         }
00768         v->multires = get_bits(gb, 1);
00769         v->res_fasttx = get_bits(gb, 1);
00770         if (!v->res_fasttx)
00771         {
00772             av_log(avctx, AV_LOG_ERROR,
00773                    "0 for reserved RES_FASTTX is forbidden\n");
00774             //return -1;
00775         }
00776     }
00777 
00778     v->fastuvmc =  get_bits(gb, 1); //common
00779     if (!v->profile && !v->fastuvmc)
00780     {
00781         av_log(avctx, AV_LOG_ERROR,
00782                "FASTUVMC unavailable in Simple Profile\n");
00783         return -1;
00784     }
00785     v->extended_mv =  get_bits(gb, 1); //common
00786     if (!v->profile && v->extended_mv)
00787     {
00788         av_log(avctx, AV_LOG_ERROR,
00789                "Extended MVs unavailable in Simple Profile\n");
00790         return -1;
00791     }
00792     v->dquant =  get_bits(gb, 2); //common
00793     v->vstransform =  get_bits(gb, 1); //common
00794 
00795 #if HAS_ADVANCED_PROFILE
00796     if (v->profile < PROFILE_ADVANCED)
00797 #endif
00798     {
00799         v->res_transtab = get_bits(gb, 1);
00800         if (v->res_transtab)
00801         {
00802             av_log(avctx, AV_LOG_ERROR,
00803                    "1 for reserved RES_TRANSTAB is forbidden\n");
00804             return -1;
00805         }
00806     }
00807 
00808     v->overlap = get_bits(gb, 1); //common
00809 
00810 #if HAS_ADVANCED_PROFILE
00811     if (v->profile < PROFILE_ADVANCED)
00812 #endif
00813     {
00814         v->s.resync_marker = get_bits(gb, 1);
00815         v->rangered = get_bits(gb, 1);
00816         if (v->rangered && v->profile == PROFILE_SIMPLE)
00817         {
00818             av_log(avctx, AV_LOG_DEBUG,
00819                    "RANGERED should be set to 0 in simple profile\n");
00820         }
00821     }
00822 
00823     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
00824     v->quantizer_mode = get_bits(gb, 2); //common
00825 
00826 #if HAS_ADVANCED_PROFILE
00827     if (v->profile < PROFILE_ADVANCED)
00828 #endif
00829     {
00830         v->finterpflag = get_bits(gb, 1); //common
00831         v->res_rtm_flag = get_bits(gb, 1); //reserved
00832         if (!v->res_rtm_flag)
00833         {
00834             av_log(avctx, AV_LOG_ERROR,
00835                    "0 for reserved RES_RTM_FLAG is forbidden\n");
00836             //return -1;
00837         }
00838 #if TRACE
00839         av_log(avctx, AV_LOG_INFO,
00840                "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
00841                "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
00842                "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
00843                "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
00844                v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
00845                v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
00846                v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
00847                v->dquant, v->quantizer_mode, avctx->max_b_frames
00848                );
00849         return 0;
00850 #endif
00851     }
00852 #if HAS_ADVANCED_PROFILE
00853     else return decode_advanced_sequence_header(avctx, gb);
00854 #endif
00855 }
00856 
00857 
00858 #if HAS_ADVANCED_PROFILE
00859 
00864 static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
00865 {
00866     VC9Context *v = avctx->priv_data;
00867     int i;
00868     if (v->profile != PROFILE_ADVANCED)
00869     {
00870         av_log(avctx, AV_LOG_ERROR,
00871                "Entry point are only defined in Advanced Profile!\n");
00872         return -1; //Only for advanced profile!
00873     }
00874     if (v->hrd_param_flag)
00875     {
00876         //Update buffer fullness
00877         av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
00878         assert(v->hrd_num_leaky_buckets > 0);
00879         for (i=0; i<v->hrd_num_leaky_buckets; i++)
00880             v->hrd_fullness[i] = get_bits(gb, 8);
00881     }
00882     if ((v->range_mapy_flag = get_bits(gb, 1)))
00883     {
00884         //RANGE_MAPY
00885         av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
00886         v->range_mapy = get_bits(gb, 3);
00887     }
00888     if ((v->range_mapuv_flag = get_bits(gb, 1)))
00889     {
00890         //RANGE_MAPUV
00891         av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
00892         v->range_mapuv = get_bits(gb, 3);
00893     }
00894     if (v->panscanflag)
00895     {
00896         //NUMPANSCANWIN
00897         v->numpanscanwin = get_bits(gb, 3);
00898         av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
00899     }
00900     return 0;
00901 }
00902 #endif
00903 
00904 /***********************************************************************/
00915 #define IMODE_RAW     0
00916 #define IMODE_NORM2   1
00917 #define IMODE_DIFF2   2
00918 #define IMODE_NORM6   3
00919 #define IMODE_DIFF6   4
00920 #define IMODE_ROWSKIP 5
00921 #define IMODE_COLSKIP 6
00922  //imode defines
00923 
00932 int alloc_bitplane(BitPlane *bp, int width, int height)
00933 {
00934     if (!bp || bp->width<0 || bp->height<0) return -1;
00935     bp->data = (uint8_t*)av_malloc(width*height);
00936     if (!bp->data) return -1;
00937     bp->width = bp->stride = width; 
00938     bp->height = height;
00939     return 0;
00940 }
00941 
00945 void free_bitplane(BitPlane *bp)
00946 {
00947     bp->width = bp->stride = bp->height = 0;
00948     if (bp->data) av_freep(&bp->data);
00949 }
00950 
00957 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
00958     int x, y;
00959 
00960     for (y=0; y<height; y++){
00961         if (!get_bits(gb, 1)) //rowskip
00962             memset(plane, 0, width);
00963         else
00964             for (x=0; x<width; x++) 
00965                 plane[x] = get_bits(gb, 1);
00966         plane += stride;
00967     }
00968 }
00969 
00977 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
00978     int x, y;
00979 
00980     for (x=0; x<width; x++){
00981         if (!get_bits(gb, 1)) //colskip
00982             for (y=0; y<height; y++)
00983                 plane[y*stride] = 0;
00984         else
00985             for (y=0; y<height; y++)
00986                 plane[y*stride] = get_bits(gb, 1);
00987         plane ++;
00988     }
00989 }
00990 
00998 static int bitplane_decoding(BitPlane *bp, VC9Context *v)
00999 {
01000     GetBitContext *gb = &v->s.gb;
01001 
01002     int imode, x, y, code, use_vertical_tile, tile_w, tile_h, offset;
01003     uint8_t invert, *planep = bp->data;
01004 
01005     invert = get_bits(gb, 1);
01006     imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
01007 
01008     bp->is_raw = 0;
01009     switch (imode)
01010     {
01011     case IMODE_RAW:
01012         //Data is actually read in the MB layer (same for all tests == "raw")
01013         bp->is_raw = 1; //invert ignored
01014         return invert;
01015     case IMODE_DIFF2:
01016     case IMODE_NORM2:
01017         if ((bp->height*bp->width) & 1)
01018         {
01019             *(++planep) = get_bits(gb, 1);
01020             offset = x = 1;
01021         }
01022         else offset = x = 0;
01023 
01024         for (y=0; y<bp->height; y++)
01025         {
01026             for(; x<bp->width; x+=2)
01027             {
01028                 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
01029                 *(++planep) = code&1; //lsb => left
01030                 *(++planep) = (code>>1)&1; //msb => right
01031             }
01032             planep += bp->stride-bp->width;
01033             if ((bp->width-offset)&1) //Odd number previously processed
01034             {
01035                 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
01036                 *planep = code&1;
01037                 planep += bp->stride-bp->width;
01038                 *planep = (code>>1)&1; //msb => right
01039                 offset = x = 1;
01040             }
01041             else
01042             {
01043                 offset = x = 0;
01044                 planep += bp->stride-bp->width;
01045             }
01046         }
01047         break;
01048     case IMODE_DIFF6:
01049     case IMODE_NORM6:
01050         use_vertical_tile=  bp->height%3==0 &&  bp->width%3!=0;
01051         tile_w= use_vertical_tile ? 2 : 3;
01052         tile_h= use_vertical_tile ? 3 : 2;
01053 
01054         for(y=  bp->height%tile_h; y< bp->height; y+=tile_h){
01055             for(x=  bp->width%tile_w; x< bp->width; x+=tile_w){
01056                 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
01057                 if(code<0){
01058                     av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
01059                     return -1;
01060                 }
01061                 //FIXME followin