Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1.c @ 41faa878

History | View | Annotate | Download (34.3 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 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
    }
341

    
342
    v->fastuvmc =  get_bits1(gb); //common
343
    if (!v->profile && !v->fastuvmc)
344
    {
345
        av_log(avctx, AV_LOG_ERROR,
346
               "FASTUVMC unavailable in Simple Profile\n");
347
        return -1;
348
    }
349
    v->extended_mv =  get_bits1(gb); //common
350
    if (!v->profile && v->extended_mv)
351
    {
352
        av_log(avctx, AV_LOG_ERROR,
353
               "Extended MVs unavailable in Simple Profile\n");
354
        return -1;
355
    }
356
    v->dquant =  get_bits(gb, 2); //common
357
    v->vstransform =  get_bits1(gb); //common
358

    
359
    v->res_transtab = get_bits1(gb);
360
    if (v->res_transtab)
361
    {
362
        av_log(avctx, AV_LOG_ERROR,
363
               "1 for reserved RES_TRANSTAB is forbidden\n");
364
        return -1;
365
    }
366

    
367
    v->overlap = get_bits1(gb); //common
368

    
369
    v->s.resync_marker = get_bits1(gb);
370
    v->rangered = get_bits1(gb);
371
    if (v->rangered && v->profile == PROFILE_SIMPLE)
372
    {
373
        av_log(avctx, AV_LOG_INFO,
374
               "RANGERED should be set to 0 in Simple Profile\n");
375
    }
376

    
377
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
378
    v->quantizer_mode = get_bits(gb, 2); //common
379

    
380
    v->finterpflag = get_bits1(gb); //common
381
    v->res_rtm_flag = get_bits1(gb); //reserved
382
    if (!v->res_rtm_flag)
383
    {
384
//            av_log(avctx, AV_LOG_ERROR,
385
//                   "0 for reserved RES_RTM_FLAG is forbidden\n");
386
        av_log(avctx, AV_LOG_ERROR,
387
               "Old WMV3 version detected, only I-frames will be decoded\n");
388
        //return -1;
389
    }
390
    //TODO: figure out what they mean (always 0x402F)
391
    if(!v->res_fasttx) skip_bits(gb, 16);
392
    av_log(avctx, AV_LOG_DEBUG,
393
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
394
               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
395
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
396
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
397
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
398
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
399
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
400
               v->dquant, v->quantizer_mode, avctx->max_b_frames
401
               );
402
    return 0;
403
}
404

    
405
static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
406
{
407
    v->res_rtm_flag = 1;
408
    v->level = get_bits(gb, 3);
409
    if(v->level >= 5)
410
    {
411
        av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
412
    }
413
    v->chromaformat = get_bits(gb, 2);
414
    if (v->chromaformat != 1)
415
    {
416
        av_log(v->s.avctx, AV_LOG_ERROR,
417
               "Only 4:2:0 chroma format supported\n");
418
        return -1;
419
    }
420

    
421
    // (fps-2)/4 (->30)
422
    v->frmrtq_postproc = get_bits(gb, 3); //common
423
    // (bitrate-32kbps)/64kbps
424
    v->bitrtq_postproc = get_bits(gb, 5); //common
425
    v->postprocflag = get_bits1(gb); //common
426

    
427
    v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
428
    v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
429
    v->s.avctx->width = v->s.avctx->coded_width;
430
    v->s.avctx->height = v->s.avctx->coded_height;
431
    v->broadcast = get_bits1(gb);
432
    v->interlace = get_bits1(gb);
433
    v->tfcntrflag = get_bits1(gb);
434
    v->finterpflag = get_bits1(gb);
435
    skip_bits1(gb); // reserved
436

    
437
    v->s.h_edge_pos = v->s.avctx->coded_width;
438
    v->s.v_edge_pos = v->s.avctx->coded_height;
439

    
440
    av_log(v->s.avctx, AV_LOG_DEBUG,
441
               "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
442
               "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
443
               "TFCTRflag=%i, FINTERPflag=%i\n",
444
               v->level, v->frmrtq_postproc, v->bitrtq_postproc,
445
               v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
446
               v->tfcntrflag, v->finterpflag
447
               );
448

    
449
    v->psf = get_bits1(gb);
450
    if(v->psf) { //PsF, 6.1.13
451
        av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
452
        return -1;
453
    }
454
    v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
455
    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
456
        int w, h, ar = 0;
457
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
458
        v->s.avctx->width  = w = get_bits(gb, 14) + 1;
459
        v->s.avctx->height = h = get_bits(gb, 14) + 1;
460
        av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
461
        if(get_bits1(gb))
462
            ar = get_bits(gb, 4);
463
        if(ar && ar < 14){
464
            v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
465
        }else if(ar == 15){
466
            w = get_bits(gb, 8);
467
            h = get_bits(gb, 8);
468
            v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
469
        }
470
        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);
