2 * VC-1 and WMV3 decoder common code
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder common code
32 #include "mpegvideo.h"
35 #include "msmpeg4data.h"
37 #include "simple_idct.h"
42 /***********************************************************************/
44 * @name VC-1 Bitplane decoding
62 /** @} */ //imode defines
64 /** Decode rows by checking if they are skipped
65 * @param plane Buffer to store decoded bits
66 * @param[in] width Width of this buffer
67 * @param[in] height Height of this buffer
68 * @param[in] stride of this buffer
70 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
73 for (y=0; y<height; y++){
74 if (!get_bits1(gb)) //rowskip
75 memset(plane, 0, width);
77 for (x=0; x<width; x++)
78 plane[x] = get_bits1(gb);
83 /** Decode columns by checking if they are skipped
84 * @param plane Buffer to store decoded bits
85 * @param[in] width Width of this buffer
86 * @param[in] height Height of this buffer
87 * @param[in] stride of this buffer
88 * @todo FIXME: Optimize
90 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
93 for (x=0; x<width; x++){
94 if (!get_bits1(gb)) //colskip
95 for (y=0; y<height; y++)
98 for (y=0; y<height; y++)
99 plane[y*stride] = get_bits1(gb);
104 /** Decode a bitplane's bits
105 * @param data bitplane where to store the decode bits
106 * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
107 * @param v VC-1 context for bit reading and logging
109 * @todo FIXME: Optimize
111 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
113 GetBitContext *gb = &v->s.gb;
115 int imode, x, y, code, offset;
116 uint8_t invert, *planep = data;
117 int width, height, stride;
119 width = v->s.mb_width;
120 height = v->s.mb_height >> v->field_mode;
121 stride = v->s.mb_stride;
122 invert = get_bits1(gb);
123 imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
129 //Data is actually read in the MB layer (same for all tests == "raw")
130 *raw_flag = 1; //invert ignored
134 if ((height * width) & 1)
136 *planep++ = get_bits1(gb);
140 // decode bitplane as one long line
141 for (y = offset; y < height * width; y += 2) {
142 code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
143 *planep++ = code & 1;
145 if(offset == width) {
147 planep += stride - width;
149 *planep++ = code >> 1;
151 if(offset == width) {
153 planep += stride - width;
159 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
160 for(y = 0; y < height; y+= 3) {
161 for(x = width & 1; x < width; x += 2) {
162 code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
164 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
167 planep[x + 0] = (code >> 0) & 1;
168 planep[x + 1] = (code >> 1) & 1;
169 planep[x + 0 + stride] = (code >> 2) & 1;
170 planep[x + 1 + stride] = (code >> 3) & 1;
171 planep[x + 0 + stride * 2] = (code >> 4) & 1;
172 planep[x + 1 + stride * 2] = (code >> 5) & 1;
174 planep += stride * 3;
176 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
178 planep += (height & 1) * stride;
179 for(y = height & 1; y < height; y += 2) {
180 for(x = width % 3; x < width; x += 3) {
181 code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
183 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
186 planep[x + 0] = (code >> 0) & 1;
187 planep[x + 1] = (code >> 1) & 1;
188 planep[x + 2] = (code >> 2) & 1;
189 planep[x + 0 + stride] = (code >> 3) & 1;
190 planep[x + 1 + stride] = (code >> 4) & 1;
191 planep[x + 2 + stride] = (code >> 5) & 1;
193 planep += stride * 2;
196 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
197 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
201 decode_rowskip(data, width, height, stride, &v->s.gb);
204 decode_colskip(data, width, height, stride, &v->s.gb);
209 /* Applying diff operator */
210 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
214 for (x=1; x<width; x++)
215 planep[x] ^= planep[x-1];
216 for (y=1; y<height; y++)
219 planep[0] ^= planep[-stride];
220 for (x=1; x<width; x++)
222 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
223 else planep[x] ^= planep[x-1];
230 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
232 return (imode<<1) + invert;
235 /** @} */ //Bitplane group
237 /***********************************************************************/
238 /** VOP Dquant decoding
239 * @param v VC-1 Context
241 static int vop_dquant_decoding(VC1Context *v)
243 GetBitContext *gb = &v->s.gb;
249 pqdiff = get_bits(gb, 3);
250 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
251 else v->altpq = v->pq + pqdiff + 1;
255 v->dquantfrm = get_bits1(gb);
258 v->dqprofile = get_bits(gb, 2);
259 switch (v->dqprofile)
261 case DQPROFILE_SINGLE_EDGE:
262 case DQPROFILE_DOUBLE_EDGES:
263 v->dqsbedge = get_bits(gb, 2);
265 case DQPROFILE_ALL_MBS:
266 v->dqbilevel = get_bits1(gb);
269 default: break; //Forbidden ?
271 if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
273 pqdiff = get_bits(gb, 3);
274 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
275 else v->altpq = v->pq + pqdiff + 1;
282 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
285 * Decode Simple/Main Profiles sequence header
286 * @see Figure 7-8, p16-17
287 * @param avctx Codec context
288 * @param gb GetBit context initialized from Codec context extra_data
291 int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
293 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
294 v->profile = get_bits(gb, 2);
295 if (v->profile == PROFILE_COMPLEX)
297 av_log(avctx, AV_LOG_WARNING, "WMV3 Complex Profile is not fully supported\n");
300 if (v->profile == PROFILE_ADVANCED)
302 v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
303 v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
304 return decode_sequence_header_adv(v, gb);
308 v->zz_8x4 = wmv2_scantableA;
309 v->zz_4x8 = wmv2_scantableB;
310 v->res_y411 = get_bits1(gb);
311 v->res_sprite = get_bits1(gb);
314 av_log(avctx, AV_LOG_ERROR,
315 "Old interlaced mode is not supported\n");
321 v->frmrtq_postproc = get_bits(gb, 3); //common
322 // (bitrate-32kbps)/64kbps
323 v->bitrtq_postproc = get_bits(gb, 5); //common
324 v->s.loop_filter = get_bits1(gb); //common
325 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
327 av_log(avctx, AV_LOG_ERROR,
328 "LOOPFILTER shall not be enabled in Simple Profile\n");
330 if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
331 v->s.loop_filter = 0;
333 v->res_x8 = get_bits1(gb); //reserved
334 v->multires = get_bits1(gb);
335 v->res_fasttx = get_bits1(gb);
338 v->vc1dsp.vc1_inv_trans_8x8 = ff_simple_idct_8;
339 v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
340 v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
341 v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
342 v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add_8;
343 v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
344 v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
345 v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
348 v->fastuvmc = get_bits1(gb); //common
349 if (!v->profile && !v->fastuvmc)
351 av_log(avctx, AV_LOG_ERROR,
352 "FASTUVMC unavailable in Simple Profile\n");
355 v->extended_mv = get_bits1(gb); //common
356 if (!v->profile && v->extended_mv)
358 av_log(avctx, AV_LOG_ERROR,
359 "Extended MVs unavailable in Simple Profile\n");
362 v->dquant = get_bits(gb, 2); //common
363 v->vstransform = get_bits1(gb); //common
365 v->res_transtab = get_bits1(gb);
368 av_log(avctx, AV_LOG_ERROR,
369 "1 for reserved RES_TRANSTAB is forbidden\n");
373 v->overlap = get_bits1(gb); //common
375 v->s.resync_marker = get_bits1(gb);
376 v->rangered = get_bits1(gb);
377 if (v->rangered && v->profile == PROFILE_SIMPLE)
379 av_log(avctx, AV_LOG_INFO,
380 "RANGERED should be set to 0 in Simple Profile\n");
383 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
384 v->quantizer_mode = get_bits(gb, 2); //common
386 v->finterpflag = get_bits1(gb); //common
389 v->s.avctx->width = v->s.avctx->coded_width = get_bits(gb, 11);
390 v->s.avctx->height = v->s.avctx->coded_height = get_bits(gb, 11);
391 skip_bits(gb, 5); //frame rate
392 v->res_x8 = get_bits1(gb);
393 if (get_bits1(gb)) { // something to do with DC VLC selection
394 av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
397 skip_bits(gb, 3); //slice code
400 v->res_rtm_flag = get_bits1(gb); //reserved
402 if (!v->res_rtm_flag)
404 // av_log(avctx, AV_LOG_ERROR,
405 // "0 for reserved RES_RTM_FLAG is forbidden\n");
406 av_log(avctx, AV_LOG_ERROR,
407 "Old WMV3 version detected, some frames may be decoded incorrectly\n");
410 //TODO: figure out what they mean (always 0x402F)
411 if(!v->res_fasttx) skip_bits(gb, 16);
412 av_log(avctx, AV_LOG_DEBUG,
413 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
414 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
415 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
416 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
417 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
418 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
419 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
420 v->dquant, v->quantizer_mode, avctx->max_b_frames
425 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
428 v->level = get_bits(gb, 3);
431 av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
433 v->chromaformat = get_bits(gb, 2);
434 if (v->chromaformat != 1)
436 av_log(v->s.avctx, AV_LOG_ERROR,
437 "Only 4:2:0 chroma format supported\n");
442 v->frmrtq_postproc = get_bits(gb, 3); //common
443 // (bitrate-32kbps)/64kbps
444 v->bitrtq_postproc = get_bits(gb, 5); //common
445 v->postprocflag = get_bits1(gb); //common
447 v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
448 v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
449 v->s.avctx->width = v->s.avctx->coded_width;
450 v->s.avctx->height = v->s.avctx->coded_height;
451 v->broadcast = get_bits1(gb);
452 v->interlace = get_bits1(gb);
453 v->tfcntrflag = get_bits1(gb);
454 v->finterpflag = get_bits1(gb);
455 skip_bits1(gb); // reserved
457 av_log(v->s.avctx, AV_LOG_DEBUG,
458 "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
459 "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
460 "TFCTRflag=%i, FINTERPflag=%i\n",
461 v->level, v->frmrtq_postproc, v->bitrtq_postproc,
462 v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
463 v->tfcntrflag, v->finterpflag
466 v->psf = get_bits1(gb);
467 if(v->psf) { //PsF, 6.1.13
468 av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
471 v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
472 if(get_bits1(gb)) { //Display Info - decoding is not affected by it
474 av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
475 w = get_bits(gb, 14) + 1;
476 h = get_bits(gb, 14) + 1;
477 av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
479 ar = get_bits(gb, 4);
481 v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
483 w = get_bits(gb, 8) + 1;
484 h = get_bits(gb, 8) + 1;
485 v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
487 av_reduce(&v->s.avctx->sample_aspect_ratio.num,
488 &v->s.avctx->sample_aspect_ratio.den,
489 v->s.avctx->height * w,
490 v->s.avctx->width * h,
493 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);
495 if(get_bits1(gb)){ //framerate stuff
497 v->s.avctx->time_base.num = 32;
498 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
501 nr = get_bits(gb, 8);
502 dr = get_bits(gb, 4);
503 if(nr && nr < 8 && dr && dr < 3){
504 v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
505 v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
508 if(v->broadcast) { // Pulldown may be present
509 v->s.avctx->time_base.den *= 2;
510 v->s.avctx->ticks_per_frame = 2;
515 v->color_prim = get_bits(gb, 8);
516 v->transfer_char = get_bits(gb, 8);
517 v->matrix_coef = get_bits(gb, 8);
521 v->hrd_param_flag = get_bits1(gb);
522 if(v->hrd_param_flag) {
524 v->hrd_num_leaky_buckets = get_bits(gb, 5);
525 skip_bits(gb, 4); //bitrate exponent
526 skip_bits(gb, 4); //buffer size exponent
527 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
528 skip_bits(gb, 16); //hrd_rate[n]
529 skip_bits(gb, 16); //hrd_buffer[n]
535 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
539 av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
540 v->broken_link = get_bits1(gb);
541 v->closed_entry = get_bits1(gb);
542 v->panscanflag = get_bits1(gb);
543 v->refdist_flag = get_bits1(gb);
544 v->s.loop_filter = get_bits1(gb);
545 v->fastuvmc = get_bits1(gb);
546 v->extended_mv = get_bits1(gb);
547 v->dquant = get_bits(gb, 2);
548 v->vstransform = get_bits1(gb);
549 v->overlap = get_bits1(gb);
550 v->quantizer_mode = get_bits(gb, 2);
552 if(v->hrd_param_flag){
553 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
554 skip_bits(gb, 8); //hrd_full[n]
559 avctx->width = avctx->coded_width = (get_bits(gb, 12)+1)<<1;
560 avctx->height = avctx->coded_height = (get_bits(gb, 12)+1)<<1;
563 v->extended_dmv = get_bits1(gb);
564 if((v->range_mapy_flag = get_bits1(gb))) {
565 av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
566 v->range_mapy = get_bits(gb, 3);
568 if((v->range_mapuv_flag = get_bits1(gb))) {
569 av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
570 v->range_mapuv = get_bits(gb, 3);
573 av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
574 "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
575 "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
576 "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
577 v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
578 v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
583 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
585 int pqindex, lowquant, status;
587 if(v->finterpflag) v->interpfrm = get_bits1(gb);
588 skip_bits(gb, 2); //framecnt unused
590 if (v->rangered) v->rangeredfrm = get_bits1(gb);
591 v->s.pict_type = get_bits1(gb);
592 if (v->s.avctx->max_b_frames) {
593 if (!v->s.pict_type) {
594 if (get_bits1(gb)) v->s.pict_type = AV_PICTURE_TYPE_I;
595 else v->s.pict_type = AV_PICTURE_TYPE_B;
596 } else v->s.pict_type = AV_PICTURE_TYPE_P;
597 } else v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
600 if(v->s.pict_type == AV_PICTURE_TYPE_B) {
601 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
602 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
603 if(v->bfraction == 0) {
604 v->s.pict_type = AV_PICTURE_TYPE_BI;
607 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
608 skip_bits(gb, 7); // skip buffer fullness
614 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
616 if(v->s.pict_type == AV_PICTURE_TYPE_P)
619 /* Quantizer stuff */
620 pqindex = get_bits(gb, 5);
621 if(!pqindex) return -1;
622 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
623 v->pq = ff_vc1_pquant_table[0][pqindex];
625 v->pq = ff_vc1_pquant_table[1][pqindex];
628 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
629 v->pquantizer = pqindex < 9;
630 if (v->quantizer_mode == QUANT_NON_UNIFORM)
632 v->pqindex = pqindex;
633 if (pqindex < 9) v->halfpq = get_bits1(gb);
635 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
636 v->pquantizer = get_bits1(gb);
638 if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
639 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
640 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
641 v->range_x = 1 << (v->k_x - 1);
642 v->range_y = 1 << (v->k_y - 1);
643 if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B) v->respic = get_bits(gb, 2);
645 if(v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)){
646 v->x8_type = get_bits1(gb);
647 }else v->x8_type = 0;
648 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
649 // (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);
651 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
653 switch(v->s.pict_type) {
654 case AV_PICTURE_TYPE_P:
655 if (v->pq < 5) v->tt_index = 0;
656 else if(v->pq < 13) v->tt_index = 1;
657 else v->tt_index = 2;
659 lowquant = (v->pq > 12) ? 0 : 1;
660 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
661 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
664 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
665 v->lumscale = get_bits(gb, 6);
666 v->lumshift = get_bits(gb, 6);
668 /* fill lookup tables for intensity compensation */
671 shift = (255 - v->lumshift * 2) << 6;
675 scale = v->lumscale + 32;
677 shift = (v->lumshift - 64) << 6;
679 shift = v->lumshift << 6;
681 for(i = 0; i < 256; i++) {
682 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
683 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
686 v->qs_last = v->s.quarter_sample;
687 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
688 v->s.quarter_sample = 0;
689 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
690 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
691 v->s.quarter_sample = 0;
693 v->s.quarter_sample = 1;
695 v->s.quarter_sample = 1;
696 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));
698 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
699 v->mv_mode2 == MV_PMODE_MIXED_MV)
700 || v->mv_mode == MV_PMODE_MIXED_MV)
702 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
703 if (status < 0) return -1;
704 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
705 "Imode: %i, Invert: %i\n", status>>1, status&1);
707 v->mv_type_is_raw = 0;
708 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
710 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
711 if (status < 0) return -1;
712 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
713 "Imode: %i, Invert: %i\n", status>>1, status&1);
715 /* Hopefully this is correct for P frames */
716 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
717 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
721 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
722 vop_dquant_decoding(v);
725 v->ttfrm = 0; //FIXME Is that so ?
728 v->ttmbf = get_bits1(gb);
731 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
738 case AV_PICTURE_TYPE_B:
739 if (v->pq < 5) v->tt_index = 0;
740 else if(v->pq < 13) v->tt_index = 1;
741 else v->tt_index = 2;
743 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
744 v->qs_last = v->s.quarter_sample;
745 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
746 v->s.mspel = v->s.quarter_sample;
748 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
749 if (status < 0) return -1;
750 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
751 "Imode: %i, Invert: %i\n", status>>1, status&1);
752 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
753 if (status < 0) return -1;
754 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
755 "Imode: %i, Invert: %i\n", status>>1, status&1);
757 v->s.mv_table_index = get_bits(gb, 2);
758 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
762 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
763 vop_dquant_decoding(v);
769 v->ttmbf = get_bits1(gb);
772 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
784 v->c_ac_table_index = decode012(gb);
785 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
787 v->y_ac_table_index = decode012(gb);
790 v->s.dc_table_index = get_bits1(gb);
793 if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
794 v->s.pict_type = AV_PICTURE_TYPE_B;
800 /* fill lookup tables for intensity compensation */
801 #define INIT_LUT(lumscale, lumshift, luty, lutuv) \
804 shift = (255 - lumshift * 2) << 6; \
808 scale = lumscale + 32; \
810 shift = (lumshift - 64) << 6; \
812 shift = lumshift << 6; \
814 for (i = 0; i < 256; i++) { \
815 luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6); \
816 lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6); \
819 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
821 int pqindex, lowquant;
823 int mbmodetab, imvtab, icbptab, twomvbptab, fourmvbptab; /* useful only for debugging */
824 int scale, shift, i; /* for initializing LUT for intensity compensation */
826 v->p_frame_skipped = 0;
827 if (v->second_field) {
828 v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
830 v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
831 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
832 if (!v->pic_header_flag)
833 goto parse_common_info;
837 v->fcm = decode012(gb);
843 if (!v->warn_interlaced++)
844 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is incomplete\n");
849 v->fptype = get_bits(gb, 3);
850 v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
851 if (v->fptype & 4) // B-picture
852 v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
854 switch(get_unary(gb, 0, 4)) {
856 v->s.pict_type = AV_PICTURE_TYPE_P;
859 v->s.pict_type = AV_PICTURE_TYPE_B;
862 v->s.pict_type = AV_PICTURE_TYPE_I;
865 v->s.pict_type = AV_PICTURE_TYPE_BI;
868 v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
869 v->p_frame_skipped = 1;
876 if(!v->interlace || v->psf) {
877 v->rptfrm = get_bits(gb, 2);
879 v->tff = get_bits1(gb);
880 v->rff = get_bits1(gb);
884 av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
887 if(v->p_frame_skipped) {
890 v->rnd = get_bits1(gb);
892 v->uvsamp = get_bits1(gb);
894 if (!v->refdist_flag)
897 if ((v->s.pict_type != AV_PICTURE_TYPE_B)
898 && (v->s.pict_type != AV_PICTURE_TYPE_BI)) {
899 v->refdist = get_bits(gb, 2);
901 v->refdist += get_unary(gb, 0, 16);
903 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
904 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
905 v->frfd = (v->bfraction * v->refdist) >> 8;
906 v->brfd = v->refdist - v->frfd - 1;
911 goto parse_common_info;
913 if(v->finterpflag) v->interpfrm = get_bits1(gb);
914 if(v->s.pict_type == AV_PICTURE_TYPE_B) {
915 v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
916 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
917 if(v->bfraction == 0) {
918 v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
924 v->cur_field_type = !(v->tff ^ v->second_field);
925 pqindex = get_bits(gb, 5);
926 if(!pqindex) return -1;
927 v->pqindex = pqindex;
928 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
929 v->pq = ff_vc1_pquant_table[0][pqindex];
931 v->pq = ff_vc1_pquant_table[1][pqindex];
934 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
935 v->pquantizer = pqindex < 9;
936 if (v->quantizer_mode == QUANT_NON_UNIFORM)
938 v->pqindex = pqindex;
940 v->halfpq = get_bits1(gb);
943 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
944 v->pquantizer = get_bits1(gb);
946 v->postproc = get_bits(gb, 2);
948 if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
954 switch(v->s.pict_type) {
955 case AV_PICTURE_TYPE_I:
956 case AV_PICTURE_TYPE_BI:
957 if (v->fcm == 1) { //interlace frame picture
958 status = bitplane_decoding(v->fieldtx_plane, &v->fieldtx_is_raw, v);
961 av_log(v->s.avctx, AV_LOG_DEBUG, "FIELDTX plane encoding: "
962 "Imode: %i, Invert: %i\n", status>>1, status&1);
964 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
967 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
968 "Imode: %i, Invert: %i\n", status>>1, status&1);
969 v->condover = CONDOVER_NONE;
970 if(v->overlap && v->pq <= 8) {
971 v->condover = decode012(gb);
972 if(v->condover == CONDOVER_SELECT) {
973 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
976 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
977 "Imode: %i, Invert: %i\n", status>>1, status&1);
981 case AV_PICTURE_TYPE_P:
983 v->numref = get_bits1(gb);
985 v->reffield = get_bits1(gb);
986 v->ref_field_type[0] = v->reffield ^ !v->cur_field_type;
990 v->mvrange = get_unary(gb, 0, 3);
995 v->dmvrange = get_unary(gb, 0, 3);
998 if (v->fcm == 1) { // interlaced frame picture
999 v->fourmvswitch = get_bits1(gb);
1000 v->intcomp = get_bits1(gb);
1002 v->lumscale = get_bits(gb, 6);
1003 v->lumshift = get_bits(gb, 6);
1004 INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
1006 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1007 av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
1008 "Imode: %i, Invert: %i\n", status>>1, status&1);
1009 mbmodetab = get_bits(gb, 2);
1010 if (v->fourmvswitch)
1011 v->mbmode_vlc = &ff_vc1_intfr_4mv_mbmode_vlc[mbmodetab];
1013 v->mbmode_vlc = &ff_vc1_intfr_non4mv_mbmode_vlc[mbmodetab];
1014 imvtab = get_bits(gb, 2);
1015 v->imv_vlc = &ff_vc1_1ref_mvdata_vlc[imvtab];
1016 // interlaced p-picture cbpcy range is [1, 63]
1017 icbptab = get_bits(gb, 3);
1018 v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
1019 twomvbptab = get_bits(gb, 2);
1020 v->twomvbp_vlc = &ff_vc1_2mv_block_pattern_vlc[twomvbptab];
1021 if (v->fourmvswitch) {
1022 fourmvbptab = get_bits(gb, 2);
1023 v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
1027 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1028 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1029 v->range_x = 1 << (v->k_x - 1);
1030 v->range_y = 1 << (v->k_y - 1);
1040 mvmode = get_unary(gb, 1, 4);
1041 lowquant = (v->pq > 12) ? 0 : 1;
1042 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][mvmode];
1043 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1045 mvmode2 = get_unary(gb, 1, 3);
1046 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
1048 v->intcompfield = decode210(gb);
1049 v->lumscale = get_bits(gb, 6);
1050 v->lumshift = get_bits(gb, 6);
1051 INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
1052 if ((v->field_mode) && !v->intcompfield) {
1053 v->lumscale2 = get_bits(gb, 6);
1054 v->lumshift2 = get_bits(gb, 6);
1055 INIT_LUT(v->lumscale2, v->lumshift2, v->luty2, v->lutuv2);
1059 v->qs_last = v->s.quarter_sample;
1060 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1061 v->s.quarter_sample = 0;
1062 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1063 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1064 v->s.quarter_sample = 0;
1066 v->s.quarter_sample = 1;
1068 v->s.quarter_sample = 1;
1069 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));
1071 if (v->fcm == 0) { // progressive
1072 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1073 v->mv_mode2 == MV_PMODE_MIXED_MV)
1074 || v->mv_mode == MV_PMODE_MIXED_MV)
1076 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1079 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1080 "Imode: %i, Invert: %i\n", status>>1, status&1);
1082 v->mv_type_is_raw = 0;
1083 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1085 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1088 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1089 "Imode: %i, Invert: %i\n", status>>1, status&1);
1091 /* Hopefully this is correct for P frames */
1092 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1093 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1094 } else if (v->fcm == 1) { // frame interlaced
1095 v->qs_last = v->s.quarter_sample;
1096 v->s.quarter_sample = 1;
1098 } else { // field interlaced
1099 mbmodetab = get_bits(gb, 3);
1100 imvtab = get_bits(gb, 2 + v->numref);
1102 v->imv_vlc = &ff_vc1_1ref_mvdata_vlc[imvtab];
1104 v->imv_vlc = &ff_vc1_2ref_mvdata_vlc[imvtab];
1105 icbptab = get_bits(gb, 3);
1106 v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
1107 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1108 v->mv_mode2 == MV_PMODE_MIXED_MV) || v->mv_mode == MV_PMODE_MIXED_MV) {
1109 fourmvbptab = get_bits(gb, 2);
1110 v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
1111 v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
1113 v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
1118 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1119 vop_dquant_decoding(v);
1122 v->ttfrm = 0; //FIXME Is that so ?
1125 v->ttmbf = get_bits1(gb);
1128 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1135 case AV_PICTURE_TYPE_B:
1136 // TODO: implement interlaced frame B picture decoding
1140 v->mvrange = get_unary(gb, 0, 3);
1143 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1144 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1145 v->range_x = 1 << (v->k_x - 1);
1146 v->range_y = 1 << (v->k_y - 1);
1155 if (v->field_mode) {
1157 if (v->extended_dmv)
1158 v->dmvrange = get_unary(gb, 0, 3);
1159 mvmode = get_unary(gb, 1, 3);
1160 lowquant = (v->pq > 12) ? 0 : 1;
1161 v->mv_mode = ff_vc1_mv_pmode_table2[lowquant][mvmode];
1162 v->qs_last = v->s.quarter_sample;
1163 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV || v->mv_mode == MV_PMODE_MIXED_MV);
1164 v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || v->mv_mode == MV_PMODE_1MV_HPEL);
1165 status = bitplane_decoding(v->forward_mb_plane, &v->fmb_is_raw, v);
1168 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Forward Type plane encoding: "
1169 "Imode: %i, Invert: %i\n", status>>1, status&1);
1170 mbmodetab = get_bits(gb, 3);
1171 if (v->mv_mode == MV_PMODE_MIXED_MV)
1172 v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
1174 v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
1175 imvtab = get_bits(gb, 3);
1176 v->imv_vlc = &ff_vc1_2ref_mvdata_vlc[imvtab];
1177 icbptab = get_bits(gb, 3);
1178 v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
1179 if (v->mv_mode == MV_PMODE_MIXED_MV) {
1180 fourmvbptab = get_bits(gb, 2);
1181 v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
1183 v->numref = 1; // interlaced field B pictures are always 2-ref
1185 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1186 v->qs_last = v->s.quarter_sample;
1187 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1188 v->s.mspel = v->s.quarter_sample;
1189 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1192 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1193 "Imode: %i, Invert: %i\n", status>>1, status&1);
1194 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1197 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1198 "Imode: %i, Invert: %i\n", status>>1, status&1);
1199 v->s.mv_table_index = get_bits(gb, 2);
1200 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1205 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1206 vop_dquant_decoding(v);
1212 v->ttmbf = get_bits1(gb);
1215 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1225 v->c_ac_table_index = decode012(gb);
1226 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
1228 v->y_ac_table_index = decode012(gb);
1231 v->s.dc_table_index = get_bits1(gb);
1232 if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) && v->dquant) {
1233 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1234 vop_dquant_decoding(v);
1238 if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
1239 v->s.pict_type = AV_PICTURE_TYPE_B;