Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ ae210479

History | View | Annotate | Download (35.4 KB)

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
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_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
        if (v->res_sprite) {
318
            av_log(avctx, AV_LOG_ERROR, "WMVP is not fully supported\n");
319
        }
320
    }
321

    
322
    // (fps-2)/4 (->30)
323
    v->frmrtq_postproc = get_bits(gb, 3); //common
324
    // (bitrate-32kbps)/64kbps
325
    v->bitrtq_postproc = get_bits(gb, 5); //common
326
    v->s.loop_filter = get_bits1(gb); //common
327
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
328
    {
329
        av_log(avctx, AV_LOG_ERROR,
330
               "LOOPFILTER shall not be enabled in Simple Profile\n");
331
    }
332
    if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
333
        v->s.loop_filter = 0;
334

    
335
    v->res_x8 = get_bits1(gb); //reserved
336
    v->multires = get_bits1(gb);
337
    v->res_fasttx = get_bits1(gb);
338
    if (!v->res_fasttx)
339
    {
340
        v->s.dsp.vc1_inv_trans_8x8 = ff_simple_idct;
341
        v->s.dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
342
        v->s.dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
343
        v->s.dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
344
        v->s.dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add;
345
        v->s.dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
346
        v->s.dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
347
        v->s.dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
348
    }
349

    
350
    v->fastuvmc =  get_bits1(gb); //common
351
    if (!v->profile && !v->fastuvmc)
352
    {
353
        av_log(avctx, AV_LOG_ERROR,
354
               "FASTUVMC unavailable in Simple Profile\n");
355
        return -1;
356
    }
357
    v->extended_mv =  get_bits1(gb); //common
358
    if (!v->profile && v->extended_mv)
359
    {
360
        av_log(avctx, AV_LOG_ERROR,
361
               "Extended MVs unavailable in Simple Profile\n");
362
        return -1;
363
    }
364
    v->dquant =  get_bits(gb, 2); //common
365
    v->vstransform =  get_bits1(gb); //common
366

    
367
    v->res_transtab = get_bits1(gb);
368
    if (v->res_transtab)
369
    {
370
        av_log(avctx, AV_LOG_ERROR,
371
               "1 for reserved RES_TRANSTAB is forbidden\n");
372
        return -1;
373
    }
374

    
375
    v->overlap = get_bits1(gb); //common
376

    
377
    v->s.resync_marker = get_bits1(gb);
378
    v->rangered = get_bits1(gb);
379
    if (v->rangered && v->profile == PROFILE_SIMPLE)
380
    {
381
        av_log(avctx, AV_LOG_INFO,
382
               "RANGERED should be set to 0 in Simple Profile\n");
383
    }
384

    
385
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
386
    v->quantizer_mode = get_bits(gb, 2); //common
387

    
388
    v->finterpflag = get_bits1(gb); //common
389

    
390
    if (v->res_sprite) {
391
        v->s.avctx->width  = v->s.avctx->coded_width  = get_bits(gb, 11);
392
        v->s.avctx->height = v->s.avctx->coded_height = get_bits(gb, 11);
393
        skip_bits(gb, 5); //frame rate
394
        v->res_x8 = get_bits1(gb);
395
        if (get_bits1(gb)) { // something to do with DC VLC selection
396
            av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
397
            return -1;
398
        }
399
        skip_bits(gb, 3); //slice code
400
        v->res_rtm_flag = 0;
401
    } else {
402
        v->res_rtm_flag = get_bits1(gb); //reserved
403
    }
404
    if (!v->res_rtm_flag)
405
    {
406
//            av_log(avctx, AV_LOG_ERROR,
407
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
408
        av_log(avctx, AV_LOG_ERROR,
409
               "Old WMV3 version detected, some frames may be decoded incorrectly\n");
410
        //return -1;
411
    }
412
    //TODO: figure out what they mean (always 0x402F)
413
    if(!v->res_fasttx) skip_bits(gb, 16);