471

    
472
        if(get_bits1(gb)){ //framerate stuff
473
            if(get_bits1(gb)) {
474
                v->s.avctx->time_base.num = 32;
475
                v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
476
            } else {
477
                int nr, dr;
478
                nr = get_bits(gb, 8);
479
                dr = get_bits(gb, 4);
480
                if(nr && nr < 8 && dr && dr < 3){
481
                    v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
482
                    v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
483
                }
484
            }
485
        }
486

    
487
        if(get_bits1(gb)){
488
            v->color_prim = get_bits(gb, 8);
489
            v->transfer_char = get_bits(gb, 8);
490
            v->matrix_coef = get_bits(gb, 8);
491
        }
492
    }
493

    
494
    v->hrd_param_flag = get_bits1(gb);
495
    if(v->hrd_param_flag) {
496
        int i;
497
        v->hrd_num_leaky_buckets = get_bits(gb, 5);
498
        skip_bits(gb, 4); //bitrate exponent
499
        skip_bits(gb, 4); //buffer size exponent
500
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
501
            skip_bits(gb, 16); //hrd_rate[n]
502
            skip_bits(gb, 16); //hrd_buffer[n]
503
        }
504
    }
505
    return 0;
506
}
507

    
508
int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
509
{
510
    int i;
511

    
512
    av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
513
    v->broken_link = get_bits1(gb);
514
    v->closed_entry = get_bits1(gb);
515
    v->panscanflag = get_bits1(gb);
516
    v->refdist_flag = get_bits1(gb);
517
    v->s.loop_filter = get_bits1(gb);
518
    v->fastuvmc = get_bits1(gb);
519
    v->extended_mv = get_bits1(gb);
520
    v->dquant = get_bits(gb, 2);
521
    v->vstransform = get_bits1(gb);
522
    v->overlap = get_bits1(gb);
523
    v->quantizer_mode = get_bits(gb, 2);
524

    
525
    if(v->hrd_param_flag){
526
        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
527
            skip_bits(gb, 8); //hrd_full[n]
528
        }
529
    }
530

    
531
    if(get_bits1(gb)){
532
        avctx->coded_width = (get_bits(gb, 12)+1)<<1;
533
        avctx->coded_height = (get_bits(gb, 12)+1)<<1;
534
    }
535
    if(v->extended_mv)
536
        v->extended_dmv = get_bits1(gb);
537
    if((v->range_mapy_flag = get_bits1(gb))) {
538
        av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
539
        v->range_mapy = get_bits(gb, 3);
540
    }
541
    if((v->range_mapuv_flag = get_bits1(gb))) {
542
        av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
543
        v->range_mapuv = get_bits(gb, 3);
544
    }
545

    
546
    av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
547
        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
548
        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
549
        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
550
        v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
551
        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
552

    
553
    return 0;
