1/*
2 * VC-1 and WMV3 decoder
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#ifndef AVCODEC_VC1_H
24#define AVCODEC_VC1_H
25
26#include "avcodec.h"
27#include "mpegvideo.h"
28#include "intrax8.h"
29
30/** Markers used in VC-1 AP frame data */
31//@{
32enum VC1Code{
33    VC1_CODE_RES0       = 0x00000100,
34    VC1_CODE_ENDOFSEQ   = 0x0000010A,
35    VC1_CODE_SLICE,
36    VC1_CODE_FIELD,
37    VC1_CODE_FRAME,
38    VC1_CODE_ENTRYPOINT,
39    VC1_CODE_SEQHDR,
40};
41//@}
42
43#define IS_MARKER(x) (((x) & ~0xFF) == VC1_CODE_RES0)
44
45/** Available Profiles */
46//@{
47enum Profile {
48    PROFILE_SIMPLE,
49    PROFILE_MAIN,
50    PROFILE_COMPLEX, ///< TODO: WMV9 specific
51    PROFILE_ADVANCED
52};
53//@}
54
55/** Sequence quantizer mode */
56//@{
57enum QuantMode {
58    QUANT_FRAME_IMPLICIT,    ///< Implicitly specified at frame level
59    QUANT_FRAME_EXPLICIT,    ///< Explicitly specified at frame level
60    QUANT_NON_UNIFORM,       ///< Non-uniform quant used for all frames
61    QUANT_UNIFORM            ///< Uniform quant used for all frames
62};
63//@}
64
65/** Where quant can be changed */
66//@{
67enum DQProfile {
68    DQPROFILE_FOUR_EDGES,
69    DQPROFILE_DOUBLE_EDGES,
70    DQPROFILE_SINGLE_EDGE,
71    DQPROFILE_ALL_MBS
72};
73//@}
74
75/** @name Where quant can be changed
76 */
77//@{
78enum DQSingleEdge {
79    DQSINGLE_BEDGE_LEFT,
80    DQSINGLE_BEDGE_TOP,
81    DQSINGLE_BEDGE_RIGHT,
82    DQSINGLE_BEDGE_BOTTOM
83};
84//@}
85
86/** Which pair of edges is quantized with ALTPQUANT */
87//@{
88enum DQDoubleEdge {
89    DQDOUBLE_BEDGE_TOPLEFT,
90    DQDOUBLE_BEDGE_TOPRIGHT,
91    DQDOUBLE_BEDGE_BOTTOMRIGHT,
92    DQDOUBLE_BEDGE_BOTTOMLEFT
93};
94//@}
95
96/** MV modes for P frames */
97//@{
98enum MVModes {
99    MV_PMODE_1MV_HPEL_BILIN,
100    MV_PMODE_1MV,
101    MV_PMODE_1MV_HPEL,
102    MV_PMODE_MIXED_MV,
103    MV_PMODE_INTENSITY_COMP
104};
105//@}
106
107/** @name MV types for B frames */
108//@{
109enum BMVTypes {
110    BMV_TYPE_BACKWARD,
111    BMV_TYPE_FORWARD,
112    BMV_TYPE_INTERPOLATED
113};
114//@}
115
116/** @name Block types for P/B frames */
117//@{
118enum TransformTypes {
119    TT_8X8,
120    TT_8X4_BOTTOM,
121    TT_8X4_TOP,
122    TT_8X4, //Both halves
123    TT_4X8_RIGHT,
124    TT_4X8_LEFT,
125    TT_4X8, //Both halves
126    TT_4X4
127};
128//@}
129
130enum CodingSet {
131    CS_HIGH_MOT_INTRA = 0,
132    CS_HIGH_MOT_INTER,
133    CS_LOW_MOT_INTRA,
134    CS_LOW_MOT_INTER,
135    CS_MID_RATE_INTRA,
136    CS_MID_RATE_INTER,
137    CS_HIGH_RATE_INTRA,
138    CS_HIGH_RATE_INTER
139};
140
141/** @name Overlap conditions for Advanced Profile */
142//@{
143enum COTypes {
144    CONDOVER_NONE = 0,
145    CONDOVER_ALL,
146    CONDOVER_SELECT
147};
148//@}
149
150
151/** The VC1 Context
152 * @todo Change size wherever another size is more efficient
153 * Many members are only used for Advanced Profile
154 */
155typedef struct VC1Context{
156    MpegEncContext s;
157    IntraX8Context x8;
158
159    int bits;
160
161    /** Simple/Main Profile sequence header */
162    //@{
163    int res_sm;           ///< reserved, 2b
164    int res_x8;           ///< reserved
165    int multires;         ///< frame-level RESPIC syntax element present
166    int res_fasttx;       ///< reserved, always 1
167    int res_transtab;     ///< reserved, always 0
168    int rangered;         ///< RANGEREDFRM (range reduction) syntax element present
169                          ///< at frame level
170    int res_rtm_flag;     ///< reserved, set to 1
171    int reserved;         ///< reserved
172    //@}
173
174    /** Advanced Profile */
175    //@{
176    int level;            ///< 3bits, for Advanced/Simple Profile, provided by TS layer
177    int chromaformat;     ///< 2bits, 2=4:2:0, only defined
178    int postprocflag;     ///< Per-frame processing suggestion flag present
179    int broadcast;        ///< TFF/RFF present
180    int interlace;        ///< Progressive/interlaced (RPTFTM syntax element)
181    int tfcntrflag;       ///< TFCNTR present
182    int panscanflag;      ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
183    int refdist_flag;     ///< REFDIST syntax element present in II, IP, PI or PP field picture headers
184    int extended_dmv;     ///< Additional extended dmv range at P/B frame-level
185    int color_prim;       ///< 8bits, chroma coordinates of the color primaries
186    int transfer_char;    ///< 8bits, Opto-electronic transfer characteristics
187    int matrix_coef;      ///< 8bits, Color primaries->YCbCr transform matrix
188    int hrd_param_flag;   ///< Presence of Hypothetical Reference
189                          ///< Decoder parameters
190    int psf;              ///< Progressive Segmented Frame
191    //@}
192
193    /** Sequence header data for all Profiles
194     * TODO: choose between ints, uint8_ts and monobit flags
195     */
196    //@{
197    int profile;          ///< 2bits, Profile
198    int frmrtq_postproc;  ///< 3bits,
199    int bitrtq_postproc;  ///< 5bits, quantized framerate-based postprocessing strength
200    int fastuvmc;         ///< Rounding of qpel vector to hpel ? (not in Simple)
201    int extended_mv;      ///< Ext MV in P/B (not in Simple)
202    int dquant;           ///< How qscale varies with MBs, 2bits (not in Simple)
203    int vstransform;      ///< variable-size [48]x[48] transform type + info
204    int overlap;          ///< overlapped transforms in use
205    int quantizer_mode;   ///< 2bits, quantizer mode used for sequence, see QUANT_*
206    int finterpflag;      ///< INTERPFRM present
207    //@}
208
209    /** Frame decoding info for all profiles */
210    //@{
211    uint8_t mv_mode;      ///< MV coding monde
212    uint8_t mv_mode2;     ///< Secondary MV coding mode (B frames)
213    int k_x;              ///< Number of bits for MVs (depends on MV range)
214    int k_y;              ///< Number of bits for MVs (depends on MV range)
215    int range_x, range_y; ///< MV range
216    uint8_t pq, altpq;    ///< Current/alternate frame quantizer scale
217    const uint8_t* zz_8x4;///< Zigzag scan table for TT_8x4 coding mode
218    const uint8_t* zz_4x8;///< Zigzag scan table for TT_4x8 coding mode
219    /** pquant parameters */
220    //@{
221    uint8_t dquantfrm;
222    uint8_t dqprofile;
223    uint8_t dqsbedge;
224    uint8_t dqbilevel;
225    //@}
226    /** AC coding set indexes
227     * @see 8.1.1.10, p(1)10
228     */
229    //@{
230    int c_ac_table_index; ///< Chroma index from ACFRM element
231    int y_ac_table_index; ///< Luma index from AC2FRM element
232    //@}
233    int ttfrm;            ///< Transform type info present at frame level
234    uint8_t ttmbf;        ///< Transform type flag
235    uint8_t ttblk4x4;     ///< Value of ttblk which indicates a 4x4 transform
236    int codingset;        ///< index of current table set from 11.8 to use for luma block decoding
237    int codingset2;       ///< index of current table set from 11.8 to use for chroma block decoding
238    int pqindex;          ///< raw pqindex used in coding set selection
239    int a_avail, c_avail;
240    uint8_t *mb_type_base, *mb_type[3];
241
242
243    /** Luma compensation parameters */
244    //@{
245    uint8_t lumscale;
246    uint8_t lumshift;
247    //@}
248    int16_t bfraction;    ///< Relative position % anchors=> how to scale MVs
249    uint8_t halfpq;       ///< Uniform quant over image and qp+.5
250    uint8_t respic;       ///< Frame-level flag for resized images
251    int buffer_fullness;  ///< HRD info
252    /** Ranges:
253     * -# 0 -> [-64n 63.f] x [-32, 31.f]
254     * -# 1 -> [-128, 127.f] x [-64, 63.f]
255     * -# 2 -> [-512, 511.f] x [-128, 127.f]
256     * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
257     */
258    uint8_t mvrange;
259    uint8_t pquantizer;           ///< Uniform (over sequence) quantizer in use
260    VLC *cbpcy_vlc;               ///< CBPCY VLC table
261    int tt_index;                 ///< Index for Transform Type tables
262    uint8_t* mv_type_mb_plane;    ///< bitplane for mv_type == (4MV)
263    uint8_t* direct_mb_plane;     ///< bitplane for "direct" MBs
264    int mv_type_is_raw;           ///< mv type mb plane is not coded
265    int dmb_is_raw;               ///< direct mb plane is raw
266    int skip_is_raw;              ///< skip mb plane is not coded
267    uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
268    int use_ic;                   ///< use intensity compensation in B-frames
269    int rnd;                      ///< rounding control
270
271    /** Frame decoding info for S/M profiles only */
272    //@{
273    uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
274    uint8_t interpfrm;
275    //@}
276
277    /** Frame decoding info for Advanced profile */
278    //@{
279    uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
280    uint8_t numpanscanwin;
281    uint8_t tfcntr;
282    uint8_t rptfrm, tff, rff;
283    uint16_t topleftx;
284    uint16_t toplefty;
285    uint16_t bottomrightx;
286    uint16_t bottomrighty;
287    uint8_t uvsamp;
288    uint8_t postproc;
289    int hrd_num_leaky_buckets;
290    uint8_t bit_rate_exponent;
291    uint8_t buffer_size_exponent;
292    uint8_t* acpred_plane;       ///< AC prediction flags bitplane
293    int acpred_is_raw;
294    uint8_t* over_flags_plane;   ///< Overflags bitplane
295    int overflg_is_raw;
296    uint8_t condover;
297    uint16_t *hrd_rate, *hrd_buffer;
298    uint8_t *hrd_fullness;
299    uint8_t range_mapy_flag;
300    uint8_t range_mapuv_flag;
301    uint8_t range_mapy;
302    uint8_t range_mapuv;
303    //@}
304
305    int p_frame_skipped;
306    int bi_type;
307    int x8_type;
308
309    uint32_t *cbp_base, *cbp;
310    uint8_t bfraction_lut_index;///< Index for BFRACTION value (see Table 40, reproduced into ff_vc1_bfraction_lut[])
311    uint8_t broken_link;        ///< Broken link flag (BROKEN_LINK syntax element)
312    uint8_t closed_entry;       ///< Closed entry point flag (CLOSED_ENTRY syntax element)
313
314    int parse_only;             ///< Context is used within parser
315
316    int warn_interlaced;
317} VC1Context;
318
319/** Find VC-1 marker in buffer
320 * @return position where next marker starts or end of buffer if no marker found
321 */
322static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end)
323{
324    uint32_t mrk = 0xFFFFFFFF;
325
326    if(end-src < 4) return end;
327    while(src < end){
328        mrk = (mrk << 8) | *src++;
329        if(IS_MARKER(mrk))
330            return src-4;
331    }
332    return end;
333}
334
335static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
336{
337    int dsize = 0, i;
338
339    if(size < 4){
340        for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
341        return size;
342    }
343    for(i = 0; i < size; i++, src++) {
344        if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
345            dst[dsize++] = src[1];
346            src++;
347            i++;
348        } else
349            dst[dsize++] = *src;
350    }
351    return dsize;
352}
353
354/**
355 * Decode Simple/Main Profiles sequence header
356 * @see Figure 7-8, p16-17
357 * @param avctx Codec context
358 * @param gb GetBit context initialized from Codec context extra_data
359 * @return Status
360 */
361int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb);
362
363int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb);
364
365int vc1_parse_frame_header    (VC1Context *v, GetBitContext *gb);
366int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb);
367
368#endif /* AVCODEC_VC1_H */
369