414
    av_log(avctx, AV_LOG_DEBUG,
415
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
416
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
417
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
418
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
419
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
420
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
421
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
422
               v->dquant, v->quantizer_mode, avctx->max_b_frames
423
               );
424
    return 0;
425
}
426

    
427
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
428
{
429
    v->res_rtm_flag = 1;
430
    v->level = get_bits(gb, 3);
431
    if(v->level >= 5)
432
    {
433
        av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
434
    }
435
    v->chromaformat = get_bits(gb, 2);
436
    if (v->chromaformat != 1)
437
    {
438
        av_log(v->s.avctx, AV_LOG_ERROR,
439
               "Only 4:2:0 chroma format supported\n");
440
        return -1;
441
    }
442

    
443
    // (fps-2)/4 (->30)
444
    v->frmrtq_postproc = get_bits(gb, 3); //common
445
    // (bitrate-32kbps)/64kbps
446
    v->bitrtq_postproc = get_bits(gb, 5); //common
447
    v->postprocflag = get_bits1(gb); //common
448

    
449
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
450
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
451
    v->s.avctx->width = v->s.avctx->coded_width;
452
    v->s.avctx->height = v->s.avctx->coded_height;
453
    v->broadcast = get_bits1(gb);
454
    v->interlace = get_bits1(gb);
455
    v->tfcntrflag = get_bits1(gb);
456
    v->finterpflag = get_bits1(gb);
457
    skip_bits1(gb); // reserved
458

    
459
    v->s.h_edge_pos = v->s.avctx->coded_width;
460
    v->s.v_edge_pos = v->s.avctx->coded_height;
461

    
462
    av_log(v->s.avctx, AV_LOG_DEBUG,
463
               "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
464
               "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
465
               "TFCTRflag=%i, FINTERPflag=%i\n",
466
               v->level, v->frmrtq_postproc, v->bitrtq_postproc,
467
               v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
468
               v->tfcntrflag, v->finterpflag
469
               );
470

    
471
    v->psf = get_bits1(gb);
472
    if(v->psf) { //PsF, 6.1.13
473
        av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
474
        return -1;
475
    }
476
    v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
477
    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
478
        int w, h, ar = 0;
479
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
480
        v->s.avctx->width  = w = get_bits(gb, 14) + 1;
481
        v->s.avctx->height = h = get_bits(gb, 14) + 1;
482
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
483
        if(get_bits1(gb))
484
            ar = get_bits(gb, 4);
485
        if(ar && ar < 14){
486
            v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
487
        }else if(ar == 15){
488
            w = get_bits(gb, 8);
489
            h = get_bits(gb, 8);
490
            v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
491
        }
492
        av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n", v->s.avctx->sample_aspect_ratio.num, v->s.avctx->sample_aspect_ratio.den);
493

    
494
        if(get_bits1(gb)){ //framerate stuff
495
            if(get_bits1(gb)) {
496
                v->s.avctx->time_base.num = 32;
497
                v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
498
            } else {
499
                int nr, dr;
500
                nr = get_bits(gb, 8);
501
                dr = get_bits(gb, 4);
502
                if(nr && nr < 8 && dr && dr < 3){
503
                    v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
504
                    v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
505
                }
506
            }
507
        }
508

    
509
        if(get_bits1(gb)){
510
            v->color_prim = get_bits(gb, 8);
511
            v->transfer_char = get_bits(gb, 8);
512
            v->matrix_coef = get_bits(gb, 8);
513
        }
514
    }
515

    
516
    v->hrd_param_flag = get_bits1(gb);
517
    if(v->hrd_param_flag) {
518
        int i;
519
        v->hrd_num_leaky_buckets = get_bits(gb, 5);
520
        skip_bits(gb, 4); //bitrate exponent
521
        skip_bits(gb, 4); //buffer size exponent
522
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
523
            skip_bits(gb, 16); //hrd_rate[n]
524
            skip_bits(gb, 16); //hrd_buffer[n]
525
        }
526
    }
