Revision 10b9c374

View differences:

libavcodec/Makefile
194 194
ifeq ($(CONFIG_ULTI_DECODER),yes)
195 195
    OBJS+= ulti.o
196 196
endif
197
ifneq ($(CONFIG_VC9_DECODER)$(CONFIG_WMV3_DECODER),)
198
    OBJS+= vc9.o
197
ifneq ($(CONFIG_VC1_DECODER)$(CONFIG_WMV3_DECODER),)
198
    OBJS+= vc1.o
199 199
endif
200 200
ifneq ($(CONFIG_VCR1_DECODER)$(CONFIG_VCR1_ENCODER),)
201 201
    OBJS+= vcr1.o
libavcodec/allcodecs.c
226 226
#ifdef CONFIG_WMV2_DECODER
227 227
    register_avcodec(&wmv2_decoder);
228 228
#endif //CONFIG_WMV2_DECODER
229
#ifdef CONFIG_VC9_DECODER
230
    register_avcodec(&vc9_decoder);
231
#endif //CONFIG_VC9_DECODER
229
#ifdef CONFIG_VC1_DECODER
230
    register_avcodec(&vc1_decoder);
231
#endif //CONFIG_VC1_DECODER
232 232
/* Reenable when it stops crashing on every file, causing bug report spam.
233 233
#ifdef CONFIG_WMV3_DECODER
234 234
    register_avcodec(&wmv3_decoder);
libavcodec/avcodec.h
101 101
    CODEC_ID_FFVHUFF,
102 102
    CODEC_ID_RV30,
103 103
    CODEC_ID_RV40,
104
    CODEC_ID_VC9,
104
    CODEC_ID_VC1,
105 105
    CODEC_ID_WMV3,
106 106
    CODEC_ID_LOCO,
107 107
    CODEC_ID_WNV1,
......
2112 2112
extern AVCodec msmpeg4v3_decoder;
2113 2113
extern AVCodec wmv1_decoder;
2114 2114
extern AVCodec wmv2_decoder;
2115
extern AVCodec vc9_decoder;
2115
extern AVCodec vc1_decoder;
2116 2116
extern AVCodec wmv3_decoder;
2117 2117
extern AVCodec mpeg1video_decoder;
2118 2118
extern AVCodec mpeg2video_decoder;
libavcodec/msmpeg4.c
75 75
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
76 76
static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
77 77

  
78
/* vc9 externs */
78
/* vc1 externs */
79 79
extern uint8_t wmv3_dc_scale_table[32];
80 80

  
81 81
#ifdef DEBUG
......
177 177
        s->y_dc_scale_table= wmv1_y_dc_scale_table;
178 178
        s->c_dc_scale_table= wmv1_c_dc_scale_table;
179 179
        break;
180
#if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC9_DECODER)
180
#if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC1_DECODER)
181 181
    case 6:
182 182
        s->y_dc_scale_table= wmv3_dc_scale_table;
183 183
        s->c_dc_scale_table= wmv3_dc_scale_table;
......
1197 1197
    case 5:
1198 1198
        s->decode_mb= wmv2_decode_mb;
1199 1199
    case 6:
1200
        //FIXME + TODO VC9 decode mb
1200
        //FIXME + TODO VC1 decode mb
1201 1201
        break;
1202 1202
    }
1203 1203

  
libavcodec/vc1.c
1
/*
2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2005 Anonymous
4
 * Copyright (c) 2005 Alex Beregszaszi
5
 * Copyright (c) 2005 Michael Niedermayer
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 *
21
 */
22

  
23
/**
24
 * @file vc1.c
25
 * VC-1 and WMV3 decoder
26
 *
27
 * TODO: most AP stuff, optimize, most of MB layer, transform, filtering and motion compensation, etc
28
 * TODO: use MPV_ !!
29
 */
30
#include "common.h"
31
#include "dsputil.h"
32
#include "avcodec.h"
33
#include "mpegvideo.h"
34
#include "vc1data.h"
35

  
36
#undef NDEBUG
37
#include <assert.h>
38

  
39
extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
40
extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
41
extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
42
#define MB_INTRA_VLC_BITS 9
43
extern VLC ff_msmp4_mb_i_vlc;
44
#define DC_VLC_BITS 9
45
static const uint16_t table_mb_intra[64][2];
46

  
47
/* Some inhibiting stuff */
48
#define HAS_ADVANCED_PROFILE   0
49
#define TRACE                  1
50

  
51
#if TRACE
52
#  define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
53
                   codes, codes_wrap, codes_size, use_static)          \
54
  if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,     \
55
               codes, codes_wrap, codes_size, use_static) < 0)         \
56
  {                                                                    \
57
    av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i);   \
58
    return -1;                                                         \
59
  }
60
#else
61
#  define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
62
                   codes, codes_wrap, codes_size, use_static)          \
63
  init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,         \
64
           codes, codes_wrap, codes_size, use_static)
65
#endif
66

  
67
/** Available Profiles */
68
//@{
69
#define PROFILE_SIMPLE   0
70
#define PROFILE_MAIN     1
71
#define PROFILE_COMPLEX  2 ///< TODO: WMV9 specific
72
#define PROFILE_ADVANCED 3
73
//@}
74

  
75
/** Sequence quantizer mode */
76
//@{
77
#define QUANT_FRAME_IMPLICIT   0 ///< Implicitly specified at frame level
78
#define QUANT_FRAME_EXPLICIT   1 ///< Explicitly specified at frame level
79
#define QUANT_NON_UNIFORM      2 ///< Non-uniform quant used for all frames
80
#define QUANT_UNIFORM          3 ///< Uniform quant used for all frames
81
//@}
82

  
83
/** Where quant can be changed */
84
//@{
85
#define DQPROFILE_FOUR_EDGES   0
86
#define DQPROFILE_DOUBLE_EDGES 1
87
#define DQPROFILE_SINGLE_EDGE  2
88
#define DQPROFILE_ALL_MBS      3
89
//@}
90

  
91
/** @name Where quant can be changed
92
 */
