4 * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * FF Video Codec 1 (a lossless codec) decoder
28 #include "libavutil/avassert.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/timer.h"
37 #include "rangecoder.h"
43 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
46 if (get_rac(c, state + 0))
51 while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
55 for (i = e - 1; i >= 0; i--)
56 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
58 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
63 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
65 return get_symbol_inline(c, state, is_signed);
68 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
75 while (i < state->error_sum) { // FIXME: optimize
80 v = get_sr_golomb(gb, k, 12, bits);
81 av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
82 v, state->bias, state->error_sum, state->drift, state->count, k);
85 if (k == 0 && 2 * state->drift <= -state->count)
88 v ^= ((2 * state->drift + state->count) >> 31);
91 ret = fold(v + state->bias, bits);
93 update_vlc_state(state, v);
98 static av_always_inline void decode_line(FFV1Context *s, int w,
100 int plane_index, int bits)
102 PlaneContext *const p = &s->plane[plane_index];
103 RangeCoder *const c = &s->c;
107 int run_index = s->run_index;
109 for (x = 0; x < w; x++) {
110 int diff, context, sign;
112 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
119 av_assert2(context < p->context_count);
122 diff = get_symbol_inline(c, p->state[context], 1);
124 if (context == 0 && run_mode == 0)
128 if (run_count == 0 && run_mode == 1) {
129 if (get_bits1(&s->gb)) {
130 run_count = 1 << ff_log2_run[run_index];
131 if (x + run_count <= w)
134 if (ff_log2_run[run_index])
135 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
147 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
154 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
156 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
157 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
163 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
166 s->run_index = run_index;
169 static void decode_plane(FFV1Context *s, uint8_t *src,
170 int w, int h, int stride, int plane_index)
174 sample[0] = s->sample_buffer + 3;
175 sample[1] = s->sample_buffer + w + 6 + 3;
179 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
181 for (y = 0; y < h; y++) {
182 int16_t *temp = sample[0]; // FIXME: try a normal buffer
184 sample[0] = sample[1];
187 sample[1][-1] = sample[0][0];
188 sample[0][w] = sample[0][w - 1];
191 if (s->avctx->bits_per_raw_sample <= 8) {
192 decode_line(s, w, sample, plane_index, 8);
193 for (x = 0; x < w; x++)
194 src[x + stride * y] = sample[1][x];
196 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
197 if (s->packed_at_lsb) {
198 for (x = 0; x < w; x++) {
199 ((uint16_t*)(src + stride*y))[x] = sample[1][x];
202 for (x = 0; x < w; x++) {
203 ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
207 // STOP_TIMER("decode-line") }
211 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
214 int16_t *sample[4][2];
215 int lbd = s->avctx->bits_per_raw_sample <= 8;
216 int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 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;
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];
235 sample[p][1][-1]= sample[p][0][0 ];
236 sample[p][0][ w]= sample[p][0][w-1];
238 decode_line(s, w, sample[p], (p + 1)/2, 9);
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];
255 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
257 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
258 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
259 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
265 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
267 RangeCoder *c = &fs->c;
268 uint8_t state[CONTEXT_SIZE];
269 unsigned ps, i, context_count;
270 memset(state, 128, sizeof(state));
272 av_assert0(f->version > 2);
274 fs->slice_x = get_symbol(c, state, 0) * f->width ;
275 fs->slice_y = get_symbol(c, state, 0) * f->height;
276 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
277 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
279 fs->slice_x /= f->num_h_slices;
280 fs->slice_y /= f->num_v_slices;
281 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
282 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
283 if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
285 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
286 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
289 for (i = 0; i < f->plane_count; i++) {
290 PlaneContext * const p = &fs->plane[i];
291 int idx = get_symbol(c, state, 0);
292 if (idx > (unsigned)f->quant_table_count) {
293 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
296 p->quant_table_index = idx;
297 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
298 context_count = f->context_count[idx];
300 if (p->context_count < context_count) {
302 av_freep(&p->vlc_state);
304 p->context_count = context_count;
307 ps = get_symbol(c, state, 0);
309 f->cur->interlaced_frame = 1;
310 f->cur->top_field_first = 1;
311 } else if (ps == 2) {
312 f->cur->interlaced_frame = 1;
313 f->cur->top_field_first = 0;
314 } else if (ps == 3) {
315 f->cur->interlaced_frame = 0;
317 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
318 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
323 static int decode_slice(AVCodecContext *c, void *arg)
325 FFV1Context *fs = *(void **)arg;
326 FFV1Context *f = fs->avctx->priv_data;
327 int width, height, x, y, ret;
328 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
329 AVFrame * const p = f->cur;
331 if (f->version > 2) {
332 if (ffv1_init_slice_state(f, fs) < 0)
333 return AVERROR(ENOMEM);
334 if (decode_slice_header(f, fs) < 0) {
335 fs->slice_damaged = 1;
336 return AVERROR_INVALIDDATA;
339 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
341 if (f->cur->key_frame)
342 ffv1_clear_slice_state(f, fs);
344 width = fs->slice_width;
345 height = fs->slice_height;
350 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
351 get_rac(&fs->c, (uint8_t[]) { 129 });
352 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
353 init_get_bits(&fs->gb,
354 fs->c.bytestream_start + fs->ac_byte_count,
355 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
358 av_assert1(width && height);
359 if (f->colorspace == 0) {
360 const int chroma_width = -((-width) >> f->chroma_h_shift);
361 const int chroma_height = -((-height) >> f->chroma_v_shift);
362 const int cx = x >> f->chroma_h_shift;
363 const int cy = y >> f->chroma_v_shift;
364 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
366 if (f->chroma_planes) {
367 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
368 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
370 if (fs->transparency)
371 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
373 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
374 p->data[1] + ps * x + y * p->linesize[1],
375 p->data[2] + ps * x + y * p->linesize[2] };
376 decode_rgb_frame(fs, planes, width, height, p->linesize);
378 if (fs->ac && f->version > 2) {
380 get_rac(&fs->c, (uint8_t[]) { 129 });
381 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
383 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
384 fs->slice_damaged = 1;
393 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
397 uint8_t state[CONTEXT_SIZE];
399 memset(state, 128, sizeof(state));
401 for (v = 0; i < 128; v++) {
402 unsigned len = get_symbol(c, state, 0) + 1;
405 return AVERROR_INVALIDDATA;
408 quant_table[i] = scale * v;
413 for (i = 1; i < 128; i++)
414 quant_table[256 - i] = -quant_table[i];
415 quant_table[128] = -quant_table[127];
420 static int read_quant_tables(RangeCoder *c,
421 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
424 int context_count = 1;
426 for (i = 0; i < 5; i++) {
427 context_count *= read_quant_table(c, quant_table[i], context_count);
428 if (context_count > 32768U) {
429 return AVERROR_INVALIDDATA;
432 return (context_count + 1) / 2;
435 static int read_extra_header(FFV1Context *f)
437 RangeCoder *const c = &f->c;
438 uint8_t state[CONTEXT_SIZE];
440 uint8_t state2[32][CONTEXT_SIZE];
442 memset(state2, 128, sizeof(state2));
443 memset(state, 128, sizeof(state));
445 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
446 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
448 f->version = get_symbol(c, state, 0);
449 if (f->version > 2) {
450 c->bytestream_end -= 4;
451 f->minor_version = get_symbol(c, state, 0);
453 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
455 for (i = 1; i < 256; i++)
456 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
459 f->colorspace = get_symbol(c, state, 0); //YUV cs type
460 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
461 f->chroma_planes = get_rac(c, state);
462 f->chroma_h_shift = get_symbol(c, state, 0);
463 f->chroma_v_shift = get_symbol(c, state, 0);
464 f->transparency = get_rac(c, state);
465 f->plane_count = 2 + f->transparency;
466 f->num_h_slices = 1 + get_symbol(c, state, 0);
467 f->num_v_slices = 1 + get_symbol(c, state, 0);
469 if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
470 f->num_v_slices > (unsigned)f->height || !f->num_v_slices
472 av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
473 return AVERROR_INVALIDDATA;
476 f->quant_table_count = get_symbol(c, state, 0);
477 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
478 return AVERROR_INVALIDDATA;
480 for (i = 0; i < f->quant_table_count; i++) {
481 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
482 if (f->context_count[i] < 0) {
483 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
484 return AVERROR_INVALIDDATA;
487 if ((ret = ffv1_allocate_initial_states(f)) < 0)
490 for (i = 0; i < f->quant_table_count; i++)
491 if (get_rac(c, state)) {
492 for (j = 0; j < f->context_count[i]; j++)
493 for (k = 0; k < CONTEXT_SIZE; k++) {
494 int pred = j ? f->initial_states[i][j - 1][k] : 128;
495 f->initial_states[i][j][k] =
496 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
500 if (f->version > 2) {
501 f->ec = get_symbol(c, state, 0);
502 if (f->minor_version > 2)
503 f->intra = get_symbol(c, state, 0);
506 if (f->version > 2) {
508 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
509 f->avctx->extradata, f->avctx->extradata_size);
511 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
512 return AVERROR_INVALIDDATA;
519 static int read_header(FFV1Context *f)
521 uint8_t state[CONTEXT_SIZE];
522 int i, j, context_count = -1; //-1 to avoid warning
523 RangeCoder *const c = &f->slice_context[0]->c;
525 memset(state, 128, sizeof(state));
527 if (f->version < 2) {
528 unsigned v= get_symbol(c, state, 0);
530 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
531 return AVERROR_INVALIDDATA;
534 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
536 for (i = 1; i < 256; i++)
537 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
540 f->colorspace = get_symbol(c, state, 0); //YUV cs type
543 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
545 f->chroma_planes = get_rac(c, state);
546 f->chroma_h_shift = get_symbol(c, state, 0);
547 f->chroma_v_shift = get_symbol(c, state, 0);
548 f->transparency = get_rac(c, state);
549 f->plane_count = 2 + f->transparency;
552 if (f->colorspace == 0) {
553 if (!f->transparency && !f->chroma_planes) {
554 if (f->avctx->bits_per_raw_sample <= 8)
555 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
557 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
558 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
559 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
560 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
561 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
562 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
563 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
564 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
565 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
567 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
568 return AVERROR(ENOSYS);
570 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
571 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
572 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
573 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
574 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
576 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
577 return AVERROR(ENOSYS);
579 } else if (f->avctx->bits_per_raw_sample == 9) {
580 f->packed_at_lsb = 1;
581 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
582 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
583 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
584 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
586 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
587 return AVERROR(ENOSYS);
589 } else if (f->avctx->bits_per_raw_sample == 10) {
590 f->packed_at_lsb = 1;
591 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
592 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
593 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
594 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
596 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
597 return AVERROR(ENOSYS);
600 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
601 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
602 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
603 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
605 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
606 return AVERROR(ENOSYS);
609 } else if (f->colorspace == 1) {
610 if (f->chroma_h_shift || f->chroma_v_shift) {
611 av_log(f->avctx, AV_LOG_ERROR,
612 "chroma subsampling not supported in this colorspace\n");
613 return AVERROR(ENOSYS);
615 if ( f->avctx->bits_per_raw_sample == 9)
616 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
617 else if (f->avctx->bits_per_raw_sample == 10)
618 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
619 else if (f->avctx->bits_per_raw_sample == 12)
620 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
621 else if (f->avctx->bits_per_raw_sample == 14)
622 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
624 if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
625 else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
627 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
628 return AVERROR(ENOSYS);
631 av_dlog(f->avctx, "%d %d %d\n",
632 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
633 if (f->version < 2) {
634 context_count = read_quant_tables(c, f->quant_table);
635 if (context_count < 0) {
636 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
637 return AVERROR_INVALIDDATA;
639 } else if (f->version < 3) {
640 f->slice_count = get_symbol(c, state, 0);
642 const uint8_t *p = c->bytestream_end;
643 for (f->slice_count = 0;
644 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
646 int trailer = 3 + 5*!!f->ec;
647 int size = AV_RB24(p-trailer);
648 if (size + trailer > p - c->bytestream_start)
653 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
654 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
655 return AVERROR_INVALIDDATA;
658 for (j = 0; j < f->slice_count; j++) {
659 FFV1Context *fs = f->slice_context[j];
661 fs->packed_at_lsb = f->packed_at_lsb;
663 fs->slice_damaged = 0;
665 if (f->version == 2) {
666 fs->slice_x = get_symbol(c, state, 0) * f->width ;
667 fs->slice_y = get_symbol(c, state, 0) * f->height;
668 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
669 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
671 fs->slice_x /= f->num_h_slices;
672 fs->slice_y /= f->num_v_slices;
673 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
674 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
675 if ((unsigned)fs->slice_width > f->width ||
676 (unsigned)fs->slice_height > f->height)
677 return AVERROR_INVALIDDATA;
678 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
679 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
680 return AVERROR_INVALIDDATA;
683 for (i = 0; i < f->plane_count; i++) {
684 PlaneContext *const p = &fs->plane[i];
686 if (f->version == 2) {
687 int idx = get_symbol(c, state, 0);
688 if (idx > (unsigned)f->quant_table_count) {
689 av_log(f->avctx, AV_LOG_ERROR,
690 "quant_table_index out of range\n");
691 return AVERROR_INVALIDDATA;
693 p->quant_table_index = idx;
694 memcpy(p->quant_table, f->quant_tables[idx],
695 sizeof(p->quant_table));
696 context_count = f->context_count[idx];
698 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
701 if (f->version <= 2) {
702 av_assert0(context_count >= 0);
703 if (p->context_count < context_count) {
705 av_freep(&p->vlc_state);
707 p->context_count = context_count;
714 static av_cold int decode_init(AVCodecContext *avctx)
716 FFV1Context *f = avctx->priv_data;
719 if ((ret = ffv1_common_init(avctx)) < 0)
722 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
725 if ((ret = ffv1_init_slice_contexts(f)) < 0)
731 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
733 const uint8_t *buf = avpkt->data;
734 int buf_size = avpkt->size;
735 FFV1Context *f = avctx->priv_data;
736 RangeCoder *const c = &f->slice_context[0]->c;
737 ThreadFrame frame = { .f = data };
739 uint8_t keystate = 128;
740 const uint8_t *buf_p;
741 AVFrame *const p = data;
746 ff_init_range_decoder(c, buf, buf_size);
747 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
749 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
750 if (get_rac(c, &keystate)) {
753 if ((ret = read_header(f)) < 0)
757 if (!f->key_frame_ok) {
758 av_log(avctx, AV_LOG_ERROR,
759 "Cannot decode non-keyframe without valid keyframe\n");
760 return AVERROR_INVALIDDATA;
765 if ((ret = ff_thread_get_buffer(avctx, &frame, AV_GET_BUFFER_FLAG_REF)) < 0)
768 if (avctx->debug & FF_DEBUG_PICT_INFO)
769 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
770 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
772 buf_p = buf + buf_size;
773 for (i = f->slice_count - 1; i >= 0; i--) {
774 FFV1Context *fs = f->slice_context[i];
775 int trailer = 3 + 5*!!f->ec;
778 if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
779 else v = buf_p - c->bytestream_start;
780 if (buf_p - c->bytestream_start < v) {
781 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
782 return AVERROR_INVALIDDATA;
787 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
789 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
790 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
791 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
792 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
793 } else if (ts != AV_NOPTS_VALUE) {
794 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
796 av_log(f->avctx, AV_LOG_ERROR, "\n");
798 fs->slice_damaged = 1;
803 ff_init_range_decoder(&fs->c, buf_p, v);
805 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
811 avctx->execute(avctx,
813 &f->slice_context[0],
818 for (i = f->slice_count - 1; i >= 0; i--) {
819 FFV1Context *fs = f->slice_context[i];
821 if (fs->slice_damaged && f->last_picture.data[0]) {
822 const uint8_t *src[4];
824 for (j = 0; j < 4; j++) {
825 int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
826 int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
827 dst[j] = p->data[j] + p->linesize[j]*
828 (fs->slice_y>>sv) + (fs->slice_x>>sh);
829 src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
830 (fs->slice_y>>sv) + (fs->slice_x>>sh);
832 av_image_copy(dst, p->linesize, (const uint8_t **)src,
833 f->last_picture.linesize,
842 av_frame_unref(&f->last_picture);
843 if ((ret = av_frame_ref(&f->last_picture, p)) < 0)
852 static int init_thread_copy(AVCodecContext *avctx)
854 FFV1Context *f = avctx->priv_data;
857 for (i = 0; i < f->quant_table_count; i++) {
858 void *p = f->initial_states[i];
859 f->initial_states[i] = av_malloc(f->context_count[i] * sizeof(*f->initial_states[i]));
860 if (!f->initial_states[i])
861 return AVERROR(ENOMEM);
862 memcpy(f->initial_states[i], p, f->context_count[i] * sizeof(*f->initial_states[i]));
865 if ((ret = ffv1_init_slice_contexts(f)) < 0)
870 AVCodec ff_ffv1_decoder = {
872 .type = AVMEDIA_TYPE_VIDEO,
873 .id = AV_CODEC_ID_FFV1,
874 .priv_data_size = sizeof(FFV1Context),
877 .decode = decode_frame,
878 .init_thread_copy = init_thread_copy,
879 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
880 CODEC_CAP_SLICE_THREADS,
881 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),