527
    return 0;
528
}
529

    
530
int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
531
{
532
    int i;
533

    
534
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
535
    v->broken_link = get_bits1(gb);
536
    v->closed_entry = get_bits1(gb);
537
    v->panscanflag = get_bits1(gb);
538
    v->refdist_flag = get_bits1(gb);
539
    v->s.loop_filter = get_bits1(gb);
540
    v->fastuvmc = get_bits1(gb);
541
    v->extended_mv = get_bits1(gb);
542
    v->dquant = get_bits(gb, 2);
543
    v->vstransform = get_bits1(gb);
544
    v->overlap = get_bits1(gb);
545
    v->quantizer_mode = get_bits(gb, 2);
546

    
547
    if(v->hrd_param_flag){
548
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
549
            skip_bits(gb, 8); //hrd_full[n]
550
        }
551
    }
552

    
553
    if(get_bits1(gb)){
554
        avctx->coded_width = (get_bits(gb, 12)+1)<<1;
555
        avctx->coded_height = (get_bits(gb, 12)+1)<<1;
556
    }
557
    if(v->extended_mv)
558
        v->extended_dmv = get_bits1(gb);
559
    if((v->range_mapy_flag = get_bits1(gb))) {
560
        av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
561
        v->range_mapy = get_bits(gb, 3);
562
    }
563
    if((v->range_mapuv_flag = get_bits1(gb))) {
564
        av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
565
        v->range_mapuv = get_bits(gb, 3);
566
    }
567

    
568
    av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
569
        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
570
        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
571
        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
572
        v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
573
        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
574

    
575
    return 0;
576
}
577

    
578
int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
579
{
580
    int pqindex, lowquant, status;
581

    
582
    if(v->res_sprite) {
583
        skip_bits(gb, 2); //not yet deciphered
584
    }
585
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
586
    skip_bits(gb, 2); //framecnt unused
587
    v->rangeredfrm = 0;
588
    if (v->rangered) v->rangeredfrm = get_bits1(gb);
589
    v->s.pict_type = get_bits1(gb);
590
    if (v->s.avctx->max_b_frames) {
591
        if (!v->s.pict_type) {
592
            if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
593
            else v->s.pict_type = FF_B_TYPE;
594
        } else v->s.pict_type = FF_P_TYPE;
595
    } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
596

    
597
    v->bi_type = 0;
598
    if(v->s.pict_type == FF_B_TYPE) {
599
        v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
600
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
601
        if(v->bfraction == 0) {
602
            v->s.pict_type = FF_BI_TYPE;
603
        }
604
    }
605
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
606
        skip_bits(gb, 7); // skip buffer fullness
607

    
608
    if(v->parse_only)
609
        return 0;
610

    
611
    /* calculate RND */
612
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
613
        v->rnd = 1;
614
    if(v->s.pict_type == FF_P_TYPE)
615
        v->rnd ^= 1;
616

    
617
    /* Quantizer stuff */
618
    pqindex = get_bits(gb, 5);
619
    if(!pqindex) return -1;
620
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
621
        v->pq = ff_vc1_pquant_table[0][pqindex];
622
    else
623
        v->pq = ff_vc1_pquant_table[1][pqindex];
624

    
625
    v->pquantizer = 1;
626
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
627
        v->pquantizer = pqindex < 9;
628
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
629
        v->pquantizer = 0;
630
    v->pqindex = pqindex;
631
    if (pqindex < 9) v->halfpq = get_bits1(gb);
632
    else v->halfpq = 0;
633
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
634
        v->pquantizer = get_bits1(gb);
635
    v->dquantfrm = 0;
636
    if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
637
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
638
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
639
    v->range_x = 1 << (v->k_x - 1);
640
    v->range_y = 1 << (v->k_y - 1);
641
    if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
642

    
643
    if(v->res_x8 && (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)){
644
        v->x8_type = get_bits1(gb);
645
    }else v->x8_type = 0;
646
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
647
//        (v->s.pict_type == FF_P_TYPE) ? 'P' : ((v->s.pict_type == FF_I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
648

    
649
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
650

    
651
    switch(v->s.pict_type) {
652
    case FF_P_TYPE:
653
        if (v->pq < 5) v->tt_index = 0;
654
        else if(v->pq < 13) v->tt_index = 1;
655
        else v->tt_index = 2;
656

    
657
        lowquant = (v->pq > 12) ? 0 : 1;
658
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
659
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
660
        {
661
            int scale, shift, i;
662
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
663
            v->lumscale = get_bits(gb, 6);
664
            v->lumshift = get_bits(gb, 6);
665
            v->use_ic = 1;
666
            /* fill lookup tables for intensity compensation */
667
            if(!v->lumscale) {
668
                scale = -64;
669
                shift = (255 - v->lumshift * 2) << 6;
670
                if(v->lumshift > 31)
671
                    shift += 128 << 6;
672
            } else {
673
                scale = v->lumscale + 32;
674
                if(v->lumshift > 31)
675
                    shift = (v->lumshift - 64) << 6;
676
                else
677
                    shift = v->lumshift << 6;
678
            }
679
            for(i = 0; i < 256; i++) {
680
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
681
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
682
            }
683
        }
684
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
685
            v->s.quarter_sample = 0;
686
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
687
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
688
                v->s.quarter_sample = 0;
689
            else
690
                v->s.quarter_sample = 1;
691
        } else
692
            v->s.quarter_sample = 1;
693
        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));