93
//@{
94
#define DQPROFILE_FOUR_EDGES   0
95
#define DQSINGLE_BEDGE_LEFT   0
96
#define DQSINGLE_BEDGE_TOP    1
97
#define DQSINGLE_BEDGE_RIGHT  2
98
#define DQSINGLE_BEDGE_BOTTOM 3
99
//@}
100

  
101
/** Which pair of edges is quantized with ALTPQUANT */
102
//@{
103
#define DQDOUBLE_BEDGE_TOPLEFT     0
104
#define DQDOUBLE_BEDGE_TOPRIGHT    1
105
#define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
106
#define DQDOUBLE_BEDGE_BOTTOMLEFT  3
107
//@}
108

  
109
/** MV modes for P frames */
110
//@{
111
#define MV_PMODE_1MV_HPEL_BILIN   0
112
#define MV_PMODE_1MV              1
113
#define MV_PMODE_1MV_HPEL         2
114
#define MV_PMODE_MIXED_MV         3
115
#define MV_PMODE_INTENSITY_COMP   4
116
//@}
117

  
118
/** @name MV types for B frames */
119
//@{
120
#define BMV_TYPE_BACKWARD          0
121
#define BMV_TYPE_BACKWARD          0
122
#define BMV_TYPE_FORWARD           1
123
#define BMV_TYPE_INTERPOLATED      3
124
//@}
125

  
126
/** MV P mode - the 5th element is only used for mode 1 */
127
static const uint8_t mv_pmode_table[2][5] = {
128
  { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
129
  { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
130
};
131

  
132
/** One more frame type */
133
#define BI_TYPE 7
134

  
135
static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
136
  fps_dr[2] = { 1000, 1001 };
137
static const uint8_t pquant_table[3][32] = {
138
  {  /* Implicit quantizer */
139
     0,  1,  2,  3,  4,  5,  6,  7,  8,  6,  7,  8,  9, 10, 11, 12,
140
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
141
  },
142
  {  /* Explicit quantizer, pquantizer uniform */
143
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
144
    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
145
  },
146
  {  /* Explicit quantizer, pquantizer non-uniform */
147
     0,  1,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
148
    14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
149
  }
150
};
151

  
152
/** @name VC-1 VLC tables and defines
153
 *  @todo TODO move this into the context
154
 */
155
//@{
156
#define VC1_BFRACTION_VLC_BITS 7
157
static VLC vc1_bfraction_vlc;
158
#define VC1_IMODE_VLC_BITS 4
159
static VLC vc1_imode_vlc;
160
#define VC1_NORM2_VLC_BITS 3
161
static VLC vc1_norm2_vlc;
162
#define VC1_NORM6_VLC_BITS 9
163
static VLC vc1_norm6_vlc;
164
/* Could be optimized, one table only needs 8 bits */
165
#define VC1_TTMB_VLC_BITS 9 //12
166
static VLC vc1_ttmb_vlc[3];
167
#define VC1_MV_DIFF_VLC_BITS 9 //15
168
static VLC vc1_mv_diff_vlc[4];
169
#define VC1_CBPCY_P_VLC_BITS 9 //14
170
static VLC vc1_cbpcy_p_vlc[4];
171
#define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
172
static VLC vc1_4mv_block_pattern_vlc[4];
173
#define VC1_TTBLK_VLC_BITS 5
174
static VLC vc1_ttblk_vlc[3];
175
#define VC1_SUBBLKPAT_VLC_BITS 6
176
static VLC vc1_subblkpat_vlc[3];
177
//@}
178

  
179
/** Bitplane struct
180
 * We mainly need data and is_raw, so this struct could be avoided
181
 * to save a level of indirection; feel free to modify
182
 * @fixme For now, stride=width
183
 * @warning Data are bits, either 1 or 0
184
 */
185
typedef struct BitPlane {
186
    uint8_t *data;      ///< Data buffer
187
    int width;          ///< Width of the buffer
188
    int stride;         ///< Stride of the buffer
189
    int height;         ///< Plane height
190
    uint8_t is_raw;     ///< Bit values must be read at MB level
191
} BitPlane;
192

  
193
/** The VC1 Context
194
 * @fixme Change size wherever another size is more efficient
195
 * Many members are only used for Advanced Profile
196
 */
197
typedef struct VC1Context{
198
    MpegEncContext s;
199

  
200
    /** Simple/Main Profile sequence header */
201
    //@{
202
    int res_sm;           ///< reserved, 2b
203
    int res_x8;           ///< reserved
204
    int multires;         ///< frame-level RESPIC syntax element present
205
    int res_fasttx;       ///< reserved, always 1
206
    int res_transtab;     ///< reserved, always 0
207
    int rangered;         ///< RANGEREDFRM (range reduction) syntax element present
208
                          ///< at frame level
209
    int res_rtm_flag;     ///< reserved, set to 1
210
    int reserved;         ///< reserved
211
    //@}
212

  
213
#if HAS_ADVANCED_PROFILE
214
    /** Advanced Profile */
215
    //@{
216
    int level;            ///< 3bits, for Advanced/Simple Profile, provided by TS layer
217
    int chromaformat;     ///< 2bits, 2=4:2:0, only defined
218
    int postprocflag;     ///< Per-frame processing suggestion flag present
219
    int broadcast;        ///< TFF/RFF present
220
    int interlace;        ///< Progressive/interlaced (RPTFTM syntax element)
221
    int tfcntrflag;       ///< TFCNTR present
222
    int panscanflag;      ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
223
    int extended_dmv;     ///< Additional extended dmv range at P/B frame-level
224
    int color_prim;       ///< 8bits, chroma coordinates of the color primaries
225
    int transfer_char;    ///< 8bits, Opto-electronic transfer characteristics
226
    int matrix_coef;      ///< 8bits, Color primaries->YCbCr transform matrix
227
    int hrd_param_flag;   ///< Presence of Hypothetical Reference
228
                          ///< Decoder parameters
229
    //@}
230
#endif
231

  
232

  
233
    /** Sequence header data for all Profiles
234
     * TODO: choose between ints, uint8_ts and monobit flags
235
     */
236
    //@{
237
    int profile;          ///< 2bits, Profile
238
    int frmrtq_postproc;  ///< 3bits,
239
    int bitrtq_postproc;  ///< 5bits, quantized framerate-based postprocessing strength
240
    int fastuvmc;         ///< Rounding of qpel vector to hpel ? (not in Simple)
241
    int extended_mv;      ///< Ext MV in P/B (not in Simple)
242
    int dquant;           ///< How qscale varies with MBs, 2bits (not in Simple)
243
    int vstransform;      ///< variable-size [48]x[48] transform type + info
244
    int overlap;          ///< overlapped transforms in use
245
    int quantizer_mode;   ///< 2bits, quantizer mode used for sequence, see QUANT_*
246
    int finterpflag;      ///< INTERPFRM present
247
    //@}
248

  
249
    /** Frame decoding info for all profiles */
250
    //@{
251
    uint8_t mv_mode;      ///< MV coding monde
252
    uint8_t mv_mode2;     ///< Secondary MV coding mode (B frames)
253
    int k_x;              ///< Number of bits for MVs (depends on MV range)
254
    int k_y;              ///< Number of bits for MVs (depends on MV range)
255
    uint8_t pq, altpq;    ///< Current/alternate frame quantizer scale
256
    /** pquant parameters */
257
    //@{
258
    uint8_t dquantfrm;
259
    uint8_t dqprofile;
260
    uint8_t dqsbedge;
261
    uint8_t dqbilevel;
262
    //@}
263
    /** AC coding set indexes
264
     * @see 8.1.1.10, p(1)10
265
     */
266
    //@{
267
    int c_ac_table_index; ///< Chroma index from ACFRM element
268
    int y_ac_table_index; ///< Luma index from AC2FRM element
269
    //@}
270
    int ttfrm;            ///< Transform type info present at frame level
271
    uint8_t ttmbf;        ///< Transform type flag
272
    int ttmb;             ///< Transform type
273
    uint8_t ttblk4x4;     ///< Value of ttblk which indicates a 4x4 transform
274
    /** Luma compensation parameters */
275
    //@{
276
    uint8_t lumscale;
277
    uint8_t lumshift;
278
    //@}
279
    int16_t bfraction;    ///< Relative position % anchors=> how to scale MVs
280
    uint8_t halfpq;       ///< Uniform quant over image and qp+.5
281
    uint8_t respic;       ///< Frame-level flag for resized images
282
    int buffer_fullness;  ///< HRD info
283
    /** Ranges:
284
     * -# 0 -> [-64n 63.f] x [-32, 31.f]
285
     * -# 1 -> [-128, 127.f] x [-64, 63.f]
286
     * -# 2 -> [-512, 511.f] x [-128, 127.f]
287
     * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
288
     */
289
    uint8_t mvrange;
290
    uint8_t pquantizer;           ///< Uniform (over sequence) quantizer in use
291
    uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
292
    VLC *cbpcy_vlc;               ///< CBPCY VLC table
293
    int tt_index;                 ///< Index for Transform Type tables
294
    BitPlane mv_type_mb_plane;    ///< bitplane for mv_type == (4MV)
295
    BitPlane skip_mb_plane;       ///< bitplane for skipped MBs
296
    BitPlane direct_mb_plane;     ///< bitplane for "direct" MBs
297

  
298
    /** Frame decoding info for S/M profiles only */
299
    //@{
300
    uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
301
    uint8_t interpfrm;
302
    //@}
303

  
304
#if HAS_ADVANCED_PROFILE
305
    /** Frame decoding info for Advanced profile */
306
    //@{
307
    uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
308
    uint8_t numpanscanwin;
309
    uint8_t tfcntr;
310
    uint8_t rptfrm, tff, rff;
311
    uint16_t topleftx;
312
    uint16_t toplefty;
313
    uint16_t bottomrightx;
314
    uint16_t bottomrighty;
315
    uint8_t uvsamp;
316
    uint8_t postproc;
317
    int hrd_num_leaky_buckets;
318
    uint8_t bit_rate_exponent;
319
    uint8_t buffer_size_exponent;
320
    BitPlane ac_pred_plane;       ///< AC prediction flags bitplane
321
    BitPlane over_flags_plane;    ///< Overflags bitplane
322
    uint8_t condover;
323
    uint16_t *hrd_rate, *hrd_buffer;
324
    uint8_t *hrd_fullness;
325
    uint8_t range_mapy_flag;
326
    uint8_t range_mapuv_flag;
327
    uint8_t range_mapy;
328
    uint8_t range_mapuv;
329
    //@}
330
#endif
331
} VC1Context;
332

  
333
/**
334
 * Get unary code of limited length
335
 * @fixme FIXME Slow and ugly
336
 * @param gb GetBitContext
337
 * @param[in] stop The bitstop value (unary code of 1's or 0's)
338
 * @param[in] len Maximum length
339
 * @return Unary length/index
340
 */
341
static int get_prefix(GetBitContext *gb, int stop, int len)
342
{
343
#if 1
344
  int i = 0, tmp = !stop;
345

  
346
  while (i != len && tmp != stop)
347
  {
348
    tmp = get_bits(gb, 1);
349
    i++;
350
  }
351
  if (i == len && tmp != stop) return len+1;
352
  return i;
353
#else
354
  unsigned int buf;
355
  int log;
356

  
357
  OPEN_READER(re, gb);
358
  UPDATE_CACHE(re, gb);
359
  buf=GET_CACHE(re, gb); //Still not sure
360
  if (stop) buf = ~buf;
361

  
362
  log= av_log2(-buf); //FIXME: -?
363
  if (log < limit){
364
    LAST_SKIP_BITS(re, gb, log+1);
365
    CLOSE_READER(re, gb);
366
    return log;
367
  }
368

  
369
  LAST_SKIP_BITS(re, gb, limit);
370
  CLOSE_READER(re, gb);
371
  return limit;
372
#endif
373
}
374

  
375
/**
376
 * Init VC-1 specific tables and VC1Context members
377
 * @param v The VC1Context to initialize
378
 * @return Status
379
 */
380
static int vc1_init_common(VC1Context *v)
381
{
382
    static int done = 0;
383
    int i = 0;
384

  
385
    /* Set the bit planes */
386
    v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
387
    v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
388
    v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
389
#if HAS_ADVANCED_PROFILE
390
    v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
391
    v->hrd_rate = v->hrd_buffer = NULL;
392
#endif
393

  
394
    /* VLC tables */
395
#if 0 // spec -> actual tables converter
396
    for(i=0; i<64; i++){
397
        int code= (vc1_norm6_spec[i][1] << vc1_norm6_spec[i][4]) + vc1_norm6_spec[i][3];
398
        av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
399
        if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
400
    }
401
    for(i=0; i<64; i++){
402
        int code= vc1_norm6_spec[i][2] + vc1_norm6_spec[i][4];
403
        av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
404
        if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
405
    }
406
#endif
407
    if(!done)
408
    {
409
        done = 1;
410
        INIT_VLC(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
411
                 vc1_bfraction_bits, 1, 1,
412
                 vc1_bfraction_codes, 1, 1, 1);
413
        INIT_VLC(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
414
                 vc1_norm2_bits, 1, 1,
415
                 vc1_norm2_codes, 1, 1, 1);
416
        INIT_VLC(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
417
                 vc1_norm6_bits, 1, 1,
418
                 vc1_norm6_codes, 2, 2, 1);
419
        INIT_VLC(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
420
                 vc1_imode_bits, 1, 1,
421
                 vc1_imode_codes, 1, 1, 1);
422
        for (i=0; i<3; i++)
423
        {
424
            INIT_VLC(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
425
                     vc1_ttmb_bits[i], 1, 1,
426
                     vc1_ttmb_codes[i], 2, 2, 1);
427
            INIT_VLC(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
428
                     vc1_ttblk_bits[i], 1, 1,
429
                     vc1_ttblk_codes[i], 1, 1, 1);
430
            INIT_VLC(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
431
                     vc1_subblkpat_bits[i], 1, 1,
432
                     vc1_subblkpat_codes[i], 1, 1, 1);
433
        }
434
        for(i=0; i<4; i++)
435
        {
436
            INIT_VLC(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
437
                     vc1_4mv_block_pattern_bits[i], 1, 1,
438
                     vc1_4mv_block_pattern_codes[i], 1, 1, 1);
439
            INIT_VLC(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
440
                     vc1_cbpcy_p_bits[i], 1, 1,
441
                     vc1_cbpcy_p_codes[i], 2, 2, 1);
442
            INIT_VLC(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
443
                     vc1_mv_diff_bits[i], 1, 1,
444
                     vc1_mv_diff_codes[i], 2, 2, 1);
445
        }
446
    }
447

  
448
    /* Other defaults */
449
    v->pq = -1;
450
    v->mvrange = 0; /* 7.1.1.18, p80 */
451

  
452
    return 0;
453
}
454

  
455
#if HAS_ADVANCED_PROFILE
456
/**
457
 * Decode sequence header's Hypothetic Reference Decoder data
458
 * @see 6.2.1, p32
459
 * @param v The VC1Context to initialize
460
 * @param gb A GetBitContext initialized from AVCodecContext extra_data
461
 * @return Status
462
 */
463
static int decode_hrd(VC1Context *v, GetBitContext *gb)
464
{
465
    int i, num;
466

  
467
    num = 1 + get_bits(gb, 5);
468

  
469
    /*hrd rate*/
470
    if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
471
    {
472
        av_freep(&v->hrd_rate);
473
    }
474
    if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
475
    if (!v->hrd_rate) return -1;
476

  
477
    /*hrd buffer*/
478
    if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
479
    {
480
        av_freep(&v->hrd_buffer);
481
    }
482
    if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
483
    if (!v->hrd_buffer)
484
    {
485
        av_freep(&v->hrd_rate);
486
        return -1;
487
    }
488

  
489
    /*hrd fullness*/
490
    if (v->hrd_fullness || num != v->hrd_num_leaky_buckets)
491
    {
492
        av_freep(&v->hrd_buffer);
493
    }
494
    if (!v->hrd_fullness) v->hrd_fullness = av_malloc(num*sizeof(uint8_t));
495
    if (!v->hrd_fullness)
496
    {
497
        av_freep(&v->hrd_rate);
498
        av_freep(&v->hrd_buffer);
499
        return -1;
500
    }
501
    v->hrd_num_leaky_buckets = num;
502

  
503
    //exponent in base-2 for rate
504
    v->bit_rate_exponent = 6 + get_bits(gb, 4);
505
    //exponent in base-2 for buffer_size
506
    v->buffer_size_exponent = 4 + get_bits(gb, 4);
507

  
508
    for (i=0; i<num; i++)
509
    {
510
        //mantissae, ordered (if not, use a function ?
511
        v->hrd_rate[i] = 1 + get_bits(gb, 16);
512
        if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
513
        {
514
            av_log(v->s.avctx, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
515
                   "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
516
            return -1;
517
        }
518
        v->hrd_buffer[i] = 1 + get_bits(gb, 16);
519
        if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
520
        {
521
            av_log(v->s.avctx, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
522
                   "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
523
            return -1;
524
        }
525
    }
526
    return 0;
527
}
528

  
529
/**
530
 * Decode sequence header for Advanced Profile
531
 * @see Table 2, p18
532
 * @see 6.1.7, pp21-27
533
 * @param v The VC1Context to initialize
534
 * @param gb A GetBitContext initialized from AVCodecContext extra_data
535
 * @return Status
536
 */
537
static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
538
{
539
    VC1Context *v = avctx->priv_data;
540
    int nr, dr, aspect_ratio;
541

  
542
    v->postprocflag = get_bits(gb, 1);
543
    v->broadcast = get_bits(gb, 1);
544
    v->interlace = get_bits(gb, 1);
545

  
546
    v->tfcntrflag = get_bits(gb, 1);
547
    v->finterpflag = get_bits(gb, 1); //common
548
    v->panscanflag = get_bits(gb, 1);
549
    v->reserved = get_bits(gb, 1);
550
    if (v->reserved)
551
    {
552
        av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
553
               v->reserved);
554
        return -1;
555
    }
556
    if (v->extended_mv)
557
        v->extended_dmv = get_bits(gb, 1);
558

  
559
    /* 6.1.7, p21 */
560
    if (get_bits(gb, 1) /* pic_size_flag */)
561
    {
562
        avctx->coded_width = get_bits(gb, 12) << 1;
563
        avctx->coded_height = get_bits(gb, 12) << 1;
564
        if ( get_bits(gb, 1) /* disp_size_flag */)
565
        {
566
            avctx->width = get_bits(gb, 14);
567
            avctx->height = get_bits(gb, 14);
568
        }
569

  
570
        /* 6.1.7.4, p23 */
571
        if ( get_bits(gb, 1) /* aspect_ratio_flag */)
572
        {
573
            aspect_ratio = get_bits(gb, 4); //SAR
574
            if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
575
            {
576
                avctx->sample_aspect_ratio.num = 1 + get_bits(gb, 8);
577
                avctx->sample_aspect_ratio.den = 1 + get_bits(gb, 8);
578
            }
579
            else if (aspect_ratio == 0x0E)
580
            {
581
                av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
582
            }
583
            else
584
            {
585
              avctx->sample_aspect_ratio = vc1_pixel_aspect[aspect_ratio];
586
            }
587
        }
588
    }
589
    else
590
    {
591
        avctx->coded_width = avctx->width;
592
        avctx->coded_height = avctx->height;
593
    }
594

  
595
    /* 6.1.8, p23 */
596
    if ( get_bits(gb, 1) /* framerateflag */)
597
    {
598
        if ( !get_bits(gb, 1) /* framerateind */)
599
        {
600
            nr = get_bits(gb, 8);
601
            dr = get_bits(gb, 4);
602
            if (nr<1)
603
            {
604
                av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
605
                return -1;
606
            }
607
            if (nr>5)
608
            {
609
                av_log(avctx, AV_LOG_ERROR,
610
                       "Reserved FRAMERATENR %i not handled\n", nr);
611
                nr = 5; /* overflow protection */
612
            }
613
            if (dr<1)
614
            {
615
                av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
616
                return -1;
617
            }
618
            if (dr>2)
619
            {
620
                av_log(avctx, AV_LOG_ERROR,
621
                       "Reserved FRAMERATEDR %i not handled\n", dr);
622
                dr = 2; /* overflow protection */
623
            }
624
            avctx->time_base.num = fps_nr[dr - 1];
625
            avctx->time_base.den = fps_nr[nr - 1];
626
        }
627
        else
628
        {
629
            nr = get_bits(gb, 16);
630
            // 0.03125->2048Hz / 0.03125Hz
631
            avctx->time_base.den = 1000000;
632
            avctx->time_base.num = 31250*(1+nr);
633
        }
634
    }
635

  
636
    /* 6.1.9, p25 */
637
    if ( get_bits(gb, 1) /* color_format_flag */)
638
    {
639
        //Chromacity coordinates of color primaries
640
        //like ITU-R BT.709-2, BT.470-2, ...
641
        v->color_prim = get_bits(gb, 8);
642
        if (v->color_prim<1)
643
        {
644
            av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is forbidden\n");
645
            return -1;
646
        }
647
        if (v->color_prim == 3 || v->color_prim>6)
648
        {
649
            av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
650
                   v->color_prim);
651
            return -1;
652
        }
653

  
654
        //Opto-electronic transfer characteristics
655
        v->transfer_char = get_bits(gb, 8);
656
        if (v->transfer_char < 1)
657
        {
658
            av_log(avctx, AV_LOG_ERROR, "0 for TRAMSFER_CHAR is forbidden\n");
659
            return -1;
660
        }
661
        if (v->transfer_char == 3 || v->transfer_char>8)
662
        {
663
            av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
664
                   v->color_prim);
665
            return -1;
666
        }
667

  
668
        //Matrix coefficient for primariev->YCbCr
669
        v->matrix_coef = get_bits(gb, 8);
670
        if (v->matrix_coef < 1)
671
        {
672
            av_log(avctx, AV_LOG_ERROR, "0 for MATRIX_COEF is forbidden\n");
673
            return -1;
674
        }
675
        if ((v->matrix_coef > 2 && v->matrix_coef < 6) || v->matrix_coef > 7)
676
        {
677
            av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
678
                   v->color_prim);
679
            return -1;
680
        }
681
    }
682

  
683
    //Hypothetical reference decoder indicator flag
684
    v->hrd_param_flag = get_bits(gb, 1);
685
    if (v->hrd_param_flag)
686
    {
687
      if (decode_hrd(v, gb) < 0) return -1;
688
    }
689

  
690
    /*reset scaling ranges, 6.2.2 & 6.2.3, p33*/
691
    v->range_mapy_flag = 0;
692
    v->range_mapuv_flag = 0;
693

  
694
    av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
695
    return -1;
696
}
697
#endif
698

  
699
/**
700
 * Decode Simple/Main Profiles sequence header
701
 * @see Figure 7-8, p16-17
702
 * @param avctx Codec context
703
 * @param gb GetBit context initialized from Codec context extra_data
704
 * @return Status
705
 */
