aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / ffv1dec.c
blob07e66b9dbbea498d2e94a9c7676abc6467b501a2
1 /*
2 * FFV1 decoder
4 * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /**
24 * @file
25 * FF Video Codec 1 (a lossless codec) decoder
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/timer.h"
35 #include "avcodec.h"
36 #include "bitstream.h"
37 #include "golomb.h"
38 #include "internal.h"
39 #include "put_bits.h"
40 #include "rangecoder.h"
41 #include "mathops.h"
42 #include "ffv1.h"
44 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
45 int is_signed)
47 if (get_rac(c, state + 0))
48 return 0;
49 else {
50 int i, e, a;
51 e = 0;
52 while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
53 e++;
55 a = 1;
56 for (i = e - 1; i >= 0; i--)
57 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
59 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
60 return (a ^ e) - e;
64 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
66 return get_symbol_inline(c, state, is_signed);
69 static inline int get_vlc_symbol(BitstreamContext *bc, VlcState *const state,
70 int bits)
72 int k, i, v, ret;
74 i = state->count;
75 k = 0;
76 while (i < state->error_sum) { // FIXME: optimize
77 k++;
78 i += i;
81 assert(k <= 8);
83 v = get_sr_golomb(bc, k, 12, bits);
84 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
85 v, state->bias, state->error_sum, state->drift, state->count, k);
87 v ^= ((2 * state->drift + state->count) >> 31);
89 ret = fold(v + state->bias, bits);
91 update_vlc_state(state, v);
93 return ret;
96 static av_always_inline void decode_line(FFV1Context *s, int w,
97 int16_t *sample[2],
98 int plane_index, int bits)
100 PlaneContext *const p = &s->plane[plane_index];
101 RangeCoder *const c = &s->c;
102 int x;
103 int run_count = 0;
104 int run_mode = 0;
105 int run_index = s->run_index;
107 for (x = 0; x < w; x++) {
108 int diff, context, sign;
110 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
111 if (context < 0) {
112 context = -context;
113 sign = 1;
114 } else
115 sign = 0;
117 av_assert2(context < p->context_count);
119 if (s->ac != AC_GOLOMB_RICE) {
120 diff = get_symbol_inline(c, p->state[context], 1);
121 } else {
122 if (context == 0 && run_mode == 0)
123 run_mode = 1;
125 if (run_mode) {
126 if (run_count == 0 && run_mode == 1) {
127 if (bitstream_read_bit(&s->bc)) {
128 run_count = 1 << ff_log2_run[run_index];
129 if (x + run_count <= w)
130 run_index++;
131 } else {
132 if (ff_log2_run[run_index])
133 run_count = bitstream_read(&s->bc, ff_log2_run[run_index]);
134 else
135 run_count = 0;
136 if (run_index)
137 run_index--;
138 run_mode = 2;
141 run_count--;
142 if (run_count < 0) {
143 run_mode = 0;
144 run_count = 0;
145 diff = get_vlc_symbol(&s->bc, &p->vlc_state[context],
146 bits);
147 if (diff >= 0)
148 diff++;
149 } else
150 diff = 0;
151 } else
152 diff = get_vlc_symbol(&s->bc, &p->vlc_state[context], bits);
154 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
155 run_count, run_index, run_mode, x, bitstream_tell(&s->bc));
158 if (sign)
159 diff = -diff;
161 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
162 ((1 << bits) - 1);
164 s->run_index = run_index;
167 static void decode_plane(FFV1Context *s, uint8_t *src,
168 int w, int h, int stride, int plane_index)
170 int x, y;
171 int16_t *sample[2];
172 sample[0] = s->sample_buffer + 3;
173 sample[1] = s->sample_buffer + w + 6 + 3;
175 s->run_index = 0;
177 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
179 for (y = 0; y < h; y++) {
180 int16_t *temp = sample[0]; // FIXME: try a normal buffer
182 sample[0] = sample[1];
183 sample[1] = temp;
185 sample[1][-1] = sample[0][0];
186 sample[0][w] = sample[0][w - 1];
188 // { START_TIMER
189 if (s->avctx->bits_per_raw_sample <= 8) {
190 decode_line(s, w, sample, plane_index, 8);
191 for (x = 0; x < w; x++)
192 src[x + stride * y] = sample[1][x];
193 } else {
194 decode_line(s, w, sample, plane_index,
195 s->avctx->bits_per_raw_sample);
196 if (s->packed_at_lsb) {
197 for (x = 0; x < w; x++)
198 ((uint16_t *)(src + stride * y))[x] = sample[1][x];
199 } else {
200 for (x = 0; x < w; x++)
201 ((uint16_t *)(src + stride * y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
204 // STOP_TIMER("decode-line") }
208 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
209 int stride[3])
211 int x, y, p;
212 int16_t *sample[4][2];
213 int lbd = s->avctx->bits_per_raw_sample <= 8;
214 int bits = s->avctx->bits_per_raw_sample > 0
215 ? s->avctx->bits_per_raw_sample
216 : 8;
217 int offset = 1 << bits;
219 for (x = 0; x < 4; x++) {
220 sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
221 sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
224 s->run_index = 0;
226 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
228 for (y = 0; y < h; y++) {
229 for (p = 0; p < 3 + s->transparency; p++) {
230 int16_t *temp = sample[p][0]; //FIXME try a normal buffer
232 sample[p][0] = sample[p][1];
233 sample[p][1] = temp;
235 sample[p][1][-1] = sample[p][0][0];
236 sample[p][0][w] = sample[p][0][w - 1];
237 if (lbd)
238 decode_line(s, w, sample[p], (p + 1) / 2, 9);
239 else
240 decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
242 for (x = 0; x < w; x++) {
243 int g = sample[0][1][x];
244 int b = sample[1][1][x];
245 int r = sample[2][1][x];
246 int a = sample[3][1][x];
248 b -= offset;
249 r -= offset;
250 g -= (b + r) >> 2;
251 b += g;
252 r += g;
254 if (lbd)
255 *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
256 (g << 8) + (r << 16) + (a << 24);
257 else {
258 *((uint16_t *)(src[0] + x * 2 + stride[0] * y)) = b;
259 *((uint16_t *)(src[1] + x * 2 + stride[1] * y)) = g;
260 *((uint16_t *)(src[2] + x * 2 + stride[2] * y)) = r;
266 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
268 RangeCoder *c = &fs->c;
269 uint8_t state[CONTEXT_SIZE];
270 unsigned ps, i, context_count;
271 memset(state, 128, sizeof(state));
273 if (fs->ac == AC_RANGE_CUSTOM_TAB) {
274 for (i = 1; i < 256; i++) {
275 fs->c.one_state[i] = f->state_transition[i];
276 fs->c.zero_state[256 - i] = 256 - fs->c.one_state[i];
280 fs->slice_x = get_symbol(c, state, 0) * f->width;
281 fs->slice_y = get_symbol(c, state, 0) * f->height;
282 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
283 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
285 fs->slice_x /= f->num_h_slices;
286 fs->slice_y /= f->num_v_slices;
287 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
288 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
289 if ((unsigned)fs->slice_width > f->width ||
290 (unsigned)fs->slice_height > f->height)
291 return AVERROR_INVALIDDATA;
292 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
293 (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
294 return AVERROR_INVALIDDATA;
296 for (i = 0; i < f->plane_count; i++) {
297 PlaneContext *const p = &fs->plane[i];
298 int idx = get_symbol(c, state, 0);
299 if (idx > (unsigned)f->quant_table_count) {
300 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
301 return AVERROR_INVALIDDATA;
303 p->quant_table_index = idx;
304 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
305 context_count = f->context_count[idx];
307 if (p->context_count < context_count) {
308 av_freep(&p->state);
309 av_freep(&p->vlc_state);
311 p->context_count = context_count;
314 ps = get_symbol(c, state, 0);
315 if (ps == 1) {
316 f->cur->interlaced_frame = 1;
317 f->cur->top_field_first = 1;
318 } else if (ps == 2) {
319 f->cur->interlaced_frame = 1;
320 f->cur->top_field_first = 0;
321 } else if (ps == 3) {
322 f->cur->interlaced_frame = 0;
324 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
325 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
327 if (av_image_check_sar(f->width, f->height,
328 f->cur->sample_aspect_ratio) < 0) {
329 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
330 f->cur->sample_aspect_ratio.num,
331 f->cur->sample_aspect_ratio.den);
332 f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
335 return 0;
338 static int decode_slice(AVCodecContext *c, void *arg)
340 FFV1Context *fs = *(void **)arg;
341 FFV1Context *f = fs->avctx->priv_data;
342 int width, height, x, y, ret;
343 const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)
344 ? (c->bits_per_raw_sample > 8) + 1
345 : 4;
346 AVFrame *const p = f->cur;
348 if (f->version > 2) {
349 if (decode_slice_header(f, fs) < 0) {
350 fs->slice_damaged = 1;
351 return AVERROR_INVALIDDATA;
354 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
355 return ret;
356 if (f->cur->key_frame)
357 ffv1_clear_slice_state(f, fs);
358 width = fs->slice_width;
359 height = fs->slice_height;
360 x = fs->slice_x;
361 y = fs->slice_y;
363 if (fs->ac == AC_GOLOMB_RICE) {
364 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
365 get_rac(&fs->c, (uint8_t[]) { 129 });
366 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
367 bitstream_init8(&fs->bc, fs->c.bytestream_start + fs->ac_byte_count,
368 (fs->c.bytestream_end - fs->c.bytestream_start -
369 fs->ac_byte_count));
372 av_assert1(width && height);
373 if (f->colorspace == 0) {
374 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
375 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
376 const int cx = x >> f->chroma_h_shift;
377 const int cy = y >> f->chroma_v_shift;
378 decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
379 height, p->linesize[0],
382 if (f->chroma_planes) {
383 decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
384 chroma_width, chroma_height, p->linesize[1],
386 decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
387 chroma_width, chroma_height, p->linesize[2],
390 if (fs->transparency)
391 decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
392 height, p->linesize[3],
394 } else {
395 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
396 p->data[1] + ps * x + y * p->linesize[1],
397 p->data[2] + ps * x + y * p->linesize[2] };
398 decode_rgb_frame(fs, planes, width, height, p->linesize);
400 if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
401 int v;
402 get_rac(&fs->c, (uint8_t[]) { 129 });
403 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
404 if (v) {
405 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
407 fs->slice_damaged = 1;
411 emms_c();
413 return 0;
416 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
418 int v;
419 int i = 0;
420 uint8_t state[CONTEXT_SIZE];
422 memset(state, 128, sizeof(state));
424 for (v = 0; i < 128; v++) {
425 unsigned len = get_symbol(c, state, 0) + 1;
427 if (len > 128 - i)
428 return -1;
430 while (len--) {
431 quant_table[i] = scale * v;
432 i++;
436 for (i = 1; i < 128; i++)
437 quant_table[256 - i] = -quant_table[i];
438 quant_table[128] = -quant_table[127];
440 return 2 * v - 1;
443 static int read_quant_tables(RangeCoder *c,
444 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
446 int i;
447 int context_count = 1;
449 for (i = 0; i < 5; i++) {
450 context_count *= read_quant_table(c, quant_table[i], context_count);
451 if (context_count > 32768U) {
452 return -1;
455 return (context_count + 1) / 2;
458 static int read_extra_header(FFV1Context *f)
460 RangeCoder *const c = &f->c;
461 uint8_t state[CONTEXT_SIZE];
462 int i, j, k, ret;
463 uint8_t state2[32][CONTEXT_SIZE];
465 memset(state2, 128, sizeof(state2));
466 memset(state, 128, sizeof(state));
468 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
469 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
471 f->version = get_symbol(c, state, 0);
472 if (f->version > 2) {
473 c->bytestream_end -= 4;
474 f->minor_version = get_symbol(c, state, 0);
476 f->ac = get_symbol(c, state, 0);
478 if (f->ac == AC_RANGE_CUSTOM_TAB) {
479 for (i = 1; i < 256; i++)
480 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
483 f->colorspace = get_symbol(c, state, 0); //YUV cs type
484 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
485 f->chroma_planes = get_rac(c, state);
486 f->chroma_h_shift = get_symbol(c, state, 0);
487 f->chroma_v_shift = get_symbol(c, state, 0);
488 f->transparency = get_rac(c, state);
489 f->plane_count = 2 + f->transparency;
490 f->num_h_slices = 1 + get_symbol(c, state, 0);
491 f->num_v_slices = 1 + get_symbol(c, state, 0);
493 if (f->num_h_slices > (unsigned)f->width ||
494 f->num_v_slices > (unsigned)f->height) {
495 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
496 return AVERROR_INVALIDDATA;
499 f->quant_table_count = get_symbol(c, state, 0);
500 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
501 return AVERROR_INVALIDDATA;
502 for (i = 0; i < f->quant_table_count; i++) {
503 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
504 if (f->context_count[i] < 0) {
505 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
506 return AVERROR_INVALIDDATA;
509 if ((ret = ffv1_allocate_initial_states(f)) < 0)
510 return ret;
512 for (i = 0; i < f->quant_table_count; i++)
513 if (get_rac(c, state)) {
514 for (j = 0; j < f->context_count[i]; j++)
515 for (k = 0; k < CONTEXT_SIZE; k++) {
516 int pred = j ? f->initial_states[i][j - 1][k] : 128;
517 f->initial_states[i][j][k] =
518 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
522 if (f->version > 2) {
523 f->ec = get_symbol(c, state, 0);
526 if (f->version > 2) {
527 unsigned v;
528 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
529 f->avctx->extradata, f->avctx->extradata_size);
530 if (v) {
531 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
532 return AVERROR_INVALIDDATA;
536 av_log(f->avctx, AV_LOG_VERBOSE,
537 "FFV1 version %d.%d colorspace %d - %d bits - %d/%d planes, %s transparent - tile geometry %dx%d - %s\n",
538 f->version, f->minor_version, f->colorspace, f->avctx->bits_per_raw_sample,
539 f->plane_count, f->chroma_planes, f->transparency ? "" : "not",
540 f->num_h_slices, f->num_v_slices,
541 f->ec ? "per-slice crc" : "no crc");
543 return 0;
547 static int read_header(FFV1Context *f)
549 uint8_t state[CONTEXT_SIZE];
550 int i, j, context_count = -1;
551 RangeCoder *const c = &f->slice_context[0]->c;
553 memset(state, 128, sizeof(state));
555 if (f->version < 2) {
556 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
557 unsigned v = get_symbol(c, state, 0);
558 if (v > 1) {
559 av_log(f->avctx, AV_LOG_ERROR,
560 "invalid version %d in version 1 header\n", v);
561 return AVERROR_INVALIDDATA;
563 f->version = v;
565 f->ac = get_symbol(c, state, 0);
567 if (f->ac == AC_RANGE_CUSTOM_TAB) {
568 for (i = 1; i < 256; i++)
569 f->state_transition[i] =
570 get_symbol(c, state, 1) + c->one_state[i];
573 colorspace = get_symbol(c, state, 0); //YUV cs type
574 bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
575 chroma_planes = get_rac(c, state);
576 chroma_h_shift = get_symbol(c, state, 0);
577 chroma_v_shift = get_symbol(c, state, 0);
578 transparency = get_rac(c, state);
580 if (f->plane_count) {
581 if (colorspace != f->colorspace ||
582 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
583 chroma_planes != f->chroma_planes ||
584 chroma_h_shift != f->chroma_h_shift ||
585 chroma_v_shift != f->chroma_v_shift ||
586 transparency != f->transparency) {
587 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
588 return AVERROR_INVALIDDATA;
592 f->colorspace = colorspace;
593 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
594 f->chroma_planes = chroma_planes;
595 f->chroma_h_shift = chroma_h_shift;
596 f->chroma_v_shift = chroma_v_shift;
597 f->transparency = transparency;
599 f->plane_count = 2 + f->transparency;
602 if (f->colorspace == 0) {
603 if (f->transparency && f->avctx->bits_per_raw_sample > 8) {
604 av_log(f->avctx, AV_LOG_ERROR,
605 "Transparency not supported for bit depth %d\n",
606 f->avctx->bits_per_raw_sample);
607 return AVERROR(ENOSYS);
609 if (!f->transparency && !f->chroma_planes) {
610 if (f->avctx->bits_per_raw_sample <= 8)
611 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
612 else
613 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
614 } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
615 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
616 case 0x00:
617 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
618 break;
619 case 0x01:
620 f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
621 break;
622 case 0x10:
623 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
624 break;
625 case 0x11:
626 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
627 break;
628 case 0x20:
629 f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
630 break;
631 case 0x22:
632 f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
633 break;
634 default:
635 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
636 return AVERROR(ENOSYS);
638 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
639 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
640 case 0x00:
641 f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
642 break;
643 case 0x10:
644 f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
645 break;
646 case 0x11:
647 f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
648 break;
649 default:
650 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
651 return AVERROR(ENOSYS);
653 } else if (f->avctx->bits_per_raw_sample == 9) {
654 f->packed_at_lsb = 1;
655 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
656 case 0x00:
657 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
658 break;
659 case 0x10:
660 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
661 break;
662 case 0x11:
663 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
664 break;
665 default:
666 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
667 return AVERROR(ENOSYS);
669 } else if (f->avctx->bits_per_raw_sample == 10) {
670 f->packed_at_lsb = 1;
671 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
672 case 0x00:
673 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
674 break;
675 case 0x10:
676 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
677 break;
678 case 0x11:
679 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
680 break;
681 default:
682 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
683 return AVERROR(ENOSYS);
685 } else {
686 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
687 case 0x00:
688 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
689 break;
690 case 0x10:
691 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
692 break;
693 case 0x11:
694 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
695 break;
696 default:
697 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
698 return AVERROR(ENOSYS);
701 } else if (f->colorspace == 1) {
702 if (f->chroma_h_shift || f->chroma_v_shift) {
703 av_log(f->avctx, AV_LOG_ERROR,
704 "chroma subsampling not supported in this colorspace\n");
705 return AVERROR(ENOSYS);
707 if (f->transparency) {
708 av_log(f->avctx, AV_LOG_ERROR,
709 "Transparency not supported in this colorspace\n");
710 return AVERROR(ENOSYS);
712 switch (f->avctx->bits_per_raw_sample) {
713 case 0:
714 case 8:
715 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
716 break;
717 case 9:
718 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
719 break;
720 case 10:
721 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
722 break;
723 default:
724 av_log(f->avctx, AV_LOG_ERROR,
725 "bit depth %d not supported\n",
726 f->avctx->bits_per_raw_sample);
727 return AVERROR(ENOSYS);
729 } else {
730 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
731 return AVERROR(ENOSYS);
734 ff_dlog(f->avctx, "%d %d %d\n",
735 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
736 if (f->version < 2) {
737 context_count = read_quant_tables(c, f->quant_table);
738 if (context_count < 0) {
739 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
740 return AVERROR_INVALIDDATA;
742 } else if (f->version < 3) {
743 f->slice_count = get_symbol(c, state, 0);
744 } else {
745 const uint8_t *p = c->bytestream_end;
746 for (f->slice_count = 0;
747 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
748 f->slice_count++) {
749 int trailer = 3 + 5 * !!f->ec;
750 int size = AV_RB24(p - trailer);
751 if (size + trailer > p - c->bytestream_start)
752 break;
753 p -= size + trailer;
756 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
757 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
758 f->slice_count);
759 return AVERROR_INVALIDDATA;
762 for (j = 0; j < f->slice_count; j++) {
763 FFV1Context *fs = f->slice_context[j];
764 fs->ac = f->ac;
765 fs->packed_at_lsb = f->packed_at_lsb;
767 fs->slice_damaged = 0;
769 if (f->version == 2) {
770 fs->slice_x = get_symbol(c, state, 0) * f->width;
771 fs->slice_y = get_symbol(c, state, 0) * f->height;
772 fs->slice_width =
773 (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
774 fs->slice_height =
775 (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
777 fs->slice_x /= f->num_h_slices;
778 fs->slice_y /= f->num_v_slices;
779 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
780 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
781 if ((unsigned)fs->slice_width > f->width ||
782 (unsigned)fs->slice_height > f->height)
783 return AVERROR_INVALIDDATA;
784 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
785 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
786 f->height)
787 return AVERROR_INVALIDDATA;
790 for (i = 0; i < f->plane_count; i++) {
791 PlaneContext *const p = &fs->plane[i];
793 if (f->version == 2) {
794 int idx = get_symbol(c, state, 0);
795 if (idx > (unsigned)f->quant_table_count) {
796 av_log(f->avctx, AV_LOG_ERROR,
797 "quant_table_index out of range\n");
798 return AVERROR_INVALIDDATA;
800 p->quant_table_index = idx;
801 memcpy(p->quant_table, f->quant_tables[idx],
802 sizeof(p->quant_table));
803 context_count = f->context_count[idx];
804 } else {
805 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
808 if (f->version <= 2) {
809 av_assert0(context_count >= 0);
810 if (p->context_count < context_count) {
811 av_freep(&p->state);
812 av_freep(&p->vlc_state);
814 p->context_count = context_count;
818 return 0;
821 static av_cold int ffv1_decode_init(AVCodecContext *avctx)
823 FFV1Context *f = avctx->priv_data;
824 int ret;
826 ffv1_common_init(avctx);
828 f->last_picture = av_frame_alloc();
829 if (!f->last_picture)
830 return AVERROR(ENOMEM);
832 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
833 return ret;
835 if ((ret = ffv1_init_slice_contexts(f)) < 0)
836 return ret;
838 return 0;
841 static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
842 int *got_frame, AVPacket *avpkt)
844 uint8_t *buf = avpkt->data;
845 int buf_size = avpkt->size;
846 FFV1Context *f = avctx->priv_data;
847 RangeCoder *const c = &f->slice_context[0]->c;
848 int i, ret;
849 uint8_t keystate = 128;
850 uint8_t *buf_p;
851 AVFrame *const p = data;
853 f->cur = p;
855 ff_init_range_decoder(c, buf, buf_size);
856 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
858 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
859 if (get_rac(c, &keystate)) {
860 p->key_frame = 1;
861 f->key_frame_ok = 0;
862 if ((ret = read_header(f)) < 0)
863 return ret;
864 f->key_frame_ok = 1;
865 } else {
866 if (!f->key_frame_ok) {
867 av_log(avctx, AV_LOG_ERROR,
868 "Cannot decode non-keyframe without valid keyframe\n");
869 return AVERROR_INVALIDDATA;
871 p->key_frame = 0;
874 if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0) {
875 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
876 return ret;
879 if (avctx->debug & FF_DEBUG_PICT_INFO)
880 av_log(avctx, AV_LOG_DEBUG,
881 "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
882 f->version, p->key_frame, f->ac, f->ec, f->slice_count,
883 f->avctx->bits_per_raw_sample);
885 buf_p = buf + buf_size;
886 for (i = f->slice_count - 1; i >= 0; i--) {
887 FFV1Context *fs = f->slice_context[i];
888 int trailer = 3 + 5 * !!f->ec;
889 int v;
891 if (i || f->version > 2)
892 v = AV_RB24(buf_p - trailer) + trailer;
893 else
894 v = buf_p - c->bytestream_start;
895 if (buf_p - c->bytestream_start < v) {
896 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
897 return AVERROR_INVALIDDATA;
899 buf_p -= v;
901 if (f->ec) {
902 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
903 if (crc) {
904 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
905 fs->slice_damaged = 1;
909 if (i) {
910 ff_init_range_decoder(&fs->c, buf_p, v);
911 } else
912 fs->c.bytestream_end = buf_p + v;
914 fs->cur = p;
917 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
918 f->slice_count,
919 sizeof(void *));
921 for (i = f->slice_count - 1; i >= 0; i--) {
922 FFV1Context *fs = f->slice_context[i];
923 int j;
924 if (fs->slice_damaged && f->last_picture->data[0]) {
925 const uint8_t *src[4];
926 uint8_t *dst[4];
927 for (j = 0; j < 4; j++) {
928 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
929 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
930 dst[j] = p->data[j] + p->linesize[j] *
931 (fs->slice_y >> sv) + (fs->slice_x >> sh);
932 src[j] = f->last_picture->data[j] +
933 f->last_picture->linesize[j] *
934 (fs->slice_y >> sv) + (fs->slice_x >> sh);
936 av_image_copy(dst, p->linesize, src,
937 f->last_picture->linesize,
938 avctx->pix_fmt, fs->slice_width,
939 fs->slice_height);
943 f->picture_number++;
945 av_frame_unref(f->last_picture);
946 if ((ret = av_frame_ref(f->last_picture, p)) < 0)
947 return ret;
948 f->cur = NULL;
950 *got_frame = 1;
952 return buf_size;
955 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
957 FFV1Context *s = avctx->priv_data;;
959 av_frame_free(&s->last_picture);
961 ffv1_close(avctx);
963 return 0;
966 AVCodec ff_ffv1_decoder = {
967 .name = "ffv1",
968 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
969 .type = AVMEDIA_TYPE_VIDEO,
970 .id = AV_CODEC_ID_FFV1,
971 .priv_data_size = sizeof(FFV1Context),
972 .init = ffv1_decode_init,
973 .close = ffv1_decode_close,
974 .decode = ffv1_decode_frame,
975 .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
976 AV_CODEC_CAP_SLICE_THREADS,