694

    
695
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
696
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
697
                || v->mv_mode == MV_PMODE_MIXED_MV)
698
        {
699
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
700
            if (status < 0) return -1;
701
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
702
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
703
        } else {
704
            v->mv_type_is_raw = 0;
705
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
706
        }
707
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
708
        if (status < 0) return -1;
709
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
710
               "Imode: %i, Invert: %i\n", status>>1, status&1);
711

    
712
        /* Hopefully this is correct for P frames */
713
        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
714
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
715

    
716
        if (v->dquant)
717
        {
718
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
719
            vop_dquant_decoding(v);
720
        }
721

    
722
        v->ttfrm = 0; //FIXME Is that so ?
723
        if (v->vstransform)
724
        {
725
            v->ttmbf = get_bits1(gb);
726
            if (v->ttmbf)
727
            {
728
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
729
            }
730
        } else {
731
            v->ttmbf = 1;
732
            v->ttfrm = TT_8X8;
733
        }
734
        break;
735
    case FF_B_TYPE:
736
        if (v->pq < 5) v->tt_index = 0;
737
        else if(v->pq < 13) v->tt_index = 1;
738
        else v->tt_index = 2;
739

    
740
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
741
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
742
        v->s.mspel = v->s.quarter_sample;
743

    
744
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
745
        if (status < 0) return -1;
746
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
747
               "Imode: %i, Invert: %i\n", status>>1, status&1);
748
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
749
        if (status < 0) return -1;
750
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
751
               "Imode: %i, Invert: %i\n", status>>1, status&1);
752

    
753
        v->s.mv_table_index = get_bits(gb, 2);
754
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
755

    
756
        if (v->dquant)
757
        {
758
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
759
            vop_dquant_decoding(v);
760
        }
761

    
762
        v->ttfrm = 0;
763
        if (v->vstransform)
764
        {
765
            v->ttmbf = get_bits1(gb);
766
            if (v->ttmbf)
767
            {
768
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
769
            }
770
        } else {
771
            v->ttmbf = 1;
772
            v->ttfrm = TT_8X8;
773
        }
774
        break;
775
    }
776

    
777
    if(!v->x8_type)
778
    {
779
        /* AC Syntax */
780
        v->c_ac_table_index = decode012(gb);
781
        if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
782
        {
783
            v->y_ac_table_index = decode012(gb);
784
        }
785
        /* DC Syntax */
786
        v->s.dc_table_index = get_bits1(gb);
787
    }