706
static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
707
{
708
    VC1Context *v = avctx->priv_data;
709

  
710
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
711
    v->profile = get_bits(gb, 2);
712
    if (v->profile == 2)
713
    {
714
        av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden\n");
715
        return -1;
716
    }
717

  
718
#if HAS_ADVANCED_PROFILE
719
    if (v->profile == PROFILE_ADVANCED)
720
    {
721
        v->level = get_bits(gb, 3);
722
        if(v->level >= 5)
723
        {
724
            av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
725
        }
726
        v->chromaformat = get_bits(gb, 2);
727
        if (v->chromaformat != 1)
728
        {
729
            av_log(avctx, AV_LOG_ERROR,
730
                   "Only 4:2:0 chroma format supported\n");
731
            return -1;
732
        }
733
    }
734
    else
735
#endif
736
    {
737
        v->res_sm = get_bits(gb, 2); //reserved
738
        if (v->res_sm)
739
        {
740
            av_log(avctx, AV_LOG_ERROR,
741
                   "Reserved RES_SM=%i is forbidden\n", v->res_sm);
742
            return -1;
743
        }
744
    }
745

  
746
    // (fps-2)/4 (->30)
747
    v->frmrtq_postproc = get_bits(gb, 3); //common
748
    // (bitrate-32kbps)/64kbps
749
    v->bitrtq_postproc = get_bits(gb, 5); //common
750
    v->s.loop_filter = get_bits(gb, 1); //common
751
    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
752
    {
753
        av_log(avctx, AV_LOG_ERROR,
754
               "LOOPFILTER shell not be enabled in simple profile\n");
755
    }
756

  
757
#if HAS_ADVANCED_PROFILE
758
    if (v->profile < PROFILE_ADVANCED)
759
#endif
760
    {
761
        v->res_x8 = get_bits(gb, 1); //reserved
762
        if (v->res_x8)
763
        {
764
            av_log(avctx, AV_LOG_ERROR,
765
                   "1 for reserved RES_X8 is forbidden\n");
766
            //return -1;
767
        }
768
        v->multires = get_bits(gb, 1);
769
        v->res_fasttx = get_bits(gb, 1);
770
        if (!v->res_fasttx)
771
        {
772
            av_log(avctx, AV_LOG_ERROR,
773
                   "0 for reserved RES_FASTTX is forbidden\n");
774
            //return -1;
775
        }
776
    }
777

  
778
    v->fastuvmc =  get_bits(gb, 1); //common
779
    if (!v->profile && !v->fastuvmc)
780
    {
781
        av_log(avctx, AV_LOG_ERROR,
782
               "FASTUVMC unavailable in Simple Profile\n");
783
        return -1;
784
    }
785
    v->extended_mv =  get_bits(gb, 1); //common
786
    if (!v->profile && v->extended_mv)
787
    {
788
        av_log(avctx, AV_LOG_ERROR,
789
               "Extended MVs unavailable in Simple Profile\n");
790
        return -1;
791
    }
792
    v->dquant =  get_bits(gb, 2); //common
793
    v->vstransform =  get_bits(gb, 1); //common
794

  
795
#if HAS_ADVANCED_PROFILE
796
    if (v->profile < PROFILE_ADVANCED)
797
#endif
798
    {
799
        v->res_transtab = get_bits(gb, 1);
800
        if (v->res_transtab)
801
        {
802
            av_log(avctx, AV_LOG_ERROR,
803
                   "1 for reserved RES_TRANSTAB is forbidden\n");
804
            return -1;
805
        }
806
    }
807

  
808
    v->overlap = get_bits(gb, 1); //common
809

  
810
#if HAS_ADVANCED_PROFILE
811
    if (v->profile < PROFILE_ADVANCED)
812
#endif
813
    {
814
        v->s.resync_marker = get_bits(gb, 1);
815
        v->rangered = get_bits(gb, 1);
816
        if (v->rangered && v->profile == PROFILE_SIMPLE)
817
        {
818
            av_log(avctx, AV_LOG_DEBUG,
819
                   "RANGERED should be set to 0 in simple profile\n");
820
        }
821
    }
822

  
823
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
824
    v->quantizer_mode = get_bits(gb, 2); //common
825

  
826
#if HAS_ADVANCED_PROFILE
827
    if (v->profile < PROFILE_ADVANCED)
828
#endif
829
    {
830
        v->finterpflag = get_bits(gb, 1); //common
831
        v->res_rtm_flag = get_bits(gb, 1); //reserved
832
        if (!v->res_rtm_flag)
833
        {
834
            av_log(avctx, AV_LOG_ERROR,
835
                   "0 for reserved RES_RTM_FLAG is forbidden\n");
836
            //return -1;
837
        }
838
#if TRACE
839
        av_log(avctx, AV_LOG_INFO,
840
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
841
               "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
842
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
843
               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
844
               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
845
               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
846
               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
847
               v->dquant, v->quantizer_mode, avctx->max_b_frames
848
               );
849
        return 0;
850
#endif
851
    }
852
#if HAS_ADVANCED_PROFILE
853
    else return decode_advanced_sequence_header(avctx, gb);
854
#endif
855
}
856

  
857

  
858
#if HAS_ADVANCED_PROFILE
859
/** Entry point decoding (Advanced Profile)
860
 * @param avctx Codec context
861
 * @param gb GetBit context initialized from avctx->extra_data
862
 * @return Status
863
 */
