/src/ffmpeg/libavcodec/vp3.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2003-2004 The FFmpeg project |
3 | | * Copyright (C) 2019 Peter Ross |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg 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.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg 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 FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * On2 VP3/VP4 Video Decoder |
25 | | * |
26 | | * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx) |
27 | | * For more information about the VP3 coding process, visit: |
28 | | * http://wiki.multimedia.cx/index.php?title=On2_VP3 |
29 | | * |
30 | | * Theora decoder by Alex Beregszaszi |
31 | | */ |
32 | | |
33 | | #include "config_components.h" |
34 | | |
35 | | #include <stddef.h> |
36 | | #include <string.h> |
37 | | |
38 | | #include "libavutil/attributes.h" |
39 | | #include "libavutil/emms.h" |
40 | | #include "libavutil/imgutils.h" |
41 | | #include "libavutil/mem.h" |
42 | | #include "libavutil/mem_internal.h" |
43 | | #include "libavutil/thread.h" |
44 | | |
45 | | #include "avcodec.h" |
46 | | #include "codec_internal.h" |
47 | | #include "decode.h" |
48 | | #include "get_bits.h" |
49 | | #include "hpeldsp.h" |
50 | | #include "jpegquanttables.h" |
51 | | #include "mathops.h" |
52 | | #include "progressframe.h" |
53 | | #include "libavutil/refstruct.h" |
54 | | #include "thread.h" |
55 | | #include "videodsp.h" |
56 | | #include "vp3data.h" |
57 | | #include "vp4data.h" |
58 | | #include "vp3dsp.h" |
59 | | #include "xiph.h" |
60 | | |
61 | 32.8M | #define VP3_MV_VLC_BITS 6 |
62 | 24.4M | #define VP4_MV_VLC_BITS 6 |
63 | 22.8M | #define SUPERBLOCK_VLC_BITS 6 |
64 | | |
65 | 47.2k | #define FRAGMENT_PIXELS 8 |
66 | | |
67 | | // FIXME split things out into their own arrays |
68 | | typedef struct Vp3Fragment { |
69 | | int16_t dc; |
70 | | uint8_t coding_method; |
71 | | uint8_t qpi; |
72 | | } Vp3Fragment; |
73 | | |
74 | 32.6M | #define SB_NOT_CODED 0 |
75 | 203M | #define SB_PARTIALLY_CODED 1 |
76 | 26.5M | #define SB_FULLY_CODED 2 |
77 | | |
78 | | // This is the maximum length of a single long bit run that can be encoded |
79 | | // for superblock coding or block qps. Theora special-cases this to read a |
80 | | // bit instead of flipping the current bit to allow for runs longer than 4129. |
81 | 3.61M | #define MAXIMUM_LONG_BIT_RUN 4129 |
82 | | |
83 | 160M | #define MODE_INTER_NO_MV 0 |
84 | 775M | #define MODE_INTRA 1 |
85 | 5.88M | #define MODE_INTER_PLUS_MV 2 |
86 | 4.81M | #define MODE_INTER_LAST_MV 3 |
87 | 1.32M | #define MODE_INTER_PRIOR_LAST 4 |
88 | 166M | #define MODE_USING_GOLDEN 5 |
89 | 123M | #define MODE_GOLDEN_MV 6 |
90 | 194M | #define MODE_INTER_FOURMV 7 |
91 | | #define CODING_MODE_COUNT 8 |
92 | | |
93 | | /* special internal mode */ |
94 | 1.26G | #define MODE_COPY 8 |
95 | | |
96 | | static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb); |
97 | | static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb); |
98 | | |
99 | | |
100 | | /* There are 6 preset schemes, plus a free-form scheme */ |
101 | | static const int ModeAlphabet[6][CODING_MODE_COUNT] = { |
102 | | /* scheme 1: Last motion vector dominates */ |
103 | | { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, |
104 | | MODE_INTER_PLUS_MV, MODE_INTER_NO_MV, |
105 | | MODE_INTRA, MODE_USING_GOLDEN, |
106 | | MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
107 | | |
108 | | /* scheme 2 */ |
109 | | { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, |
110 | | MODE_INTER_NO_MV, MODE_INTER_PLUS_MV, |
111 | | MODE_INTRA, MODE_USING_GOLDEN, |
112 | | MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
113 | | |
114 | | /* scheme 3 */ |
115 | | { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV, |
116 | | MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV, |
117 | | MODE_INTRA, MODE_USING_GOLDEN, |
118 | | MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
119 | | |
120 | | /* scheme 4 */ |
121 | | { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV, |
122 | | MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST, |
123 | | MODE_INTRA, MODE_USING_GOLDEN, |
124 | | MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
125 | | |
126 | | /* scheme 5: No motion vector dominates */ |
127 | | { MODE_INTER_NO_MV, MODE_INTER_LAST_MV, |
128 | | MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV, |
129 | | MODE_INTRA, MODE_USING_GOLDEN, |
130 | | MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
131 | | |
132 | | /* scheme 6 */ |
133 | | { MODE_INTER_NO_MV, MODE_USING_GOLDEN, |
134 | | MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, |
135 | | MODE_INTER_PLUS_MV, MODE_INTRA, |
136 | | MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
137 | | }; |
138 | | |
139 | | static const uint8_t hilbert_offset[16][2] = { |
140 | | { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, |
141 | | { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 }, |
142 | | { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 }, |
143 | | { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 } |
144 | | }; |
145 | | |
146 | | enum { |
147 | | VP4_DC_INTRA = 0, |
148 | | VP4_DC_INTER = 1, |
149 | | VP4_DC_GOLDEN = 2, |
150 | | NB_VP4_DC_TYPES, |
151 | | VP4_DC_UNDEFINED = NB_VP4_DC_TYPES |
152 | | }; |
153 | | |
154 | | static const uint8_t vp4_pred_block_type_map[8] = { |
155 | | [MODE_INTER_NO_MV] = VP4_DC_INTER, |
156 | | [MODE_INTRA] = VP4_DC_INTRA, |
157 | | [MODE_INTER_PLUS_MV] = VP4_DC_INTER, |
158 | | [MODE_INTER_LAST_MV] = VP4_DC_INTER, |
159 | | [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER, |
160 | | [MODE_USING_GOLDEN] = VP4_DC_GOLDEN, |
161 | | [MODE_GOLDEN_MV] = VP4_DC_GOLDEN, |
162 | | [MODE_INTER_FOURMV] = VP4_DC_INTER, |
163 | | }; |
164 | | |
165 | | static VLCElem superblock_run_length_vlc[88]; /* version < 2 */ |
166 | | static VLCElem fragment_run_length_vlc[56]; /* version < 2 */ |
167 | | static VLCElem motion_vector_vlc[112]; /* version < 2 */ |
168 | | |
169 | | // The VP4 tables reuse this vlc. |
170 | | static VLCElem mode_code_vlc[24 + 2108 * CONFIG_VP4_DECODER]; |
171 | | |
172 | | #if CONFIG_VP4_DECODER |
173 | | static const VLCElem *vp4_mv_vlc_table[2][7]; /* version >= 2 */ |
174 | | static const VLCElem *block_pattern_vlc[2]; /* version >= 2 */ |
175 | | #endif |
176 | | |
177 | | typedef struct { |
178 | | int dc; |
179 | | int type; |
180 | | } VP4Predictor; |
181 | | |
182 | | #define MIN_DEQUANT_VAL 2 |
183 | | |
184 | | typedef struct HuffEntry { |
185 | | uint8_t len, sym; |
186 | | } HuffEntry; |
187 | | |
188 | | typedef struct HuffTable { |
189 | | HuffEntry entries[32]; |
190 | | uint8_t nb_entries; |
191 | | } HuffTable; |
192 | | |
193 | | typedef struct CoeffVLCs { |
194 | | const VLCElem *vlc_tabs[80]; |
195 | | VLC vlcs[80]; |
196 | | } CoeffVLCs; |
197 | | |
198 | | typedef struct Vp3DecodeContext { |
199 | | AVCodecContext *avctx; |
200 | | int theora, theora_tables, theora_header; |
201 | | int version; |
202 | | int width, height; |
203 | | int chroma_x_shift, chroma_y_shift; |
204 | | ProgressFrame golden_frame; |
205 | | ProgressFrame last_frame; |
206 | | ProgressFrame current_frame; |
207 | | int keyframe; |
208 | | uint8_t idct_permutation[64]; |
209 | | uint8_t idct_scantable[64]; |
210 | | HpelDSPContext hdsp; |
211 | | VideoDSPContext vdsp; |
212 | | VP3DSPContext vp3dsp; |
213 | | DECLARE_ALIGNED(16, int16_t, block)[64]; |
214 | | int flipped_image; |
215 | | int last_slice_end; |
216 | | int skip_loop_filter; |
217 | | |
218 | | int qps[3]; |
219 | | int nqps; |
220 | | |
221 | | int superblock_count; |
222 | | int y_superblock_width; |
223 | | int y_superblock_height; |
224 | | int y_superblock_count; |
225 | | int c_superblock_width; |
226 | | int c_superblock_height; |
227 | | int c_superblock_count; |
228 | | int u_superblock_start; |
229 | | int v_superblock_start; |
230 | | unsigned char *superblock_coding; |
231 | | |
232 | | int macroblock_count; /* y macroblock count */ |
233 | | int macroblock_width; |
234 | | int macroblock_height; |
235 | | int c_macroblock_count; |
236 | | int c_macroblock_width; |
237 | | int c_macroblock_height; |
238 | | int yuv_macroblock_count; /* y+u+v macroblock count */ |
239 | | |
240 | | int fragment_count; |
241 | | int fragment_width[2]; |
242 | | int fragment_height[2]; |
243 | | |
244 | | Vp3Fragment *all_fragments; |
245 | | int fragment_start[3]; |
246 | | int data_offset[3]; |
247 | | uint8_t offset_x; |
248 | | uint8_t offset_y; |
249 | | int offset_x_warned; |
250 | | |
251 | | int8_t (*motion_val[2])[2]; |
252 | | |
253 | | /* tables */ |
254 | | uint16_t coded_dc_scale_factor[2][64]; |
255 | | uint32_t coded_ac_scale_factor[64]; |
256 | | uint8_t base_matrix[384][64]; |
257 | | uint8_t qr_count[2][3]; |
258 | | uint8_t qr_size[2][3][64]; |
259 | | uint16_t qr_base[2][3][64]; |
260 | | |
261 | | /** |
262 | | * This is a list of all tokens in bitstream order. Reordering takes place |
263 | | * by pulling from each level during IDCT. As a consequence, IDCT must be |
264 | | * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32 |
265 | | * otherwise. The 32 different tokens with up to 12 bits of extradata are |
266 | | * collapsed into 3 types, packed as follows: |
267 | | * (from the low to high bits) |
268 | | * |
269 | | * 2 bits: type (0,1,2) |
270 | | * 0: EOB run, 14 bits for run length (12 needed) |
271 | | * 1: zero run, 7 bits for run length |
272 | | * 7 bits for the next coefficient (3 needed) |
273 | | * 2: coefficient, 14 bits (11 needed) |
274 | | * |
275 | | * Coefficients are signed, so are packed in the highest bits for automatic |
276 | | * sign extension. |
277 | | */ |
278 | | int16_t *dct_tokens[3][64]; |
279 | | int16_t *dct_tokens_base; |
280 | 35.8M | #define TOKEN_EOB(eob_run) ((eob_run) << 2) |
281 | 41.9M | #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1) |
282 | 175M | #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2) |
283 | | |
284 | | /** |
285 | | * number of blocks that contain DCT coefficients at |
286 | | * the given level or higher |
287 | | */ |
288 | | int num_coded_frags[3][64]; |
289 | | int total_num_coded_frags; |
290 | | |
291 | | /* this is a list of indexes into the all_fragments array indicating |
292 | | * which of the fragments are coded */ |
293 | | int *coded_fragment_list[3]; |
294 | | |
295 | | int *kf_coded_fragment_list; |
296 | | int *nkf_coded_fragment_list; |
297 | | int num_kf_coded_fragment[3]; |
298 | | |
299 | | /** |
300 | | * The first 16 of the following VLCs are for the dc coefficients; |
301 | | * the others are four groups of 16 VLCs each for ac coefficients. |
302 | | * This is a RefStruct reference to share these VLCs between threads. |
303 | | */ |
304 | | CoeffVLCs *coeff_vlc; |
305 | | |
306 | | /* these arrays need to be on 16-byte boundaries since SSE2 operations |
307 | | * index into them */ |
308 | | DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane] |
309 | | |
310 | | /* This table contains superblock_count * 16 entries. Each set of 16 |
311 | | * numbers corresponds to the fragment indexes 0..15 of the superblock. |
312 | | * An entry will be -1 to indicate that no entry corresponds to that |
313 | | * index. */ |
314 | | int *superblock_fragments; |
315 | | |
316 | | /* This is an array that indicates how a particular macroblock |
317 | | * is coded. */ |
318 | | unsigned char *macroblock_coding; |
319 | | |
320 | | uint8_t *edge_emu_buffer; |
321 | | |
322 | | /* Huffman decode */ |
323 | | HuffTable huffman_table[5 * 16]; |
324 | | |
325 | | uint8_t filter_limit_values[64]; |
326 | | DECLARE_ALIGNED(16, int, bounding_values_array)[256 + 4]; |
327 | | |
328 | | VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */ |
329 | | } Vp3DecodeContext; |
330 | | |
331 | | /************************************************************************ |
332 | | * VP3 specific functions |
333 | | ************************************************************************/ |
334 | | |
335 | | static av_cold void free_tables(AVCodecContext *avctx) |
336 | 83.9k | { |
337 | 83.9k | Vp3DecodeContext *s = avctx->priv_data; |
338 | | |
339 | 83.9k | av_freep(&s->superblock_coding); |
340 | 83.9k | av_freep(&s->all_fragments); |
341 | 83.9k | av_freep(&s->nkf_coded_fragment_list); |
342 | 83.9k | av_freep(&s->kf_coded_fragment_list); |
343 | 83.9k | av_freep(&s->dct_tokens_base); |
344 | 83.9k | av_freep(&s->superblock_fragments); |
345 | 83.9k | av_freep(&s->macroblock_coding); |
346 | 83.9k | av_freep(&s->dc_pred_row); |
347 | 83.9k | av_freep(&s->motion_val[0]); |
348 | 83.9k | av_freep(&s->motion_val[1]); |
349 | 83.9k | } |
350 | | |
351 | | static av_cold void vp3_decode_flush(AVCodecContext *avctx) |
352 | 184k | { |
353 | 184k | Vp3DecodeContext *s = avctx->priv_data; |
354 | | |
355 | 184k | ff_progress_frame_unref(&s->golden_frame); |
356 | 184k | ff_progress_frame_unref(&s->last_frame); |
357 | 184k | ff_progress_frame_unref(&s->current_frame); |
358 | 184k | } |
359 | | |
360 | | static av_cold int vp3_decode_end(AVCodecContext *avctx) |
361 | 60.2k | { |
362 | 60.2k | Vp3DecodeContext *s = avctx->priv_data; |
363 | | |
364 | 60.2k | free_tables(avctx); |
365 | 60.2k | av_freep(&s->edge_emu_buffer); |
366 | | |
367 | 60.2k | s->theora_tables = 0; |
368 | | |
369 | | /* release all frames */ |
370 | 60.2k | vp3_decode_flush(avctx); |
371 | | |
372 | 60.2k | av_refstruct_unref(&s->coeff_vlc); |
373 | | |
374 | 60.2k | return 0; |
375 | 60.2k | } |
376 | | |
377 | | /** |
378 | | * This function sets up all of the various blocks mappings: |
379 | | * superblocks <-> fragments, macroblocks <-> fragments, |
380 | | * superblocks <-> macroblocks |
381 | | * |
382 | | * @return 0 is successful; returns 1 if *anything* went wrong. |
383 | | */ |
384 | | static int init_block_mapping(Vp3DecodeContext *s) |
385 | 23.6k | { |
386 | 23.6k | int j = 0; |
387 | | |
388 | 94.5k | for (int plane = 0; plane < 3; plane++) { |
389 | 70.9k | int sb_width = plane ? s->c_superblock_width |
390 | 70.9k | : s->y_superblock_width; |
391 | 70.9k | int sb_height = plane ? s->c_superblock_height |
392 | 70.9k | : s->y_superblock_height; |
393 | 70.9k | int frag_width = s->fragment_width[!!plane]; |
394 | 70.9k | int frag_height = s->fragment_height[!!plane]; |
395 | | |
396 | 3.88M | for (int sb_y = 0; sb_y < sb_height; sb_y++) |
397 | 86.7M | for (int sb_x = 0; sb_x < sb_width; sb_x++) |
398 | 1.40G | for (int i = 0; i < 16; i++) { |
399 | 1.32G | int x = 4 * sb_x + hilbert_offset[i][0]; |
400 | 1.32G | int y = 4 * sb_y + hilbert_offset[i][1]; |
401 | | |
402 | 1.32G | if (x < frag_width && y < frag_height) |
403 | 617M | s->superblock_fragments[j++] = s->fragment_start[plane] + |
404 | 617M | y * frag_width + x; |
405 | 708M | else |
406 | 708M | s->superblock_fragments[j++] = -1; |
407 | 1.32G | } |
408 | 70.9k | } |
409 | | |
410 | 23.6k | return 0; /* successful path out */ |
411 | 23.6k | } |
412 | | |
413 | | /* |
414 | | * This function sets up the dequantization tables used for a particular |
415 | | * frame. |
416 | | */ |
417 | | static void init_dequantizer(Vp3DecodeContext *s, int qpi) |
418 | 93.9k | { |
419 | 93.9k | int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]]; |
420 | | |
421 | 281k | for (int inter = 0; inter < 2; inter++) { |
422 | 751k | for (int plane = 0; plane < 3; plane++) { |
423 | 563k | int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]]; |
424 | 563k | int sum = 0, bmi, bmj, qistart, qri; |
425 | 1.06M | for (qri = 0; qri < s->qr_count[inter][plane]; qri++) { |
426 | 1.06M | sum += s->qr_size[inter][plane][qri]; |
427 | 1.06M | if (s->qps[qpi] <= sum) |
428 | 563k | break; |
429 | 1.06M | } |
430 | 563k | qistart = sum - s->qr_size[inter][plane][qri]; |
431 | 563k | bmi = s->qr_base[inter][plane][qri]; |
432 | 563k | bmj = s->qr_base[inter][plane][qri + 1]; |
433 | 36.6M | for (int i = 0; i < 64; i++) { |
434 | 36.0M | int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] - |
435 | 36.0M | 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] + |
436 | 36.0M | s->qr_size[inter][plane][qri]) / |
437 | 36.0M | (2 * s->qr_size[inter][plane][qri]); |
438 | | |
439 | 36.0M | int qmin = 8 << (inter + !i); |
440 | 36.0M | int qscale = i ? ac_scale_factor : dc_scale_factor; |
441 | 36.0M | int qbias = (1 + inter) * 3; |
442 | 36.0M | s->qmat[qpi][inter][plane][s->idct_permutation[i]] = |
443 | 36.0M | (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096) |
444 | 36.0M | : (qscale * (coeff - qbias) / 100 + qbias) * 4; |
445 | 36.0M | } |
446 | | /* all DC coefficients use the same quant so as not to interfere |
447 | | * with DC prediction */ |
448 | 563k | s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0]; |
449 | 563k | } |
450 | 187k | } |
451 | 93.9k | } |
452 | | |
453 | | /* |
454 | | * This function initializes the loop filter boundary limits if the frame's |
455 | | * quality index is different from the previous frame's. |
456 | | * |
457 | | * The filter_limit_values may not be larger than 127. |
458 | | */ |
459 | | static void init_loop_filter(Vp3DecodeContext *s) |
460 | 86.3k | { |
461 | 86.3k | ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]); |
462 | 86.3k | } |
463 | | |
464 | | /* |
465 | | * This function unpacks all of the superblock/macroblock/fragment coding |
466 | | * information from the bitstream. |
467 | | */ |
468 | | static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) |
469 | 154k | { |
470 | 154k | const int superblock_starts[3] = { |
471 | 154k | 0, s->u_superblock_start, s->v_superblock_start |
472 | 154k | }; |
473 | 154k | int bit = 0; |
474 | 154k | int current_superblock = 0; |
475 | 154k | int current_run = 0; |
476 | 154k | int num_partial_superblocks = 0; |
477 | | |
478 | 154k | int current_fragment; |
479 | 154k | int plane0_num_coded_frags = 0; |
480 | | |
481 | 154k | if (s->keyframe) { |
482 | 98.3k | memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count); |
483 | 98.3k | } else { |
484 | | /* unpack the list of partially-coded superblocks */ |
485 | 55.7k | bit = get_bits1(gb) ^ 1; |
486 | 55.7k | current_run = 0; |
487 | | |
488 | 12.6M | while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) { |
489 | 12.5M | if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN) |
490 | 538 | bit = get_bits1(gb); |
491 | 12.5M | else |
492 | 12.5M | bit ^= 1; |
493 | | |
494 | 12.5M | current_run = get_vlc2(gb, superblock_run_length_vlc, |
495 | 12.5M | SUPERBLOCK_VLC_BITS, 2); |
496 | 12.5M | if (current_run == 34) |
497 | 16.0k | current_run += get_bits(gb, 12); |
498 | | |
499 | 12.5M | if (current_run > s->superblock_count - current_superblock) { |
500 | 11.4k | av_log(s->avctx, AV_LOG_ERROR, |
501 | 11.4k | "Invalid partially coded superblock run length\n"); |
502 | 11.4k | return -1; |
503 | 11.4k | } |
504 | | |
505 | 12.5M | memset(s->superblock_coding + current_superblock, bit, current_run); |
506 | | |
507 | 12.5M | current_superblock += current_run; |
508 | 12.5M | if (bit) |
509 | 6.28M | num_partial_superblocks += current_run; |
510 | 12.5M | } |
511 | | |
512 | | /* unpack the list of fully coded superblocks if any of the blocks were |
513 | | * not marked as partially coded in the previous step */ |
514 | 44.3k | if (num_partial_superblocks < s->superblock_count) { |
515 | 43.2k | int superblocks_decoded = 0; |
516 | | |
517 | 43.2k | current_superblock = 0; |
518 | 43.2k | bit = get_bits1(gb) ^ 1; |
519 | 43.2k | current_run = 0; |
520 | | |
521 | 9.82M | while (superblocks_decoded < s->superblock_count - num_partial_superblocks && |
522 | 9.80M | get_bits_left(gb) > 0) { |
523 | 9.78M | if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN) |
524 | 207 | bit = get_bits1(gb); |
525 | 9.78M | else |
526 | 9.78M | bit ^= 1; |
527 | | |
528 | 9.78M | current_run = get_vlc2(gb, superblock_run_length_vlc, |
529 | 9.78M | SUPERBLOCK_VLC_BITS, 2); |
530 | 9.78M | if (current_run == 34) |
531 | 4.32k | current_run += get_bits(gb, 12); |
532 | | |
533 | 42.4M | for (int j = 0; j < current_run; current_superblock++) { |
534 | 32.6M | if (current_superblock >= s->superblock_count) { |
535 | 6.21k | av_log(s->avctx, AV_LOG_ERROR, |
536 | 6.21k | "Invalid fully coded superblock run length\n"); |
537 | 6.21k | return -1; |
538 | 6.21k | } |
539 | | |
540 | | /* skip any superblocks already marked as partially coded */ |
541 | 32.6M | if (s->superblock_coding[current_superblock] == SB_NOT_CODED) { |
542 | 15.4M | s->superblock_coding[current_superblock] = 2 * bit; |
543 | 15.4M | j++; |
544 | 15.4M | } |
545 | 32.6M | } |
546 | 9.78M | superblocks_decoded += current_run; |
547 | 9.78M | } |
548 | 43.2k | } |
549 | | |
550 | | /* if there were partial blocks, initialize bitstream for |
551 | | * unpacking fragment codings */ |
552 | 38.0k | if (num_partial_superblocks) { |
553 | 24.0k | current_run = 0; |
554 | 24.0k | bit = get_bits1(gb); |
555 | | /* toggle the bit because as soon as the first run length is |
556 | | * fetched the bit will be toggled again */ |
557 | 24.0k | bit ^= 1; |
558 | 24.0k | } |
559 | 38.0k | } |
560 | | |
561 | | /* figure out which fragments are coded; iterate through each |
562 | | * superblock (all planes) */ |
563 | 136k | s->total_num_coded_frags = 0; |
564 | 136k | memset(s->macroblock_coding, MODE_COPY, s->macroblock_count); |
565 | | |
566 | 136k | s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list |
567 | 136k | : s->nkf_coded_fragment_list; |
568 | | |
569 | 543k | for (int plane = 0; plane < 3; plane++) { |
570 | 408k | int sb_start = superblock_starts[plane]; |
571 | 408k | int sb_end = sb_start + (plane ? s->c_superblock_count |
572 | 408k | : s->y_superblock_count); |
573 | 408k | int num_coded_frags = 0; |
574 | | |
575 | 408k | if (s->keyframe) { |
576 | 294k | if (s->num_kf_coded_fragment[plane] == -1) { |
577 | 12.4M | for (int i = sb_start; i < sb_end; i++) { |
578 | | /* iterate through all 16 fragments in a superblock */ |
579 | 212M | for (int j = 0; j < 16; j++) { |
580 | | /* if the fragment is in bounds, check its coding status */ |
581 | 199M | current_fragment = s->superblock_fragments[i * 16 + j]; |
582 | 199M | if (current_fragment != -1) { |
583 | 92.4M | s->coded_fragment_list[plane][num_coded_frags++] = |
584 | 92.4M | current_fragment; |
585 | 92.4M | } |
586 | 199M | } |
587 | 12.4M | } |
588 | 9.33k | s->num_kf_coded_fragment[plane] = num_coded_frags; |
589 | 9.33k | } else |
590 | 285k | num_coded_frags = s->num_kf_coded_fragment[plane]; |
591 | 294k | } else { |
592 | 27.8M | for (int i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) { |
593 | 27.7M | if (get_bits_left(gb) < plane0_num_coded_frags >> 2) { |
594 | 1.24k | return AVERROR_INVALIDDATA; |
595 | 1.24k | } |
596 | | /* iterate through all 16 fragments in a superblock */ |
597 | 470M | for (int j = 0; j < 16; j++) { |
598 | | /* if the fragment is in bounds, check its coding status */ |
599 | 443M | current_fragment = s->superblock_fragments[i * 16 + j]; |
600 | 443M | if (current_fragment != -1) { |
601 | 190M | int coded = s->superblock_coding[i]; |
602 | | |
603 | 190M | if (coded == SB_PARTIALLY_CODED) { |
604 | | /* fragment may or may not be coded; this is the case |
605 | | * that cares about the fragment coding runs */ |
606 | 102M | if (current_run-- == 0) { |
607 | 70.6M | bit ^= 1; |
608 | 70.6M | current_run = get_vlc2(gb, fragment_run_length_vlc, 5, 2); |
609 | 70.6M | } |
610 | 102M | coded = bit; |
611 | 102M | } |
612 | | |
613 | 190M | if (coded) { |
614 | | /* default mode; actual mode will be decoded in |
615 | | * the next phase */ |
616 | 94.7M | s->all_fragments[current_fragment].coding_method = |
617 | 94.7M | MODE_INTER_NO_MV; |
618 | 94.7M | s->coded_fragment_list[plane][num_coded_frags++] = |
619 | 94.7M | current_fragment; |
620 | 96.0M | } else { |
621 | | /* not coded; copy this fragment from the prior frame */ |
622 | 96.0M | s->all_fragments[current_fragment].coding_method = |
623 | 96.0M | MODE_COPY; |
624 | 96.0M | } |
625 | 190M | } |
626 | 443M | } |
627 | 27.7M | } |
628 | 113k | } |
629 | 406k | if (!plane) |
630 | 136k | plane0_num_coded_frags = num_coded_frags; |
631 | 406k | s->total_num_coded_frags += num_coded_frags; |
632 | 26.4M | for (int i = 0; i < 64; i++) |
633 | 26.0M | s->num_coded_frags[plane][i] = num_coded_frags; |
634 | 406k | if (plane < 2) |
635 | 271k | s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] + |
636 | 271k | num_coded_frags; |
637 | 406k | } |
638 | 135k | return 0; |
639 | 136k | } |
640 | | |
641 | 606M | #define BLOCK_X (2 * mb_x + (k & 1)) |
642 | 606M | #define BLOCK_Y (2 * mb_y + (k >> 1)) |
643 | | |
644 | | #if CONFIG_VP4_DECODER |
645 | | /** |
646 | | * @return number of blocks, or > yuv_macroblock_count on error. |
647 | | * return value is always >= 1. |
648 | | */ |
649 | | static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb) |
650 | 33.0M | { |
651 | 33.0M | int v = 1; |
652 | 33.0M | int bits; |
653 | 33.1M | while ((bits = show_bits(gb, 9)) == 0x1ff) { |
654 | 69.2k | skip_bits(gb, 9); |
655 | 69.2k | v += 256; |
656 | 69.2k | if (v > s->yuv_macroblock_count) { |
657 | 2.63k | av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n"); |
658 | 2.63k | return v; |
659 | 2.63k | } |
660 | 69.2k | } |
661 | 33.0M | #define body(n) { \ |
662 | 1.21M | skip_bits(gb, 2 + n); \ |
663 | 1.21M | v += (1 << n) + get_bits(gb, n); } |
664 | 33.0M | #define thresh(n) (0x200 - (0x80 >> n)) |
665 | 33.0M | #define else_if(n) else if (bits < thresh(n)) body(n) |
666 | 33.0M | if (bits < 0x100) { |
667 | 30.2M | skip_bits(gb, 1); |
668 | 30.2M | } else if (bits < thresh(0)) { |
669 | 1.61M | skip_bits(gb, 2); |
670 | 1.61M | v += 1; |
671 | 1.61M | } |
672 | 2.83M | else_if(1) |
673 | 1.21M | else_if(2) |
674 | 492k | else_if(3) |
675 | 232k | else_if(4) |
676 | 107k | else_if(5) |
677 | 57.7k | else_if(6) |
678 | 13.6k | else body(7) |
679 | 33.0M | #undef body |
680 | 33.0M | #undef thresh |
681 | 33.0M | #undef else_if |
682 | 33.0M | return v; |
683 | 33.0M | } |
684 | | |
685 | | static int vp4_get_block_pattern(GetBitContext *gb, int *next_block_pattern_table) |
686 | 6.57M | { |
687 | 6.57M | int v = get_vlc2(gb, block_pattern_vlc[*next_block_pattern_table], 5, 1); |
688 | 6.57M | *next_block_pattern_table = vp4_block_pattern_table_selector[v]; |
689 | 6.57M | return v + 1; |
690 | 6.57M | } |
691 | | |
692 | | static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb) |
693 | 62.9k | { |
694 | 62.9k | int fragment; |
695 | 62.9k | int next_block_pattern_table; |
696 | 62.9k | int bit, current_run, has_partial; |
697 | | |
698 | 62.9k | memset(s->macroblock_coding, MODE_COPY, s->macroblock_count); |
699 | | |
700 | 62.9k | if (s->keyframe) |
701 | 26.3k | return 0; |
702 | | |
703 | 36.6k | has_partial = 0; |
704 | 36.6k | bit = get_bits1(gb); |
705 | 21.8M | for (int i = 0; i < s->yuv_macroblock_count; i += current_run) { |
706 | 21.8M | if (get_bits_left(gb) <= 0) |
707 | 7.93k | return AVERROR_INVALIDDATA; |
708 | 21.8M | current_run = vp4_get_mb_count(s, gb); |
709 | 21.8M | if (current_run > s->yuv_macroblock_count - i) |
710 | 5.47k | return -1; |
711 | 21.8M | memset(s->superblock_coding + i, 2 * bit, current_run); |
712 | 21.8M | bit ^= 1; |
713 | 21.8M | has_partial |= bit; |
714 | 21.8M | } |
715 | | |
716 | 23.2k | if (has_partial) { |
717 | 21.1k | if (get_bits_left(gb) <= 0) |
718 | 843 | return AVERROR_INVALIDDATA; |
719 | 20.2k | bit = get_bits1(gb); |
720 | 20.2k | current_run = vp4_get_mb_count(s, gb); |
721 | 35.5M | for (int i = 0; i < s->yuv_macroblock_count; i++) { |
722 | 35.5M | if (!s->superblock_coding[i]) { |
723 | 17.8M | if (!current_run) { |
724 | 11.1M | bit ^= 1; |
725 | 11.1M | current_run = vp4_get_mb_count(s, gb); |
726 | 11.1M | } |
727 | 17.8M | s->superblock_coding[i] = bit; |
728 | 17.8M | current_run--; |
729 | 17.8M | } |
730 | 35.5M | } |
731 | 20.2k | if (current_run) /* handle situation when vp4_get_mb_count() fails */ |
732 | 2.25k | return -1; |
733 | 20.2k | } |
734 | | |
735 | 20.1k | next_block_pattern_table = 0; |
736 | 80.5k | for (int plane = 0, i = 0; plane < 3; plane++) { |
737 | 60.4k | int sb_width = plane ? s->c_superblock_width : s->y_superblock_width; |
738 | 60.4k | int sb_height = plane ? s->c_superblock_height : s->y_superblock_height; |
739 | 60.4k | int mb_width = plane ? s->c_macroblock_width : s->macroblock_width; |
740 | 60.4k | int mb_height = plane ? s->c_macroblock_height : s->macroblock_height; |
741 | 60.4k | int fragment_width = s->fragment_width[!!plane]; |
742 | 60.4k | int fragment_height = s->fragment_height[!!plane]; |
743 | | |
744 | 853k | for (int sb_y = 0; sb_y < sb_height; sb_y++) { |
745 | 11.1M | for (int sb_x = 0; sb_x < sb_width; sb_x++) { |
746 | 51.6M | for (int j = 0; j < 4; j++) { |
747 | 41.3M | int mb_x = 2 * sb_x + (j >> 1); |
748 | 41.3M | int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1); |
749 | 41.3M | int mb_coded, pattern, coded; |
750 | | |
751 | 41.3M | if (mb_x >= mb_width || mb_y >= mb_height) |
752 | 14.9M | continue; |
753 | | |
754 | 26.4M | mb_coded = s->superblock_coding[i++]; |
755 | | |
756 | 26.4M | if (mb_coded == SB_FULLY_CODED) |
757 | 13.3M | pattern = 0xF; |
758 | 13.0M | else if (mb_coded == SB_PARTIALLY_CODED) |
759 | 6.57M | pattern = vp4_get_block_pattern(gb, &next_block_pattern_table); |
760 | 6.47M | else |
761 | 6.47M | pattern = 0; |
762 | | |
763 | 132M | for (int k = 0; k < 4; k++) { |
764 | 105M | if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height) |
765 | 14.6M | continue; |
766 | 90.9M | fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X; |
767 | 90.9M | coded = pattern & (8 >> k); |
768 | | /* MODE_INTER_NO_MV is the default for coded fragments. |
769 | | the actual method is decoded in the next phase. */ |
770 | 90.9M | s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY; |
771 | 90.9M | } |
772 | 26.4M | } |
773 | 10.3M | } |
774 | 792k | } |
775 | 60.4k | } |
776 | 20.1k | return 0; |
777 | 23.2k | } |
778 | | #endif |
779 | | |
780 | | /* |
781 | | * This function unpacks all the coding mode data for individual macroblocks |
782 | | * from the bitstream. |
783 | | */ |
784 | | static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) |
785 | 181k | { |
786 | 181k | int scheme; |
787 | 181k | int current_macroblock; |
788 | 181k | int current_fragment; |
789 | 181k | int coding_mode; |
790 | 181k | int custom_mode_alphabet[CODING_MODE_COUNT]; |
791 | 181k | const int *alphabet; |
792 | 181k | Vp3Fragment *frag; |
793 | | |
794 | 181k | if (s->keyframe) { |
795 | 404M | for (int i = 0; i < s->fragment_count; i++) |
796 | 404M | s->all_fragments[i].coding_method = MODE_INTRA; |
797 | 124k | } else { |
798 | | /* fetch the mode coding scheme for this frame */ |
799 | 56.9k | scheme = get_bits(gb, 3); |
800 | | |
801 | | /* is it a custom coding scheme? */ |
802 | 56.9k | if (scheme == 0) { |
803 | 236k | for (int i = 0; i < 8; i++) |
804 | 210k | custom_mode_alphabet[i] = MODE_INTER_NO_MV; |
805 | 236k | for (int i = 0; i < 8; i++) |
806 | 210k | custom_mode_alphabet[get_bits(gb, 3)] = i; |
807 | 26.2k | alphabet = custom_mode_alphabet; |
808 | 26.2k | } else |
809 | 30.7k | alphabet = ModeAlphabet[scheme - 1]; |
810 | | |
811 | | /* iterate through all of the macroblocks that contain 1 or more |
812 | | * coded fragments */ |
813 | 773k | for (int sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { |
814 | 19.2M | for (int sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { |
815 | 18.5M | if (get_bits_left(gb) <= 0) |
816 | 19.8k | return -1; |
817 | | |
818 | 92.6M | for (int j = 0; j < 4; j++) { |
819 | 74.1M | int k; |
820 | 74.1M | int mb_x = 2 * sb_x + (j >> 1); |
821 | 74.1M | int mb_y = 2 * sb_y + (((j >> 1) + j) & 1); |
822 | 74.1M | current_macroblock = mb_y * s->macroblock_width + mb_x; |
823 | | |
824 | 74.1M | if (mb_x >= s->macroblock_width || |
825 | 73.7M | mb_y >= s->macroblock_height) |
826 | 30.6M | continue; |
827 | | |
828 | | /* coding modes are only stored if the macroblock has |
829 | | * at least one luma block coded, otherwise it must be |
830 | | * INTER_NO_MV */ |
831 | 102M | for (k = 0; k < 4; k++) { |
832 | 91.0M | current_fragment = BLOCK_Y * |
833 | 91.0M | s->fragment_width[0] + BLOCK_X; |
834 | 91.0M | if (s->all_fragments[current_fragment].coding_method != MODE_COPY) |
835 | 32.4M | break; |
836 | 91.0M | } |
837 | 43.5M | if (k == 4) { |
838 | 11.0M | s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV; |
839 | 11.0M | continue; |
840 | 11.0M | } |
841 | | |
842 | | /* mode 7 means get 3 bits for each coding mode */ |
843 | 32.4M | if (scheme == 7) |
844 | 5.75M | coding_mode = get_bits(gb, 3); |
845 | 26.6M | else |
846 | 26.6M | coding_mode = alphabet[get_vlc2(gb, mode_code_vlc, 4, 2)]; |
847 | | |
848 | 32.4M | s->macroblock_coding[current_macroblock] = coding_mode; |
849 | 162M | for (k = 0; k < 4; k++) { |
850 | 129M | frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X; |
851 | 129M | if (frag->coding_method != MODE_COPY) |
852 | 92.9M | frag->coding_method = coding_mode; |
853 | 129M | } |
854 | | |
855 | 32.4M | #define SET_CHROMA_MODES \ |
856 | 32.7M | if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \ |
857 | 32.7M | frag[s->fragment_start[1]].coding_method = coding_mode; \ |
858 | 32.7M | if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \ |
859 | 32.7M | frag[s->fragment_start[2]].coding_method = coding_mode; |
860 | | |
861 | 32.4M | if (s->chroma_y_shift) { |
862 | 32.2M | frag = s->all_fragments + mb_y * |
863 | 32.2M | s->fragment_width[1] + mb_x; |
864 | 32.2M | SET_CHROMA_MODES |
865 | 32.2M | } else if (s->chroma_x_shift) { |
866 | 98.8k | frag = s->all_fragments + |
867 | 98.8k | 2 * mb_y * s->fragment_width[1] + mb_x; |
868 | 296k | for (k = 0; k < 2; k++) { |
869 | 197k | SET_CHROMA_MODES |
870 | 197k | frag += s->fragment_width[1]; |
871 | 197k | } |
872 | 98.8k | } else { |
873 | 430k | for (k = 0; k < 4; k++) { |
874 | 344k | frag = s->all_fragments + |
875 | 344k | BLOCK_Y * s->fragment_width[1] + BLOCK_X; |
876 | 344k | SET_CHROMA_MODES |
877 | 344k | } |
878 | 86.0k | } |
879 | 32.4M | } |
880 | 18.5M | } |
881 | 735k | } |
882 | 56.9k | } |
883 | | |
884 | 161k | return 0; |
885 | 181k | } |
886 | | |
887 | | static int vp4_get_mv(GetBitContext *gb, int axis, int last_motion) |
888 | 24.4M | { |
889 | 24.4M | #if CONFIG_VP4_DECODER |
890 | 24.4M | int v = get_vlc2(gb, vp4_mv_vlc_table[axis][vp4_mv_table_selector[FFABS(last_motion)]], |
891 | 24.4M | VP4_MV_VLC_BITS, 2); |
892 | 24.4M | return last_motion < 0 ? -v : v; |
893 | | #else |
894 | | return 0; |
895 | | #endif |
896 | 24.4M | } |
897 | | |
898 | | /* |
899 | | * This function unpacks all the motion vectors for the individual |
900 | | * macroblocks from the bitstream. |
901 | | */ |
902 | | static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) |
903 | 161k | { |
904 | 161k | int coding_mode; |
905 | 161k | int motion_x[4]; |
906 | 161k | int motion_y[4]; |
907 | 161k | int last_motion_x = 0; |
908 | 161k | int last_motion_y = 0; |
909 | 161k | int prior_last_motion_x = 0; |
910 | 161k | int prior_last_motion_y = 0; |
911 | 161k | int last_gold_motion_x = 0; |
912 | 161k | int last_gold_motion_y = 0; |
913 | 161k | int current_macroblock; |
914 | 161k | int current_fragment; |
915 | 161k | int frag; |
916 | | |
917 | 161k | if (s->keyframe) |
918 | 124k | return 0; |
919 | | |
920 | | /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */ |
921 | 37.1k | coding_mode = s->version < 2 ? get_bits1(gb) : 2; |
922 | | |
923 | | /* iterate through all of the macroblocks that contain 1 or more |
924 | | * coded fragments */ |
925 | 652k | for (int sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { |
926 | 16.3M | for (int sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { |
927 | 15.7M | if (get_bits_left(gb) <= 0) |
928 | 8.69k | return -1; |
929 | | |
930 | 78.7M | for (int j = 0; j < 4; j++) { |
931 | 63.0M | int mb_x = 2 * sb_x + (j >> 1); |
932 | 63.0M | int mb_y = 2 * sb_y + (((j >> 1) + j) & 1); |
933 | 63.0M | current_macroblock = mb_y * s->macroblock_width + mb_x; |
934 | | |
935 | 63.0M | if (mb_x >= s->macroblock_width || |
936 | 62.6M | mb_y >= s->macroblock_height || |
937 | 36.7M | s->macroblock_coding[current_macroblock] == MODE_COPY) |
938 | 26.2M | continue; |
939 | | |
940 | 36.7M | switch (s->macroblock_coding[current_macroblock]) { |
941 | 2.11M | case MODE_GOLDEN_MV: |
942 | 2.11M | if (coding_mode == 2) { /* VP4 */ |
943 | 1.14M | last_gold_motion_x = motion_x[0] = vp4_get_mv(gb, 0, last_gold_motion_x); |
944 | 1.14M | last_gold_motion_y = motion_y[0] = vp4_get_mv(gb, 1, last_gold_motion_y); |
945 | 1.14M | break; |
946 | 1.14M | } /* otherwise fall through */ |
947 | 2.94M | case MODE_INTER_PLUS_MV: |
948 | | /* all 6 fragments use the same motion vector */ |
949 | 2.94M | if (coding_mode == 0) { |
950 | 1.64M | motion_x[0] = get_vlc2(gb, motion_vector_vlc, |
951 | 1.64M | VP3_MV_VLC_BITS, 2); |
952 | 1.64M | motion_y[0] = get_vlc2(gb, motion_vector_vlc, |
953 | 1.64M | VP3_MV_VLC_BITS, 2); |
954 | 1.64M | } else if (coding_mode == 1) { |
955 | 851k | motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; |
956 | 851k | motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)]; |
957 | 851k | } else { /* VP4 */ |
958 | 450k | motion_x[0] = vp4_get_mv(gb, 0, last_motion_x); |
959 | 450k | motion_y[0] = vp4_get_mv(gb, 1, last_motion_y); |
960 | 450k | } |
961 | | |
962 | | /* vector maintenance, only on MODE_INTER_PLUS_MV */ |
963 | 2.94M | if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) { |
964 | 1.97M | prior_last_motion_x = last_motion_x; |
965 | 1.97M | prior_last_motion_y = last_motion_y; |
966 | 1.97M | last_motion_x = motion_x[0]; |
967 | 1.97M | last_motion_y = motion_y[0]; |
968 | 1.97M | } |
969 | 2.94M | break; |
970 | | |
971 | 10.4M | case MODE_INTER_FOURMV: |
972 | | /* vector maintenance */ |
973 | 10.4M | prior_last_motion_x = last_motion_x; |
974 | 10.4M | prior_last_motion_y = last_motion_y; |
975 | | |
976 | | /* fetch 4 vectors from the bitstream, one for each |
977 | | * Y fragment, then average for the C fragment vectors */ |
978 | 52.1M | for (int k = 0; k < 4; k++) { |
979 | 41.7M | current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X; |
980 | 41.7M | if (s->all_fragments[current_fragment].coding_method != MODE_COPY) { |
981 | 29.6M | if (coding_mode == 0) { |
982 | 14.7M | motion_x[k] = get_vlc2(gb, motion_vector_vlc, |
983 | 14.7M | VP3_MV_VLC_BITS, 2); |
984 | 14.7M | motion_y[k] = get_vlc2(gb, motion_vector_vlc, |
985 | 14.7M | VP3_MV_VLC_BITS, 2); |
986 | 14.8M | } else if (coding_mode == 1) { |
987 | 4.22M | motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; |
988 | 4.22M | motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; |
989 | 10.6M | } else { /* VP4 */ |
990 | 10.6M | motion_x[k] = vp4_get_mv(gb, 0, prior_last_motion_x); |
991 | 10.6M | motion_y[k] = vp4_get_mv(gb, 1, prior_last_motion_y); |
992 | 10.6M | } |
993 | 29.6M | last_motion_x = motion_x[k]; |
994 | 29.6M | last_motion_y = motion_y[k]; |
995 | 29.6M | } else { |
996 | 12.1M | motion_x[k] = 0; |
997 | 12.1M | motion_y[k] = 0; |
998 | 12.1M | } |
999 | 41.7M | } |
1000 | 10.4M | break; |
1001 | | |
1002 | 4.81M | case MODE_INTER_LAST_MV: |
1003 | | /* all 6 fragments use the last motion vector */ |
1004 | 4.81M | motion_x[0] = last_motion_x; |
1005 | 4.81M | motion_y[0] = last_motion_y; |
1006 | | |
1007 | | /* no vector maintenance (last vector remains the |
1008 | | * last vector) */ |
1009 | 4.81M | break; |
1010 | | |
1011 | 1.32M | case MODE_INTER_PRIOR_LAST: |
1012 | | /* all 6 fragments use the motion vector prior to the |
1013 | | * last motion vector */ |
1014 | 1.32M | motion_x[0] = prior_last_motion_x; |
1015 | 1.32M | motion_y[0] = prior_last_motion_y; |
1016 | | |
1017 | | /* vector maintenance */ |
1018 | 1.32M | prior_last_motion_x = last_motion_x; |
1019 | 1.32M | prior_last_motion_y = last_motion_y; |
1020 | 1.32M | last_motion_x = motion_x[0]; |
1021 | 1.32M | last_motion_y = motion_y[0]; |
1022 | 1.32M | break; |
1023 | | |
1024 | 16.0M | default: |
1025 | | /* covers intra, inter without MV, golden without MV */ |
1026 | 16.0M | motion_x[0] = 0; |
1027 | 16.0M | motion_y[0] = 0; |
1028 | | |
1029 | | /* no vector maintenance */ |
1030 | 16.0M | break; |
1031 | 36.7M | } |
1032 | | |
1033 | | /* assign the motion vectors to the correct fragments */ |
1034 | 183M | for (int k = 0; k < 4; k++) { |
1035 | 146M | current_fragment = |
1036 | 146M | BLOCK_Y * s->fragment_width[0] + BLOCK_X; |
1037 | 146M | if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
1038 | 41.7M | s->motion_val[0][current_fragment][0] = motion_x[k]; |
1039 | 41.7M | s->motion_val[0][current_fragment][1] = motion_y[k]; |
1040 | 105M | } else { |
1041 | 105M | s->motion_val[0][current_fragment][0] = motion_x[0]; |
1042 | 105M | s->motion_val[0][current_fragment][1] = motion_y[0]; |
1043 | 105M | } |
1044 | 146M | } |
1045 | | |
1046 | 36.7M | if (s->chroma_y_shift) { |
1047 | 36.5M | if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
1048 | 10.4M | motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + |
1049 | 10.4M | motion_x[2] + motion_x[3], 2); |
1050 | 10.4M | motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + |
1051 | 10.4M | motion_y[2] + motion_y[3], 2); |
1052 | 10.4M | } |
1053 | 36.5M | if (s->version <= 2) { |
1054 | 25.7M | motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1); |
1055 | 25.7M | motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1); |
1056 | 25.7M | } |
1057 | 36.5M | frag = mb_y * s->fragment_width[1] + mb_x; |
1058 | 36.5M | s->motion_val[1][frag][0] = motion_x[0]; |
1059 | 36.5M | s->motion_val[1][frag][1] = motion_y[0]; |
1060 | 36.5M | } else if (s->chroma_x_shift) { |
1061 | 93.0k | if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
1062 | 12.8k | motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1); |
1063 | 12.8k | motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1); |
1064 | 12.8k | motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1); |
1065 | 12.8k | motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1); |
1066 | 80.2k | } else { |
1067 | 80.2k | motion_x[1] = motion_x[0]; |
1068 | 80.2k | motion_y[1] = motion_y[0]; |
1069 | 80.2k | } |
1070 | 93.0k | if (s->version <= 2) { |
1071 | 77.1k | motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1); |
1072 | 77.1k | motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1); |
1073 | 77.1k | } |
1074 | 93.0k | frag = 2 * mb_y * s->fragment_width[1] + mb_x; |
1075 | 279k | for (int k = 0; k < 2; k++) { |
1076 | 186k | s->motion_val[1][frag][0] = motion_x[k]; |
1077 | 186k | s->motion_val[1][frag][1] = motion_y[k]; |
1078 | 186k | frag += s->fragment_width[1]; |
1079 | 186k | } |
1080 | 93.0k | } else { |
1081 | 229k | for (int k = 0; k < 4; k++) { |
1082 | 183k | frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X; |
1083 | 183k | if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
1084 | 9.59k | s->motion_val[1][frag][0] = motion_x[k]; |
1085 | 9.59k | s->motion_val[1][frag][1] = motion_y[k]; |
1086 | 174k | } else { |
1087 | 174k | s->motion_val[1][frag][0] = motion_x[0]; |
1088 | 174k | s->motion_val[1][frag][1] = motion_y[0]; |
1089 | 174k | } |
1090 | 183k | } |
1091 | 45.9k | } |
1092 | 36.7M | } |
1093 | 15.7M | } |
1094 | 624k | } |
1095 | | |
1096 | 28.4k | return 0; |
1097 | 37.1k | } |
1098 | | |
1099 | | static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) |
1100 | 153k | { |
1101 | 153k | int num_blocks = s->total_num_coded_frags; |
1102 | | |
1103 | 157k | for (int qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) { |
1104 | 7.99k | int i = 0, blocks_decoded = 0, num_blocks_at_qpi = 0; |
1105 | 7.99k | int bit, run_length; |
1106 | | |
1107 | 7.99k | bit = get_bits1(gb) ^ 1; |
1108 | 7.99k | run_length = 0; |
1109 | | |
1110 | 468k | do { |
1111 | 468k | if (run_length == MAXIMUM_LONG_BIT_RUN) |
1112 | 387 | bit = get_bits1(gb); |
1113 | 468k | else |
1114 | 468k | bit ^= 1; |
1115 | | |
1116 | 468k | run_length = get_vlc2(gb, superblock_run_length_vlc, |
1117 | 468k | SUPERBLOCK_VLC_BITS, 2); |
1118 | 468k | if (run_length == 34) |
1119 | 9.99k | run_length += get_bits(gb, 12); |
1120 | 468k | blocks_decoded += run_length; |
1121 | | |
1122 | 468k | if (!bit) |
1123 | 234k | num_blocks_at_qpi += run_length; |
1124 | | |
1125 | 26.6M | for (int j = 0; j < run_length; i++) { |
1126 | 26.1M | if (i >= s->total_num_coded_frags) |
1127 | 3.34k | return -1; |
1128 | | |
1129 | 26.1M | if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) { |
1130 | 25.4M | s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit; |
1131 | 25.4M | j++; |
1132 | 25.4M | } |
1133 | 26.1M | } |
1134 | 468k | } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0); |
1135 | | |
1136 | 4.65k | num_blocks -= num_blocks_at_qpi; |
1137 | 4.65k | } |
1138 | | |
1139 | 149k | return 0; |
1140 | 153k | } |
1141 | | |
1142 | | static inline int get_eob_run(GetBitContext *gb, int token) |
1143 | 12.8M | { |
1144 | 12.8M | int v = eob_run_table[token].base; |
1145 | 12.8M | if (eob_run_table[token].bits) |
1146 | 1.00M | v += get_bits(gb, eob_run_table[token].bits); |
1147 | 12.8M | return v; |
1148 | 12.8M | } |
1149 | | |
1150 | | static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff) |
1151 | 216M | { |
1152 | 216M | int bits_to_get, zero_run; |
1153 | | |
1154 | 216M | bits_to_get = coeff_get_bits[token]; |
1155 | 216M | if (bits_to_get) |
1156 | 122M | bits_to_get = get_bits(gb, bits_to_get); |
1157 | 216M | *coeff = coeff_tables[token][bits_to_get]; |
1158 | | |
1159 | 216M | zero_run = zero_run_base[token]; |
1160 | 216M | if (zero_run_get_bits[token]) |
1161 | 10.5M | zero_run += get_bits(gb, zero_run_get_bits[token]); |
1162 | | |
1163 | 216M | return zero_run; |
1164 | 216M | } |
1165 | | |
1166 | | /* |
1167 | | * This function is called by unpack_dct_coeffs() to extract the VLCs from |
1168 | | * the bitstream. The VLCs encode tokens which are used to unpack DCT |
1169 | | * data. This function unpacks all the VLCs for either the Y plane or both |
1170 | | * C planes, and is called for DC coefficients or different AC coefficient |
1171 | | * levels (since different coefficient types require different VLC tables. |
1172 | | * |
1173 | | * This function returns a residual eob run. E.g, if a particular token gave |
1174 | | * instructions to EOB the next 5 fragments and there were only 2 fragments |
1175 | | * left in the current fragment range, 3 would be returned so that it could |
1176 | | * be passed into the next call to this same function. |
1177 | | */ |
1178 | | static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, |
1179 | | const VLCElem *vlc_table, int coeff_index, |
1180 | | int plane, |
1181 | | int eob_run) |
1182 | 12.2M | { |
1183 | 12.2M | int j = 0; |
1184 | 12.2M | int token; |
1185 | 12.2M | int zero_run = 0; |
1186 | 12.2M | int16_t coeff = 0; |
1187 | 12.2M | int blocks_ended; |
1188 | 12.2M | int coeff_i = 0; |
1189 | 12.2M | int num_coeffs = s->num_coded_frags[plane][coeff_index]; |
1190 | 12.2M | int16_t *dct_tokens = s->dct_tokens[plane][coeff_index]; |
1191 | | |
1192 | | /* local references to structure members to avoid repeated dereferences */ |
1193 | 12.2M | const int *coded_fragment_list = s->coded_fragment_list[plane]; |
1194 | 12.2M | Vp3Fragment *all_fragments = s->all_fragments; |
1195 | | |
1196 | 12.2M | if (num_coeffs < 0) { |
1197 | 0 | av_log(s->avctx, AV_LOG_ERROR, |
1198 | 0 | "Invalid number of coefficients at level %d\n", coeff_index); |
1199 | 0 | return AVERROR_INVALIDDATA; |
1200 | 0 | } |
1201 | | |
1202 | 12.2M | if (eob_run > num_coeffs) { |
1203 | 4.00M | coeff_i = |
1204 | 4.00M | blocks_ended = num_coeffs; |
1205 | 4.00M | eob_run -= num_coeffs; |
1206 | 8.23M | } else { |
1207 | 8.23M | coeff_i = |
1208 | 8.23M | blocks_ended = eob_run; |
1209 | 8.23M | eob_run = 0; |
1210 | 8.23M | } |
1211 | | |
1212 | | // insert fake EOB token to cover the split between planes or zzi |
1213 | 12.2M | if (blocks_ended) |
1214 | 121k | dct_tokens[j++] = blocks_ended << 2; |
1215 | | |
1216 | 113M | while (coeff_i < num_coeffs && get_bits_left(gb) > 0) { |
1217 | | /* decode a VLC into a token */ |
1218 | 101M | token = get_vlc2(gb, vlc_table, 11, 3); |
1219 | | /* use the token to get a zero run, a coefficient, and an eob run */ |
1220 | 101M | if ((unsigned) token <= 6U) { |
1221 | 5.31M | eob_run = get_eob_run(gb, token); |
1222 | 5.31M | if (!eob_run) |
1223 | 1.24k | eob_run = INT_MAX; |
1224 | | |
1225 | | // record only the number of blocks ended in this plane, |
1226 | | // any spill will be recorded in the next plane. |
1227 | 5.31M | if (eob_run > num_coeffs - coeff_i) { |
1228 | 83.4k | dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i); |
1229 | 83.4k | blocks_ended += num_coeffs - coeff_i; |
1230 | 83.4k | eob_run -= num_coeffs - coeff_i; |
1231 | 83.4k | coeff_i = num_coeffs; |
1232 | 5.22M | } else { |
1233 | 5.22M | dct_tokens[j++] = TOKEN_EOB(eob_run); |
1234 | 5.22M | blocks_ended += eob_run; |
1235 | 5.22M | coeff_i += eob_run; |
1236 | 5.22M | eob_run = 0; |
1237 | 5.22M | } |
1238 | 96.0M | } else if (token >= 0) { |
1239 | 96.0M | zero_run = get_coeff(gb, token, &coeff); |
1240 | | |
1241 | 96.0M | if (zero_run) { |
1242 | 11.3M | dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run); |
1243 | 84.6M | } else { |
1244 | | // Save DC into the fragment structure. DC prediction is |
1245 | | // done in raster order, so the actual DC can't be in with |
1246 | | // other tokens. We still need the token in dct_tokens[] |
1247 | | // however, or else the structure collapses on itself. |
1248 | 84.6M | if (!coeff_index) |
1249 | 45.3M | all_fragments[coded_fragment_list[coeff_i]].dc = coeff; |
1250 | | |
1251 | 84.6M | dct_tokens[j++] = TOKEN_COEFF(coeff); |
1252 | 84.6M | } |
1253 | | |
1254 | 96.0M | if (coeff_index + zero_run > 64) { |
1255 | 13.1k | av_log(s->avctx, AV_LOG_DEBUG, |
1256 | 13.1k | "Invalid zero run of %d with %d coeffs left\n", |
1257 | 13.1k | zero_run, 64 - coeff_index); |
1258 | 13.1k | zero_run = 64 - coeff_index; |
1259 | 13.1k | } |
1260 | | |
1261 | | // zero runs code multiple coefficients, |
1262 | | // so don't try to decode coeffs for those higher levels |
1263 | 133M | for (int i = coeff_index + 1; i <= coeff_index + zero_run; i++) |
1264 | 37.0M | s->num_coded_frags[plane][i]--; |
1265 | 96.0M | coeff_i++; |
1266 | 96.0M | } else { |
1267 | 2.02k | av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token); |
1268 | 2.02k | return -1; |
1269 | 2.02k | } |
1270 | 101M | } |
1271 | | |
1272 | 12.2M | if (blocks_ended > s->num_coded_frags[plane][coeff_index]) |
1273 | 0 | av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n"); |
1274 | | |
1275 | | // decrement the number of blocks that have higher coefficients for each |
1276 | | // EOB run at this level |
1277 | 12.2M | if (blocks_ended) |
1278 | 17.6M | for (int i = coeff_index + 1; i < 64; i++) |
1279 | 17.4M | s->num_coded_frags[plane][i] -= blocks_ended; |
1280 | | |
1281 | | // setup the next buffer |
1282 | 12.2M | if (plane < 2) |
1283 | 8.16M | s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j; |
1284 | 4.07M | else if (coeff_index < 63) |
1285 | 4.00M | s->dct_tokens[0][coeff_index + 1] = dct_tokens + j; |
1286 | | |
1287 | 12.2M | return eob_run; |
1288 | 12.2M | } |
1289 | | |
1290 | | static void reverse_dc_prediction(Vp3DecodeContext *s, |
1291 | | int first_fragment, |
1292 | | int fragment_width, |
1293 | | int fragment_height); |
1294 | | /* |
1295 | | * This function unpacks all of the DCT coefficient data from the |
1296 | | * bitstream. |
1297 | | */ |
1298 | | static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) |
1299 | 108k | { |
1300 | 108k | const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs; |
1301 | 108k | int dc_y_table; |
1302 | 108k | int dc_c_table; |
1303 | 108k | int ac_y_table; |
1304 | 108k | int ac_c_table; |
1305 | 108k | int residual_eob_run = 0; |
1306 | 108k | const VLCElem *y_tables[64], *c_tables[64]; |
1307 | | |
1308 | 108k | s->dct_tokens[0][0] = s->dct_tokens_base; |
1309 | | |
1310 | 108k | if (get_bits_left(gb) < 16) |
1311 | 16.5k | return AVERROR_INVALIDDATA; |
1312 | | |
1313 | | /* fetch the DC table indexes */ |
1314 | 92.1k | dc_y_table = get_bits(gb, 4); |
1315 | 92.1k | dc_c_table = get_bits(gb, 4); |
1316 | | |
1317 | | /* unpack the Y plane DC coefficients */ |
1318 | 92.1k | residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_y_table], 0, |
1319 | 92.1k | 0, residual_eob_run); |
1320 | 92.1k | if (residual_eob_run < 0) |
1321 | 753 | return residual_eob_run; |
1322 | 91.4k | if (get_bits_left(gb) < 8) |
1323 | 23.6k | return AVERROR_INVALIDDATA; |
1324 | | |
1325 | | /* reverse prediction of the Y-plane DC coefficients */ |
1326 | 67.7k | reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]); |
1327 | | |
1328 | | /* unpack the C plane DC coefficients */ |
1329 | 67.7k | residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0, |
1330 | 67.7k | 1, residual_eob_run); |
1331 | 67.7k | if (residual_eob_run < 0) |
1332 | 327 | return residual_eob_run; |
1333 | 67.4k | residual_eob_run = unpack_vlcs(s, gb, coeff_vlc[dc_c_table], 0, |
1334 | 67.4k | 2, residual_eob_run); |
1335 | 67.4k | if (residual_eob_run < 0) |
1336 | 254 | return residual_eob_run; |
1337 | | |
1338 | | /* reverse prediction of the C-plane DC coefficients */ |
1339 | 67.1k | if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) { |
1340 | 67.1k | reverse_dc_prediction(s, s->fragment_start[1], |
1341 | 67.1k | s->fragment_width[1], s->fragment_height[1]); |
1342 | 67.1k | reverse_dc_prediction(s, s->fragment_start[2], |
1343 | 67.1k | s->fragment_width[1], s->fragment_height[1]); |
1344 | 67.1k | } |
1345 | | |
1346 | 67.1k | if (get_bits_left(gb) < 8) |
1347 | 2.89k | return AVERROR_INVALIDDATA; |
1348 | | /* fetch the AC table indexes */ |
1349 | 64.2k | ac_y_table = get_bits(gb, 4); |
1350 | 64.2k | ac_c_table = get_bits(gb, 4); |
1351 | | |
1352 | | /* build tables of AC VLC tables */ |
1353 | 385k | for (int i = 1; i <= 5; i++) { |
1354 | | /* AC VLC table group 1 */ |
1355 | 321k | y_tables[i] = coeff_vlc[ac_y_table + 16]; |
1356 | 321k | c_tables[i] = coeff_vlc[ac_c_table + 16]; |
1357 | 321k | } |
1358 | 642k | for (int i = 6; i <= 14; i++) { |
1359 | | /* AC VLC table group 2 */ |
1360 | 578k | y_tables[i] = coeff_vlc[ac_y_table + 32]; |
1361 | 578k | c_tables[i] = coeff_vlc[ac_c_table + 32]; |
1362 | 578k | } |
1363 | 899k | for (int i = 15; i <= 27; i++) { |
1364 | | /* AC VLC table group 3 */ |
1365 | 835k | y_tables[i] = coeff_vlc[ac_y_table + 48]; |
1366 | 835k | c_tables[i] = coeff_vlc[ac_c_table + 48]; |
1367 | 835k | } |
1368 | 2.37M | for (int i = 28; i <= 63; i++) { |
1369 | | /* AC VLC table group 4 */ |
1370 | 2.31M | y_tables[i] = coeff_vlc[ac_y_table + 64]; |
1371 | 2.31M | c_tables[i] = coeff_vlc[ac_c_table + 64]; |
1372 | 2.31M | } |
1373 | | |
1374 | | /* decode all AC coefficients */ |
1375 | 4.06M | for (int i = 1; i <= 63; i++) { |
1376 | 4.00M | residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i, |
1377 | 4.00M | 0, residual_eob_run); |
1378 | 4.00M | if (residual_eob_run < 0) |
1379 | 245 | return residual_eob_run; |
1380 | | |
1381 | 4.00M | residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, |
1382 | 4.00M | 1, residual_eob_run); |
1383 | 4.00M | if (residual_eob_run < 0) |
1384 | 231 | return residual_eob_run; |
1385 | 4.00M | residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, |
1386 | 4.00M | 2, residual_eob_run); |
1387 | 4.00M | if (residual_eob_run < 0) |
1388 | 210 | return residual_eob_run; |
1389 | 4.00M | } |
1390 | | |
1391 | 63.5k | return 0; |
1392 | 64.2k | } |
1393 | | |
1394 | | #if CONFIG_VP4_DECODER |
1395 | | /** |
1396 | | * eob_tracker[] is instead of TOKEN_EOB(value) |
1397 | | * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work |
1398 | | * |
1399 | | * @return < 0 on error |
1400 | | */ |
1401 | | static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, |
1402 | | const VLCElem *const vlc_tables[64], |
1403 | | int plane, int eob_tracker[64], int fragment) |
1404 | 31.2M | { |
1405 | 31.2M | int token; |
1406 | 31.2M | int zero_run = 0; |
1407 | 31.2M | int16_t coeff = 0; |
1408 | 31.2M | int coeff_i = 0; |
1409 | 31.2M | int eob_run; |
1410 | | |
1411 | 151M | while (!eob_tracker[coeff_i]) { |
1412 | 128M | if (get_bits_left(gb) < 1) |
1413 | 25.3k | return AVERROR_INVALIDDATA; |
1414 | | |
1415 | 128M | token = get_vlc2(gb, vlc_tables[coeff_i], 11, 3); |
1416 | | |
1417 | | /* use the token to get a zero run, a coefficient, and an eob run */ |
1418 | 128M | if ((unsigned) token <= 6U) { |
1419 | 7.49M | eob_run = get_eob_run(gb, token); |
1420 | 7.49M | *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0); |
1421 | 7.49M | eob_tracker[coeff_i] = eob_run - 1; |
1422 | 7.49M | return 0; |
1423 | 120M | } else if (token >= 0) { |
1424 | 120M | zero_run = get_coeff(gb, token, &coeff); |
1425 | | |
1426 | 120M | if (zero_run) { |
1427 | 30.5M | if (coeff_i + zero_run > 64) { |
1428 | 199k | av_log(s->avctx, AV_LOG_DEBUG, |
1429 | 199k | "Invalid zero run of %d with %d coeffs left\n", |
1430 | 199k | zero_run, 64 - coeff_i); |
1431 | 199k | zero_run = 64 - coeff_i; |
1432 | 199k | } |
1433 | 30.5M | *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run); |
1434 | 30.5M | coeff_i += zero_run; |
1435 | 90.3M | } else { |
1436 | 90.3M | if (!coeff_i) |
1437 | 13.9M | s->all_fragments[fragment].dc = coeff; |
1438 | | |
1439 | 90.3M | *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff); |
1440 | 90.3M | } |
1441 | 120M | coeff_i++; |
1442 | 120M | if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */ |
1443 | 664k | return 0; /* stop */ |
1444 | 120M | } else { |
1445 | 2.00k | av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token); |
1446 | 2.00k | return -1; |
1447 | 2.00k | } |
1448 | 128M | } |
1449 | 23.0M | *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0); |
1450 | 23.0M | eob_tracker[coeff_i]--; |
1451 | 23.0M | return 0; |
1452 | 31.2M | } |
1453 | | |
1454 | | static void vp4_dc_predictor_reset(VP4Predictor *p) |
1455 | 117M | { |
1456 | 117M | p->dc = 0; |
1457 | 117M | p->type = VP4_DC_UNDEFINED; |
1458 | 117M | } |
1459 | | |
1460 | | static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x) |
1461 | 4.55M | { |
1462 | 22.7M | for (int i = 0; i < 4; i++) |
1463 | 18.2M | dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i]; |
1464 | | |
1465 | 22.7M | for (int j = 1; j < 5; j++) |
1466 | 91.1M | for (int i = 0; i < 4; i++) |
1467 | 72.9M | vp4_dc_predictor_reset(&dc_pred[j][i + 1]); |
1468 | 4.55M | } |
1469 | | |
1470 | | static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x) |
1471 | 4.53M | { |
1472 | 22.6M | for (int i = 0; i < 4; i++) |
1473 | 18.1M | s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1]; |
1474 | | |
1475 | 22.6M | for (int i = 1; i < 5; i++) |
1476 | 18.1M | dc_pred[i][0] = dc_pred[i][4]; |
1477 | 4.53M | } |
1478 | | |
1479 | | /* note: dc_pred points to the current block */ |
1480 | | static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane) |
1481 | 31.2M | { |
1482 | 31.2M | int count = 0; |
1483 | 31.2M | int dc = 0; |
1484 | | |
1485 | 31.2M | if (dc_pred[-6].type == type) { |
1486 | 11.2M | dc += dc_pred[-6].dc; |
1487 | 11.2M | count++; |
1488 | 11.2M | } |
1489 | | |
1490 | 31.2M | if (dc_pred[6].type == type) { |
1491 | 8.11M | dc += dc_pred[6].dc; |
1492 | 8.11M | count++; |
1493 | 8.11M | } |
1494 | | |
1495 | 31.2M | if (count != 2 && dc_pred[-1].type == type) { |
1496 | 18.1M | dc += dc_pred[-1].dc; |
1497 | 18.1M | count++; |
1498 | 18.1M | } |
1499 | | |
1500 | 31.2M | if (count != 2 && dc_pred[1].type == type) { |
1501 | 2.23M | dc += dc_pred[1].dc; |
1502 | 2.23M | count++; |
1503 | 2.23M | } |
1504 | | |
1505 | | /* using division instead of shift to correctly handle negative values */ |
1506 | 31.2M | return count == 2 ? dc / 2 : last_dc[type]; |
1507 | 31.2M | } |
1508 | | |
1509 | | static void vp4_set_tokens_base(Vp3DecodeContext *s) |
1510 | 37.9k | { |
1511 | 37.9k | int16_t *base = s->dct_tokens_base; |
1512 | 151k | for (int plane = 0; plane < 3; plane++) { |
1513 | 7.39M | for (int i = 0; i < 64; i++) { |
1514 | 7.28M | s->dct_tokens[plane][i] = base; |
1515 | 7.28M | base += s->fragment_width[!!plane] * s->fragment_height[!!plane]; |
1516 | 7.28M | } |
1517 | 113k | } |
1518 | 37.9k | } |
1519 | | |
1520 | | static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) |
1521 | 41.1k | { |
1522 | 41.1k | const VLCElem *const *coeff_vlc = s->coeff_vlc->vlc_tabs; |
1523 | 41.1k | int dc_y_table; |
1524 | 41.1k | int dc_c_table; |
1525 | 41.1k | int ac_y_table; |
1526 | 41.1k | int ac_c_table; |
1527 | 41.1k | const VLCElem *tables[2][64]; |
1528 | 41.1k | int eob_tracker[64]; |
1529 | 41.1k | VP4Predictor dc_pred[6][6]; |
1530 | 41.1k | int last_dc[NB_VP4_DC_TYPES]; |
1531 | | |
1532 | 41.1k | if (get_bits_left(gb) < 16) |
1533 | 8.46k | return AVERROR_INVALIDDATA; |
1534 | | |
1535 | | /* fetch the DC table indexes */ |
1536 | 32.6k | dc_y_table = get_bits(gb, 4); |
1537 | 32.6k | dc_c_table = get_bits(gb, 4); |
1538 | | |
1539 | 32.6k | ac_y_table = get_bits(gb, 4); |
1540 | 32.6k | ac_c_table = get_bits(gb, 4); |
1541 | | |
1542 | | /* build tables of DC/AC VLC tables */ |
1543 | | |
1544 | | /* DC table group */ |
1545 | 32.6k | tables[0][0] = coeff_vlc[dc_y_table]; |
1546 | 32.6k | tables[1][0] = coeff_vlc[dc_c_table]; |
1547 | 195k | for (int i = 1; i <= 5; i++) { |
1548 | | /* AC VLC table group 1 */ |
1549 | 163k | tables[0][i] = coeff_vlc[ac_y_table + 16]; |
1550 | 163k | tables[1][i] = coeff_vlc[ac_c_table + 16]; |
1551 | 163k | } |
1552 | 326k | for (int i = 6; i <= 14; i++) { |
1553 | | /* AC VLC table group 2 */ |
1554 | 293k | tables[0][i] = coeff_vlc[ac_y_table + 32]; |
1555 | 293k | tables[1][i] = coeff_vlc[ac_c_table + 32]; |
1556 | 293k | } |
1557 | 456k | for (int i = 15; i <= 27; i++) { |
1558 | | /* AC VLC table group 3 */ |
1559 | 424k | tables[0][i] = coeff_vlc[ac_y_table + 48]; |
1560 | 424k | tables[1][i] = coeff_vlc[ac_c_table + 48]; |
1561 | 424k | } |
1562 | 1.20M | for (int i = 28; i <= 63; i++) { |
1563 | | /* AC VLC table group 4 */ |
1564 | 1.17M | tables[0][i] = coeff_vlc[ac_y_table + 64]; |
1565 | 1.17M | tables[1][i] = coeff_vlc[ac_c_table + 64]; |
1566 | 1.17M | } |
1567 | | |
1568 | 32.6k | vp4_set_tokens_base(s); |
1569 | | |
1570 | 32.6k | memset(last_dc, 0, sizeof(last_dc)); |
1571 | | |
1572 | 50.9k | for (int plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) { |
1573 | 45.6k | memset(eob_tracker, 0, sizeof(eob_tracker)); |
1574 | | |
1575 | | /* initialise dc prediction */ |
1576 | 42.5M | for (int i = 0; i < s->fragment_width[!!plane]; i++) |
1577 | 42.4M | vp4_dc_predictor_reset(&s->dc_pred_row[i]); |
1578 | | |
1579 | 319k | for (int j = 0; j < 6; j++) |
1580 | 1.91M | for (int i = 0; i < 6; i++) |
1581 | 1.64M | vp4_dc_predictor_reset(&dc_pred[j][i]); |
1582 | | |
1583 | 450k | for (int sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) { |
1584 | 4.96M | for (int sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) { |
1585 | 4.55M | vp4_dc_pred_before(s, dc_pred, sb_x); |
1586 | 77.1M | for (int j = 0; j < 16; j++) { |
1587 | 72.6M | int hx = hilbert_offset[j][0]; |
1588 | 72.6M | int hy = hilbert_offset[j][1]; |
1589 | 72.6M | int x = 4 * sb_x + hx; |
1590 | 72.6M | int y = 4 * sb_y + hy; |
1591 | 72.6M | VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1]; |
1592 | 72.6M | int fragment, dc_block_type; |
1593 | | |
1594 | 72.6M | if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane]) |
1595 | 29.3M | continue; |
1596 | | |
1597 | 43.2M | fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x; |
1598 | | |
1599 | 43.2M | if (s->all_fragments[fragment].coding_method == MODE_COPY) |
1600 | 12.0M | continue; |
1601 | | |
1602 | 31.2M | if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0) |
1603 | 27.3k | return -1; |
1604 | | |
1605 | 31.2M | dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method]; |
1606 | | |
1607 | 31.2M | s->all_fragments[fragment].dc += |
1608 | 31.2M | vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane); |
1609 | | |
1610 | 31.2M | this_dc_pred->type = dc_block_type, |
1611 | 31.2M | this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc; |
1612 | 31.2M | } |
1613 | 4.53M | vp4_dc_pred_after(s, dc_pred, sb_x); |
1614 | 4.53M | } |
1615 | 431k | } |
1616 | 45.6k | } |
1617 | | |
1618 | 5.28k | vp4_set_tokens_base(s); |
1619 | | |
1620 | 5.28k | return 0; |
1621 | 32.6k | } |
1622 | | #endif |
1623 | | |
1624 | | /* |
1625 | | * This function reverses the DC prediction for each coded fragment in |
1626 | | * the frame. Much of this function is adapted directly from the original |
1627 | | * VP3 source code. |
1628 | | */ |
1629 | | #define COMPATIBLE_FRAME(x) \ |
1630 | 286M | (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type) |
1631 | 521M | #define DC_COEFF(u) s->all_fragments[u].dc |
1632 | | |
1633 | | static void reverse_dc_prediction(Vp3DecodeContext *s, |
1634 | | int first_fragment, |
1635 | | int fragment_width, |
1636 | | int fragment_height) |
1637 | 202k | { |
1638 | 50.9M | #define PUL 8 |
1639 | 48.6M | #define PU 4 |
1640 | 51.0M | #define PUR 2 |
1641 | 88.2M | #define PL 1 |
1642 | | |
1643 | 202k | int i = first_fragment; |
1644 | | |
1645 | 202k | int predicted_dc; |
1646 | | |
1647 | | /* DC values for the left, up-left, up, and up-right fragments */ |
1648 | 202k | int vl, vul, vu, vur; |
1649 | | |
1650 | | /* indexes for the left, up-left, up, and up-right fragments */ |
1651 | 202k | int l, ul, u, ur; |
1652 | | |
1653 | | /* |
1654 | | * The 6 fields mean: |
1655 | | * 0: up-left multiplier |
1656 | | * 1: up multiplier |
1657 | | * 2: up-right multiplier |
1658 | | * 3: left multiplier |
1659 | | */ |
1660 | 202k | static const int predictor_transform[16][4] = { |
1661 | 202k | { 0, 0, 0, 0 }, |
1662 | 202k | { 0, 0, 0, 128 }, // PL |
1663 | 202k | { 0, 0, 128, 0 }, // PUR |
1664 | 202k | { 0, 0, 53, 75 }, // PUR|PL |
1665 | 202k | { 0, 128, 0, 0 }, // PU |
1666 | 202k | { 0, 64, 0, 64 }, // PU |PL |
1667 | 202k | { 0, 128, 0, 0 }, // PU |PUR |
1668 | 202k | { 0, 0, 53, 75 }, // PU |PUR|PL |
1669 | 202k | { 128, 0, 0, 0 }, // PUL |
1670 | 202k | { 0, 0, 0, 128 }, // PUL|PL |
1671 | 202k | { 64, 0, 64, 0 }, // PUL|PUR |
1672 | 202k | { 0, 0, 53, 75 }, // PUL|PUR|PL |
1673 | 202k | { 0, 128, 0, 0 }, // PUL|PU |
1674 | 202k | { -104, 116, 0, 116 }, // PUL|PU |PL |
1675 | 202k | { 24, 80, 24, 0 }, // PUL|PU |PUR |
1676 | 202k | { -104, 116, 0, 116 } // PUL|PU |PUR|PL |
1677 | 202k | }; |
1678 | | |
1679 | | /* This table shows which types of blocks can use other blocks for |
1680 | | * prediction. For example, INTRA is the only mode in this table to |
1681 | | * have a frame number of 0. That means INTRA blocks can only predict |
1682 | | * from other INTRA blocks. There are 2 golden frame coding types; |
1683 | | * blocks encoding in these modes can only predict from other blocks |
1684 | | * that were encoded with these 1 of these 2 modes. */ |
1685 | 202k | static const unsigned char compatible_frame[9] = { |
1686 | 202k | 1, /* MODE_INTER_NO_MV */ |
1687 | 202k | 0, /* MODE_INTRA */ |
1688 | 202k | 1, /* MODE_INTER_PLUS_MV */ |
1689 | 202k | 1, /* MODE_INTER_LAST_MV */ |
1690 | 202k | 1, /* MODE_INTER_PRIOR_MV */ |
1691 | 202k | 2, /* MODE_USING_GOLDEN */ |
1692 | 202k | 2, /* MODE_GOLDEN_MV */ |
1693 | 202k | 1, /* MODE_INTER_FOUR_MV */ |
1694 | 202k | 3 /* MODE_COPY */ |
1695 | 202k | }; |
1696 | 202k | int current_frame_type; |
1697 | | |
1698 | | /* there is a last DC predictor for each of the 3 frame types */ |
1699 | 202k | short last_dc[3]; |
1700 | | |
1701 | 202k | int transform = 0; |
1702 | | |
1703 | 202k | vul = |
1704 | 202k | vu = |
1705 | 202k | vur = |
1706 | 202k | vl = 0; |
1707 | 202k | last_dc[0] = |
1708 | 202k | last_dc[1] = |
1709 | 202k | last_dc[2] = 0; |
1710 | | |
1711 | | /* for each fragment row... */ |
1712 | 4.32M | for (int y = 0; y < fragment_height; y++) { |
1713 | | /* for each fragment in a row... */ |
1714 | 175M | for (int x = 0; x < fragment_width; x++, i++) { |
1715 | | |
1716 | | /* reverse prediction if this block was coded */ |
1717 | 171M | if (s->all_fragments[i].coding_method != MODE_COPY) { |
1718 | 117M | current_frame_type = |
1719 | 117M | compatible_frame[s->all_fragments[i].coding_method]; |
1720 | | |
1721 | 117M | transform = 0; |
1722 | 117M | if (x) { |
1723 | 114M | l = i - 1; |
1724 | 114M | vl = DC_COEFF(l); |
1725 | 114M | if (COMPATIBLE_FRAME(l)) |
1726 | 88.2M | transform |= PL; |
1727 | 114M | } |
1728 | 117M | if (y) { |
1729 | 59.0M | u = i - fragment_width; |
1730 | 59.0M | vu = DC_COEFF(u); |
1731 | 59.0M | if (COMPATIBLE_FRAME(u)) |
1732 | 48.6M | transform |= PU; |
1733 | 59.0M | if (x) { |
1734 | 56.3M | ul = i - fragment_width - 1; |
1735 | 56.3M | vul = DC_COEFF(ul); |
1736 | 56.3M | if (COMPATIBLE_FRAME(ul)) |
1737 | 50.9M | transform |= PUL; |
1738 | 56.3M | } |
1739 | 59.0M | if (x + 1 < fragment_width) { |
1740 | 56.2M | ur = i - fragment_width + 1; |
1741 | 56.2M | vur = DC_COEFF(ur); |
1742 | 56.2M | if (COMPATIBLE_FRAME(ur)) |
1743 | 51.0M | transform |= PUR; |
1744 | 56.2M | } |
1745 | 59.0M | } |
1746 | | |
1747 | 117M | if (transform == 0) { |
1748 | | /* if there were no fragments to predict from, use last |
1749 | | * DC saved */ |
1750 | 15.7M | predicted_dc = last_dc[current_frame_type]; |
1751 | 101M | } else { |
1752 | | /* apply the appropriate predictor transform */ |
1753 | 101M | predicted_dc = |
1754 | 101M | (predictor_transform[transform][0] * vul) + |
1755 | 101M | (predictor_transform[transform][1] * vu) + |
1756 | 101M | (predictor_transform[transform][2] * vur) + |
1757 | 101M | (predictor_transform[transform][3] * vl); |
1758 | | |
1759 | 101M | predicted_dc /= 128; |
1760 | | |
1761 | | /* check for outranging on the [ul u l] and |
1762 | | * [ul u ur l] predictors */ |
1763 | 101M | if ((transform == 15) || (transform == 13)) { |
1764 | 42.7M | if (FFABS(predicted_dc - vu) > 128) |
1765 | 313k | predicted_dc = vu; |
1766 | 42.4M | else if (FFABS(predicted_dc - vl) > 128) |
1767 | 241k | predicted_dc = vl; |
1768 | 42.1M | else if (FFABS(predicted_dc - vul) > 128) |
1769 | 247k | predicted_dc = vul; |
1770 | 42.7M | } |
1771 | 101M | } |
1772 | | |
1773 | | /* at long last, apply the predictor */ |
1774 | 117M | DC_COEFF(i) += predicted_dc; |
1775 | | /* save the DC */ |
1776 | 117M | last_dc[current_frame_type] = DC_COEFF(i); |
1777 | 117M | } |
1778 | 171M | } |
1779 | 4.12M | } |
1780 | 202k | } |
1781 | | |
1782 | | static void apply_loop_filter(Vp3DecodeContext *s, int plane, |
1783 | | int ystart, int yend) |
1784 | 1.18M | { |
1785 | 1.18M | int *bounding_values = s->bounding_values_array + 127; |
1786 | | |
1787 | 1.18M | int width = s->fragment_width[!!plane]; |
1788 | 1.18M | int height = s->fragment_height[!!plane]; |
1789 | 1.18M | int fragment = s->fragment_start[plane] + ystart * width; |
1790 | 1.18M | ptrdiff_t stride = s->current_frame.f->linesize[plane]; |
1791 | 1.18M | uint8_t *plane_data = s->current_frame.f->data[plane]; |
1792 | 1.18M | if (!s->flipped_image) |
1793 | 1.17M | stride = -stride; |
1794 | 1.18M | plane_data += s->data_offset[plane] + 8 * ystart * stride; |
1795 | | |
1796 | 4.56M | for (int y = ystart; y < yend; y++) { |
1797 | 143M | for (int x = 0; x < width; x++) { |
1798 | | /* This code basically just deblocks on the edges of coded blocks. |
1799 | | * However, it has to be much more complicated because of the |
1800 | | * brain damaged deblock ordering used in VP3/Theora. Order matters |
1801 | | * because some pixels get filtered twice. */ |
1802 | 140M | if (s->all_fragments[fragment].coding_method != MODE_COPY) { |
1803 | | /* do not perform left edge filter for left columns frags */ |
1804 | 100M | if (x > 0) { |
1805 | 97.9M | s->vp3dsp.h_loop_filter( |
1806 | 97.9M | plane_data + 8 * x, |
1807 | 97.9M | stride, bounding_values); |
1808 | 97.9M | } |
1809 | | |
1810 | | /* do not perform top edge filter for top row fragments */ |
1811 | 100M | if (y > 0) { |
1812 | 49.2M | s->vp3dsp.v_loop_filter( |
1813 | 49.2M | plane_data + 8 * x, |
1814 | 49.2M | stride, bounding_values); |
1815 | 49.2M | } |
1816 | | |
1817 | | /* do not perform right edge filter for right column |
1818 | | * fragments or if right fragment neighbor is also coded |
1819 | | * in this frame (it will be filtered in next iteration) */ |
1820 | 100M | if ((x < width - 1) && |
1821 | 97.9M | (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) { |
1822 | 18.3M | s->vp3dsp.h_loop_filter( |
1823 | 18.3M | plane_data + 8 * x + 8, |
1824 | 18.3M | stride, bounding_values); |
1825 | 18.3M | } |
1826 | | |
1827 | | /* do not perform bottom edge filter for bottom row |
1828 | | * fragments or if bottom fragment neighbor is also coded |
1829 | | * in this frame (it will be filtered in the next row) */ |
1830 | 100M | if ((y < height - 1) && |
1831 | 46.9M | (s->all_fragments[fragment + width].coding_method == MODE_COPY)) { |
1832 | 6.75M | s->vp3dsp.v_loop_filter( |
1833 | 6.75M | plane_data + 8 * x + 8 * stride, |
1834 | 6.75M | stride, bounding_values); |
1835 | 6.75M | } |
1836 | 100M | } |
1837 | | |
1838 | 140M | fragment++; |
1839 | 140M | } |
1840 | 3.37M | plane_data += 8 * stride; |
1841 | 3.37M | } |
1842 | 1.18M | } |
1843 | | |
1844 | | /** |
1845 | | * Pull DCT tokens from the 64 levels to decode and dequant the coefficients |
1846 | | * for the next block in coding order |
1847 | | */ |
1848 | | static inline int vp3_dequant(Vp3DecodeContext *s, const Vp3Fragment *frag, |
1849 | | int plane, int inter, int16_t block[64]) |
1850 | 123M | { |
1851 | 123M | const int16_t *dequantizer = s->qmat[frag->qpi][inter][plane]; |
1852 | 123M | const uint8_t *perm = s->idct_scantable; |
1853 | 123M | int i = 0; |
1854 | | |
1855 | 368M | do { |
1856 | 368M | int token = *s->dct_tokens[plane][i]; |
1857 | 368M | switch (token & 3) { |
1858 | 121M | case 0: // EOB |
1859 | 121M | if (--token < 4) // 0-3 are token types so the EOB run must now be 0 |
1860 | 103M | s->dct_tokens[plane][i]++; |
1861 | 18.1M | else |
1862 | 18.1M | *s->dct_tokens[plane][i] = token & ~3; |
1863 | 121M | goto end; |
1864 | 23.2M | case 1: // zero run |
1865 | 23.2M | s->dct_tokens[plane][i]++; |
1866 | 23.2M | i += (token >> 2) & 0x7f; |
1867 | 23.2M | if (i > 63) { |
1868 | 273k | av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n"); |
1869 | 273k | return i; |
1870 | 273k | } |
1871 | 22.9M | block[perm[i]] = (token >> 9) * dequantizer[perm[i]]; |
1872 | 22.9M | i++; |
1873 | 22.9M | break; |
1874 | 223M | case 2: // coeff |
1875 | 223M | block[perm[i]] = (token >> 2) * dequantizer[perm[i]]; |
1876 | 223M | s->dct_tokens[plane][i++]++; |
1877 | 223M | break; |
1878 | 0 | default: // shouldn't happen |
1879 | 0 | return i; |
1880 | 368M | } |
1881 | 368M | } while (i < 64); |
1882 | | // return value is expected to be a valid level |
1883 | 1.88M | i--; |
1884 | 123M | end: |
1885 | | // the actual DC+prediction is in the fragment structure |
1886 | 123M | block[0] = frag->dc * s->qmat[0][inter][plane][0]; |
1887 | 123M | return i; |
1888 | 1.88M | } |
1889 | | |
1890 | | /** |
1891 | | * called when all pixels up to row y are complete |
1892 | | */ |
1893 | | static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y) |
1894 | 425k | { |
1895 | 425k | int h, cy; |
1896 | 425k | int offset[AV_NUM_DATA_POINTERS]; |
1897 | | |
1898 | 425k | if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) { |
1899 | 0 | int y_flipped = s->flipped_image ? s->height - y : y; |
1900 | | |
1901 | | /* At the end of the frame, report INT_MAX instead of the height of |
1902 | | * the frame. This makes the other threads' ff_thread_await_progress() |
1903 | | * calls cheaper, because they don't have to clip their values. */ |
1904 | 0 | ff_progress_frame_report(&s->current_frame, |
1905 | 0 | y_flipped == s->height ? INT_MAX |
1906 | 0 | : y_flipped - 1); |
1907 | 0 | } |
1908 | | |
1909 | 425k | if (!s->avctx->draw_horiz_band) |
1910 | 425k | return; |
1911 | | |
1912 | 0 | h = y - s->last_slice_end; |
1913 | 0 | s->last_slice_end = y; |
1914 | 0 | y -= h; |
1915 | |
|
1916 | 0 | if (!s->flipped_image) |
1917 | 0 | y = s->height - y - h; |
1918 | |
|
1919 | 0 | cy = y >> s->chroma_y_shift; |
1920 | 0 | offset[0] = s->current_frame.f->linesize[0] * y; |
1921 | 0 | offset[1] = s->current_frame.f->linesize[1] * cy; |
1922 | 0 | offset[2] = s->current_frame.f->linesize[2] * cy; |
1923 | 0 | for (int i = 3; i < AV_NUM_DATA_POINTERS; i++) |
1924 | 0 | offset[i] = 0; |
1925 | |
|
1926 | 0 | emms_c(); |
1927 | 0 | s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h); |
1928 | 0 | } |
1929 | | |
1930 | | /** |
1931 | | * Wait for the reference frame of the current fragment. |
1932 | | * The progress value is in luma pixel rows. |
1933 | | */ |
1934 | | static void await_reference_row(Vp3DecodeContext *s, const Vp3Fragment *fragment, |
1935 | | int motion_y, int y) |
1936 | 0 | { |
1937 | 0 | const ProgressFrame *ref_frame; |
1938 | 0 | int ref_row; |
1939 | 0 | int border = motion_y & 1; |
1940 | |
|
1941 | 0 | if (fragment->coding_method == MODE_USING_GOLDEN || |
1942 | 0 | fragment->coding_method == MODE_GOLDEN_MV) |
1943 | 0 | ref_frame = &s->golden_frame; |
1944 | 0 | else |
1945 | 0 | ref_frame = &s->last_frame; |
1946 | |
|
1947 | 0 | ref_row = y + (motion_y >> 1); |
1948 | 0 | ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border); |
1949 | |
|
1950 | 0 | ff_progress_frame_await(ref_frame, ref_row); |
1951 | 0 | } |
1952 | | |
1953 | | #if CONFIG_VP4_DECODER |
1954 | | /** |
1955 | | * @return non-zero if temp (edge_emu_buffer) was populated |
1956 | | */ |
1957 | | static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by, |
1958 | | const uint8_t *motion_source, ptrdiff_t stride, |
1959 | | int src_x, int src_y, uint8_t *temp) |
1960 | 11.1M | { |
1961 | 11.1M | int motion_shift = plane ? 4 : 2; |
1962 | 11.1M | int subpel_mask = plane ? 3 : 1; |
1963 | 11.1M | int *bounding_values = s->bounding_values_array + 127; |
1964 | | |
1965 | 11.1M | int x, y; |
1966 | 11.1M | int x2, y2; |
1967 | 11.1M | int x_subpel, y_subpel; |
1968 | 11.1M | int x_offset, y_offset; |
1969 | | |
1970 | 11.1M | int block_width = plane ? 8 : 16; |
1971 | 11.1M | int plane_width = s->width >> (plane && s->chroma_x_shift); |
1972 | 11.1M | int plane_height = s->height >> (plane && s->chroma_y_shift); |
1973 | | |
1974 | 93.6M | #define loop_stride 12 |
1975 | 11.1M | uint8_t loop[12 * loop_stride]; |
1976 | | |
1977 | | /* using division instead of shift to correctly handle negative values */ |
1978 | 11.1M | x = 8 * bx + motion_x / motion_shift; |
1979 | 11.1M | y = 8 * by + motion_y / motion_shift; |
1980 | | |
1981 | 11.1M | x_subpel = motion_x & subpel_mask; |
1982 | 11.1M | y_subpel = motion_y & subpel_mask; |
1983 | | |
1984 | 11.1M | if (x_subpel || y_subpel) { |
1985 | 10.0M | x--; |
1986 | 10.0M | y--; |
1987 | | |
1988 | 10.0M | if (x_subpel) |
1989 | 8.26M | x = FFMIN(x, x + FFSIGN(motion_x)); |
1990 | | |
1991 | 10.0M | if (y_subpel) |
1992 | 6.63M | y = FFMIN(y, y + FFSIGN(motion_y)); |
1993 | | |
1994 | 10.0M | x2 = x + block_width; |
1995 | 10.0M | y2 = y + block_width; |
1996 | | |
1997 | 10.0M | if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height) |
1998 | 3.23M | return 0; |
1999 | | |
2000 | 6.77M | x_offset = (-(x + 2) & 7) + 2; |
2001 | 6.77M | y_offset = (-(y + 2) & 7) + 2; |
2002 | | |
2003 | 6.77M | av_assert1(!(x_offset > 8 + x_subpel && y_offset > 8 + y_subpel)); |
2004 | | |
2005 | 6.77M | s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1, |
2006 | 6.77M | loop_stride, stride, |
2007 | 6.77M | 12, 12, src_x - 1, src_y - 1, |
2008 | 6.77M | plane_width, |
2009 | 6.77M | plane_height); |
2010 | | |
2011 | 6.77M | if (x_offset <= 8 + x_subpel) |
2012 | 6.46M | ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values); |
2013 | | |
2014 | 6.77M | if (y_offset <= 8 + y_subpel) |
2015 | 4.95M | ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values); |
2016 | | |
2017 | 6.77M | } else { |
2018 | | |
2019 | 1.09M | x_offset = -x & 7; |
2020 | 1.09M | y_offset = -y & 7; |
2021 | | |
2022 | 1.09M | if (!x_offset && !y_offset) |
2023 | 141k | return 0; |
2024 | | |
2025 | 951k | s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1, |
2026 | 951k | loop_stride, stride, |
2027 | 951k | 12, 12, src_x - 1, src_y - 1, |
2028 | 951k | plane_width, |
2029 | 951k | plane_height); |
2030 | | |
2031 | 951k | #define safe_loop_filter(name, ptr, stride, bounding_values) \ |
2032 | 1.29M | if (VP3_LOOP_FILTER_NO_UNALIGNED_SUPPORT && (uintptr_t)(ptr) & 7) \ |
2033 | 1.29M | s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \ |
2034 | 1.29M | else \ |
2035 | 1.29M | s->vp3dsp.name(ptr, stride, bounding_values); |
2036 | | |
2037 | 951k | if (x_offset) |
2038 | 835k | safe_loop_filter(h_loop_filter, loop + loop_stride + x_offset + 1, loop_stride, bounding_values); |
2039 | | |
2040 | 951k | if (y_offset) |
2041 | 455k | safe_loop_filter(v_loop_filter, loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values); |
2042 | 951k | } |
2043 | | |
2044 | 77.3M | for (int i = 0; i < 9; i++) |
2045 | 69.5M | memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9); |
2046 | | |
2047 | 7.73M | return 1; |
2048 | 11.1M | } |
2049 | | #endif |
2050 | | |
2051 | | /* |
2052 | | * Perform the final rendering for a particular slice of data. |
2053 | | * The slice number ranges from 0..(c_superblock_height - 1). |
2054 | | */ |
2055 | | static void render_slice(Vp3DecodeContext *s, int slice) |
2056 | 356k | { |
2057 | 356k | int16_t *block = s->block; |
2058 | 356k | int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef; |
2059 | | /* When decoding keyframes, the earlier frames may not be available, |
2060 | | * so we just use the current frame in this case instead; |
2061 | | * it also avoid using undefined pointer arithmetic. Nothing is |
2062 | | * ever read from these frames in case of a keyframe. */ |
2063 | 356k | const AVFrame *last_frame = s->last_frame.f ? |
2064 | 275k | s->last_frame.f : s->current_frame.f; |
2065 | 356k | const AVFrame *golden_frame = s->golden_frame.f ? |
2066 | 356k | s->golden_frame.f : s->current_frame.f; |
2067 | 356k | int motion_halfpel_index; |
2068 | 356k | int first_pixel; |
2069 | | |
2070 | 356k | if (slice >= s->c_superblock_height) |
2071 | 0 | return; |
2072 | | |
2073 | 1.42M | for (int plane = 0; plane < 3; plane++) { |
2074 | 1.07M | uint8_t *output_plane = s->current_frame.f->data[plane] + |
2075 | 1.07M | s->data_offset[plane]; |
2076 | 1.07M | const uint8_t *last_plane = last_frame->data[plane] + |
2077 | 1.07M | s->data_offset[plane]; |
2078 | 1.07M | const uint8_t *golden_plane = golden_frame->data[plane] + |
2079 | 1.07M | s->data_offset[plane]; |
2080 | 1.07M | ptrdiff_t stride = s->current_frame.f->linesize[plane]; |
2081 | 1.07M | int plane_width = s->width >> (plane && s->chroma_x_shift); |
2082 | 1.07M | int plane_height = s->height >> (plane && s->chroma_y_shift); |
2083 | 1.07M | const int8_t (*motion_val)[2] = s->motion_val[!!plane]; |
2084 | | |
2085 | 1.07M | int sb_y = slice << (!plane && s->chroma_y_shift); |
2086 | 1.07M | int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift); |
2087 | 1.07M | int slice_width = plane ? s->c_superblock_width |
2088 | 1.07M | : s->y_superblock_width; |
2089 | | |
2090 | 1.07M | int fragment_width = s->fragment_width[!!plane]; |
2091 | 1.07M | int fragment_height = s->fragment_height[!!plane]; |
2092 | 1.07M | int fragment_start = s->fragment_start[plane]; |
2093 | | |
2094 | 1.07M | int do_await = !plane && HAVE_THREADS && |
2095 | 356k | (s->avctx->active_thread_type & FF_THREAD_FRAME); |
2096 | | |
2097 | 1.07M | if (!s->flipped_image) |
2098 | 1.06M | stride = -stride; |
2099 | 1.07M | if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY)) |
2100 | 0 | continue; |
2101 | | |
2102 | | /* for each superblock row in the slice (both of them)... */ |
2103 | 2.49M | for (; sb_y < slice_height; sb_y++) { |
2104 | | /* for each superblock in a row... */ |
2105 | 36.8M | for (int sb_x = 0; sb_x < slice_width; sb_x++) { |
2106 | | /* for each block in a superblock... */ |
2107 | 601M | for (int j = 0; j < 16; j++) { |
2108 | 566M | int x = 4 * sb_x + hilbert_offset[j][0]; |
2109 | 566M | int y = 4 * sb_y + hilbert_offset[j][1]; |
2110 | 566M | int fragment = y * fragment_width + x; |
2111 | | |
2112 | 566M | int i = fragment_start + fragment; |
2113 | | |
2114 | | // bounds check |
2115 | 566M | if (x >= fragment_width || y >= fragment_height) |
2116 | 388M | continue; |
2117 | | |
2118 | 177M | first_pixel = 8 * y * stride + 8 * x; |
2119 | | |
2120 | 177M | if (do_await && |
2121 | 0 | s->all_fragments[i].coding_method != MODE_INTRA) |
2122 | 0 | await_reference_row(s, &s->all_fragments[i], |
2123 | 0 | motion_val[fragment][1], |
2124 | 0 | (16 * y) >> s->chroma_y_shift); |
2125 | | |
2126 | | /* transform if this block was coded */ |
2127 | 177M | if (s->all_fragments[i].coding_method != MODE_COPY) { |
2128 | 123M | const uint8_t *motion_source; |
2129 | 123M | if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || |
2130 | 121M | (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) |
2131 | 6.01M | motion_source = golden_plane; |
2132 | 117M | else |
2133 | 117M | motion_source = last_plane; |
2134 | | |
2135 | 123M | motion_source += first_pixel; |
2136 | 123M | motion_halfpel_index = 0; |
2137 | | |
2138 | | /* sort out the motion vector if this fragment is coded |
2139 | | * using a motion vector method */ |
2140 | 123M | if ((s->all_fragments[i].coding_method > MODE_INTRA) && |
2141 | 42.7M | (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) { |
2142 | 40.8M | int src_x, src_y; |
2143 | 40.8M | int standard_mc = 1; |
2144 | 40.8M | motion_x = motion_val[fragment][0]; |
2145 | 40.8M | motion_y = motion_val[fragment][1]; |
2146 | 40.8M | #if CONFIG_VP4_DECODER |
2147 | 40.8M | if (plane && s->version >= 2) { |
2148 | 2.96M | motion_x = (motion_x >> 1) | (motion_x & 1); |
2149 | 2.96M | motion_y = (motion_y >> 1) | (motion_y & 1); |
2150 | 2.96M | } |
2151 | 40.8M | #endif |
2152 | | |
2153 | 40.8M | src_x = (motion_x >> 1) + 8 * x; |
2154 | 40.8M | src_y = (motion_y >> 1) + 8 * y; |
2155 | | |
2156 | 40.8M | motion_halfpel_index = motion_x & 0x01; |
2157 | 40.8M | motion_source += (motion_x >> 1); |
2158 | | |
2159 | 40.8M | motion_halfpel_index |= (motion_y & 0x01) << 1; |
2160 | 40.8M | motion_source += ((motion_y >> 1) * stride); |
2161 | | |
2162 | 40.8M | #if CONFIG_VP4_DECODER |
2163 | 40.8M | if (s->version >= 2) { |
2164 | 11.1M | uint8_t *temp = s->edge_emu_buffer; |
2165 | 11.1M | if (stride < 0) |
2166 | 11.0M | temp -= 8 * stride; |
2167 | 11.1M | if (vp4_mc_loop_filter(s, plane, motion_val[fragment][0], motion_val[fragment][1], x, y, motion_source, stride, src_x, src_y, temp)) { |
2168 | 7.73M | motion_source = temp; |
2169 | 7.73M | standard_mc = 0; |
2170 | 7.73M | } |
2171 | 11.1M | } |
2172 | 40.8M | #endif |
2173 | | |
2174 | 40.8M | if (standard_mc && ( |
2175 | 33.0M | src_x < 0 || src_y < 0 || |
2176 | 28.2M | src_x + 9 >= plane_width || |
2177 | 27.5M | src_y + 9 >= plane_height)) { |
2178 | 20.3M | uint8_t *temp = s->edge_emu_buffer; |
2179 | 20.3M | if (stride < 0) |
2180 | 20.1M | temp -= 8 * stride; |
2181 | | |
2182 | 20.3M | s->vdsp.emulated_edge_mc(temp, motion_source, |
2183 | 20.3M | stride, stride, |
2184 | 20.3M | 9, 9, src_x, src_y, |
2185 | 20.3M | plane_width, |
2186 | 20.3M | plane_height); |
2187 | 20.3M | motion_source = temp; |
2188 | 20.3M | } |
2189 | 40.8M | } |
2190 | | |
2191 | | /* first, take care of copying a block from either the |
2192 | | * previous or the golden frame */ |
2193 | 123M | if (s->all_fragments[i].coding_method != MODE_INTRA) { |
2194 | | /* Note, it is possible to implement all MC cases |
2195 | | * with put_no_rnd_pixels_l2 which would look more |
2196 | | * like the VP3 source but this would be slower as |
2197 | | * put_no_rnd_pixels_tab is better optimized */ |
2198 | 56.5M | if (motion_halfpel_index != 3) { |
2199 | 45.5M | s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index]( |
2200 | 45.5M | output_plane + first_pixel, |
2201 | 45.5M | motion_source, stride, 8); |
2202 | 45.5M | } else { |
2203 | | /* d is 0 if motion_x and _y have the same sign, |
2204 | | * else -1 */ |
2205 | 10.9M | int d = (motion_x ^ motion_y) >> 31; |
2206 | 10.9M | s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel, |
2207 | 10.9M | motion_source - d, |
2208 | 10.9M | motion_source + stride + 1 + d, |
2209 | 10.9M | stride, 8); |
2210 | 10.9M | } |
2211 | 56.5M | } |
2212 | | |
2213 | | /* invert DCT and place (or add) in final output */ |
2214 | | |
2215 | 123M | if (s->all_fragments[i].coding_method == MODE_INTRA) { |
2216 | 67.0M | vp3_dequant(s, s->all_fragments + i, |
2217 | 67.0M | plane, 0, block); |
2218 | 67.0M | s->vp3dsp.idct_put(output_plane + first_pixel, |
2219 | 67.0M | stride, |
2220 | 67.0M | block); |
2221 | 67.0M | } else { |
2222 | 56.5M | if (vp3_dequant(s, s->all_fragments + i, |
2223 | 56.5M | plane, 1, block)) { |
2224 | 30.3M | s->vp3dsp.idct_add(output_plane + first_pixel, |
2225 | 30.3M | stride, |
2226 | 30.3M | block); |
2227 | 30.3M | } else { |
2228 | 26.1M | s->vp3dsp.idct_dc_add(output_plane + first_pixel, |
2229 | 26.1M | stride, block); |
2230 | 26.1M | } |
2231 | 56.5M | } |
2232 | 123M | } else { |
2233 | | /* copy directly from the previous frame */ |
2234 | 54.3M | s->hdsp.put_pixels_tab[1][0]( |
2235 | 54.3M | output_plane + first_pixel, |
2236 | 54.3M | last_plane + first_pixel, |
2237 | 54.3M | stride, 8); |
2238 | 54.3M | } |
2239 | 177M | } |
2240 | 35.3M | } |
2241 | | |
2242 | | // Filter up to the last row in the superblock row |
2243 | 1.42M | if (s->version < 2 && !s->skip_loop_filter) |
2244 | 995k | apply_loop_filter(s, plane, 4 * sb_y - !!sb_y, |
2245 | 995k | FFMIN(4 * sb_y + 3, fragment_height - 1)); |
2246 | 1.42M | } |
2247 | 1.07M | } |
2248 | | |
2249 | | /* this looks like a good place for slice dispatch... */ |
2250 | | /* algorithm: |
2251 | | * if (slice == s->macroblock_height - 1) |
2252 | | * dispatch (both last slice & 2nd-to-last slice); |
2253 | | * else if (slice > 0) |
2254 | | * dispatch (slice - 1); |
2255 | | */ |
2256 | | |
2257 | 356k | vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16, |
2258 | 356k | s->height - 16)); |
2259 | 356k | } |
2260 | | |
2261 | | static av_cold void init_tables_once(void) |
2262 | 3 | { |
2263 | 3 | VLCInitState state = VLC_INIT_STATE(mode_code_vlc); |
2264 | | |
2265 | 3 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(superblock_run_length_vlc, |
2266 | 3 | SUPERBLOCK_VLC_BITS, 34, |
2267 | 3 | superblock_run_length_vlc_lens, 1, |
2268 | 3 | NULL, 0, 0, 1, 0); |
2269 | | |
2270 | 3 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(fragment_run_length_vlc, 5, 30, |
2271 | 3 | fragment_run_length_vlc_len, 1, |
2272 | 3 | NULL, 0, 0, 0, 0); |
2273 | | |
2274 | 3 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(motion_vector_vlc, VP3_MV_VLC_BITS, 63, |
2275 | 3 | &motion_vector_vlc_table[0][1], 2, |
2276 | 3 | &motion_vector_vlc_table[0][0], 2, 1, |
2277 | 3 | -31, 0); |
2278 | | |
2279 | 3 | ff_vlc_init_tables_from_lengths(&state, 4, 8, |
2280 | 3 | mode_code_vlc_len, 1, |
2281 | 3 | NULL, 0, 0, 0, 0); |
2282 | | |
2283 | 3 | #if CONFIG_VP4_DECODER |
2284 | 9 | for (int j = 0; j < 2; j++) |
2285 | 48 | for (int i = 0; i < 7; i++) { |
2286 | 42 | vp4_mv_vlc_table[j][i] = |
2287 | 42 | ff_vlc_init_tables_from_lengths(&state, VP4_MV_VLC_BITS, 63, |
2288 | 42 | &vp4_mv_vlc[j][i][0][1], 2, |
2289 | 42 | &vp4_mv_vlc[j][i][0][0], 2, 1, |
2290 | 42 | -31, 0); |
2291 | 42 | } |
2292 | | |
2293 | | /* version >= 2 */ |
2294 | 9 | for (int i = 0; i < 2; i++) { |
2295 | 6 | block_pattern_vlc[i] = |
2296 | 6 | ff_vlc_init_tables(&state, 5, 14, |
2297 | 6 | &vp4_block_pattern_vlc[i][0][1], 2, 1, |
2298 | 6 | &vp4_block_pattern_vlc[i][0][0], 2, 1, 0); |
2299 | 6 | } |
2300 | 3 | #endif |
2301 | 3 | } |
2302 | | |
2303 | | /// Allocate tables for per-frame data in Vp3DecodeContext |
2304 | | static av_cold int allocate_tables(AVCodecContext *avctx) |
2305 | 23.6k | { |
2306 | 23.6k | Vp3DecodeContext *s = avctx->priv_data; |
2307 | 23.6k | int y_fragment_count, c_fragment_count; |
2308 | | |
2309 | 23.6k | free_tables(avctx); |
2310 | | |
2311 | 23.6k | y_fragment_count = s->fragment_width[0] * s->fragment_height[0]; |
2312 | 23.6k | c_fragment_count = s->fragment_width[1] * s->fragment_height[1]; |
2313 | | |
2314 | | /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */ |
2315 | 23.6k | s->superblock_coding = av_mallocz(FFMAX(s->superblock_count, s->yuv_macroblock_count)); |
2316 | 23.6k | s->all_fragments = av_calloc(s->fragment_count, sizeof(*s->all_fragments)); |
2317 | | |
2318 | 23.6k | s-> kf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int)); |
2319 | 23.6k | s->nkf_coded_fragment_list = av_calloc(s->fragment_count, sizeof(int)); |
2320 | 23.6k | memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment)); |
2321 | | |
2322 | 23.6k | s->dct_tokens_base = av_calloc(s->fragment_count, |
2323 | 23.6k | 64 * sizeof(*s->dct_tokens_base)); |
2324 | 23.6k | s->motion_val[0] = av_calloc(y_fragment_count, sizeof(*s->motion_val[0])); |
2325 | 23.6k | s->motion_val[1] = av_calloc(c_fragment_count, sizeof(*s->motion_val[1])); |
2326 | | |
2327 | | /* work out the block mapping tables */ |
2328 | 23.6k | s->superblock_fragments = av_calloc(s->superblock_count, 16 * sizeof(int)); |
2329 | 23.6k | s->macroblock_coding = av_mallocz(s->macroblock_count + 1); |
2330 | | |
2331 | 23.6k | s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row)); |
2332 | | |
2333 | 23.6k | if (!s->superblock_coding || !s->all_fragments || |
2334 | 23.6k | !s->dct_tokens_base || !s->kf_coded_fragment_list || |
2335 | 23.6k | !s->nkf_coded_fragment_list || |
2336 | 23.6k | !s->superblock_fragments || !s->macroblock_coding || |
2337 | 23.6k | !s->dc_pred_row || |
2338 | 23.6k | !s->motion_val[0] || !s->motion_val[1]) { |
2339 | 0 | return -1; |
2340 | 0 | } |
2341 | | |
2342 | 23.6k | init_block_mapping(s); |
2343 | | |
2344 | 23.6k | return 0; |
2345 | 23.6k | } |
2346 | | |
2347 | | |
2348 | | static av_cold void free_vlc_tables(AVRefStructOpaque unused, void *obj) |
2349 | 23.6k | { |
2350 | 23.6k | CoeffVLCs *vlcs = obj; |
2351 | | |
2352 | 1.91M | for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) |
2353 | 1.89M | ff_vlc_free(&vlcs->vlcs[i]); |
2354 | 23.6k | } |
2355 | | |
2356 | | static av_cold int vp3_decode_init(AVCodecContext *avctx) |
2357 | 23.9k | { |
2358 | 23.9k | static AVOnce init_static_once = AV_ONCE_INIT; |
2359 | 23.9k | Vp3DecodeContext *s = avctx->priv_data; |
2360 | 23.9k | int ret; |
2361 | 23.9k | int c_width; |
2362 | 23.9k | int c_height; |
2363 | 23.9k | int y_fragment_count, c_fragment_count; |
2364 | | |
2365 | 23.9k | if (avctx->codec_tag == MKTAG('V', 'P', '4', '0')) { |
2366 | 2.78k | s->version = 3; |
2367 | | #if !CONFIG_VP4_DECODER |
2368 | | av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n"); |
2369 | | return AVERROR_DECODER_NOT_FOUND; |
2370 | | #endif |
2371 | 21.1k | } else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0')) |
2372 | 884 | s->version = 0; |
2373 | 20.3k | else |
2374 | 20.3k | s->version = 1; |
2375 | | |
2376 | 23.9k | s->avctx = avctx; |
2377 | 23.9k | s->width = FFALIGN(avctx->coded_width, 16); |
2378 | 23.9k | s->height = FFALIGN(avctx->coded_height, 16); |
2379 | 23.9k | if (s->width < 18) |
2380 | 325 | return AVERROR_PATCHWELCOME; |
2381 | 23.6k | if (avctx->codec_id != AV_CODEC_ID_THEORA) |
2382 | 3.87k | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
2383 | 23.6k | avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; |
2384 | 23.6k | ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT); |
2385 | 23.6k | ff_videodsp_init(&s->vdsp, 8); |
2386 | 23.6k | ff_vp3dsp_init(&s->vp3dsp); |
2387 | | |
2388 | 1.53M | for (int i = 0; i < 64; i++) { |
2389 | 3.02M | #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3)) |
2390 | 1.51M | s->idct_permutation[i] = TRANSPOSE(i); |
2391 | 1.51M | s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]); |
2392 | 1.51M | #undef TRANSPOSE |
2393 | 1.51M | } |
2394 | | |
2395 | | /* initialize to an impossible value which will force a recalculation |
2396 | | * in the first frame decode */ |
2397 | 94.5k | for (int i = 0; i < 3; i++) |
2398 | 70.9k | s->qps[i] = -1; |
2399 | | |
2400 | 23.6k | ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift); |
2401 | 23.6k | if (ret) |
2402 | 0 | return ret; |
2403 | | |
2404 | 23.6k | s->y_superblock_width = (s->width + 31) / 32; |
2405 | 23.6k | s->y_superblock_height = (s->height + 31) / 32; |
2406 | 23.6k | s->y_superblock_count = s->y_superblock_width * s->y_superblock_height; |
2407 | | |
2408 | | /* work out the dimensions for the C planes */ |
2409 | 23.6k | c_width = s->width >> s->chroma_x_shift; |
2410 | 23.6k | c_height = s->height >> s->chroma_y_shift; |
2411 | 23.6k | s->c_superblock_width = (c_width + 31) / 32; |
2412 | 23.6k | s->c_superblock_height = (c_height + 31) / 32; |
2413 | 23.6k | s->c_superblock_count = s->c_superblock_width * s->c_superblock_height; |
2414 | | |
2415 | 23.6k | s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2); |
2416 | 23.6k | s->u_superblock_start = s->y_superblock_count; |
2417 | 23.6k | s->v_superblock_start = s->u_superblock_start + s->c_superblock_count; |
2418 | | |
2419 | 23.6k | s->macroblock_width = (s->width + 15) / 16; |
2420 | 23.6k | s->macroblock_height = (s->height + 15) / 16; |
2421 | 23.6k | s->macroblock_count = s->macroblock_width * s->macroblock_height; |
2422 | 23.6k | s->c_macroblock_width = (c_width + 15) / 16; |
2423 | 23.6k | s->c_macroblock_height = (c_height + 15) / 16; |
2424 | 23.6k | s->c_macroblock_count = s->c_macroblock_width * s->c_macroblock_height; |
2425 | 23.6k | s->yuv_macroblock_count = s->macroblock_count + 2 * s->c_macroblock_count; |
2426 | | |
2427 | 23.6k | s->fragment_width[0] = s->width / FRAGMENT_PIXELS; |
2428 | 23.6k | s->fragment_height[0] = s->height / FRAGMENT_PIXELS; |
2429 | 23.6k | s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift; |
2430 | 23.6k | s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift; |
2431 | | |
2432 | | /* fragment count covers all 8x8 blocks for all 3 planes */ |
2433 | 23.6k | y_fragment_count = s->fragment_width[0] * s->fragment_height[0]; |
2434 | 23.6k | c_fragment_count = s->fragment_width[1] * s->fragment_height[1]; |
2435 | 23.6k | s->fragment_count = y_fragment_count + 2 * c_fragment_count; |
2436 | 23.6k | s->fragment_start[1] = y_fragment_count; |
2437 | 23.6k | s->fragment_start[2] = y_fragment_count + c_fragment_count; |
2438 | | |
2439 | 23.6k | if (!s->theora_tables) { |
2440 | 1.44M | for (int i = 0; i < 64; i++) { |
2441 | 1.42M | s->coded_dc_scale_factor[0][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_y_dc_scale_factor[i]; |
2442 | 1.42M | s->coded_dc_scale_factor[1][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_uv_dc_scale_factor[i]; |
2443 | 1.42M | s->coded_ac_scale_factor[i] = s->version < 2 ? vp31_ac_scale_factor[i] : vp4_ac_scale_factor[i]; |
2444 | 1.42M | s->base_matrix[0][i] = s->version < 2 ? vp31_intra_y_dequant[i] : vp4_generic_dequant[i]; |
2445 | 1.42M | s->base_matrix[1][i] = s->version < 2 ? ff_mjpeg_std_chrominance_quant_tbl[i] : vp4_generic_dequant[i]; |
2446 | 1.42M | s->base_matrix[2][i] = s->version < 2 ? vp31_inter_dequant[i] : vp4_generic_dequant[i]; |
2447 | 1.42M | s->filter_limit_values[i] = s->version < 2 ? vp31_filter_limit_values[i] : vp4_filter_limit_values[i]; |
2448 | 1.42M | } |
2449 | | |
2450 | 66.7k | for (int inter = 0; inter < 2; inter++) { |
2451 | 178k | for (int plane = 0; plane < 3; plane++) { |
2452 | 133k | s->qr_count[inter][plane] = 1; |
2453 | 133k | s->qr_size[inter][plane][0] = 63; |
2454 | 133k | s->qr_base[inter][plane][0] = |
2455 | 133k | s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter; |
2456 | 133k | } |
2457 | 44.5k | } |
2458 | 22.2k | } |
2459 | | |
2460 | 23.6k | if (ff_thread_sync_ref(avctx, offsetof(Vp3DecodeContext, coeff_vlc)) != FF_THREAD_IS_COPY) { |
2461 | 23.6k | CoeffVLCs *vlcs = av_refstruct_alloc_ext(sizeof(*s->coeff_vlc), 0, |
2462 | 23.6k | NULL, free_vlc_tables); |
2463 | 23.6k | if (!vlcs) |
2464 | 0 | return AVERROR(ENOMEM); |
2465 | | |
2466 | 23.6k | s->coeff_vlc = vlcs; |
2467 | | |
2468 | 23.6k | if (!s->theora_tables) { |
2469 | 22.2k | const uint8_t (*bias_tabs)[32][2]; |
2470 | | |
2471 | | /* init VLC tables */ |
2472 | 22.2k | bias_tabs = CONFIG_VP4_DECODER && s->version >= 2 ? vp4_bias : vp3_bias; |
2473 | 1.80M | for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) { |
2474 | 1.78M | ret = ff_vlc_init_from_lengths(&vlcs->vlcs[i], 11, 32, |
2475 | 1.78M | &bias_tabs[i][0][1], 2, |
2476 | 1.78M | &bias_tabs[i][0][0], 2, 1, |
2477 | 1.78M | 0, 0, avctx); |
2478 | 1.78M | if (ret < 0) |
2479 | 0 | return ret; |
2480 | 1.78M | vlcs->vlc_tabs[i] = vlcs->vlcs[i].table; |
2481 | 1.78M | } |
2482 | 22.2k | } else { |
2483 | 112k | for (int i = 0; i < FF_ARRAY_ELEMS(vlcs->vlcs); i++) { |
2484 | 111k | const HuffTable *tab = &s->huffman_table[i]; |
2485 | | |
2486 | 111k | ret = ff_vlc_init_from_lengths(&vlcs->vlcs[i], 11, tab->nb_entries, |
2487 | 111k | &tab->entries[0].len, sizeof(*tab->entries), |
2488 | 111k | &tab->entries[0].sym, sizeof(*tab->entries), 1, |
2489 | 111k | 0, 0, avctx); |
2490 | 111k | if (ret < 0) |
2491 | 0 | return ret; |
2492 | 111k | vlcs->vlc_tabs[i] = vlcs->vlcs[i].table; |
2493 | 111k | } |
2494 | 1.39k | } |
2495 | 23.6k | } |
2496 | | |
2497 | 23.6k | ff_thread_once(&init_static_once, init_tables_once); |
2498 | | |
2499 | 23.6k | return allocate_tables(avctx); |
2500 | 23.6k | } |
2501 | | |
2502 | | #if HAVE_THREADS |
2503 | | static void ref_frames(Vp3DecodeContext *dst, const Vp3DecodeContext *src) |
2504 | 0 | { |
2505 | 0 | ff_progress_frame_replace(&dst->current_frame, &src->current_frame); |
2506 | 0 | ff_progress_frame_replace(&dst->golden_frame, &src->golden_frame); |
2507 | 0 | } |
2508 | | |
2509 | | static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src) |
2510 | 0 | { |
2511 | 0 | Vp3DecodeContext *s = dst->priv_data; |
2512 | 0 | const Vp3DecodeContext *s1 = src->priv_data; |
2513 | 0 | int qps_changed = 0; |
2514 | | |
2515 | | // copy previous frame data |
2516 | 0 | ref_frames(s, s1); |
2517 | |
|
2518 | 0 | if (s != s1) { |
2519 | | // copy qscale data if necessary |
2520 | 0 | for (int i = 0; i < 3; i++) { |
2521 | 0 | if (s->qps[i] != s1->qps[1]) { |
2522 | 0 | qps_changed = 1; |
2523 | 0 | memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i])); |
2524 | 0 | } |
2525 | 0 | } |
2526 | |
|
2527 | 0 | if (s->qps[0] != s1->qps[0]) |
2528 | 0 | memcpy(&s->bounding_values_array, &s1->bounding_values_array, |
2529 | 0 | sizeof(s->bounding_values_array)); |
2530 | |
|
2531 | 0 | if (qps_changed) { |
2532 | 0 | memcpy(s->qps, s1->qps, sizeof(s->qps)); |
2533 | 0 | s->nqps = s1->nqps; |
2534 | 0 | } |
2535 | 0 | } |
2536 | 0 | return 0; |
2537 | 0 | } |
2538 | | #endif |
2539 | | |
2540 | | static int vp3_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
2541 | | int *got_frame, AVPacket *avpkt) |
2542 | 361k | { |
2543 | 361k | const uint8_t *buf = avpkt->data; |
2544 | 361k | int buf_size = avpkt->size; |
2545 | 361k | Vp3DecodeContext *s = avctx->priv_data; |
2546 | 361k | GetBitContext gb; |
2547 | 361k | int ret; |
2548 | | |
2549 | 361k | if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0) |
2550 | 0 | return ret; |
2551 | | |
2552 | 361k | #if CONFIG_THEORA_DECODER |
2553 | 361k | if (s->theora && get_bits1(&gb)) { |
2554 | 38.9k | int type = get_bits(&gb, 7); |
2555 | 38.9k | skip_bits_long(&gb, 6*8); /* "theora" */ |
2556 | | |
2557 | 38.9k | if (s->avctx->active_thread_type&FF_THREAD_FRAME) { |
2558 | 0 | av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n"); |
2559 | 0 | return AVERROR_PATCHWELCOME; |
2560 | 0 | } |
2561 | 38.9k | if (type == 0) { |
2562 | 25.4k | vp3_decode_end(avctx); |
2563 | 25.4k | ret = theora_decode_header(avctx, &gb); |
2564 | | |
2565 | 25.4k | if (ret >= 0) |
2566 | 15.2k | ret = vp3_decode_init(avctx); |
2567 | 25.4k | if (ret < 0) { |
2568 | 10.2k | vp3_decode_end(avctx); |
2569 | 10.2k | return ret; |
2570 | 10.2k | } |
2571 | 15.2k | return buf_size; |
2572 | 25.4k | } else if (type == 2) { |
2573 | 9.15k | vp3_decode_end(avctx); |
2574 | 9.15k | ret = theora_decode_tables(avctx, &gb); |
2575 | 9.15k | if (ret >= 0) |
2576 | 1.39k | ret = vp3_decode_init(avctx); |
2577 | 9.15k | if (ret < 0) { |
2578 | 7.76k | vp3_decode_end(avctx); |
2579 | 7.76k | return ret; |
2580 | 7.76k | } |
2581 | 1.39k | return buf_size; |
2582 | 9.15k | } |
2583 | | |
2584 | 4.34k | av_log(avctx, AV_LOG_ERROR, |
2585 | 4.34k | "Header packet passed to frame decoder, skipping\n"); |
2586 | 4.34k | return -1; |
2587 | 38.9k | } |
2588 | 322k | #endif |
2589 | | |
2590 | 322k | s->keyframe = !get_bits1(&gb); |
2591 | 322k | if (!s->all_fragments) { |
2592 | 10.9k | av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n"); |
2593 | 10.9k | return -1; |
2594 | 10.9k | } |
2595 | 311k | if (!s->theora) |
2596 | 229k | skip_bits(&gb, 1); |
2597 | | |
2598 | 311k | int last_qps[3]; |
2599 | 1.24M | for (int i = 0; i < 3; i++) |
2600 | 933k | last_qps[i] = s->qps[i]; |
2601 | | |
2602 | 311k | s->nqps = 0; |
2603 | 326k | do { |
2604 | 326k | s->qps[s->nqps++] = get_bits(&gb, 6); |
2605 | 326k | } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb)); |
2606 | 918k | for (int i = s->nqps; i < 3; i++) |
2607 | 607k | s->qps[i] = -1; |
2608 | | |
2609 | 311k | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
2610 | 0 | av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%"PRId64": Q index = %d\n", |
2611 | 0 | s->keyframe ? "key" : "", avctx->frame_num + 1, s->qps[0]); |
2612 | | |
2613 | 311k | s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] || |
2614 | 156k | avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL |
2615 | 156k | : AVDISCARD_NONKEY); |
2616 | | |
2617 | 311k | if (s->qps[0] != last_qps[0]) |
2618 | 86.3k | init_loop_filter(s); |
2619 | | |
2620 | 637k | for (int i = 0; i < s->nqps; i++) |
2621 | | // reinit all dequantizers if the first one changed, because |
2622 | | // the DC of the first quantizer must be used for all matrices |
2623 | 326k | if (s->qps[i] != last_qps[i] || s->qps[0] != last_qps[0]) |
2624 | 93.9k | init_dequantizer(s, i); |
2625 | | |
2626 | 311k | if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) |
2627 | 94.0k | return buf_size; |
2628 | | |
2629 | 217k | ret = ff_progress_frame_get_buffer(avctx, &s->last_frame, |
2630 | 217k | AV_GET_BUFFER_FLAG_REF); |
2631 | 217k | if (ret < 0) { |
2632 | | // Don't goto error here, as one can't report progress on or |
2633 | | // unref a non-existent frame. |
2634 | 20 | return ret; |
2635 | 20 | } |
2636 | 217k | FFSWAP(ProgressFrame, s->last_frame, s->current_frame); |
2637 | 217k | s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I |
2638 | 217k | : AV_PICTURE_TYPE_P; |
2639 | 217k | if (s->keyframe) |
2640 | 124k | s->current_frame.f->flags |= AV_FRAME_FLAG_KEY; |
2641 | 92.4k | else |
2642 | 92.4k | s->current_frame.f->flags &= ~AV_FRAME_FLAG_KEY; |
2643 | | |
2644 | 217k | if (!s->edge_emu_buffer) { |
2645 | 9.17k | s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0])); |
2646 | 9.17k | if (!s->edge_emu_buffer) { |
2647 | 0 | ret = AVERROR(ENOMEM); |
2648 | 0 | goto error; |
2649 | 0 | } |
2650 | 9.17k | } |
2651 | | |
2652 | 217k | if (s->keyframe) { |
2653 | 124k | if (!s->theora) { |
2654 | 76.0k | skip_bits(&gb, 4); /* width code */ |
2655 | 76.0k | skip_bits(&gb, 4); /* height code */ |
2656 | 76.0k | if (s->version) { |
2657 | 12.7k | int version = get_bits(&gb, 5); |
2658 | | #if !CONFIG_VP4_DECODER |
2659 | | if (version >= 2) { |
2660 | | av_log(avctx, AV_LOG_ERROR, "This build does not support decoding VP4.\n"); |
2661 | | ret = AVERROR_DECODER_NOT_FOUND; |
2662 | | goto error; |
2663 | | } |
2664 | | #endif |
2665 | 12.7k | s->version = version; |
2666 | 12.7k | if (avctx->frame_num == 0) |
2667 | 9.94k | av_log(s->avctx, AV_LOG_DEBUG, |
2668 | 9.94k | "VP version: %d\n", s->version); |
2669 | 12.7k | } |
2670 | 76.0k | } |
2671 | 124k | if (s->version || s->theora) { |
2672 | 60.4k | if (get_bits1(&gb)) |
2673 | 25.7k | av_log(s->avctx, AV_LOG_ERROR, |
2674 | 25.7k | "Warning, unsupported keyframe coding type?!\n"); |
2675 | 60.4k | skip_bits(&gb, 2); /* reserved? */ |
2676 | | |
2677 | 60.4k | #if CONFIG_VP4_DECODER |
2678 | 60.4k | if (s->version >= 2) { |
2679 | 26.3k | int mb_height, mb_width; |
2680 | 26.3k | int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div; |
2681 | | |
2682 | 26.3k | mb_height = get_bits(&gb, 8); |
2683 | 26.3k | mb_width = get_bits(&gb, 8); |
2684 | 26.3k | if (mb_height != s->macroblock_height || |
2685 | 2.26k | mb_width != s->macroblock_width) |
2686 | 24.8k | avpriv_request_sample(s->avctx, "macroblock dimension mismatch"); |
2687 | | |
2688 | 26.3k | mb_width_mul = get_bits(&gb, 5); |
2689 | 26.3k | mb_width_div = get_bits(&gb, 3); |
2690 | 26.3k | mb_height_mul = get_bits(&gb, 5); |
2691 | 26.3k | mb_height_div = get_bits(&gb, 3); |
2692 | 26.3k | if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1) |
2693 | 25.6k | avpriv_request_sample(s->avctx, "unexpected macroblock dimension multiplier/divider"); |
2694 | | |
2695 | 26.3k | if (get_bits(&gb, 2)) |
2696 | 10.7k | avpriv_request_sample(s->avctx, "unknown bits"); |
2697 | 26.3k | } |
2698 | 60.4k | #endif |
2699 | 60.4k | } |
2700 | 124k | ff_progress_frame_replace(&s->golden_frame, &s->current_frame); |
2701 | 124k | } else { |
2702 | 92.4k | if (!s->golden_frame.f) { |
2703 | 32.5k | av_log(s->avctx, AV_LOG_WARNING, |
2704 | 32.5k | "vp3: first frame not a keyframe\n"); |
2705 | | |
2706 | 32.5k | if ((ret = ff_progress_frame_get_buffer(avctx, &s->golden_frame, |
2707 | 32.5k | AV_GET_BUFFER_FLAG_REF)) < 0) |
2708 | 0 | goto error; |
2709 | 32.5k | s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I; |
2710 | 32.5k | ff_progress_frame_replace(&s->last_frame, &s->golden_frame); |
2711 | 32.5k | ff_progress_frame_report(&s->golden_frame, INT_MAX); |
2712 | 32.5k | } |
2713 | 92.4k | } |
2714 | 217k | ff_thread_finish_setup(avctx); |
2715 | | |
2716 | 217k | memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment)); |
2717 | | |
2718 | 217k | if (s->version < 2) { |
2719 | 154k | if ((ret = unpack_superblocks(s, &gb)) < 0) { |
2720 | 18.9k | av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); |
2721 | 18.9k | goto error; |
2722 | 18.9k | } |
2723 | 154k | #if CONFIG_VP4_DECODER |
2724 | 154k | } else { |
2725 | 62.9k | if ((ret = vp4_unpack_macroblocks(s, &gb)) < 0) { |
2726 | 16.5k | av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n"); |
2727 | 16.5k | goto error; |
2728 | 16.5k | } |
2729 | 62.9k | #endif |
2730 | 62.9k | } |
2731 | 181k | if ((ret = unpack_modes(s, &gb)) < 0) { |
2732 | 19.8k | av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); |
2733 | 19.8k | goto error; |
2734 | 19.8k | } |
2735 | 161k | if (ret = unpack_vectors(s, &gb)) { |
2736 | 8.69k | av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); |
2737 | 8.69k | goto error; |
2738 | 8.69k | } |
2739 | 153k | if ((ret = unpack_block_qpis(s, &gb)) < 0) { |
2740 | 3.34k | av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); |
2741 | 3.34k | goto error; |
2742 | 3.34k | } |
2743 | | |
2744 | 149k | if (s->version < 2) { |
2745 | 108k | if ((ret = unpack_dct_coeffs(s, &gb)) < 0) { |
2746 | 45.0k | av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); |
2747 | 45.0k | goto error; |
2748 | 45.0k | } |
2749 | 108k | #if CONFIG_VP4_DECODER |
2750 | 108k | } else { |
2751 | 41.1k | if ((ret = vp4_unpack_dct_coeffs(s, &gb)) < 0) { |
2752 | 35.8k | av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n"); |
2753 | 35.8k | goto error; |
2754 | 35.8k | } |
2755 | 41.1k | #endif |
2756 | 41.1k | } |
2757 | | |
2758 | 275k | for (int i = 0; i < 3; i++) { |
2759 | 206k | int height = s->height >> (i && s->chroma_y_shift); |
2760 | 206k | if (s->flipped_image) |
2761 | 4.62k | s->data_offset[i] = 0; |
2762 | 201k | else |
2763 | 201k | s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i]; |
2764 | 206k | } |
2765 | | |
2766 | 68.8k | s->last_slice_end = 0; |
2767 | 425k | for (int i = 0; i < s->c_superblock_height; i++) |
2768 | 356k | render_slice(s, i); |
2769 | | |
2770 | | // filter the last row |
2771 | 68.8k | if (s->version < 2) |
2772 | 254k | for (int i = 0; i < 3; i++) { |
2773 | 190k | int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1; |
2774 | 190k | apply_loop_filter(s, i, row, row + 1); |
2775 | 190k | } |
2776 | 68.8k | vp3_draw_horiz_band(s, s->height); |
2777 | | |
2778 | 68.8k | ff_progress_frame_unref(&s->last_frame); |
2779 | | |
2780 | | /* output frame, offset as needed */ |
2781 | 68.8k | if ((ret = av_frame_ref(frame, s->current_frame.f)) < 0) |
2782 | 0 | return ret; |
2783 | | |
2784 | 68.8k | frame->crop_left = s->offset_x; |
2785 | 68.8k | frame->crop_right = avctx->coded_width - avctx->width - s->offset_x; |
2786 | 68.8k | frame->crop_top = s->offset_y; |
2787 | 68.8k | frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y; |
2788 | | |
2789 | 68.8k | *got_frame = 1; |
2790 | | |
2791 | 68.8k | return buf_size; |
2792 | | |
2793 | 148k | error: |
2794 | 148k | ff_progress_frame_report(&s->current_frame, INT_MAX); |
2795 | 148k | ff_progress_frame_unref(&s->last_frame); |
2796 | | |
2797 | 148k | return ret; |
2798 | 68.8k | } |
2799 | | |
2800 | | static int read_huffman_tree(HuffTable *huff, GetBitContext *gb, int length, |
2801 | | AVCodecContext *avctx) |
2802 | 438k | { |
2803 | 438k | if (get_bits1(gb)) { |
2804 | 180k | int token; |
2805 | 180k | if (huff->nb_entries >= 32) { /* overflow */ |
2806 | 245 | av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); |
2807 | 245 | return -1; |
2808 | 245 | } |
2809 | 180k | token = get_bits(gb, 5); |
2810 | 180k | ff_dlog(avctx, "code length %d, curr entry %d, token %d\n", |
2811 | 180k | length, huff->nb_entries, token); |
2812 | 180k | huff->entries[huff->nb_entries++] = (HuffEntry){ length, token }; |
2813 | 257k | } else { |
2814 | | /* The following bound follows from the fact that nb_entries <= 32. */ |
2815 | 257k | if (length >= 31) { /* overflow */ |
2816 | 6.02k | av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); |
2817 | 6.02k | return -1; |
2818 | 6.02k | } |
2819 | 251k | length++; |
2820 | 251k | if (read_huffman_tree(huff, gb, length, avctx)) |
2821 | 187k | return -1; |
2822 | 64.5k | if (read_huffman_tree(huff, gb, length, avctx)) |
2823 | 1.17k | return -1; |
2824 | 64.5k | } |
2825 | 243k | return 0; |
2826 | 438k | } |
2827 | | |
2828 | | #if CONFIG_THEORA_DECODER |
2829 | | static const enum AVPixelFormat theora_pix_fmts[4] = { |
2830 | | AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P |
2831 | | }; |
2832 | | |
2833 | | static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) |
2834 | 25.5k | { |
2835 | 25.5k | Vp3DecodeContext *s = avctx->priv_data; |
2836 | 25.5k | int visible_width, visible_height, colorspace; |
2837 | 25.5k | uint8_t offset_x = 0, offset_y = 0; |
2838 | 25.5k | int ret; |
2839 | 25.5k | AVRational fps, aspect; |
2840 | | |
2841 | 25.5k | if (get_bits_left(gb) < 206) |
2842 | 2.34k | return AVERROR_INVALIDDATA; |
2843 | | |
2844 | 23.2k | s->theora_header = 0; |
2845 | 23.2k | s->theora = get_bits(gb, 24); |
2846 | 23.2k | av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora); |
2847 | 23.2k | if (!s->theora) { |
2848 | 937 | s->theora = 1; |
2849 | 937 | avpriv_request_sample(s->avctx, "theora 0"); |
2850 | 937 | } |
2851 | | |
2852 | | /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 |
2853 | | * but previous versions have the image flipped relative to vp3 */ |
2854 | 23.2k | if (s->theora < 0x030200) { |
2855 | 2.47k | s->flipped_image = 1; |
2856 | 2.47k | av_log(avctx, AV_LOG_DEBUG, |
2857 | 2.47k | "Old (<alpha3) Theora bitstream, flipped image\n"); |
2858 | 2.47k | } |
2859 | | |
2860 | 23.2k | visible_width = |
2861 | 23.2k | s->width = get_bits(gb, 16) << 4; |
2862 | 23.2k | visible_height = |
2863 | 23.2k | s->height = get_bits(gb, 16) << 4; |
2864 | | |
2865 | 23.2k | if (s->theora >= 0x030200) { |
2866 | 20.7k | visible_width = get_bits(gb, 24); |
2867 | 20.7k | visible_height = get_bits(gb, 24); |
2868 | | |
2869 | 20.7k | offset_x = get_bits(gb, 8); /* offset x */ |
2870 | 20.7k | offset_y = get_bits(gb, 8); /* offset y, from bottom */ |
2871 | 20.7k | } |
2872 | | |
2873 | | /* sanity check */ |
2874 | 23.2k | if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 || |
2875 | 21.4k | visible_width + offset_x > s->width || |
2876 | 20.8k | visible_height + offset_y > s->height || |
2877 | 20.3k | visible_width + 512 < s->width || |
2878 | 18.7k | visible_height + 512 < s->height || |
2879 | 17.8k | visible_width < 18 |
2880 | 23.2k | ) { |
2881 | 5.57k | av_log(avctx, AV_LOG_ERROR, |
2882 | 5.57k | "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n", |
2883 | 5.57k | visible_width, visible_height, offset_x, offset_y, |
2884 | 5.57k | s->width, s->height); |
2885 | 5.57k | return AVERROR_INVALIDDATA; |
2886 | 5.57k | } |
2887 | | |
2888 | 17.6k | fps.num = get_bits_long(gb, 32); |
2889 | 17.6k | fps.den = get_bits_long(gb, 32); |
2890 | 17.6k | if (fps.num && fps.den) { |
2891 | 15.3k | if (fps.num < 0 || fps.den < 0) { |
2892 | 715 | av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n"); |
2893 | 715 | return AVERROR_INVALIDDATA; |
2894 | 715 | } |
2895 | 14.6k | av_reduce(&avctx->framerate.den, &avctx->framerate.num, |
2896 | 14.6k | fps.den, fps.num, 1 << 30); |
2897 | 14.6k | } |
2898 | | |
2899 | 16.9k | aspect.num = get_bits(gb, 24); |
2900 | 16.9k | aspect.den = get_bits(gb, 24); |
2901 | 16.9k | if (aspect.num && aspect.den) { |
2902 | 9.93k | av_reduce(&avctx->sample_aspect_ratio.num, |
2903 | 9.93k | &avctx->sample_aspect_ratio.den, |
2904 | 9.93k | aspect.num, aspect.den, 1 << 30); |
2905 | 9.93k | ff_set_sar(avctx, avctx->sample_aspect_ratio); |
2906 | 9.93k | } |
2907 | | |
2908 | 16.9k | if (s->theora < 0x030200) |
2909 | 1.46k | skip_bits(gb, 5); /* keyframe frequency force */ |
2910 | 16.9k | colorspace = get_bits(gb, 8); |
2911 | 16.9k | skip_bits(gb, 24); /* bitrate */ |
2912 | | |
2913 | 16.9k | skip_bits(gb, 6); /* quality hint */ |
2914 | | |
2915 | 16.9k | if (s->theora >= 0x030200) { |
2916 | 15.4k | skip_bits(gb, 5); /* keyframe frequency force */ |
2917 | 15.4k | avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)]; |
2918 | 15.4k | if (avctx->pix_fmt == AV_PIX_FMT_NONE) { |
2919 | 589 | av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n"); |
2920 | 589 | return AVERROR_INVALIDDATA; |
2921 | 589 | } |
2922 | 14.8k | skip_bits(gb, 3); /* reserved */ |
2923 | 14.8k | } else |
2924 | 1.46k | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
2925 | | |
2926 | 16.3k | if (s->width < 18) |
2927 | 0 | return AVERROR_PATCHWELCOME; |
2928 | 16.3k | ret = ff_set_dimensions(avctx, s->width, s->height); |
2929 | 16.3k | if (ret < 0) |
2930 | 1.06k | return ret; |
2931 | 15.2k | if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) { |
2932 | 15.2k | avctx->width = visible_width; |
2933 | 15.2k | avctx->height = visible_height; |
2934 | | // translate offsets from theora axis ([0,0] lower left) |
2935 | | // to normal axis ([0,0] upper left) |
2936 | 15.2k | s->offset_x = offset_x; |
2937 | 15.2k | s->offset_y = s->height - visible_height - offset_y; |
2938 | 15.2k | } |
2939 | | |
2940 | 15.2k | if (colorspace == 1) |
2941 | 783 | avctx->color_primaries = AVCOL_PRI_BT470M; |
2942 | 14.4k | else if (colorspace == 2) |
2943 | 1.07k | avctx->color_primaries = AVCOL_PRI_BT470BG; |
2944 | | |
2945 | 15.2k | if (colorspace == 1 || colorspace == 2) { |
2946 | 1.86k | avctx->colorspace = AVCOL_SPC_BT470BG; |
2947 | 1.86k | avctx->color_trc = AVCOL_TRC_BT709; |
2948 | 1.86k | } |
2949 | | |
2950 | 15.2k | s->theora_header = 1; |
2951 | 15.2k | return 0; |
2952 | 16.3k | } |
2953 | | |
2954 | | static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) |
2955 | 9.15k | { |
2956 | 9.15k | Vp3DecodeContext *s = avctx->priv_data; |
2957 | 9.15k | int n, matrices, ret; |
2958 | | |
2959 | 9.15k | if (!s->theora_header) |
2960 | 608 | return AVERROR_INVALIDDATA; |
2961 | | |
2962 | 8.54k | if (s->theora >= 0x030200) { |
2963 | 8.26k | n = get_bits(gb, 3); |
2964 | | /* loop filter limit values table */ |
2965 | 8.26k | if (n) |
2966 | 351k | for (int i = 0; i < 64; i++) |
2967 | 345k | s->filter_limit_values[i] = get_bits(gb, n); |
2968 | 8.26k | } |
2969 | | |
2970 | 8.54k | if (s->theora >= 0x030200) |
2971 | 8.26k | n = get_bits(gb, 4) + 1; |
2972 | 281 | else |
2973 | 281 | n = 16; |
2974 | | /* quality threshold table */ |
2975 | 555k | for (int i = 0; i < 64; i++) |
2976 | 546k | s->coded_ac_scale_factor[i] = get_bits(gb, n); |
2977 | | |
2978 | 8.54k | if (s->theora >= 0x030200) |
2979 | 8.26k | n = get_bits(gb, 4) + 1; |
2980 | 281 | else |
2981 | 281 | n = 16; |
2982 | | /* dc scale factor table */ |
2983 | 555k | for (int i = 0; i < 64; i++) |
2984 | 546k | s->coded_dc_scale_factor[0][i] = |
2985 | 546k | s->coded_dc_scale_factor[1][i] = get_bits(gb, n); |
2986 | | |
2987 | 8.54k | if (s->theora >= 0x030200) |
2988 | 8.26k | matrices = get_bits(gb, 9) + 1; |
2989 | 281 | else |
2990 | 281 | matrices = 3; |
2991 | | |
2992 | 8.54k | if (matrices > 384) { |
2993 | 248 | av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n"); |
2994 | 248 | return -1; |
2995 | 248 | } |
2996 | | |
2997 | 60.6k | for (int j = 0; j < matrices; j++) |
2998 | 3.40M | for (int i = 0; i < 64; i++) |
2999 | 3.35M | s->base_matrix[j][i] = get_bits(gb, 8); |
3000 | | |
3001 | 23.6k | for (int inter = 0; inter <= 1; inter++) { |
3002 | 62.1k | for (int plane = 0; plane <= 2; plane++) { |
3003 | 46.7k | int newqr = 1; |
3004 | 46.7k | if (inter || plane > 0) |
3005 | 38.4k | newqr = get_bits1(gb); |
3006 | 46.7k | if (!newqr) { |
3007 | 37.5k | int qtj, plj; |
3008 | 37.5k | if (inter && get_bits1(gb)) { |
3009 | 2.25k | qtj = 0; |
3010 | 2.25k | plj = plane; |
3011 | 35.3k | } else { |
3012 | 35.3k | qtj = (3 * inter + plane - 1) / 3; |
3013 | 35.3k | plj = (plane + 2) % 3; |
3014 | 35.3k | } |
3015 | 37.5k | s->qr_count[inter][plane] = s->qr_count[qtj][plj]; |
3016 | 37.5k | memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], |
3017 | 37.5k | sizeof(s->qr_size[0][0])); |
3018 | 37.5k | memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], |
3019 | 37.5k | sizeof(s->qr_base[0][0])); |
3020 | 37.5k | } else { |
3021 | 9.19k | int qri = 0; |
3022 | 9.19k | int qi = 0; |
3023 | | |
3024 | 451k | for (;;) { |
3025 | 451k | int i = get_bits(gb, av_log2(matrices - 1) + 1); |
3026 | 451k | if (i >= matrices) { |
3027 | 396 | av_log(avctx, AV_LOG_ERROR, |
3028 | 396 | "invalid base matrix index\n"); |
3029 | 396 | return -1; |
3030 | 396 | } |
3031 | 450k | s->qr_base[inter][plane][qri] = i; |
3032 | 450k | if (qi >= 63) |
3033 | 8.80k | break; |
3034 | 442k | i = get_bits(gb, av_log2(63 - qi) + 1) + 1; |
3035 | 442k | s->qr_size[inter][plane][qri++] = i; |
3036 | 442k | qi += i; |
3037 | 442k | } |
3038 | | |
3039 | 8.80k | if (qi > 63) { |
3040 | 241 | av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); |
3041 | 241 | return -1; |
3042 | 241 | } |
3043 | 8.56k | s->qr_count[inter][plane] = qri; |
3044 | 8.56k | } |
3045 | 46.7k | } |
3046 | 15.9k | } |
3047 | | |
3048 | | /* Huffman tables */ |
3049 | 123k | for (int i = 0; i < FF_ARRAY_ELEMS(s->huffman_table); i++) { |
3050 | 121k | s->huffman_table[i].nb_entries = 0; |
3051 | 121k | if ((ret = read_huffman_tree(&s->huffman_table[i], gb, 0, avctx)) < 0) |
3052 | 6.26k | return ret; |
3053 | 121k | } |
3054 | | |
3055 | 1.39k | s->theora_tables = 1; |
3056 | | |
3057 | 1.39k | return 0; |
3058 | 7.66k | } |
3059 | | |
3060 | | static av_cold int theora_decode_init(AVCodecContext *avctx) |
3061 | 3.46k | { |
3062 | 3.46k | Vp3DecodeContext *s = avctx->priv_data; |
3063 | 3.46k | GetBitContext gb; |
3064 | 3.46k | int ptype; |
3065 | 3.46k | const uint8_t *header_start[3]; |
3066 | 3.46k | int header_len[3]; |
3067 | 3.46k | int ret; |
3068 | | |
3069 | 3.46k | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
3070 | | |
3071 | 3.46k | s->theora = 1; |
3072 | | |
3073 | 3.46k | if (!avctx->extradata_size) { |
3074 | 182 | av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n"); |
3075 | 182 | return -1; |
3076 | 182 | } |
3077 | | |
3078 | 3.28k | if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size, |
3079 | 3.28k | 42, header_start, header_len) < 0) { |
3080 | 51 | av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n"); |
3081 | 51 | return -1; |
3082 | 51 | } |
3083 | | |
3084 | 5.46k | for (int i = 0; i < 3; i++) { |
3085 | 5.43k | if (header_len[i] <= 0) |
3086 | 2.22k | continue; |
3087 | 3.21k | ret = init_get_bits8(&gb, header_start[i], header_len[i]); |
3088 | 3.21k | if (ret < 0) |
3089 | 0 | return ret; |
3090 | | |
3091 | 3.21k | ptype = get_bits(&gb, 8); |
3092 | | |
3093 | 3.21k | if (!(ptype & 0x80)) { |
3094 | 1.73k | av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n"); |
3095 | | // return -1; |
3096 | 1.73k | } |
3097 | | |
3098 | | // FIXME: Check for this as well. |
3099 | 3.21k | skip_bits_long(&gb, 6 * 8); /* "theora" */ |
3100 | | |
3101 | 3.21k | switch (ptype) { |
3102 | 56 | case 0x80: |
3103 | 56 | if (theora_decode_header(avctx, &gb) < 0) |
3104 | 8 | return -1; |
3105 | 48 | break; |
3106 | 48 | case 0x81: |
3107 | | // FIXME: is this needed? it breaks sometimes |
3108 | | // theora_decode_comments(avctx, gb); |
3109 | 16 | break; |
3110 | 1 | case 0x82: |
3111 | 1 | if (theora_decode_tables(avctx, &gb)) |
3112 | 1 | return -1; |
3113 | 0 | break; |
3114 | 3.14k | default: |
3115 | 3.14k | av_log(avctx, AV_LOG_ERROR, |
3116 | 3.14k | "Unknown Theora config packet: %d\n", ptype & ~0x80); |
3117 | 3.14k | break; |
3118 | 3.21k | } |
3119 | 3.20k | if (ptype != 0x81 && get_bits_left(&gb) >= 8U) |
3120 | 2.80k | av_log(avctx, AV_LOG_WARNING, |
3121 | 2.80k | "%d bits left in packet %X\n", |
3122 | 2.80k | get_bits_left(&gb), ptype); |
3123 | 3.20k | if (s->theora < 0x030200) |
3124 | 3.19k | break; |
3125 | 3.20k | } |
3126 | | |
3127 | 3.22k | return vp3_decode_init(avctx); |
3128 | 3.23k | } |
3129 | | |
3130 | | const FFCodec ff_theora_decoder = { |
3131 | | .p.name = "theora", |
3132 | | CODEC_LONG_NAME("Theora"), |
3133 | | .p.type = AVMEDIA_TYPE_VIDEO, |
3134 | | .p.id = AV_CODEC_ID_THEORA, |
3135 | | .priv_data_size = sizeof(Vp3DecodeContext), |
3136 | | .init = theora_decode_init, |
3137 | | .close = vp3_decode_end, |
3138 | | FF_CODEC_DECODE_CB(vp3_decode_frame), |
3139 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND | |
3140 | | AV_CODEC_CAP_FRAME_THREADS, |
3141 | | .flush = vp3_decode_flush, |
3142 | | UPDATE_THREAD_CONTEXT(vp3_update_thread_context), |
3143 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
3144 | | FF_CODEC_CAP_EXPORTS_CROPPING | |
3145 | | FF_CODEC_CAP_USES_PROGRESSFRAMES, |
3146 | | }; |
3147 | | #endif |
3148 | | |
3149 | | const FFCodec ff_vp3_decoder = { |
3150 | | .p.name = "vp3", |
3151 | | CODEC_LONG_NAME("On2 VP3"), |
3152 | | .p.type = AVMEDIA_TYPE_VIDEO, |
3153 | | .p.id = AV_CODEC_ID_VP3, |
3154 | | .priv_data_size = sizeof(Vp3DecodeContext), |
3155 | | .init = vp3_decode_init, |
3156 | | .close = vp3_decode_end, |
3157 | | FF_CODEC_DECODE_CB(vp3_decode_frame), |
3158 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND | |
3159 | | AV_CODEC_CAP_FRAME_THREADS, |
3160 | | .flush = vp3_decode_flush, |
3161 | | UPDATE_THREAD_CONTEXT(vp3_update_thread_context), |
3162 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
3163 | | FF_CODEC_CAP_USES_PROGRESSFRAMES, |
3164 | | }; |
3165 | | |
3166 | | #if CONFIG_VP4_DECODER |
3167 | | const FFCodec ff_vp4_decoder = { |
3168 | | .p.name = "vp4", |
3169 | | CODEC_LONG_NAME("On2 VP4"), |
3170 | | .p.type = AVMEDIA_TYPE_VIDEO, |
3171 | | .p.id = AV_CODEC_ID_VP4, |
3172 | | .priv_data_size = sizeof(Vp3DecodeContext), |
3173 | | .init = vp3_decode_init, |
3174 | | .close = vp3_decode_end, |
3175 | | FF_CODEC_DECODE_CB(vp3_decode_frame), |
3176 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND | |
3177 | | AV_CODEC_CAP_FRAME_THREADS, |
3178 | | .flush = vp3_decode_flush, |
3179 | | UPDATE_THREAD_CONTEXT(vp3_update_thread_context), |
3180 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
3181 | | FF_CODEC_CAP_USES_PROGRESSFRAMES, |
3182 | | }; |
3183 | | #endif |