FFmpeg
Loading...
Searching...
No Matches
ffv1dec.c
Go to the documentation of this file.
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
48static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
49 int bits)
50{
51 int k, i, v, ret;
52
53 i = state->count;
54 k = 0;
55 while (i < state->error_sum) { // FIXME: optimize
56 k++;
57 i += i;
58 }
59 if (k > bits) {
60 ff_dlog(NULL, "k-overflow bias:%d error:%d drift:%d count:%d k:%d",
61 state->bias, state->error_sum, state->drift, state->count, k);
62 k = bits;
63 }
64
65 v = get_sr_golomb(gb, k, 12, bits);
66 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
67 v, state->bias, state->error_sum, state->drift, state->count, k);
68
69 v ^= ((2 * state->drift + state->count) >> 31);
70
71 ret = fold(v + state->bias, bits);
72
74
75 return ret;
76}
77
78static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
79{
80 if (ac != AC_GOLOMB_RICE) {
81 if (c->overread > MAX_OVERREAD)
83 } else {
84 if (get_bits_left(gb) < 1)
86 }
87 return 0;
88}
89
90#define TYPE int16_t
91#define RENAME(name) name
92#include "ffv1dec_template.c"
93#undef TYPE
94#undef RENAME
95
96#define TYPE int32_t
97#define RENAME(name) name ## 32
98#include "ffv1dec_template.c"
99
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{
105 int x, y;
106 int16_t *sample[2];
107 int bits;
108 unsigned mask;
109
110 if (sc->remap) {
111 bits = av_ceil_log2(sc->remap_count[remap_index]);
112 mask = (1<<bits)-1;
113
114 av_assert0(sc->fltmap_size[remap_index] >= (mask + 1) * sizeof(*sc->fltmap[remap_index]));
115 } else {
116 bits = f->avctx->bits_per_raw_sample;
117 }
118
119 sample[0] = sc->sample_buffer + 3;
120 sample[1] = sc->sample_buffer + w + 6 + 3;
121
122 sc->run_index = 0;
123
124 memset(sc->sample_buffer, 0, 2 * (w + 6) * sizeof(*sc->sample_buffer));
125
126 for (y = 0; y < h; y++) {
127 int16_t *temp = sample[0]; // FIXME: try a normal buffer
128
129 sample[0] = sample[1];
130 sample[1] = temp;
131
132 sample[1][-1] = sample[0][0];
133 sample[0][w] = sample[0][w - 1];
134
135 if (f->avctx->bits_per_raw_sample <= 8) {
136 int ret = decode_line(f, sc, gb, w, sample, plane_index, 8, ac);
137 if (ret < 0)
138 return ret;
139 if (sc->remap)
140 for (x = 0; x < w; x++)
141 sample[1][x] = sc->fltmap[remap_index][sample[1][x] & mask];
142 for (x = 0; x < w; x++)
143 src[x*pixel_stride + stride * y] = sample[1][x];
144 } else {
145 int ret = decode_line(f, sc, gb, w, sample, plane_index, bits, ac);
146 if (ret < 0)
147 return ret;
148
149 if (sc->remap) {
150 if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) {
151 for (x = 0; x < w; x++) {
152 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sc->fltmap[remap_index][sample[1][x] & mask];
153 }
154 } else {
155 for (x = 0; x < w; x++) {
156 int v = sc->fltmap[remap_index][sample[1][x] & mask];
157 ((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 }
159 }
160 } else {
161 if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) {
162 for (x = 0; x < w; x++) {
163 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
164 }
165 } else {
166 for (x = 0; x < w; x++) {
167 ((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 }
169 }
170 }
171 }
172 }
173 return 0;
174}
175
178{
179 RangeCoder *c = &sc->c;
180 uint8_t state[CONTEXT_SIZE];
181 unsigned ps, context_count;
182 int sx, sy, sw, sh;
183
184 memset(state, 128, sizeof(state));
185 sx = ff_ffv1_get_symbol(c, state, 0);
186 sy = ff_ffv1_get_symbol(c, state, 0);
187 sw = ff_ffv1_get_symbol(c, state, 0) + 1U;
188 sh = ff_ffv1_get_symbol(c, state, 0) + 1U;
189
190 av_assert0(f->version > 2);
191
192
193 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
194 return AVERROR_INVALIDDATA;
195 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
196 return AVERROR_INVALIDDATA;
197
198 sc->slice_x = ff_slice_coord(f, f->width , sx , f->num_h_slices, f->chroma_h_shift);
199 sc->slice_y = ff_slice_coord(f, f->height, sy , f->num_v_slices, f->chroma_v_shift);
200 sc->slice_width = ff_slice_coord(f, f->width , sx + sw, f->num_h_slices, f->chroma_h_shift) - sc->slice_x;
201 sc->slice_height = ff_slice_coord(f, f->height, sy + sh, f->num_v_slices, f->chroma_v_shift) - sc->slice_y;
202
203 av_assert0((unsigned)sc->slice_width <= f->width &&
204 (unsigned)sc->slice_height <= f->height);
205 av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width
206 && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
207
208 if (f->ac == AC_GOLOMB_RICE && sc->slice_width >= (1<<23))
209 return AVERROR_INVALIDDATA;
210
211 for (unsigned i = 0; i < f->plane_count; i++) {
212 PlaneContext * const p = &sc->plane[i];
213 int idx = ff_ffv1_get_symbol(c, state, 0);
214 if (idx >= (unsigned)f->quant_table_count) {
215 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
216 return -1;
217 }
218 p->quant_table_index = idx;
219 context_count = f->context_count[idx];
220
221 if (p->context_count < context_count) {
222 av_freep(&p->state);
223 av_freep(&p->vlc_state);
224 }
225 p->context_count = context_count;
226 }
227
228 ps = ff_ffv1_get_symbol(c, state, 0);
229 if (ps == 1) {
232 } else if (ps == 2) {
235 } else if (ps == 3) {
237 }
238 frame->sample_aspect_ratio.num = ff_ffv1_get_symbol(c, state, 0);
239 frame->sample_aspect_ratio.den = ff_ffv1_get_symbol(c, state, 0);
240
241 if (av_image_check_sar(f->width, f->height,
242 frame->sample_aspect_ratio) < 0) {
243 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
244 frame->sample_aspect_ratio.num,
245 frame->sample_aspect_ratio.den);
246 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
247 }
248
249 if (f->version > 3) {
252 if (sc->slice_coding_mode != 1 && f->colorspace != 0) {
255 if ((uint64_t)sc->slice_rct_by_coef + (uint64_t)sc->slice_rct_ry_coef > 4) {
256 av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
257 return AVERROR_INVALIDDATA;
258 }
259 }
260 if (f->combined_version >= 0x40004) {
261 sc->remap = ff_ffv1_get_symbol(c, state, 0);
262 if (sc->remap > 2U ||
263 sc->remap && !f->flt) {
264 av_log(f->avctx, AV_LOG_ERROR, "unsupported remap %d\n", sc->remap);
265 return AVERROR_INVALIDDATA;
266 }
267 }
268 }
269 if (f->avctx->bits_per_raw_sample == 32) {
270 if (!sc->remap) {
271 av_log(f->avctx, AV_LOG_ERROR, "unsupported remap\n");
272 return AVERROR_INVALIDDATA;
273 }
274 }
275
276 return 0;
277}
278
280{
281 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 if (f->avctx->active_thread_type & FF_THREAD_FRAME)
286 f->frame_damaged = 1;
287}
288
289static int decode_current_mul(RangeCoder *rc, uint8_t state[32], int *mul, int mul_count, int64_t i)
290{
291 int ndx = (i * mul_count) >> 32;
292 av_assert2(ndx <= 4096U);
293
294 if (mul[ndx] < 0)
295 mul[ndx] = ff_ffv1_get_symbol(rc, state, 0) & 0x3FFFFFFF;
296
297 return mul[ndx];
298}
299
301{
302 unsigned int end = (1LL<<f->avctx->bits_per_raw_sample) - 1;
303 int flip = sc->remap == 2 ? (end>>1) : 0;
304
305 for (int p = 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
306 int j = 0;
307 int lu = 0;
308 uint8_t state[2][3][32];
309 int64_t i;
310 int mul[4096+1];
311 int mul_count;
312
313 const int is_chroma = (p == 1 || p == 2) && f->chroma_planes;
314 const int plane_width = AV_CEIL_RSHIFT(sc->slice_width , is_chroma ? f->chroma_h_shift : 0);
315 const int plane_height = AV_CEIL_RSHIFT(sc->slice_height, is_chroma ? f->chroma_v_shift : 0);
316 const int pixel_num = FFMIN(plane_width * plane_height, end + 1LL);
317 const size_t fltmap_ceil = 1ULL << av_ceil_log2(pixel_num);
318
319 if (f->avctx->bits_per_raw_sample == 32) {
320 av_fast_malloc(&sc->fltmap32[p], &sc->fltmap32_size[p], fltmap_ceil * sizeof(*sc->fltmap32[p]));
321 if (!sc->fltmap32[p])
322 return AVERROR(ENOMEM);
323 } else {
324 av_fast_malloc(&sc->fltmap[p] , &sc->fltmap_size[p] , fltmap_ceil * sizeof(*sc->fltmap[p]));
325 if (!sc->fltmap[p])
326 return AVERROR(ENOMEM);
327 }
328
329 memset(state, 128, sizeof(state));
330 mul_count = ff_ffv1_get_symbol(&sc->c, state[0][0], 0);
331
332 if (mul_count > 4096U)
333 return AVERROR_INVALIDDATA;
334 for (int i = 0; i<mul_count; i++) {
335 mul[i] = -1;
336
337 }
338 mul[mul_count] = 1;
339
340 memset(state, 128, sizeof(state));
341 int current_mul = 1;
342 for (i=0; i <= end ;) {
343 unsigned run = get_symbol_inline(&sc->c, state[lu][0], 0);
344 unsigned run0 = lu ? 0 : run;
345 unsigned run1 = lu ? run : 1;
346
347 i += run0 * current_mul;
348
349 while (run1--) {
350 if (current_mul > 1) {
351 int delta = get_symbol_inline(&sc->c, state[lu][1], 1);
352 if (delta <= -current_mul || delta > current_mul/2)
353 return AVERROR_INVALIDDATA; //not sure we should check this
354 i += current_mul - 1 + delta;
355 }
356 if (i - 1 >= end)
357 break;
358 if (j >= pixel_num)
359 return AVERROR_INVALIDDATA;
360 if (end <= 0xFFFF) {
361 sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip);
362 } else
363 sc->fltmap32[p][j++] = i ^ ((i&0x80000000) ? 0 : flip);
364 i++;
365 current_mul = decode_current_mul(&sc->c, state[0][2], mul, mul_count, i);
366 }
367 if (lu) {
368 i += current_mul;
369 }
370 lu ^= !run;
371 }
372 if (!j)
373 return AVERROR_INVALIDDATA;
374 sc->remap_count[p] = j;
375 }
376 return 0;
377}
378
380 GetBitContext *gb,
381 uint8_t *src, int w, int h, int stride)
382{
383 int x, y, p;
384 TYPE *sample[4][2];
385 int ac = f->ac;
386 unsigned mask[4];
387
388 int bits[4], offset;
389 ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, mask, f->avctx->bits_per_raw_sample);
390
391 w >>= 1;
392
393 if (sc->slice_coding_mode == 1)
394 ac = 1;
395
396 for (x = 0; x < 4; x++) {
397 sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3;
398 sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
399 }
400
401 sc->run_index = 0;
402
403 memset(RENAME(sc->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
404
405 for (y = 0; y < h; y += 2) {
406 for (p = 0; p < 4; p++) {
407 int ret;
408 TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
409
410 sample[p][0] = sample[p][1];
411 sample[p][1] = temp;
412
413 sample[p][1][-1]= sample[p][0][0 ];
414 sample[p][0][ w]= sample[p][0][w-1];
415 ret = RENAME(decode_line)(f, sc, gb, w, sample[p],
416 p == 1 ? 2 : (p > 1), bits[p], ac);
417 if (ret < 0)
418 return ret;
419 }
420
421 for (x = 0; x < w; x++) {
422 int g_r = sample[0][1][x];
423 int g_b = sample[1][1][x];
424 int b = sample[2][1][x];
425 int r = sample[3][1][x];
426
427 if (sc->slice_coding_mode != 1) {
428 b -= offset;
429 r -= offset;
430 g_r -= (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
431 b += g_r;
432 r += g_r;
433
434 /* Recover green pair: encoder stored gm = gb + (gd >> 1), gd = gr - gb */
435 int gd = g_b - offset;
436 g_b = g_r - (gd >> 1);
437 g_r = g_b + gd;
438 }
439
440 *((uint16_t*)(src + (x*2 + 0)*2 + stride*(y + 0))) = r;
441 *((uint16_t*)(src + (x*2 + 1)*2 + stride*(y + 0))) = g_r;
442 *((uint16_t*)(src + (x*2 + 0)*2 + stride*(y + 1))) = g_b;
443 *((uint16_t*)(src + (x*2 + 1)*2 + stride*(y + 1))) = b;
444 }
445 }
446 return 0;
447}
448
449static int decode_slice(AVCodecContext *c, void *arg)
450{
451 FFV1Context *f = c->priv_data;
452 FFV1SliceContext *sc = arg;
453 int width, height, x, y, ret;
454 const int ps = av_pix_fmt_desc_get(f->pix_fmt)->comp[0].step;
455 AVFrame * const p = f->picture.f;
456 const int si = sc - f->slices;
457 GetBitContext gb;
458 int ac = f->ac || sc->slice_coding_mode == 1;
459
460 if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
461 ff_progress_frame_await(&f->last_picture, si);
462
463 if (f->slice_damaged[si])
464 slice_set_damaged(f, sc);
465
466 sc->slice_rct_by_coef = 1;
467 sc->slice_rct_ry_coef = 1;
468
469 if (f->version > 2) {
470 if (ff_ffv1_init_slice_state(f, sc) < 0)
471 return AVERROR(ENOMEM);
472 if (decode_slice_header(f, sc, p) < 0) {
473 sc->slice_x = sc->slice_y = sc->slice_height = sc->slice_width = 0;
474 slice_set_damaged(f, sc);
475 return AVERROR_INVALIDDATA;
476 }
477 }
478 if ((ret = ff_ffv1_init_slice_state(f, sc)) < 0)
479 return ret;
480 if ((p->flags & AV_FRAME_FLAG_KEY) || sc->slice_reset_contexts) {
482 } else if (sc->slice_damaged) {
483 return AVERROR_INVALIDDATA;
484 }
485
486 width = sc->slice_width;
487 height = sc->slice_height;
488 x = sc->slice_x;
489 y = sc->slice_y;
490
491 if (sc->remap) {
492 ret = decode_remap(f, sc);
493 if (ret < 0) {
494 slice_set_damaged(f, sc);
495 return ret;
496 }
497 }
498
499 if (ac == AC_GOLOMB_RICE) {
500 if (f->combined_version >= 0x30002)
501 get_rac(&sc->c, (uint8_t[]) { 129 });
502 sc->ac_byte_count = f->version > 2 || (!x && !y) ? sc->c.bytestream - sc->c.bytestream_start - 1 : 0;
503 init_get_bits(&gb,
505 (sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count) * 8);
506 }
507
509 if (f->colorspace == 0 && (f->chroma_planes || !f->transparency)) {
510 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
511 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
512 const int cx = x >> f->chroma_h_shift;
513 const int cy = y >> f->chroma_v_shift;
514 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 1, ac);
515
516 if (f->chroma_planes) {
517 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);
518 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);
519 }
520 if (f->transparency)
521 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,
522 (f->version >= 4 && !f->chroma_planes) ? 1 : 3, 1, ac);
523 } else if (f->colorspace == 0) {
524 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 0, 2, ac);
525 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);
526 } else if (f->bayer) {
527 decode_bayer_frame(f, sc, &gb, p->data[0] + ps * x + y * p->linesize[0],
528 width, height, p->linesize[0]);
529 } else if (f->use32bit) {
530 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
531 p->data[1] + ps * x + y * p->linesize[1],
532 p->data[2] + ps * x + y * p->linesize[2] };
533 if (f->transparency)
534 planes[3] = p->data[3] + ps * x + y * p->linesize[3];
535 decode_rgb_frame32(f, sc, &gb, planes, width, height, p->linesize);
536 } else {
537 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0] };
538 if (f->avctx->bits_per_raw_sample > 8) {
539 planes[1] = p->data[1] + ps * x + y * p->linesize[1];
540 planes[2] = p->data[2] + ps * x + y * p->linesize[2];
541 if (f->transparency)
542 planes[3] = p->data[3] + ps * x + y * p->linesize[3];
543 }
544 decode_rgb_frame(f, sc, &gb, planes, width, height, p->linesize);
545 }
546 if (ac != AC_GOLOMB_RICE && f->version > 2) {
547 int v;
548 get_rac(&sc->c, (uint8_t[]) { 129 });
549 v = sc->c.bytestream_end - sc->c.bytestream - 2 - 5*!!f->ec;
550 if (v) {
551 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
552 slice_set_damaged(f, sc);
553 }
554 }
555
556 if (sc->slice_damaged && (f->avctx->err_recognition & AV_EF_EXPLODE))
557 return AVERROR_INVALIDDATA;
558
559 if ((c->active_thread_type & FF_THREAD_FRAME) && !f->frame_damaged)
560 ff_progress_frame_report(&f->picture, si);
561
562 return 0;
563}
564
566{
567 enum AVPixelFormat pix_fmts[] = {
568#if CONFIG_FFV1_VULKAN_HWACCEL
570#endif
571 f->pix_fmt,
573 };
574
575 return ff_get_format(f->avctx, pix_fmts);
576}
577
579{
580 uint8_t state[CONTEXT_SIZE];
581 int context_count = -1; //-1 to avoid warning
582 int ret;
583
584 memset(state, 128, sizeof(state));
585
586 ret = ff_ffv1_parse_header(f, c, state);
587 if (ret < 0)
588 return ret;
589
590 if (f->bayer && f->combined_version <= 0x40002) {
591 av_log(f->avctx, AV_LOG_ERROR,
592 "Bayer requires aligned slice coordinates (combined_version > 0x40002)\n");
593 return AVERROR_INVALIDDATA;
594 }
595
596 if (f->configured_pix_fmt != f->pix_fmt ||
597 f->configured_width != f->width ||
598 f->configured_height != f->height ||
599 f->configured_ac != f->ac) {
600 f->avctx->pix_fmt = get_pixel_format(f);
601 if (f->avctx->pix_fmt < 0)
602 return AVERROR(EINVAL);
603 f->configured_pix_fmt = f->pix_fmt;
604 f->configured_width = f->width;
605 f->configured_height = f->height;
606 f->configured_ac = f->ac;
607 }
608
609 ff_dlog(f->avctx, "%d %d %d\n",
610 f->chroma_h_shift, f->chroma_v_shift, f->pix_fmt);
611 if (f->version < 2) {
612 context_count = ff_ffv1_read_quant_tables(c, f->quant_tables[0]);
613 if (context_count < 0) {
614 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
615 return AVERROR_INVALIDDATA;
616 }
617 f->slice_count = f->max_slice_count;
618 } else if (f->version < 3) {
619 f->slice_count = ff_ffv1_get_symbol(c, state, 0);
620 } else {
621 const uint8_t *p = c->bytestream_end;
622 for (f->slice_count = 0;
623 f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
624 f->slice_count++) {
625 int trailer = 3 + 5*!!f->ec;
626 int size = AV_RB24(p-trailer);
627 if (size + trailer > p - c->bytestream_start)
628 break;
629 p -= size + trailer;
630 }
631 }
632 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
633 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
634 return AVERROR_INVALIDDATA;
635 }
636
637 av_refstruct_unref(&f->slice_damaged);
638 f->slice_damaged = av_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged));
639 if (!f->slice_damaged)
640 return AVERROR(ENOMEM);
641
642 for (int j = 0; j < f->slice_count; j++) {
643 FFV1SliceContext *sc = &f->slices[j];
644
645 if (f->version == 2) {
646 int sx = ff_ffv1_get_symbol(c, state, 0);
647 int sy = ff_ffv1_get_symbol(c, state, 0);
648 int sw = ff_ffv1_get_symbol(c, state, 0) + 1U;
649 int sh = ff_ffv1_get_symbol(c, state, 0) + 1U;
650
651 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
652 return AVERROR_INVALIDDATA;
653 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
654 return AVERROR_INVALIDDATA;
655
656 sc->slice_x = sx * (int64_t)f->width / f->num_h_slices;
657 sc->slice_y = sy * (int64_t)f->height / f->num_v_slices;
658 sc->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - sc->slice_x;
659 sc->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - sc->slice_y;
660
661 av_assert0((unsigned)sc->slice_width <= f->width &&
662 (unsigned)sc->slice_height <= f->height);
663 av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width
664 && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
665 }
666
669 if (!sc->plane)
670 return AVERROR(ENOMEM);
671
672 for (int i = 0; i < f->plane_count; i++) {
673 PlaneContext *const p = &sc->plane[i];
674
675 if (f->version == 2) {
676 int idx = ff_ffv1_get_symbol(c, state, 0);
677 if (idx >= (unsigned)f->quant_table_count) {
678 av_log(f->avctx, AV_LOG_ERROR,
679 "quant_table_index out of range\n");
680 return AVERROR_INVALIDDATA;
681 }
682 p->quant_table_index = idx;
683 context_count = f->context_count[idx];
684 }
685
686 if (f->version <= 2) {
687 av_assert0(context_count >= 0);
688 p->context_count = context_count;
689 }
690 }
691 }
692 return 0;
693}
694
696{
697 FFV1Context *f = avctx->priv_data;
698 int ret;
699
700 f->pix_fmt = AV_PIX_FMT_NONE;
701 f->configured_pix_fmt = AV_PIX_FMT_NONE;
702
703 if ((ret = ff_ffv1_common_init(avctx, f)) < 0)
704 return ret;
705
706 if (avctx->extradata_size > 0 && (ret = ff_ffv1_read_extra_header(f)) < 0)
707 return ret;
708
709 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
710 return ret;
711
712 return 0;
713}
714
716 uint8_t *buf, uint8_t *buf_end, int idx,
717 uint8_t **pos, uint32_t *len)
718{
719 FFV1Context *f = avctx->priv_data;
720
721 /* Length field */
722 uint32_t v = buf_end - buf;
723 if (idx || f->version > 2) {
724 /* Three bytes of length, plus flush bit + CRC */
725 uint32_t trailer = 3 + 5*!!f->ec;
726 if (trailer > buf_end - buf)
727 v = INT_MAX;
728 else
729 v = AV_RB24(buf_end - trailer) + trailer;
730 }
731
732 if (buf_end - buf < v) {
733 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
734 ff_progress_frame_report(&f->picture, INT_MAX);
735 return AVERROR_INVALIDDATA;
736 }
737
738 *len = v;
739 if (idx)
740 *pos = buf_end - v;
741 else
742 *pos = buf;
743
744 return 0;
745}
746
748 uint8_t *buf, size_t buf_size)
749{
750 int ret;
751 FFV1Context *f = avctx->priv_data;
752
753 uint8_t keystate = 128;
754 ff_init_range_decoder(c, buf, buf_size);
755 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
756
757 if (get_rac(c, &keystate)) {
758 f->key_frame = AV_FRAME_FLAG_KEY;
759 f->key_frame_ok = 0;
760 if ((ret = read_header(f, c)) < 0)
761 return ret;
762 f->key_frame_ok = 1;
763 } else {
764 if (!f->key_frame_ok) {
765 av_log(avctx, AV_LOG_ERROR,
766 "Cannot decode non-keyframe without valid keyframe\n");
767 return AVERROR_INVALIDDATA;
768 }
769 f->key_frame = 0;
770 }
771
772 if (f->ac != AC_GOLOMB_RICE) {
773 if (buf_size < avctx->width * avctx->height / (128*8))
774 return AVERROR_INVALIDDATA;
775 } else {
776 int w = avctx->width;
777 int s = 1 + w / (1<<23);
778 int i;
779
780 w /= s;
781
782 for (i = 0; w > (1<<ff_log2_run[i]); i++)
783 w -= ff_log2_run[i];
784 if (buf_size < (avctx->height + i + 6) / 8 * s)
785 return AVERROR_INVALIDDATA;
786 }
787
788 return 0;
789}
790
792 AVPacket *avpkt)
793{
794 FFV1Context *f = avctx->priv_data;
795 AVFrame *p = f->picture.f;
796
797 uint8_t *buf = avpkt->data;
798 size_t buf_size = avpkt->size;
799 uint8_t *buf_end = buf + buf_size;
800
801 for (int i = f->slice_count - 1; i >= 0; i--) {
802 FFV1SliceContext *sc = &f->slices[i];
803
804 uint8_t *pos;
805 uint32_t len;
806 int err = find_next_slice(avctx, buf, buf_end, i,
807 &pos, &len);
808 if (err < 0)
809 return err;
810
811 buf_end -= len;
812
813 sc->slice_damaged = 0;
814
815 if (f->ec) {
816 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, pos, len);
817 if (crc != f->crcref) {
818 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
819 av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
820 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
821 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
822 } else if (ts != AV_NOPTS_VALUE) {
823 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
824 } else {
825 av_log(f->avctx, AV_LOG_ERROR, "\n");
826 }
827 slice_set_damaged(f, sc);
828 }
829 if (avctx->debug & FF_DEBUG_PICT_INFO) {
830 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(pos + len - 4));
831 }
832 }
833
834 if (i) {
836 ff_build_rac_states(&sc->c, 0.05 * (1LL << 32), 256 - 8);
837 } else {
838 sc->c = c;
839 sc->c.bytestream_end = pos + len;
840 }
841 }
842
843 avctx->execute(avctx,
845 f->slices,
846 NULL,
847 f->slice_count,
848 sizeof(*f->slices));
849
850 for (int i = f->slice_count - 1; i >= 0; i--) {
851 FFV1SliceContext *sc = &f->slices[i];
852 if (sc->slice_damaged && f->last_picture.f) {
853 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(f->pix_fmt);
854 const uint8_t *src[4];
855 uint8_t *dst[4];
856 ff_progress_frame_await(&f->last_picture, INT_MAX);
857 for (int j = 0; j < desc->nb_components; j++) {
858 int pixshift = desc->comp[j].depth > 8;
859 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
860 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
861 dst[j] = p->data[j] + p->linesize[j] *
862 (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift);
863 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
864 (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift);
865
866 }
867
868 av_image_copy(dst, p->linesize, src,
869 f->last_picture.f->linesize,
870 f->pix_fmt,
871 sc->slice_width,
872 sc->slice_height);
873
874 f->slice_damaged[i] = 1;
875 }
876 }
877
878 return 0;
879}
880
881static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
882 int *got_frame, AVPacket *avpkt)
883{
884 FFV1Context *f = avctx->priv_data;
885 int ret;
886 AVFrame *p;
887 const FFHWAccel *hwaccel = NULL;
888
889 /* This is copied onto the first slice's range coder context */
891
892 ff_progress_frame_unref(&f->last_picture);
893 av_refstruct_unref(&f->hwaccel_last_picture_private);
894
895 FFSWAP(ProgressFrame, f->picture, f->last_picture);
896 FFSWAP(void *, f->hwaccel_picture_private, f->hwaccel_last_picture_private);
897
898 f->avctx = avctx;
899 f->frame_damaged = 0;
900
901 ret = decode_header(avctx, &c, avpkt->data, avpkt->size);
902 if (ret < 0)
903 return ret;
904
905 if (avctx->debug & FF_DEBUG_PICT_INFO)
906 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
907 f->version, !!f->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
908
909 if (avctx->skip_frame >= AVDISCARD_ALL)
910 return avpkt->size;
911
912 if (avctx->hwaccel)
913 hwaccel = ffhwaccel(avctx->hwaccel);
914
915 ret = ff_progress_frame_get_buffer(avctx, &f->picture,
917 if (ret < 0)
918 return ret;
919
920 ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
921 if (ret < 0)
922 return ret;
923
924 p = f->picture.f;
925
926 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
927 p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | f->key_frame;
928
929 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
930 /* we have interlaced material flagged in container */
931 p->flags |= AV_FRAME_FLAG_INTERLACED;
932 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
934 }
935
936 /* Start */
937 if (hwaccel) {
938 ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
939 if (ret < 0)
940 return ret;
941 }
942
944
945 /* Decode slices */
946 if (hwaccel) {
947 uint8_t *buf_end = avpkt->data + avpkt->size;
948
949 if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
950 ff_progress_frame_await(&f->last_picture, f->slice_count - 1);
951
952 for (int i = f->slice_count - 1; i >= 0; i--) {
953 uint8_t *pos;
954 uint32_t len;
955 ret = find_next_slice(avctx, avpkt->data, buf_end, i,
956 &pos, &len);
957 if (ret < 0)
958 return ret;
959
960 buf_end -= len;
961
962 ret = hwaccel->decode_slice(avctx, pos, len);
963 if (ret < 0)
964 return ret;
965 }
966 } else {
967 ret = decode_slices(avctx, c, avpkt);
968 if (ret < 0)
969 return ret;
970 }
971
972 /* Finalize */
973 if (hwaccel) {
974 ret = hwaccel->end_frame(avctx);
975 if (ret < 0)
976 return ret;
977 }
978
979 ff_progress_frame_report(&f->picture, INT_MAX);
980
981 ff_progress_frame_unref(&f->last_picture);
982 av_refstruct_unref(&f->hwaccel_last_picture_private);
983 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
984 return ret;
985
986 *got_frame = 1;
987
988 return avpkt->size;
989}
990
991#if HAVE_THREADS
992static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
993{
994 FFV1Context *fsrc = src->priv_data;
995 FFV1Context *fdst = dst->priv_data;
996
997 if (dst == src)
998 return 0;
999
1000 fdst->version = fsrc->version;
1001 fdst->micro_version = fsrc->micro_version;
1002 fdst->combined_version = fsrc->combined_version;
1003 fdst->chroma_planes = fsrc->chroma_planes;
1004 fdst->chroma_h_shift = fsrc->chroma_h_shift;
1005 fdst->chroma_v_shift = fsrc->chroma_v_shift;
1006 fdst->transparency = fsrc->transparency;
1007 fdst->plane_count = fsrc->plane_count;
1008 fdst->ac = fsrc->ac;
1009 fdst->colorspace = fsrc->colorspace;
1010 fdst->pix_fmt = fsrc->pix_fmt;
1012 fdst->configured_ac = fsrc->configured_ac;
1013 fdst->configured_width = fsrc->configured_width;
1015
1016 fdst->ec = fsrc->ec;
1017 fdst->intra = fsrc->intra;
1018 fdst->key_frame_ok = fsrc->key_frame_ok;
1019
1020 fdst->packed_at_lsb = fsrc->packed_at_lsb;
1021 fdst->slice_count = fsrc->slice_count;
1022 fdst->use32bit = fsrc->use32bit;
1023 memcpy(fdst->state_transition, fsrc->state_transition,
1024 sizeof(fdst->state_transition));
1025
1026 // in version 1 there is a single per-keyframe quant table, so
1027 // we need to propagate it between threads
1028 if (fsrc->version < 2)
1029 memcpy(fdst->quant_tables[0], fsrc->quant_tables[0], sizeof(fsrc->quant_tables[0]));
1030
1031 for (int i = 0; i < fdst->num_h_slices * fdst->num_v_slices; i++) {
1032 FFV1SliceContext *sc = &fdst->slices[i];
1033 const FFV1SliceContext *sc0 = &fsrc->slices[i];
1034
1035 av_refstruct_replace(&sc->plane, sc0->plane);
1036
1037 if (fsrc->version < 3) {
1038 sc->slice_x = sc0->slice_x;
1039 sc->slice_y = sc0->slice_y;
1040 sc->slice_width = sc0->slice_width;
1041 sc->slice_height = sc0->slice_height;
1042 }
1043 }
1044
1046
1048
1052
1053 return 0;
1054}
1055#endif
1056
1058{
1059 FFV1Context *const s = avctx->priv_data;
1060
1061 ff_progress_frame_unref(&s->picture);
1062 av_refstruct_unref(&s->hwaccel_picture_private);
1063
1064 ff_progress_frame_unref(&s->last_picture);
1065 av_refstruct_unref(&s->hwaccel_last_picture_private);
1066
1068
1069 return 0;
1070}
1071
1073 .p.name = "ffv1",
1074 CODEC_LONG_NAME("FFmpeg video codec #1"),
1075 .p.type = AVMEDIA_TYPE_VIDEO,
1076 .p.id = AV_CODEC_ID_FFV1,
1077 .priv_data_size = sizeof(FFV1Context),
1078 .init = decode_init,
1081 UPDATE_THREAD_CONTEXT(update_thread_context),
1082 .p.capabilities = AV_CODEC_CAP_DR1 |
1084 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1087 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1088#if CONFIG_FFV1_VULKAN_HWACCEL
1089 HWACCEL_VULKAN(ffv1),
1090#endif
1091 NULL
1092 },
1093};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define RENAME(element)
const FFCodec ff_ffv1_decoder
Definition ffv1dec.c:1072
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1590
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1393
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_CAP_USES_PROGRESSFRAMES
The decoder might make use of the ProgressFrame API.
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_ceil_log2
Definition common.h:97
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
#define MAX_SLICES
void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
Do nothing if dst and src already refer to the same AVFrame; otherwise unreference dst and if src is ...
Definition decode.c:1969
void ff_progress_frame_await(const ProgressFrame *f, int n)
Wait for earlier decoding threads to finish reference frames.
Definition decode.c:1984
void ff_progress_frame_report(ProgressFrame *f, int n)
Notify later decoding threads when part of their reference frame is ready.
Definition decode.c:1979
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition decode.c:1229
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
Definition decode.c:2336
int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
Wrapper around ff_progress_frame_alloc() and ff_thread_get_buffer().
Definition decode.c:1939
void ff_progress_frame_unref(ProgressFrame *f)
Give up a reference to the underlying frame contained in a ProgressFrame and reset the ProgressFrame,...
Definition decode.c:1962
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition defs.h:214
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition defs.h:216
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
Definition fastaudio.c:100
static const char * hwaccel
Definition ffplay.c:357
av_cold int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1.c:72
av_cold int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s)
Definition ffv1.c:36
PlaneContext * ff_ffv1_planes_alloc(void)
Definition ffv1.c:66
int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift)
This is intended for both width and height.
Definition ffv1.c:127
av_cold void ff_ffv1_close(FFV1Context *s)
Definition ffv1.c:268
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1.c:200
void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample)
Definition ffv1.c:224
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
Definition ffv1.c:142
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Definition ffv1.c:263
FF Video Codec 1 (a lossless codec)
int ff_ffv1_read_extra_header(FFV1Context *f)
Definition ffv1_parse.c:70
#define CONTEXT_SIZE
Definition ffv1.h:45
static void update_vlc_state(VlcState *const state, const int v)
Definition ffv1.h:227
int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state)
Definition ffv1_parse.c:216
#define AC_GOLOMB_RICE
Definition ffv1.h:52
int ff_ffv1_read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition ffv1_parse.c:52
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition ffv1.h:256
static av_always_inline int fold(int diff, int bits)
Definition ffv1.h:216
static int decode_slice_header(const FFV1Context *f, FFV1SliceContext *sc, AVFrame *frame)
Definition ffv1dec.c:176
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition ffv1dec.c:881
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition ffv1dec.c:48
static av_cold int ffv1_decode_close(AVCodecContext *avctx)
Definition ffv1dec.c:1057
static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
Definition ffv1dec.c:78
static int decode_slices(AVCodecContext *avctx, RangeCoder c, AVPacket *avpkt)
Definition ffv1dec.c:791
#define TYPE
Definition ffv1dec.c:90
static int decode_plane(FFV1Context *f, FFV1SliceContext *sc, GetBitContext *gb, uint8_t *src, int w, int h, int stride, int plane_index, int remap_index, int pixel_stride, int ac)
Definition ffv1dec.c:100
static av_cold int decode_init(AVCodecContext *avctx)
Definition ffv1dec.c:695
static int decode_slice(AVCodecContext *c, void *arg)
Definition ffv1dec.c:449
static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1dec.c:279
static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1dec.c:300
#define RENAME(name)
Definition ffv1dec.c:91
static int decode_header(AVCodecContext *avctx, RangeCoder *c, uint8_t *buf, size_t buf_size)
Definition ffv1dec.c:747
static int find_next_slice(AVCodecContext *avctx, uint8_t *buf, uint8_t *buf_end, int idx, uint8_t **pos, uint32_t *len)
Definition ffv1dec.c:715
static int decode_bayer_frame(FFV1Context *f, FFV1SliceContext *sc, GetBitContext *gb, uint8_t *src, int w, int h, int stride)
Definition ffv1dec.c:379
static enum AVPixelFormat get_pixel_format(FFV1Context *f)
Definition ffv1dec.c:565
static int decode_current_mul(RangeCoder *rc, uint8_t state[32], int *mul, int mul_count, int64_t i)
Definition ffv1dec.c:289
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define sample
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
exp golomb vlc stuff
static int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (ffv1).
Definition golomb.h:532
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_FFV1
Definition codec_id.h:83
@ AVDISCARD_ALL
discard all
Definition defs.h:232
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_32_IEEE
Definition crc.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:695
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:700
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:278
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:555
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4], const uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition imgutils.c:422
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition imgutils.c:323
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_VULKAN(codec)
Definition hwconfig.h:78
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RB32(p)
#define AV_RB24(x)
#define MAX_OVERREAD
Definition lagarithrac.h:49
unsigned offset
Definition libaomenc.c:763
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
const char * arg
Definition jacosubdec.c:65
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition rawdec.c:131
Multithreading API for decoders.
#define av_cold
Definition attributes.h:117
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
const uint8_t ff_log2_run[41]
Definition mathtables.c:155
Memory handling functions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition rangecoder.c:53
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition rangecoder.c:68
Range coder.
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition rangecoder.h:118
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:160
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition refstruct.h:105
unsigned int pos
Definition spdifenc.c:431
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
int debug
debug
Definition avcodec.h:1392
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition avcodec.h:554
enum AVFieldOrder field_order
Field order.
Definition avcodec.h:694
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1423
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition avcodec.h:1609
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1667
int step
Number of elements between 2 horizontally consecutive pixels.
Definition pixdesc.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:586
int size
Definition packet.h:604
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
uint8_t * data
Definition packet.h:603
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int configured_height
Definition ffv1.h:142
int ac
1=range coder <-> 0=golomb rice
Definition ffv1.h:147
int colorspace
Definition ffv1.h:152
int version
Definition ffv1.h:127
int transparency
Definition ffv1.h:133
int num_v_slices
Definition ffv1.h:176
ProgressFrame picture
Definition ffv1.h:137
int configured_width
Definition ffv1.h:142
uint8_t * slice_damaged
Definition ffv1.h:187
int max_slice_count
Definition ffv1.h:175
int chroma_planes
Definition ffv1.h:131
int num_h_slices
Definition ffv1.h:177
int plane_count
Definition ffv1.h:146
int slice_count
Definition ffv1.h:174
enum AVPixelFormat configured_pix_fmt
Definition ffv1.h:141
int micro_version
Definition ffv1.h:128
int use32bit
Definition ffv1.h:160
void * hwaccel_picture_private
Definition ffv1.h:138
int chroma_h_shift
Definition ffv1.h:132
int key_frame_ok
Definition ffv1.h:164
int configured_ac
Definition ffv1.h:143
int packed_at_lsb
Definition ffv1.h:169
FFV1SliceContext * slices
Definition ffv1.h:179
int chroma_v_shift
Definition ffv1.h:132
int combined_version
Definition ffv1.h:129
int ec
Definition ffv1.h:162
uint8_t state_transition[256]
Definition ffv1.h:150
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]
Definition ffv1.h:148
enum AVPixelFormat pix_fmt
Definition ffv1.h:140
int intra
Definition ffv1.h:163
int slice_rct_ry_coef
Definition ffv1.h:86
int slice_damaged
Definition ffv1.h:100
int16_t * sample_buffer
Definition ffv1.h:74
unsigned int fltmap32_size[4]
Definition ffv1.h:115
int ac_byte_count
number of bytes used for AC coding
Definition ffv1.h:94
RangeCoder c
Definition ffv1.h:92
int slice_reset_contexts
Definition ffv1.h:99
uint32_t * fltmap32[4]
Definition ffv1.h:113
PlaneContext * plane
Definition ffv1.h:90
uint16_t * fltmap[4]
Definition ffv1.h:112
unsigned int fltmap_size[4]
Definition ffv1.h:114
int run_index
Definition ffv1.h:83
int remap_count[4]
Definition ffv1.h:109
int slice_height
Definition ffv1.h:78
int slice_width
Definition ffv1.h:77
int slice_coding_mode
Definition ffv1.h:84
int slice_rct_by_coef
Definition ffv1.h:85
The ProgressFrame structure.
uint8_t * bytestream_start
Definition rangecoder.h:42
uint8_t * bytestream
Definition rangecoder.h:43
uint8_t * bytestream_end
Definition rangecoder.h:44
uint8_t run
Definition svq3.c:207
#define stride
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size
else temp
Definition vf_mcdeint.c:275
float delta
int len
static double c[64]