864
static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
865
{
866
    VC1Context *v = avctx->priv_data;
867
    int i;
868
    if (v->profile != PROFILE_ADVANCED)
869
    {
870
        av_log(avctx, AV_LOG_ERROR,
871
               "Entry point are only defined in Advanced Profile!\n");
872
        return -1; //Only for advanced profile!
873
    }
874
    if (v->hrd_param_flag)
875
    {
876
        //Update buffer fullness
877
        av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
878
        assert(v->hrd_num_leaky_buckets > 0);
879
        for (i=0; i<v->hrd_num_leaky_buckets; i++)
880
            v->hrd_fullness[i] = get_bits(gb, 8);
881
    }
882
    if ((v->range_mapy_flag = get_bits(gb, 1)))
883
    {
884
        //RANGE_MAPY
885
        av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
886
        v->range_mapy = get_bits(gb, 3);
887
    }
888
    if ((v->range_mapuv_flag = get_bits(gb, 1)))
889
    {
890
        //RANGE_MAPUV
891
        av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
892
        v->range_mapuv = get_bits(gb, 3);
893
    }
894
    if (v->panscanflag)
895
    {
896
        //NUMPANSCANWIN
897
        v->numpanscanwin = get_bits(gb, 3);
898
        av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
899
    }
900
    return 0;
901
}
902
#endif
903

  
904
/***********************************************************************/
905
/**
906
 * @defgroup bitplane VC1 Bitplane decoding
907
 * @see 8.7, p56
908
 * @{
909
 */
