4 * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * FF Video Codec 1 (a lossless codec) decoder
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
37 #include "rangecoder.h"
42 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
45 if (get_rac(c, state + 0))
50 while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
54 for (i = e - 1; i >= 0; i--)
55 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
57 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
62 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
64 return get_symbol_inline(c, state, is_signed);
67 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
74 while (i < state->error_sum) { // FIXME: optimize
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);
86 if (k == 0 && 2 * state->drift <= -state->count)
89 v ^= ((2 * state->drift + state->count) >> 31);
92 ret = fold(v + state->bias, bits);
94 update_vlc_state(state, v);
99 static av_always_inline void decode_line(FFV1Context *s, int w,
101 int plane_index, int bits)
103 PlaneContext *const p = &s->plane[plane_index];
104 RangeCoder *const c = &s->c;
108 int run_index = s->run_index;
110 for (x = 0; x < w; x++) {
111 int diff, context, sign;
113 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
120 av_assert2(context < p->context_count);
123 diff = get_symbol_inline(c, p->state[context], 1);
125 if (context == 0 && run_mode == 0)
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)
135 if (ff_log2_run[run_index])
136 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
148 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
155 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
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));
164 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
167 s->run_index = run_index;
170 static void decode_plane(FFV1Context *s, uint8_t *src,
171 int w, int h, int stride, int plane_index)
175 sample[0] = s->sample_buffer + 3;
176 sample[1] = s->sample_buffer + w + 6 + 3;
180 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
182 for (y = 0; y < h; y++) {
183 int16_t *temp = sample[0]; // FIXME: try a normal buffer
185 sample[0] = sample[1];
188 sample[1][-1] = sample[0][0];
189 sample[0][w] = sample[0][w - 1];
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];
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];
203 for (x = 0; x < w; x++)
204 ((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,
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
220 int offset = 1 << bits;
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;
229 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
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
235 sample[p][0] = sample[p][1];
238 sample[p][1][-1] = sample[p][0][0];
239 sample[p][0][w] = sample[p][0][w - 1];
241 decode_line(s, w, sample[p], (p + 1) / 2, 9);
243 decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
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];
258 *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
259 (g << 8) + (r << 16) + (a << 24);
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;
269 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
271 RangeCoder *c = &fs->c;
272 uint8_t state[CONTEXT_SIZE];
273 unsigned ps, i, context_count;
274 memset(state, 128, sizeof(state));
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];
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;
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;
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;
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];
310 if (p->context_count < context_count) {
312 av_freep(&p->vlc_state);
314 p->context_count = context_count;
317 ps = get_symbol(c, state, 0);
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;
327 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
328 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
333 static int decode_slice(AVCodecContext *c, void *arg)
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 & AV_PIX_FMT_FLAG_PLANAR)
339 ? (c->bits_per_raw_sample > 8) + 1
341 AVFrame *const p = f->cur;
343 if (f->version > 2) {
344 if (decode_slice_header(f, fs) < 0) {
345 fs->slice_damaged = 1;
346 return AVERROR_INVALIDDATA;
349 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
351 if (f->cur->key_frame)
352 ffv1_clear_slice_state(f, fs);
353 width = fs->slice_width;
354 height = fs->slice_height;
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);
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],
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],
381 decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
382 chroma_width, chroma_height, p->linesize[2],
385 if (fs->transparency)
386 decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
387 height, p->linesize[3],
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);
395 if (fs->ac && f->version > 2) {
397 get_rac(&fs->c, (uint8_t[]) { 129 });
398 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
400 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
402 fs->slice_damaged = 1;
411 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
415 uint8_t state[CONTEXT_SIZE];
417 memset(state, 128, sizeof(state));
419 for (v = 0; i < 128; v++) {
420 unsigned len = get_symbol(c, state, 0) + 1;
426 quant_table[i] = scale * v;
431 for (i = 1; i < 128; i++)
432 quant_table[256 - i] = -quant_table[i];
433 quant_table[128] = -quant_table[127];
438 static int read_quant_tables(RangeCoder *c,
439 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
442 int context_count = 1;
444 for (i = 0; i < 5; i++) {
445 context_count *= read_quant_table(c, quant_table[i], context_count);
446 if (context_count > 32768U) {
450 return (context_count + 1) / 2;
453 static int read_extra_header(FFV1Context *f)
455 RangeCoder *const c = &f->c;
456 uint8_t state[CONTEXT_SIZE];
458 uint8_t state2[32][CONTEXT_SIZE];
460 memset(state2, 128, sizeof(state2));
461 memset(state, 128, sizeof(state));
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);
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);
471 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
474 for (i = 1; i < 256; i++)
475 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
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);
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;
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;
504 if ((ret = ffv1_allocate_initial_states(f)) < 0)
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;
517 if (f->version > 2) {
518 f->ec = get_symbol(c, state, 0);
521 if (f->version > 2) {
523 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
524 f->avctx->extradata, f->avctx->extradata_size);
526 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
527 return AVERROR_INVALIDDATA;
535 static int read_header(FFV1Context *f)
537 uint8_t state[CONTEXT_SIZE];
538 int i, j, context_count = -1;
539 RangeCoder *const c = &f->slice_context[0]->c;
541 memset(state, 128, sizeof(state));
543 if (f->version < 2) {
544 unsigned v = get_symbol(c, state, 0);
546 av_log(f->avctx, AV_LOG_ERROR,
547 "invalid version %d in version 1 header\n", v);
548 return AVERROR_INVALIDDATA;
552 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
555 for (i = 1; i < 256; i++)
556 f->state_transition[i] =
557 get_symbol(c, state, 1) + c->one_state[i];
560 f->colorspace = get_symbol(c, state, 0); //YUV cs type
563 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
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;
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;
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) {
581 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
584 f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
587 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
590 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
593 f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
596 f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
599 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
600 return AVERROR(ENOSYS);
602 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
603 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
605 f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
608 f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
611 f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
614 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
615 return AVERROR(ENOSYS);
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) {
621 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
624 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
627 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
630 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
631 return AVERROR(ENOSYS);
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) {
637 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
640 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
643 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
646 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
647 return AVERROR(ENOSYS);
650 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
652 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
655 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
658 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
661 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
662 return AVERROR(ENOSYS);
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);
671 switch (f->avctx->bits_per_raw_sample) {
673 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
676 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
679 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
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);
688 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
689 return AVERROR(ENOSYS);
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;
700 } else if (f->version < 3) {
701 f->slice_count = get_symbol(c, state, 0);
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;
707 int trailer = 3 + 5 * !!f->ec;
708 int size = AV_RB24(p - trailer);
709 if (size + trailer > p - c->bytestream_start)
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",
717 return AVERROR_INVALIDDATA;
720 for (j = 0; j < f->slice_count; j++) {
721 FFV1Context *fs = f->slice_context[j];
723 fs->packed_at_lsb = f->packed_at_lsb;
725 fs->slice_damaged = 0;
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;
731 (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
733 (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
735 fs->slice_x /= f->num_h_slices;
736 fs->slice_y /= f->num_v_slices;
737 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
738 fs->slice_height = 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 >
745 return AVERROR_INVALIDDATA;
748 for (i = 0; i < f->plane_count; i++) {
749 PlaneContext *const p = &fs->plane[i];
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;
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];
763 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
766 if (f->version <= 2) {
767 av_assert0(context_count >= 0);
768 if (p->context_count < context_count) {
770 av_freep(&p->vlc_state);
772 p->context_count = context_count;
779 static av_cold int ffv1_decode_init(AVCodecContext *avctx)
781 FFV1Context *f = avctx->priv_data;
784 ffv1_common_init(avctx);
786 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
789 if ((ret = ffv1_init_slice_contexts(f)) < 0)
795 static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
796 int *got_frame, AVPacket *avpkt)
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;
803 uint8_t keystate = 128;
804 const uint8_t *buf_p;
805 AVFrame *const p = data;
809 ff_init_range_decoder(c, buf, buf_size);
810 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
812 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
813 if (get_rac(c, &keystate)) {
816 if ((ret = read_header(f)) < 0)
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;
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");
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);
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;
845 if (i || f->version > 2)
846 v = AV_RB24(buf_p - trailer) + trailer;
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;
856 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
858 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
859 fs->slice_damaged = 1;
864 ff_init_range_decoder(&fs->c, buf_p, v);
866 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
871 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
875 for (i = f->slice_count - 1; i >= 0; i--) {
876 FFV1Context *fs = f->slice_context[i];
878 if (fs->slice_damaged && f->last_picture.data[0]) {
879 const uint8_t *src[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);
890 av_image_copy(dst, p->linesize, (const uint8_t **)src,
891 f->last_picture.linesize,
892 avctx->pix_fmt, fs->slice_width,
899 av_frame_unref(&f->last_picture);
900 if ((ret = av_frame_ref(&f->last_picture, p)) < 0)
909 AVCodec ff_ffv1_decoder = {
911 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
912 .type = AVMEDIA_TYPE_VIDEO,
913 .id = AV_CODEC_ID_FFV1,
914 .priv_data_size = sizeof(FFV1Context),
915 .init = ffv1_decode_init,
917 .decode = ffv1_decode_frame,
918 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
919 CODEC_CAP_SLICE_THREADS,