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