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"
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
82 v = get_sr_golomb(gb, k, 12, bits);
83 ff_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);
86 v ^= ((2 * state->drift + state->count) >> 31);
88 ret = fold(v + state->bias, bits);
90 update_vlc_state(state, v);
95 static av_always_inline void decode_line(FFV1Context *s, int w,
97 int plane_index, int bits)
99 PlaneContext *const p = &s->plane[plane_index];
100 RangeCoder *const c = &s->c;
104 int run_index = s->run_index;
106 for (x = 0; x < w; x++) {
107 int diff, context, sign;
109 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
116 av_assert2(context < p->context_count);
118 if (s->ac != AC_GOLOMB_RICE) {
119 diff = get_symbol_inline(c, p->state[context], 1);
121 if (context == 0 && run_mode == 0)
125 if (run_count == 0 && run_mode == 1) {
126 if (get_bits1(&s->gb)) {
127 run_count = 1 << ff_log2_run[run_index];
128 if (x + run_count <= w)
131 if (ff_log2_run[run_index])
132 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
144 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
151 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
153 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
154 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
160 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
163 s->run_index = run_index;
166 static void decode_plane(FFV1Context *s, uint8_t *src,
167 int w, int h, int stride, int plane_index)
171 sample[0] = s->sample_buffer + 3;
172 sample[1] = s->sample_buffer + w + 6 + 3;
176 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
178 for (y = 0; y < h; y++) {
179 int16_t *temp = sample[0]; // FIXME: try a normal buffer
181 sample[0] = sample[1];
184 sample[1][-1] = sample[0][0];
185 sample[0][w] = sample[0][w - 1];
188 if (s->avctx->bits_per_raw_sample <= 8) {
189 decode_line(s, w, sample, plane_index, 8);
190 for (x = 0; x < w; x++)
191 src[x + stride * y] = sample[1][x];
193 decode_line(s, w, sample, plane_index,
194 s->avctx->bits_per_raw_sample);
195 if (s->packed_at_lsb) {
196 for (x = 0; x < w; x++)
197 ((uint16_t *)(src + stride * y))[x] = sample[1][x];
199 for (x = 0; x < w; x++)
200 ((uint16_t *)(src + stride * y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
203 // STOP_TIMER("decode-line") }
207 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
211 int16_t *sample[4][2];
212 int lbd = s->avctx->bits_per_raw_sample <= 8;
213 int bits = s->avctx->bits_per_raw_sample > 0
214 ? s->avctx->bits_per_raw_sample
216 int offset = 1 << bits;
218 for (x = 0; x < 4; x++) {
219 sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
220 sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
225 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
227 for (y = 0; y < h; y++) {
228 for (p = 0; p < 3 + s->transparency; p++) {
229 int16_t *temp = sample[p][0]; //FIXME try a normal buffer
231 sample[p][0] = sample[p][1];
234 sample[p][1][-1] = sample[p][0][0];
235 sample[p][0][w] = sample[p][0][w - 1];
237 decode_line(s, w, sample[p], (p + 1) / 2, 9);
239 decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
241 for (x = 0; x < w; x++) {
242 int g = sample[0][1][x];
243 int b = sample[1][1][x];
244 int r = sample[2][1][x];
245 int a = sample[3][1][x];
254 *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
255 (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 if (fs->ac == AC_RANGE_CUSTOM_TAB) {
273 for (i = 1; i < 256; i++) {
274 fs->c.one_state[i] = f->state_transition[i];
275 fs->c.zero_state[256 - i] = 256 - fs->c.one_state[i];
279 fs->slice_x = get_symbol(c, state, 0) * f->width;
280 fs->slice_y = get_symbol(c, state, 0) * f->height;
281 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
282 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
284 fs->slice_x /= f->num_h_slices;
285 fs->slice_y /= f->num_v_slices;
286 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
287 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
288 if ((unsigned)fs->slice_width > f->width ||
289 (unsigned)fs->slice_height > f->height)
290 return AVERROR_INVALIDDATA;
291 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
292 (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
293 return AVERROR_INVALIDDATA;
295 for (i = 0; i < f->plane_count; i++) {
296 PlaneContext *const p = &fs->plane[i];
297 int idx = get_symbol(c, state, 0);
298 if (idx > (unsigned)f->quant_table_count) {
299 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
300 return AVERROR_INVALIDDATA;
302 p->quant_table_index = idx;
303 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
304 context_count = f->context_count[idx];
306 if (p->context_count < context_count) {
308 av_freep(&p->vlc_state);
310 p->context_count = context_count;
313 ps = get_symbol(c, state, 0);
315 f->cur->interlaced_frame = 1;
316 f->cur->top_field_first = 1;
317 } else if (ps == 2) {
318 f->cur->interlaced_frame = 1;
319 f->cur->top_field_first = 0;
320 } else if (ps == 3) {
321 f->cur->interlaced_frame = 0;
323 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
324 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
326 if (av_image_check_sar(f->width, f->height,
327 f->cur->sample_aspect_ratio) < 0) {
328 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
329 f->cur->sample_aspect_ratio.num,
330 f->cur->sample_aspect_ratio.den);
331 f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
337 static int decode_slice(AVCodecContext *c, void *arg)
339 FFV1Context *fs = *(void **)arg;
340 FFV1Context *f = fs->avctx->priv_data;
341 int width, height, x, y, ret;
342 const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)
343 ? (c->bits_per_raw_sample > 8) + 1
345 AVFrame *const p = f->cur;
347 if (f->version > 2) {
348 if (decode_slice_header(f, fs) < 0) {
349 fs->slice_damaged = 1;
350 return AVERROR_INVALIDDATA;
353 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
355 if (f->cur->key_frame)
356 ffv1_clear_slice_state(f, fs);
357 width = fs->slice_width;
358 height = fs->slice_height;
362 if (fs->ac == AC_GOLOMB_RICE) {
363 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
364 get_rac(&fs->c, (uint8_t[]) { 129 });
365 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
366 init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,
367 (fs->c.bytestream_end - fs->c.bytestream_start -
368 fs->ac_byte_count) * 8);
371 av_assert1(width && height);
372 if (f->colorspace == 0) {
373 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
374 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
375 const int cx = x >> f->chroma_h_shift;
376 const int cy = y >> f->chroma_v_shift;
377 decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
378 height, p->linesize[0],
381 if (f->chroma_planes) {
382 decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
383 chroma_width, chroma_height, p->linesize[1],
385 decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
386 chroma_width, chroma_height, p->linesize[2],
389 if (fs->transparency)
390 decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
391 height, p->linesize[3],
394 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
395 p->data[1] + ps * x + y * p->linesize[1],
396 p->data[2] + ps * x + y * p->linesize[2] };
397 decode_rgb_frame(fs, planes, width, height, p->linesize);
399 if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
401 get_rac(&fs->c, (uint8_t[]) { 129 });
402 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
404 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
406 fs->slice_damaged = 1;
415 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
419 uint8_t state[CONTEXT_SIZE];
421 memset(state, 128, sizeof(state));
423 for (v = 0; i < 128; v++) {
424 unsigned len = get_symbol(c, state, 0) + 1;
430 quant_table[i] = scale * v;
435 for (i = 1; i < 128; i++)
436 quant_table[256 - i] = -quant_table[i];
437 quant_table[128] = -quant_table[127];
442 static int read_quant_tables(RangeCoder *c,
443 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
446 int context_count = 1;
448 for (i = 0; i < 5; i++) {
449 context_count *= read_quant_table(c, quant_table[i], context_count);
450 if (context_count > 32768U) {
454 return (context_count + 1) / 2;
457 static int read_extra_header(FFV1Context *f)
459 RangeCoder *const c = &f->c;
460 uint8_t state[CONTEXT_SIZE];
462 uint8_t state2[32][CONTEXT_SIZE];
464 memset(state2, 128, sizeof(state2));
465 memset(state, 128, sizeof(state));
467 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
468 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
470 f->version = get_symbol(c, state, 0);
471 if (f->version > 2) {
472 c->bytestream_end -= 4;
473 f->minor_version = get_symbol(c, state, 0);
475 f->ac = get_symbol(c, state, 0);
477 if (f->ac == AC_RANGE_CUSTOM_TAB) {
478 for (i = 1; i < 256; i++)
479 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
482 f->colorspace = get_symbol(c, state, 0); //YUV cs type
483 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
484 f->chroma_planes = get_rac(c, state);
485 f->chroma_h_shift = get_symbol(c, state, 0);
486 f->chroma_v_shift = get_symbol(c, state, 0);
487 f->transparency = get_rac(c, state);
488 f->plane_count = 2 + f->transparency;
489 f->num_h_slices = 1 + get_symbol(c, state, 0);
490 f->num_v_slices = 1 + get_symbol(c, state, 0);
492 if (f->num_h_slices > (unsigned)f->width ||
493 f->num_v_slices > (unsigned)f->height) {
494 av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
495 return AVERROR_INVALIDDATA;
498 f->quant_table_count = get_symbol(c, state, 0);
499 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
500 return AVERROR_INVALIDDATA;
501 for (i = 0; i < f->quant_table_count; i++) {
502 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
503 if (f->context_count[i] < 0) {
504 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
505 return AVERROR_INVALIDDATA;
508 if ((ret = ffv1_allocate_initial_states(f)) < 0)
511 for (i = 0; i < f->quant_table_count; i++)
512 if (get_rac(c, state)) {
513 for (j = 0; j < f->context_count[i]; j++)
514 for (k = 0; k < CONTEXT_SIZE; k++) {
515 int pred = j ? f->initial_states[i][j - 1][k] : 128;
516 f->initial_states[i][j][k] =
517 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
521 if (f->version > 2) {
522 f->ec = get_symbol(c, state, 0);
525 if (f->version > 2) {
527 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
528 f->avctx->extradata, f->avctx->extradata_size);
530 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
531 return AVERROR_INVALIDDATA;
535 av_log(f->avctx, AV_LOG_VERBOSE,
536 "FFV1 version %d.%d colorspace %d - %d bits - %d/%d planes, %s transparent - tile geometry %dx%d - %s\n",
537 f->version, f->minor_version, f->colorspace, f->avctx->bits_per_raw_sample,
538 f->plane_count, f->chroma_planes, f->transparency ? "" : "not",
539 f->num_h_slices, f->num_v_slices,
540 f->ec ? "per-slice crc" : "no crc");
546 static int read_header(FFV1Context *f)
548 uint8_t state[CONTEXT_SIZE];
549 int i, j, context_count = -1;
550 RangeCoder *const c = &f->slice_context[0]->c;
552 memset(state, 128, sizeof(state));
554 if (f->version < 2) {
555 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
556 unsigned v = get_symbol(c, state, 0);
558 av_log(f->avctx, AV_LOG_ERROR,
559 "invalid version %d in version 1 header\n", v);
560 return AVERROR_INVALIDDATA;
564 f->ac = get_symbol(c, state, 0);
566 if (f->ac == AC_RANGE_CUSTOM_TAB) {
567 for (i = 1; i < 256; i++)
568 f->state_transition[i] =
569 get_symbol(c, state, 1) + c->one_state[i];
572 colorspace = get_symbol(c, state, 0); //YUV cs type
573 bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
574 chroma_planes = get_rac(c, state);
575 chroma_h_shift = get_symbol(c, state, 0);
576 chroma_v_shift = get_symbol(c, state, 0);
577 transparency = get_rac(c, state);
579 if (f->plane_count) {
580 if (colorspace != f->colorspace ||
581 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
582 chroma_planes != f->chroma_planes ||
583 chroma_h_shift != f->chroma_h_shift ||
584 chroma_v_shift != f->chroma_v_shift ||
585 transparency != f->transparency) {
586 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
587 return AVERROR_INVALIDDATA;
591 f->colorspace = colorspace;
592 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
593 f->chroma_planes = chroma_planes;
594 f->chroma_h_shift = chroma_h_shift;
595 f->chroma_v_shift = chroma_v_shift;
596 f->transparency = transparency;
598 f->plane_count = 2 + f->transparency;
601 if (f->colorspace == 0) {
602 if (f->transparency && f->avctx->bits_per_raw_sample > 8) {
603 av_log(f->avctx, AV_LOG_ERROR,
604 "Transparency not supported for bit depth %d\n",
605 f->avctx->bits_per_raw_sample);
606 return AVERROR(ENOSYS);
608 if (!f->transparency && !f->chroma_planes) {
609 if (f->avctx->bits_per_raw_sample <= 8)
610 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
612 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
613 } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
614 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
616 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
619 f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
622 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
625 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
628 f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
631 f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
634 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
635 return AVERROR(ENOSYS);
637 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
638 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
640 f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
643 f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
646 f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
649 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
650 return AVERROR(ENOSYS);
652 } else if (f->avctx->bits_per_raw_sample == 9) {
653 f->packed_at_lsb = 1;
654 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
656 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
659 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
662 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
665 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
666 return AVERROR(ENOSYS);
668 } else if (f->avctx->bits_per_raw_sample == 10) {
669 f->packed_at_lsb = 1;
670 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
672 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
675 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
678 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
681 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
682 return AVERROR(ENOSYS);
685 switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
687 f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
690 f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
693 f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
696 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
697 return AVERROR(ENOSYS);
700 } else if (f->colorspace == 1) {
701 if (f->chroma_h_shift || f->chroma_v_shift) {
702 av_log(f->avctx, AV_LOG_ERROR,
703 "chroma subsampling not supported in this colorspace\n");
704 return AVERROR(ENOSYS);
706 if (f->transparency) {
707 av_log(f->avctx, AV_LOG_ERROR,
708 "Transparency not supported in this colorspace\n");
709 return AVERROR(ENOSYS);
711 switch (f->avctx->bits_per_raw_sample) {
714 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
717 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
720 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
723 av_log(f->avctx, AV_LOG_ERROR,
724 "bit depth %d not supported\n",
725 f->avctx->bits_per_raw_sample);
726 return AVERROR(ENOSYS);
729 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
730 return AVERROR(ENOSYS);
733 ff_dlog(f->avctx, "%d %d %d\n",
734 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
735 if (f->version < 2) {
736 context_count = read_quant_tables(c, f->quant_table);
737 if (context_count < 0) {
738 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
739 return AVERROR_INVALIDDATA;
741 } else if (f->version < 3) {
742 f->slice_count = get_symbol(c, state, 0);
744 const uint8_t *p = c->bytestream_end;
745 for (f->slice_count = 0;
746 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
748 int trailer = 3 + 5 * !!f->ec;
749 int size = AV_RB24(p - trailer);
750 if (size + trailer > p - c->bytestream_start)
755 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
756 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
758 return AVERROR_INVALIDDATA;
761 for (j = 0; j < f->slice_count; j++) {
762 FFV1Context *fs = f->slice_context[j];
764 fs->packed_at_lsb = f->packed_at_lsb;
766 fs->slice_damaged = 0;
768 if (f->version == 2) {
769 fs->slice_x = get_symbol(c, state, 0) * f->width;
770 fs->slice_y = get_symbol(c, state, 0) * f->height;
772 (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
774 (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
776 fs->slice_x /= f->num_h_slices;
777 fs->slice_y /= f->num_v_slices;
778 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
779 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
780 if ((unsigned)fs->slice_width > f->width ||
781 (unsigned)fs->slice_height > f->height)
782 return AVERROR_INVALIDDATA;
783 if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
784 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
786 return AVERROR_INVALIDDATA;
789 for (i = 0; i < f->plane_count; i++) {
790 PlaneContext *const p = &fs->plane[i];
792 if (f->version == 2) {
793 int idx = get_symbol(c, state, 0);
794 if (idx > (unsigned)f->quant_table_count) {
795 av_log(f->avctx, AV_LOG_ERROR,
796 "quant_table_index out of range\n");
797 return AVERROR_INVALIDDATA;
799 p->quant_table_index = idx;
800 memcpy(p->quant_table, f->quant_tables[idx],
801 sizeof(p->quant_table));
802 context_count = f->context_count[idx];
804 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
807 if (f->version <= 2) {
808 av_assert0(context_count >= 0);
809 if (p->context_count < context_count) {
811 av_freep(&p->vlc_state);
813 p->context_count = context_count;
820 static av_cold int ffv1_decode_init(AVCodecContext *avctx)
822 FFV1Context *f = avctx->priv_data;
825 ffv1_common_init(avctx);
827 f->last_picture = av_frame_alloc();
828 if (!f->last_picture)
829 return AVERROR(ENOMEM);
831 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
834 if ((ret = ffv1_init_slice_contexts(f)) < 0)
840 static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
841 int *got_frame, AVPacket *avpkt)
843 uint8_t *buf = avpkt->data;
844 int buf_size = avpkt->size;
845 FFV1Context *f = avctx->priv_data;
846 RangeCoder *const c = &f->slice_context[0]->c;
848 uint8_t keystate = 128;
850 AVFrame *const p = data;
854 ff_init_range_decoder(c, buf, buf_size);
855 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
857 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
858 if (get_rac(c, &keystate)) {
861 if ((ret = read_header(f)) < 0)
865 if (!f->key_frame_ok) {
866 av_log(avctx, AV_LOG_ERROR,
867 "Cannot decode non-keyframe without valid keyframe\n");
868 return AVERROR_INVALIDDATA;
873 if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0) {
874 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
878 if (avctx->debug & FF_DEBUG_PICT_INFO)
879 av_log(avctx, AV_LOG_DEBUG,
880 "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
881 f->version, p->key_frame, f->ac, f->ec, f->slice_count,
882 f->avctx->bits_per_raw_sample);
884 buf_p = buf + buf_size;
885 for (i = f->slice_count - 1; i >= 0; i--) {
886 FFV1Context *fs = f->slice_context[i];
887 int trailer = 3 + 5 * !!f->ec;
890 if (i || f->version > 2)
891 v = AV_RB24(buf_p - trailer) + trailer;
893 v = buf_p - c->bytestream_start;
894 if (buf_p - c->bytestream_start < v) {
895 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
896 return AVERROR_INVALIDDATA;
901 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
903 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
904 fs->slice_damaged = 1;
909 ff_init_range_decoder(&fs->c, buf_p, v);
911 fs->c.bytestream_end = buf_p + v;
916 avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
920 for (i = f->slice_count - 1; i >= 0; i--) {
921 FFV1Context *fs = f->slice_context[i];
923 if (fs->slice_damaged && f->last_picture->data[0]) {
924 const uint8_t *src[4];
926 for (j = 0; j < 4; j++) {
927 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
928 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
929 dst[j] = p->data[j] + p->linesize[j] *
930 (fs->slice_y >> sv) + (fs->slice_x >> sh);
931 src[j] = f->last_picture->data[j] +
932 f->last_picture->linesize[j] *
933 (fs->slice_y >> sv) + (fs->slice_x >> sh);
935 av_image_copy(dst, p->linesize, src,
936 f->last_picture->linesize,
937 avctx->pix_fmt, fs->slice_width,
944 av_frame_unref(f->last_picture);
945 if ((ret = av_frame_ref(f->last_picture, p)) < 0)
954 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
956 FFV1Context *s = avctx->priv_data;;
958 av_frame_free(&s->last_picture);
965 AVCodec ff_ffv1_decoder = {
967 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
968 .type = AVMEDIA_TYPE_VIDEO,
969 .id = AV_CODEC_ID_FFV1,
970 .priv_data_size = sizeof(FFV1Context),
971 .init = ffv1_decode_init,
972 .close = ffv1_decode_close,
973 .decode = ffv1_decode_frame,
974 .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
975 AV_CODEC_CAP_SLICE_THREADS,