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"
38 #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
82 v = get_sr_golomb(gb, k, 12, bits);
83 av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
84 v, state->bias, state->error_sum, state->drift, state->count, k);
87 if (k == 0 && 2 * state->drift <= -state->count)
90 v ^= ((2 * state->drift + state->count) >> 31);
93 ret = fold(v + state->bias, bits);
95 update_vlc_state(state, v);
100 static av_always_inline void decode_line(FFV1Context *s, int w,
102 int plane_index, int bits)
104 PlaneContext *const p = &s->plane[plane_index];
105 RangeCoder *const c = &s->c;
109 int run_index = s->run_index;
111 for (x = 0; x < w; x++) {
112 int diff, context, sign;
114 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
121 av_assert2(context < p->context_count);
124 diff = get_symbol_inline(c, p->state[context], 1);
126 if (context == 0 && run_mode == 0)
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)
136 if (ff_log2_run[run_index])
137 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
149 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
156 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
158 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
159 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
165 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
168 s->run_index = run_index;
171 static void decode_plane(FFV1Context *s, uint8_t *src,
172 int w, int h, int stride, int plane_index)
176 sample[0] = s->sample_buffer + 3;
177 sample[1] = s->sample_buffer + w + 6 + 3;
181 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
183 for (y = 0; y < h; y++) {
184 int16_t *temp = sample[0]; // FIXME: try a normal buffer
186 sample[0] = sample[1];
189 sample[1][-1] = sample[0][0];
190 sample[0][w] = sample[0][w - 1];
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];
198 decode_line(s, w, sample, plane_index,
199 s->avctx->bits_per_raw_sample);
200 if (s->packed_at_lsb) {
201 for (x = 0; x < w; x++)
202 ((uint16_t *)(src + stride * y))[x] = sample[1][x];
204 for (x = 0; x < w; x++)
205 ((uint16_t *)(src + stride * y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
208 // STOP_TIMER("decode-line") }
212 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
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
221 int offset = 1 << bits;
223 for (x = 0; x < 4; x++) {
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;
230 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
232 for (y = 0; y < h; y++) {
233 for (p = 0; p < 3 + s->transparency; p++) {
234 int16_t *temp = sample[p][0]; //FIXME try a normal buffer
236 sample[p][0] = sample[p][1];
239 sample[p][1][-1] = sample[p][0][0];
240 sample[p][0][w] = sample[p][0][w - 1];
242 decode_line(s, w, sample[p], (p + 1) / 2, 9);
244 decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
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];
250 int a = sample[3][1][x];
259 *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
260 (g << 8) + (r << 16) + (a << 24);
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;
270 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
272 RangeCoder *c = &fs->c;
273 uint8_t state[CONTEXT_SIZE];
274 unsigned ps, i, context_count;
275 memset(state, 128, sizeof(state));
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];
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;
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;
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;
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];
311 if (p->context_count < context_count) {
313 av_freep(&p->vlc_state);
315 p->context_count = context_count;
318 ps = get_symbol(c, state, 0);
320 f->picture.interlaced_frame = 1;
321 f->picture.top_field_first = 1;
322 } else if (ps == 2) {
323 f->picture.interlaced_frame = 1;
324 f->picture.top_field_first = 0;
325 } else if (ps == 3) {
326 f->picture.interlaced_frame = 0;
328 f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
329 f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
334 static int decode_slice(AVCodecContext *c, void *arg)
336 FFV1Context *fs = *(void **)arg;
337 FFV1Context *f = fs->avctx->priv_data;
338 int width, height, x, y, ret;
339 const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
340 ? (c->bits_per_raw_sample > 8) + 1
342 AVFrame *const p = &f->picture;
344 if (f->version > 2) {
345 if (decode_slice_header(f, fs) < 0) {
346 fs->slice_damaged = 1;
347 return AVERROR_INVALIDDATA;
350 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
352 if (f->picture.key_frame)
353 ffv1_clear_slice_state(f, fs);
354 width = fs->slice_width;
355 height = fs->slice_height;
360 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
361 get_rac(&fs->c, (uint8_t[]) { 129 });
362 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
363 init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,
364 (fs->c.bytestream_end - fs->c.bytestream_start -
365 fs->ac_byte_count) * 8);
368 av_assert1(width && height);
369 if (f->colorspace == 0) {
370 const int chroma_width = -((-width) >> f->chroma_h_shift);
371 const int chroma_height = -((-height) >> f->chroma_v_shift);
372 const int cx = x >> f->chroma_h_shift;
373 const int cy = y >> f->chroma_v_shift;
374 decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
375 height, p->linesize[0],
378 if (f->chroma_planes) {
379 decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
380 chroma_width, chroma_height, p->linesize[1],
382 decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
383 chroma_width, chroma_height, p->linesize[2],
386 if (fs->transparency)
387 decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
388 height, p->linesize[3],
391 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
392 p->data[1] + ps * x + y * p->linesize[1],
393 p->data[2] + ps * x + y * p->linesize[2] };
394 decode_rgb_frame(fs, planes, width, height, p->linesize);
396 if (fs->ac && f->version > 2) {
398 get_rac(&fs->c, (uint8_t[]) { 129 });
399 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
401 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
403 fs->slice_damaged = 1;
412 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
416 uint8_t state[CONTEXT_SIZE];
418 memset(state, 128, sizeof(state));
420 for (v = 0; i < 128; v++) {
421 unsigned len = get_symbol(c, state, 0) + 1;
427 quant_table[i] = scale * v;
432 for (i = 1; i < 128; i++)
433 quant_table[256 - i] = -quant_table[i];
434 quant_table[128] = -quant_table[127];
439 static int read_quant_tables(RangeCoder *c,
440 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
443 int context_count = 1;
445 for (i = 0; i < 5; i++) {
446 context_count *= read_quant_table(c, quant_table[i], context_count);
447 if (context_count > 32768U) {
451 return (context_count + 1) / 2;
454 static int read_extra_header(FFV1Context *f)
456 RangeCoder *const c = &f->c;
457 uint8_t state[CONTEXT_SIZE];
459 uint8_t state2[32][CONTEXT_SIZE];
461 memset(state2, 128, sizeof(state2));
462 memset(state, 128, sizeof(state));
464 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
465 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
467 f->version = get_symbol(c, state, 0);
468 if (f->version > 2) {
469 c->bytestream_end -= 4;
470 f->minor_version = get_symbol(c, state, 0);
472 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
475 for (i = 1; i < 256; i++)
476 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
479 f->colorspace = get_symbol(c, state, 0); //YUV cs type
480 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
481 f->chroma_planes = get_rac(c, state);
482 f->chroma_h_shift = get_symbol(c, state, 0);
483 f->chroma_v_shift = get_symbol(c, state, 0);
484 f->transparency = get_rac(c, state);
485 f->plane_count = 2 + f->transparency;
486 f->num_h_slices = 1 + get_symbol(c, state, 0);
487 f->num_v_slices = 1 + get_symbol(c, state, 0);
489 if (f->num_h_slices > (unsigned)f->width ||
490 f->num_v_slices > (unsigned)f->height) {
491 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
492 return AVERROR_INVALIDDATA;
495 f->quant_table_count = get_symbol(c, state, 0);
496 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
497 return AVERROR_INVALIDDATA;
498 for (i = 0; i < f->quant_table_count; i++) {
499 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
500 if (f->context_count[i] < 0) {
501 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
502 return AVERROR_INVALIDDATA;
505 if ((ret = ffv1_allocate_initial_states(f)) < 0)
508 for (i = 0; i < f->quant_table_count; i++)
509 if (get_rac(c, state)) {
510 for (j = 0; j < f->context_count[i]; j++)
511 for (k = 0; k < CONTEXT_SIZE; k++) {
512 int pred = j ? f->initial_states[i][j - 1][k] : 128;
513 f->initial_states[i][j][k] =
514 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
518 if (f->version > 2) {
519 f->ec = get_symbol(c, state, 0);
522 if (f->version > 2) {
524 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
525 f->avctx->extradata, f->avctx->extradata_size);
527 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
528 return AVERROR_INVALIDDATA;
536 static int read_header(FFV1Context *f)
538 uint8_t state[CONTEXT_SIZE];
539 int i, j, context_count = -1;
540 RangeCoder *const c = &f->slice_context[0]->c;
542 memset(state, 128, sizeof(state));
544 if (f->version < 2) {
545 unsigned v = get_symbol(c, state, 0);
547 av_log(f->avctx, AV_LOG_ERROR,
548 "invalid version %d in version 1 header\n", v);
549 return AVERROR_INVALIDDATA;
553 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
556 for (i = 1; i < 256; i++)
557 f->state_transition[i] =
558 get_symbol(c, state, 1) + c->one_state[i];
561 f->colorspace = get_symbol(c, state, 0); //YUV cs type
564 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
566 f->chroma_planes = get_rac(c, state);
567 f->chroma_h_shift = get_symbol(c, state, 0);
568 f->chroma_v_shift = get_symbol(c, state, 0);
569 f->transparency = get_rac(c, state);
570 f->plane_count = 2 + f->transparency;
573 if (f->colorspace == 0) {
574 if (!f->transparency && !f->chroma_planes) {
575 if (f->avctx->bits_per_raw_sample <= 8)
576 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
578 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
579 } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
580 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
582 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
585 f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
588 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
591 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
594 f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
597 f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
600 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
601 return AVERROR(ENOSYS);
603 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
604 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
606 f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
609 f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
612 f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
615 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
616 return AVERROR(ENOSYS);
618 } else if (f->avctx->bits_per_raw_sample == 9) {
619 f->packed_at_lsb = 1;
620 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
622 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
625 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
628 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
631 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
632 return AVERROR(ENOSYS);
634 } else if (f->avctx->bits_per_raw_sample == 10) {
635 f->packed_at_lsb = 1;
636 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
638 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
641 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
644 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
647 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
648 return AVERROR(ENOSYS);
651 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
653 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
656 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
659 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
662 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
663 return AVERROR(ENOSYS);
666 } else if (f->colorspace == 1) {
667 if (f->chroma_h_shift || f->chroma_v_shift) {
668 av_log(f->avctx, AV_LOG_ERROR,
669 "chroma subsampling not supported in this colorspace\n");
670 return AVERROR(ENOSYS);
672 switch (f->avctx->bits_per_raw_sample) {
674 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
677 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
680 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
683 av_log(f->avctx, AV_LOG_ERROR,
684 "bit depth %d not supported\n",
685 f->avctx->bits_per_raw_sample);
686 return AVERROR(ENOSYS);
689 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
690 return AVERROR(ENOSYS);
693 av_dlog(f->avctx, "%d %d %d\n",
694 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
695 if (f->version < 2) {
696 context_count = read_quant_tables(c, f->quant_table);
697 if (context_count < 0) {
698 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
699 return AVERROR_INVALIDDATA;
701 } else if (f->version < 3) {
702 f->slice_count = get_symbol(c, state, 0);
704 const uint8_t *p = c->bytestream_end;
705 for (f->slice_count = 0;
706 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
708 int trailer = 3 + 5 * !!f->ec;
709 int size = AV_RB24(p - trailer);
710 if (size + trailer > p - c->bytestream_start)
715 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
716 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
718 return AVERROR_INVALIDDATA;
721 for (j = 0; j < f->slice_count; j++) {
722 FFV1Context *fs = f->slice_context[j];
724 fs->packed_at_lsb = f->packed_at_lsb;
726 fs->slice_damaged = 0;
728 if (f->version == 2) {
729 fs->slice_x = get_symbol(c, state, 0) * f->width;
730 fs->slice_y = get_symbol(c, state, 0) * f->height;
732 (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
734 (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
736 fs->slice_x /= f->num_h_slices;
737 fs->slice_y /= f->num_v_slices;
738 fs->slice_width /= f->num_h_slices - fs->slice_x;
739 fs->slice_height /= f->num_v_slices - fs->slice_y;
740 if ((unsigned)fs->slice_width > f->width ||
741 (unsigned)fs->slice_height > f->height)
742 return AVERROR_INVALIDDATA;
743 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
744 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
746 return AVERROR_INVALIDDATA;
749 for (i = 0; i < f->plane_count; i++) {
750 PlaneContext *const p = &fs->plane[i];
752 if (f->version == 2) {
753 int idx = get_symbol(c, state, 0);
754 if (idx > (unsigned)f->quant_table_count) {
755 av_log(f->avctx, AV_LOG_ERROR,
756 "quant_table_index out of range\n");
757 return AVERROR_INVALIDDATA;
759 p->quant_table_index = idx;
760 memcpy(p->quant_table, f->quant_tables[idx],
761 sizeof(p->quant_table));
762 context_count = f->context_count[idx];
764 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
767 if (f->version <= 2) {
768 av_assert0(context_count >= 0);
769 if (p->context_count < context_count) {
771 av_freep(&p->vlc_state);
773 p->context_count = context_count;
780 static av_cold int ffv1_decode_init(AVCodecContext *avctx)
782 FFV1Context *f = avctx->priv_data;
785 ffv1_common_init(avctx);
787 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
790 if ((ret = ffv1_init_slice_contexts(f)) < 0)
796 static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
797 int *got_frame, AVPacket *avpkt)
799 const uint8_t *buf = avpkt->data;
800 int buf_size = avpkt->size;
801 FFV1Context *f = avctx->priv_data;
802 RangeCoder *const c = &f->slice_context[0]->c;
803 AVFrame *const p = &f->picture;
805 uint8_t keystate = 128;
806 const uint8_t *buf_p;
808 AVFrame *picture = data;
810 /* release previously stored data */
812 avctx->release_buffer(avctx, p);
814 ff_init_range_decoder(c, buf, buf_size);
815 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
817 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
818 if (get_rac(c, &keystate)) {
821 if ((ret = read_header(f)) < 0)
825 if (!f->key_frame_ok) {
826 av_log(avctx, AV_LOG_ERROR,
827 "Cannot decode non-keyframe without valid keyframe\n");
828 return AVERROR_INVALIDDATA;
833 p->reference = 3; //for error concealment
834 if ((ret = ff_get_buffer(avctx, p)) < 0) {
835 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
839 if (avctx->debug & FF_DEBUG_PICT_INFO)
840 av_log(avctx, AV_LOG_DEBUG,
841 "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
842 f->version, p->key_frame, f->ac, f->ec, f->slice_count,
843 f->avctx->bits_per_raw_sample);
845 buf_p = buf + buf_size;
846 for (i = f->slice_count - 1; i >= 0; i--) {
847 FFV1Context *fs = f->slice_context[i];
848 int trailer = 3 + 5 * !!f->ec;
851 if (i || f->version > 2)
852 v = AV_RB24(buf_p - trailer) + trailer;
854 v = buf_p - c->bytestream_start;
855 if (buf_p - c->bytestream_start < v) {
856 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
857 return AVERROR_INVALIDDATA;
862 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
864 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
865 fs->slice_damaged = 1;
870 ff_init_range_decoder(&fs->c, buf_p, v);
872 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
875 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
879 for (i = f->slice_count - 1; i >= 0; i--) {
880 FFV1Context *fs = f->slice_context[i];
882 if (fs->slice_damaged && f->last_picture.data[0]) {
883 const uint8_t *src[4];
885 for (j = 0; j < 4; j++) {
886 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
887 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
888 dst[j] = f->picture.data[j] + f->picture.linesize[j] *
889 (fs->slice_y >> sv) + (fs->slice_x >> sh);
890 src[j] = f->last_picture.data[j] +
891 f->last_picture.linesize[j] *
892 (fs->slice_y >> sv) + (fs->slice_x >> sh);
894 av_image_copy(dst, f->picture.linesize, (const uint8_t **)src,
895 f->last_picture.linesize,
896 avctx->pix_fmt, fs->slice_width,
906 FFSWAP(AVFrame, f->picture, f->last_picture);
911 AVCodec ff_ffv1_decoder = {
913 .type = AVMEDIA_TYPE_VIDEO,
914 .id = AV_CODEC_ID_FFV1,
915 .priv_data_size = sizeof(FFV1Context),
916 .init = ffv1_decode_init,
918 .decode = ffv1_decode_frame,
919 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
920 CODEC_CAP_SLICE_THREADS,
921 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),