788

    
789
    if(v->s.pict_type == FF_BI_TYPE) {
790
        v->s.pict_type = FF_B_TYPE;
791
        v->bi_type = 1;
792
    }
793
    return 0;
794
}
795

    
796
int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
797
{
798
    int pqindex, lowquant;
799
    int status;
800

    
801
    v->p_frame_skipped = 0;
802

    
803
    if(v->interlace){
804
        v->fcm = decode012(gb);
805
        if(v->fcm){
806
            if(!v->warn_interlaced++)
807
                av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
808
            return -1;
809
        }
810
    }
811
    switch(get_unary(gb, 0, 4)) {
812
    case 0:
813
        v->s.pict_type = FF_P_TYPE;
814
        break;
815
    case 1:
816
        v->s.pict_type = FF_B_TYPE;
817
        break;
818
    case 2:
819
        v->s.pict_type = FF_I_TYPE;
820
        break;
821
    case 3:
822
        v->s.pict_type = FF_BI_TYPE;
823
        break;
824
    case 4:
825
        v->s.pict_type = FF_P_TYPE; // skipped pic
826
        v->p_frame_skipped = 1;
827
        return 0;
828
    }
829
    if(v->tfcntrflag)
830
        skip_bits(gb, 8);
831
    if(v->broadcast) {
832
        if(!v->interlace || v->psf) {
833
            v->rptfrm = get_bits(gb, 2);
834
        } else {
835
            v->tff = get_bits1(gb);
836
            v->rptfrm = get_bits1(gb);
837
        }
838
    }
839
    if(v->panscanflag) {
840
        //...
841
    }
842
    v->rnd = get_bits1(gb);
843
    if(v->interlace)
844
        v->uvsamp = get_bits1(gb);
845
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
846
    if(v->s.pict_type == FF_B_TYPE) {
847
        v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
848
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
849
        if(v->bfraction == 0) {
850
            v->s.pict_type = FF_BI_TYPE; /* XXX: should not happen here */
851
        }
852
    }
853
    pqindex = get_bits(gb, 5);
854
    if(!pqindex) return -1;
855
    v->pqindex = pqindex;
856
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
857
        v->pq = ff_vc1_pquant_table[0][pqindex];
858
    else
859
        v->pq = ff_vc1_pquant_table[1][pqindex];
860

    
861
    v->pquantizer = 1;
862
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
863
        v->pquantizer = pqindex < 9;
864
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
865
        v->pquantizer = 0;
866
    v->pqindex = pqindex;
867
    if (pqindex < 9) v->halfpq = get_bits1(gb);
868
    else v->halfpq = 0;
869
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
870
        v->pquantizer = get_bits1(gb);
871
    if(v->postprocflag)
872
        v->postproc = get_bits(gb, 2);
873

    
874
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
875

    
876
    if(v->parse_only)
877
        return 0;
878

    
879
    switch(v->s.pict_type) {
880
    case FF_I_TYPE:
881
    case FF_BI_TYPE:
882
        status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
883
        if (status < 0) return -1;
884
        av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
885
                "Imode: %i, Invert: %i\n", status>>1, status&1);
886
        v->condover = CONDOVER_NONE;
887
        if(v->overlap && v->pq <= 8) {
888
            v->condover = decode012(gb);
889
            if(v->condover == CONDOVER_SELECT) {
890
                status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
891
                if (status < 0) return -1;
892
                av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
893
                        "Imode: %i, Invert: %i\n", status>>1, status&1);
894
            }
895
        }
896
        break;
897
    case FF_P_TYPE:
898
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
899
        else v->mvrange = 0;
900
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
901
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
902
        v->range_x = 1 << (v->k_x - 1);
903
        v->range_y = 1 << (v->k_y - 1);
904

    
905
        if (v->pq < 5) v->tt_index = 0;
906
        else if(v->pq < 13) v->tt_index = 1;
907
        else v->tt_index = 2;
908

    
909
        lowquant = (v->pq > 12) ? 0 : 1;