910

  
911
/** @addtogroup bitplane
912
 * Imode types
913
 * @{
914
 */
915
#define IMODE_RAW     0
916
#define IMODE_NORM2   1
917
#define IMODE_DIFF2   2
918
#define IMODE_NORM6   3
919
#define IMODE_DIFF6   4
920
#define IMODE_ROWSKIP 5
921
#define IMODE_COLSKIP 6
922
/** @} */ //imode defines
923

  
924
/** Allocate the buffer from a bitplane, given its dimensions
925
 * @param bp Bitplane which buffer is to allocate
926
 * @param[in] width Width of the buffer
927
 * @param[in] height Height of the buffer
928
 * @return Status
929
 * @todo TODO: Take into account stride
930
 * @todo TODO: Allow use of external buffers ?
931
 */
932
static int alloc_bitplane(BitPlane *bp, int width, int height)
933
{
934
    if (!bp || bp->width<0 || bp->height<0) return -1;
935
    bp->data = (uint8_t*)av_malloc(width*height);
936
    if (!bp->data) return -1;
937
    bp->width = bp->stride = width;
938
    bp->height = height;
939
    return 0;
940
}
941

  
942
/** Free the bitplane's buffer
943
 * @param bp Bitplane which buffer is to free
944
 */
945
static void free_bitplane(BitPlane *bp)
946
{
947
    bp->width = bp->stride = bp->height = 0;
948
    if (bp->data) av_freep(&bp->data);
949
}
950

  
951
/** Decode rows by checking if they are skipped
952
 * @param plane Buffer to store decoded bits
953
 * @param[in] width Width of this buffer
954
 * @param[in] height Height of this buffer
955
 * @param[in] stride of this buffer
956
 */
