2 * VC-1 and WMV3 decoder common code
3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * VC-1 and WMV3 decoder common code
31 #include "mpegvideo.h"
34 #include "msmpeg4data.h"
36 #include "simple_idct.h"
41 /***********************************************************************/
43 * @name VC-1 Bitplane decoding
61 /** @} */ //imode defines
63 /** Decode rows by checking if they are skipped
64 * @param plane Buffer to store decoded bits
65 * @param[in] width Width of this buffer
66 * @param[in] height Height of this buffer
67 * @param[in] stride of this buffer
69 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
72 for (y=0; y<height; y++){
73 if (!get_bits1(gb)) //rowskip
74 memset(plane, 0, width);
76 for (x=0; x<width; x++)
77 plane[x] = get_bits1(gb);
82 /** Decode columns by checking if they are skipped
83 * @param plane Buffer to store decoded bits
84 * @param[in] width Width of this buffer
85 * @param[in] height Height of this buffer
86 * @param[in] stride of this buffer
87 * @todo FIXME: Optimize
89 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
92 for (x=0; x<width; x++){
93 if (!get_bits1(gb)) //colskip
94 for (y=0; y<height; y++)
97 for (y=0; y<height; y++)
98 plane[y*stride] = get_bits1(gb);
103 /** Decode a bitplane's bits
104 * @param data bitplane where to store the decode bits
105 * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
106 * @param v VC-1 context for bit reading and logging
108 * @todo FIXME: Optimize
110 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
112 GetBitContext *gb = &v->s.gb;
114 int imode, x, y, code, offset;
115 uint8_t invert, *planep = data;
116 int width, height, stride;
118 width = v->s.mb_width;
119 height = v->s.mb_height;
120 stride = v->s.mb_stride;
121 invert = get_bits1(gb);
122 imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
128 //Data is actually read in the MB layer (same for all tests == "raw")
129 *raw_flag = 1; //invert ignored
133 if ((height * width) & 1)
135 *planep++ = get_bits1(gb);
139 // decode bitplane as one long line
140 for (y = offset; y < height * width; y += 2) {
141 code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
142 *planep++ = code & 1;
144 if(offset == width) {
146 planep += stride - width;
148 *planep++ = code >> 1;
150 if(offset == width) {
152 planep += stride - width;
158 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
159 for(y = 0; y < height; y+= 3) {
160 for(x = width & 1; x < width; x += 2) {
161 code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
163 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
166 planep[x + 0] = (code >> 0) & 1;
167 planep[x + 1] = (code >> 1) & 1;
168 planep[x + 0 + stride] = (code >> 2) & 1;
169 planep[x + 1 + stride] = (code >> 3) & 1;
170 planep[x + 0 + stride * 2] = (code >> 4) & 1;
171 planep[x + 1 + stride * 2] = (code >> 5) & 1;
173 planep += stride * 3;
175 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
177 planep += (height & 1) * stride;
178 for(y = height & 1; y < height; y += 2) {
179 for(x = width % 3; x < width; x += 3) {
180 code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
182 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
185 planep[x + 0] = (code >> 0) & 1;
186 planep[x + 1] = (code >> 1) & 1;
187 planep[x + 2] = (code >> 2) & 1;
188 planep[x + 0 + stride] = (code >> 3) & 1;
189 planep[x + 1 + stride] = (code >> 4) & 1;
190 planep[x + 2 + stride] = (code >> 5) & 1;
192 planep += stride * 2;
195 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
196 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
200 decode_rowskip(data, width, height, stride, &v->s.gb);
203 decode_colskip(data, width, height, stride, &v->s.gb);
208 /* Applying diff operator */
209 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
213 for (x=1; x<width; x++)
214 planep[x] ^= planep[x-1];
215 for (y=1; y<height; y++)
218 planep[0] ^= planep[-stride];
219 for (x=1; x<width; x++)
221 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
222 else planep[x] ^= planep[x-1];
229 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
231 return (imode<<1) + invert;
234 /** @} */ //Bitplane group
236 /***********************************************************************/
237 /** VOP Dquant decoding
238 * @param v VC-1 Context
240 static int vop_dquant_decoding(VC1Context *v)
242 GetBitContext *gb = &v->s.gb;
248 pqdiff = get_bits(gb, 3);
249 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
250 else v->altpq = v->pq + pqdiff + 1;
254 v->dquantfrm = get_bits1(gb);
257 v->dqprofile = get_bits(gb, 2);
258 switch (v->dqprofile)
260 case DQPROFILE_SINGLE_EDGE:
261 case DQPROFILE_DOUBLE_EDGES:
262 v->dqsbedge = get_bits(gb, 2);
264 case DQPROFILE_ALL_MBS:
265 v->dqbilevel = get_bits1(gb);
268 default: break; //Forbidden ?
270 if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
272 pqdiff = get_bits(gb, 3);
273 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
274 else v->altpq = v->pq + pqdiff + 1;
281 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
284 * Decode Simple/Main Profiles sequence header
285 * @see Figure 7-8, p16-17
286 * @param avctx Codec context
287 * @param gb GetBit context initialized from Codec context extra_data
290 int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
292 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
293 v->profile = get_bits(gb, 2);
294 if (v->profile == PROFILE_COMPLEX)
296 av_log(avctx, AV_LOG_WARNING, "WMV3 Complex Profile is not fully supported\n");
299 if (v->profile == PROFILE_ADVANCED)
301 v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
302 v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
303 return decode_sequence_header_adv(v, gb);
307 v->zz_8x4 = wmv2_scantableA;
308 v->zz_4x8 = wmv2_scantableB;
309 v->res_y411 = get_bits1(gb);
310 v->res_sprite = get_bits1(gb);
313 av_log(avctx, AV_LOG_ERROR,
314 "Old interlaced mode is not supported\n");
320 v->frmrtq_postproc = get_bits(gb, 3); //common
321 // (bitrate-32kbps)/64kbps
322 v->bitrtq_postproc = get_bits(gb, 5); //common
323 v->s.loop_filter = get_bits1(gb); //common
324 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
326 av_log(avctx, AV_LOG_ERROR,
327 "LOOPFILTER shall not be enabled in Simple Profile\n");
329 if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
330 v->s.loop_filter = 0;
332 v->res_x8 = get_bits1(gb); //reserved
333 v->multires = get_bits1(gb);
334 v->res_fasttx = get_bits1(gb);
337 v->vc1dsp.vc1_inv_trans_8x8 = ff_simple_idct_8;
338 v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
339 v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
340 v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
341 v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add_8;
342 v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
343 v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
344 v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
347 v->fastuvmc = get_bits1(gb); //common
348 if (!v->profile && !v->fastuvmc)
350 av_log(avctx, AV_LOG_ERROR,
351 "FASTUVMC unavailable in Simple Profile\n");
354 v->extended_mv = get_bits1(gb); //common
355 if (!v->profile && v->extended_mv)
357 av_log(avctx, AV_LOG_ERROR,
358 "Extended MVs unavailable in Simple Profile\n");
361 v->dquant = get_bits(gb, 2); //common
362 v->vstransform = get_bits1(gb); //common
364 v->res_transtab = get_bits1(gb);
367 av_log(avctx, AV_LOG_ERROR,
368 "1 for reserved RES_TRANSTAB is forbidden\n");
372 v->overlap = get_bits1(gb); //common
374 v->s.resync_marker = get_bits1(gb);
375 v->rangered = get_bits1(gb);
376 if (v->rangered && v->profile == PROFILE_SIMPLE)
378 av_log(avctx, AV_LOG_INFO,
379 "RANGERED should be set to 0 in Simple Profile\n");
382 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
383 v->quantizer_mode = get_bits(gb, 2); //common
385 v->finterpflag = get_bits1(gb); //common
388 v->s.avctx->width = v->s.avctx->coded_width = get_bits(gb, 11);
389 v->s.avctx->height = v->s.avctx->coded_height = get_bits(gb, 11);
390 skip_bits(gb, 5); //frame rate
391 v->res_x8 = get_bits1(gb);
392 if (get_bits1(gb)) { // something to do with DC VLC selection
393 av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
396 skip_bits(gb, 3); //slice code
399 v->res_rtm_flag = get_bits1(gb); //reserved
401 if (!v->res_rtm_flag)
403 // av_log(avctx, AV_LOG_ERROR,
404 // "0 for reserved RES_RTM_FLAG is forbidden\n");
405 av_log(avctx, AV_LOG_ERROR,
406 "Old WMV3 version detected, some frames may be decoded incorrectly\n");
409 //TODO: figure out what they mean (always 0x402F)
410 if(!v->res_fasttx) skip_bits(gb, 16);
411 av_log(avctx, AV_LOG_DEBUG,
412 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
413 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
414 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
415 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
416 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
417 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
418 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
419 v->dquant, v->quantizer_mode, avctx->max_b_frames
424 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
427 v->level = get_bits(gb, 3);
430 av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
432 v->chromaformat = get_bits(gb, 2);
433 if (v->chromaformat != 1)
435 av_log(v->s.avctx, AV_LOG_ERROR,
436 "Only 4:2:0 chroma format supported\n");
441 v->frmrtq_postproc = get_bits(gb, 3); //common
442 // (bitrate-32kbps)/64kbps
443 v->bitrtq_postproc = get_bits(gb, 5); //common
444 v->postprocflag = get_bits1(gb); //common
446 v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
447 v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
448 v->s.avctx->width = v->s.avctx->coded_width;
449 v->s.avctx->height = v->s.avctx->coded_height;
450 v->broadcast = get_bits1(gb);
451 v->interlace = get_bits1(gb);
452 v->tfcntrflag = get_bits1(gb);
453 v->finterpflag = get_bits1(gb);
454 skip_bits1(gb); // reserved
456 av_log(v->s.avctx, AV_LOG_DEBUG,
457 "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
458 "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
459 "TFCTRflag=%i, FINTERPflag=%i\n",
460 v->level, v->frmrtq_postproc, v->bitrtq_postproc,
461 v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
462 v->tfcntrflag, v->finterpflag
465 v->psf = get_bits1(gb);
466 if(v->psf) { //PsF, 6.1.13
467 av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
470 v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
471 if(get_bits1(gb)) { //Display Info - decoding is not affected by it
473 av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
474 w = get_bits(gb, 14) + 1;
475 h = get_bits(gb, 14) + 1;
476 av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
478 ar = get_bits(gb, 4);
480 v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
482 w = get_bits(gb, 8) + 1;
483 h = get_bits(gb, 8) + 1;
484 v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
486 av_reduce(&v->s.avctx->sample_aspect_ratio.num,
487 &v->s.avctx->sample_aspect_ratio.den,
488 v->s.avctx->height * w,
489 v->s.avctx->width * h,
492 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);
494 if(get_bits1(gb)){ //framerate stuff
496 v->s.avctx->time_base.num = 32;
497 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
500 nr = get_bits(gb, 8);
501 dr = get_bits(gb, 4);
502 if(nr && nr < 8 && dr && dr < 3){
503 v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
504 v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
507 if(v->broadcast) { // Pulldown may be present
508 v->s.avctx->time_base.den *= 2;
509 v->s.avctx->ticks_per_frame = 2;
514 v->color_prim = get_bits(gb, 8);
515 v->transfer_char = get_bits(gb, 8);
516 v->matrix_coef = get_bits(gb, 8);
520 v->hrd_param_flag = get_bits1(gb);
521 if(v->hrd_param_flag) {
523 v->hrd_num_leaky_buckets = get_bits(gb, 5);
524 skip_bits(gb, 4); //bitrate exponent
525 skip_bits(gb, 4); //buffer size exponent
526 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
527 skip_bits(gb, 16); //hrd_rate[n]
528 skip_bits(gb, 16); //hrd_buffer[n]
534 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
538 av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
539 v->broken_link = get_bits1(gb);
540 v->closed_entry = get_bits1(gb);
541 v->panscanflag = get_bits1(gb);
542 v->refdist_flag = get_bits1(gb);
543 v->s.loop_filter = get_bits1(gb);
544 v->fastuvmc = get_bits1(gb);
545 v->extended_mv = get_bits1(gb);
546 v->dquant = get_bits(gb, 2);
547 v->vstransform = get_bits1(gb);
548 v->overlap = get_bits1(gb);
549 v->quantizer_mode = get_bits(gb, 2);
551 if(v->hrd_param_flag){
552 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
553 skip_bits(gb, 8); //hrd_full[n]
558 avctx->width = avctx->coded_width = (get_bits(gb, 12)+1)<<1;
559 avctx->height = avctx->coded_height = (get_bits(gb, 12)+1)<<1;
562 v->extended_dmv = get_bits1(gb);
563 if((v->range_mapy_flag = get_bits1(gb))) {
564 av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
565 v->range_mapy = get_bits(gb, 3);
567 if((v->range_mapuv_flag = get_bits1(gb))) {
568 av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
569 v->range_mapuv = get_bits(gb, 3);
572 av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
573 "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
574 "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
575 "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
576 v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
577 v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
582 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
584 int pqindex, lowquant, status;
586 if(v->finterpflag) v->interpfrm = get_bits1(gb);
587 skip_bits(gb, 2); //framecnt unused
589 if (v->rangered) v->rangeredfrm = get_bits1(gb);
590 v->s.pict_type = get_bits1(gb);
591 if (v->s.avctx->max_b_frames) {
592 if (!v->s.pict_type) {
593 if (get_bits1(gb)) v->s.pict_type = AV_PICTURE_TYPE_I;
594 else v->s.pict_type = AV_PICTURE_TYPE_B;
595 } else v->s.pict_type = AV_PICTURE_TYPE_P;
596 } else v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
599 if(v->s.pict_type == AV_PICTURE_TYPE_B) {
600 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
601 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
602 if(v->bfraction == 0) {
603 v->s.pict_type = AV_PICTURE_TYPE_BI;
606 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
607 skip_bits(gb, 7); // skip buffer fullness
613 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
615 if(v->s.pict_type == AV_PICTURE_TYPE_P)
618 /* Quantizer stuff */
619 pqindex = get_bits(gb, 5);
620 if(!pqindex) return -1;
621 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
622 v->pq = ff_vc1_pquant_table[0][pqindex];
624 v->pq = ff_vc1_pquant_table[1][pqindex];
627 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
628 v->pquantizer = pqindex < 9;
629 if (v->quantizer_mode == QUANT_NON_UNIFORM)
631 v->pqindex = pqindex;
632 if (pqindex < 9) v->halfpq = get_bits1(gb);
634 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
635 v->pquantizer = get_bits1(gb);
637 if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
638 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
639 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
640 v->range_x = 1 << (v->k_x - 1);
641 v->range_y = 1 << (v->k_y - 1);
642 if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B) v->respic = get_bits(gb, 2);
644 if(v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)){
645 v->x8_type = get_bits1(gb);
646 }else v->x8_type = 0;
647 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
648 // (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);
650 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
652 switch(v->s.pict_type) {
653 case AV_PICTURE_TYPE_P:
654 if (v->pq < 5) v->tt_index = 0;
655 else if(v->pq < 13) v->tt_index = 1;
656 else v->tt_index = 2;
658 lowquant = (v->pq > 12) ? 0 : 1;
659 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
660 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
663 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
664 v->lumscale = get_bits(gb, 6);
665 v->lumshift = get_bits(gb, 6);
667 /* fill lookup tables for intensity compensation */
670 shift = (255 - v->lumshift * 2) << 6;
674 scale = v->lumscale + 32;
676 shift = (v->lumshift - 64) << 6;
678 shift = v->lumshift << 6;
680 for(i = 0; i < 256; i++) {
681 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
682 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
685 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
686 v->s.quarter_sample = 0;
687 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
688 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
689 v->s.quarter_sample = 0;
691 v->s.quarter_sample = 1;
693 v->s.quarter_sample = 1;
694 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));
696 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
697 v->mv_mode2 == MV_PMODE_MIXED_MV)
698 || v->mv_mode == MV_PMODE_MIXED_MV)
700 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
701 if (status < 0) return -1;
702 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
703 "Imode: %i, Invert: %i\n", status>>1, status&1);
705 v->mv_type_is_raw = 0;
706 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
708 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
709 if (status < 0) return -1;
710 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
711 "Imode: %i, Invert: %i\n", status>>1, status&1);
713 /* Hopefully this is correct for P frames */
714 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
715 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
719 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
720 vop_dquant_decoding(v);
723 v->ttfrm = 0; //FIXME Is that so ?
726 v->ttmbf = get_bits1(gb);
729 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
736 case AV_PICTURE_TYPE_B:
737 if (v->pq < 5) v->tt_index = 0;
738 else if(v->pq < 13) v->tt_index = 1;
739 else v->tt_index = 2;
741 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
742 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
743 v->s.mspel = v->s.quarter_sample;
745 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
746 if (status < 0) return -1;
747 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
748 "Imode: %i, Invert: %i\n", status>>1, status&1);
749 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
750 if (status < 0) return -1;
751 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
752 "Imode: %i, Invert: %i\n", status>>1, status&1);
754 v->s.mv_table_index = get_bits(gb, 2);
755 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
759 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
760 vop_dquant_decoding(v);
766 v->ttmbf = get_bits1(gb);
769 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
781 v->c_ac_table_index = decode012(gb);
782 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
784 v->y_ac_table_index = decode012(gb);
787 v->s.dc_table_index = get_bits1(gb);
790 if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
791 v->s.pict_type = AV_PICTURE_TYPE_B;
797 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
799 int pqindex, lowquant;
802 v->p_frame_skipped = 0;
805 v->fcm = decode012(gb);
807 if(!v->warn_interlaced++)
808 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
812 switch(get_unary(gb, 0, 4)) {
814 v->s.pict_type = AV_PICTURE_TYPE_P;
817 v->s.pict_type = AV_PICTURE_TYPE_B;
820 v->s.pict_type = AV_PICTURE_TYPE_I;
823 v->s.pict_type = AV_PICTURE_TYPE_BI;
826 v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
827 v->p_frame_skipped = 1;
833 if(!v->interlace || v->psf) {
834 v->rptfrm = get_bits(gb, 2);
836 v->tff = get_bits1(gb);
837 v->rff = get_bits1(gb);
841 av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
844 if(v->p_frame_skipped) {
847 v->rnd = get_bits1(gb);
849 v->uvsamp = get_bits1(gb);
850 if(v->finterpflag) v->interpfrm = get_bits1(gb);
851 if(v->s.pict_type == AV_PICTURE_TYPE_B) {
852 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
853 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
854 if(v->bfraction == 0) {
855 v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
858 pqindex = get_bits(gb, 5);
859 if(!pqindex) return -1;
860 v->pqindex = pqindex;
861 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
862 v->pq = ff_vc1_pquant_table[0][pqindex];
864 v->pq = ff_vc1_pquant_table[1][pqindex];
867 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
868 v->pquantizer = pqindex < 9;
869 if (v->quantizer_mode == QUANT_NON_UNIFORM)
871 v->pqindex = pqindex;
872 if (pqindex < 9) v->halfpq = get_bits1(gb);
874 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
875 v->pquantizer = get_bits1(gb);
877 v->postproc = get_bits(gb, 2);
879 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
884 switch(v->s.pict_type) {
885 case AV_PICTURE_TYPE_I:
886 case AV_PICTURE_TYPE_BI:
887 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
888 if (status < 0) return -1;
889 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
890 "Imode: %i, Invert: %i\n", status>>1, status&1);
891 v->condover = CONDOVER_NONE;
892 if(v->overlap && v->pq <= 8) {
893 v->condover = decode012(gb);
894 if(v->condover == CONDOVER_SELECT) {
895 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
896 if (status < 0) return -1;
897 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
898 "Imode: %i, Invert: %i\n", status>>1, status&1);
902 case AV_PICTURE_TYPE_P:
903 if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
905 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
906 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
907 v->range_x = 1 << (v->k_x - 1);
908 v->range_y = 1 << (v->k_y - 1);
910 if (v->pq < 5) v->tt_index = 0;
911 else if(v->pq < 13) v->tt_index = 1;
912 else v->tt_index = 2;
914 lowquant = (v->pq > 12) ? 0 : 1;
915 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
916 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
919 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
920 v->lumscale = get_bits(gb, 6);
921 v->lumshift = get_bits(gb, 6);
922 /* fill lookup tables for intensity compensation */
925 shift = (255 - v->lumshift * 2) << 6;
929 scale = v->lumscale + 32;
931 shift = (v->lumshift - 64) << 6;
933 shift = v->lumshift << 6;
935 for(i = 0; i < 256; i++) {
936 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
937 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
941 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
942 v->s.quarter_sample = 0;
943 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
944 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
945 v->s.quarter_sample = 0;
947 v->s.quarter_sample = 1;
949 v->s.quarter_sample = 1;
950 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));
952 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
953 v->mv_mode2 == MV_PMODE_MIXED_MV)
954 || v->mv_mode == MV_PMODE_MIXED_MV)
956 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
957 if (status < 0) return -1;
958 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
959 "Imode: %i, Invert: %i\n", status>>1, status&1);
961 v->mv_type_is_raw = 0;
962 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
964 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
965 if (status < 0) return -1;
966 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
967 "Imode: %i, Invert: %i\n", status>>1, status&1);
969 /* Hopefully this is correct for P frames */
970 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
971 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
974 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
975 vop_dquant_decoding(v);
978 v->ttfrm = 0; //FIXME Is that so ?
981 v->ttmbf = get_bits1(gb);
984 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
991 case AV_PICTURE_TYPE_B:
992 if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
994 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
995 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
996 v->range_x = 1 << (v->k_x - 1);
997 v->range_y = 1 << (v->k_y - 1);
999 if (v->pq < 5) v->tt_index = 0;
1000 else if(v->pq < 13) v->tt_index = 1;
1001 else v->tt_index = 2;
1003 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1004 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1005 v->s.mspel = v->s.quarter_sample;
1007 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1008 if (status < 0) return -1;
1009 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1010 "Imode: %i, Invert: %i\n", status>>1, status&1);
1011 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1012 if (status < 0) return -1;
1013 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1014 "Imode: %i, Invert: %i\n", status>>1, status&1);
1016 v->s.mv_table_index = get_bits(gb, 2);
1017 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1021 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1022 vop_dquant_decoding(v);
1028 v->ttmbf = get_bits1(gb);
1031 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1041 v->c_ac_table_index = decode012(gb);
1042 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
1044 v->y_ac_table_index = decode012(gb);
1047 v->s.dc_table_index = get_bits1(gb);
1048 if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) && v->dquant) {
1049 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1050 vop_dquant_decoding(v);
1054 if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
1055 v->s.pict_type = AV_PICTURE_TYPE_B;