]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/vc1.c
Pad ff_cos_tabs and ff_sin_tabs so that index n points to the table for n bits.
[frescor/ffmpeg.git] / libavcodec / vc1.c
1 /*
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
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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.
12  *
13  * FFmpeg 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file libavcodec/vc1.c
25  * VC-1 and WMV3 decoder common code
26  *
27  */
28 #include "internal.h"
29 #include "dsputil.h"
30 #include "avcodec.h"
31 #include "mpegvideo.h"
32 #include "vc1.h"
33 #include "vc1data.h"
34 #include "msmpeg4data.h"
35 #include "unary.h"
36 #include "simple_idct.h"
37
38 #undef NDEBUG
39 #include <assert.h>
40
41 /***********************************************************************/
42 /**
43  * @defgroup vc1bitplane VC-1 Bitplane decoding
44  * @see 8.7, p56
45  * @{
46  */
47
48 /**
49  * Imode types
50  * @{
51  */
52 enum Imode {
53     IMODE_RAW,
54     IMODE_NORM2,
55     IMODE_DIFF2,
56     IMODE_NORM6,
57     IMODE_DIFF6,
58     IMODE_ROWSKIP,
59     IMODE_COLSKIP
60 };
61 /** @} */ //imode defines
62
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
68  */
69 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
70     int x, y;
71
72     for (y=0; y<height; y++){
73         if (!get_bits1(gb)) //rowskip
74             memset(plane, 0, width);
75         else
76             for (x=0; x<width; x++)
77                 plane[x] = get_bits1(gb);
78         plane += stride;
79     }
80 }
81
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
88  */
89 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
90     int x, y;
91
92     for (x=0; x<width; x++){
93         if (!get_bits1(gb)) //colskip
94             for (y=0; y<height; y++)
95                 plane[y*stride] = 0;
96         else
97             for (y=0; y<height; y++)
98                 plane[y*stride] = get_bits1(gb);
99         plane ++;
100     }
101 }
102
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
107  * @return Status
108  * @todo FIXME: Optimize
109  */
110 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
111 {
112     GetBitContext *gb = &v->s.gb;
113
114     int imode, x, y, code, offset;
115     uint8_t invert, *planep = data;
116     int width, height, stride;
117
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);
123
124     *raw_flag = 0;
125     switch (imode)
126     {
127     case IMODE_RAW:
128         //Data is actually read in the MB layer (same for all tests == "raw")
129         *raw_flag = 1; //invert ignored
130         return invert;
131     case IMODE_DIFF2:
132     case IMODE_NORM2:
133         if ((height * width) & 1)
134         {
135             *planep++ = get_bits1(gb);
136             offset = 1;
137         }
138         else offset = 0;
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;
143             offset++;
144             if(offset == width) {
145                 offset = 0;
146                 planep += stride - width;
147             }
148             *planep++ = code >> 1;
149             offset++;
150             if(offset == width) {
151                 offset = 0;
152                 planep += stride - width;
153             }
154         }
155         break;
156     case IMODE_DIFF6:
157     case IMODE_NORM6:
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);
162                     if(code < 0){
163                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
164                         return -1;
165                     }
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;
172                 }
173                 planep += stride * 3;
174             }
175             if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
176         } else { // 3x2
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);
181                     if(code < 0){
182                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
183                         return -1;
184                     }
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;
191                 }
192                 planep += stride * 2;
193             }
194             x = width % 3;
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);
197         }
198         break;
199     case IMODE_ROWSKIP:
200         decode_rowskip(data, width, height, stride, &v->s.gb);
201         break;
202     case IMODE_COLSKIP:
203         decode_colskip(data, width, height, stride, &v->s.gb);
204         break;
205     default: break;
206     }
207
208     /* Applying diff operator */
209     if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
210     {
211         planep = data;
212         planep[0] ^= invert;
213         for (x=1; x<width; x++)
214             planep[x] ^= planep[x-1];
215         for (y=1; y<height; y++)
216         {
217             planep += stride;
218             planep[0] ^= planep[-stride];
219             for (x=1; x<width; x++)
220             {
221                 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
222                 else                                 planep[x] ^= planep[x-1];
223             }
224         }
225     }
226     else if (invert)
227     {
228         planep = data;
229         for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
230     }
231     return (imode<<1) + invert;
232 }
233
234 /** @} */ //Bitplane group
235
236 /***********************************************************************/
237 /** VOP Dquant decoding
238  * @param v VC-1 Context
239  */
240 static int vop_dquant_decoding(VC1Context *v)
241 {
242     GetBitContext *gb = &v->s.gb;
243     int pqdiff;
244
245     //variable size
246     if (v->dquant == 2)
247     {
248         pqdiff = get_bits(gb, 3);
249         if (pqdiff == 7) v->altpq = get_bits(gb, 5);
250         else v->altpq = v->pq + pqdiff + 1;
251     }
252     else
253     {
254         v->dquantfrm = get_bits1(gb);
255         if ( v->dquantfrm )
256         {
257             v->dqprofile = get_bits(gb, 2);
258             switch (v->dqprofile)
259             {
260             case DQPROFILE_SINGLE_EDGE:
261             case DQPROFILE_DOUBLE_EDGES:
262                 v->dqsbedge = get_bits(gb, 2);
263                 break;
264             case DQPROFILE_ALL_MBS:
265                 v->dqbilevel = get_bits1(gb);
266                 if(!v->dqbilevel)
267                     v->halfpq = 0;
268             default: break; //Forbidden ?
269             }
270             if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
271             {
272                 pqdiff = get_bits(gb, 3);
273                 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
274                 else v->altpq = v->pq + pqdiff + 1;
275             }
276         }
277     }
278     return 0;
279 }
280
281 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
282
283 /**
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
288  * @return Status
289  */
290 int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
291 {
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)
295     {
296         av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
297     }
298
299     if (v->profile == PROFILE_ADVANCED)
300     {
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);
304     }
305     else
306     {
307         v->zz_8x4 = wmv2_scantableA;
308         v->zz_4x8 = wmv2_scantableB;
309         v->res_sm = get_bits(gb, 2); //reserved
310         if (v->res_sm)
311         {
312             av_log(avctx, AV_LOG_ERROR,
313                    "Reserved RES_SM=%i is forbidden\n", v->res_sm);
314             return -1;
315         }
316     }
317
318     // (fps-2)/4 (->30)
319     v->frmrtq_postproc = get_bits(gb, 3); //common
320     // (bitrate-32kbps)/64kbps
321     v->bitrtq_postproc = get_bits(gb, 5); //common
322     v->s.loop_filter = get_bits1(gb); //common
323     if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
324     {
325         av_log(avctx, AV_LOG_ERROR,
326                "LOOPFILTER shall not be enabled in Simple Profile\n");
327     }
328     if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
329         v->s.loop_filter = 0;
330
331     v->res_x8 = get_bits1(gb); //reserved
332     v->multires = get_bits1(gb);
333     v->res_fasttx = get_bits1(gb);
334     if (!v->res_fasttx)
335     {
336         v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
337         v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
338         v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
339         v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
340         v->s.dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add;
341         v->s.dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
342         v->s.dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
343         v->s.dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
344     }
345
346     v->fastuvmc =  get_bits1(gb); //common
347     if (!v->profile && !v->fastuvmc)
348     {
349         av_log(avctx, AV_LOG_ERROR,
350                "FASTUVMC unavailable in Simple Profile\n");
351         return -1;
352     }
353     v->extended_mv =  get_bits1(gb); //common
354     if (!v->profile && v->extended_mv)
355     {
356         av_log(avctx, AV_LOG_ERROR,
357                "Extended MVs unavailable in Simple Profile\n");
358         return -1;
359     }
360     v->dquant =  get_bits(gb, 2); //common
361     v->vstransform =  get_bits1(gb); //common
362
363     v->res_transtab = get_bits1(gb);
364     if (v->res_transtab)
365     {
366         av_log(avctx, AV_LOG_ERROR,
367                "1 for reserved RES_TRANSTAB is forbidden\n");
368         return -1;
369     }
370
371     v->overlap = get_bits1(gb); //common
372
373     v->s.resync_marker = get_bits1(gb);
374     v->rangered = get_bits1(gb);
375     if (v->rangered && v->profile == PROFILE_SIMPLE)
376     {
377         av_log(avctx, AV_LOG_INFO,
378                "RANGERED should be set to 0 in Simple Profile\n");
379     }
380
381     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
382     v->quantizer_mode = get_bits(gb, 2); //common
383
384     v->finterpflag = get_bits1(gb); //common
385     v->res_rtm_flag = get_bits1(gb); //reserved
386     if (!v->res_rtm_flag)
387     {
388 //            av_log(avctx, AV_LOG_ERROR,
389 //                   "0 for reserved RES_RTM_FLAG is forbidden\n");
390         av_log(avctx, AV_LOG_ERROR,
391                "Old WMV3 version detected, only I-frames will be decoded\n");
392         //return -1;
393     }
394     //TODO: figure out what they mean (always 0x402F)
395     if(!v->res_fasttx) skip_bits(gb, 16);
396     av_log(avctx, AV_LOG_DEBUG,
397                "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
398                "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
399                "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
400                "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
401                v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
402                v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
403                v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
404                v->dquant, v->quantizer_mode, avctx->max_b_frames
405                );
406     return 0;
407 }
408
409 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
410 {
411     v->res_rtm_flag = 1;
412     v->level = get_bits(gb, 3);
413     if(v->level >= 5)
414     {
415         av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
416     }
417     v->chromaformat = get_bits(gb, 2);
418     if (v->chromaformat != 1)
419     {
420         av_log(v->s.avctx, AV_LOG_ERROR,
421                "Only 4:2:0 chroma format supported\n");
422         return -1;
423     }
424
425     // (fps-2)/4 (->30)
426     v->frmrtq_postproc = get_bits(gb, 3); //common
427     // (bitrate-32kbps)/64kbps
428     v->bitrtq_postproc = get_bits(gb, 5); //common
429     v->postprocflag = get_bits1(gb); //common
430
431     v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
432     v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
433     v->s.avctx->width = v->s.avctx->coded_width;
434     v->s.avctx->height = v->s.avctx->coded_height;
435     v->broadcast = get_bits1(gb);
436     v->interlace = get_bits1(gb);
437     v->tfcntrflag = get_bits1(gb);
438     v->finterpflag = get_bits1(gb);
439     skip_bits1(gb); // reserved
440
441     v->s.h_edge_pos = v->s.avctx->coded_width;
442     v->s.v_edge_pos = v->s.avctx->coded_height;
443
444     av_log(v->s.avctx, AV_LOG_DEBUG,
445                "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
446                "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
447                "TFCTRflag=%i, FINTERPflag=%i\n",
448                v->level, v->frmrtq_postproc, v->bitrtq_postproc,
449                v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
450                v->tfcntrflag, v->finterpflag
451                );
452
453     v->psf = get_bits1(gb);
454     if(v->psf) { //PsF, 6.1.13
455         av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
456         return -1;
457     }
458     v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
459     if(get_bits1(gb)) { //Display Info - decoding is not affected by it
460         int w, h, ar = 0;
461         av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
462         v->s.avctx->width  = w = get_bits(gb, 14) + 1;
463         v->s.avctx->height = h = get_bits(gb, 14) + 1;
464         av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
465         if(get_bits1(gb))
466             ar = get_bits(gb, 4);
467         if(ar && ar < 14){
468             v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
469         }else if(ar == 15){
470             w = get_bits(gb, 8);
471             h = get_bits(gb, 8);
472             v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
473         }
474         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);
475
476         if(get_bits1(gb)){ //framerate stuff
477             if(get_bits1(gb)) {
478                 v->s.avctx->time_base.num = 32;
479                 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
480             } else {
481                 int nr, dr;
482                 nr = get_bits(gb, 8);
483                 dr = get_bits(gb, 4);
484                 if(nr && nr < 8 && dr && dr < 3){
485                     v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
486                     v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
487                 }
488             }
489         }
490
491         if(get_bits1(gb)){
492             v->color_prim = get_bits(gb, 8);
493             v->transfer_char = get_bits(gb, 8);
494             v->matrix_coef = get_bits(gb, 8);
495         }
496     }
497
498     v->hrd_param_flag = get_bits1(gb);
499     if(v->hrd_param_flag) {
500         int i;
501         v->hrd_num_leaky_buckets = get_bits(gb, 5);
502         skip_bits(gb, 4); //bitrate exponent
503         skip_bits(gb, 4); //buffer size exponent
504         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
505             skip_bits(gb, 16); //hrd_rate[n]
506             skip_bits(gb, 16); //hrd_buffer[n]
507         }
508     }
509     return 0;
510 }
511
512 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
513 {
514     int i;
515
516     av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
517     v->broken_link = get_bits1(gb);
518     v->closed_entry = get_bits1(gb);
519     v->panscanflag = get_bits1(gb);
520     v->refdist_flag = get_bits1(gb);
521     v->s.loop_filter = get_bits1(gb);
522     v->fastuvmc = get_bits1(gb);
523     v->extended_mv = get_bits1(gb);
524     v->dquant = get_bits(gb, 2);
525     v->vstransform = get_bits1(gb);
526     v->overlap = get_bits1(gb);
527     v->quantizer_mode = get_bits(gb, 2);
528
529     if(v->hrd_param_flag){
530         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
531             skip_bits(gb, 8); //hrd_full[n]
532         }
533     }
534
535     if(get_bits1(gb)){
536         avctx->coded_width = (get_bits(gb, 12)+1)<<1;
537         avctx->coded_height = (get_bits(gb, 12)+1)<<1;
538     }
539     if(v->extended_mv)
540         v->extended_dmv = get_bits1(gb);
541     if((v->range_mapy_flag = get_bits1(gb))) {
542         av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
543         v->range_mapy = get_bits(gb, 3);
544     }
545     if((v->range_mapuv_flag = get_bits1(gb))) {
546         av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
547         v->range_mapuv = get_bits(gb, 3);
548     }
549
550     av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
551         "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
552         "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
553         "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
554         v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
555         v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
556
557     return 0;
558 }
559
560 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
561 {
562     int pqindex, lowquant, status;
563
564     if(v->finterpflag) v->interpfrm = get_bits1(gb);
565     skip_bits(gb, 2); //framecnt unused
566     v->rangeredfrm = 0;
567     if (v->rangered) v->rangeredfrm = get_bits1(gb);
568     v->s.pict_type = get_bits1(gb);
569     if (v->s.avctx->max_b_frames) {
570         if (!v->s.pict_type) {
571             if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
572             else v->s.pict_type = FF_B_TYPE;
573         } else v->s.pict_type = FF_P_TYPE;
574     } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
575
576     v->bi_type = 0;
577     if(v->s.pict_type == FF_B_TYPE) {
578         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
579         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
580         if(v->bfraction == 0) {
581             v->s.pict_type = FF_BI_TYPE;
582         }
583     }
584     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
585         skip_bits(gb, 7); // skip buffer fullness
586
587     if(v->parse_only)
588         return 0;
589
590     /* calculate RND */
591     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
592         v->rnd = 1;
593     if(v->s.pict_type == FF_P_TYPE)
594         v->rnd ^= 1;
595
596     /* Quantizer stuff */
597     pqindex = get_bits(gb, 5);
598     if(!pqindex) return -1;
599     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
600         v->pq = ff_vc1_pquant_table[0][pqindex];
601     else
602         v->pq = ff_vc1_pquant_table[1][pqindex];
603
604     v->pquantizer = 1;
605     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
606         v->pquantizer = pqindex < 9;
607     if (v->quantizer_mode == QUANT_NON_UNIFORM)
608         v->pquantizer = 0;
609     v->pqindex = pqindex;
610     if (pqindex < 9) v->halfpq = get_bits1(gb);
611     else v->halfpq = 0;
612     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
613         v->pquantizer = get_bits1(gb);
614     v->dquantfrm = 0;
615     if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
616     v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
617     v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
618     v->range_x = 1 << (v->k_x - 1);
619     v->range_y = 1 << (v->k_y - 1);
620     if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
621
622     if(v->res_x8 && (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)){
623         v->x8_type = get_bits1(gb);
624     }else v->x8_type = 0;
625 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
626 //        (v->s.pict_type == FF_P_TYPE) ? 'P' : ((v->s.pict_type == FF_I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
627
628     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
629
630     switch(v->s.pict_type) {
631     case FF_P_TYPE:
632         if (v->pq < 5) v->tt_index = 0;
633         else if(v->pq < 13) v->tt_index = 1;
634         else v->tt_index = 2;
635
636         lowquant = (v->pq > 12) ? 0 : 1;
637         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
638         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
639         {
640             int scale, shift, i;
641             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
642             v->lumscale = get_bits(gb, 6);
643             v->lumshift = get_bits(gb, 6);
644             v->use_ic = 1;
645             /* fill lookup tables for intensity compensation */
646             if(!v->lumscale) {
647                 scale = -64;
648                 shift = (255 - v->lumshift * 2) << 6;
649                 if(v->lumshift > 31)
650                     shift += 128 << 6;
651             } else {
652                 scale = v->lumscale + 32;
653                 if(v->lumshift > 31)
654                     shift = (v->lumshift - 64) << 6;
655                 else
656                     shift = v->lumshift << 6;
657             }
658             for(i = 0; i < 256; i++) {
659                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
660                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
661             }
662         }
663         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
664             v->s.quarter_sample = 0;
665         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
666             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
667                 v->s.quarter_sample = 0;
668             else
669                 v->s.quarter_sample = 1;
670         } else
671             v->s.quarter_sample = 1;
672         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));
673
674         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
675                  v->mv_mode2 == MV_PMODE_MIXED_MV)
676                 || v->mv_mode == MV_PMODE_MIXED_MV)
677         {
678             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
679             if (status < 0) return -1;
680             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
681                    "Imode: %i, Invert: %i\n", status>>1, status&1);
682         } else {
683             v->mv_type_is_raw = 0;
684             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
685         }
686         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
687         if (status < 0) return -1;
688         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
689                "Imode: %i, Invert: %i\n", status>>1, status&1);
690
691         /* Hopefully this is correct for P frames */
692         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
693         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
694
695         if (v->dquant)
696         {
697             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
698             vop_dquant_decoding(v);
699         }
700
701         v->ttfrm = 0; //FIXME Is that so ?
702         if (v->vstransform)
703         {
704             v->ttmbf = get_bits1(gb);
705             if (v->ttmbf)
706             {
707                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
708             }
709         } else {
710             v->ttmbf = 1;
711             v->ttfrm = TT_8X8;
712         }
713         break;
714     case FF_B_TYPE:
715         if (v->pq < 5) v->tt_index = 0;
716         else if(v->pq < 13) v->tt_index = 1;
717         else v->tt_index = 2;
718
719         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
720         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
721         v->s.mspel = v->s.quarter_sample;
722
723         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
724         if (status < 0) return -1;
725         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
726                "Imode: %i, Invert: %i\n", status>>1, status&1);
727         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
728         if (status < 0) return -1;
729         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
730                "Imode: %i, Invert: %i\n", status>>1, status&1);
731
732         v->s.mv_table_index = get_bits(gb, 2);
733         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
734
735         if (v->dquant)
736         {
737             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
738             vop_dquant_decoding(v);
739         }
740
741         v->ttfrm = 0;
742         if (v->vstransform)
743         {
744             v->ttmbf = get_bits1(gb);
745             if (v->ttmbf)
746             {
747                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
748             }
749         } else {
750             v->ttmbf = 1;
751             v->ttfrm = TT_8X8;
752         }
753         break;
754     }
755
756     if(!v->x8_type)
757     {
758         /* AC Syntax */
759         v->c_ac_table_index = decode012(gb);
760         if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
761         {
762             v->y_ac_table_index = decode012(gb);
763         }
764         /* DC Syntax */
765         v->s.dc_table_index = get_bits1(gb);
766     }
767
768     if(v->s.pict_type == FF_BI_TYPE) {
769         v->s.pict_type = FF_B_TYPE;
770         v->bi_type = 1;
771     }
772     return 0;
773 }
774
775 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
776 {
777     int pqindex, lowquant;
778     int status;
779
780     v->p_frame_skipped = 0;
781
782     if(v->interlace){
783         v->fcm = decode012(gb);
784         if(v->fcm){
785             if(!v->warn_interlaced++)
786                 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
787             return -1;
788         }
789     }
790     switch(get_unary(gb, 0, 4)) {
791     case 0:
792         v->s.pict_type = FF_P_TYPE;
793         break;
794     case 1:
795         v->s.pict_type = FF_B_TYPE;
796         break;
797     case 2:
798         v->s.pict_type = FF_I_TYPE;
799         break;
800     case 3:
801         v->s.pict_type = FF_BI_TYPE;
802         break;
803     case 4:
804         v->s.pict_type = FF_P_TYPE; // skipped pic
805         v->p_frame_skipped = 1;
806         return 0;
807     }
808     if(v->tfcntrflag)
809         skip_bits(gb, 8);
810     if(v->broadcast) {
811         if(!v->interlace || v->psf) {
812             v->rptfrm = get_bits(gb, 2);
813         } else {
814             v->tff = get_bits1(gb);
815             v->rptfrm = get_bits1(gb);
816         }
817     }
818     if(v->panscanflag) {
819         //...
820     }
821     v->rnd = get_bits1(gb);
822     if(v->interlace)
823         v->uvsamp = get_bits1(gb);
824     if(v->finterpflag) v->interpfrm = get_bits1(gb);
825     if(v->s.pict_type == FF_B_TYPE) {
826         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
827         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
828         if(v->bfraction == 0) {
829             v->s.pict_type = FF_BI_TYPE; /* XXX: should not happen here */
830         }
831     }
832     pqindex = get_bits(gb, 5);
833     if(!pqindex) return -1;
834     v->pqindex = pqindex;
835     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
836         v->pq = ff_vc1_pquant_table[0][pqindex];
837     else
838         v->pq = ff_vc1_pquant_table[1][pqindex];
839
840     v->pquantizer = 1;
841     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
842         v->pquantizer = pqindex < 9;
843     if (v->quantizer_mode == QUANT_NON_UNIFORM)
844         v->pquantizer = 0;
845     v->pqindex = pqindex;
846     if (pqindex < 9) v->halfpq = get_bits1(gb);
847     else v->halfpq = 0;
848     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
849         v->pquantizer = get_bits1(gb);
850     if(v->postprocflag)
851         v->postproc = get_bits(gb, 2);
852
853     if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
854
855     if(v->parse_only)
856         return 0;
857
858     switch(v->s.pict_type) {
859     case FF_I_TYPE:
860     case FF_BI_TYPE:
861         status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
862         if (status < 0) return -1;
863         av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
864                 "Imode: %i, Invert: %i\n", status>>1, status&1);
865         v->condover = CONDOVER_NONE;
866         if(v->overlap && v->pq <= 8) {
867             v->condover = decode012(gb);
868             if(v->condover == CONDOVER_SELECT) {
869                 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
870                 if (status < 0) return -1;
871                 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
872                         "Imode: %i, Invert: %i\n", status>>1, status&1);
873             }
874         }
875         break;
876     case FF_P_TYPE:
877         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
878         else v->mvrange = 0;
879         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
880         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
881         v->range_x = 1 << (v->k_x - 1);
882         v->range_y = 1 << (v->k_y - 1);
883
884         if (v->pq < 5) v->tt_index = 0;
885         else if(v->pq < 13) v->tt_index = 1;
886         else v->tt_index = 2;
887
888         lowquant = (v->pq > 12) ? 0 : 1;
889         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
890         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
891         {
892             int scale, shift, i;
893             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
894             v->lumscale = get_bits(gb, 6);
895             v->lumshift = get_bits(gb, 6);
896             /* fill lookup tables for intensity compensation */
897             if(!v->lumscale) {
898                 scale = -64;
899                 shift = (255 - v->lumshift * 2) << 6;
900                 if(v->lumshift > 31)
901                     shift += 128 << 6;
902             } else {
903                 scale = v->lumscale + 32;
904                 if(v->lumshift > 31)
905                     shift = (v->lumshift - 64) << 6;
906                 else
907                     shift = v->lumshift << 6;
908             }
909             for(i = 0; i < 256; i++) {
910                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
911                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
912             }
913             v->use_ic = 1;
914         }
915         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
916             v->s.quarter_sample = 0;
917         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
918             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
919                 v->s.quarter_sample = 0;
920             else
921                 v->s.quarter_sample = 1;
922         } else
923             v->s.quarter_sample = 1;
924         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));
925
926         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
927                  v->mv_mode2 == MV_PMODE_MIXED_MV)
928                 || v->mv_mode == MV_PMODE_MIXED_MV)
929         {
930             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
931             if (status < 0) return -1;
932             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
933                    "Imode: %i, Invert: %i\n", status>>1, status&1);
934         } else {
935             v->mv_type_is_raw = 0;
936             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
937         }
938         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
939         if (status < 0) return -1;
940         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
941                "Imode: %i, Invert: %i\n", status>>1, status&1);
942
943         /* Hopefully this is correct for P frames */
944         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
945         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
946         if (v->dquant)
947         {
948             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
949             vop_dquant_decoding(v);
950         }
951
952         v->ttfrm = 0; //FIXME Is that so ?
953         if (v->vstransform)
954         {
955             v->ttmbf = get_bits1(gb);
956             if (v->ttmbf)
957             {
958                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
959             }
960         } else {
961             v->ttmbf = 1;
962             v->ttfrm = TT_8X8;
963         }
964         break;
965     case FF_B_TYPE:
966         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
967         else v->mvrange = 0;
968         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
969         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
970         v->range_x = 1 << (v->k_x - 1);
971         v->range_y = 1 << (v->k_y - 1);
972
973         if (v->pq < 5) v->tt_index = 0;
974         else if(v->pq < 13) v->tt_index = 1;
975         else v->tt_index = 2;
976
977         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
978         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
979         v->s.mspel = v->s.quarter_sample;
980
981         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
982         if (status < 0) return -1;
983         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
984                "Imode: %i, Invert: %i\n", status>>1, status&1);
985         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
986         if (status < 0) return -1;
987         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
988                "Imode: %i, Invert: %i\n", status>>1, status&1);
989
990         v->s.mv_table_index = get_bits(gb, 2);
991         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
992
993         if (v->dquant)
994         {
995             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
996             vop_dquant_decoding(v);
997         }
998
999         v->ttfrm = 0;
1000         if (v->vstransform)
1001         {
1002             v->ttmbf = get_bits1(gb);
1003             if (v->ttmbf)
1004             {
1005                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1006             }
1007         } else {
1008             v->ttmbf = 1;
1009             v->ttfrm = TT_8X8;
1010         }
1011         break;
1012     }
1013
1014     /* AC Syntax */
1015     v->c_ac_table_index = decode012(gb);
1016     if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1017     {
1018         v->y_ac_table_index = decode012(gb);
1019     }
1020     /* DC Syntax */
1021     v->s.dc_table_index = get_bits1(gb);
1022     if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
1023         av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1024         vop_dquant_decoding(v);
1025     }
1026
1027     v->bi_type = 0;
1028     if(v->s.pict_type == FF_BI_TYPE) {
1029         v->s.pict_type = FF_B_TYPE;
1030         v->bi_type = 1;
1031     }
1032     return 0;
1033 }