957
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
958
    int x, y;
959

  
960
    for (y=0; y<height; y++){
961
        if (!get_bits(gb, 1)) //rowskip
962
            memset(plane, 0, width);
963
        else
964
            for (x=0; x<width; x++)
965
                plane[x] = get_bits(gb, 1);
966
        plane += stride;
967
    }
968
}
969

  
970
/** Decode columns by checking if they are skipped
971
 * @param plane Buffer to store decoded bits
972
 * @param[in] width Width of this buffer
973
 * @param[in] height Height of this buffer
974
 * @param[in] stride of this buffer
975
 * @fixme FIXME: Optimize
976
 */
977
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
978
    int x, y;
979

  
980
    for (x=0; x<width; x++){
981
        if (!get_bits(gb, 1)) //colskip
982
            for (y=0; y<height; y++)
983
                plane[y*stride] = 0;
984
        else
985
            for (y=0; y<height; y++)
986
                plane[y*stride] = get_bits(gb, 1);
987
        plane ++;
988
    }
989
}
990

  
991
/** Decode a bitplane's bits
992
 * @param bp Bitplane where to store the decode bits
993
 * @param v VC1 context for bit reading and logging
994
 * @return Status
995
 * @fixme FIXME: Optimize
996
 * @todo TODO: Decide if a struct is needed
997
 */
998
static int bitplane_decoding(BitPlane *bp, VC1Context *v)
999
{
1000
    GetBitContext *gb = &v->s.gb;
1001

  
1002
    int imode, x, y, code, use_vertical_tile, tile_w, tile_h, offset;
1003
    uint8_t invert, *planep = bp->data;
1004

  
1005
    invert = get_bits(gb, 1);
1006
    imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 2);
1007

  
1008
    bp->is_raw = 0;
1009
    switch (imode)
1010
    {
1011
    case IMODE_RAW:
1012
        //Data is actually read in the MB layer (same for all tests == "raw")
1013
        bp->is_raw = 1; //invert ignored
1014
        return invert;
1015
    case IMODE_DIFF2:
1016
    case IMODE_NORM2:
1017
        if ((bp->height*bp->width) & 1)
1018
        {
1019
            *(++planep) = get_bits(gb, 1);
1020
            offset = x = 1;
1021
        }
1022
        else offset = x = 0;
1023

  
1024
        for (y=0; y<bp->height; y++)
1025
        {
1026
            for(; x<bp->width; x+=2)
1027
            {
1028
                code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 2);
1029
                *(++planep) = code&1; //lsb => left
1030
                *(++planep) = (code>>1)&1; //msb => right
1031
            }
1032
            planep += bp->stride-bp->width;
1033
            if ((bp->width-offset)&1) //Odd number previously processed
1034
            {
1035
                code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 2);
1036
                *planep = code&1;
1037
                planep += bp->stride-bp->width;
1038
                *planep = (code>>1)&1; //msb => right
1039
                offset = x = 1;
1040
            }
