avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / vc1.c
blobd263c70be7aa4a27416a5d16bda702a1ef3a9051
1 /*
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 FFmpeg.
9 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 /**
25 * @file
26 * VC-1 and WMV3 decoder common code
29 #include "avcodec.h"
30 #include "decode.h"
31 #include "mpegvideo.h"
32 #include "vc1.h"
33 #include "vc1data.h"
34 #include "wmv2data.h"
35 #include "unary.h"
37 /***********************************************************************/
38 /**
39 * @name VC-1 Bitplane decoding
40 * @see 8.7, p56
41 * @{
44 /** Decode rows by checking if they are skipped
45 * @param plane Buffer to store decoded bits
46 * @param[in] width Width of this buffer
47 * @param[in] height Height of this buffer
48 * @param[in] stride of this buffer
50 static void decode_rowskip(uint8_t* plane, int width, int height, int stride,
51 GetBitContext *gb)
53 int x, y;
55 for (y = 0; y < height; y++) {
56 if (!get_bits1(gb)) //rowskip
57 memset(plane, 0, width);
58 else
59 for (x = 0; x < width; x++)
60 plane[x] = get_bits1(gb);
61 plane += stride;
65 /** Decode columns by checking if they are skipped
66 * @param plane Buffer to store decoded bits
67 * @param[in] width Width of this buffer
68 * @param[in] height Height of this buffer
69 * @param[in] stride of this buffer
70 * @todo FIXME: Optimize
72 static void decode_colskip(uint8_t* plane, int width, int height, int stride,
73 GetBitContext *gb)
75 int x, y;
77 for (x = 0; x < width; x++) {
78 if (!get_bits1(gb)) //colskip
79 for (y = 0; y < height; y++)
80 plane[y*stride] = 0;
81 else
82 for (y = 0; y < height; y++)
83 plane[y*stride] = get_bits1(gb);
84 plane ++;
88 /** Decode a bitplane's bits
89 * @param data bitplane where to store the decode bits
90 * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
91 * @param v VC-1 context for bit reading and logging
92 * @return Status
93 * @todo FIXME: Optimize
95 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
97 GetBitContext *gb = &v->s.gb;
99 int imode, x, y, code, offset;
100 uint8_t invert, *planep = data;
101 int width, height, stride;
103 width = v->s.mb_width;
104 height = v->s.mb_height >> v->field_mode;
105 stride = v->s.mb_stride;
106 invert = get_bits1(gb);
107 imode = get_vlc2(gb, ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 1);
109 *raw_flag = 0;
110 switch (imode) {
111 case IMODE_RAW:
112 //Data is actually read in the MB layer (same for all tests == "raw")
113 *raw_flag = 1; //invert ignored
114 return invert;
115 case IMODE_DIFF2:
116 case IMODE_NORM2:
117 if ((height * width) & 1) {
118 *planep++ = get_bits1(gb);
119 y = offset = 1;
120 if (offset == width) {
121 offset = 0;
122 planep += stride - width;
125 else
126 y = offset = 0;
127 // decode bitplane as one long line
128 for (; y < height * width; y += 2) {
129 code = get_vlc2(gb, ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 1);
130 *planep++ = code & 1;
131 offset++;
132 if (offset == width) {
133 offset = 0;
134 planep += stride - width;
136 *planep++ = code >> 1;
137 offset++;
138 if (offset == width) {
139 offset = 0;
140 planep += stride - width;
143 break;
144 case IMODE_DIFF6:
145 case IMODE_NORM6:
146 if (!(height % 3) && (width % 3)) { // use 2x3 decoding
147 for (y = 0; y < height; y += 3) {
148 for (x = width & 1; x < width; x += 2) {
149 code = get_vlc2(gb, ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 2);
150 if (code < 0) {
151 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
152 return -1;
154 planep[x + 0] = (code >> 0) & 1;
155 planep[x + 1] = (code >> 1) & 1;
156 planep[x + 0 + stride] = (code >> 2) & 1;
157 planep[x + 1 + stride] = (code >> 3) & 1;
158 planep[x + 0 + stride * 2] = (code >> 4) & 1;
159 planep[x + 1 + stride * 2] = (code >> 5) & 1;
161 planep += stride * 3;
163 if (width & 1)
164 decode_colskip(data, 1, height, stride, &v->s.gb);
165 } else { // 3x2
166 planep += (height & 1) * stride;
167 for (y = height & 1; y < height; y += 2) {
168 for (x = width % 3; x < width; x += 3) {
169 code = get_vlc2(gb, ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 2);
170 if (code < 0) {
171 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
172 return -1;
174 planep[x + 0] = (code >> 0) & 1;
175 planep[x + 1] = (code >> 1) & 1;
176 planep[x + 2] = (code >> 2) & 1;
177 planep[x + 0 + stride] = (code >> 3) & 1;
178 planep[x + 1 + stride] = (code >> 4) & 1;
179 planep[x + 2 + stride] = (code >> 5) & 1;
181 planep += stride * 2;
183 x = width % 3;
184 if (x)
185 decode_colskip(data, x, height, stride, &v->s.gb);
186 if (height & 1)
187 decode_rowskip(data + x, width - x, 1, stride, &v->s.gb);
189 break;
190 case IMODE_ROWSKIP:
191 decode_rowskip(data, width, height, stride, &v->s.gb);
192 break;
193 case IMODE_COLSKIP:
194 decode_colskip(data, width, height, stride, &v->s.gb);
195 break;
196 default:
197 break;
200 /* Applying diff operator */
201 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6) {
202 planep = data;
203 planep[0] ^= invert;
204 for (x = 1; x < width; x++)
205 planep[x] ^= planep[x-1];
206 for (y = 1; y < height; y++) {
207 planep += stride;
208 planep[0] ^= planep[-stride];
209 for (x = 1; x < width; x++) {
210 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
211 else planep[x] ^= planep[x-1];
214 } else if (invert) {
215 planep = data;
216 for (x = 0; x < stride * height; x++)
217 planep[x] = !planep[x]; //FIXME stride
219 return (imode << 1) + invert;
222 /** @} */ //Bitplane group
224 /***********************************************************************/
225 /** VOP Dquant decoding
226 * @param v VC-1 Context
228 static int vop_dquant_decoding(VC1Context *v)
230 GetBitContext *gb = &v->s.gb;
231 int pqdiff;
233 //variable size
234 if (v->dquant != 2) {
235 v->dquantfrm = get_bits1(gb);
236 if (!v->dquantfrm)
237 return 0;
239 v->dqprofile = get_bits(gb, 2);
240 switch (v->dqprofile) {
241 case DQPROFILE_SINGLE_EDGE:
242 case DQPROFILE_DOUBLE_EDGES:
243 v->dqsbedge = get_bits(gb, 2);
244 break;
245 case DQPROFILE_ALL_MBS:
246 v->dqbilevel = get_bits1(gb);
247 if (!v->dqbilevel) {
248 v->halfpq = 0;
249 return 0;
251 default:
252 break; //Forbidden ?
256 pqdiff = get_bits(gb, 3);
257 if (pqdiff == 7)
258 v->altpq = get_bits(gb, 5);
259 else
260 v->altpq = v->pq + pqdiff + 1;
262 return 0;
265 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
268 * Decode Simple/Main Profiles sequence header
269 * @see Figure 7-8, p16-17
270 * @param avctx Codec context
271 * @param gb GetBit context initialized from Codec context extra_data
272 * @return Status
274 int ff_vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
276 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits_long(gb, 32));
277 v->profile = get_bits(gb, 2);
278 if (v->profile == PROFILE_COMPLEX) {
279 av_log(avctx, AV_LOG_WARNING, "WMV3 Complex Profile is not fully supported\n");
282 if (v->profile == PROFILE_ADVANCED) {
283 v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
284 v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
285 return decode_sequence_header_adv(v, gb);
286 } else {
287 v->chromaformat = 1;
288 v->zz_8x4 = ff_wmv2_scantableA;
289 v->zz_4x8 = ff_wmv2_scantableB;
290 v->res_y411 = get_bits1(gb);
291 v->res_sprite = get_bits1(gb);
292 if (v->res_y411) {
293 av_log(avctx, AV_LOG_ERROR,
294 "Old interlaced mode is not supported\n");
295 return -1;
299 // (fps-2)/4 (->30)
300 v->frmrtq_postproc = get_bits(gb, 3); //common
301 // (bitrate-32kbps)/64kbps
302 v->bitrtq_postproc = get_bits(gb, 5); //common
303 v->s.loop_filter = get_bits1(gb); //common
304 if (v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE) {
305 av_log(avctx, AV_LOG_ERROR,
306 "LOOPFILTER shall not be enabled in Simple Profile\n");
308 if (v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
309 v->s.loop_filter = 0;
311 v->res_x8 = get_bits1(gb); //reserved
312 v->multires = get_bits1(gb);
313 v->res_fasttx = get_bits1(gb);
315 v->fastuvmc = get_bits1(gb); //common
316 if (!v->profile && !v->fastuvmc) {
317 av_log(avctx, AV_LOG_ERROR,
318 "FASTUVMC unavailable in Simple Profile\n");
319 return -1;
321 v->extended_mv = get_bits1(gb); //common
322 if (!v->profile && v->extended_mv) {
323 av_log(avctx, AV_LOG_ERROR,
324 "Extended MVs unavailable in Simple Profile\n");
325 return -1;
327 v->dquant = get_bits(gb, 2); //common
328 v->vstransform = get_bits1(gb); //common
330 v->res_transtab = get_bits1(gb);
331 if (v->res_transtab) {
332 av_log(avctx, AV_LOG_ERROR,
333 "1 for reserved RES_TRANSTAB is forbidden\n");
334 return -1;
337 v->overlap = get_bits1(gb); //common
339 v->resync_marker = get_bits1(gb);
340 v->rangered = get_bits1(gb);
341 if (v->rangered && v->profile == PROFILE_SIMPLE) {
342 av_log(avctx, AV_LOG_INFO,
343 "RANGERED should be set to 0 in Simple Profile\n");
346 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
347 v->quantizer_mode = get_bits(gb, 2); //common
349 v->finterpflag = get_bits1(gb); //common
351 if (v->res_sprite) {
352 int w = get_bits(gb, 11);
353 int h = get_bits(gb, 11);
354 int ret = ff_set_dimensions(v->s.avctx, w, h);
355 if (ret < 0) {
356 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions %d %d\n", w, h);
357 return ret;
359 skip_bits(gb, 5); //frame rate
360 v->res_x8 = get_bits1(gb);
361 if (get_bits1(gb)) { // something to do with DC VLC selection
362 av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
363 return -1;
365 skip_bits(gb, 3); //slice code
366 v->res_rtm_flag = 0;
367 } else {
368 v->res_rtm_flag = get_bits1(gb); //reserved
370 //TODO: figure out what they mean (always 0x402F)
371 if (!v->res_fasttx)
372 skip_bits(gb, 16);
373 av_log(avctx, AV_LOG_DEBUG,
374 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
375 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
376 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
377 "DQuant=%i, Quantizer mode=%i, Max B-frames=%i\n",
378 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
379 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
380 v->rangered, v->vstransform, v->overlap, v->resync_marker,
381 v->dquant, v->quantizer_mode, avctx->max_b_frames);
382 return 0;
385 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
387 v->res_rtm_flag = 1;
388 v->level = get_bits(gb, 3);
389 if (v->level >= 5) {
390 av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
392 v->chromaformat = get_bits(gb, 2);
393 if (v->chromaformat != 1) {
394 av_log(v->s.avctx, AV_LOG_ERROR,
395 "Only 4:2:0 chroma format supported\n");
396 return -1;
399 // (fps-2)/4 (->30)
400 v->frmrtq_postproc = get_bits(gb, 3); //common
401 // (bitrate-32kbps)/64kbps
402 v->bitrtq_postproc = get_bits(gb, 5); //common
403 v->postprocflag = get_bits1(gb); //common
405 v->max_coded_width = (get_bits(gb, 12) + 1) << 1;
406 v->max_coded_height = (get_bits(gb, 12) + 1) << 1;
407 v->broadcast = get_bits1(gb);
408 v->interlace = get_bits1(gb);
409 v->tfcntrflag = get_bits1(gb);
410 v->finterpflag = get_bits1(gb);
411 skip_bits1(gb); // reserved
413 av_log(v->s.avctx, AV_LOG_DEBUG,
414 "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
415 "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
416 "TFCTRflag=%i, FINTERPflag=%i\n",
417 v->level, v->frmrtq_postproc, v->bitrtq_postproc,
418 v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
419 v->tfcntrflag, v->finterpflag);
421 #if FF_API_TICKS_PER_FRAME
422 FF_DISABLE_DEPRECATION_WARNINGS
423 if (v->broadcast) { // Pulldown may be present
424 v->s.avctx->ticks_per_frame = 2;
426 FF_ENABLE_DEPRECATION_WARNINGS
427 #endif
429 v->psf = get_bits1(gb);
430 if (v->psf) { //PsF, 6.1.13
431 av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
432 return -1;
434 v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
435 if (get_bits1(gb)) { //Display Info - decoding is not affected by it
436 int w, h, ar = 0;
437 av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
438 w = get_bits(gb, 14) + 1;
439 h = get_bits(gb, 14) + 1;
440 av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
441 if (get_bits1(gb))
442 ar = get_bits(gb, 4);
443 if (ar && ar < 14) {
444 v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
445 } else if (ar == 15) {
446 w = get_bits(gb, 8) + 1;
447 h = get_bits(gb, 8) + 1;
448 v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
449 } else {
450 if (v->s.avctx->width > v->max_coded_width ||
451 v->s.avctx->height > v->max_coded_height) {
452 avpriv_request_sample(v->s.avctx, "Huge resolution");
453 } else
454 av_reduce(&v->s.avctx->sample_aspect_ratio.num,
455 &v->s.avctx->sample_aspect_ratio.den,
456 v->s.avctx->height * w,
457 v->s.avctx->width * h,
458 1 << 30);
460 ff_set_sar(v->s.avctx, v->s.avctx->sample_aspect_ratio);
461 av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",
462 v->s.avctx->sample_aspect_ratio.num,
463 v->s.avctx->sample_aspect_ratio.den);
465 if (get_bits1(gb)) { //framerate stuff
466 if (get_bits1(gb)) {
467 v->s.avctx->framerate.den = 32;
468 v->s.avctx->framerate.num = get_bits(gb, 16) + 1;
469 } else {
470 int nr, dr;
471 nr = get_bits(gb, 8);
472 dr = get_bits(gb, 4);
473 if (nr > 0 && nr < 8 && dr > 0 && dr < 3) {
474 v->s.avctx->framerate.den = ff_vc1_fps_dr[dr - 1];
475 v->s.avctx->framerate.num = ff_vc1_fps_nr[nr - 1] * 1000;
480 if (get_bits1(gb)) {
481 v->color_prim = get_bits(gb, 8);
482 v->transfer_char = get_bits(gb, 8);
483 v->matrix_coef = get_bits(gb, 8);
487 v->hrd_param_flag = get_bits1(gb);
488 if (v->hrd_param_flag) {
489 int i;
490 v->hrd_num_leaky_buckets = get_bits(gb, 5);
491 skip_bits(gb, 4); //bitrate exponent
492 skip_bits(gb, 4); //buffer size exponent
493 for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
494 skip_bits(gb, 16); //hrd_rate[n]
495 skip_bits(gb, 16); //hrd_buffer[n]
498 return 0;
501 int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
503 int i;
504 int w,h;
505 int ret;
507 av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
508 v->broken_link = get_bits1(gb);
509 v->closed_entry = get_bits1(gb);
510 v->panscanflag = get_bits1(gb);
511 v->refdist_flag = get_bits1(gb);
512 v->s.loop_filter = get_bits1(gb);
513 if (v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
514 v->s.loop_filter = 0;
515 v->fastuvmc = get_bits1(gb);
516 v->extended_mv = get_bits1(gb);
517 v->dquant = get_bits(gb, 2);
518 v->vstransform = get_bits1(gb);
519 v->overlap = get_bits1(gb);
520 v->quantizer_mode = get_bits(gb, 2);
522 if (v->hrd_param_flag) {
523 for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
524 skip_bits(gb, 8); //hrd_full[n]
528 if(get_bits1(gb)){
529 w = (get_bits(gb, 12)+1)<<1;
530 h = (get_bits(gb, 12)+1)<<1;
531 } else {
532 w = v->max_coded_width;
533 h = v->max_coded_height;
535 if ((ret = ff_set_dimensions(avctx, w, h)) < 0) {
536 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions %d %d\n", w, h);
537 return ret;
540 if (v->extended_mv)
541 v->extended_dmv = get_bits1(gb);
542 if ((v->range_mapy_flag = get_bits1(gb))) {
543 av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
544 v->range_mapy = get_bits(gb, 3);
546 if ((v->range_mapuv_flag = get_bits1(gb))) {
547 av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
548 v->range_mapuv = get_bits(gb, 3);
551 av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
552 "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
553 "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
554 "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
555 v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
556 v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
558 return 0;
561 /* fill lookup tables for intensity compensation */
562 #define INIT_LUT(lumscale, lumshift, luty, lutuv, chain) do { \
563 int scale, shift, i; \
564 if (!lumscale) { \
565 scale = -64; \
566 shift = (255 - lumshift * 2) * 64; \
567 if (lumshift > 31) \
568 shift += 128 << 6; \
569 } else { \
570 scale = lumscale + 32; \
571 if (lumshift > 31) \
572 shift = (lumshift - 64) * 64; \
573 else \
574 shift = lumshift << 6; \
576 for (i = 0; i < 256; i++) { \
577 int iy = chain ? luty[i] : i; \
578 int iu = chain ? lutuv[i] : i; \
579 luty[i] = av_clip_uint8((scale * iy + shift + 32) >> 6); \
580 lutuv[i] = av_clip_uint8((scale * (iu - 128) + 128*64 + 32) >> 6);\
582 } while(0)
584 static void rotate_luts(VC1Context *v)
586 if (v->s.pict_type == AV_PICTURE_TYPE_BI || v->s.pict_type == AV_PICTURE_TYPE_B) {
587 v->curr_use_ic = &v->aux_use_ic;
588 v->curr_luty = v->aux_luty;
589 v->curr_lutuv = v->aux_lutuv;
590 } else {
591 #define ROTATE(DEF, L, N, C) do { \
592 DEF; \
593 memcpy(&tmp, L , sizeof(tmp)); \
594 memcpy(L , N , sizeof(tmp)); \
595 memcpy(N , &tmp, sizeof(tmp)); \
596 C = N; \
597 } while(0)
599 ROTATE(int tmp, &v->last_use_ic, &v->next_use_ic, v->curr_use_ic);
600 ROTATE(uint8_t tmp[2][256], v->last_luty, v->next_luty, v->curr_luty);
601 ROTATE(uint8_t tmp[2][256], v->last_lutuv, v->next_lutuv, v->curr_lutuv);
604 INIT_LUT(32, 0, v->curr_luty[0], v->curr_lutuv[0], 0);
605 INIT_LUT(32, 0, v->curr_luty[1], v->curr_lutuv[1], 0);
606 *v->curr_use_ic = 0;
609 static int read_bfraction(VC1Context *v, GetBitContext* gb) {
610 int bfraction_lut_index = get_bits(gb, 3);
612 if (bfraction_lut_index == 7)
613 bfraction_lut_index = 7 + get_bits(gb, 4);
615 if (bfraction_lut_index == 21) {
616 av_log(v->s.avctx, AV_LOG_ERROR, "bfraction invalid\n");
617 return AVERROR_INVALIDDATA;
619 v->bfraction_lut_index = bfraction_lut_index;
620 v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
621 return 0;
624 int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
626 int pqindex, lowquant, status;
628 v->field_mode = 0;
629 v->fcm = PROGRESSIVE;
630 if (v->finterpflag)
631 v->interpfrm = get_bits1(gb);
632 if (v->s.avctx->codec_id == AV_CODEC_ID_MSS2)
633 v->respic =
634 v->rangered =
635 v->multires = get_bits(gb, 2) == 1;
636 else
637 skip_bits(gb, 2); //framecnt unused
638 v->rangeredfrm = 0;
639 if (v->rangered)
640 v->rangeredfrm = get_bits1(gb);
641 if (get_bits1(gb)) {
642 v->s.pict_type = AV_PICTURE_TYPE_P;
643 } else {
644 if (v->s.avctx->max_b_frames && !get_bits1(gb)) {
645 v->s.pict_type = AV_PICTURE_TYPE_B;
646 } else
647 v->s.pict_type = AV_PICTURE_TYPE_I;
650 v->bi_type = 0;
651 if (v->s.pict_type == AV_PICTURE_TYPE_B) {
652 if (read_bfraction(v, gb) < 0)
653 return AVERROR_INVALIDDATA;
654 if (v->bfraction == 0) {
655 v->s.pict_type = AV_PICTURE_TYPE_BI;
658 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
659 skip_bits(gb, 7); // skip buffer fullness
661 if (v->parse_only)
662 return 0;
664 /* calculate RND */
665 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
666 v->rnd = 1;
667 if (v->s.pict_type == AV_PICTURE_TYPE_P)
668 v->rnd ^= 1;
670 if (get_bits_left(gb) < 5)
671 return AVERROR_INVALIDDATA;
672 /* Quantizer stuff */
673 pqindex = get_bits(gb, 5);
674 if (!pqindex)
675 return -1;
676 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
677 v->pq = ff_vc1_pquant_table[0][pqindex];
678 else
679 v->pq = ff_vc1_pquant_table[1][pqindex];
680 v->pqindex = pqindex;
681 if (pqindex < 9)
682 v->halfpq = get_bits1(gb);
683 else
684 v->halfpq = 0;
685 switch (v->quantizer_mode) {
686 case QUANT_FRAME_IMPLICIT:
687 v->pquantizer = pqindex < 9;
688 break;
689 case QUANT_NON_UNIFORM:
690 v->pquantizer = 0;
691 break;
692 case QUANT_FRAME_EXPLICIT:
693 v->pquantizer = get_bits1(gb);
694 break;
695 default:
696 v->pquantizer = 1;
697 break;
699 v->dquantfrm = 0;
700 if (v->extended_mv == 1)
701 v->mvrange = get_unary(gb, 0, 3);
702 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
703 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
704 v->range_x = 1 << (v->k_x - 1);
705 v->range_y = 1 << (v->k_y - 1);
706 if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B)
707 v->respic = get_bits(gb, 2);
709 if (v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)) {
710 v->x8_type = get_bits1(gb);
711 } else
712 v->x8_type = 0;
713 ff_dlog(v->s.avctx, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
714 (v->s.pict_type == AV_PICTURE_TYPE_P) ? 'P' : ((v->s.pict_type == AV_PICTURE_TYPE_I) ? 'I' : 'B'),
715 pqindex, v->pq, v->halfpq, v->rangeredfrm);
717 if (v->first_pic_header_flag)
718 rotate_luts(v);
720 switch (v->s.pict_type) {
721 case AV_PICTURE_TYPE_P:
722 v->tt_index = (v->pq > 4) + (v->pq > 12);
724 lowquant = (v->pq > 12) ? 0 : 1;
725 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
726 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
727 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
728 v->lumscale = get_bits(gb, 6);
729 v->lumshift = get_bits(gb, 6);
730 v->last_use_ic = 1;
731 /* fill lookup tables for intensity compensation */
732 INIT_LUT(v->lumscale, v->lumshift, v->last_luty[0], v->last_lutuv[0], 1);
733 INIT_LUT(v->lumscale, v->lumshift, v->last_luty[1], v->last_lutuv[1], 1);
735 v->qs_last = v->s.quarter_sample;
736 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
737 v->s.quarter_sample = (v->mv_mode2 != MV_PMODE_1MV_HPEL &&
738 v->mv_mode2 != MV_PMODE_1MV_HPEL_BILIN);
739 v->s.mspel = (v->mv_mode2 != MV_PMODE_1MV_HPEL_BILIN);
740 } else {
741 v->s.quarter_sample = (v->mv_mode != MV_PMODE_1MV_HPEL &&
742 v->mv_mode != MV_PMODE_1MV_HPEL_BILIN);
743 v->s.mspel = (v->mv_mode != MV_PMODE_1MV_HPEL_BILIN);
746 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
747 v->mv_mode2 == MV_PMODE_MIXED_MV) ||
748 v->mv_mode == MV_PMODE_MIXED_MV) {
749 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
750 if (status < 0)
751 return -1;
752 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
753 "Imode: %i, Invert: %i\n", status>>1, status&1);
754 } else {
755 v->mv_type_is_raw = 0;
756 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
758 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
759 if (status < 0)
760 return -1;
761 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
762 "Imode: %i, Invert: %i\n", status>>1, status&1);
764 if (get_bits_left(gb) < 4)
765 return AVERROR_INVALIDDATA;
767 /* Hopefully this is correct for P-frames */
768 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
769 v->cbptab = get_bits(gb, 2);
770 v->cbpcy_vlc = ff_vc1_cbpcy_p_vlc[v->cbptab];
772 if (v->dquant) {
773 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
774 vop_dquant_decoding(v);
777 if (v->vstransform) {
778 v->ttmbf = get_bits1(gb);
779 if (v->ttmbf) {
780 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
781 } else
782 v->ttfrm = 0; //FIXME Is that so ?
783 } else {
784 v->ttmbf = 1;
785 v->ttfrm = TT_8X8;
787 break;
788 case AV_PICTURE_TYPE_B:
789 v->tt_index = (v->pq > 4) + (v->pq > 12);
791 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
792 v->qs_last = v->s.quarter_sample;
793 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
794 v->s.mspel = v->s.quarter_sample;
796 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
797 if (status < 0)
798 return -1;
799 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
800 "Imode: %i, Invert: %i\n", status>>1, status&1);
801 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
802 if (status < 0)
803 return -1;
804 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
805 "Imode: %i, Invert: %i\n", status>>1, status&1);
807 v->s.mv_table_index = get_bits(gb, 2);
808 v->cbptab = get_bits(gb, 2);
809 v->cbpcy_vlc = ff_vc1_cbpcy_p_vlc[v->cbptab];
811 if (v->dquant) {
812 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
813 vop_dquant_decoding(v);
816 if (v->vstransform) {
817 v->ttmbf = get_bits1(gb);
818 if (v->ttmbf) {
819 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
820 } else
821 v->ttfrm = 0;
822 } else {
823 v->ttmbf = 1;
824 v->ttfrm = TT_8X8;
826 break;
829 if (!v->x8_type) {
830 /* AC Syntax */
831 v->c_ac_table_index = decode012(gb);
832 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
833 v->y_ac_table_index = decode012(gb);
835 /* DC Syntax */
836 v->s.dc_table_index = get_bits1(gb);
839 if (v->s.pict_type == AV_PICTURE_TYPE_BI) {
840 v->s.pict_type = AV_PICTURE_TYPE_B;
841 v->bi_type = 1;
843 return 0;
846 int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
848 int pqindex, lowquant;
849 int status;
850 int field_mode, fcm;
852 v->numref = 0;
853 v->p_frame_skipped = 0;
854 if (v->second_field) {
855 if (v->fcm != ILACE_FIELD || v->field_mode!=1)
856 return -1;
857 if (v->fptype & 4)
858 v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
859 else
860 v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
861 v->s.cur_pic.ptr->f->pict_type = v->s.pict_type;
862 if (!v->pic_header_flag)
863 goto parse_common_info;
866 field_mode = 0;
867 if (v->interlace) {
868 fcm = decode012(gb);
869 if (fcm) {
870 if (fcm == ILACE_FIELD)
871 field_mode = 1;
873 } else {
874 fcm = PROGRESSIVE;
876 if (!v->first_pic_header_flag && v->field_mode != field_mode)
877 return AVERROR_INVALIDDATA;
878 v->field_mode = field_mode;
879 v->fcm = fcm;
881 av_assert0( v->s.mb_height == v->s.height + 15 >> 4
882 || v->s.mb_height == FFALIGN(v->s.height + 15 >> 4, 2));
883 if (v->field_mode) {
884 v->s.mb_height = FFALIGN(v->s.height + 15 >> 4, 2);
885 v->fptype = get_bits(gb, 3);
886 if (v->fptype & 4) // B-picture
887 v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
888 else
889 v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
890 } else {
891 v->s.mb_height = v->s.height + 15 >> 4;
892 switch (get_unary(gb, 0, 4)) {
893 case 0:
894 v->s.pict_type = AV_PICTURE_TYPE_P;
895 break;
896 case 1:
897 v->s.pict_type = AV_PICTURE_TYPE_B;
898 break;
899 case 2:
900 v->s.pict_type = AV_PICTURE_TYPE_I;
901 break;
902 case 3:
903 v->s.pict_type = AV_PICTURE_TYPE_BI;
904 break;
905 case 4:
906 v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
907 v->p_frame_skipped = 1;
908 break;
911 if (v->tfcntrflag)
912 skip_bits(gb, 8);
913 if (v->broadcast) {
914 if (!v->interlace || v->psf) {
915 v->rptfrm = get_bits(gb, 2);
916 } else {
917 v->tff = get_bits1(gb);
918 v->rff = get_bits1(gb);
920 } else {
921 v->tff = 1;
923 if (v->panscanflag) {
924 avpriv_report_missing_feature(v->s.avctx, "Pan-scan");
925 //...
927 if (v->p_frame_skipped) {
928 return 0;
930 v->rnd = get_bits1(gb);
931 if (v->interlace)
932 v->uvsamp = get_bits1(gb);
933 if (v->field_mode) {
934 if (!v->refdist_flag)
935 v->refdist = 0;
936 else if ((v->s.pict_type != AV_PICTURE_TYPE_B) && (v->s.pict_type != AV_PICTURE_TYPE_BI)) {
937 v->refdist = get_bits(gb, 2);
938 if (v->refdist == 3)
939 v->refdist += get_unary(gb, 0, 14);
940 if (v->refdist > 16)
941 return AVERROR_INVALIDDATA;
943 if ((v->s.pict_type == AV_PICTURE_TYPE_B) || (v->s.pict_type == AV_PICTURE_TYPE_BI)) {
944 if (read_bfraction(v, gb) < 0)
945 return AVERROR_INVALIDDATA;
946 v->frfd = (v->bfraction * v->refdist) >> 8;
947 v->brfd = v->refdist - v->frfd - 1;
948 if (v->brfd < 0)
949 v->brfd = 0;
951 goto parse_common_info;
953 if (v->fcm == PROGRESSIVE) {
954 if (v->finterpflag)
955 v->interpfrm = get_bits1(gb);
956 if (v->s.pict_type == AV_PICTURE_TYPE_B) {
957 if (read_bfraction(v, gb) < 0)
958 return AVERROR_INVALIDDATA;
959 if (v->bfraction == 0) {
960 v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
965 parse_common_info:
966 if (v->field_mode)
967 v->cur_field_type = !(v->tff ^ v->second_field);
968 pqindex = get_bits(gb, 5);
969 if (!pqindex)
970 return -1;
971 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
972 v->pq = ff_vc1_pquant_table[0][pqindex];
973 else
974 v->pq = ff_vc1_pquant_table[1][pqindex];
975 v->pqindex = pqindex;
976 if (pqindex < 9)
977 v->halfpq = get_bits1(gb);
978 else
979 v->halfpq = 0;
980 switch (v->quantizer_mode) {
981 case QUANT_FRAME_IMPLICIT:
982 v->pquantizer = pqindex < 9;
983 break;
984 case QUANT_NON_UNIFORM:
985 v->pquantizer = 0;
986 break;
987 case QUANT_FRAME_EXPLICIT:
988 v->pquantizer = get_bits1(gb);
989 break;
990 default:
991 v->pquantizer = 1;
992 break;
994 v->dquantfrm = 0;
995 if (v->postprocflag)
996 v->postproc = get_bits(gb, 2);
998 if (v->parse_only)
999 return 0;
1001 if (v->first_pic_header_flag)
1002 rotate_luts(v);
1004 switch (v->s.pict_type) {
1005 case AV_PICTURE_TYPE_I:
1006 case AV_PICTURE_TYPE_BI:
1007 if (v->fcm == ILACE_FRAME) { //interlace frame picture
1008 status = bitplane_decoding(v->fieldtx_plane, &v->fieldtx_is_raw, v);
1009 if (status < 0)
1010 return -1;
1011 av_log(v->s.avctx, AV_LOG_DEBUG, "FIELDTX plane encoding: "
1012 "Imode: %i, Invert: %i\n", status>>1, status&1);
1013 } else
1014 v->fieldtx_is_raw = 0;
1015 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1016 if (status < 0)
1017 return -1;
1018 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1019 "Imode: %i, Invert: %i\n", status>>1, status&1);
1020 v->condover = CONDOVER_NONE;
1021 if (v->overlap && v->pq <= 8) {
1022 v->condover = decode012(gb);
1023 if (v->condover == CONDOVER_SELECT) {
1024 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1025 if (status < 0)
1026 return -1;
1027 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1028 "Imode: %i, Invert: %i\n", status>>1, status&1);
1031 break;
1032 case AV_PICTURE_TYPE_P:
1033 if (v->field_mode) {
1034 v->numref = get_bits1(gb);
1035 if (!v->numref) {
1036 v->reffield = get_bits1(gb);
1037 v->ref_field_type[0] = v->reffield ^ !v->cur_field_type;
1040 if (v->extended_mv)
1041 v->mvrange = get_unary(gb, 0, 3);
1042 else
1043 v->mvrange = 0;
1044 if (v->interlace) {
1045 if (v->extended_dmv)
1046 v->dmvrange = get_unary(gb, 0, 3);
1047 else
1048 v->dmvrange = 0;
1049 if (v->fcm == ILACE_FRAME) { // interlaced frame picture
1050 v->fourmvswitch = get_bits1(gb);
1051 v->intcomp = get_bits1(gb);
1052 if (v->intcomp) {
1053 v->lumscale = get_bits(gb, 6);
1054 v->lumshift = get_bits(gb, 6);
1055 INIT_LUT(v->lumscale, v->lumshift, v->last_luty[0], v->last_lutuv[0], 1);
1056 INIT_LUT(v->lumscale, v->lumshift, v->last_luty[1], v->last_lutuv[1], 1);
1057 v->last_use_ic = 1;
1059 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1060 if (status < 0)
1061 return -1;
1062 av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
1063 "Imode: %i, Invert: %i\n", status>>1, status&1);
1064 v->mbmodetab = get_bits(gb, 2);
1065 if (v->fourmvswitch)
1066 v->mbmode_vlc = ff_vc1_intfr_4mv_mbmode_vlc[v->mbmodetab];
1067 else
1068 v->mbmode_vlc = ff_vc1_intfr_non4mv_mbmode_vlc[v->mbmodetab];
1069 v->imvtab = get_bits(gb, 2);
1070 v->imv_vlc = ff_vc1_1ref_mvdata_vlc[v->imvtab];
1071 // interlaced p-picture cbpcy range is [1, 63]
1072 v->icbptab = get_bits(gb, 3);
1073 v->cbpcy_vlc = ff_vc1_icbpcy_vlc[v->icbptab];
1074 v->twomvbptab = get_bits(gb, 2);
1075 v->twomvbp_vlc = ff_vc1_2mv_block_pattern_vlc[v->twomvbptab];
1076 if (v->fourmvswitch) {
1077 v->fourmvbptab = get_bits(gb, 2);
1078 v->fourmvbp_vlc = ff_vc1_4mv_block_pattern_vlc[v->fourmvbptab];
1082 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1083 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1084 v->range_x = 1 << (v->k_x - 1);
1085 v->range_y = 1 << (v->k_y - 1);
1087 v->tt_index = (v->pq > 4) + (v->pq > 12);
1088 if (v->fcm != ILACE_FRAME) {
1089 int mvmode;
1090 mvmode = get_unary(gb, 1, 4);
1091 lowquant = (v->pq > 12) ? 0 : 1;
1092 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][mvmode];
1093 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1094 int mvmode2;
1095 mvmode2 = get_unary(gb, 1, 3);
1096 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
1097 if (v->field_mode) {
1098 v->intcompfield = decode210(gb) ^ 3;
1099 } else
1100 v->intcompfield = 3;
1102 v->lumscale2 = v->lumscale = 32;
1103 v->lumshift2 = v->lumshift = 0;
1104 if (v->intcompfield & 1) {
1105 v->lumscale = get_bits(gb, 6);
1106 v->lumshift = get_bits(gb, 6);
1108 if ((v->intcompfield & 2) && v->field_mode) {
1109 v->lumscale2 = get_bits(gb, 6);
1110 v->lumshift2 = get_bits(gb, 6);
1111 } else if(!v->field_mode) {
1112 v->lumscale2 = v->lumscale;
1113 v->lumshift2 = v->lumshift;
1115 if (v->field_mode && v->second_field) {
1116 if (v->cur_field_type) {
1117 INIT_LUT(v->lumscale , v->lumshift , v->curr_luty[v->cur_field_type^1], v->curr_lutuv[v->cur_field_type^1], 0);
1118 INIT_LUT(v->lumscale2, v->lumshift2, v->last_luty[v->cur_field_type ], v->last_lutuv[v->cur_field_type ], 1);
1119 } else {
1120 INIT_LUT(v->lumscale2, v->lumshift2, v->curr_luty[v->cur_field_type^1], v->curr_lutuv[v->cur_field_type^1], 0);
1121 INIT_LUT(v->lumscale , v->lumshift , v->last_luty[v->cur_field_type ], v->last_lutuv[v->cur_field_type ], 1);
1123 v->next_use_ic = *v->curr_use_ic = 1;
1124 } else {
1125 INIT_LUT(v->lumscale , v->lumshift , v->last_luty[0], v->last_lutuv[0], 1);
1126 INIT_LUT(v->lumscale2, v->lumshift2, v->last_luty[1], v->last_lutuv[1], 1);
1128 v->last_use_ic = 1;
1130 v->qs_last = v->s.quarter_sample;
1131 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1132 v->s.quarter_sample = (v->mv_mode2 != MV_PMODE_1MV_HPEL &&
1133 v->mv_mode2 != MV_PMODE_1MV_HPEL_BILIN);
1134 v->s.mspel = (v->mv_mode2 != MV_PMODE_1MV_HPEL_BILIN);
1135 } else {
1136 v->s.quarter_sample = (v->mv_mode != MV_PMODE_1MV_HPEL &&
1137 v->mv_mode != MV_PMODE_1MV_HPEL_BILIN);
1138 v->s.mspel = (v->mv_mode != MV_PMODE_1MV_HPEL_BILIN);
1141 if (v->fcm == PROGRESSIVE) { // progressive
1142 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1143 v->mv_mode2 == MV_PMODE_MIXED_MV)
1144 || v->mv_mode == MV_PMODE_MIXED_MV) {
1145 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1146 if (status < 0)
1147 return -1;
1148 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1149 "Imode: %i, Invert: %i\n", status>>1, status&1);
1150 } else {
1151 v->mv_type_is_raw = 0;
1152 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1154 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1155 if (status < 0)
1156 return -1;
1157 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1158 "Imode: %i, Invert: %i\n", status>>1, status&1);
1160 /* Hopefully this is correct for P-frames */
1161 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1162 v->cbptab = get_bits(gb, 2);
1163 v->cbpcy_vlc = ff_vc1_cbpcy_p_vlc[v->cbptab];
1164 } else if (v->fcm == ILACE_FRAME) { // frame interlaced
1165 v->qs_last = v->s.quarter_sample;
1166 v->s.quarter_sample = 1;
1167 v->s.mspel = 1;
1168 } else { // field interlaced
1169 v->mbmodetab = get_bits(gb, 3);
1170 v->imvtab = get_bits(gb, 2 + v->numref);
1171 if (!v->numref)
1172 v->imv_vlc = ff_vc1_1ref_mvdata_vlc[v->imvtab];
1173 else
1174 v->imv_vlc = ff_vc1_2ref_mvdata_vlc[v->imvtab];
1175 v->icbptab = get_bits(gb, 3);
1176 v->cbpcy_vlc = ff_vc1_icbpcy_vlc[v->icbptab];
1177 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1178 v->mv_mode2 == MV_PMODE_MIXED_MV) || v->mv_mode == MV_PMODE_MIXED_MV) {
1179 v->fourmvbptab = get_bits(gb, 2);
1180 v->fourmvbp_vlc = ff_vc1_4mv_block_pattern_vlc[v->fourmvbptab];
1181 v->mbmode_vlc = ff_vc1_if_mmv_mbmode_vlc[v->mbmodetab];
1182 } else {
1183 v->mbmode_vlc = ff_vc1_if_1mv_mbmode_vlc[v->mbmodetab];
1186 if (v->dquant) {
1187 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1188 vop_dquant_decoding(v);
1191 if (v->vstransform) {
1192 v->ttmbf = get_bits1(gb);
1193 if (v->ttmbf) {
1194 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1195 } else
1196 v->ttfrm = 0; //FIXME Is that so ?
1197 } else {
1198 v->ttmbf = 1;
1199 v->ttfrm = TT_8X8;
1201 break;
1202 case AV_PICTURE_TYPE_B:
1203 if (v->fcm == ILACE_FRAME) {
1204 if (read_bfraction(v, gb) < 0)
1205 return AVERROR_INVALIDDATA;
1206 if (v->bfraction == 0) {
1207 return -1;
1210 if (v->extended_mv)
1211 v->mvrange = get_unary(gb, 0, 3);
1212 else
1213 v->mvrange = 0;
1214 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1215 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1216 v->range_x = 1 << (v->k_x - 1);
1217 v->range_y = 1 << (v->k_y - 1);
1219 v->tt_index = (v->pq > 4) + (v->pq > 12);
1221 if (v->field_mode) {
1222 int mvmode;
1223 av_log(v->s.avctx, AV_LOG_DEBUG, "B Fields\n");
1224 if (v->extended_dmv)
1225 v->dmvrange = get_unary(gb, 0, 3);
1226 mvmode = get_unary(gb, 1, 3);
1227 lowquant = (v->pq > 12) ? 0 : 1;
1228 v->mv_mode = ff_vc1_mv_pmode_table2[lowquant][mvmode];
1229 v->qs_last = v->s.quarter_sample;
1230 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV || v->mv_mode == MV_PMODE_MIXED_MV);
1231 v->s.mspel = (v->mv_mode != MV_PMODE_1MV_HPEL_BILIN);
1232 status = bitplane_decoding(v->forward_mb_plane, &v->fmb_is_raw, v);
1233 if (status < 0)
1234 return -1;
1235 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Forward Type plane encoding: "
1236 "Imode: %i, Invert: %i\n", status>>1, status&1);
1237 v->mbmodetab = get_bits(gb, 3);
1238 if (v->mv_mode == MV_PMODE_MIXED_MV)
1239 v->mbmode_vlc = ff_vc1_if_mmv_mbmode_vlc[v->mbmodetab];
1240 else
1241 v->mbmode_vlc = ff_vc1_if_1mv_mbmode_vlc[v->mbmodetab];
1242 v->imvtab = get_bits(gb, 3);
1243 v->imv_vlc = ff_vc1_2ref_mvdata_vlc[v->imvtab];
1244 v->icbptab = get_bits(gb, 3);
1245 v->cbpcy_vlc = ff_vc1_icbpcy_vlc[v->icbptab];
1246 if (v->mv_mode == MV_PMODE_MIXED_MV) {
1247 v->fourmvbptab = get_bits(gb, 2);
1248 v->fourmvbp_vlc = ff_vc1_4mv_block_pattern_vlc[v->fourmvbptab];
1250 v->numref = 1; // interlaced field B pictures are always 2-ref
1251 } else if (v->fcm == ILACE_FRAME) {
1252 if (v->extended_dmv)
1253 v->dmvrange = get_unary(gb, 0, 3);
1254 if (get_bits1(gb)) /* intcomp - present but shall always be 0 */
1255 av_log(v->s.avctx, AV_LOG_WARNING, "Intensity compensation set for B picture\n");
1256 v->intcomp = 0;
1257 v->mv_mode = MV_PMODE_1MV;
1258 v->fourmvswitch = 0;
1259 v->qs_last = v->s.quarter_sample;
1260 v->s.quarter_sample = 1;
1261 v->s.mspel = 1;
1262 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1263 if (status < 0)
1264 return -1;
1265 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1266 "Imode: %i, Invert: %i\n", status>>1, status&1);
1267 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1268 if (status < 0)
1269 return -1;
1270 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1271 "Imode: %i, Invert: %i\n", status>>1, status&1);
1272 v->mbmodetab = get_bits(gb, 2);
1273 v->mbmode_vlc = ff_vc1_intfr_non4mv_mbmode_vlc[v->mbmodetab];
1274 v->imvtab = get_bits(gb, 2);
1275 v->imv_vlc = ff_vc1_1ref_mvdata_vlc[v->imvtab];
1276 // interlaced p/b-picture cbpcy range is [1, 63]
1277 v->icbptab = get_bits(gb, 3);
1278 v->cbpcy_vlc = ff_vc1_icbpcy_vlc[v->icbptab];
1279 v->twomvbptab = get_bits(gb, 2);
1280 v->twomvbp_vlc = ff_vc1_2mv_block_pattern_vlc[v->twomvbptab];
1281 v->fourmvbptab = get_bits(gb, 2);
1282 v->fourmvbp_vlc = ff_vc1_4mv_block_pattern_vlc[v->fourmvbptab];
1283 } else {
1284 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1285 v->qs_last = v->s.quarter_sample;
1286 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1287 v->s.mspel = v->s.quarter_sample;
1288 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1289 if (status < 0)
1290 return -1;
1291 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1292 "Imode: %i, Invert: %i\n", status>>1, status&1);
1293 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1294 if (status < 0)
1295 return -1;
1296 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1297 "Imode: %i, Invert: %i\n", status>>1, status&1);
1298 v->s.mv_table_index = get_bits(gb, 2);
1299 v->cbptab = get_bits(gb, 2);
1300 v->cbpcy_vlc = ff_vc1_cbpcy_p_vlc[v->cbptab];
1303 if (v->dquant) {
1304 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1305 vop_dquant_decoding(v);
1308 if (v->vstransform) {
1309 v->ttmbf = get_bits1(gb);
1310 if (v->ttmbf) {
1311 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1312 } else
1313 v->ttfrm = 0;
1314 } else {
1315 v->ttmbf = 1;
1316 v->ttfrm = TT_8X8;
1318 break;
1322 /* AC Syntax */
1323 v->c_ac_table_index = decode012(gb);
1324 if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
1325 v->y_ac_table_index = decode012(gb);
1327 else if (v->fcm != PROGRESSIVE && !v->s.quarter_sample) {
1328 v->range_x <<= 1;
1329 v->range_y <<= 1;
1332 /* DC Syntax */
1333 v->s.dc_table_index = get_bits1(gb);
1334 if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
1335 && v->dquant) {
1336 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1337 vop_dquant_decoding(v);
1340 v->bi_type = (v->s.pict_type == AV_PICTURE_TYPE_BI);
1341 if (v->bi_type)
1342 v->s.pict_type = AV_PICTURE_TYPE_B;
1344 return 0;