ffv1: Error out on unsupported format
[libav.git] / libavcodec / ffv1dec.c
CommitLineData
71f7b22d
LB
1/*
2 * FFV1 decoder
3 *
0f13cd31 4 * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
71f7b22d
LB
5 *
6 * This file is part of Libav.
7 *
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.
12 *
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.
17 *
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
21 */
22
23/**
24 * @file
25 * FF Video Codec 1 (a lossless codec) decoder
26 */
27
28#include "libavutil/avassert.h"
0f13cd31
LB
29#include "libavutil/pixdesc.h"
30#include "libavutil/crc.h"
31#include "libavutil/opt.h"
32#include "libavutil/imgutils.h"
fb0c9d41 33#include "libavutil/timer.h"
71f7b22d 34#include "avcodec.h"
594d4d5d 35#include "internal.h"
71f7b22d
LB
36#include "get_bits.h"
37#include "put_bits.h"
71f7b22d
LB
38#include "rangecoder.h"
39#include "golomb.h"
40#include "mathops.h"
41#include "ffv1.h"
42
43static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
44 int is_signed)
45{
46 if (get_rac(c, state + 0))
47 return 0;
48 else {
49 int i, e, a;
50 e = 0;
51 while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
52 e++;
53
54 a = 1;
55 for (i = e - 1; i >= 0; i--)
56 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
57
58 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
59 return (a ^ e) - e;
60 }
61}
62
63static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
64{
65 return get_symbol_inline(c, state, is_signed);
66}
67
68static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
69 int bits)
70{
71 int k, i, v, ret;
72
73 i = state->count;
74 k = 0;
75 while (i < state->error_sum) { // FIXME: optimize
76 k++;
77 i += i;
78 }
79
80 assert(k <= 8);
81
82 v = get_sr_golomb(gb, k, 12, bits);
6a85dfc8 83 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
71f7b22d
LB
84 v, state->bias, state->error_sum, state->drift, state->count, k);
85
86#if 0 // JPEG LS
87 if (k == 0 && 2 * state->drift <= -state->count)
88 v ^= (-1);
89#else
90 v ^= ((2 * state->drift + state->count) >> 31);
91#endif
92
93 ret = fold(v + state->bias, bits);
94
95 update_vlc_state(state, v);
96
97 return ret;
98}
99
100static av_always_inline void decode_line(FFV1Context *s, int w,
101 int16_t *sample[2],
102 int plane_index, int bits)
103{
104 PlaneContext *const p = &s->plane[plane_index];
105 RangeCoder *const c = &s->c;
106 int x;
107 int run_count = 0;
108 int run_mode = 0;
109 int run_index = s->run_index;
110
111 for (x = 0; x < w; x++) {
112 int diff, context, sign;
113
114 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
115 if (context < 0) {
116 context = -context;
117 sign = 1;
118 } else
119 sign = 0;
120
121 av_assert2(context < p->context_count);
122
4bb1070c 123 if (s->ac != AC_GOLOMB_RICE) {
71f7b22d
LB
124 diff = get_symbol_inline(c, p->state[context], 1);
125 } else {
126 if (context == 0 && run_mode == 0)
127 run_mode = 1;
128
129 if (run_mode) {
130 if (run_count == 0 && run_mode == 1) {
131 if (get_bits1(&s->gb)) {
132 run_count = 1 << ff_log2_run[run_index];
133 if (x + run_count <= w)
134 run_index++;
135 } else {
136 if (ff_log2_run[run_index])
137 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
138 else
139 run_count = 0;
140 if (run_index)
141 run_index--;
142 run_mode = 2;
143 }
144 }
145 run_count--;
146 if (run_count < 0) {
147 run_mode = 0;
148 run_count = 0;
149 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
150 bits);
151 if (diff >= 0)
152 diff++;
153 } else
154 diff = 0;
155 } else
156 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
157
6a85dfc8 158 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
71f7b22d
LB
159 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
160 }
161
162 if (sign)
163 diff = -diff;
164
165 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
166 ((1 << bits) - 1);
167 }
168 s->run_index = run_index;
169}
170
171static void decode_plane(FFV1Context *s, uint8_t *src,
172 int w, int h, int stride, int plane_index)
173{
174 int x, y;
175 int16_t *sample[2];
176 sample[0] = s->sample_buffer + 3;
177 sample[1] = s->sample_buffer + w + 6 + 3;
178
179 s->run_index = 0;
180
181 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
182
183 for (y = 0; y < h; y++) {
184 int16_t *temp = sample[0]; // FIXME: try a normal buffer
185
186 sample[0] = sample[1];
187 sample[1] = temp;
188
189 sample[1][-1] = sample[0][0];
190 sample[0][w] = sample[0][w - 1];
191
192// { START_TIMER
193 if (s->avctx->bits_per_raw_sample <= 8) {
194 decode_line(s, w, sample, plane_index, 8);
195 for (x = 0; x < w; x++)
196 src[x + stride * y] = sample[1][x];
197 } else {
198 decode_line(s, w, sample, plane_index,
199 s->avctx->bits_per_raw_sample);
0f13cd31
LB
200 if (s->packed_at_lsb) {
201 for (x = 0; x < w; x++)
202 ((uint16_t *)(src + stride * y))[x] = sample[1][x];
203 } else {
204 for (x = 0; x < w; x++)
205 ((uint16_t *)(src + stride * y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
206 }
71f7b22d
LB
207 }
208// STOP_TIMER("decode-line") }
209 }
210}
211
0f13cd31
LB
212static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
213 int stride[3])
71f7b22d
LB
214{
215 int x, y, p;
0f13cd31
LB
216 int16_t *sample[4][2];
217 int lbd = s->avctx->bits_per_raw_sample <= 8;
218 int bits = s->avctx->bits_per_raw_sample > 0
219 ? s->avctx->bits_per_raw_sample
220 : 8;
221 int offset = 1 << bits;
222
223 for (x = 0; x < 4; x++) {
71f7b22d
LB
224 sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
225 sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
226 }
227
228 s->run_index = 0;
229
0f13cd31 230 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
71f7b22d
LB
231
232 for (y = 0; y < h; y++) {
0f13cd31
LB
233 for (p = 0; p < 3 + s->transparency; p++) {
234 int16_t *temp = sample[p][0]; //FIXME try a normal buffer
71f7b22d
LB
235
236 sample[p][0] = sample[p][1];
237 sample[p][1] = temp;
238
239 sample[p][1][-1] = sample[p][0][0];
240 sample[p][0][w] = sample[p][0][w - 1];
0f13cd31
LB
241 if (lbd)
242 decode_line(s, w, sample[p], (p + 1) / 2, 9);
243 else
244 decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
71f7b22d
LB
245 }
246 for (x = 0; x < w; x++) {
247 int g = sample[0][1][x];
248 int b = sample[1][1][x];
249 int r = sample[2][1][x];
0f13cd31 250 int a = sample[3][1][x];
71f7b22d 251
0f13cd31
LB
252 b -= offset;
253 r -= offset;
71f7b22d
LB
254 g -= (b + r) >> 2;
255 b += g;
256 r += g;
257
0f13cd31
LB
258 if (lbd)
259 *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
260 (g << 8) + (r << 16) + (a << 24);
261 else {
262 *((uint16_t *)(src[0] + x * 2 + stride[0] * y)) = b;
263 *((uint16_t *)(src[1] + x * 2 + stride[1] * y)) = g;
264 *((uint16_t *)(src[2] + x * 2 + stride[2] * y)) = r;
265 }
71f7b22d
LB
266 }
267 }
268}
269
0f13cd31
LB
270static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
271{
272 RangeCoder *c = &fs->c;
273 uint8_t state[CONTEXT_SIZE];
274 unsigned ps, i, context_count;
275 memset(state, 128, sizeof(state));
276
4bb1070c 277 if (fs->ac == AC_RANGE_CUSTOM_TAB) {
22f7942f
LB
278 for (i = 1; i < 256; i++) {
279 fs->c.one_state[i] = f->state_transition[i];
280 fs->c.zero_state[256 - i] = 256 - fs->c.one_state[i];
281 }
282 }
0f13cd31
LB
283
284 fs->slice_x = get_symbol(c, state, 0) * f->width;
285 fs->slice_y = get_symbol(c, state, 0) * f->height;
286 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
287 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
288
289 fs->slice_x /= f->num_h_slices;
290 fs->slice_y /= f->num_v_slices;
291 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
292 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
293 if ((unsigned)fs->slice_width > f->width ||
294 (unsigned)fs->slice_height > f->height)
295 return AVERROR_INVALIDDATA;
296 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
297 (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
298 return AVERROR_INVALIDDATA;
299
300 for (i = 0; i < f->plane_count; i++) {
301 PlaneContext *const p = &fs->plane[i];
302 int idx = get_symbol(c, state, 0);
303 if (idx > (unsigned)f->quant_table_count) {
304 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
305 return AVERROR_INVALIDDATA;
306 }
307 p->quant_table_index = idx;
308 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
309 context_count = f->context_count[idx];
310
311 if (p->context_count < context_count) {
312 av_freep(&p->state);
313 av_freep(&p->vlc_state);
314 }
315 p->context_count = context_count;
316 }
317
318 ps = get_symbol(c, state, 0);
319 if (ps == 1) {
759001c5
AK
320 f->cur->interlaced_frame = 1;
321 f->cur->top_field_first = 1;
0f13cd31 322 } else if (ps == 2) {
759001c5
AK
323 f->cur->interlaced_frame = 1;
324 f->cur->top_field_first = 0;
0f13cd31 325 } else if (ps == 3) {
759001c5 326 f->cur->interlaced_frame = 0;
0f13cd31 327 }
759001c5
AK
328 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
329 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
0f13cd31 330
9e500efd
JR
331 if (av_image_check_sar(f->width, f->height,
332 f->cur->sample_aspect_ratio) < 0) {
333 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
334 f->cur->sample_aspect_ratio.num,
335 f->cur->sample_aspect_ratio.den);
336 f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
337 }
338
0f13cd31
LB
339 return 0;
340}
341
71f7b22d
LB
342static int decode_slice(AVCodecContext *c, void *arg)
343{
0f13cd31
LB
344 FFV1Context *fs = *(void **)arg;
345 FFV1Context *f = fs->avctx->priv_data;
346 int width, height, x, y, ret;
e6c4ac7b 347 const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)
0f13cd31
LB
348 ? (c->bits_per_raw_sample > 8) + 1
349 : 4;
759001c5 350 AVFrame *const p = f->cur;
71f7b22d 351
0f13cd31
LB
352 if (f->version > 2) {
353 if (decode_slice_header(f, fs) < 0) {
354 fs->slice_damaged = 1;
355 return AVERROR_INVALIDDATA;
356 }
357 }
358 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
359 return ret;
759001c5 360 if (f->cur->key_frame)
0f13cd31
LB
361 ffv1_clear_slice_state(f, fs);
362 width = fs->slice_width;
363 height = fs->slice_height;
364 x = fs->slice_x;
365 y = fs->slice_y;
366
4bb1070c 367 if (fs->ac == AC_GOLOMB_RICE) {
0f13cd31
LB
368 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
369 get_rac(&fs->c, (uint8_t[]) { 129 });
370 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
371 init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,
372 (fs->c.bytestream_end - fs->c.bytestream_start -
373 fs->ac_byte_count) * 8);
374 }
375
71f7b22d
LB
376 av_assert1(width && height);
377 if (f->colorspace == 0) {
e8bc6422
CB
378 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
379 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
71f7b22d
LB
380 const int cx = x >> f->chroma_h_shift;
381 const int cy = y >> f->chroma_v_shift;
0f13cd31
LB
382 decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
383 height, p->linesize[0],
384 0);
385
386 if (f->chroma_planes) {
387 decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
388 chroma_width, chroma_height, p->linesize[1],
389 1);
390 decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
391 chroma_width, chroma_height, p->linesize[2],
392 1);
393 }
394 if (fs->transparency)
395 decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
396 height, p->linesize[3],
397 2);
71f7b22d 398 } else {
0f13cd31
LB
399 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
400 p->data[1] + ps * x + y * p->linesize[1],
401 p->data[2] + ps * x + y * p->linesize[2] };
402 decode_rgb_frame(fs, planes, width, height, p->linesize);
403 }
4bb1070c 404 if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
0f13cd31
LB
405 int v;
406 get_rac(&fs->c, (uint8_t[]) { 129 });
407 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
408 if (v) {
409 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
410 v);
411 fs->slice_damaged = 1;
412 }
71f7b22d
LB
413 }
414
415 emms_c();
416
417 return 0;
418}
419
420static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
421{
422 int v;
423 int i = 0;
424 uint8_t state[CONTEXT_SIZE];
425
426 memset(state, 128, sizeof(state));
427
428 for (v = 0; i < 128; v++) {
0f13cd31 429 unsigned len = get_symbol(c, state, 0) + 1;
71f7b22d 430
0f13cd31
LB
431 if (len > 128 - i)
432 return -1;
71f7b22d
LB
433
434 while (len--) {
435 quant_table[i] = scale * v;
436 i++;
437 }
438 }
439
440 for (i = 1; i < 128; i++)
441 quant_table[256 - i] = -quant_table[i];
442 quant_table[128] = -quant_table[127];
443
444 return 2 * v - 1;
445}
446
447static int read_quant_tables(RangeCoder *c,
448 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
449{
450 int i;
451 int context_count = 1;
452
453 for (i = 0; i < 5; i++) {
454 context_count *= read_quant_table(c, quant_table[i], context_count);
455 if (context_count > 32768U) {
0f13cd31 456 return -1;
71f7b22d
LB
457 }
458 }
459 return (context_count + 1) / 2;
460}
461
462static int read_extra_header(FFV1Context *f)
463{
464 RangeCoder *const c = &f->c;
465 uint8_t state[CONTEXT_SIZE];
0f13cd31 466 int i, j, k, ret;
71f7b22d
LB
467 uint8_t state2[32][CONTEXT_SIZE];
468
469 memset(state2, 128, sizeof(state2));
470 memset(state, 128, sizeof(state));
471
472 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
473 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
474
475 f->version = get_symbol(c, state, 0);
0f13cd31
LB
476 if (f->version > 2) {
477 c->bytestream_end -= 4;
478 f->minor_version = get_symbol(c, state, 0);
479 }
4bb1070c 480 f->ac = get_symbol(c, state, 0);
0f13cd31 481
4bb1070c 482 if (f->ac == AC_RANGE_CUSTOM_TAB) {
71f7b22d
LB
483 for (i = 1; i < 256; i++)
484 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
0f13cd31
LB
485 }
486
487 f->colorspace = get_symbol(c, state, 0); //YUV cs type
71f7b22d 488 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
0f13cd31
LB
489 f->chroma_planes = get_rac(c, state);
490 f->chroma_h_shift = get_symbol(c, state, 0);
491 f->chroma_v_shift = get_symbol(c, state, 0);
492 f->transparency = get_rac(c, state);
493 f->plane_count = 2 + f->transparency;
494 f->num_h_slices = 1 + get_symbol(c, state, 0);
495 f->num_v_slices = 1 + get_symbol(c, state, 0);
71f7b22d
LB
496
497 if (f->num_h_slices > (unsigned)f->width ||
498 f->num_v_slices > (unsigned)f->height) {
499 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
4a2a4524 500 return AVERROR_INVALIDDATA;
71f7b22d
LB
501 }
502
503 f->quant_table_count = get_symbol(c, state, 0);
71f7b22d 504 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
4a2a4524 505 return AVERROR_INVALIDDATA;
71f7b22d
LB
506 for (i = 0; i < f->quant_table_count; i++) {
507 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
508 if (f->context_count[i] < 0) {
509 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
0f13cd31 510 return AVERROR_INVALIDDATA;
71f7b22d
LB
511 }
512 }
0f13cd31
LB
513 if ((ret = ffv1_allocate_initial_states(f)) < 0)
514 return ret;
71f7b22d
LB
515
516 for (i = 0; i < f->quant_table_count; i++)
0f13cd31 517 if (get_rac(c, state)) {
71f7b22d
LB
518 for (j = 0; j < f->context_count[i]; j++)
519 for (k = 0; k < CONTEXT_SIZE; k++) {
520 int pred = j ? f->initial_states[i][j - 1][k] : 128;
521 f->initial_states[i][j][k] =
522 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
523 }
0f13cd31
LB
524 }
525
526 if (f->version > 2) {
527 f->ec = get_symbol(c, state, 0);
528 }
529
530 if (f->version > 2) {
531 unsigned v;
532 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
533 f->avctx->extradata, f->avctx->extradata_size);
534 if (v) {
535 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
536 return AVERROR_INVALIDDATA;
537 }
538 }
539
71f7b22d
LB
540 return 0;
541}
542
0f13cd31 543
71f7b22d
LB
544static int read_header(FFV1Context *f)
545{
546 uint8_t state[CONTEXT_SIZE];
0f13cd31 547 int i, j, context_count = -1;
71f7b22d
LB
548 RangeCoder *const c = &f->slice_context[0]->c;
549
550 memset(state, 128, sizeof(state));
551
552 if (f->version < 2) {
da7d839a 553 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
0f13cd31
LB
554 unsigned v = get_symbol(c, state, 0);
555 if (v > 1) {
556 av_log(f->avctx, AV_LOG_ERROR,
557 "invalid version %d in version 1 header\n", v);
558 return AVERROR_INVALIDDATA;
559 }
560 f->version = v;
561
4bb1070c 562 f->ac = get_symbol(c, state, 0);
0f13cd31 563
4bb1070c 564 if (f->ac == AC_RANGE_CUSTOM_TAB) {
71f7b22d 565 for (i = 1; i < 256; i++)
0f13cd31
LB
566 f->state_transition[i] =
567 get_symbol(c, state, 1) + c->one_state[i];
568 }
569
da7d839a
MN
570 colorspace = get_symbol(c, state, 0); //YUV cs type
571 bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
572 chroma_planes = get_rac(c, state);
573 chroma_h_shift = get_symbol(c, state, 0);
574 chroma_v_shift = get_symbol(c, state, 0);
575 transparency = get_rac(c, state);
576
577 if (f->plane_count) {
578 if (colorspace != f->colorspace ||
579 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
580 chroma_planes != f->chroma_planes ||
581 chroma_h_shift != f->chroma_h_shift ||
582 chroma_v_shift != f->chroma_v_shift ||
583 transparency != f->transparency) {
584 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
585 return AVERROR_INVALIDDATA;
586 }
587 }
0f13cd31 588
da7d839a
MN
589 f->colorspace = colorspace;
590 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
591 f->chroma_planes = chroma_planes;
592 f->chroma_h_shift = chroma_h_shift;
593 f->chroma_v_shift = chroma_v_shift;
594 f->transparency = transparency;
0f13cd31 595
0f13cd31 596 f->plane_count = 2 + f->transparency;
71f7b22d
LB
597 }
598
599 if (f->colorspace == 0) {
ec9f0442
JM
600 if (f->transparency && f->avctx->bits_per_raw_sample > 8) {
601 av_log(f->avctx, AV_LOG_ERROR,
602 "Transparency not supported for bit depth %d\n",
603 f->avctx->bits_per_raw_sample);
604 return AVERROR(ENOSYS);
605 }
0f13cd31
LB
606 if (!f->transparency && !f->chroma_planes) {
607 if (f->avctx->bits_per_raw_sample <= 8)
608 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
609 else
610 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
611 } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
71f7b22d
LB
612 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
613 case 0x00:
614 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
615 break;
0f13cd31
LB
616 case 0x01:
617 f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
618 break;
71f7b22d
LB
619 case 0x10:
620 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
621 break;
622 case 0x11:
623 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
624 break;
625 case 0x20:
626 f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
627 break;
628 case 0x22:
629 f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
630 break;
631 default:
632 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
4a2a4524 633 return AVERROR(ENOSYS);
71f7b22d 634 }
0f13cd31
LB
635 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
636 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
637 case 0x00:
638 f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
639 break;
640 case 0x10:
641 f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
642 break;
643 case 0x11:
644 f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
645 break;
646 default:
647 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
648 return AVERROR(ENOSYS);
649 }
650 } else if (f->avctx->bits_per_raw_sample == 9) {
651 f->packed_at_lsb = 1;
652 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
653 case 0x00:
654 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
655 break;
656 case 0x10:
657 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
658 break;
659 case 0x11:
660 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
661 break;
662 default:
663 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
664 return AVERROR(ENOSYS);
665 }
666 } else if (f->avctx->bits_per_raw_sample == 10) {
667 f->packed_at_lsb = 1;
668 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
669 case 0x00:
670 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
671 break;
672 case 0x10:
673 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
674 break;
675 case 0x11:
676 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
677 break;
678 default:
679 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
680 return AVERROR(ENOSYS);
681 }
71f7b22d
LB
682 } else {
683 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
684 case 0x00:
685 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
686 break;
687 case 0x10:
688 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
689 break;
690 case 0x11:
691 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
692 break;
693 default:
694 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
4a2a4524 695 return AVERROR(ENOSYS);
71f7b22d
LB
696 }
697 }
698 } else if (f->colorspace == 1) {
699 if (f->chroma_h_shift || f->chroma_v_shift) {
700 av_log(f->avctx, AV_LOG_ERROR,
701 "chroma subsampling not supported in this colorspace\n");
4a2a4524 702 return AVERROR(ENOSYS);
71f7b22d 703 }
ec9f0442
JM
704 if (f->transparency) {
705 av_log(f->avctx, AV_LOG_ERROR,
706 "Transparency not supported in this colorspace\n");
707 return AVERROR(ENOSYS);
708 }
0f13cd31 709 switch (f->avctx->bits_per_raw_sample) {
a90905db 710 case 0:
0f13cd31
LB
711 case 8:
712 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
713 break;
714 case 9:
715 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
716 break;
717 case 10:
718 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
719 break;
720 default:
721 av_log(f->avctx, AV_LOG_ERROR,
722 "bit depth %d not supported\n",
723 f->avctx->bits_per_raw_sample);
724 return AVERROR(ENOSYS);
725 }
71f7b22d
LB
726 } else {
727 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
4a2a4524 728 return AVERROR(ENOSYS);
71f7b22d
LB
729 }
730
6a85dfc8 731 ff_dlog(f->avctx, "%d %d %d\n",
71f7b22d 732 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
71f7b22d
LB
733 if (f->version < 2) {
734 context_count = read_quant_tables(c, f->quant_table);
735 if (context_count < 0) {
736 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
0f13cd31 737 return AVERROR_INVALIDDATA;
71f7b22d 738 }
0f13cd31 739 } else if (f->version < 3) {
71f7b22d 740 f->slice_count = get_symbol(c, state, 0);
0f13cd31
LB
741 } else {
742 const uint8_t *p = c->bytestream_end;
743 for (f->slice_count = 0;
744 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
745 f->slice_count++) {
746 int trailer = 3 + 5 * !!f->ec;
747 int size = AV_RB24(p - trailer);
748 if (size + trailer > p - c->bytestream_start)
749 break;
750 p -= size + trailer;
751 }
752 }
753 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
754 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
755 f->slice_count);
756 return AVERROR_INVALIDDATA;
71f7b22d
LB
757 }
758
759 for (j = 0; j < f->slice_count; j++) {
760 FFV1Context *fs = f->slice_context[j];
0f13cd31
LB
761 fs->ac = f->ac;
762 fs->packed_at_lsb = f->packed_at_lsb;
763
764 fs->slice_damaged = 0;
765
766 if (f->version == 2) {
767 fs->slice_x = get_symbol(c, state, 0) * f->width;
768 fs->slice_y = get_symbol(c, state, 0) * f->height;
769 fs->slice_width =
770 (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
771 fs->slice_height =
772 (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
773
774 fs->slice_x /= f->num_h_slices;
775 fs->slice_y /= f->num_v_slices;
d2438969
AK
776 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
777 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
0f13cd31 778 if ((unsigned)fs->slice_width > f->width ||
71f7b22d 779 (unsigned)fs->slice_height > f->height)
4a2a4524 780 return AVERROR_INVALIDDATA;
0f13cd31
LB
781 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
782 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
783 f->height)
4a2a4524 784 return AVERROR_INVALIDDATA;
71f7b22d
LB
785 }
786
787 for (i = 0; i < f->plane_count; i++) {
788 PlaneContext *const p = &fs->plane[i];
789
0f13cd31 790 if (f->version == 2) {
71f7b22d
LB
791 int idx = get_symbol(c, state, 0);
792 if (idx > (unsigned)f->quant_table_count) {
793 av_log(f->avctx, AV_LOG_ERROR,
794 "quant_table_index out of range\n");
4a2a4524 795 return AVERROR_INVALIDDATA;
71f7b22d
LB
796 }
797 p->quant_table_index = idx;
798 memcpy(p->quant_table, f->quant_tables[idx],
799 sizeof(p->quant_table));
800 context_count = f->context_count[idx];
801 } else {
802 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
803 }
804
0f13cd31
LB
805 if (f->version <= 2) {
806 av_assert0(context_count >= 0);
807 if (p->context_count < context_count) {
808 av_freep(&p->state);
809 av_freep(&p->vlc_state);
810 }
811 p->context_count = context_count;
71f7b22d 812 }
71f7b22d
LB
813 }
814 }
71f7b22d
LB
815 return 0;
816}
817
818static av_cold int ffv1_decode_init(AVCodecContext *avctx)
819{
820 FFV1Context *f = avctx->priv_data;
4a2a4524 821 int ret;
71f7b22d
LB
822
823 ffv1_common_init(avctx);
824
a6064b12
AK
825 f->last_picture = av_frame_alloc();
826 if (!f->last_picture)
827 return AVERROR(ENOMEM);
828
4a2a4524
LB
829 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
830 return ret;
71f7b22d 831
4a2a4524
LB
832 if ((ret = ffv1_init_slice_contexts(f)) < 0)
833 return ret;
71f7b22d
LB
834
835 return 0;
836}
837
838static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
df9b9567 839 int *got_frame, AVPacket *avpkt)
71f7b22d 840{
ca09effb 841 uint8_t *buf = avpkt->data;
71f7b22d
LB
842 int buf_size = avpkt->size;
843 FFV1Context *f = avctx->priv_data;
844 RangeCoder *const c = &f->slice_context[0]->c;
0f13cd31 845 int i, ret;
71f7b22d 846 uint8_t keystate = 128;
ca09effb 847 uint8_t *buf_p;
759001c5 848 AVFrame *const p = data;
71f7b22d 849
759001c5 850 f->cur = p;
71f7b22d
LB
851
852 ff_init_range_decoder(c, buf, buf_size);
853 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
854
0f13cd31 855 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
71f7b22d 856 if (get_rac(c, &keystate)) {
0f13cd31
LB
857 p->key_frame = 1;
858 f->key_frame_ok = 0;
4a2a4524
LB
859 if ((ret = read_header(f)) < 0)
860 return ret;
0f13cd31 861 f->key_frame_ok = 1;
71f7b22d 862 } else {
0f13cd31
LB
863 if (!f->key_frame_ok) {
864 av_log(avctx, AV_LOG_ERROR,
511cf612 865 "Cannot decode non-keyframe without valid keyframe\n");
0f13cd31 866 return AVERROR_INVALIDDATA;
71f7b22d 867 }
0f13cd31 868 p->key_frame = 0;
71f7b22d
LB
869 }
870
759001c5 871 if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0) {
71f7b22d 872 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
4a2a4524 873 return ret;
71f7b22d
LB
874 }
875
876 if (avctx->debug & FF_DEBUG_PICT_INFO)
0f13cd31
LB
877 av_log(avctx, AV_LOG_DEBUG,
878 "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
879 f->version, p->key_frame, f->ac, f->ec, f->slice_count,
880 f->avctx->bits_per_raw_sample);
71f7b22d
LB
881
882 buf_p = buf + buf_size;
0f13cd31 883 for (i = f->slice_count - 1; i >= 0; i--) {
71f7b22d 884 FFV1Context *fs = f->slice_context[i];
0f13cd31
LB
885 int trailer = 3 + 5 * !!f->ec;
886 int v;
887
888 if (i || f->version > 2)
889 v = AV_RB24(buf_p - trailer) + trailer;
890 else
891 v = buf_p - c->bytestream_start;
892 if (buf_p - c->bytestream_start < v) {
71f7b22d 893 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
4a2a4524 894 return AVERROR_INVALIDDATA;
71f7b22d
LB
895 }
896 buf_p -= v;
0f13cd31
LB
897
898 if (f->ec) {
899 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
900 if (crc) {
901 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
902 fs->slice_damaged = 1;
903 }
904 }
905
906 if (i) {
71f7b22d 907 ff_init_range_decoder(&fs->c, buf_p, v);
0f13cd31 908 } else
ca09effb 909 fs->c.bytestream_end = buf_p + v;
759001c5
AK
910
911 fs->cur = p;
0f13cd31
LB
912 }
913
914 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
915 f->slice_count,
916 sizeof(void *));
917
918 for (i = f->slice_count - 1; i >= 0; i--) {
919 FFV1Context *fs = f->slice_context[i];
920 int j;
a6064b12 921 if (fs->slice_damaged && f->last_picture->data[0]) {
0f13cd31
LB
922 const uint8_t *src[4];
923 uint8_t *dst[4];
924 for (j = 0; j < 4; j++) {
925 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
926 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
759001c5 927 dst[j] = p->data[j] + p->linesize[j] *
0f13cd31 928 (fs->slice_y >> sv) + (fs->slice_x >> sh);
a6064b12
AK
929 src[j] = f->last_picture->data[j] +
930 f->last_picture->linesize[j] *
0f13cd31
LB
931 (fs->slice_y >> sv) + (fs->slice_x >> sh);
932 }
ca09effb 933 av_image_copy(dst, p->linesize, src,
a6064b12 934 f->last_picture->linesize,
0f13cd31
LB
935 avctx->pix_fmt, fs->slice_width,
936 fs->slice_height);
937 }
71f7b22d
LB
938 }
939
71f7b22d
LB
940 f->picture_number++;
941
a6064b12
AK
942 av_frame_unref(f->last_picture);
943 if ((ret = av_frame_ref(f->last_picture, p)) < 0)
759001c5
AK
944 return ret;
945 f->cur = NULL;
71f7b22d 946
759001c5 947 *got_frame = 1;
0f13cd31 948
71f7b22d
LB
949 return buf_size;
950}
951
a6064b12
AK
952static av_cold int ffv1_decode_close(AVCodecContext *avctx)
953{
954 FFV1Context *s = avctx->priv_data;;
955
956 av_frame_free(&s->last_picture);
957
958 ffv1_close(avctx);
959
960 return 0;
961}
962
71f7b22d
LB
963AVCodec ff_ffv1_decoder = {
964 .name = "ffv1",
b2bed932 965 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
71f7b22d
LB
966 .type = AVMEDIA_TYPE_VIDEO,
967 .id = AV_CODEC_ID_FFV1,
968 .priv_data_size = sizeof(FFV1Context),
969 .init = ffv1_decode_init,
a6064b12 970 .close = ffv1_decode_close,
71f7b22d 971 .decode = ffv1_decode_frame,
def97856
VG
972 .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
973 AV_CODEC_CAP_SLICE_THREADS,
71f7b22d 974};