1041
            else
1042
            {
1043
                offset = x = 0;
1044
                planep += bp->stride-bp->width;
1045
            }
1046
        }
1047
        break;
1048
    case IMODE_DIFF6:
1049
    case IMODE_NORM6:
1050
        use_vertical_tile=  bp->height%3==0 &&  bp->width%3!=0;
1051
        tile_w= use_vertical_tile ? 2 : 3;
1052
        tile_h= use_vertical_tile ? 3 : 2;
1053

  
1054
        for(y=  bp->height%tile_h; y< bp->height; y+=tile_h){
1055
            for(x=  bp->width%tile_w; x< bp->width; x+=tile_w){
1056
                code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
1057
                if(code<0){
1058
                    av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
1059
                    return -1;
1060
                }
1061
                //FIXME following is a pure guess and probably wrong
1062
                //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
1063
                planep[x     + 0*bp->stride]= (code>>0)&1;
1064
                planep[x + 1 + 0*bp->stride]= (code>>1)&1;
1065
                //FIXME Does branch prediction help here?
1066
                if(use_vertical_tile){
1067
                    planep[x + 0 + 1*bp->stride]= (code>>2)&1;
1068
                    planep[x + 1 + 1*bp->stride]= (code>>3)&1;
1069
                    planep[x + 0 + 2*bp->stride]= (code>>4)&1;
1070
                    planep[x + 1 + 2*bp->stride]= (code>>5)&1;
1071
                }else{
1072
                    planep[x + 2 + 0*bp->stride]= (code>>2)&1;
1073
                    planep[x + 0 + 1*bp->stride]= (code>>3)&1;
1074
                    planep[x + 1 + 1*bp->stride]= (code>>4)&1;
1075
                    planep[x + 2 + 1*bp->stride]= (code>>5)&1;
1076
                }
1077
            }
1078
        }
1079

  
1080
        x=  bp->width % tile_w;
1081
        decode_colskip(bp->data  ,             x, bp->height         , bp->stride, &v->s.gb);
1082
        decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, &v->s.gb);
1083

  
1084
        break;
1085
    case IMODE_ROWSKIP:
1086
        decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
1087
        break;
1088
    case IMODE_COLSKIP:
1089
        decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
1090
        break;
1091
    default: break;
1092
    }
1093

  
1094
    /* Applying diff operator */
1095
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
1096
    {
1097
        planep = bp->data;
1098
        planep[0] ^= invert;
1099
        for (x=1; x<bp->width; x++)
1100
            planep[x] ^= planep[x-1];
1101
        for (y=1; y<bp->height; y++)
1102
        {
1103
            planep += bp->stride;
1104
            planep[0] ^= planep[-bp->stride];
1105
            for (x=1; x<bp->width; x++)
1106
            {
1107
                if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
1108
                else                                     planep[x] ^= planep[x-1];
1109
            }
1110
        }
1111
    }
1112
    else if (invert)
1113
    {
1114
        planep = bp->data;
1115
        for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
1116
    }
1117
    return (imode<<1) + invert;
1118
}
1119
/** @} */ //Bitplane group
1120

  
1121
/***********************************************************************/
1122
/** VOP Dquant decoding
1123
 * @param v VC1 Context
1124
 */
1125
static int vop_dquant_decoding(VC1Context *v)
1126
{
1127
    GetBitContext *gb = &v->s.gb;
1128
    int pqdiff;
1129

  
1130
    //variable size
1131
    if (v->dquant == 2)
1132
    {
1133
        pqdiff = get_bits(gb, 3);
1134
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
1135
        else v->altpq = v->pq + pqdiff + 1;
1136
    }
1137
    else
1138
    {
1139
        v->dquantfrm = get_bits(gb, 1);
1140
        if ( v->dquantfrm )
1141
        {
1142
            v->dqprofile = get_bits(gb, 2);
1143
            switch (v->dqprofile)
1144
            {
1145
            case DQPROFILE_SINGLE_EDGE:
1146
            case DQPROFILE_DOUBLE_EDGES:
1147
                v->dqsbedge = get_bits(gb, 2);
1148
                break;
1149
            case DQPROFILE_ALL_MBS:
1150
                v->dqbilevel = get_bits(gb, 1);
1151
            default: break; //Forbidden ?
1152
            }
1153
            if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
1154
            {
1155
                pqdiff = get_bits(gb, 3);
1156
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
1157
                else v->altpq = v->pq + pqdiff + 1;
1158
            }
1159
        }
1160
    }
1161
    return 0;
1162
}
1163

  
1164
/***********************************************************************/
1165
/**
1166
 * @defgroup all_frame_hdr All VC1 profiles frame header
1167
 * @brief Part of the frame header decoding from all profiles
1168
 * @warning Only pro/epilog differs between Simple/Main and Advanced => check caller
1169
 * @{
1170
 */
1171
/** B and BI frame header decoding, primary part
1172
 * @see Tables 11+12, p62-65
1173
 * @param v VC1 context
1174
 * @return Status
1175
 * @warning Also handles BI frames
1176
 */