554
}
555

    
556
int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
557
{
558
    int pqindex, lowquant, status;
559

    
560
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
561
    skip_bits(gb, 2); //framecnt unused
562
    v->rangeredfrm = 0;
563
    if (v->rangered) v->rangeredfrm = get_bits1(gb);
564
    v->s.pict_type = get_bits1(gb);
565
    if (v->s.avctx->max_b_frames) {
566
        if (!v->s.pict_type) {
567
            if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
568
            else v->s.pict_type = FF_B_TYPE;
569
        } else v->s.pict_type = FF_P_TYPE;
570
    } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
571

    
572
    v->bi_type = 0;
573
    if(v->s.pict_type == FF_B_TYPE) {
574
        v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
575
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
576
        if(v->bfraction == 0) {
577
            v->s.pict_type = FF_BI_TYPE;
578
        }
579
    }
580
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
581
        skip_bits(gb, 7); // skip buffer fullness
582

    
583
    if(v->parse_only)
584
        return 0;
585

    
586
    /* calculate RND */
587
    if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
588
        v->rnd = 1;
589
    if(v->s.pict_type == FF_P_TYPE)
590
        v->rnd ^= 1;
591

    
592
    /* Quantizer stuff */
593
    pqindex = get_bits(gb, 5);
594
    if(!pqindex) return -1;
595
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
596
        v->pq = ff_vc1_pquant_table[0][pqindex];
597
    else
598
        v->pq = ff_vc1_pquant_table[1][pqindex];
599

    
600
    v->pquantizer = 1;
601
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
602
        v->pquantizer = pqindex < 9;
603
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
604
        v->pquantizer = 0;
605
    v->pqindex = pqindex;
606
    if (pqindex < 9) v->halfpq = get_bits1(gb);
607
    else v->halfpq = 0;
608
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
609
        v->pquantizer = get_bits1(gb);
610
    v->dquantfrm = 0;
611
    if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
612
    v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
613
    v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
614
    v->range_x = 1 << (v->k_x - 1);
615
    v->range_y = 1 << (v->k_y - 1);
616
    if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
617

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

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

    
626
    switch(v->s.pict_type) {
627
    case FF_P_TYPE:
628
        if (v->pq < 5) v->tt_index = 0;
629
        else if(v->pq < 13) v->tt_index = 1;
630
        else v->tt_index = 2;
631

    
632
        lowquant = (v->pq > 12) ? 0 : 1;
633
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
634
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
635
        {
636
            int scale, shift, i;
637
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
638
            v->lumscale = get_bits(gb, 6);
639
            v->lumshift = get_bits(gb, 6);
640
            v->use_ic = 1;
641
            /* fill lookup tables for intensity compensation */
642
            if(!v->lumscale) {
643
                scale = -64;
644
                shift = (255 - v->lumshift * 2) << 6;
645
                if(v->lumshift > 31)
646
                    shift += 128 << 6;
647
            } else {
648
                scale = v->lumscale + 32;
649
                if(v->lumshift > 31)
650
                    shift = (v->lumshift - 64) << 6;
651
                else
652
                    shift = v->lumshift << 6;
653
            }
654
            for(i = 0; i < 256; i++) {
655
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
656
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
657
            }
658
        }
659
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
660
            v->s.quarter_sample = 0;
661
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
662
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
663
                v->s.quarter_sample = 0;
664
            else
665
                v->s.quarter_sample = 1;
666
        } else
667
            v->s.quarter_sample = 1;
668
        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));
669

    
670
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
671
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
672
                || v->mv_mode == MV_PMODE_MIXED_MV)
673
        {
674
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
675
            if (status < 0) return -1;
676
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
677
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
678
        } else {
679
            v->mv_type_is_raw = 0;
680
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
681
        }
682
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
683
        if (status < 0) return -1;
684
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
685
               "Imode: %i, Invert: %i\n", status>>1, status&1);
686

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

    
691
        if (v->dquant)
692
        {
693
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
694
            vop_dquant_decoding(v);
695
        }
696

    
697
        v->ttfrm = 0; //FIXME Is that so ?
698
        if (v->vstransform)
699
        {
700
            v->ttmbf = get_bits1(gb);
701
            if (v->ttmbf)
702
            {
703
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
704
            }
705
        } else {
706
            v->ttmbf = 1;
707
            v->ttfrm = TT_8X8;
708
        }
709
        break;
710
    case FF_B_TYPE:
711
        if (v->pq < 5) v->tt_index = 0;
712
        else if(v->pq < 13) v->tt_index = 1;
713
        else v->tt_index = 2;
714

    
715
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
716
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
717
        v->s.mspel = v->s.quarter_sample;
718

    
719
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
720
        if (status < 0) return -1;
721
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
722
               "Imode: %i, Invert: %i\n", status>>1, status&1);
723
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
724
        if (status < 0) return -1;
725
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
726
               "Imode: %i, Invert: %i\n", status>>1, status&1);
727

    
728
        v->s.mv_table_index = get_bits(gb, 2);
729
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
730

    
731
        if (v->dquant)
732
        {
733
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
734
            vop_dquant_decoding(v);
735
        }
736

    
737
        v->ttfrm = 0;
738
        if (v->vstransform)
