/src/ffmpeg/libavcodec/ffv1dec.c
Line | Count | Source |
1 | | /* |
2 | | * FFV1 decoder |
3 | | * |
4 | | * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at> |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | /** |
24 | | * @file |
25 | | * FF Video Codec 1 (a lossless codec) decoder |
26 | | */ |
27 | | |
28 | | #include "libavutil/avassert.h" |
29 | | #include "libavutil/crc.h" |
30 | | #include "libavutil/mem.h" |
31 | | #include "libavutil/imgutils.h" |
32 | | #include "libavutil/pixdesc.h" |
33 | | #include "avcodec.h" |
34 | | #include "codec_internal.h" |
35 | | #include "get_bits.h" |
36 | | #include "rangecoder.h" |
37 | | #include "golomb.h" |
38 | | #include "mathops.h" |
39 | | #include "ffv1.h" |
40 | | #include "progressframe.h" |
41 | | #include "libavutil/refstruct.h" |
42 | | #include "thread.h" |
43 | | #include "decode.h" |
44 | | #include "hwconfig.h" |
45 | | #include "hwaccel_internal.h" |
46 | | #include "config_components.h" |
47 | | |
48 | | static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state, |
49 | | int bits) |
50 | 40.2M | { |
51 | 40.2M | int k, i, v, ret; |
52 | | |
53 | 40.2M | i = state->count; |
54 | 40.2M | k = 0; |
55 | 145M | while (i < state->error_sum) { // FIXME: optimize |
56 | 105M | k++; |
57 | 105M | i += i; |
58 | 105M | } |
59 | 40.2M | if (k > bits) { |
60 | 53.6k | ff_dlog(NULL, "k-overflow bias:%d error:%d drift:%d count:%d k:%d", |
61 | 53.6k | state->bias, state->error_sum, state->drift, state->count, k); |
62 | 53.6k | k = bits; |
63 | 53.6k | } |
64 | | |
65 | 40.2M | v = get_sr_golomb(gb, k, 12, bits); |
66 | 40.2M | ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d", |
67 | 40.2M | v, state->bias, state->error_sum, state->drift, state->count, k); |
68 | | |
69 | 40.2M | v ^= ((2 * state->drift + state->count) >> 31); |
70 | | |
71 | 40.2M | ret = fold(v + state->bias, bits); |
72 | | |
73 | 40.2M | update_vlc_state(state, v); |
74 | | |
75 | 40.2M | return ret; |
76 | 40.2M | } |
77 | | |
78 | | static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac) |
79 | 12.1M | { |
80 | 12.1M | if (ac != AC_GOLOMB_RICE) { |
81 | 3.29M | if (c->overread > MAX_OVERREAD) |
82 | 7.35k | return AVERROR_INVALIDDATA; |
83 | 8.88M | } else { |
84 | 8.88M | if (get_bits_left(gb) < 1) |
85 | 45.1k | return AVERROR_INVALIDDATA; |
86 | 8.88M | } |
87 | 12.1M | return 0; |
88 | 12.1M | } |
89 | | |
90 | 1.86M | #define TYPE int16_t |
91 | 330M | #define RENAME(name) name |
92 | | #include "ffv1dec_template.c" |
93 | | #undef TYPE |
94 | | #undef RENAME |
95 | | |
96 | 656k | #define TYPE int32_t |
97 | 92.5M | #define RENAME(name) name ## 32 |
98 | | #include "ffv1dec_template.c" |
99 | | |
100 | | static int decode_plane(FFV1Context *f, FFV1SliceContext *sc, |
101 | | GetBitContext *gb, |
102 | | uint8_t *src, int w, int h, int stride, int plane_index, |
103 | | int remap_index, int pixel_stride, int ac) |
104 | 165k | { |
105 | 165k | int x, y; |
106 | 165k | int16_t *sample[2]; |
107 | 165k | int bits; |
108 | 165k | unsigned mask; |
109 | | |
110 | 165k | if (sc->remap) { |
111 | 0 | bits = av_ceil_log2(sc->remap_count[remap_index]); |
112 | 0 | mask = (1<<bits)-1; |
113 | |
|
114 | 0 | av_assert0(sc->fltmap_size[remap_index] >= (mask + 1) * sizeof(*sc->fltmap[remap_index])); |
115 | 165k | } else { |
116 | 165k | bits = f->avctx->bits_per_raw_sample; |
117 | 165k | } |
118 | | |
119 | 165k | sample[0] = sc->sample_buffer + 3; |
120 | 165k | sample[1] = sc->sample_buffer + w + 6 + 3; |
121 | | |
122 | 165k | sc->run_index = 0; |
123 | | |
124 | 165k | memset(sc->sample_buffer, 0, 2 * (w + 6) * sizeof(*sc->sample_buffer)); |
125 | | |
126 | 3.73M | for (y = 0; y < h; y++) { |
127 | 3.60M | int16_t *temp = sample[0]; // FIXME: try a normal buffer |
128 | | |
129 | 3.60M | sample[0] = sample[1]; |
130 | 3.60M | sample[1] = temp; |
131 | | |
132 | 3.60M | sample[1][-1] = sample[0][0]; |
133 | 3.60M | sample[0][w] = sample[0][w - 1]; |
134 | | |
135 | 3.60M | if (f->avctx->bits_per_raw_sample <= 8) { |
136 | 2.08M | int ret = decode_line(f, sc, gb, w, sample, plane_index, 8, ac); |
137 | 2.08M | if (ret < 0) |
138 | 16.9k | return ret; |
139 | 2.06M | if (sc->remap) |
140 | 0 | for (x = 0; x < w; x++) |
141 | 0 | sample[1][x] = sc->fltmap[remap_index][sample[1][x]]; |
142 | 510M | for (x = 0; x < w; x++) |
143 | 508M | src[x*pixel_stride + stride * y] = sample[1][x]; |
144 | 2.06M | } else { |
145 | 1.52M | int ret = decode_line(f, sc, gb, w, sample, plane_index, bits, ac); |
146 | 1.52M | if (ret < 0) |
147 | 18.9k | return ret; |
148 | | |
149 | 1.50M | if (sc->remap) { |
150 | 0 | if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) { |
151 | 0 | for (x = 0; x < w; x++) { |
152 | 0 | ((uint16_t*)(src + stride*y))[x*pixel_stride] = sc->fltmap[remap_index][sample[1][x] & mask]; |
153 | 0 | } |
154 | 0 | } else { |
155 | 0 | for (x = 0; x < w; x++) { |
156 | 0 | int v = sc->fltmap[remap_index][sample[1][x] & mask]; |
157 | 0 | ((uint16_t*)(src + stride*y))[x*pixel_stride] = v << (16 - f->avctx->bits_per_raw_sample) | v >> (2 * f->avctx->bits_per_raw_sample - 16); |
158 | 0 | } |
159 | 0 | } |
160 | 1.50M | } else { |
161 | 1.50M | if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) { |
162 | 185M | for (x = 0; x < w; x++) { |
163 | 184M | ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x]; |
164 | 184M | } |
165 | 1.50M | } else { |
166 | 0 | for (x = 0; x < w; x++) { |
167 | 0 | ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x] << (16 - f->avctx->bits_per_raw_sample) | ((uint16_t **)sample)[1][x] >> (2 * f->avctx->bits_per_raw_sample - 16); |
168 | 0 | } |
169 | 0 | } |
170 | 1.50M | } |
171 | 1.50M | } |
172 | 3.60M | } |
173 | 130k | return 0; |
174 | 165k | } |
175 | | |
176 | | static int decode_slice_header(const FFV1Context *f, |
177 | | FFV1SliceContext *sc, AVFrame *frame) |
178 | 0 | { |
179 | 0 | RangeCoder *c = &sc->c; |
180 | 0 | uint8_t state[CONTEXT_SIZE]; |
181 | 0 | unsigned ps, context_count; |
182 | 0 | int sx, sy, sw, sh; |
183 | |
|
184 | 0 | memset(state, 128, sizeof(state)); |
185 | 0 | sx = ff_ffv1_get_symbol(c, state, 0); |
186 | 0 | sy = ff_ffv1_get_symbol(c, state, 0); |
187 | 0 | sw = ff_ffv1_get_symbol(c, state, 0) + 1U; |
188 | 0 | sh = ff_ffv1_get_symbol(c, state, 0) + 1U; |
189 | |
|
190 | 0 | av_assert0(f->version > 2); |
191 | | |
192 | | |
193 | 0 | if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0) |
194 | 0 | return AVERROR_INVALIDDATA; |
195 | 0 | if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh) |
196 | 0 | return AVERROR_INVALIDDATA; |
197 | | |
198 | 0 | sc->slice_x = ff_slice_coord(f, f->width , sx , f->num_h_slices, f->chroma_h_shift); |
199 | 0 | sc->slice_y = ff_slice_coord(f, f->height, sy , f->num_v_slices, f->chroma_v_shift); |
200 | 0 | sc->slice_width = ff_slice_coord(f, f->width , sx + sw, f->num_h_slices, f->chroma_h_shift) - sc->slice_x; |
201 | 0 | sc->slice_height = ff_slice_coord(f, f->height, sy + sh, f->num_v_slices, f->chroma_v_shift) - sc->slice_y; |
202 | |
|
203 | 0 | av_assert0((unsigned)sc->slice_width <= f->width && |
204 | 0 | (unsigned)sc->slice_height <= f->height); |
205 | 0 | av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width |
206 | 0 | && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height); |
207 | | |
208 | 0 | if (f->ac == AC_GOLOMB_RICE && sc->slice_width >= (1<<23)) |
209 | 0 | return AVERROR_INVALIDDATA; |
210 | | |
211 | 0 | for (unsigned i = 0; i < f->plane_count; i++) { |
212 | 0 | PlaneContext * const p = &sc->plane[i]; |
213 | 0 | int idx = ff_ffv1_get_symbol(c, state, 0); |
214 | 0 | if (idx >= (unsigned)f->quant_table_count) { |
215 | 0 | av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n"); |
216 | 0 | return -1; |
217 | 0 | } |
218 | 0 | p->quant_table_index = idx; |
219 | 0 | context_count = f->context_count[idx]; |
220 | |
|
221 | 0 | if (p->context_count < context_count) { |
222 | 0 | av_freep(&p->state); |
223 | 0 | av_freep(&p->vlc_state); |
224 | 0 | } |
225 | 0 | p->context_count = context_count; |
226 | 0 | } |
227 | | |
228 | 0 | ps = ff_ffv1_get_symbol(c, state, 0); |
229 | 0 | if (ps == 1) { |
230 | 0 | frame->flags |= AV_FRAME_FLAG_INTERLACED; |
231 | 0 | frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; |
232 | 0 | } else if (ps == 2) { |
233 | 0 | frame->flags |= AV_FRAME_FLAG_INTERLACED; |
234 | 0 | frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST; |
235 | 0 | } else if (ps == 3) { |
236 | 0 | frame->flags &= ~AV_FRAME_FLAG_INTERLACED; |
237 | 0 | } |
238 | 0 | frame->sample_aspect_ratio.num = ff_ffv1_get_symbol(c, state, 0); |
239 | 0 | frame->sample_aspect_ratio.den = ff_ffv1_get_symbol(c, state, 0); |
240 | |
|
241 | 0 | if (av_image_check_sar(f->width, f->height, |
242 | 0 | frame->sample_aspect_ratio) < 0) { |
243 | 0 | av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n", |
244 | 0 | frame->sample_aspect_ratio.num, |
245 | 0 | frame->sample_aspect_ratio.den); |
246 | 0 | frame->sample_aspect_ratio = (AVRational){ 0, 1 }; |
247 | 0 | } |
248 | |
|
249 | 0 | if (f->version > 3) { |
250 | 0 | sc->slice_reset_contexts = get_rac(c, state); |
251 | 0 | sc->slice_coding_mode = ff_ffv1_get_symbol(c, state, 0); |
252 | 0 | if (sc->slice_coding_mode != 1 && f->colorspace == 1) { |
253 | 0 | sc->slice_rct_by_coef = ff_ffv1_get_symbol(c, state, 0); |
254 | 0 | sc->slice_rct_ry_coef = ff_ffv1_get_symbol(c, state, 0); |
255 | 0 | if ((uint64_t)sc->slice_rct_by_coef + (uint64_t)sc->slice_rct_ry_coef > 4) { |
256 | 0 | av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n"); |
257 | 0 | return AVERROR_INVALIDDATA; |
258 | 0 | } |
259 | 0 | } |
260 | 0 | if (f->combined_version >= 0x40004) { |
261 | 0 | sc->remap = ff_ffv1_get_symbol(c, state, 0); |
262 | 0 | if (sc->remap > 2U || |
263 | 0 | sc->remap && !f->flt) { |
264 | 0 | av_log(f->avctx, AV_LOG_ERROR, "unsupported remap %d\n", sc->remap); |
265 | 0 | return AVERROR_INVALIDDATA; |
266 | 0 | } |
267 | 0 | } |
268 | 0 | } |
269 | 0 | if (f->avctx->bits_per_raw_sample == 32) { |
270 | 0 | if (!sc->remap) { |
271 | 0 | av_log(f->avctx, AV_LOG_ERROR, "unsupported remap\n"); |
272 | 0 | return AVERROR_INVALIDDATA; |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | 0 | return 0; |
277 | 0 | } |
278 | | |
279 | | static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc) |
280 | 0 | { |
281 | 0 | sc->slice_damaged = 1; |
282 | | |
283 | | // only set this for frame threading, as for slice threading its value is |
284 | | // not used and setting it would be a race |
285 | 0 | if (f->avctx->active_thread_type & FF_THREAD_FRAME) |
286 | 0 | f->frame_damaged = 1; |
287 | 0 | } |
288 | | |
289 | | static int decode_current_mul(RangeCoder *rc, uint8_t state[32], int *mul, int mul_count, int64_t i) |
290 | 0 | { |
291 | 0 | int ndx = (i * mul_count) >> 32; |
292 | 0 | av_assert2(ndx <= 4096U); |
293 | |
|
294 | 0 | if (mul[ndx] < 0) |
295 | 0 | mul[ndx] = ff_ffv1_get_symbol(rc, state, 0) & 0x3FFFFFFF; |
296 | |
|
297 | 0 | return mul[ndx]; |
298 | 0 | } |
299 | | |
300 | | static int decode_remap(FFV1Context *f, FFV1SliceContext *sc) |
301 | 0 | { |
302 | 0 | unsigned int end = (1LL<<f->avctx->bits_per_raw_sample) - 1; |
303 | 0 | int flip = sc->remap == 2 ? (end>>1) : 0; |
304 | |
|
305 | 0 | for (int p = 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) { |
306 | 0 | int j = 0; |
307 | 0 | int lu = 0; |
308 | 0 | uint8_t state[2][3][32]; |
309 | 0 | int64_t i; |
310 | 0 | int mul[4096+1]; |
311 | 0 | int mul_count; |
312 | |
|
313 | 0 | const int is_chroma = (p == 1 || p == 2) && f->chroma_planes; |
314 | 0 | const int plane_width = AV_CEIL_RSHIFT(sc->slice_width , is_chroma ? f->chroma_h_shift : 0); |
315 | 0 | const int plane_height = AV_CEIL_RSHIFT(sc->slice_height, is_chroma ? f->chroma_v_shift : 0); |
316 | 0 | const int pixel_num = FFMIN(plane_width * plane_height, end + 1LL); |
317 | 0 | const size_t fltmap_ceil = 1ULL << av_ceil_log2(pixel_num); |
318 | |
|
319 | 0 | if (f->avctx->bits_per_raw_sample == 32) { |
320 | 0 | av_fast_malloc(&sc->fltmap32[p], &sc->fltmap32_size[p], fltmap_ceil * sizeof(*sc->fltmap32[p])); |
321 | 0 | if (!sc->fltmap32[p]) |
322 | 0 | return AVERROR(ENOMEM); |
323 | 0 | } else { |
324 | 0 | av_fast_malloc(&sc->fltmap[p] , &sc->fltmap_size[p] , fltmap_ceil * sizeof(*sc->fltmap[p])); |
325 | 0 | if (!sc->fltmap[p]) |
326 | 0 | return AVERROR(ENOMEM); |
327 | 0 | } |
328 | | |
329 | 0 | memset(state, 128, sizeof(state)); |
330 | 0 | mul_count = ff_ffv1_get_symbol(&sc->c, state[0][0], 0); |
331 | |
|
332 | 0 | if (mul_count > 4096U) |
333 | 0 | return AVERROR_INVALIDDATA; |
334 | 0 | for (int i = 0; i<mul_count; i++) { |
335 | 0 | mul[i] = -1; |
336 | |
|
337 | 0 | } |
338 | 0 | mul[mul_count] = 1; |
339 | |
|
340 | 0 | memset(state, 128, sizeof(state)); |
341 | 0 | int current_mul = 1; |
342 | 0 | for (i=0; i <= end ;) { |
343 | 0 | unsigned run = get_symbol_inline(&sc->c, state[lu][0], 0); |
344 | 0 | unsigned run0 = lu ? 0 : run; |
345 | 0 | unsigned run1 = lu ? run : 1; |
346 | |
|
347 | 0 | i += run0 * current_mul; |
348 | |
|
349 | 0 | while (run1--) { |
350 | 0 | if (current_mul > 1) { |
351 | 0 | int delta = get_symbol_inline(&sc->c, state[lu][1], 1); |
352 | 0 | if (delta <= -current_mul || delta > current_mul/2) |
353 | 0 | return AVERROR_INVALIDDATA; //not sure we should check this |
354 | 0 | i += current_mul - 1 + delta; |
355 | 0 | } |
356 | 0 | if (i - 1 >= end) |
357 | 0 | break; |
358 | 0 | if (j >= pixel_num) |
359 | 0 | return AVERROR_INVALIDDATA; |
360 | 0 | if (end <= 0xFFFF) { |
361 | 0 | sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip); |
362 | 0 | } else |
363 | 0 | sc->fltmap32[p][j++] = i ^ ((i&0x80000000) ? 0 : flip); |
364 | 0 | i++; |
365 | 0 | current_mul = decode_current_mul(&sc->c, state[0][2], mul, mul_count, i); |
366 | 0 | } |
367 | 0 | if (lu) { |
368 | 0 | i += current_mul; |
369 | 0 | } |
370 | 0 | lu ^= !run; |
371 | 0 | } |
372 | 0 | sc->remap_count[p] = j; |
373 | 0 | } |
374 | 0 | return 0; |
375 | 0 | } |
376 | | |
377 | | static int decode_slice(AVCodecContext *c, void *arg) |
378 | 96.6k | { |
379 | 96.6k | FFV1Context *f = c->priv_data; |
380 | 96.6k | FFV1SliceContext *sc = arg; |
381 | 96.6k | int width, height, x, y, ret; |
382 | 96.6k | const int ps = av_pix_fmt_desc_get(f->pix_fmt)->comp[0].step; |
383 | 96.6k | AVFrame * const p = f->picture.f; |
384 | 96.6k | const int si = sc - f->slices; |
385 | 96.6k | GetBitContext gb; |
386 | 96.6k | int ac = f->ac || sc->slice_coding_mode == 1; |
387 | | |
388 | 96.6k | if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f) |
389 | 90.5k | ff_progress_frame_await(&f->last_picture, si); |
390 | | |
391 | 96.6k | if (f->slice_damaged[si]) |
392 | 0 | slice_set_damaged(f, sc); |
393 | | |
394 | 96.6k | sc->slice_rct_by_coef = 1; |
395 | 96.6k | sc->slice_rct_ry_coef = 1; |
396 | | |
397 | 96.6k | if (f->version > 2) { |
398 | 0 | if (ff_ffv1_init_slice_state(f, sc) < 0) |
399 | 0 | return AVERROR(ENOMEM); |
400 | 0 | if (decode_slice_header(f, sc, p) < 0) { |
401 | 0 | sc->slice_x = sc->slice_y = sc->slice_height = sc->slice_width = 0; |
402 | 0 | slice_set_damaged(f, sc); |
403 | 0 | return AVERROR_INVALIDDATA; |
404 | 0 | } |
405 | 0 | } |
406 | 96.6k | if ((ret = ff_ffv1_init_slice_state(f, sc)) < 0) |
407 | 0 | return ret; |
408 | 96.6k | if ((p->flags & AV_FRAME_FLAG_KEY) || sc->slice_reset_contexts) { |
409 | 5.02k | ff_ffv1_clear_slice_state(f, sc); |
410 | 91.6k | } else if (sc->slice_damaged) { |
411 | 0 | return AVERROR_INVALIDDATA; |
412 | 0 | } |
413 | | |
414 | 96.6k | width = sc->slice_width; |
415 | 96.6k | height = sc->slice_height; |
416 | 96.6k | x = sc->slice_x; |
417 | 96.6k | y = sc->slice_y; |
418 | | |
419 | 96.6k | if (sc->remap) { |
420 | 0 | ret = decode_remap(f, sc); |
421 | 0 | if (ret < 0) |
422 | 0 | return ret; |
423 | 0 | } |
424 | | |
425 | 96.6k | if (ac == AC_GOLOMB_RICE) { |
426 | 81.3k | if (f->combined_version >= 0x30002) |
427 | 0 | get_rac(&sc->c, (uint8_t[]) { 129 }); |
428 | 81.3k | sc->ac_byte_count = f->version > 2 || (!x && !y) ? sc->c.bytestream - sc->c.bytestream_start - 1 : 0; |
429 | 81.3k | init_get_bits(&gb, |
430 | 81.3k | sc->c.bytestream_start + sc->ac_byte_count, |
431 | 81.3k | (sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count) * 8); |
432 | 81.3k | } |
433 | | |
434 | 96.6k | av_assert1(width && height); |
435 | 96.6k | if (f->colorspace == 0 && (f->chroma_planes || !f->transparency)) { |
436 | 62.7k | const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift); |
437 | 62.7k | const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift); |
438 | 62.7k | const int cx = x >> f->chroma_h_shift; |
439 | 62.7k | const int cy = y >> f->chroma_v_shift; |
440 | 62.7k | decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 1, ac); |
441 | | |
442 | 62.7k | if (f->chroma_planes) { |
443 | 50.6k | decode_plane(f, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, 1, ac); |
444 | 50.6k | decode_plane(f, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 2, 1, ac); |
445 | 50.6k | } |
446 | 62.7k | if (f->transparency) |
447 | 1.73k | decode_plane(f, sc, &gb, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, |
448 | 1.73k | (f->version >= 4 && !f->chroma_planes) ? 1 : 3, 1, ac); |
449 | 62.7k | } else if (f->colorspace == 0) { |
450 | 0 | decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 0, 2, ac); |
451 | 0 | decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + (ps>>1), width, height, p->linesize[0], 1, 1, 2, ac); |
452 | 33.8k | } else if (f->use32bit) { |
453 | 2.52k | uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0], |
454 | 2.52k | p->data[1] + ps * x + y * p->linesize[1], |
455 | 2.52k | p->data[2] + ps * x + y * p->linesize[2] }; |
456 | 2.52k | if (f->transparency) |
457 | 648 | planes[3] = p->data[3] + ps * x + y * p->linesize[3]; |
458 | 2.52k | decode_rgb_frame32(f, sc, &gb, planes, width, height, p->linesize); |
459 | 31.3k | } else { |
460 | 31.3k | uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0] }; |
461 | 31.3k | if (f->avctx->bits_per_raw_sample > 8) { |
462 | 28.9k | planes[1] = p->data[1] + ps * x + y * p->linesize[1]; |
463 | 28.9k | planes[2] = p->data[2] + ps * x + y * p->linesize[2]; |
464 | 28.9k | if (f->transparency) |
465 | 27.7k | planes[3] = p->data[3] + ps * x + y * p->linesize[3]; |
466 | 28.9k | } |
467 | 31.3k | decode_rgb_frame(f, sc, &gb, planes, width, height, p->linesize); |
468 | 31.3k | } |
469 | 96.6k | if (ac != AC_GOLOMB_RICE && f->version > 2) { |
470 | 0 | int v; |
471 | 0 | get_rac(&sc->c, (uint8_t[]) { 129 }); |
472 | 0 | v = sc->c.bytestream_end - sc->c.bytestream - 2 - 5*!!f->ec; |
473 | 0 | if (v) { |
474 | 0 | av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v); |
475 | 0 | slice_set_damaged(f, sc); |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | 96.6k | if (sc->slice_damaged && (f->avctx->err_recognition & AV_EF_EXPLODE)) |
480 | 0 | return AVERROR_INVALIDDATA; |
481 | | |
482 | 96.6k | if ((c->active_thread_type & FF_THREAD_FRAME) && !f->frame_damaged) |
483 | 0 | ff_progress_frame_report(&f->picture, si); |
484 | | |
485 | 96.6k | return 0; |
486 | 96.6k | } |
487 | | |
488 | | static enum AVPixelFormat get_pixel_format(FFV1Context *f) |
489 | 3.35k | { |
490 | 3.35k | enum AVPixelFormat pix_fmts[] = { |
491 | | #if CONFIG_FFV1_VULKAN_HWACCEL |
492 | | AV_PIX_FMT_VULKAN, |
493 | | #endif |
494 | 3.35k | f->pix_fmt, |
495 | 3.35k | AV_PIX_FMT_NONE, |
496 | 3.35k | }; |
497 | | |
498 | 3.35k | return ff_get_format(f->avctx, pix_fmts); |
499 | 3.35k | } |
500 | | |
501 | | static int read_header(FFV1Context *f, RangeCoder *c) |
502 | 62.4k | { |
503 | 62.4k | uint8_t state[CONTEXT_SIZE]; |
504 | 62.4k | int context_count = -1; //-1 to avoid warning |
505 | 62.4k | int ret; |
506 | | |
507 | 62.4k | memset(state, 128, sizeof(state)); |
508 | | |
509 | 62.4k | ret = ff_ffv1_parse_header(f, c, state); |
510 | 62.4k | if (ret < 0) |
511 | 22.6k | return ret; |
512 | | |
513 | 39.7k | if (f->configured_pix_fmt != f->pix_fmt || |
514 | 37.6k | f->configured_width != f->width || |
515 | 37.6k | f->configured_height != f->height || |
516 | 37.6k | f->configured_ac != f->ac) { |
517 | 3.35k | f->avctx->pix_fmt = get_pixel_format(f); |
518 | 3.35k | if (f->avctx->pix_fmt < 0) |
519 | 0 | return AVERROR(EINVAL); |
520 | 3.35k | f->configured_pix_fmt = f->pix_fmt; |
521 | 3.35k | f->configured_width = f->width; |
522 | 3.35k | f->configured_height = f->height; |
523 | 3.35k | f->configured_ac = f->ac; |
524 | 3.35k | } |
525 | | |
526 | 39.7k | ff_dlog(f->avctx, "%d %d %d\n", |
527 | 39.7k | f->chroma_h_shift, f->chroma_v_shift, f->pix_fmt); |
528 | 39.7k | if (f->version < 2) { |
529 | 22.2k | context_count = ff_ffv1_read_quant_tables(c, f->quant_tables[0]); |
530 | 22.2k | if (context_count < 0) { |
531 | 19.2k | av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n"); |
532 | 19.2k | return AVERROR_INVALIDDATA; |
533 | 19.2k | } |
534 | 2.96k | f->slice_count = f->max_slice_count; |
535 | 17.5k | } else if (f->version < 3) { |
536 | 17.5k | f->slice_count = ff_ffv1_get_symbol(c, state, 0); |
537 | 17.5k | } else { |
538 | 0 | const uint8_t *p = c->bytestream_end; |
539 | 0 | for (f->slice_count = 0; |
540 | 0 | f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start; |
541 | 0 | f->slice_count++) { |
542 | 0 | int trailer = 3 + 5*!!f->ec; |
543 | 0 | int size = AV_RB24(p-trailer); |
544 | 0 | if (size + trailer > p - c->bytestream_start) |
545 | 0 | break; |
546 | 0 | p -= size + trailer; |
547 | 0 | } |
548 | 0 | } |
549 | 20.5k | if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) { |
550 | 4.43k | av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count); |
551 | 4.43k | return AVERROR_INVALIDDATA; |
552 | 4.43k | } |
553 | | |
554 | 16.0k | av_refstruct_unref(&f->slice_damaged); |
555 | 16.0k | f->slice_damaged = av_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged)); |
556 | 16.0k | if (!f->slice_damaged) |
557 | 0 | return AVERROR(ENOMEM); |
558 | | |
559 | 26.0k | for (int j = 0; j < f->slice_count; j++) { |
560 | 18.8k | FFV1SliceContext *sc = &f->slices[j]; |
561 | | |
562 | 18.8k | if (f->version == 2) { |
563 | 15.8k | int sx = ff_ffv1_get_symbol(c, state, 0); |
564 | 15.8k | int sy = ff_ffv1_get_symbol(c, state, 0); |
565 | 15.8k | int sw = ff_ffv1_get_symbol(c, state, 0) + 1U; |
566 | 15.8k | int sh = ff_ffv1_get_symbol(c, state, 0) + 1U; |
567 | | |
568 | 15.8k | if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0) |
569 | 1.33k | return AVERROR_INVALIDDATA; |
570 | 14.5k | if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh) |
571 | 1.88k | return AVERROR_INVALIDDATA; |
572 | | |
573 | 12.6k | sc->slice_x = sx * (int64_t)f->width / f->num_h_slices; |
574 | 12.6k | sc->slice_y = sy * (int64_t)f->height / f->num_v_slices; |
575 | 12.6k | sc->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - sc->slice_x; |
576 | 12.6k | sc->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - sc->slice_y; |
577 | | |
578 | 12.6k | av_assert0((unsigned)sc->slice_width <= f->width && |
579 | 12.6k | (unsigned)sc->slice_height <= f->height); |
580 | 12.6k | av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width |
581 | 12.6k | && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height); |
582 | 12.6k | } |
583 | | |
584 | 15.5k | av_refstruct_unref(&sc->plane); |
585 | 15.5k | sc->plane = ff_ffv1_planes_alloc(); |
586 | 15.5k | if (!sc->plane) |
587 | 0 | return AVERROR(ENOMEM); |
588 | | |
589 | 41.8k | for (int i = 0; i < f->plane_count; i++) { |
590 | 31.8k | PlaneContext *const p = &sc->plane[i]; |
591 | | |
592 | 31.8k | if (f->version == 2) { |
593 | 25.0k | int idx = ff_ffv1_get_symbol(c, state, 0); |
594 | 25.0k | if (idx >= (unsigned)f->quant_table_count) { |
595 | 5.59k | av_log(f->avctx, AV_LOG_ERROR, |
596 | 5.59k | "quant_table_index out of range\n"); |
597 | 5.59k | return AVERROR_INVALIDDATA; |
598 | 5.59k | } |
599 | 19.4k | p->quant_table_index = idx; |
600 | 19.4k | context_count = f->context_count[idx]; |
601 | 19.4k | } |
602 | | |
603 | 26.2k | if (f->version <= 2) { |
604 | 26.2k | av_assert0(context_count >= 0); |
605 | 26.2k | p->context_count = context_count; |
606 | 26.2k | } |
607 | 26.2k | } |
608 | 15.5k | } |
609 | 7.26k | return 0; |
610 | 16.0k | } |
611 | | |
612 | | static av_cold int decode_init(AVCodecContext *avctx) |
613 | 2.78k | { |
614 | 2.78k | FFV1Context *f = avctx->priv_data; |
615 | 2.78k | int ret; |
616 | | |
617 | 2.78k | f->pix_fmt = AV_PIX_FMT_NONE; |
618 | 2.78k | f->configured_pix_fmt = AV_PIX_FMT_NONE; |
619 | | |
620 | 2.78k | if ((ret = ff_ffv1_common_init(avctx, f)) < 0) |
621 | 120 | return ret; |
622 | | |
623 | 2.66k | if (avctx->extradata_size > 0 && (ret = ff_ffv1_read_extra_header(f)) < 0) |
624 | 120 | return ret; |
625 | | |
626 | 2.54k | if ((ret = ff_ffv1_init_slice_contexts(f)) < 0) |
627 | 0 | return ret; |
628 | | |
629 | 2.54k | return 0; |
630 | 2.54k | } |
631 | | |
632 | | static int find_next_slice(AVCodecContext *avctx, |
633 | | uint8_t *buf, uint8_t *buf_end, int idx, |
634 | | uint8_t **pos, uint32_t *len) |
635 | 97.3k | { |
636 | 97.3k | FFV1Context *f = avctx->priv_data; |
637 | | |
638 | | /* Length field */ |
639 | 97.3k | uint32_t v = buf_end - buf; |
640 | 97.3k | if (idx || f->version > 2) { |
641 | | /* Three bytes of length, plus flush bit + CRC */ |
642 | 1.35k | uint32_t trailer = 3 + 5*!!f->ec; |
643 | 1.35k | if (trailer > buf_end - buf) |
644 | 347 | v = INT_MAX; |
645 | 1.00k | else |
646 | 1.00k | v = AV_RB24(buf_end - trailer) + trailer; |
647 | 1.35k | } |
648 | | |
649 | 97.3k | if (buf_end - buf < v) { |
650 | 660 | av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n"); |
651 | 660 | ff_progress_frame_report(&f->picture, INT_MAX); |
652 | 660 | return AVERROR_INVALIDDATA; |
653 | 660 | } |
654 | | |
655 | 96.6k | *len = v; |
656 | 96.6k | if (idx) |
657 | 693 | *pos = buf_end - v; |
658 | 95.9k | else |
659 | 95.9k | *pos = buf; |
660 | | |
661 | 96.6k | return 0; |
662 | 97.3k | } |
663 | | |
664 | | static int decode_header(AVCodecContext *avctx, RangeCoder *c, |
665 | | uint8_t *buf, size_t buf_size) |
666 | 177k | { |
667 | 177k | int ret; |
668 | 177k | FFV1Context *f = avctx->priv_data; |
669 | | |
670 | 177k | uint8_t keystate = 128; |
671 | 177k | ff_init_range_decoder(c, buf, buf_size); |
672 | 177k | ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8); |
673 | | |
674 | 177k | if (get_rac(c, &keystate)) { |
675 | 62.4k | f->key_frame = AV_FRAME_FLAG_KEY; |
676 | 62.4k | f->key_frame_ok = 0; |
677 | 62.4k | if ((ret = read_header(f, c)) < 0) |
678 | 55.2k | return ret; |
679 | 7.26k | f->key_frame_ok = 1; |
680 | 115k | } else { |
681 | 115k | if (!f->key_frame_ok) { |
682 | 19.1k | av_log(avctx, AV_LOG_ERROR, |
683 | 19.1k | "Cannot decode non-keyframe without valid keyframe\n"); |
684 | 19.1k | return AVERROR_INVALIDDATA; |
685 | 19.1k | } |
686 | 96.2k | f->key_frame = 0; |
687 | 96.2k | } |
688 | | |
689 | 103k | if (f->ac != AC_GOLOMB_RICE) { |
690 | 18.9k | if (buf_size < avctx->width * avctx->height / (128*8)) |
691 | 3.55k | return AVERROR_INVALIDDATA; |
692 | 84.5k | } else { |
693 | 84.5k | int w = avctx->width; |
694 | 84.5k | int s = 1 + w / (1<<23); |
695 | 84.5k | int i; |
696 | | |
697 | 84.5k | w /= s; |
698 | | |
699 | 752k | for (i = 0; w > (1<<ff_log2_run[i]); i++) |
700 | 668k | w -= ff_log2_run[i]; |
701 | 84.5k | if (buf_size < (avctx->height + i + 6) / 8 * s) |
702 | 2.56k | return AVERROR_INVALIDDATA; |
703 | 84.5k | } |
704 | | |
705 | 97.3k | return 0; |
706 | 103k | } |
707 | | |
708 | | static int decode_slices(AVCodecContext *avctx, RangeCoder c, |
709 | | AVPacket *avpkt) |
710 | 96.6k | { |
711 | 96.6k | FFV1Context *f = avctx->priv_data; |
712 | 96.6k | AVFrame *p = f->picture.f; |
713 | | |
714 | 96.6k | uint8_t *buf = avpkt->data; |
715 | 96.6k | size_t buf_size = avpkt->size; |
716 | 96.6k | uint8_t *buf_end = buf + buf_size; |
717 | | |
718 | 193k | for (int i = f->slice_count - 1; i >= 0; i--) { |
719 | 97.3k | FFV1SliceContext *sc = &f->slices[i]; |
720 | | |
721 | 97.3k | uint8_t *pos; |
722 | 97.3k | uint32_t len; |
723 | 97.3k | int err = find_next_slice(avctx, buf, buf_end, i, |
724 | 97.3k | &pos, &len); |
725 | 97.3k | if (err < 0) |
726 | 660 | return err; |
727 | | |
728 | 96.6k | buf_end -= len; |
729 | | |
730 | 96.6k | sc->slice_damaged = 0; |
731 | | |
732 | 96.6k | if (f->ec) { |
733 | 0 | unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, pos, len); |
734 | 0 | if (crc != f->crcref) { |
735 | 0 | int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts; |
736 | 0 | av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc); |
737 | 0 | if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) { |
738 | 0 | av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase)); |
739 | 0 | } else if (ts != AV_NOPTS_VALUE) { |
740 | 0 | av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts); |
741 | 0 | } else { |
742 | 0 | av_log(f->avctx, AV_LOG_ERROR, "\n"); |
743 | 0 | } |
744 | 0 | slice_set_damaged(f, sc); |
745 | 0 | } |
746 | 0 | if (avctx->debug & FF_DEBUG_PICT_INFO) { |
747 | 0 | av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(pos + len - 4)); |
748 | 0 | } |
749 | 0 | } |
750 | | |
751 | 96.6k | if (i) { |
752 | 693 | ff_init_range_decoder(&sc->c, pos, len); |
753 | 693 | ff_build_rac_states(&sc->c, 0.05 * (1LL << 32), 256 - 8); |
754 | 95.9k | } else { |
755 | 95.9k | sc->c = c; |
756 | 95.9k | sc->c.bytestream_end = pos + len; |
757 | 95.9k | } |
758 | 96.6k | } |
759 | | |
760 | 95.9k | avctx->execute(avctx, |
761 | 95.9k | decode_slice, |
762 | 95.9k | f->slices, |
763 | 95.9k | NULL, |
764 | 95.9k | f->slice_count, |
765 | 95.9k | sizeof(*f->slices)); |
766 | | |
767 | 192k | for (int i = f->slice_count - 1; i >= 0; i--) { |
768 | 96.6k | FFV1SliceContext *sc = &f->slices[i]; |
769 | 96.6k | if (sc->slice_damaged && f->last_picture.f) { |
770 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(f->pix_fmt); |
771 | 0 | const uint8_t *src[4]; |
772 | 0 | uint8_t *dst[4]; |
773 | 0 | ff_progress_frame_await(&f->last_picture, INT_MAX); |
774 | 0 | for (int j = 0; j < desc->nb_components; j++) { |
775 | 0 | int pixshift = desc->comp[j].depth > 8; |
776 | 0 | int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0; |
777 | 0 | int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0; |
778 | 0 | dst[j] = p->data[j] + p->linesize[j] * |
779 | 0 | (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift); |
780 | 0 | src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] * |
781 | 0 | (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift); |
782 | |
|
783 | 0 | } |
784 | |
|
785 | 0 | av_image_copy(dst, p->linesize, src, |
786 | 0 | f->last_picture.f->linesize, |
787 | 0 | f->pix_fmt, |
788 | 0 | sc->slice_width, |
789 | 0 | sc->slice_height); |
790 | |
|
791 | 0 | f->slice_damaged[i] = 1; |
792 | 0 | } |
793 | 96.6k | } |
794 | | |
795 | 95.9k | return 0; |
796 | 96.6k | } |
797 | | |
798 | | static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
799 | | int *got_frame, AVPacket *avpkt) |
800 | 177k | { |
801 | 177k | FFV1Context *f = avctx->priv_data; |
802 | 177k | int ret; |
803 | 177k | AVFrame *p; |
804 | 177k | const FFHWAccel *hwaccel = NULL; |
805 | | |
806 | | /* This is copied onto the first slice's range coder context */ |
807 | 177k | RangeCoder c; |
808 | | |
809 | 177k | ff_progress_frame_unref(&f->last_picture); |
810 | 177k | av_refstruct_unref(&f->hwaccel_last_picture_private); |
811 | | |
812 | 177k | FFSWAP(ProgressFrame, f->picture, f->last_picture); |
813 | 177k | FFSWAP(void *, f->hwaccel_picture_private, f->hwaccel_last_picture_private); |
814 | | |
815 | 177k | f->avctx = avctx; |
816 | 177k | f->frame_damaged = 0; |
817 | | |
818 | 177k | ret = decode_header(avctx, &c, avpkt->data, avpkt->size); |
819 | 177k | if (ret < 0) |
820 | 80.4k | return ret; |
821 | | |
822 | 97.3k | if (avctx->debug & FF_DEBUG_PICT_INFO) |
823 | 0 | av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n", |
824 | 0 | f->version, !!f->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample); |
825 | | |
826 | 97.3k | if (avctx->skip_frame >= AVDISCARD_ALL) |
827 | 445 | return avpkt->size; |
828 | | |
829 | 96.9k | if (avctx->hwaccel) |
830 | 0 | hwaccel = ffhwaccel(avctx->hwaccel); |
831 | | |
832 | 96.9k | ret = ff_progress_frame_get_buffer(avctx, &f->picture, |
833 | 96.9k | AV_GET_BUFFER_FLAG_REF); |
834 | 96.9k | if (ret < 0) |
835 | 276 | return ret; |
836 | | |
837 | 96.6k | ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private); |
838 | 96.6k | if (ret < 0) |
839 | 0 | return ret; |
840 | | |
841 | 96.6k | p = f->picture.f; |
842 | | |
843 | 96.6k | p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P |
844 | 96.6k | p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | f->key_frame; |
845 | | |
846 | 96.6k | if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) { |
847 | | /* we have interlaced material flagged in container */ |
848 | 0 | p->flags |= AV_FRAME_FLAG_INTERLACED; |
849 | 0 | if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB) |
850 | 0 | p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; |
851 | 0 | } |
852 | | |
853 | | /* Start */ |
854 | 96.6k | if (hwaccel) { |
855 | 0 | ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size); |
856 | 0 | if (ret < 0) |
857 | 0 | return ret; |
858 | 0 | } |
859 | | |
860 | 96.6k | ff_thread_finish_setup(avctx); |
861 | | |
862 | | /* Decode slices */ |
863 | 96.6k | if (hwaccel) { |
864 | 0 | uint8_t *buf_end = avpkt->data + avpkt->size; |
865 | |
|
866 | 0 | if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f) |
867 | 0 | ff_progress_frame_await(&f->last_picture, f->slice_count - 1); |
868 | |
|
869 | 0 | for (int i = f->slice_count - 1; i >= 0; i--) { |
870 | 0 | uint8_t *pos; |
871 | 0 | uint32_t len; |
872 | 0 | ret = find_next_slice(avctx, avpkt->data, buf_end, i, |
873 | 0 | &pos, &len); |
874 | 0 | if (ret < 0) |
875 | 0 | return ret; |
876 | | |
877 | 0 | buf_end -= len; |
878 | |
|
879 | 0 | ret = hwaccel->decode_slice(avctx, pos, len); |
880 | 0 | if (ret < 0) |
881 | 0 | return ret; |
882 | 0 | } |
883 | 96.6k | } else { |
884 | 96.6k | ret = decode_slices(avctx, c, avpkt); |
885 | 96.6k | if (ret < 0) |
886 | 660 | return ret; |
887 | 96.6k | } |
888 | | |
889 | | /* Finalize */ |
890 | 95.9k | if (hwaccel) { |
891 | 0 | ret = hwaccel->end_frame(avctx); |
892 | 0 | if (ret < 0) |
893 | 0 | return ret; |
894 | 0 | } |
895 | | |
896 | 95.9k | ff_progress_frame_report(&f->picture, INT_MAX); |
897 | | |
898 | 95.9k | ff_progress_frame_unref(&f->last_picture); |
899 | 95.9k | av_refstruct_unref(&f->hwaccel_last_picture_private); |
900 | 95.9k | if ((ret = av_frame_ref(rframe, f->picture.f)) < 0) |
901 | 0 | return ret; |
902 | | |
903 | 95.9k | *got_frame = 1; |
904 | | |
905 | 95.9k | return avpkt->size; |
906 | 95.9k | } |
907 | | |
908 | | #if HAVE_THREADS |
909 | | static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) |
910 | 0 | { |
911 | 0 | FFV1Context *fsrc = src->priv_data; |
912 | 0 | FFV1Context *fdst = dst->priv_data; |
913 | |
|
914 | 0 | if (dst == src) |
915 | 0 | return 0; |
916 | | |
917 | 0 | fdst->version = fsrc->version; |
918 | 0 | fdst->micro_version = fsrc->micro_version; |
919 | 0 | fdst->combined_version = fsrc->combined_version; |
920 | 0 | fdst->chroma_planes = fsrc->chroma_planes; |
921 | 0 | fdst->chroma_h_shift = fsrc->chroma_h_shift; |
922 | 0 | fdst->chroma_v_shift = fsrc->chroma_v_shift; |
923 | 0 | fdst->transparency = fsrc->transparency; |
924 | 0 | fdst->plane_count = fsrc->plane_count; |
925 | 0 | fdst->ac = fsrc->ac; |
926 | 0 | fdst->colorspace = fsrc->colorspace; |
927 | 0 | fdst->pix_fmt = fsrc->pix_fmt; |
928 | 0 | fdst->configured_pix_fmt = fsrc->configured_pix_fmt; |
929 | 0 | fdst->configured_ac = fsrc->configured_ac; |
930 | 0 | fdst->configured_width = fsrc->configured_width; |
931 | 0 | fdst->configured_height = fsrc->configured_height; |
932 | |
|
933 | 0 | fdst->ec = fsrc->ec; |
934 | 0 | fdst->intra = fsrc->intra; |
935 | 0 | fdst->key_frame_ok = fsrc->key_frame_ok; |
936 | |
|
937 | 0 | fdst->packed_at_lsb = fsrc->packed_at_lsb; |
938 | 0 | fdst->slice_count = fsrc->slice_count; |
939 | 0 | fdst->use32bit = fsrc->use32bit; |
940 | 0 | memcpy(fdst->state_transition, fsrc->state_transition, |
941 | 0 | sizeof(fdst->state_transition)); |
942 | | |
943 | | // in version 1 there is a single per-keyframe quant table, so |
944 | | // we need to propagate it between threads |
945 | 0 | if (fsrc->version < 2) |
946 | 0 | memcpy(fdst->quant_tables[0], fsrc->quant_tables[0], sizeof(fsrc->quant_tables[0])); |
947 | |
|
948 | 0 | for (int i = 0; i < fdst->num_h_slices * fdst->num_v_slices; i++) { |
949 | 0 | FFV1SliceContext *sc = &fdst->slices[i]; |
950 | 0 | const FFV1SliceContext *sc0 = &fsrc->slices[i]; |
951 | |
|
952 | 0 | av_refstruct_replace(&sc->plane, sc0->plane); |
953 | |
|
954 | 0 | if (fsrc->version < 3) { |
955 | 0 | sc->slice_x = sc0->slice_x; |
956 | 0 | sc->slice_y = sc0->slice_y; |
957 | 0 | sc->slice_width = sc0->slice_width; |
958 | 0 | sc->slice_height = sc0->slice_height; |
959 | 0 | } |
960 | 0 | } |
961 | |
|
962 | 0 | av_refstruct_replace(&fdst->slice_damaged, fsrc->slice_damaged); |
963 | |
|
964 | 0 | av_assert1(fdst->max_slice_count == fsrc->max_slice_count); |
965 | |
|
966 | 0 | ff_progress_frame_replace(&fdst->picture, &fsrc->picture); |
967 | 0 | av_refstruct_replace(&fdst->hwaccel_picture_private, |
968 | 0 | fsrc->hwaccel_picture_private); |
969 | |
|
970 | 0 | return 0; |
971 | 0 | } |
972 | | #endif |
973 | | |
974 | | static av_cold int ffv1_decode_close(AVCodecContext *avctx) |
975 | 2.78k | { |
976 | 2.78k | FFV1Context *const s = avctx->priv_data; |
977 | | |
978 | 2.78k | ff_progress_frame_unref(&s->picture); |
979 | 2.78k | av_refstruct_unref(&s->hwaccel_picture_private); |
980 | | |
981 | 2.78k | ff_progress_frame_unref(&s->last_picture); |
982 | 2.78k | av_refstruct_unref(&s->hwaccel_last_picture_private); |
983 | | |
984 | 2.78k | ff_ffv1_close(s); |
985 | | |
986 | 2.78k | return 0; |
987 | 2.78k | } |
988 | | |
989 | | const FFCodec ff_ffv1_decoder = { |
990 | | .p.name = "ffv1", |
991 | | CODEC_LONG_NAME("FFmpeg video codec #1"), |
992 | | .p.type = AVMEDIA_TYPE_VIDEO, |
993 | | .p.id = AV_CODEC_ID_FFV1, |
994 | | .priv_data_size = sizeof(FFV1Context), |
995 | | .init = decode_init, |
996 | | .close = ffv1_decode_close, |
997 | | FF_CODEC_DECODE_CB(decode_frame), |
998 | | UPDATE_THREAD_CONTEXT(update_thread_context), |
999 | | .p.capabilities = AV_CODEC_CAP_DR1 | |
1000 | | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS, |
1001 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
1002 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | |
1003 | | FF_CODEC_CAP_USES_PROGRESSFRAMES, |
1004 | | .hw_configs = (const AVCodecHWConfigInternal *const []) { |
1005 | | #if CONFIG_FFV1_VULKAN_HWACCEL |
1006 | | HWACCEL_VULKAN(ffv1), |
1007 | | #endif |
1008 | | NULL |
1009 | | }, |
1010 | | }; |