| Line | Branch | Exec | 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 | 163659991 | static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state, | |
| 49 | int bits) | ||
| 50 | { | ||
| 51 | int k, i, v, ret; | ||
| 52 | |||
| 53 | 163659991 | i = state->count; | |
| 54 | 163659991 | k = 0; | |
| 55 |
2/2✓ Branch 0 taken 272532174 times.
✓ Branch 1 taken 163659991 times.
|
436192165 | while (i < state->error_sum) { // FIXME: optimize |
| 56 | 272532174 | k++; | |
| 57 | 272532174 | i += i; | |
| 58 | } | ||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 163659991 times.
|
163659991 | 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 | 163659991 | 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 | 163659991 | v ^= ((2 * state->drift + state->count) >> 31); | |
| 70 | |||
| 71 | 163659991 | ret = fold(v + state->bias, bits); | |
| 72 | |||
| 73 | 163659991 | update_vlc_state(state, v); | |
| 74 | |||
| 75 | 163659991 | return ret; | |
| 76 | } | ||
| 77 | |||
| 78 | 4542400 | static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac) | |
| 79 | { | ||
| 80 |
2/2✓ Branch 0 taken 2514800 times.
✓ Branch 1 taken 2027600 times.
|
4542400 | if (ac != AC_GOLOMB_RICE) { |
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2514800 times.
|
2514800 | if (c->overread > MAX_OVERREAD) |
| 82 | ✗ | return AVERROR_INVALIDDATA; | |
| 83 | } else { | ||
| 84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2027600 times.
|
2027600 | if (get_bits_left(gb) < 1) |
| 85 | ✗ | return AVERROR_INVALIDDATA; | |
| 86 | } | ||
| 87 | 4542400 | 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 | |||
| 100 | 18300 | 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 | { | ||
| 105 | int x, y; | ||
| 106 | int16_t *sample[2]; | ||
| 107 | int bits; | ||
| 108 | unsigned mask; | ||
| 109 | |||
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18300 times.
|
18300 | 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 | 18300 | bits = f->avctx->bits_per_raw_sample; | |
| 117 | } | ||
| 118 | |||
| 119 | 18300 | sample[0] = sc->sample_buffer + 3; | |
| 120 | 18300 | sample[1] = sc->sample_buffer + w + 6 + 3; | |
| 121 | |||
| 122 | 18300 | sc->run_index = 0; | |
| 123 | |||
| 124 | 18300 | memset(sc->sample_buffer, 0, 2 * (w + 6) * sizeof(*sc->sample_buffer)); | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 1732400 times.
✓ Branch 1 taken 18300 times.
|
1750700 | for (y = 0; y < h; y++) { |
| 127 | 1732400 | int16_t *temp = sample[0]; // FIXME: try a normal buffer | |
| 128 | |||
| 129 | 1732400 | sample[0] = sample[1]; | |
| 130 | 1732400 | sample[1] = temp; | |
| 131 | |||
| 132 | 1732400 | sample[1][-1] = sample[0][0]; | |
| 133 | 1732400 | sample[0][w] = sample[0][w - 1]; | |
| 134 | |||
| 135 |
2/2✓ Branch 0 taken 924200 times.
✓ Branch 1 taken 808200 times.
|
1732400 | if (f->avctx->bits_per_raw_sample <= 8) { |
| 136 | 924200 | int ret = decode_line(f, sc, gb, w, sample, plane_index, 8, ac); | |
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 924200 times.
|
924200 | if (ret < 0) |
| 138 | ✗ | return ret; | |
| 139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 924200 times.
|
924200 | if (sc->remap) |
| 140 | ✗ | for (x = 0; x < w; x++) | |
| 141 | ✗ | sample[1][x] = sc->fltmap[remap_index][sample[1][x]]; | |
| 142 |
2/2✓ Branch 0 taken 145253900 times.
✓ Branch 1 taken 924200 times.
|
146178100 | for (x = 0; x < w; x++) |
| 143 | 145253900 | src[x*pixel_stride + stride * y] = sample[1][x]; | |
| 144 | } else { | ||
| 145 | 808200 | int ret = decode_line(f, sc, gb, w, sample, plane_index, bits, ac); | |
| 146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 808200 times.
|
808200 | if (ret < 0) |
| 147 | ✗ | return ret; | |
| 148 | |||
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 808200 times.
|
808200 | 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 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 808200 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
808200 | if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) { |
| 162 |
2/2✓ Branch 0 taken 106856200 times.
✓ Branch 1 taken 808200 times.
|
107664400 | for (x = 0; x < w; x++) { |
| 163 | 106856200 | ((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 | 18300 | return 0; | |
| 174 | } | ||
| 175 | |||
| 176 | 6700 | static int decode_slice_header(const FFV1Context *f, | |
| 177 | FFV1SliceContext *sc, AVFrame *frame) | ||
| 178 | { | ||
| 179 | 6700 | RangeCoder *c = &sc->c; | |
| 180 | uint8_t state[CONTEXT_SIZE]; | ||
| 181 | unsigned ps, context_count; | ||
| 182 | int sx, sy, sw, sh; | ||
| 183 | |||
| 184 | 6700 | memset(state, 128, sizeof(state)); | |
| 185 | 6700 | sx = ff_ffv1_get_symbol(c, state, 0); | |
| 186 | 6700 | sy = ff_ffv1_get_symbol(c, state, 0); | |
| 187 | 6700 | sw = ff_ffv1_get_symbol(c, state, 0) + 1U; | |
| 188 | 6700 | sh = ff_ffv1_get_symbol(c, state, 0) + 1U; | |
| 189 | |||
| 190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | av_assert0(f->version > 2); |
| 191 | |||
| 192 | |||
| 193 |
4/8✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6700 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6700 times.
|
6700 | if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0) |
| 194 | ✗ | return AVERROR_INVALIDDATA; | |
| 195 |
2/4✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6700 times.
|
6700 | if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh) |
| 196 | ✗ | return AVERROR_INVALIDDATA; | |
| 197 | |||
| 198 | 6700 | sc->slice_x = ff_slice_coord(f, f->width , sx , f->num_h_slices, f->chroma_h_shift); | |
| 199 | 6700 | sc->slice_y = ff_slice_coord(f, f->height, sy , f->num_v_slices, f->chroma_v_shift); | |
| 200 | 6700 | sc->slice_width = ff_slice_coord(f, f->width , sx + sw, f->num_h_slices, f->chroma_h_shift) - sc->slice_x; | |
| 201 | 6700 | sc->slice_height = ff_slice_coord(f, f->height, sy + sh, f->num_v_slices, f->chroma_v_shift) - sc->slice_y; | |
| 202 | |||
| 203 |
2/4✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6700 times.
|
6700 | av_assert0((unsigned)sc->slice_width <= f->width && |
| 204 | (unsigned)sc->slice_height <= f->height); | ||
| 205 |
2/4✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6700 times.
|
6700 | 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 |
3/4✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 4000 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2700 times.
|
6700 | if (f->ac == AC_GOLOMB_RICE && sc->slice_width >= (1<<23)) |
| 209 | ✗ | return AVERROR_INVALIDDATA; | |
| 210 | |||
| 211 |
2/2✓ Branch 0 taken 13400 times.
✓ Branch 1 taken 6700 times.
|
20100 | for (unsigned i = 0; i < f->plane_count; i++) { |
| 212 | 13400 | PlaneContext * const p = &sc->plane[i]; | |
| 213 | 13400 | int idx = ff_ffv1_get_symbol(c, state, 0); | |
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13400 times.
|
13400 | 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 | 13400 | p->quant_table_index = idx; | |
| 219 | 13400 | context_count = f->context_count[idx]; | |
| 220 | |||
| 221 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 12056 times.
|
13400 | if (p->context_count < context_count) { |
| 222 | 1344 | av_freep(&p->state); | |
| 223 | 1344 | av_freep(&p->vlc_state); | |
| 224 | } | ||
| 225 | 13400 | p->context_count = context_count; | |
| 226 | } | ||
| 227 | |||
| 228 | 6700 | ps = ff_ffv1_get_symbol(c, state, 0); | |
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | if (ps == 1) { |
| 230 | ✗ | frame->flags |= AV_FRAME_FLAG_INTERLACED; | |
| 231 | ✗ | frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
| 232 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | } else if (ps == 2) { |
| 233 | ✗ | frame->flags |= AV_FRAME_FLAG_INTERLACED; | |
| 234 | ✗ | frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
| 235 |
1/2✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
|
6700 | } else if (ps == 3) { |
| 236 | 6700 | frame->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
| 237 | } | ||
| 238 | 6700 | frame->sample_aspect_ratio.num = ff_ffv1_get_symbol(c, state, 0); | |
| 239 | 6700 | frame->sample_aspect_ratio.den = ff_ffv1_get_symbol(c, state, 0); | |
| 240 | |||
| 241 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
|
6700 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | if (f->version > 3) { |
| 250 | ✗ | sc->slice_reset_contexts = get_rac(c, state); | |
| 251 | ✗ | sc->slice_coding_mode = ff_ffv1_get_symbol(c, state, 0); | |
| 252 | ✗ | if (sc->slice_coding_mode != 1 && f->colorspace != 0) { | |
| 253 | ✗ | sc->slice_rct_by_coef = ff_ffv1_get_symbol(c, state, 0); | |
| 254 | ✗ | sc->slice_rct_ry_coef = ff_ffv1_get_symbol(c, state, 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | 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 | 6700 | return 0; | |
| 277 | } | ||
| 278 | |||
| 279 | ✗ | static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc) | |
| 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 | |||
| 289 | ✗ | static 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 | |||
| 300 | ✗ | static int decode_remap(FFV1Context *f, FFV1SliceContext *sc) | |
| 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 | ✗ | sc->remap_count[p] = j; | |
| 373 | } | ||
| 374 | ✗ | return 0; | |
| 375 | } | ||
| 376 | |||
| 377 | ✗ | static int decode_bayer_frame(FFV1Context *f, FFV1SliceContext *sc, | |
| 378 | GetBitContext *gb, | ||
| 379 | uint8_t *src, int w, int h, int stride) | ||
| 380 | { | ||
| 381 | int x, y, p; | ||
| 382 | TYPE *sample[4][2]; | ||
| 383 | ✗ | int ac = f->ac; | |
| 384 | unsigned mask[4]; | ||
| 385 | |||
| 386 | int bits[4], offset; | ||
| 387 | ✗ | ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, mask, f->avctx->bits_per_raw_sample); | |
| 388 | |||
| 389 | ✗ | w >>= 1; | |
| 390 | |||
| 391 | ✗ | if (sc->slice_coding_mode == 1) | |
| 392 | ✗ | ac = 1; | |
| 393 | |||
| 394 | ✗ | for (x = 0; x < 4; x++) { | |
| 395 | ✗ | sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3; | |
| 396 | ✗ | sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3; | |
| 397 | } | ||
| 398 | |||
| 399 | ✗ | sc->run_index = 0; | |
| 400 | |||
| 401 | ✗ | memset(RENAME(sc->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(sc->sample_buffer))); | |
| 402 | |||
| 403 | ✗ | for (y = 0; y < h; y += 2) { | |
| 404 | ✗ | for (p = 0; p < 4; p++) { | |
| 405 | int ret; | ||
| 406 | ✗ | TYPE *temp = sample[p][0]; // FIXME: try a normal buffer | |
| 407 | |||
| 408 | ✗ | sample[p][0] = sample[p][1]; | |
| 409 | ✗ | sample[p][1] = temp; | |
| 410 | |||
| 411 | ✗ | sample[p][1][-1]= sample[p][0][0 ]; | |
| 412 | ✗ | sample[p][0][ w]= sample[p][0][w-1]; | |
| 413 | ✗ | ret = RENAME(decode_line)(f, sc, gb, w, sample[p], | |
| 414 | ✗ | p == 1 ? 2 : (p > 1), bits[p], ac); | |
| 415 | ✗ | if (ret < 0) | |
| 416 | ✗ | return ret; | |
| 417 | } | ||
| 418 | |||
| 419 | ✗ | for (x = 0; x < w; x++) { | |
| 420 | ✗ | int g_r = sample[0][1][x]; | |
| 421 | ✗ | int g_b = sample[1][1][x]; | |
| 422 | ✗ | int b = sample[2][1][x]; | |
| 423 | ✗ | int r = sample[3][1][x]; | |
| 424 | |||
| 425 | ✗ | if (sc->slice_coding_mode != 1) { | |
| 426 | ✗ | b -= offset; | |
| 427 | ✗ | r -= offset; | |
| 428 | ✗ | g_r -= (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2; | |
| 429 | ✗ | b += g_r; | |
| 430 | ✗ | r += g_r; | |
| 431 | |||
| 432 | /* Recover green pair: encoder stored gm = gb + (gd >> 1), gd = gr - gb */ | ||
| 433 | ✗ | int gd = g_b - offset; | |
| 434 | ✗ | g_b = g_r - (gd >> 1); | |
| 435 | ✗ | g_r = g_b + gd; | |
| 436 | } | ||
| 437 | |||
| 438 | ✗ | *((uint16_t*)(src + (x*2 + 0)*2 + stride*(y + 0))) = r; | |
| 439 | ✗ | *((uint16_t*)(src + (x*2 + 1)*2 + stride*(y + 0))) = g_r; | |
| 440 | ✗ | *((uint16_t*)(src + (x*2 + 0)*2 + stride*(y + 1))) = g_b; | |
| 441 | ✗ | *((uint16_t*)(src + (x*2 + 1)*2 + stride*(y + 1))) = b; | |
| 442 | } | ||
| 443 | } | ||
| 444 | ✗ | return 0; | |
| 445 | } | ||
| 446 | |||
| 447 | 7700 | static int decode_slice(AVCodecContext *c, void *arg) | |
| 448 | { | ||
| 449 | 7700 | FFV1Context *f = c->priv_data; | |
| 450 | 7700 | FFV1SliceContext *sc = arg; | |
| 451 | int width, height, x, y, ret; | ||
| 452 | 7700 | const int ps = av_pix_fmt_desc_get(f->pix_fmt)->comp[0].step; | |
| 453 | 7700 | AVFrame * const p = f->picture.f; | |
| 454 | 7700 | const int si = sc - f->slices; | |
| 455 | GetBitContext gb; | ||
| 456 |
3/4✓ Branch 0 taken 3700 times.
✓ Branch 1 taken 4000 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3700 times.
|
7700 | int ac = f->ac || sc->slice_coding_mode == 1; |
| 457 | |||
| 458 |
3/4✓ Branch 0 taken 6928 times.
✓ Branch 1 taken 772 times.
✓ Branch 2 taken 6928 times.
✗ Branch 3 not taken.
|
7700 | if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f) |
| 459 | 6928 | ff_progress_frame_await(&f->last_picture, si); | |
| 460 | |||
| 461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
|
7700 | if (f->slice_damaged[si]) |
| 462 | ✗ | slice_set_damaged(f, sc); | |
| 463 | |||
| 464 | 7700 | sc->slice_rct_by_coef = 1; | |
| 465 | 7700 | sc->slice_rct_ry_coef = 1; | |
| 466 | |||
| 467 |
2/2✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 1000 times.
|
7700 | if (f->version > 2) { |
| 468 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
|
6700 | if (ff_ffv1_init_slice_state(f, sc) < 0) |
| 469 | ✗ | return AVERROR(ENOMEM); | |
| 470 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
|
6700 | if (decode_slice_header(f, sc, p) < 0) { |
| 471 | ✗ | sc->slice_x = sc->slice_y = sc->slice_height = sc->slice_width = 0; | |
| 472 | ✗ | slice_set_damaged(f, sc); | |
| 473 | ✗ | return AVERROR_INVALIDDATA; | |
| 474 | } | ||
| 475 | } | ||
| 476 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7700 times.
|
7700 | if ((ret = ff_ffv1_init_slice_state(f, sc)) < 0) |
| 477 | ✗ | return ret; | |
| 478 |
3/4✓ Branch 0 taken 6928 times.
✓ Branch 1 taken 772 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6928 times.
|
7700 | if ((p->flags & AV_FRAME_FLAG_KEY) || sc->slice_reset_contexts) { |
| 479 | 772 | ff_ffv1_clear_slice_state(f, sc); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6928 times.
|
6928 | } else if (sc->slice_damaged) { |
| 481 | ✗ | return AVERROR_INVALIDDATA; | |
| 482 | } | ||
| 483 | |||
| 484 | 7700 | width = sc->slice_width; | |
| 485 | 7700 | height = sc->slice_height; | |
| 486 | 7700 | x = sc->slice_x; | |
| 487 | 7700 | y = sc->slice_y; | |
| 488 | |||
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
|
7700 | if (sc->remap) { |
| 490 | ✗ | ret = decode_remap(f, sc); | |
| 491 | ✗ | if (ret < 0) | |
| 492 | ✗ | return ret; | |
| 493 | } | ||
| 494 | |||
| 495 |
2/2✓ Branch 0 taken 3700 times.
✓ Branch 1 taken 4000 times.
|
7700 | if (ac == AC_GOLOMB_RICE) { |
| 496 |
2/2✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 1000 times.
|
3700 | if (f->combined_version >= 0x30002) |
| 497 | 2700 | get_rac(&sc->c, (uint8_t[]) { 129 }); | |
| 498 |
6/6✓ Branch 0 taken 1000 times.
✓ Branch 1 taken 2700 times.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 400 times.
✓ Branch 4 taken 400 times.
✓ Branch 5 taken 200 times.
|
3700 | sc->ac_byte_count = f->version > 2 || (!x && !y) ? sc->c.bytestream - sc->c.bytestream_start - 1 : 0; |
| 499 | 3700 | init_get_bits(&gb, | |
| 500 | 3700 | sc->c.bytestream_start + sc->ac_byte_count, | |
| 501 | 3700 | (sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count) * 8); | |
| 502 | } | ||
| 503 | |||
| 504 | av_assert1(width && height); | ||
| 505 |
3/6✓ Branch 0 taken 6100 times.
✓ Branch 1 taken 1600 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6100 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
13800 | if (f->colorspace == 0 && (f->chroma_planes || !f->transparency)) { |
| 506 | 6100 | const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift); | |
| 507 | 6100 | const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift); | |
| 508 | 6100 | const int cx = x >> f->chroma_h_shift; | |
| 509 | 6100 | const int cy = y >> f->chroma_v_shift; | |
| 510 | 6100 | decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 1, ac); | |
| 511 | |||
| 512 |
1/2✓ Branch 0 taken 6100 times.
✗ Branch 1 not taken.
|
6100 | if (f->chroma_planes) { |
| 513 | 6100 | 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); | |
| 514 | 6100 | 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); | |
| 515 | } | ||
| 516 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6100 times.
|
6100 | if (f->transparency) |
| 517 | ✗ | 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, | |
| 518 | ✗ | (f->version >= 4 && !f->chroma_planes) ? 1 : 3, 1, ac); | |
| 519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
|
1600 | } else if (f->colorspace == 0) { |
| 520 | ✗ | decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 0, 2, ac); | |
| 521 | ✗ | 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); | |
| 522 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
|
1600 | } else if (f->bayer) { |
| 523 | ✗ | decode_bayer_frame(f, sc, &gb, p->data[0] + ps * x + y * p->linesize[0], | |
| 524 | width, height, p->linesize[0]); | ||
| 525 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
|
1600 | } else if (f->use32bit) { |
| 526 | 800 | uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0], | |
| 527 | 800 | p->data[1] + ps * x + y * p->linesize[1], | |
| 528 | 800 | p->data[2] + ps * x + y * p->linesize[2] }; | |
| 529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (f->transparency) |
| 530 | ✗ | planes[3] = p->data[3] + ps * x + y * p->linesize[3]; | |
| 531 | 800 | decode_rgb_frame32(f, sc, &gb, planes, width, height, p->linesize); | |
| 532 | } else { | ||
| 533 | 800 | uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0] }; | |
| 534 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (f->avctx->bits_per_raw_sample > 8) { |
| 535 | ✗ | planes[1] = p->data[1] + ps * x + y * p->linesize[1]; | |
| 536 | ✗ | planes[2] = p->data[2] + ps * x + y * p->linesize[2]; | |
| 537 | ✗ | if (f->transparency) | |
| 538 | ✗ | planes[3] = p->data[3] + ps * x + y * p->linesize[3]; | |
| 539 | } | ||
| 540 | 800 | decode_rgb_frame(f, sc, &gb, planes, width, height, p->linesize); | |
| 541 | } | ||
| 542 |
3/4✓ Branch 0 taken 4000 times.
✓ Branch 1 taken 3700 times.
✓ Branch 2 taken 4000 times.
✗ Branch 3 not taken.
|
7700 | if (ac != AC_GOLOMB_RICE && f->version > 2) { |
| 543 | int v; | ||
| 544 | 4000 | get_rac(&sc->c, (uint8_t[]) { 129 }); | |
| 545 |
1/2✓ Branch 0 taken 4000 times.
✗ Branch 1 not taken.
|
4000 | v = sc->c.bytestream_end - sc->c.bytestream - 2 - 5*!!f->ec; |
| 546 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4000 times.
|
4000 | if (v) { |
| 547 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v); | |
| 548 | ✗ | slice_set_damaged(f, sc); | |
| 549 | } | ||
| 550 | } | ||
| 551 | |||
| 552 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7700 | if (sc->slice_damaged && (f->avctx->err_recognition & AV_EF_EXPLODE)) |
| 553 | ✗ | return AVERROR_INVALIDDATA; | |
| 554 | |||
| 555 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7700 | if ((c->active_thread_type & FF_THREAD_FRAME) && !f->frame_damaged) |
| 556 | ✗ | ff_progress_frame_report(&f->picture, si); | |
| 557 | |||
| 558 | 7700 | return 0; | |
| 559 | } | ||
| 560 | |||
| 561 | 87 | static enum AVPixelFormat get_pixel_format(FFV1Context *f) | |
| 562 | { | ||
| 563 | 87 | enum AVPixelFormat pix_fmts[] = { | |
| 564 | #if CONFIG_FFV1_VULKAN_HWACCEL | ||
| 565 | AV_PIX_FMT_VULKAN, | ||
| 566 | #endif | ||
| 567 | 87 | f->pix_fmt, | |
| 568 | AV_PIX_FMT_NONE, | ||
| 569 | }; | ||
| 570 | |||
| 571 | 87 | return ff_get_format(f->avctx, pix_fmts); | |
| 572 | } | ||
| 573 | |||
| 574 | 253 | static int read_header(FFV1Context *f, RangeCoder *c) | |
| 575 | { | ||
| 576 | uint8_t state[CONTEXT_SIZE]; | ||
| 577 | 253 | int context_count = -1; //-1 to avoid warning | |
| 578 | int ret; | ||
| 579 | |||
| 580 | 253 | memset(state, 128, sizeof(state)); | |
| 581 | |||
| 582 | 253 | ret = ff_ffv1_parse_header(f, c, state); | |
| 583 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
|
253 | if (ret < 0) |
| 584 | ✗ | return ret; | |
| 585 | |||
| 586 |
2/2✓ Branch 0 taken 166 times.
✓ Branch 1 taken 87 times.
|
253 | if (f->configured_pix_fmt != f->pix_fmt || |
| 587 |
1/2✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
|
166 | f->configured_width != f->width || |
| 588 |
1/2✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
|
166 | f->configured_height != f->height || |
| 589 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
|
166 | f->configured_ac != f->ac) { |
| 590 | 87 | f->avctx->pix_fmt = get_pixel_format(f); | |
| 591 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
|
87 | if (f->avctx->pix_fmt < 0) |
| 592 | ✗ | return AVERROR(EINVAL); | |
| 593 | 87 | f->configured_pix_fmt = f->pix_fmt; | |
| 594 | 87 | f->configured_width = f->width; | |
| 595 | 87 | f->configured_height = f->height; | |
| 596 | 87 | f->configured_ac = f->ac; | |
| 597 | } | ||
| 598 | |||
| 599 | ff_dlog(f->avctx, "%d %d %d\n", | ||
| 600 | f->chroma_h_shift, f->chroma_v_shift, f->pix_fmt); | ||
| 601 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 229 times.
|
253 | if (f->version < 2) { |
| 602 | 24 | context_count = ff_ffv1_read_quant_tables(c, f->quant_tables[0]); | |
| 603 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (context_count < 0) { |
| 604 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n"); | |
| 605 | ✗ | return AVERROR_INVALIDDATA; | |
| 606 | } | ||
| 607 | 24 | f->slice_count = f->max_slice_count; | |
| 608 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 205 times.
|
229 | } else if (f->version < 3) { |
| 609 | 24 | f->slice_count = ff_ffv1_get_symbol(c, state, 0); | |
| 610 | } else { | ||
| 611 | 205 | const uint8_t *p = c->bytestream_end; | |
| 612 | 205 | for (f->slice_count = 0; | |
| 613 |
4/6✓ Branch 0 taken 1035 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1035 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 830 times.
✓ Branch 5 taken 205 times.
|
1035 | f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start; |
| 614 | 830 | f->slice_count++) { | |
| 615 |
1/2✓ Branch 0 taken 830 times.
✗ Branch 1 not taken.
|
830 | int trailer = 3 + 5*!!f->ec; |
| 616 | 830 | int size = AV_RB24(p-trailer); | |
| 617 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 830 times.
|
830 | if (size + trailer > p - c->bytestream_start) |
| 618 | ✗ | break; | |
| 619 | 830 | p -= size + trailer; | |
| 620 | } | ||
| 621 | } | ||
| 622 |
3/6✓ Branch 0 taken 253 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 253 times.
|
253 | if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) { |
| 623 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count); | |
| 624 | ✗ | return AVERROR_INVALIDDATA; | |
| 625 | } | ||
| 626 | |||
| 627 | 253 | av_refstruct_unref(&f->slice_damaged); | |
| 628 | 253 | f->slice_damaged = av_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged)); | |
| 629 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
|
253 | if (!f->slice_damaged) |
| 630 | ✗ | return AVERROR(ENOMEM); | |
| 631 | |||
| 632 |
2/2✓ Branch 0 taken 950 times.
✓ Branch 1 taken 253 times.
|
1203 | for (int j = 0; j < f->slice_count; j++) { |
| 633 | 950 | FFV1SliceContext *sc = &f->slices[j]; | |
| 634 | |||
| 635 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 854 times.
|
950 | if (f->version == 2) { |
| 636 | 96 | int sx = ff_ffv1_get_symbol(c, state, 0); | |
| 637 | 96 | int sy = ff_ffv1_get_symbol(c, state, 0); | |
| 638 | 96 | int sw = ff_ffv1_get_symbol(c, state, 0) + 1U; | |
| 639 | 96 | int sh = ff_ffv1_get_symbol(c, state, 0) + 1U; | |
| 640 | |||
| 641 |
4/8✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 96 times.
|
96 | if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0) |
| 642 | ✗ | return AVERROR_INVALIDDATA; | |
| 643 |
2/4✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
|
96 | if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh) |
| 644 | ✗ | return AVERROR_INVALIDDATA; | |
| 645 | |||
| 646 | 96 | sc->slice_x = sx * (int64_t)f->width / f->num_h_slices; | |
| 647 | 96 | sc->slice_y = sy * (int64_t)f->height / f->num_v_slices; | |
| 648 | 96 | sc->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - sc->slice_x; | |
| 649 | 96 | sc->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - sc->slice_y; | |
| 650 | |||
| 651 |
2/4✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
|
96 | av_assert0((unsigned)sc->slice_width <= f->width && |
| 652 | (unsigned)sc->slice_height <= f->height); | ||
| 653 |
2/4✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
|
96 | av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width |
| 654 | && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height); | ||
| 655 | } | ||
| 656 | |||
| 657 | 950 | av_refstruct_unref(&sc->plane); | |
| 658 | 950 | sc->plane = ff_ffv1_planes_alloc(); | |
| 659 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 950 times.
|
950 | if (!sc->plane) |
| 660 | ✗ | return AVERROR(ENOMEM); | |
| 661 | |||
| 662 |
2/2✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 950 times.
|
2850 | for (int i = 0; i < f->plane_count; i++) { |
| 663 | 1900 | PlaneContext *const p = &sc->plane[i]; | |
| 664 | |||
| 665 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 1708 times.
|
1900 | if (f->version == 2) { |
| 666 | 192 | int idx = ff_ffv1_get_symbol(c, state, 0); | |
| 667 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (idx >= (unsigned)f->quant_table_count) { |
| 668 | ✗ | av_log(f->avctx, AV_LOG_ERROR, | |
| 669 | "quant_table_index out of range\n"); | ||
| 670 | ✗ | return AVERROR_INVALIDDATA; | |
| 671 | } | ||
| 672 | 192 | p->quant_table_index = idx; | |
| 673 | 192 | context_count = f->context_count[idx]; | |
| 674 | } | ||
| 675 | |||
| 676 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 1660 times.
|
1900 | if (f->version <= 2) { |
| 677 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
|
240 | av_assert0(context_count >= 0); |
| 678 | 240 | p->context_count = context_count; | |
| 679 | } | ||
| 680 | } | ||
| 681 | } | ||
| 682 | 253 | return 0; | |
| 683 | } | ||
| 684 | |||
| 685 | 88 | static av_cold int decode_init(AVCodecContext *avctx) | |
| 686 | { | ||
| 687 | 88 | FFV1Context *f = avctx->priv_data; | |
| 688 | int ret; | ||
| 689 | |||
| 690 | 88 | f->pix_fmt = AV_PIX_FMT_NONE; | |
| 691 | 88 | f->configured_pix_fmt = AV_PIX_FMT_NONE; | |
| 692 | |||
| 693 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
|
88 | if ((ret = ff_ffv1_common_init(avctx, f)) < 0) |
| 694 | ✗ | return ret; | |
| 695 | |||
| 696 |
3/4✓ Branch 0 taken 80 times.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
|
88 | if (avctx->extradata_size > 0 && (ret = ff_ffv1_read_extra_header(f)) < 0) |
| 697 | ✗ | return ret; | |
| 698 | |||
| 699 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
|
88 | if ((ret = ff_ffv1_init_slice_contexts(f)) < 0) |
| 700 | ✗ | return ret; | |
| 701 | |||
| 702 | 88 | return 0; | |
| 703 | } | ||
| 704 | |||
| 705 | 7700 | static int find_next_slice(AVCodecContext *avctx, | |
| 706 | uint8_t *buf, uint8_t *buf_end, int idx, | ||
| 707 | uint8_t **pos, uint32_t *len) | ||
| 708 | { | ||
| 709 | 7700 | FFV1Context *f = avctx->priv_data; | |
| 710 | |||
| 711 | /* Length field */ | ||
| 712 | 7700 | uint32_t v = buf_end - buf; | |
| 713 |
4/4✓ Branch 0 taken 2075 times.
✓ Branch 1 taken 5625 times.
✓ Branch 2 taken 1675 times.
✓ Branch 3 taken 400 times.
|
7700 | if (idx || f->version > 2) { |
| 714 | /* Three bytes of length, plus flush bit + CRC */ | ||
| 715 |
2/2✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 600 times.
|
7300 | uint32_t trailer = 3 + 5*!!f->ec; |
| 716 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7300 times.
|
7300 | if (trailer > buf_end - buf) |
| 717 | ✗ | v = INT_MAX; | |
| 718 | else | ||
| 719 | 7300 | v = AV_RB24(buf_end - trailer) + trailer; | |
| 720 | } | ||
| 721 | |||
| 722 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
|
7700 | if (buf_end - buf < v) { |
| 723 | ✗ | av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n"); | |
| 724 | ✗ | ff_progress_frame_report(&f->picture, INT_MAX); | |
| 725 | ✗ | return AVERROR_INVALIDDATA; | |
| 726 | } | ||
| 727 | |||
| 728 | 7700 | *len = v; | |
| 729 |
2/2✓ Branch 0 taken 5625 times.
✓ Branch 1 taken 2075 times.
|
7700 | if (idx) |
| 730 | 5625 | *pos = buf_end - v; | |
| 731 | else | ||
| 732 | 2075 | *pos = buf; | |
| 733 | |||
| 734 | 7700 | return 0; | |
| 735 | } | ||
| 736 | |||
| 737 | 2120 | static int decode_header(AVCodecContext *avctx, RangeCoder *c, | |
| 738 | uint8_t *buf, size_t buf_size) | ||
| 739 | { | ||
| 740 | int ret; | ||
| 741 | 2120 | FFV1Context *f = avctx->priv_data; | |
| 742 | |||
| 743 | 2120 | uint8_t keystate = 128; | |
| 744 | 2120 | ff_init_range_decoder(c, buf, buf_size); | |
| 745 | 2120 | ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8); | |
| 746 | |||
| 747 |
2/2✓ Branch 1 taken 253 times.
✓ Branch 2 taken 1867 times.
|
2120 | if (get_rac(c, &keystate)) { |
| 748 | 253 | f->key_frame = AV_FRAME_FLAG_KEY; | |
| 749 | 253 | f->key_frame_ok = 0; | |
| 750 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
|
253 | if ((ret = read_header(f, c)) < 0) |
| 751 | ✗ | return ret; | |
| 752 | 253 | f->key_frame_ok = 1; | |
| 753 | } else { | ||
| 754 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1867 times.
|
1867 | if (!f->key_frame_ok) { |
| 755 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 756 | "Cannot decode non-keyframe without valid keyframe\n"); | ||
| 757 | ✗ | return AVERROR_INVALIDDATA; | |
| 758 | } | ||
| 759 | 1867 | f->key_frame = 0; | |
| 760 | } | ||
| 761 | |||
| 762 |
2/2✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 1098 times.
|
2120 | if (f->ac != AC_GOLOMB_RICE) { |
| 763 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
|
1022 | if (buf_size < avctx->width * avctx->height / (128*8)) |
| 764 | ✗ | return AVERROR_INVALIDDATA; | |
| 765 | } else { | ||
| 766 | 1098 | int w = avctx->width; | |
| 767 | 1098 | int s = 1 + w / (1<<23); | |
| 768 | int i; | ||
| 769 | |||
| 770 | 1098 | w /= s; | |
| 771 | |||
| 772 |
2/2✓ Branch 0 taken 25232 times.
✓ Branch 1 taken 1098 times.
|
26330 | for (i = 0; w > (1<<ff_log2_run[i]); i++) |
| 773 | 25232 | w -= ff_log2_run[i]; | |
| 774 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
|
1098 | if (buf_size < (avctx->height + i + 6) / 8 * s) |
| 775 | ✗ | return AVERROR_INVALIDDATA; | |
| 776 | } | ||
| 777 | |||
| 778 | 2120 | return 0; | |
| 779 | } | ||
| 780 | |||
| 781 | 2075 | static int decode_slices(AVCodecContext *avctx, RangeCoder c, | |
| 782 | AVPacket *avpkt) | ||
| 783 | { | ||
| 784 | 2075 | FFV1Context *f = avctx->priv_data; | |
| 785 | 2075 | AVFrame *p = f->picture.f; | |
| 786 | |||
| 787 | 2075 | uint8_t *buf = avpkt->data; | |
| 788 | 2075 | size_t buf_size = avpkt->size; | |
| 789 | 2075 | uint8_t *buf_end = buf + buf_size; | |
| 790 | |||
| 791 |
2/2✓ Branch 0 taken 7700 times.
✓ Branch 1 taken 2075 times.
|
9775 | for (int i = f->slice_count - 1; i >= 0; i--) { |
| 792 | 7700 | FFV1SliceContext *sc = &f->slices[i]; | |
| 793 | |||
| 794 | uint8_t *pos; | ||
| 795 | uint32_t len; | ||
| 796 | 7700 | int err = find_next_slice(avctx, buf, buf_end, i, | |
| 797 | &pos, &len); | ||
| 798 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
|
7700 | if (err < 0) |
| 799 | ✗ | return err; | |
| 800 | |||
| 801 | 7700 | buf_end -= len; | |
| 802 | |||
| 803 | 7700 | sc->slice_damaged = 0; | |
| 804 | |||
| 805 |
2/2✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 1000 times.
|
7700 | if (f->ec) { |
| 806 | 6700 | unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, pos, len); | |
| 807 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | if (crc != f->crcref) { |
| 808 | ✗ | int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts; | |
| 809 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc); | |
| 810 | ✗ | if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) { | |
| 811 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase)); | |
| 812 | ✗ | } else if (ts != AV_NOPTS_VALUE) { | |
| 813 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts); | |
| 814 | } else { | ||
| 815 | ✗ | av_log(f->avctx, AV_LOG_ERROR, "\n"); | |
| 816 | } | ||
| 817 | ✗ | slice_set_damaged(f, sc); | |
| 818 | } | ||
| 819 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
|
6700 | if (avctx->debug & FF_DEBUG_PICT_INFO) { |
| 820 | ✗ | av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(pos + len - 4)); | |
| 821 | } | ||
| 822 | } | ||
| 823 | |||
| 824 |
2/2✓ Branch 0 taken 5625 times.
✓ Branch 1 taken 2075 times.
|
7700 | if (i) { |
| 825 | 5625 | ff_init_range_decoder(&sc->c, pos, len); | |
| 826 | 5625 | ff_build_rac_states(&sc->c, 0.05 * (1LL << 32), 256 - 8); | |
| 827 | } else { | ||
| 828 | 2075 | sc->c = c; | |
| 829 | 2075 | sc->c.bytestream_end = pos + len; | |
| 830 | } | ||
| 831 | } | ||
| 832 | |||
| 833 | 2075 | avctx->execute(avctx, | |
| 834 | decode_slice, | ||
| 835 | 2075 | f->slices, | |
| 836 | NULL, | ||
| 837 | f->slice_count, | ||
| 838 | sizeof(*f->slices)); | ||
| 839 | |||
| 840 |
2/2✓ Branch 0 taken 7700 times.
✓ Branch 1 taken 2075 times.
|
9775 | for (int i = f->slice_count - 1; i >= 0; i--) { |
| 841 | 7700 | FFV1SliceContext *sc = &f->slices[i]; | |
| 842 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7700 | if (sc->slice_damaged && f->last_picture.f) { |
| 843 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(f->pix_fmt); | |
| 844 | const uint8_t *src[4]; | ||
| 845 | uint8_t *dst[4]; | ||
| 846 | ✗ | ff_progress_frame_await(&f->last_picture, INT_MAX); | |
| 847 | ✗ | for (int j = 0; j < desc->nb_components; j++) { | |
| 848 | ✗ | int pixshift = desc->comp[j].depth > 8; | |
| 849 | ✗ | int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0; | |
| 850 | ✗ | int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0; | |
| 851 | ✗ | dst[j] = p->data[j] + p->linesize[j] * | |
| 852 | ✗ | (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift); | |
| 853 | ✗ | src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] * | |
| 854 | ✗ | (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift); | |
| 855 | |||
| 856 | } | ||
| 857 | |||
| 858 | ✗ | av_image_copy(dst, p->linesize, src, | |
| 859 | ✗ | f->last_picture.f->linesize, | |
| 860 | f->pix_fmt, | ||
| 861 | sc->slice_width, | ||
| 862 | sc->slice_height); | ||
| 863 | |||
| 864 | ✗ | f->slice_damaged[i] = 1; | |
| 865 | } | ||
| 866 | } | ||
| 867 | |||
| 868 | 2075 | return 0; | |
| 869 | } | ||
| 870 | |||
| 871 | 2120 | static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
| 872 | int *got_frame, AVPacket *avpkt) | ||
| 873 | { | ||
| 874 | 2120 | FFV1Context *f = avctx->priv_data; | |
| 875 | int ret; | ||
| 876 | AVFrame *p; | ||
| 877 | 2120 | const FFHWAccel *hwaccel = NULL; | |
| 878 | |||
| 879 | /* This is copied onto the first slice's range coder context */ | ||
| 880 | RangeCoder c; | ||
| 881 | |||
| 882 | 2120 | ff_progress_frame_unref(&f->last_picture); | |
| 883 | 2120 | av_refstruct_unref(&f->hwaccel_last_picture_private); | |
| 884 | |||
| 885 | 2120 | FFSWAP(ProgressFrame, f->picture, f->last_picture); | |
| 886 | 2120 | FFSWAP(void *, f->hwaccel_picture_private, f->hwaccel_last_picture_private); | |
| 887 | |||
| 888 | 2120 | f->avctx = avctx; | |
| 889 | 2120 | f->frame_damaged = 0; | |
| 890 | |||
| 891 | 2120 | ret = decode_header(avctx, &c, avpkt->data, avpkt->size); | |
| 892 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
|
2120 | if (ret < 0) |
| 893 | ✗ | return ret; | |
| 894 | |||
| 895 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
|
2120 | if (avctx->debug & FF_DEBUG_PICT_INFO) |
| 896 | ✗ | av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n", | |
| 897 | ✗ | f->version, !!f->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample); | |
| 898 | |||
| 899 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2075 times.
|
2120 | if (avctx->skip_frame >= AVDISCARD_ALL) |
| 900 | 45 | return avpkt->size; | |
| 901 | |||
| 902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (avctx->hwaccel) |
| 903 | ✗ | hwaccel = ffhwaccel(avctx->hwaccel); | |
| 904 | |||
| 905 | 2075 | ret = ff_progress_frame_get_buffer(avctx, &f->picture, | |
| 906 | AV_GET_BUFFER_FLAG_REF); | ||
| 907 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (ret < 0) |
| 908 | ✗ | return ret; | |
| 909 | |||
| 910 | 2075 | ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private); | |
| 911 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (ret < 0) |
| 912 | ✗ | return ret; | |
| 913 | |||
| 914 | 2075 | p = f->picture.f; | |
| 915 | |||
| 916 | 2075 | p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P | |
| 917 | 2075 | p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | f->key_frame; | |
| 918 | |||
| 919 |
3/4✓ Branch 0 taken 400 times.
✓ Branch 1 taken 1675 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 400 times.
|
2075 | if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) { |
| 920 | /* we have interlaced material flagged in container */ | ||
| 921 | ✗ | p->flags |= AV_FRAME_FLAG_INTERLACED; | |
| 922 | ✗ | if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB) | |
| 923 | ✗ | p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
| 924 | } | ||
| 925 | |||
| 926 | /* Start */ | ||
| 927 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (hwaccel) { |
| 928 | ✗ | ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size); | |
| 929 | ✗ | if (ret < 0) | |
| 930 | ✗ | return ret; | |
| 931 | } | ||
| 932 | |||
| 933 | 2075 | ff_thread_finish_setup(avctx); | |
| 934 | |||
| 935 | /* Decode slices */ | ||
| 936 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (hwaccel) { |
| 937 | ✗ | uint8_t *buf_end = avpkt->data + avpkt->size; | |
| 938 | |||
| 939 | ✗ | if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f) | |
| 940 | ✗ | ff_progress_frame_await(&f->last_picture, f->slice_count - 1); | |
| 941 | |||
| 942 | ✗ | for (int i = f->slice_count - 1; i >= 0; i--) { | |
| 943 | uint8_t *pos; | ||
| 944 | uint32_t len; | ||
| 945 | ✗ | ret = find_next_slice(avctx, avpkt->data, buf_end, i, | |
| 946 | &pos, &len); | ||
| 947 | ✗ | if (ret < 0) | |
| 948 | ✗ | return ret; | |
| 949 | |||
| 950 | ✗ | buf_end -= len; | |
| 951 | |||
| 952 | ✗ | ret = hwaccel->decode_slice(avctx, pos, len); | |
| 953 | ✗ | if (ret < 0) | |
| 954 | ✗ | return ret; | |
| 955 | } | ||
| 956 | } else { | ||
| 957 | 2075 | ret = decode_slices(avctx, c, avpkt); | |
| 958 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (ret < 0) |
| 959 | ✗ | return ret; | |
| 960 | } | ||
| 961 | |||
| 962 | /* Finalize */ | ||
| 963 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
|
2075 | if (hwaccel) { |
| 964 | ✗ | ret = hwaccel->end_frame(avctx); | |
| 965 | ✗ | if (ret < 0) | |
| 966 | ✗ | return ret; | |
| 967 | } | ||
| 968 | |||
| 969 | 2075 | ff_progress_frame_report(&f->picture, INT_MAX); | |
| 970 | |||
| 971 | 2075 | ff_progress_frame_unref(&f->last_picture); | |
| 972 | 2075 | av_refstruct_unref(&f->hwaccel_last_picture_private); | |
| 973 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2075 times.
|
2075 | if ((ret = av_frame_ref(rframe, f->picture.f)) < 0) |
| 974 | ✗ | return ret; | |
| 975 | |||
| 976 | 2075 | *got_frame = 1; | |
| 977 | |||
| 978 | 2075 | return avpkt->size; | |
| 979 | } | ||
| 980 | |||
| 981 | #if HAVE_THREADS | ||
| 982 | ✗ | static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) | |
| 983 | { | ||
| 984 | ✗ | FFV1Context *fsrc = src->priv_data; | |
| 985 | ✗ | FFV1Context *fdst = dst->priv_data; | |
| 986 | |||
| 987 | ✗ | if (dst == src) | |
| 988 | ✗ | return 0; | |
| 989 | |||
| 990 | ✗ | fdst->version = fsrc->version; | |
| 991 | ✗ | fdst->micro_version = fsrc->micro_version; | |
| 992 | ✗ | fdst->combined_version = fsrc->combined_version; | |
| 993 | ✗ | fdst->chroma_planes = fsrc->chroma_planes; | |
| 994 | ✗ | fdst->chroma_h_shift = fsrc->chroma_h_shift; | |
| 995 | ✗ | fdst->chroma_v_shift = fsrc->chroma_v_shift; | |
| 996 | ✗ | fdst->transparency = fsrc->transparency; | |
| 997 | ✗ | fdst->plane_count = fsrc->plane_count; | |
| 998 | ✗ | fdst->ac = fsrc->ac; | |
| 999 | ✗ | fdst->colorspace = fsrc->colorspace; | |
| 1000 | ✗ | fdst->pix_fmt = fsrc->pix_fmt; | |
| 1001 | ✗ | fdst->configured_pix_fmt = fsrc->configured_pix_fmt; | |
| 1002 | ✗ | fdst->configured_ac = fsrc->configured_ac; | |
| 1003 | ✗ | fdst->configured_width = fsrc->configured_width; | |
| 1004 | ✗ | fdst->configured_height = fsrc->configured_height; | |
| 1005 | |||
| 1006 | ✗ | fdst->ec = fsrc->ec; | |
| 1007 | ✗ | fdst->intra = fsrc->intra; | |
| 1008 | ✗ | fdst->key_frame_ok = fsrc->key_frame_ok; | |
| 1009 | |||
| 1010 | ✗ | fdst->packed_at_lsb = fsrc->packed_at_lsb; | |
| 1011 | ✗ | fdst->slice_count = fsrc->slice_count; | |
| 1012 | ✗ | fdst->use32bit = fsrc->use32bit; | |
| 1013 | ✗ | memcpy(fdst->state_transition, fsrc->state_transition, | |
| 1014 | sizeof(fdst->state_transition)); | ||
| 1015 | |||
| 1016 | // in version 1 there is a single per-keyframe quant table, so | ||
| 1017 | // we need to propagate it between threads | ||
| 1018 | ✗ | if (fsrc->version < 2) | |
| 1019 | ✗ | memcpy(fdst->quant_tables[0], fsrc->quant_tables[0], sizeof(fsrc->quant_tables[0])); | |
| 1020 | |||
| 1021 | ✗ | for (int i = 0; i < fdst->num_h_slices * fdst->num_v_slices; i++) { | |
| 1022 | ✗ | FFV1SliceContext *sc = &fdst->slices[i]; | |
| 1023 | ✗ | const FFV1SliceContext *sc0 = &fsrc->slices[i]; | |
| 1024 | |||
| 1025 | ✗ | av_refstruct_replace(&sc->plane, sc0->plane); | |
| 1026 | |||
| 1027 | ✗ | if (fsrc->version < 3) { | |
| 1028 | ✗ | sc->slice_x = sc0->slice_x; | |
| 1029 | ✗ | sc->slice_y = sc0->slice_y; | |
| 1030 | ✗ | sc->slice_width = sc0->slice_width; | |
| 1031 | ✗ | sc->slice_height = sc0->slice_height; | |
| 1032 | } | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | ✗ | av_refstruct_replace(&fdst->slice_damaged, fsrc->slice_damaged); | |
| 1036 | |||
| 1037 | av_assert1(fdst->max_slice_count == fsrc->max_slice_count); | ||
| 1038 | |||
| 1039 | ✗ | ff_progress_frame_replace(&fdst->picture, &fsrc->picture); | |
| 1040 | ✗ | av_refstruct_replace(&fdst->hwaccel_picture_private, | |
| 1041 | ✗ | fsrc->hwaccel_picture_private); | |
| 1042 | |||
| 1043 | ✗ | return 0; | |
| 1044 | } | ||
| 1045 | #endif | ||
| 1046 | |||
| 1047 | 88 | static av_cold int ffv1_decode_close(AVCodecContext *avctx) | |
| 1048 | { | ||
| 1049 | 88 | FFV1Context *const s = avctx->priv_data; | |
| 1050 | |||
| 1051 | 88 | ff_progress_frame_unref(&s->picture); | |
| 1052 | 88 | av_refstruct_unref(&s->hwaccel_picture_private); | |
| 1053 | |||
| 1054 | 88 | ff_progress_frame_unref(&s->last_picture); | |
| 1055 | 88 | av_refstruct_unref(&s->hwaccel_last_picture_private); | |
| 1056 | |||
| 1057 | 88 | ff_ffv1_close(s); | |
| 1058 | |||
| 1059 | 88 | return 0; | |
| 1060 | } | ||
| 1061 | |||
| 1062 | const FFCodec ff_ffv1_decoder = { | ||
| 1063 | .p.name = "ffv1", | ||
| 1064 | CODEC_LONG_NAME("FFmpeg video codec #1"), | ||
| 1065 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 1066 | .p.id = AV_CODEC_ID_FFV1, | ||
| 1067 | .priv_data_size = sizeof(FFV1Context), | ||
| 1068 | .init = decode_init, | ||
| 1069 | .close = ffv1_decode_close, | ||
| 1070 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 1071 | UPDATE_THREAD_CONTEXT(update_thread_context), | ||
| 1072 | .p.capabilities = AV_CODEC_CAP_DR1 | | ||
| 1073 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS, | ||
| 1074 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
| 1075 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | | ||
| 1076 | FF_CODEC_CAP_USES_PROGRESSFRAMES, | ||
| 1077 | .hw_configs = (const AVCodecHWConfigInternal *const []) { | ||
| 1078 | #if CONFIG_FFV1_VULKAN_HWACCEL | ||
| 1079 | HWACCEL_VULKAN(ffv1), | ||
| 1080 | #endif | ||
| 1081 | NULL | ||
| 1082 | }, | ||
| 1083 | }; | ||
| 1084 |