739
        {
740
            v->ttmbf = get_bits1(gb);
741
            if (v->ttmbf)
742
            {
743
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
744
            }
745
        } else {
746
            v->ttmbf = 1;
747
            v->ttfrm = TT_8X8;
748
        }
749
        break;
750
    }
751

    
752
    if(!v->x8_type)
753
    {
754
        /* AC Syntax */
755
        v->c_ac_table_index = decode012(gb);
756
        if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
757
        {
758
            v->y_ac_table_index = decode012(gb);
759
        }
760
        /* DC Syntax */
761
        v->s.dc_table_index = get_bits1(gb);
762
    }
763

    
764
    if(v->s.pict_type == FF_BI_TYPE) {
765
        v->s.pict_type = FF_B_TYPE;
766
        v->bi_type = 1;
767
    }
768
    return 0;
769
}
770

    
771
int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
772
{
773
    int pqindex, lowquant;
774
    int status;
775

    
776
    v->p_frame_skipped = 0;
777

    
778
    if(v->interlace){
779
        v->fcm = decode012(gb);
780
        if(v->fcm) return -1; // interlaced frames/fields are not implemented
781
    }
782
    switch(get_unary(gb, 0, 4)) {
783
    case 0:
784
        v->s.pict_type = FF_P_TYPE;
785
        break;
786
    case 1:
787
        v->s.pict_type = FF_B_TYPE;
788
        break;
789
    case 2:
790
        v->s.pict_type = FF_I_TYPE;
791
        break;
792
    case 3:
793
        v->s.pict_type = FF_BI_TYPE;
794
        break;
795
    case 4:
796
        v->s.pict_type = FF_P_TYPE; // skipped pic
797
        v->p_frame_skipped = 1;
798
        return 0;
799
    }
800
    if(v->tfcntrflag)
801
        skip_bits(gb, 8);
802
    if(v->broadcast) {
803
        if(!v->interlace || v->psf) {
804
            v->rptfrm = get_bits(gb, 2);
805
        } else {
806
            v->tff = get_bits1(gb);
807
            v->rptfrm = get_bits1(gb);
808
        }
809
    }
810
    if(v->panscanflag) {
811
        //...
812
    }
813
    v->rnd = get_bits1(gb);
814
    if(v->interlace)
815
        v->uvsamp = get_bits1(gb);
816
    if(v->finterpflag) v->interpfrm = get_bits1(gb);
817
    if(v->s.pict_type == FF_B_TYPE) {
818
        v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
819
        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
820
        if(v->bfraction == 0) {
821
            v->s.pict_type = FF_BI_TYPE; /* XXX: should not happen here */
822
        }
823
    }
824
    pqindex = get_bits(gb, 5);
825
    if(!pqindex) return -1;
826
    v->pqindex = pqindex;
827
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
828
        v->pq = ff_vc1_pquant_table[0][pqindex];
829
    else
830
        v->pq = ff_vc1_pquant_table[1][pqindex];
831

    
832
    v->pquantizer = 1;
833
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
834
        v->pquantizer = pqindex < 9;
835
    if (v->quantizer_mode == QUANT_NON_UNIFORM)
836
        v->pquantizer = 0;
837
    v->pqindex = pqindex;
838
    if (pqindex < 9) v->halfpq = get_bits1(gb);
839
    else v->halfpq = 0;
840
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
841
        v->pquantizer = get_bits1(gb);
842
    if(v->postprocflag)
843
        v->postproc = get_bits(gb, 2);
844

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

    
847
    if(v->parse_only)
848
        return 0;
849

    
850
    switch(v->s.pict_type) {
851
    case FF_I_TYPE:
852
    case FF_BI_TYPE:
853
        status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
854
        if (status < 0) return -1;
855
        av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
856
                "Imode: %i, Invert: %i\n", status>>1, status&1);
857
        v->condover = CONDOVER_NONE;
858
        if(v->overlap && v->pq <= 8) {
859
            v->condover = decode012(gb);
860
            if(v->condover == CONDOVER_SELECT) {
861
                status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
862
                if (status < 0) return -1;
863
                av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
864
                        "Imode: %i, Invert: %i\n", status>>1, status&1);
865
            }
866
        }
867
        break;
868
    case FF_P_TYPE:
869
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
870
        else v->mvrange = 0;
871
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
872
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
873
        v->range_x = 1 << (v->k_x - 1);
874
        v->range_y = 1 << (v->k_y - 1);
