]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1.c
c3649ac3839e2a9872a0a30d8e59caa7d2e7cafb
[ffmpeg] / 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 Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
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  * @name 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_WARNING, "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_y411   = get_bits1(gb);
310         v->res_sprite = get_bits1(gb);
311         if (v->res_y411)
312         {
313             av_log(avctx, AV_LOG_ERROR,
314                    "Old interlaced mode is not supported\n");
315             return -1;
316         }
317     }
318
319     // (fps-2)/4 (->30)
320     v->frmrtq_postproc = get_bits(gb, 3); //common
321     // (bitrate-32kbps)/64kbps
322     v->bitrtq_postproc = get_bits(gb, 5); //common
323     v->s.loop_filter = get_bits1(gb); //common
324     if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
325     {
326         av_log(avctx, AV_LOG_ERROR,
327                "LOOPFILTER shall not be enabled in Simple Profile\n");
328     }
329     if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
330         v->s.loop_filter = 0;
331
332     v->res_x8 = get_bits1(gb); //reserved
333     v->multires = get_bits1(gb);
334     v->res_fasttx = get_bits1(gb);
335     if (!v->res_fasttx)
336     {
337         v->vc1dsp.vc1_inv_trans_8x8 = ff_simple_idct_8;
338         v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
339         v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
340         v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
341         v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add_8;
342         v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
343         v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
344         v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
345     }
346
347     v->fastuvmc =  get_bits1(gb); //common
348     if (!v->profile && !v->fastuvmc)
349     {
350         av_log(avctx, AV_LOG_ERROR,
351                "FASTUVMC unavailable in Simple Profile\n");
352         return -1;
353     }
354     v->extended_mv =  get_bits1(gb); //common
355     if (!v->profile && v->extended_mv)
356     {
357         av_log(avctx, AV_LOG_ERROR,
358                "Extended MVs unavailable in Simple Profile\n");
359         return -1;
360     }
361     v->dquant =  get_bits(gb, 2); //common
362     v->vstransform =  get_bits1(gb); //common
363
364     v->res_transtab = get_bits1(gb);
365     if (v->res_transtab)
366     {
367         av_log(avctx, AV_LOG_ERROR,
368                "1 for reserved RES_TRANSTAB is forbidden\n");
369         return -1;
370     }
371
372     v->overlap = get_bits1(gb); //common
373
374     v->s.resync_marker = get_bits1(gb);
375     v->rangered = get_bits1(gb);
376     if (v->rangered && v->profile == PROFILE_SIMPLE)
377     {
378         av_log(avctx, AV_LOG_INFO,
379                "RANGERED should be set to 0 in Simple Profile\n");
380     }
381
382     v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
383     v->quantizer_mode = get_bits(gb, 2); //common
384
385     v->finterpflag = get_bits1(gb); //common
386
387     if (v->res_sprite) {
388         v->s.avctx->width  = v->s.avctx->coded_width  = get_bits(gb, 11);
389         v->s.avctx->height = v->s.avctx->coded_height = get_bits(gb, 11);
390         skip_bits(gb, 5); //frame rate
391         v->res_x8 = get_bits1(gb);
392         if (get_bits1(gb)) { // something to do with DC VLC selection
393             av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
394             return -1;
395         }
396         skip_bits(gb, 3); //slice code
397         v->res_rtm_flag = 0;
398     } else {
399         v->res_rtm_flag = get_bits1(gb); //reserved
400     }
401     if (!v->res_rtm_flag)
402     {
403 //            av_log(avctx, AV_LOG_ERROR,
404 //                   "0 for reserved RES_RTM_FLAG is forbidden\n");
405         av_log(avctx, AV_LOG_ERROR,
406                "Old WMV3 version detected, some frames may be decoded incorrectly\n");
407         //return -1;
408     }
409     //TODO: figure out what they mean (always 0x402F)
410     if(!v->res_fasttx) skip_bits(gb, 16);
411     av_log(avctx, AV_LOG_DEBUG,
412                "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
413                "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
414                "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
415                "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
416                v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
417                v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
418                v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
419                v->dquant, v->quantizer_mode, avctx->max_b_frames
420                );
421     return 0;
422 }
423
424 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
425 {
426     v->res_rtm_flag = 1;
427     v->level = get_bits(gb, 3);
428     if(v->level >= 5)
429     {
430         av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
431     }
432     v->chromaformat = get_bits(gb, 2);
433     if (v->chromaformat != 1)
434     {
435         av_log(v->s.avctx, AV_LOG_ERROR,
436                "Only 4:2:0 chroma format supported\n");
437         return -1;
438     }
439
440     // (fps-2)/4 (->30)
441     v->frmrtq_postproc = get_bits(gb, 3); //common
442     // (bitrate-32kbps)/64kbps
443     v->bitrtq_postproc = get_bits(gb, 5); //common
444     v->postprocflag = get_bits1(gb); //common
445
446     v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
447     v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
448     v->s.avctx->width = v->s.avctx->coded_width;
449     v->s.avctx->height = v->s.avctx->coded_height;
450     v->broadcast = get_bits1(gb);
451     v->interlace = get_bits1(gb);
452     v->tfcntrflag = get_bits1(gb);
453     v->finterpflag = get_bits1(gb);
454     skip_bits1(gb); // reserved
455
456     v->s.h_edge_pos = v->s.avctx->coded_width;
457     v->s.v_edge_pos = v->s.avctx->coded_height;
458
459     av_log(v->s.avctx, AV_LOG_DEBUG,
460                "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
461                "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
462                "TFCTRflag=%i, FINTERPflag=%i\n",
463                v->level, v->frmrtq_postproc, v->bitrtq_postproc,
464                v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
465                v->tfcntrflag, v->finterpflag
466                );
467
468     v->psf = get_bits1(gb);
469     if(v->psf) { //PsF, 6.1.13
470         av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
471         return -1;
472     }
473     v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
474     if(get_bits1(gb)) { //Display Info - decoding is not affected by it
475         int w, h, ar = 0;
476         av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
477         v->s.avctx->width  = w = get_bits(gb, 14) + 1;
478         v->s.avctx->height = h = get_bits(gb, 14) + 1;
479         av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
480         if(get_bits1(gb))
481             ar = get_bits(gb, 4);
482         if(ar && ar < 14){
483             v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
484         }else if(ar == 15){
485             w = get_bits(gb, 8) + 1;
486             h = get_bits(gb, 8) + 1;
487             v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
488         }
489         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);
490
491         if(get_bits1(gb)){ //framerate stuff
492             if(get_bits1(gb)) {
493                 v->s.avctx->time_base.num = 32;
494                 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
495             } else {
496                 int nr, dr;
497                 nr = get_bits(gb, 8);
498                 dr = get_bits(gb, 4);
499                 if(nr && nr < 8 && dr && dr < 3){
500                     v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
501                     v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
502                 }
503             }
504             if(v->broadcast) { // Pulldown may be present
505                 v->s.avctx->time_base.den *= 2;
506                 v->s.avctx->ticks_per_frame = 2;
507             }
508         }
509
510         if(get_bits1(gb)){
511             v->color_prim = get_bits(gb, 8);
512             v->transfer_char = get_bits(gb, 8);
513             v->matrix_coef = get_bits(gb, 8);
514         }
515     }
516
517     v->hrd_param_flag = get_bits1(gb);
518     if(v->hrd_param_flag) {
519         int i;
520         v->hrd_num_leaky_buckets = get_bits(gb, 5);
521         skip_bits(gb, 4); //bitrate exponent
522         skip_bits(gb, 4); //buffer size exponent
523         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
524             skip_bits(gb, 16); //hrd_rate[n]
525             skip_bits(gb, 16); //hrd_buffer[n]
526         }
527     }
528     return 0;
529 }
530
531 int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
532 {
533     int i;
534
535     av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
536     v->broken_link = get_bits1(gb);
537     v->closed_entry = get_bits1(gb);
538     v->panscanflag = get_bits1(gb);
539     v->refdist_flag = get_bits1(gb);
540     v->s.loop_filter = get_bits1(gb);
541     v->fastuvmc = get_bits1(gb);
542     v->extended_mv = get_bits1(gb);
543     v->dquant = get_bits(gb, 2);
544     v->vstransform = get_bits1(gb);
545     v->overlap = get_bits1(gb);
546     v->quantizer_mode = get_bits(gb, 2);
547
548     if(v->hrd_param_flag){
549         for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
550             skip_bits(gb, 8); //hrd_full[n]
551         }
552     }
553
554     if(get_bits1(gb)){
555         avctx->coded_width = (get_bits(gb, 12)+1)<<1;
556         avctx->coded_height = (get_bits(gb, 12)+1)<<1;
557     }
558     if(v->extended_mv)
559         v->extended_dmv = get_bits1(gb);
560     if((v->range_mapy_flag = get_bits1(gb))) {
561         av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
562         v->range_mapy = get_bits(gb, 3);
563     }
564     if((v->range_mapuv_flag = get_bits1(gb))) {
565         av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
566         v->range_mapuv = get_bits(gb, 3);
567     }
568
569     av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
570         "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
571         "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
572         "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
573         v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
574         v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
575
576     return 0;
577 }
578
579 int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
580 {
581     int pqindex, lowquant, status;
582
583     if(v->finterpflag) v->interpfrm = get_bits1(gb);
584     skip_bits(gb, 2); //framecnt unused
585     v->rangeredfrm = 0;
586     if (v->rangered) v->rangeredfrm = get_bits1(gb);
587     v->s.pict_type = get_bits1(gb);
588     if (v->s.avctx->max_b_frames) {
589         if (!v->s.pict_type) {
590             if (get_bits1(gb)) v->s.pict_type = AV_PICTURE_TYPE_I;
591             else v->s.pict_type = AV_PICTURE_TYPE_B;
592         } else v->s.pict_type = AV_PICTURE_TYPE_P;
593     } else v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
594
595     v->bi_type = 0;
596     if(v->s.pict_type == AV_PICTURE_TYPE_B) {
597         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
598         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
599         if(v->bfraction == 0) {
600             v->s.pict_type = AV_PICTURE_TYPE_BI;
601         }
602     }
603     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
604         skip_bits(gb, 7); // skip buffer fullness
605
606     if(v->parse_only)
607         return 0;
608
609     /* calculate RND */
610     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
611         v->rnd = 1;
612     if(v->s.pict_type == AV_PICTURE_TYPE_P)
613         v->rnd ^= 1;
614
615     /* Quantizer stuff */
616     pqindex = get_bits(gb, 5);
617     if(!pqindex) return -1;
618     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
619         v->pq = ff_vc1_pquant_table[0][pqindex];
620     else
621         v->pq = ff_vc1_pquant_table[1][pqindex];
622
623     v->pquantizer = 1;
624     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
625         v->pquantizer = pqindex < 9;
626     if (v->quantizer_mode == QUANT_NON_UNIFORM)
627         v->pquantizer = 0;
628     v->pqindex = pqindex;
629     if (pqindex < 9) v->halfpq = get_bits1(gb);
630     else v->halfpq = 0;
631     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
632         v->pquantizer = get_bits1(gb);
633     v->dquantfrm = 0;
634     if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
635     v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
636     v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
637     v->range_x = 1 << (v->k_x - 1);
638     v->range_y = 1 << (v->k_y - 1);
639     if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B) v->respic = get_bits(gb, 2);
640
641     if(v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)){
642         v->x8_type = get_bits1(gb);
643     }else v->x8_type = 0;
644 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
645 //        (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);
646
647     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
648
649     switch(v->s.pict_type) {
650     case AV_PICTURE_TYPE_P:
651         if (v->pq < 5) v->tt_index = 0;
652         else if(v->pq < 13) v->tt_index = 1;
653         else v->tt_index = 2;
654
655         lowquant = (v->pq > 12) ? 0 : 1;
656         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
657         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
658         {
659             int scale, shift, i;
660             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
661             v->lumscale = get_bits(gb, 6);
662             v->lumshift = get_bits(gb, 6);
663             v->use_ic = 1;
664             /* fill lookup tables for intensity compensation */
665             if(!v->lumscale) {
666                 scale = -64;
667                 shift = (255 - v->lumshift * 2) << 6;
668                 if(v->lumshift > 31)
669                     shift += 128 << 6;
670             } else {
671                 scale = v->lumscale + 32;
672                 if(v->lumshift > 31)
673                     shift = (v->lumshift - 64) << 6;
674                 else
675                     shift = v->lumshift << 6;
676             }
677             for(i = 0; i < 256; i++) {
678                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
679                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
680             }
681         }
682         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
683             v->s.quarter_sample = 0;
684         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
685             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
686                 v->s.quarter_sample = 0;
687             else
688                 v->s.quarter_sample = 1;
689         } else
690             v->s.quarter_sample = 1;
691         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));
692
693         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
694                  v->mv_mode2 == MV_PMODE_MIXED_MV)
695                 || v->mv_mode == MV_PMODE_MIXED_MV)
696         {
697             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
698             if (status < 0) return -1;
699             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
700                    "Imode: %i, Invert: %i\n", status>>1, status&1);
701         } else {
702             v->mv_type_is_raw = 0;
703             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
704         }
705         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
706         if (status < 0) return -1;
707         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
708                "Imode: %i, Invert: %i\n", status>>1, status&1);
709
710         /* Hopefully this is correct for P frames */
711         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
712         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
713
714         if (v->dquant)
715         {
716             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
717             vop_dquant_decoding(v);
718         }
719
720         v->ttfrm = 0; //FIXME Is that so ?
721         if (v->vstransform)
722         {
723             v->ttmbf = get_bits1(gb);
724             if (v->ttmbf)
725             {
726                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
727             }
728         } else {
729             v->ttmbf = 1;
730             v->ttfrm = TT_8X8;
731         }
732         break;
733     case AV_PICTURE_TYPE_B:
734         if (v->pq < 5) v->tt_index = 0;
735         else if(v->pq < 13) v->tt_index = 1;
736         else v->tt_index = 2;
737
738         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
739         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
740         v->s.mspel = v->s.quarter_sample;
741
742         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
743         if (status < 0) return -1;
744         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
745                "Imode: %i, Invert: %i\n", status>>1, status&1);
746         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
747         if (status < 0) return -1;
748         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
749                "Imode: %i, Invert: %i\n", status>>1, status&1);
750
751         v->s.mv_table_index = get_bits(gb, 2);
752         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
753
754         if (v->dquant)
755         {
756             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
757             vop_dquant_decoding(v);
758         }
759
760         v->ttfrm = 0;
761         if (v->vstransform)
762         {
763             v->ttmbf = get_bits1(gb);
764             if (v->ttmbf)
765             {
766                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
767             }
768         } else {
769             v->ttmbf = 1;
770             v->ttfrm = TT_8X8;
771         }
772         break;
773     }
774
775     if(!v->x8_type)
776     {
777         /* AC Syntax */
778         v->c_ac_table_index = decode012(gb);
779         if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
780         {
781             v->y_ac_table_index = decode012(gb);
782         }
783         /* DC Syntax */
784         v->s.dc_table_index = get_bits1(gb);
785     }
786
787     if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
788         v->s.pict_type = AV_PICTURE_TYPE_B;
789         v->bi_type = 1;
790     }
791     return 0;
792 }
793
794 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
795 {
796     int pqindex, lowquant;
797     int status;
798
799     v->p_frame_skipped = 0;
800
801     if(v->interlace){
802         v->fcm = decode012(gb);
803         if(v->fcm){
804             if(!v->warn_interlaced++)
805                 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
806             return -1;
807         }
808     }
809     switch(get_unary(gb, 0, 4)) {
810     case 0:
811         v->s.pict_type = AV_PICTURE_TYPE_P;
812         break;
813     case 1:
814         v->s.pict_type = AV_PICTURE_TYPE_B;
815         break;
816     case 2:
817         v->s.pict_type = AV_PICTURE_TYPE_I;
818         break;
819     case 3:
820         v->s.pict_type = AV_PICTURE_TYPE_BI;
821         break;
822     case 4:
823         v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
824         v->p_frame_skipped = 1;
825         break;
826     }
827     if(v->tfcntrflag)
828         skip_bits(gb, 8);
829     if(v->broadcast) {
830         if(!v->interlace || v->psf) {
831             v->rptfrm = get_bits(gb, 2);
832         } else {
833             v->tff = get_bits1(gb);
834             v->rff = get_bits1(gb);
835         }
836     }
837     if(v->panscanflag) {
838         av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
839         //...
840     }
841     if(v->p_frame_skipped) {
842         return 0;
843     }
844     v->rnd = get_bits1(gb);
845     if(v->interlace)
846         v->uvsamp = get_bits1(gb);
847     if(v->finterpflag) v->interpfrm = get_bits1(gb);
848     if(v->s.pict_type == AV_PICTURE_TYPE_B) {
849         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
850         v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
851         if(v->bfraction == 0) {
852             v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
853         }
854     }
855     pqindex = get_bits(gb, 5);
856     if(!pqindex) return -1;
857     v->pqindex = pqindex;
858     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
859         v->pq = ff_vc1_pquant_table[0][pqindex];
860     else
861         v->pq = ff_vc1_pquant_table[1][pqindex];
862
863     v->pquantizer = 1;
864     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
865         v->pquantizer = pqindex < 9;
866     if (v->quantizer_mode == QUANT_NON_UNIFORM)
867         v->pquantizer = 0;
868     v->pqindex = pqindex;
869     if (pqindex < 9) v->halfpq = get_bits1(gb);
870     else v->halfpq = 0;
871     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
872         v->pquantizer = get_bits1(gb);
873     if(v->postprocflag)
874         v->postproc = get_bits(gb, 2);
875
876     if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
877
878     if(v->parse_only)
879         return 0;
880
881     switch(v->s.pict_type) {
882     case AV_PICTURE_TYPE_I:
883     case AV_PICTURE_TYPE_BI:
884         status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
885         if (status < 0) return -1;
886         av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
887                 "Imode: %i, Invert: %i\n", status>>1, status&1);
888         v->condover = CONDOVER_NONE;
889         if(v->overlap && v->pq <= 8) {
890             v->condover = decode012(gb);
891             if(v->condover == CONDOVER_SELECT) {
892                 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
893                 if (status < 0) return -1;
894                 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
895                         "Imode: %i, Invert: %i\n", status>>1, status&1);
896             }
897         }
898         break;
899     case AV_PICTURE_TYPE_P:
900         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
901         else v->mvrange = 0;
902         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
903         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
904         v->range_x = 1 << (v->k_x - 1);
905         v->range_y = 1 << (v->k_y - 1);
906
907         if (v->pq < 5) v->tt_index = 0;
908         else if(v->pq < 13) v->tt_index = 1;
909         else v->tt_index = 2;
910
911         lowquant = (v->pq > 12) ? 0 : 1;
912         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
913         if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
914         {
915             int scale, shift, i;
916             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
917             v->lumscale = get_bits(gb, 6);
918             v->lumshift = get_bits(gb, 6);
919             /* fill lookup tables for intensity compensation */
920             if(!v->lumscale) {
921                 scale = -64;
922                 shift = (255 - v->lumshift * 2) << 6;
923                 if(v->lumshift > 31)
924                     shift += 128 << 6;
925             } else {
926                 scale = v->lumscale + 32;
927                 if(v->lumshift > 31)
928                     shift = (v->lumshift - 64) << 6;
929                 else
930                     shift = v->lumshift << 6;
931             }
932             for(i = 0; i < 256; i++) {
933                 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
934                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
935             }
936             v->use_ic = 1;
937         }
938         if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
939             v->s.quarter_sample = 0;
940         else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
941             if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
942                 v->s.quarter_sample = 0;
943             else
944                 v->s.quarter_sample = 1;
945         } else
946             v->s.quarter_sample = 1;
947         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));
948
949         if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
950                  v->mv_mode2 == MV_PMODE_MIXED_MV)
951                 || v->mv_mode == MV_PMODE_MIXED_MV)
952         {
953             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
954             if (status < 0) return -1;
955             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
956                    "Imode: %i, Invert: %i\n", status>>1, status&1);
957         } else {
958             v->mv_type_is_raw = 0;
959             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
960         }
961         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
962         if (status < 0) return -1;
963         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
964                "Imode: %i, Invert: %i\n", status>>1, status&1);
965
966         /* Hopefully this is correct for P frames */
967         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
968         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
969         if (v->dquant)
970         {
971             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
972             vop_dquant_decoding(v);
973         }
974
975         v->ttfrm = 0; //FIXME Is that so ?
976         if (v->vstransform)
977         {
978             v->ttmbf = get_bits1(gb);
979             if (v->ttmbf)
980             {
981                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
982             }
983         } else {
984             v->ttmbf = 1;
985             v->ttfrm = TT_8X8;
986         }
987         break;
988     case AV_PICTURE_TYPE_B:
989         if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
990         else v->mvrange = 0;
991         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
992         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
993         v->range_x = 1 << (v->k_x - 1);
994         v->range_y = 1 << (v->k_y - 1);
995
996         if (v->pq < 5) v->tt_index = 0;
997         else if(v->pq < 13) v->tt_index = 1;
998         else v->tt_index = 2;
999
1000         v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1001         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1002         v->s.mspel = v->s.quarter_sample;
1003
1004         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1005         if (status < 0) return -1;
1006         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1007                "Imode: %i, Invert: %i\n", status>>1, status&1);
1008         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1009         if (status < 0) return -1;
1010         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1011                "Imode: %i, Invert: %i\n", status>>1, status&1);
1012
1013         v->s.mv_table_index = get_bits(gb, 2);
1014         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1015
1016         if (v->dquant)
1017         {
1018             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1019             vop_dquant_decoding(v);
1020         }
1021
1022         v->ttfrm = 0;
1023         if (v->vstransform)
1024         {
1025             v->ttmbf = get_bits1(gb);
1026             if (v->ttmbf)
1027             {
1028                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1029             }
1030         } else {
1031             v->ttmbf = 1;
1032             v->ttfrm = TT_8X8;
1033         }
1034         break;
1035     }
1036
1037     /* AC Syntax */
1038     v->c_ac_table_index = decode012(gb);
1039     if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
1040     {
1041         v->y_ac_table_index = decode012(gb);
1042     }
1043     /* DC Syntax */
1044     v->s.dc_table_index = get_bits1(gb);
1045     if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) && v->dquant) {
1046         av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1047         vop_dquant_decoding(v);
1048     }
1049
1050     v->bi_type = 0;
1051     if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
1052         v->s.pict_type = AV_PICTURE_TYPE_B;
1053         v->bi_type = 1;
1054     }
1055     return 0;
1056 }