ffv1: propagate errors
[libav.git] / libavcodec / ffv1dec.c
CommitLineData
71f7b22d
LB
1/*
2 * FFV1 decoder
3 *
4 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
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"
29#include "avcodec.h"
30#include "get_bits.h"
31#include "put_bits.h"
32#include "dsputil.h"
33#include "rangecoder.h"
34#include "golomb.h"
35#include "mathops.h"
36#include "ffv1.h"
37
38static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
39 int is_signed)
40{
41 if (get_rac(c, state + 0))
42 return 0;
43 else {
44 int i, e, a;
45 e = 0;
46 while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
47 e++;
48
49 a = 1;
50 for (i = e - 1; i >= 0; i--)
51 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
52
53 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
54 return (a ^ e) - e;
55 }
56}
57
58static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
59{
60 return get_symbol_inline(c, state, is_signed);
61}
62
63static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
64 int bits)
65{
66 int k, i, v, ret;
67
68 i = state->count;
69 k = 0;
70 while (i < state->error_sum) { // FIXME: optimize
71 k++;
72 i += i;
73 }
74
75 assert(k <= 8);
76
77 v = get_sr_golomb(gb, k, 12, bits);
78 av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
79 v, state->bias, state->error_sum, state->drift, state->count, k);
80
81#if 0 // JPEG LS
82 if (k == 0 && 2 * state->drift <= -state->count)
83 v ^= (-1);
84#else
85 v ^= ((2 * state->drift + state->count) >> 31);
86#endif
87
88 ret = fold(v + state->bias, bits);
89
90 update_vlc_state(state, v);
91
92 return ret;
93}
94
95static av_always_inline void decode_line(FFV1Context *s, int w,
96 int16_t *sample[2],
97 int plane_index, int bits)
98{
99 PlaneContext *const p = &s->plane[plane_index];
100 RangeCoder *const c = &s->c;
101 int x;
102 int run_count = 0;
103 int run_mode = 0;
104 int run_index = s->run_index;
105
106 for (x = 0; x < w; x++) {
107 int diff, context, sign;
108
109 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
110 if (context < 0) {
111 context = -context;
112 sign = 1;
113 } else
114 sign = 0;
115
116 av_assert2(context < p->context_count);
117
118 if (s->ac) {
119 diff = get_symbol_inline(c, p->state[context], 1);
120 } else {
121 if (context == 0 && run_mode == 0)
122 run_mode = 1;
123
124 if (run_mode) {
125 if (run_count == 0 && run_mode == 1) {
126 if (get_bits1(&s->gb)) {
127 run_count = 1 << ff_log2_run[run_index];
128 if (x + run_count <= w)
129 run_index++;
130 } else {
131 if (ff_log2_run[run_index])
132 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
133 else
134 run_count = 0;
135 if (run_index)
136 run_index--;
137 run_mode = 2;
138 }
139 }
140 run_count--;
141 if (run_count < 0) {
142 run_mode = 0;
143 run_count = 0;
144 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
145 bits);
146 if (diff >= 0)
147 diff++;
148 } else
149 diff = 0;
150 } else
151 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
152
153 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
154 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
155 }
156
157 if (sign)
158 diff = -diff;
159
160 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
161 ((1 << bits) - 1);
162 }
163 s->run_index = run_index;
164}
165
166static void decode_plane(FFV1Context *s, uint8_t *src,
167 int w, int h, int stride, int plane_index)
168{
169 int x, y;
170 int16_t *sample[2];
171 sample[0] = s->sample_buffer + 3;
172 sample[1] = s->sample_buffer + w + 6 + 3;
173
174 s->run_index = 0;
175
176 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
177
178 for (y = 0; y < h; y++) {
179 int16_t *temp = sample[0]; // FIXME: try a normal buffer
180
181 sample[0] = sample[1];
182 sample[1] = temp;
183
184 sample[1][-1] = sample[0][0];
185 sample[0][w] = sample[0][w - 1];
186
187// { START_TIMER
188 if (s->avctx->bits_per_raw_sample <= 8) {
189 decode_line(s, w, sample, plane_index, 8);
190 for (x = 0; x < w; x++)
191 src[x + stride * y] = sample[1][x];
192 } else {
193 decode_line(s, w, sample, plane_index,
194 s->avctx->bits_per_raw_sample);
195 for (x = 0; x < w; x++)
196 ((uint16_t *)(src + stride * y))[x] =
197 sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
198 }
199// STOP_TIMER("decode-line") }
200 }
201}
202
203static void decode_rgb_frame(FFV1Context *s, uint32_t *src,
204 int w, int h, int stride)
205{
206 int x, y, p;
207 int16_t *sample[3][2];
208 for (x = 0; x < 3; x++) {
209 sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
210 sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
211 }
212
213 s->run_index = 0;
214
215 memset(s->sample_buffer, 0, 6 * (w + 6) * sizeof(*s->sample_buffer));
216
217 for (y = 0; y < h; y++) {
218 for (p = 0; p < 3; p++) {
219 int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
220
221 sample[p][0] = sample[p][1];
222 sample[p][1] = temp;
223
224 sample[p][1][-1] = sample[p][0][0];
225 sample[p][0][w] = sample[p][0][w - 1];
226 decode_line(s, w, sample[p], FFMIN(p, 1), 9);
227 }
228 for (x = 0; x < w; x++) {
229 int g = sample[0][1][x];
230 int b = sample[1][1][x];
231 int r = sample[2][1][x];
232
233// assert(g >= 0 && b >= 0 && r >= 0);
234// assert(g < 256 && b < 512 && r < 512);
235
236 b -= 0x100;
237 r -= 0x100;
238 g -= (b + r) >> 2;
239 b += g;
240 r += g;
241
242 src[x + stride * y] = b + (g << 8) + (r << 16) + (0xFF << 24);
243 }
244 }
245}
246
247static int decode_slice(AVCodecContext *c, void *arg)
248{
249 FFV1Context *fs = *(void **)arg;
250 FFV1Context *f = fs->avctx->priv_data;
251 int width = fs->slice_width;
252 int height = fs->slice_height;
253 int x = fs->slice_x;
254 int y = fs->slice_y;
255 AVFrame *const p = &f->picture;
256
257 av_assert1(width && height);
258 if (f->colorspace == 0) {
259 const int chroma_width = -((-width) >> f->chroma_h_shift);
260 const int chroma_height = -((-height) >> f->chroma_v_shift);
261 const int cx = x >> f->chroma_h_shift;
262 const int cy = y >> f->chroma_v_shift;
263
264 decode_plane(fs, p->data[0] + x + y * p->linesize[0],
265 width, height, p->linesize[0], 0);
266
267 decode_plane(fs, p->data[1] + cx + cy * p->linesize[1],
268 chroma_width, chroma_height, p->linesize[1], 1);
269 decode_plane(fs, p->data[2] + cx + cy * p->linesize[1],
270 chroma_width, chroma_height, p->linesize[2], 1);
271 } else {
272 decode_rgb_frame(fs,
273 (uint32_t *)p->data[0] + x + y * (p->linesize[0] / 4),
274 width, height, p->linesize[0] / 4);
275 }
276
277 emms_c();
278
279 return 0;
280}
281
282static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
283{
284 int v;
285 int i = 0;
286 uint8_t state[CONTEXT_SIZE];
287
288 memset(state, 128, sizeof(state));
289
290 for (v = 0; i < 128; v++) {
291 int len = get_symbol(c, state, 0) + 1;
292
293 if (len + i > 128)
4a2a4524 294 return AVERROR_INVALIDDATA;
71f7b22d
LB
295
296 while (len--) {
297 quant_table[i] = scale * v;
298 i++;
299 }
300 }
301
302 for (i = 1; i < 128; i++)
303 quant_table[256 - i] = -quant_table[i];
304 quant_table[128] = -quant_table[127];
305
306 return 2 * v - 1;
307}
308
309static int read_quant_tables(RangeCoder *c,
310 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
311{
312 int i;
313 int context_count = 1;
314
315 for (i = 0; i < 5; i++) {
316 context_count *= read_quant_table(c, quant_table[i], context_count);
317 if (context_count > 32768U) {
4a2a4524 318 return AVERROR_INVALIDDATA;
71f7b22d
LB
319 }
320 }
321 return (context_count + 1) / 2;
322}
323
324static int read_extra_header(FFV1Context *f)
325{
326 RangeCoder *const c = &f->c;
327 uint8_t state[CONTEXT_SIZE];
328 int i, j, k;
329 uint8_t state2[32][CONTEXT_SIZE];
330
331 memset(state2, 128, sizeof(state2));
332 memset(state, 128, sizeof(state));
333
334 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
335 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
336
337 f->version = get_symbol(c, state, 0);
338 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
339 if (f->ac > 1)
340 for (i = 1; i < 256; i++)
341 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
342 f->colorspace = get_symbol(c, state, 0); // YUV cs type
343 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
344 get_rac(c, state); // no chroma = false
345 f->chroma_h_shift = get_symbol(c, state, 0);
346 f->chroma_v_shift = get_symbol(c, state, 0);
347 get_rac(c, state); // transparency plane
348 f->plane_count = 2;
349 f->num_h_slices = 1 + get_symbol(c, state, 0);
350 f->num_v_slices = 1 + get_symbol(c, state, 0);
351
352 if (f->num_h_slices > (unsigned)f->width ||
353 f->num_v_slices > (unsigned)f->height) {
354 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
4a2a4524 355 return AVERROR_INVALIDDATA;
71f7b22d
LB
356 }
357
358 f->quant_table_count = get_symbol(c, state, 0);
359
360 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
4a2a4524 361 return AVERROR_INVALIDDATA;
71f7b22d
LB
362
363 for (i = 0; i < f->quant_table_count; i++) {
364 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
365 if (f->context_count[i] < 0) {
366 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
4a2a4524 367 return f->context_count[i];
71f7b22d
LB
368 }
369 }
370
371 if (ffv1_allocate_initial_states(f) < 0)
372 return AVERROR(ENOMEM);
373
374 for (i = 0; i < f->quant_table_count; i++)
375 if (get_rac(c, state))
376 for (j = 0; j < f->context_count[i]; j++)
377 for (k = 0; k < CONTEXT_SIZE; k++) {
378 int pred = j ? f->initial_states[i][j - 1][k] : 128;
379 f->initial_states[i][j][k] =
380 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
381 }
382 return 0;
383}
384
385static int read_header(FFV1Context *f)
386{
387 uint8_t state[CONTEXT_SIZE];
388 int i, j, context_count;
389 RangeCoder *const c = &f->slice_context[0]->c;
390
391 memset(state, 128, sizeof(state));
392
393 if (f->version < 2) {
394 f->version = get_symbol(c, state, 0);
395 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
396 if (f->ac > 1)
397 for (i = 1; i < 256; i++)
398 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
399 f->colorspace = get_symbol(c, state, 0); // YUV cs type
400 if (f->version > 0)
401 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
402 get_rac(c, state); // no chroma = false
403 f->chroma_h_shift = get_symbol(c, state, 0);
404 f->chroma_v_shift = get_symbol(c, state, 0);
405 get_rac(c, state); // transparency plane
406 f->plane_count = 2;
407 }
408
409 if (f->colorspace == 0) {
410 if (f->avctx->bits_per_raw_sample <= 8) {
411 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
412 case 0x00:
413 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
414 break;
415 case 0x10:
416 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
417 break;
418 case 0x11:
419 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
420 break;
421 case 0x20:
422 f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
423 break;
424 case 0x22:
425 f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
426 break;
427 default:
428 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
4a2a4524 429 return AVERROR(ENOSYS);
71f7b22d
LB
430 }
431 } else {
432 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
433 case 0x00:
434 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
435 break;
436 case 0x10:
437 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
438 break;
439 case 0x11:
440 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
441 break;
442 default:
443 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
4a2a4524 444 return AVERROR(ENOSYS);
71f7b22d
LB
445 }
446 }
447 } else if (f->colorspace == 1) {
448 if (f->chroma_h_shift || f->chroma_v_shift) {
449 av_log(f->avctx, AV_LOG_ERROR,
450 "chroma subsampling not supported in this colorspace\n");
4a2a4524 451 return AVERROR(ENOSYS);
71f7b22d
LB
452 }
453 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
454 } else {
455 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
4a2a4524 456 return AVERROR(ENOSYS);
71f7b22d
LB
457 }
458
459 av_dlog(f->avctx, "%d %d %d\n",
460 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
461
462 if (f->version < 2) {
463 context_count = read_quant_tables(c, f->quant_table);
464 if (context_count < 0) {
465 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
4a2a4524 466 return context_count;
71f7b22d
LB
467 }
468 } else {
469 f->slice_count = get_symbol(c, state, 0);
470 if (f->slice_count > (unsigned)MAX_SLICES)
4a2a4524 471 return AVERROR_INVALIDDATA;
71f7b22d
LB
472 }
473
474 for (j = 0; j < f->slice_count; j++) {
475 FFV1Context *fs = f->slice_context[j];
476 fs->ac = f->ac;
477
478 if (f->version >= 2) {
479 fs->slice_x = get_symbol(c, state, 0) * f->width;
480 fs->slice_y = get_symbol(c, state, 0) * f->height;
481 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
482 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
483
484 fs->slice_x /= f->num_h_slices;
485 fs->slice_y /= f->num_v_slices;
486 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
487 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
488 if ((unsigned)fs->slice_width > f->width ||
489 (unsigned)fs->slice_height > f->height)
4a2a4524 490 return AVERROR_INVALIDDATA;
71f7b22d
LB
491 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
492 (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
4a2a4524 493 return AVERROR_INVALIDDATA;
71f7b22d
LB
494 }
495
496 for (i = 0; i < f->plane_count; i++) {
497 PlaneContext *const p = &fs->plane[i];
498
499 if (f->version >= 2) {
500 int idx = get_symbol(c, state, 0);
501 if (idx > (unsigned)f->quant_table_count) {
502 av_log(f->avctx, AV_LOG_ERROR,
503 "quant_table_index out of range\n");
4a2a4524 504 return AVERROR_INVALIDDATA;
71f7b22d
LB
505 }
506 p->quant_table_index = idx;
507 memcpy(p->quant_table, f->quant_tables[idx],
508 sizeof(p->quant_table));
509 context_count = f->context_count[idx];
510 } else {
511 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
512 }
513
514 if (p->context_count < context_count) {
515 av_freep(&p->state);
516 av_freep(&p->vlc_state);
517 }
518 p->context_count = context_count;
519 }
520 }
521
522 return 0;
523}
524
525static av_cold int ffv1_decode_init(AVCodecContext *avctx)
526{
527 FFV1Context *f = avctx->priv_data;
4a2a4524 528 int ret;
71f7b22d
LB
529
530 ffv1_common_init(avctx);
531
4a2a4524
LB
532 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
533 return ret;
71f7b22d 534
4a2a4524
LB
535 if ((ret = ffv1_init_slice_contexts(f)) < 0)
536 return ret;
71f7b22d
LB
537
538 return 0;
539}
540
541static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
542 int *data_size, AVPacket *avpkt)
543{
544 const uint8_t *buf = avpkt->data;
545 int buf_size = avpkt->size;
546 FFV1Context *f = avctx->priv_data;
547 RangeCoder *const c = &f->slice_context[0]->c;
548 AVFrame *const p = &f->picture;
4a2a4524 549 int bytes_read, i, ret;
71f7b22d
LB
550 uint8_t keystate = 128;
551 const uint8_t *buf_p;
552
553 AVFrame *picture = data;
554
555 /* release previously stored data */
556 if (p->data[0])
557 avctx->release_buffer(avctx, p);
558
559 ff_init_range_decoder(c, buf, buf_size);
560 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
561
562 p->pict_type = AV_PICTURE_TYPE_I; // FIXME: I vs. P
563 if (get_rac(c, &keystate)) {
564 p->key_frame = 1;
4a2a4524
LB
565 if ((ret = read_header(f)) < 0)
566 return ret;
567 if ((ret = ffv1_init_slice_state(f)) < 0)
568 return ret;
71f7b22d
LB
569
570 ffv1_clear_state(f);
571 } else {
572 p->key_frame = 0;
573 }
574 if (f->ac > 1) {
575 int i;
576 for (i = 1; i < 256; i++) {
577 c->one_state[i] = f->state_transition[i];
578 c->zero_state[256 - i] = 256 - c->one_state[i];
579 }
580 }
581
582 p->reference = 0;
4a2a4524 583 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
71f7b22d 584 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
4a2a4524 585 return ret;
71f7b22d
LB
586 }
587
588 if (avctx->debug & FF_DEBUG_PICT_INFO)
589 av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
590
591 if (!f->ac) {
592 bytes_read = c->bytestream - c->bytestream_start - 1;
593 if (bytes_read == 0)
594 av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); // FIXME
595 init_get_bits(&f->slice_context[0]->gb, buf + bytes_read,
596 (buf_size - bytes_read) * 8);
597 } else {
598 bytes_read = 0; /* avoid warning */
599 }
600
601 buf_p = buf + buf_size;
602 for (i = f->slice_count - 1; i > 0; i--) {
603 FFV1Context *fs = f->slice_context[i];
604 int v = AV_RB24(buf_p - 3) + 3;
605 if (buf_p - buf <= v) {
606 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
4a2a4524 607 return AVERROR_INVALIDDATA;
71f7b22d
LB
608 }
609 buf_p -= v;
610 if (fs->ac)
611 ff_init_range_decoder(&fs->c, buf_p, v);
612 else
613 init_get_bits(&fs->gb, buf_p, v * 8);
614 }
615
616 avctx->execute(avctx, decode_slice, &f->slice_context[0],
617 NULL, f->slice_count, sizeof(void *));
618 f->picture_number++;
619
620 *picture = *p;
621 *data_size = sizeof(AVFrame);
622
623 return buf_size;
624}
625
626AVCodec ff_ffv1_decoder = {
627 .name = "ffv1",
628 .type = AVMEDIA_TYPE_VIDEO,
629 .id = AV_CODEC_ID_FFV1,
630 .priv_data_size = sizeof(FFV1Context),
631 .init = ffv1_decode_init,
632 .close = ffv1_close,
633 .decode = ffv1_decode_frame,
634 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
635 CODEC_CAP_SLICE_THREADS,
636 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
637};