875

    
876
        if (v->pq < 5) v->tt_index = 0;
877
        else if(v->pq < 13) v->tt_index = 1;
878
        else v->tt_index = 2;
879

    
880
        lowquant = (v->pq > 12) ? 0 : 1;
881
        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
882
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
883
        {
884
            int scale, shift, i;
885
            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
886
            v->lumscale = get_bits(gb, 6);
887
            v->lumshift = get_bits(gb, 6);
888
            /* fill lookup tables for intensity compensation */
889
            if(!v->lumscale) {
890
                scale = -64;
891
                shift = (255 - v->lumshift * 2) << 6;
892
                if(v->lumshift > 31)
893
                    shift += 128 << 6;
894
            } else {
895
                scale = v->lumscale + 32;
896
                if(v->lumshift > 31)
897
                    shift = (v->lumshift - 64) << 6;
898
                else
899
                    shift = v->lumshift << 6;
900
            }
901
            for(i = 0; i < 256; i++) {
902
                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
903
                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
904
            }
905
            v->use_ic = 1;
906
        }
907
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
908
            v->s.quarter_sample = 0;
909
        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
910
            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
911
                v->s.quarter_sample = 0;
912
            else
913
                v->s.quarter_sample = 1;
914
        } else
915
            v->s.quarter_sample = 1;
916
        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));
917

    
918
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
919
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
920
                || v->mv_mode == MV_PMODE_MIXED_MV)
921
        {
922
            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
923
            if (status < 0) return -1;
924
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
925
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
926
        } else {
927
            v->mv_type_is_raw = 0;
928
            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
929
        }
930
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
931
        if (status < 0) return -1;
932
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
933
               "Imode: %i, Invert: %i\n", status>>1, status&1);
934

    
935
        /* Hopefully this is correct for P frames */
936
        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
937
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
938
        if (v->dquant)
939
        {
940
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
941
            vop_dquant_decoding(v);
942
        }
943

    
944
        v->ttfrm = 0; //FIXME Is that so ?
945
        if (v->vstransform)
946
        {
947
            v->ttmbf = get_bits1(gb);
948
            if (v->ttmbf)
949
            {
950
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
951
            }
952
        } else {
953
            v->ttmbf = 1;
954
            v->ttfrm = TT_8X8;
955
        }
956
        break;
957
    case FF_B_TYPE:
958
        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
959
        else v->mvrange = 0;
960
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
961
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
962
        v->range_x = 1 << (v->k_x - 1);
963
        v->range_y = 1 << (v->k_y - 1);
964

    
965
        if (v->pq < 5) v->tt_index = 0;
966
        else if(v->pq < 13) v->tt_index = 1;
967
        else v->tt_index = 2;
968

    
969
        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
970
        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
971
        v->s.mspel = v->s.quarter_sample;
972

    
973
        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
974
        if (status < 0) return -1;
975
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
976
               "Imode: %i, Invert: %i\n", status>>1, status&1);
977
        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
978
        if (status < 0) return -1;
979
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
980
               "Imode: %i, Invert: %i\n", status>>1, status&1);
981

    
982
        v->s.mv_table_index = get_bits(gb, 2);
983
        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
984

    
985
        if (v->dquant)
986
        {
987
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
988
            vop_dquant_decoding(v);
989
        }
990

    
991
        v->ttfrm = 0;
992
        if (v->vstransform)
993
        {
994
            v->ttmbf = get_bits1(gb);
995
            if (v->ttmbf)
996
            {
997
                v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
998
            }
999
        } else {
1000
            v->ttmbf = 1;
1001
            v->ttfrm = TT_8X8;
1002
        }
1003
        break;
1004
    }
1005

    
1006
    /* AC Syntax */
1007
    v->c_ac_table_index = decode012(gb);
1008
    if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
1009
    {
1010
        v->y_ac_table_index = decode012(gb);
1011
    }
1012
    /* DC Syntax */
1013
    v->s.dc_table_index = get_bits1(gb);
1014
    if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
1015
        av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1016
        vop_dquant_decoding(v);
1017
    }
1018

    
1019
    v->bi_type = 0;
1020
    if(v->s.pict_type == FF_BI_TYPE) {
1021
        v->s.pict_type = FF_B_TYPE;
1022
        v->bi_type = 1;
1023
    }
1024
    return 0;
1025
}