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"
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
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);
504 if (f->version > 2) {
506 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
507 f->avctx->extradata, f->avctx->extradata_size);
509 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
510 return AVERROR_INVALIDDATA;
517 static int read_header(FFV1Context *f)
519 uint8_t state[CONTEXT_SIZE];
520 int i, j, context_count = -1; //-1 to avoid warning
521 RangeCoder *const c = &f->slice_context[0]->c;
523 memset(state, 128, sizeof(state));
525 if (f->version < 2) {
526 unsigned v= get_symbol(c, state, 0);
528 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
529 return AVERROR_INVALIDDATA;
532 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
534 for (i = 1; i < 256; i++)
535 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
538 f->colorspace = get_symbol(c, state, 0); //YUV cs type
541 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
543 f->chroma_planes = get_rac(c, state);
544 f->chroma_h_shift = get_symbol(c, state, 0);
545 f->chroma_v_shift = get_symbol(c, state, 0);
546 f->transparency = get_rac(c, state);
547 f->plane_count = 2 + f->transparency;
550 if (f->colorspace == 0) {
551 if (!f->transparency && !f->chroma_planes) {
552 if (f->avctx->bits_per_raw_sample <= 8)
553 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
555 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
556 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
557 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
558 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
559 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
560 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
561 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
562 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
563 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
565 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
566 return AVERROR(ENOSYS);
568 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
569 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
570 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
571 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
572 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
574 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
575 return AVERROR(ENOSYS);
577 } else if (f->avctx->bits_per_raw_sample == 9) {
578 f->packed_at_lsb = 1;
579 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
580 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
581 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
582 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
584 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
585 return AVERROR(ENOSYS);
587 } else if (f->avctx->bits_per_raw_sample == 10) {
588 f->packed_at_lsb = 1;
589 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
590 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
591 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
592 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
594 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
595 return AVERROR(ENOSYS);
598 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
599 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
600 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
601 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
603 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
604 return AVERROR(ENOSYS);
607 } else if (f->colorspace == 1) {
608 if (f->chroma_h_shift || f->chroma_v_shift) {
609 av_log(f->avctx, AV_LOG_ERROR,
610 "chroma subsampling not supported in this colorspace\n");
611 return AVERROR(ENOSYS);
613 if ( f->avctx->bits_per_raw_sample == 9)
614 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
615 else if (f->avctx->bits_per_raw_sample == 10)
616 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
617 else if (f->avctx->bits_per_raw_sample == 12)
618 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
619 else if (f->avctx->bits_per_raw_sample == 14)
620 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
622 if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
623 else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
625 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
626 return AVERROR(ENOSYS);
629 av_dlog(f->avctx, "%d %d %d\n",
630 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
631 if (f->version < 2) {
632 context_count = read_quant_tables(c, f->quant_table);
633 if (context_count < 0) {
634 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
635 return AVERROR_INVALIDDATA;
637 } else if (f->version < 3) {
638 f->slice_count = get_symbol(c, state, 0);
640 const uint8_t *p = c->bytestream_end;
641 for (f->slice_count = 0;
642 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
644 int trailer = 3 + 5*!!f->ec;
645 int size = AV_RB24(p-trailer);
646 if (size + trailer > p - c->bytestream_start)
651 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
652 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
653 return AVERROR_INVALIDDATA;
656 for (j = 0; j < f->slice_count; j++) {
657 FFV1Context *fs = f->slice_context[j];
659 fs->packed_at_lsb = f->packed_at_lsb;
661 fs->slice_damaged = 0;
663 if (f->version == 2) {
664 fs->slice_x = get_symbol(c, state, 0) * f->width ;
665 fs->slice_y = get_symbol(c, state, 0) * f->height;
666 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
667 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
669 fs->slice_x /= f->num_h_slices;
670 fs->slice_y /= f->num_v_slices;
671 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
672 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
673 if ((unsigned)fs->slice_width > f->width ||
674 (unsigned)fs->slice_height > f->height)
675 return AVERROR_INVALIDDATA;
676 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
677 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
678 return AVERROR_INVALIDDATA;
681 for (i = 0; i < f->plane_count; i++) {
682 PlaneContext *const p = &fs->plane[i];
684 if (f->version == 2) {
685 int idx = get_symbol(c, state, 0);
686 if (idx > (unsigned)f->quant_table_count) {
687 av_log(f->avctx, AV_LOG_ERROR,
688 "quant_table_index out of range\n");
689 return AVERROR_INVALIDDATA;
691 p->quant_table_index = idx;
692 memcpy(p->quant_table, f->quant_tables[idx],
693 sizeof(p->quant_table));
694 context_count = f->context_count[idx];
696 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
699 if (f->version <= 2) {
700 av_assert0(context_count >= 0);
701 if (p->context_count < context_count) {
703 av_freep(&p->vlc_state);
705 p->context_count = context_count;
712 static av_cold int decode_init(AVCodecContext *avctx)
714 FFV1Context *f = avctx->priv_data;
717 if ((ret = ffv1_common_init(avctx)) < 0)
720 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
723 if ((ret = ffv1_init_slice_contexts(f)) < 0)
729 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
731 const uint8_t *buf = avpkt->data;
732 int buf_size = avpkt->size;
733 FFV1Context *f = avctx->priv_data;
734 RangeCoder *const c = &f->slice_context[0]->c;
736 uint8_t keystate = 128;
737 const uint8_t *buf_p;
738 AVFrame *const p = data;
742 ff_init_range_decoder(c, buf, buf_size);
743 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
745 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
746 if (get_rac(c, &keystate)) {
749 if ((ret = read_header(f)) < 0)
753 if (!f->key_frame_ok) {
754 av_log(avctx, AV_LOG_ERROR,
755 "Cannot decode non-keyframe without valid keyframe\n");
756 return AVERROR_INVALIDDATA;
761 if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
764 if (avctx->debug & FF_DEBUG_PICT_INFO)
765 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
766 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
768 buf_p = buf + buf_size;
769 for (i = f->slice_count - 1; i >= 0; i--) {
770 FFV1Context *fs = f->slice_context[i];
771 int trailer = 3 + 5*!!f->ec;
774 if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
775 else v = buf_p - c->bytestream_start;
776 if (buf_p - c->bytestream_start < v) {
777 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
778 return AVERROR_INVALIDDATA;
783 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
785 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
786 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
787 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
788 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
789 } else if (ts != AV_NOPTS_VALUE) {
790 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
792 av_log(f->avctx, AV_LOG_ERROR, "\n");
794 fs->slice_damaged = 1;
799 ff_init_range_decoder(&fs->c, buf_p, v);
801 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
806 avctx->execute(avctx,
808 &f->slice_context[0],
813 for (i = f->slice_count - 1; i >= 0; i--) {
814 FFV1Context *fs = f->slice_context[i];
816 if (fs->slice_damaged && f->last_picture.data[0]) {
817 const uint8_t *src[4];
819 for (j = 0; j < 4; j++) {
820 int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
821 int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
822 dst[j] = p->data[j] + p->linesize[j]*
823 (fs->slice_y>>sv) + (fs->slice_x>>sh);
824 src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
825 (fs->slice_y>>sv) + (fs->slice_x>>sh);
827 av_image_copy(dst, p->linesize, (const uint8_t **)src,
828 f->last_picture.linesize,
837 av_frame_unref(&f->last_picture);
838 if ((ret = av_frame_ref(&f->last_picture, p)) < 0)
847 AVCodec ff_ffv1_decoder = {
849 .type = AVMEDIA_TYPE_VIDEO,
850 .id = AV_CODEC_ID_FFV1,
851 .priv_data_size = sizeof(FFV1Context),
854 .decode = decode_frame,
855 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
856 CODEC_CAP_SLICE_THREADS,
857 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),