910
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
911
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
912
        {
913
            int scale, shift, i;
914
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
915
            v->lumscale = get_bits(gb, 6);
916
            v->lumshift = get_bits(gb, 6);
917
            /* fill lookup tables for intensity compensation */
918
            if(!v->lumscale) {
919
                scale = -64;
920
                shift = (255 - v->lumshift * 2) << 6;
921
                if(v->lumshift > 31)
922
                    shift += 128 << 6;
923
            } else {
924
                scale = v->lumscale + 32;
925
                if(v->lumshift > 31)
926
                    shift = (v->lumshift - 64) << 6;
927
                else
928
                    shift = v->lumshift << 6;
929
            }
930
            for(i = 0; i < 256; i++) {
931
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
932
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
933
            }
934
            v->use_ic = 1;
935
        }
936
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
937
            v->s.quarter_sample = 0;
938
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
939
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
940
                v->s.quarter_sample = 0;
941
            else
942
                v->s.quarter_sample = 1;
943
        } else
944
            v->s.quarter_sample = 1;
945
        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));
946

    
947
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
948
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
949
                || v->mv_mode == MV_PMODE_MIXED_MV)
950
        {
951
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
952
            if (status < 0) return -1;
953
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
954
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
955
        } else {
956
            v->mv_type_is_raw = 0;
957
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
958
        }
959
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
960
        if (status < 0) return -1;
961
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
962
               "Imode: %i, Invert: %i\n", status>>1, status&1);
963

    
964
        /* Hopefully this is correct for P frames */
965
        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
966
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
967
        if (v->dquant)
968
        {
969
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
970
            vop_dquant_decoding(v);
971
        }
972

    
973
        v->ttfrm = 0; //FIXME Is that so ?
974
        if (v->vstransform)
975
        {
976
            v->ttmbf = get_bits1(gb);
977
            if (v->ttmbf)
978
            {
979
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
980
            }
981
        } else {
982
            v->ttmbf = 1;
983
            v->ttfrm = TT_8X8;
984
        }
985
        break;
986
    case FF_B_TYPE:
987
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
988
        else v->mvrange = 0;
989
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
990
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
991
        v->range_x = 1 << (v->k_x - 1);
992
        v->range_y = 1 << (v->k_y - 1);
993

    
994
        if (v->pq < 5) v->tt_index = 0;
995
        else if(v->pq < 13) v->tt_index = 1;
996
        else v->tt_index = 2;
997

    
998
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
999
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1000
        v->s.mspel = v->s.quarter_sample;
1001

    
1002
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1003
        if (status < 0) return -1;
1004
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1005
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1006
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1007
        if (status < 0) return -1;
1008
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1009
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1010

    
1011
        v->s.mv_table_index = get_bits(gb, 2);
1012
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1013

    
1014
        if (v->dquant)
1015
        {
1016
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1017
            vop_dquant_decoding(v);
1018
        }
1019

    
1020
        v->ttfrm = 0;
1021
        if (v->vstransform)
1022
        {
1023
            v->ttmbf = get_bits1(gb);
1024
            if (v->ttmbf)
1025
            {
1026
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1027
            }
1028
        } else {
1029
            v->ttmbf = 1;
1030
            v->ttfrm = TT_8X8;
1031
        }
1032
        break;
1033
    }
1034

    
1035
    /* AC Syntax */
1036
    v->c_ac_table_index = decode012(gb);
1037
    if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1038
    {
1039
        v->y_ac_table_index = decode012(gb);
1040
    }
1041
    /* DC Syntax */
1042
    v->s.dc_table_index = get_bits1(gb);
1043
    if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
1044
        av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1045
        vop_dquant_decoding(v);
1046
    }
1047

    
1048
    v->bi_type = 0;
1049
    if(v->s.pict_type == FF_BI_TYPE) {
1050
        v->s.pict_type = FF_B_TYPE;
1051
        v->bi_type = 1;
1052
    }
1053
    return 0;
1054
}