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