00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 };
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))
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))
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
00129 *raw_flag = 1;
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
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)) {
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 {
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
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];
00230 }
00231 return (imode<<1) + invert;
00232 }
00233
00235
00236
00240 static int vop_dquant_decoding(VC1Context *v)
00241 {
00242 GetBitContext *gb = &v->s.gb;
00243 int pqdiff;
00244
00245
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;
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_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
00323 v->frmrtq_postproc = get_bits(gb, 3);
00324
00325 v->bitrtq_postproc = get_bits(gb, 5);
00326 v->s.loop_filter = get_bits1(gb);
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);
00336 v->multires = get_bits1(gb);
00337 v->res_fasttx = get_bits1(gb);
00338 if (!v->res_fasttx)
00339 {
00340 v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
00341 v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
00342 v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
00343 v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
00344 v->s.dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add;
00345 v->s.dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
00346 v->s.dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
00347 v->s.dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
00348 }
00349
00350 v->fastuvmc = get_bits1(gb);
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);
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);
00365 v->vstransform = get_bits1(gb);
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);
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);
00386 v->quantizer_mode = get_bits(gb, 2);
00387
00388 v->finterpflag = get_bits1(gb);
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);
00394 v->res_x8 = get_bits1(gb);
00395 if (get_bits1(gb)) {
00396 av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
00397 return -1;
00398 }
00399 skip_bits(gb, 3);
00400 v->res_rtm_flag = 0;
00401 } else {
00402 v->res_rtm_flag = get_bits1(gb);
00403 }
00404 if (!v->res_rtm_flag)
00405 {
00406
00407
00408 av_log(avctx, AV_LOG_ERROR,
00409 "Old WMV3 version detected, some frames may be decoded incorrectly\n");
00410
00411 }
00412
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
00444 v->frmrtq_postproc = get_bits(gb, 3);
00445
00446 v->bitrtq_postproc = get_bits(gb, 5);
00447 v->postprocflag = get_bits1(gb);
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);
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) {
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)) {
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);
00489 h = get_bits(gb, 8);
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)){
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);
00521 skip_bits(gb, 4);
00522 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
00523 skip_bits(gb, 16);
00524 skip_bits(gb, 16);
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);
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->res_sprite) {
00583 skip_bits(gb, 2);
00584 }
00585 if(v->finterpflag) v->interpfrm = get_bits1(gb);
00586 skip_bits(gb, 2);
00587 v->rangeredfrm = 0;
00588 if (v->rangered) v->rangeredfrm = get_bits1(gb);
00589 v->s.pict_type = get_bits1(gb);
00590 if (v->s.avctx->max_b_frames) {
00591 if (!v->s.pict_type) {
00592 if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
00593 else v->s.pict_type = FF_B_TYPE;
00594 } else v->s.pict_type = FF_P_TYPE;
00595 } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
00596
00597 v->bi_type = 0;
00598 if(v->s.pict_type == FF_B_TYPE) {
00599 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00600 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00601 if(v->bfraction == 0) {
00602 v->s.pict_type = FF_BI_TYPE;
00603 }
00604 }
00605 if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
00606 skip_bits(gb, 7);
00607
00608 if(v->parse_only)
00609 return 0;
00610
00611
00612 if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
00613 v->rnd = 1;
00614 if(v->s.pict_type == FF_P_TYPE)
00615 v->rnd ^= 1;
00616
00617
00618 pqindex = get_bits(gb, 5);
00619 if(!pqindex) return -1;
00620 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00621 v->pq = ff_vc1_pquant_table[0][pqindex];
00622 else
00623 v->pq = ff_vc1_pquant_table[1][pqindex];
00624
00625 v->pquantizer = 1;
00626 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00627 v->pquantizer = pqindex < 9;
00628 if (v->quantizer_mode == QUANT_NON_UNIFORM)
00629 v->pquantizer = 0;
00630 v->pqindex = pqindex;
00631 if (pqindex < 9) v->halfpq = get_bits1(gb);
00632 else v->halfpq = 0;
00633 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00634 v->pquantizer = get_bits1(gb);
00635 v->dquantfrm = 0;
00636 if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
00637 v->k_x = v->mvrange + 9 + (v->mvrange >> 1);
00638 v->k_y = v->mvrange + 8;
00639 v->range_x = 1 << (v->k_x - 1);
00640 v->range_y = 1 << (v->k_y - 1);
00641 if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
00642
00643 if(v->res_x8 && (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)){
00644 v->x8_type = get_bits1(gb);
00645 }else v->x8_type = 0;
00646
00647
00648
00649 if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
00650
00651 switch(v->s.pict_type) {
00652 case FF_P_TYPE:
00653 if (v->pq < 5) v->tt_index = 0;
00654 else if(v->pq < 13) v->tt_index = 1;
00655 else v->tt_index = 2;
00656
00657 lowquant = (v->pq > 12) ? 0 : 1;
00658 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00659 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00660 {
00661 int scale, shift, i;
00662 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00663 v->lumscale = get_bits(gb, 6);
00664 v->lumshift = get_bits(gb, 6);
00665 v->use_ic = 1;
00666
00667 if(!v->lumscale) {
00668 scale = -64;
00669 shift = (255 - v->lumshift * 2) << 6;
00670 if(v->lumshift > 31)
00671 shift += 128 << 6;
00672 } else {
00673 scale = v->lumscale + 32;
00674 if(v->lumshift > 31)
00675 shift = (v->lumshift - 64) << 6;
00676 else
00677 shift = v->lumshift << 6;
00678 }
00679 for(i = 0; i < 256; i++) {
00680 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
00681 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00682 }
00683 }
00684 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00685 v->s.quarter_sample = 0;
00686 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00687 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00688 v->s.quarter_sample = 0;
00689 else
00690 v->s.quarter_sample = 1;
00691 } else
00692 v->s.quarter_sample = 1;
00693 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));
00694
00695 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00696 v->mv_mode2 == MV_PMODE_MIXED_MV)
00697 || v->mv_mode == MV_PMODE_MIXED_MV)
00698 {
00699 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00700 if (status < 0) return -1;
00701 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00702 "Imode: %i, Invert: %i\n", status>>1, status&1);
00703 } else {
00704 v->mv_type_is_raw = 0;
00705 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00706 }
00707 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00708 if (status < 0) return -1;
00709 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00710 "Imode: %i, Invert: %i\n", status>>1, status&1);
00711
00712
00713 v->s.mv_table_index = get_bits(gb, 2);
00714 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00715
00716 if (v->dquant)
00717 {
00718 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00719 vop_dquant_decoding(v);
00720 }
00721
00722 v->ttfrm = 0;
00723 if (v->vstransform)
00724 {
00725 v->ttmbf = get_bits1(gb);
00726 if (v->ttmbf)
00727 {
00728 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00729 }
00730 } else {
00731 v->ttmbf = 1;
00732 v->ttfrm = TT_8X8;
00733 }
00734 break;
00735 case FF_B_TYPE:
00736 if (v->pq < 5) v->tt_index = 0;
00737 else if(v->pq < 13) v->tt_index = 1;
00738 else v->tt_index = 2;
00739
00740 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00741 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
00742 v->s.mspel = v->s.quarter_sample;
00743
00744 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
00745 if (status < 0) return -1;
00746 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
00747 "Imode: %i, Invert: %i\n", status>>1, status&1);
00748 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00749 if (status < 0) return -1;
00750 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00751 "Imode: %i, Invert: %i\n", status>>1, status&1);
00752
00753 v->s.mv_table_index = get_bits(gb, 2);
00754 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00755
00756 if (v->dquant)
00757 {
00758 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00759 vop_dquant_decoding(v);
00760 }
00761
00762 v->ttfrm = 0;
00763 if (v->vstransform)
00764 {
00765 v->ttmbf = get_bits1(gb);
00766 if (v->ttmbf)
00767 {
00768 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00769 }
00770 } else {
00771 v->ttmbf = 1;
00772 v->ttfrm = TT_8X8;
00773 }
00774 break;
00775 }
00776
00777 if(!v->x8_type)
00778 {
00779
00780 v->c_ac_table_index = decode012(gb);
00781 if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
00782 {
00783 v->y_ac_table_index = decode012(gb);
00784 }
00785
00786 v->s.dc_table_index = get_bits1(gb);
00787 }
00788
00789 if(v->s.pict_type == FF_BI_TYPE) {
00790 v->s.pict_type = FF_B_TYPE;
00791 v->bi_type = 1;
00792 }
00793 return 0;
00794 }
00795
00796 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
00797 {
00798 int pqindex, lowquant;
00799 int status;
00800
00801 v->p_frame_skipped = 0;
00802
00803 if(v->interlace){
00804 v->fcm = decode012(gb);
00805 if(v->fcm){
00806 if(!v->warn_interlaced++)
00807 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
00808 return -1;
00809 }
00810 }
00811 switch(get_unary(gb, 0, 4)) {
00812 case 0:
00813 v->s.pict_type = FF_P_TYPE;
00814 break;
00815 case 1:
00816 v->s.pict_type = FF_B_TYPE;
00817 break;
00818 case 2:
00819 v->s.pict_type = FF_I_TYPE;
00820 break;
00821 case 3:
00822 v->s.pict_type = FF_BI_TYPE;
00823 break;
00824 case 4:
00825 v->s.pict_type = FF_P_TYPE;
00826 v->p_frame_skipped = 1;
00827 return 0;
00828 }
00829 if(v->tfcntrflag)
00830 skip_bits(gb, 8);
00831 if(v->broadcast) {
00832 if(!v->interlace || v->psf) {
00833 v->rptfrm = get_bits(gb, 2);
00834 } else {
00835 v->tff = get_bits1(gb);
00836 v->rptfrm = get_bits1(gb);
00837 }
00838 }
00839 if(v->panscanflag) {
00840
00841 }
00842 v->rnd = get_bits1(gb);
00843 if(v->interlace)
00844 v->uvsamp = get_bits1(gb);
00845 if(v->finterpflag) v->interpfrm = get_bits1(gb);
00846 if(v->s.pict_type == FF_B_TYPE) {
00847 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
00848 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
00849 if(v->bfraction == 0) {
00850 v->s.pict_type = FF_BI_TYPE;
00851 }
00852 }
00853 pqindex = get_bits(gb, 5);
00854 if(!pqindex) return -1;
00855 v->pqindex = pqindex;
00856 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00857 v->pq = ff_vc1_pquant_table[0][pqindex];
00858 else
00859 v->pq = ff_vc1_pquant_table[1][pqindex];
00860
00861 v->pquantizer = 1;
00862 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
00863 v->pquantizer = pqindex < 9;
00864 if (v->quantizer_mode == QUANT_NON_UNIFORM)
00865 v->pquantizer = 0;
00866 v->pqindex = pqindex;
00867 if (pqindex < 9) v->halfpq = get_bits1(gb);
00868 else v->halfpq = 0;
00869 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
00870 v->pquantizer = get_bits1(gb);
00871 if(v->postprocflag)
00872 v->postproc = get_bits(gb, 2);
00873
00874 if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
00875
00876 if(v->parse_only)
00877 return 0;
00878
00879 switch(v->s.pict_type) {
00880 case FF_I_TYPE:
00881 case FF_BI_TYPE:
00882 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
00883 if (status < 0) return -1;
00884 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
00885 "Imode: %i, Invert: %i\n", status>>1, status&1);
00886 v->condover = CONDOVER_NONE;
00887 if(v->overlap && v->pq <= 8) {
00888 v->condover = decode012(gb);
00889 if(v->condover == CONDOVER_SELECT) {
00890 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
00891 if (status < 0) return -1;
00892 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
00893 "Imode: %i, Invert: %i\n", status>>1, status&1);
00894 }
00895 }
00896 break;
00897 case FF_P_TYPE:
00898 if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
00899 else v->mvrange = 0;
00900 v->k_x = v->mvrange + 9 + (v->mvrange >> 1);
00901 v->k_y = v->mvrange + 8;
00902 v->range_x = 1 << (v->k_x - 1);
00903 v->range_y = 1 << (v->k_y - 1);
00904
00905 if (v->pq < 5) v->tt_index = 0;
00906 else if(v->pq < 13) v->tt_index = 1;
00907 else v->tt_index = 2;
00908
00909 lowquant = (v->pq > 12) ? 0 : 1;
00910 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
00911 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00912 {
00913 int scale, shift, i;
00914 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
00915 v->lumscale = get_bits(gb, 6);
00916 v->lumshift = get_bits(gb, 6);
00917
00918 if(!v->lumscale) {
00919 scale = -64;
00920 shift = (255 - v->lumshift * 2) << 6;
00921 if(v->lumshift > 31)
00922 shift += 128 << 6;
00923 } else {
00924 scale = v->lumscale + 32;
00925 if(v->lumshift > 31)
00926 shift = (v->lumshift - 64) << 6;
00927 else
00928 shift = v->lumshift << 6;
00929 }
00930 for(i = 0; i < 256; i++) {
00931 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
00932 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
00933 }
00934 v->use_ic = 1;
00935 }
00936 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
00937 v->s.quarter_sample = 0;
00938 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00939 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
00940 v->s.quarter_sample = 0;
00941 else
00942 v->s.quarter_sample = 1;
00943 } else
00944 v->s.quarter_sample = 1;
00945 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));
00946
00947 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
00948 v->mv_mode2 == MV_PMODE_MIXED_MV)
00949 || v->mv_mode == MV_PMODE_MIXED_MV)
00950 {
00951 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
00952 if (status < 0) return -1;
00953 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
00954 "Imode: %i, Invert: %i\n", status>>1, status&1);
00955 } else {
00956 v->mv_type_is_raw = 0;
00957 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
00958 }
00959 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
00960 if (status < 0) return -1;
00961 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
00962 "Imode: %i, Invert: %i\n", status>>1, status&1);
00963
00964
00965 v->s.mv_table_index = get_bits(gb, 2);
00966 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
00967 if (v->dquant)
00968 {
00969 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
00970 vop_dquant_decoding(v);
00971 }
00972
00973 v->ttfrm = 0;
00974 if (v->vstransform)
00975 {
00976 v->ttmbf = get_bits1(gb);
00977 if (v->ttmbf)
00978 {
00979 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
00980 }
00981 } else {
00982 v->ttmbf = 1;
00983 v->ttfrm = TT_8X8;
00984 }
00985 break;
00986 case FF_B_TYPE:
00987 if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
00988 else v->mvrange = 0;
00989 v->k_x = v->mvrange + 9 + (v->mvrange >> 1);
00990 v->k_y = v->mvrange + 8;
00991 v->range_x = 1 << (v->k_x - 1);
00992 v->range_y = 1 << (v->k_y - 1);
00993
00994 if (v->pq < 5) v->tt_index = 0;
00995 else if(v->pq < 13) v->tt_index = 1;
00996 else v->tt_index = 2;
00997
00998 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
00999 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
01000 v->s.mspel = v->s.quarter_sample;
01001
01002 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
01003 if (status < 0) return -1;
01004 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
01005 "Imode: %i, Invert: %i\n", status>>1, status&1);
01006 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
01007 if (status < 0) return -1;
01008 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
01009 "Imode: %i, Invert: %i\n", status>>1, status&1);
01010
01011 v->s.mv_table_index = get_bits(gb, 2);
01012 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
01013
01014 if (v->dquant)
01015 {
01016 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01017 vop_dquant_decoding(v);
01018 }
01019
01020 v->ttfrm = 0;
01021 if (v->vstransform)
01022 {
01023 v->ttmbf = get_bits1(gb);
01024 if (v->ttmbf)
01025 {
01026 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
01027 }
01028 } else {
01029 v->ttmbf = 1;
01030 v->ttfrm = TT_8X8;
01031 }
01032 break;
01033 }
01034
01035
01036 v->c_ac_table_index = decode012(gb);
01037 if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
01038 {
01039 v->y_ac_table_index = decode012(gb);
01040 }
01041
01042 v->s.dc_table_index = get_bits1(gb);
01043 if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
01044 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
01045 vop_dquant_decoding(v);
01046 }
01047
01048 v->bi_type = 0;
01049 if(v->s.pict_type == FF_BI_TYPE) {
01050 v->s.pict_type = FF_B_TYPE;
01051 v->bi_type = 1;
01052 }
01053 return 0;
01054 }