]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp8.h
mpeg12dec: avoid signed overflow in bitrate calculation
[ffmpeg] / libavcodec / vp8.h
1 /*
2  * VP8 compatible video decoder
3  *
4  * Copyright (C) 2010 David Conrad
5  * Copyright (C) 2010 Ronald S. Bultje
6  * Copyright (C) 2010 Fiona Glaser
7  * Copyright (C) 2012 Daniel Kang
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 #ifndef AVCODEC_VP8_H
27 #define AVCODEC_VP8_H
28
29 #include "libavutil/buffer.h"
30
31 #include "h264pred.h"
32 #include "thread.h"
33 #include "vp56.h"
34 #include "vp8dsp.h"
35
36 #if HAVE_PTHREADS
37 #   include <pthread.h>
38 #elif HAVE_W32THREADS
39 #   include "compat/w32pthreads.h"
40 #endif
41
42 #define VP8_MAX_QUANT 127
43
44 enum dct_token {
45     DCT_0,
46     DCT_1,
47     DCT_2,
48     DCT_3,
49     DCT_4,
50     DCT_CAT1,
51     DCT_CAT2,
52     DCT_CAT3,
53     DCT_CAT4,
54     DCT_CAT5,
55     DCT_CAT6,
56     DCT_EOB,
57
58     NUM_DCT_TOKENS
59 };
60
61 // used to signal 4x4 intra pred in luma MBs
62 #define MODE_I4x4 4
63
64 enum inter_mvmode {
65     VP8_MVMODE_ZERO = MODE_I4x4 + 1,
66     VP8_MVMODE_MV,
67     VP8_MVMODE_SPLIT
68 };
69
70 enum inter_splitmvmode {
71     VP8_SPLITMVMODE_16x8 = 0,    ///< 2 16x8 blocks (vertical)
72     VP8_SPLITMVMODE_8x16,        ///< 2 8x16 blocks (horizontal)
73     VP8_SPLITMVMODE_8x8,         ///< 2x2 blocks of 8x8px each
74     VP8_SPLITMVMODE_4x4,         ///< 4x4 blocks of 4x4px each
75     VP8_SPLITMVMODE_NONE,        ///< (only used in prediction) no split MVs
76 };
77
78 typedef struct VP8FilterStrength {
79     uint8_t filter_level;
80     uint8_t inner_limit;
81     uint8_t inner_filter;
82 } VP8FilterStrength;
83
84 typedef struct VP8Macroblock {
85     uint8_t skip;
86     // TODO: make it possible to check for at least (i4x4 or split_mv)
87     // in one op. are others needed?
88     uint8_t mode;
89     uint8_t ref_frame;
90     uint8_t partitioning;
91     uint8_t chroma_pred_mode;
92     uint8_t segment;
93     uint8_t intra4x4_pred_mode_mb[16];
94     DECLARE_ALIGNED(4, uint8_t, intra4x4_pred_mode_top)[4];
95     VP56mv mv;
96     VP56mv bmv[16];
97 } VP8Macroblock;
98
99 typedef struct VP8ThreadData {
100     DECLARE_ALIGNED(16, int16_t, block)[6][4][16];
101     DECLARE_ALIGNED(16, int16_t, block_dc)[16];
102     /**
103      * This is the index plus one of the last non-zero coeff
104      * for each of the blocks in the current macroblock.
105      * So, 0 -> no coeffs
106      *     1 -> dc-only (special transform)
107      *     2+-> full transform
108      */
109     DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
110     /**
111      * For coeff decode, we need to know whether the above block had non-zero
112      * coefficients. This means for each macroblock, we need data for 4 luma
113      * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
114      * per macroblock. We keep the last row in top_nnz.
115      */
116     DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
117     int thread_nr;
118 #if HAVE_THREADS
119     pthread_mutex_t lock;
120     pthread_cond_t cond;
121 #endif
122     int thread_mb_pos; // (mb_y << 16) | (mb_x & 0xFFFF)
123     int wait_mb_pos; // What the current thread is waiting on.
124
125 #define EDGE_EMU_LINESIZE 32
126     DECLARE_ALIGNED(16, uint8_t, edge_emu_buffer)[21 * EDGE_EMU_LINESIZE];
127     VP8FilterStrength *filter_strength;
128 } VP8ThreadData;
129
130 typedef struct VP8Frame {
131     ThreadFrame tf;
132     AVBufferRef *seg_map;
133
134     AVBufferRef *hwaccel_priv_buf;
135     void *hwaccel_picture_private;
136 } VP8Frame;
137
138 #define MAX_THREADS 8
139 typedef struct VP8Context {
140     VP8ThreadData *thread_data;
141     AVCodecContext *avctx;
142     enum AVPixelFormat pix_fmt;
143
144     VP8Frame *framep[4];
145     VP8Frame *next_framep[4];
146     VP8Frame *curframe;
147     VP8Frame *prev_frame;
148
149     uint16_t mb_width;   /* number of horizontal MB */
150     uint16_t mb_height;  /* number of vertical MB */
151     ptrdiff_t linesize;
152     ptrdiff_t uvlinesize;
153
154     uint8_t keyframe;
155     uint8_t deblock_filter;
156     uint8_t mbskip_enabled;
157     uint8_t profile;
158     VP56mv mv_min;
159     VP56mv mv_max;
160
161     int8_t sign_bias[4]; ///< one state [0, 1] per ref frame type
162     int ref_count[3];
163
164     /**
165      * Base parameters for segmentation, i.e. per-macroblock parameters.
166      * These must be kept unchanged even if segmentation is not used for
167      * a frame, since the values persist between interframes.
168      */
169     struct {
170         uint8_t enabled;
171         uint8_t absolute_vals;
172         uint8_t update_map;
173         uint8_t update_feature_data;
174         int8_t base_quant[4];
175         int8_t filter_level[4];     ///< base loop filter level
176     } segmentation;
177
178     struct {
179         uint8_t simple;
180         uint8_t level;
181         uint8_t sharpness;
182     } filter;
183
184     VP8Macroblock *macroblocks;
185
186     uint8_t *intra4x4_pred_mode_top;
187     uint8_t intra4x4_pred_mode_left[4];
188
189     /**
190      * Macroblocks can have one of 4 different quants in a frame when
191      * segmentation is enabled.
192      * If segmentation is disabled, only the first segment's values are used.
193      */
194     struct {
195         // [0] - DC qmul  [1] - AC qmul
196         int16_t luma_qmul[2];
197         int16_t luma_dc_qmul[2];    ///< luma dc-only block quant
198         int16_t chroma_qmul[2];
199     } qmat[4];
200
201     // Raw quantisation values, which may be needed by hwaccel decode.
202     struct {
203         int yac_qi;
204         int ydc_delta;
205         int y2dc_delta;
206         int y2ac_delta;
207         int uvdc_delta;
208         int uvac_delta;
209     } quant;
210
211     struct {
212         uint8_t enabled;    ///< whether each mb can have a different strength based on mode/ref
213         uint8_t update;
214
215         /**
216          * filter strength adjustment for the following macroblock modes:
217          * [0-3] - i16x16 (always zero)
218          * [4]   - i4x4
219          * [5]   - zero mv
220          * [6]   - inter modes except for zero or split mv
221          * [7]   - split mv
222          *  i16x16 modes never have any adjustment
223          */
224         int8_t mode[VP8_MVMODE_SPLIT + 1];
225
226         /**
227          * filter strength adjustment for macroblocks that reference:
228          * [0] - intra / VP56_FRAME_CURRENT
229          * [1] - VP56_FRAME_PREVIOUS
230          * [2] - VP56_FRAME_GOLDEN
231          * [3] - altref / VP56_FRAME_GOLDEN2
232          */
233         int8_t ref[4];
234     } lf_delta;
235
236     uint8_t (*top_border)[16 + 8 + 8];
237     uint8_t (*top_nnz)[9];
238
239     VP56RangeCoder c;   ///< header context, includes mb modes and motion vectors
240
241     /* This contains the entropy coder state at the end of the header
242      * block, in the form specified by the standard.  For use by
243      * hwaccels, so that a hardware decoder has the information to
244      * start decoding at the macroblock layer.
245      */
246     struct {
247         const uint8_t *input;
248         uint32_t range;
249         uint32_t value;
250         int bit_count;
251     } coder_state_at_header_end;
252
253     int header_partition_size;
254
255     /**
256      * These are all of the updatable probabilities for binary decisions.
257      * They are only implicitly reset on keyframes, making it quite likely
258      * for an interframe to desync if a prior frame's header was corrupt
259      * or missing outright!
260      */
261     struct {
262         uint8_t segmentid[3];
263         uint8_t mbskip;
264         uint8_t intra;
265         uint8_t last;
266         uint8_t golden;
267         uint8_t pred16x16[4];
268         uint8_t pred8x8c[3];
269         uint8_t token[4][16][3][NUM_DCT_TOKENS - 1];
270         uint8_t mvc[2][19];
271         uint8_t scan[16];
272     } prob[2];
273
274     VP8Macroblock *macroblocks_base;
275     int invisible;
276     int update_last;    ///< update VP56_FRAME_PREVIOUS with the current one
277     int update_golden;  ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
278     int update_altref;
279
280     /**
281      * If this flag is not set, all the probability updates
282      * are discarded after this frame is decoded.
283      */
284     int update_probabilities;
285
286     /**
287      * All coefficients are contained in separate arith coding contexts.
288      * There can be 1, 2, 4, or 8 of these after the header context.
289      */
290     int num_coeff_partitions;
291     VP56RangeCoder coeff_partition[8];
292     int coeff_partition_size[8];
293     VideoDSPContext vdsp;
294     VP8DSPContext vp8dsp;
295     H264PredContext hpc;
296     vp8_mc_func put_pixels_tab[3][3][3];
297     VP8Frame frames[5];
298
299     uint8_t colorspace; ///< 0 is the only value allowed (meaning bt601)
300     uint8_t fullrange;  ///< whether we can skip clamping in dsp functions
301
302     int num_jobs;
303     /**
304      * This describes the macroblock memory layout.
305      * 0 -> Only width+height*2+1 macroblocks allocated (frame/single thread).
306      * 1 -> Macroblocks for entire frame allocated (sliced thread).
307      */
308     int mb_layout;
309
310     /**
311      * Fade bit present in bitstream (VP7)
312      */
313     int fade_present;
314
315     /**
316      * Interframe DC prediction (VP7)
317      * [0] VP56_FRAME_PREVIOUS
318      * [1] VP56_FRAME_GOLDEN
319      */
320     uint16_t inter_dc_pred[2][2];
321
322     /**
323      * Macroblock features (VP7)
324      */
325     uint8_t feature_enabled[4];
326     uint8_t feature_present_prob[4];
327     uint8_t feature_index_prob[4][3];
328     uint8_t feature_value[4][4];
329 } VP8Context;
330
331 int ff_vp8_decode_init(AVCodecContext *avctx);
332
333 int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
334                         AVPacket *avpkt);
335
336 int ff_vp8_decode_free(AVCodecContext *avctx);
337
338 #endif /* AVCODEC_VP8_H */