1177
static int decode_b_picture_primary_header(VC1Context *v)
1178
{
1179
    GetBitContext *gb = &v->s.gb;
1180
    int pqindex;
1181

  
1182
    /* Prolog common to all frametypes should be done in caller */
1183
    if (v->profile == PROFILE_SIMPLE)
1184
    {
1185
        av_log(v->s.avctx, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n");
1186
        return FRAME_SKIPPED;
1187
    }
1188
    v->bfraction = vc1_bfraction_lut[get_vlc2(gb, vc1_bfraction_vlc.table,
1189
                                              VC1_BFRACTION_VLC_BITS, 2)];
1190
    if (v->bfraction < -1)
1191
    {
1192
        av_log(v->s.avctx, AV_LOG_ERROR, "Invalid BFRaction\n");
1193
        return FRAME_SKIPPED;
1194
    }
1195
    else if (!v->bfraction)
1196
    {
1197
        /* We actually have a BI frame */
1198
        v->s.pict_type = BI_TYPE;
1199
        v->buffer_fullness = get_bits(gb, 7);
1200
    }
1201

  
1202
    /* Read the quantization stuff */
1203
    pqindex = get_bits(gb, 5);
1204
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1205
        v->pq = pquant_table[0][pqindex];
1206
    else
1207
    {
1208
        v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1209
    }
1210
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1211
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1212
        v->pquantizer = get_bits(gb, 1);
1213
#if HAS_ADVANCED_PROFILE
1214
    if (v->profile == PROFILE_ADVANCED)
1215
    {
1216
        if (v->postprocflag) v->postproc = get_bits(gb, 2);
1217
        if (v->extended_mv == 1 && v->s.pict_type != BI_TYPE)
1218
            v->mvrange = get_prefix(gb, 0, 3);
1219
    }
1220
#endif
1221
    else
1222
    {
1223
        if (v->extended_mv == 1)
1224
            v->mvrange = get_prefix(gb, 0, 3);
1225
    }
1226
    /* Read the MV mode */
1227
    if (v->s.pict_type != BI_TYPE)
1228
    {
1229
        v->mv_mode = get_bits(gb, 1);
1230
        if (v->pq < 13)
1231
        {
1232
            if (!v->mv_mode)
1233
            {
1234
                v->mv_mode = get_bits(gb, 2);
1235
                if (v->mv_mode)
1236
                av_log(v->s.avctx, AV_LOG_ERROR,
1237
                       "mv_mode for lowquant B frame was %i\n", v->mv_mode);
1238
            }
1239
        }
1240
        else
1241
        {
1242
            if (!v->mv_mode)
1243
            {
1244
                if (get_bits(gb, 1))
1245
                     av_log(v->s.avctx, AV_LOG_ERROR,
1246
                            "mv_mode for highquant B frame was %i\n", v->mv_mode);
1247
            }
1248
            v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping
1249
        }
1250
    }
1251

  
1252
    return 0;
1253
}
1254

  
1255
/** B and BI frame header decoding, secondary part
1256
 * @see Tables 11+12, p62-65
1257
 * @param v VC1 context
1258
 * @return Status
1259
 * @warning Also handles BI frames
1260
 * @warning To call once all MB arrays are allocated
1261
 * @todo Support Advanced Profile headers
1262
 */
1263
static int decode_b_picture_secondary_header(VC1Context *v)
1264
{
1265
    GetBitContext *gb = &v->s.gb;
1266
    int status;
1267

  
1268
    status = bitplane_decoding(&v->skip_mb_plane, v);
1269
    if (status < 0) return -1;
1270
#if TRACE
1271
    if (v->mv_mode == MV_PMODE_MIXED_MV)
1272
    {
1273
        status = bitplane_decoding(&v->mv_type_mb_plane, v);
1274
        if (status < 0)
1275
            return -1;
1276
#if TRACE
1277
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1278
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1279
#endif
1280
    }
1281

  
1282
    //bitplane
1283
    status = bitplane_decoding(&v->direct_mb_plane, v);
1284
    if (status < 0) return -1;
1285
#if TRACE
1286
    av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct plane encoding: "
1287
           "Imode: %i, Invert: %i\n", status>>1, status&1);
1288
#endif
1289

  
1290
    av_log(v->s.avctx, AV_LOG_DEBUG, "Skip MB plane encoding: "
1291
           "Imode: %i, Invert: %i\n", status>>1, status&1);
1292
#endif
1293

  
1294
    /* FIXME: what is actually chosen for B frames ? */
1295
    v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1296
    v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1297

  
1298
    if (v->dquant)
1299
    {
1300
        vop_dquant_decoding(v);
1301
    }
1302

  
1303
    if (v->vstransform)
1304
    {
1305
        v->ttmbf = get_bits(gb, 1);
1306
        if (v->ttmbf)
1307
        {
1308
            v->ttfrm = get_bits(gb, 2);
1309
            av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
1310
                   (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
1311
        }
1312
    }
1313
    /* Epilog (AC/DC syntax) should be done in caller */
1314
    return 0;
1315
}
1316

  
1317
/** I frame header decoding, primary part
1318
 * @see Tables 5+7, p53-54 and 55-57
1319
 * @param v VC1 context
1320
 * @return Status
1321
 * @todo Support Advanced Profile headers
1322
 */
1323
static int decode_i_picture_primary_header(VC1Context *v)
1324
{
1325
    GetBitContext *gb = &v->s.gb;
1326
    int pqindex;
1327

  
1328
    /* Prolog common to all frametypes should be done in caller */
1329
    //BF = Buffer Fullness
1330
    if (v->profile < PROFILE_ADVANCED && get_bits(gb, 7))
1331
    {
1332
        av_log(v->s.avctx, AV_LOG_DEBUG, "I BufferFullness not 0\n");
1333
    }
1334

  
1335
    /* Quantizer stuff */
1336
    pqindex = get_bits(gb, 5);
1337
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1338
        v->pq = pquant_table[0][pqindex];
1339
    else
1340
    {
1341
        v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1342
    }
1343
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1344
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1345
        v->pquantizer = get_bits(gb, 1);
1346
    av_log(v->s.avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n",
1347
           v->pq, v->halfpq);
1348
    return 0;
1349
}
1350

  
1351
/** I frame header decoding, secondary part
1352
 * @param v VC1 context
1353
 * @return Status
1354
 * @warning Not called in A/S/C profiles, it seems
1355
 * @todo Support Advanced Profile headers
1356
 */
1357
static int decode_i_picture_secondary_header(VC1Context *v)
1358
{
1359
#if HAS_ADVANCED_PROFILE
1360
    int status;
1361
    if (v->profile == PROFILE_ADVANCED)
1362
    {
1363
        v->s.ac_pred = get_bits(&v->s.gb, 1);
1364
        if (v->postprocflag) v->postproc = get_bits(&v->s.gb, 1);
1365
        /* 7.1.1.34 + 8.5.2 */
1366
        if (v->overlap && v->pq<9)
1367
        {
1368
            v->condover = get_bits(&v->s.gb, 1);
1369
            if (v->condover)
1370
            {
1371
                v->condover = 2+get_bits(&v->s.gb, 1);
1372
                if (v->condover == 3)
1373
                {
1374
                    status = bitplane_decoding(&v->over_flags_plane, v);
1375
                    if (status < 0) return -1;
1376
#  if TRACE
1377
                    av_log(v->s.avctx, AV_LOG_DEBUG, "Overflags plane encoding: "
1378
                           "Imode: %i, Invert: %i\n", status>>1, status&1);
1379
#  endif
1380
                }
1381
            }
1382
        }
1383
    }
1384
#endif
1385

  
1386
    /* Epilog (AC/DC syntax) should be done in caller */
1387
    return 0;
1388
}
1389

  
1390
/** P frame header decoding, primary part
1391
 * @see Tables 5+7, p53-54 and 55-57
1392
 * @param v VC1 context
1393
 * @todo Support Advanced Profile headers
1394
 * @return Status
1395
 */
1396
static int decode_p_picture_primary_header(VC1Context *v)
1397
{
1398
    /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
1399
    GetBitContext *gb = &v->s.gb;
1400
    int lowquant, pqindex;
1401

  
1402
    pqindex = get_bits(gb, 5);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff