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"
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
79 v = get_sr_golomb(gb, k, 12, bits);
80 av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
81 v, state->bias, state->error_sum, state->drift, state->count, k);
84 if (k == 0 && 2 * state->drift <= -state->count)
87 v ^= ((2 * state->drift + state->count) >> 31);
90 ret = fold(v + state->bias, bits);
92 update_vlc_state(state, v);
97 static av_always_inline void decode_line(FFV1Context *s, int w,
99 int plane_index, int bits)
101 PlaneContext *const p = &s->plane[plane_index];
102 RangeCoder *const c = &s->c;
106 int run_index = s->run_index;
108 for (x = 0; x < w; x++) {
109 int diff, context, sign;
111 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
118 av_assert2(context < p->context_count);
121 diff = get_symbol_inline(c, p->state[context], 1);
123 if (context == 0 && run_mode == 0)
127 if (run_count == 0 && run_mode == 1) {
128 if (get_bits1(&s->gb)) {
129 run_count = 1 << ff_log2_run[run_index];
130 if (x + run_count <= w)
133 if (ff_log2_run[run_index])
134 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
146 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
153 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
155 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
156 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
162 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
165 s->run_index = run_index;
168 static void decode_plane(FFV1Context *s, uint8_t *src,
169 int w, int h, int stride, int plane_index)
173 sample[0] = s->sample_buffer + 3;
174 sample[1] = s->sample_buffer + w + 6 + 3;
178 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
180 for (y = 0; y < h; y++) {
181 int16_t *temp = sample[0]; // FIXME: try a normal buffer
183 sample[0] = sample[1];
186 sample[1][-1] = sample[0][0];
187 sample[0][w] = sample[0][w - 1];
190 if (s->avctx->bits_per_raw_sample <= 8) {
191 decode_line(s, w, sample, plane_index, 8);
192 for (x = 0; x < w; x++)
193 src[x + stride * y] = sample[1][x];
195 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
196 if (s->packed_at_lsb) {
197 for (x = 0; x < w; x++) {
198 ((uint16_t*)(src + stride*y))[x] = sample[1][x];
201 for (x = 0; x < w; x++) {
202 ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
206 // STOP_TIMER("decode-line") }
210 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
213 int16_t *sample[4][2];
214 int lbd = s->avctx->bits_per_raw_sample <= 8;
215 int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
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 + (g<<8) + (r<<16) + (a<<24);
256 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
257 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
258 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
264 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
266 RangeCoder *c = &fs->c;
267 uint8_t state[CONTEXT_SIZE];
268 unsigned ps, i, context_count;
269 memset(state, 128, sizeof(state));
271 av_assert0(f->version > 2);
273 fs->slice_x = get_symbol(c, state, 0) * f->width ;
274 fs->slice_y = get_symbol(c, state, 0) * f->height;
275 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
276 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
278 fs->slice_x /= f->num_h_slices;
279 fs->slice_y /= f->num_v_slices;
280 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
281 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
282 if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
284 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
285 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
288 for (i = 0; i < f->plane_count; i++) {
289 PlaneContext * const p = &fs->plane[i];
290 int idx = get_symbol(c, state, 0);
291 if (idx > (unsigned)f->quant_table_count) {
292 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
295 p->quant_table_index = idx;
296 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
297 context_count = f->context_count[idx];
299 if (p->context_count < context_count) {
301 av_freep(&p->vlc_state);
303 p->context_count = context_count;
306 ps = get_symbol(c, state, 0);
308 f->cur->interlaced_frame = 1;
309 f->cur->top_field_first = 1;
310 } else if (ps == 2) {
311 f->cur->interlaced_frame = 1;
312 f->cur->top_field_first = 0;
313 } else if (ps == 3) {
314 f->cur->interlaced_frame = 0;
316 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
317 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
322 static int decode_slice(AVCodecContext *c, void *arg)
324 FFV1Context *fs = *(void **)arg;
325 FFV1Context *f = fs->avctx->priv_data;
326 int width, height, x, y, ret;
327 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
328 AVFrame * const p = f->cur;
331 for( si=0; fs != f->slice_context[si]; si ++)
334 if(f->fsrc && !p->key_frame)
335 ff_thread_await_progress(&f->last_picture, si, 0);
337 if(f->fsrc && !p->key_frame) {
338 FFV1Context *fssrc = f->fsrc->slice_context[si];
339 FFV1Context *fsdst = f->slice_context[si];
340 av_assert1(fsdst->plane_count == fssrc->plane_count);
341 av_assert1(fsdst == fs);
344 fsdst->slice_damaged |= fssrc->slice_damaged;
346 for (i = 0; i < f->plane_count; i++) {
347 PlaneContext *psrc = &fssrc->plane[i];
348 PlaneContext *pdst = &fsdst->plane[i];
350 av_free(pdst->state);
351 av_free(pdst->vlc_state);
352 memcpy(pdst, psrc, sizeof(*pdst));
354 pdst->vlc_state = NULL;
357 pdst->state = av_malloc(CONTEXT_SIZE * psrc->context_count);
358 memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
360 pdst->vlc_state = av_malloc(sizeof(*pdst->vlc_state) * psrc->context_count);
361 memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
366 if (f->version > 2) {
367 if (ffv1_init_slice_state(f, fs) < 0)
368 return AVERROR(ENOMEM);
369 if (decode_slice_header(f, fs) < 0) {
370 fs->slice_damaged = 1;
371 return AVERROR_INVALIDDATA;
374 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
376 if (f->cur->key_frame)
377 ffv1_clear_slice_state(f, fs);
379 width = fs->slice_width;
380 height = fs->slice_height;
385 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
386 get_rac(&fs->c, (uint8_t[]) { 129 });
387 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
388 init_get_bits(&fs->gb,
389 fs->c.bytestream_start + fs->ac_byte_count,
390 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
393 av_assert1(width && height);
394 if (f->colorspace == 0) {
395 const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
396 const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
397 const int cx = x >> f->chroma_h_shift;
398 const int cy = y >> f->chroma_v_shift;
399 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
401 if (f->chroma_planes) {
402 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
403 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
405 if (fs->transparency)
406 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
408 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
409 p->data[1] + ps * x + y * p->linesize[1],
410 p->data[2] + ps * x + y * p->linesize[2] };
411 decode_rgb_frame(fs, planes, width, height, p->linesize);
413 if (fs->ac && f->version > 2) {
415 get_rac(&fs->c, (uint8_t[]) { 129 });
416 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
418 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
419 fs->slice_damaged = 1;
425 ff_thread_report_progress(&f->picture, si, 0);
430 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
434 uint8_t state[CONTEXT_SIZE];
436 memset(state, 128, sizeof(state));
438 for (v = 0; i < 128; v++) {
439 unsigned len = get_symbol(c, state, 0) + 1;
442 return AVERROR_INVALIDDATA;
445 quant_table[i] = scale * v;
450 for (i = 1; i < 128; i++)
451 quant_table[256 - i] = -quant_table[i];
452 quant_table[128] = -quant_table[127];
457 static int read_quant_tables(RangeCoder *c,
458 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
461 int context_count = 1;
463 for (i = 0; i < 5; i++) {
464 context_count *= read_quant_table(c, quant_table[i], context_count);
465 if (context_count > 32768U) {
466 return AVERROR_INVALIDDATA;
469 return (context_count + 1) / 2;
472 static int read_extra_header(FFV1Context *f)
474 RangeCoder *const c = &f->c;
475 uint8_t state[CONTEXT_SIZE];
477 uint8_t state2[32][CONTEXT_SIZE];
479 memset(state2, 128, sizeof(state2));
480 memset(state, 128, sizeof(state));
482 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
483 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
485 f->version = get_symbol(c, state, 0);
486 if (f->version > 2) {
487 c->bytestream_end -= 4;
488 f->minor_version = get_symbol(c, state, 0);
490 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
492 for (i = 1; i < 256; i++)
493 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
496 f->colorspace = get_symbol(c, state, 0); //YUV cs type
497 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
498 f->chroma_planes = get_rac(c, state);
499 f->chroma_h_shift = get_symbol(c, state, 0);
500 f->chroma_v_shift = get_symbol(c, state, 0);
501 f->transparency = get_rac(c, state);
502 f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
503 f->num_h_slices = 1 + get_symbol(c, state, 0);
504 f->num_v_slices = 1 + get_symbol(c, state, 0);
506 if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
507 f->num_v_slices > (unsigned)f->height || !f->num_v_slices
509 av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
510 return AVERROR_INVALIDDATA;
513 f->quant_table_count = get_symbol(c, state, 0);
514 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
515 return AVERROR_INVALIDDATA;
517 for (i = 0; i < f->quant_table_count; i++) {
518 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
519 if (f->context_count[i] < 0) {
520 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
521 return AVERROR_INVALIDDATA;
524 if ((ret = ffv1_allocate_initial_states(f)) < 0)
527 for (i = 0; i < f->quant_table_count; i++)
528 if (get_rac(c, state)) {
529 for (j = 0; j < f->context_count[i]; j++)
530 for (k = 0; k < CONTEXT_SIZE; k++) {
531 int pred = j ? f->initial_states[i][j - 1][k] : 128;
532 f->initial_states[i][j][k] =
533 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
537 if (f->version > 2) {
538 f->ec = get_symbol(c, state, 0);
539 if (f->minor_version > 2)
540 f->intra = get_symbol(c, state, 0);
543 if (f->version > 2) {
545 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
546 f->avctx->extradata, f->avctx->extradata_size);
548 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
549 return AVERROR_INVALIDDATA;
553 if (f->avctx->debug & FF_DEBUG_PICT_INFO)
554 av_log(f->avctx, AV_LOG_DEBUG,
555 "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d\n",
556 f->version, f->minor_version,
559 f->avctx->bits_per_raw_sample,
560 f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
562 f->num_h_slices, f->num_v_slices,
563 f->quant_table_count,
570 static int read_header(FFV1Context *f)
572 uint8_t state[CONTEXT_SIZE];
573 int i, j, context_count = -1; //-1 to avoid warning
574 RangeCoder *const c = &f->slice_context[0]->c;
576 memset(state, 128, sizeof(state));
578 if (f->version < 2) {
579 unsigned v= get_symbol(c, state, 0);
581 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
582 return AVERROR_INVALIDDATA;
585 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
587 for (i = 1; i < 256; i++)
588 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
591 f->colorspace = get_symbol(c, state, 0); //YUV cs type
594 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
596 f->chroma_planes = get_rac(c, state);
597 f->chroma_h_shift = get_symbol(c, state, 0);
598 f->chroma_v_shift = get_symbol(c, state, 0);
599 f->transparency = get_rac(c, state);
600 f->plane_count = 2 + f->transparency;
603 if (f->colorspace == 0) {
604 if (!f->transparency && !f->chroma_planes) {
605 if (f->avctx->bits_per_raw_sample <= 8)
606 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
608 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
609 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
610 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
611 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
612 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
613 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
614 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
615 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
616 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
618 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
619 return AVERROR(ENOSYS);
621 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
622 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
623 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
624 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
625 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
627 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
628 return AVERROR(ENOSYS);
630 } else if (f->avctx->bits_per_raw_sample == 9) {
631 f->packed_at_lsb = 1;
632 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
633 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
634 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
635 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
637 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
638 return AVERROR(ENOSYS);
640 } else if (f->avctx->bits_per_raw_sample == 10) {
641 f->packed_at_lsb = 1;
642 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
643 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
644 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
645 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
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) {
652 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
653 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
654 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
656 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
657 return AVERROR(ENOSYS);
660 } else if (f->colorspace == 1) {
661 if (f->chroma_h_shift || f->chroma_v_shift) {
662 av_log(f->avctx, AV_LOG_ERROR,
663 "chroma subsampling not supported in this colorspace\n");
664 return AVERROR(ENOSYS);
666 if ( f->avctx->bits_per_raw_sample == 9)
667 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
668 else if (f->avctx->bits_per_raw_sample == 10)
669 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
670 else if (f->avctx->bits_per_raw_sample == 12)
671 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
672 else if (f->avctx->bits_per_raw_sample == 14)
673 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
675 if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
676 else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
678 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
679 return AVERROR(ENOSYS);
682 av_dlog(f->avctx, "%d %d %d\n",
683 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
684 if (f->version < 2) {
685 context_count = read_quant_tables(c, f->quant_table);
686 if (context_count < 0) {
687 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
688 return AVERROR_INVALIDDATA;
690 } else if (f->version < 3) {
691 f->slice_count = get_symbol(c, state, 0);
693 const uint8_t *p = c->bytestream_end;
694 for (f->slice_count = 0;
695 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
697 int trailer = 3 + 5*!!f->ec;
698 int size = AV_RB24(p-trailer);
699 if (size + trailer > p - c->bytestream_start)
704 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
705 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
706 return AVERROR_INVALIDDATA;
709 for (j = 0; j < f->slice_count; j++) {
710 FFV1Context *fs = f->slice_context[j];
712 fs->packed_at_lsb = f->packed_at_lsb;
714 fs->slice_damaged = 0;
716 if (f->version == 2) {
717 fs->slice_x = get_symbol(c, state, 0) * f->width ;
718 fs->slice_y = get_symbol(c, state, 0) * f->height;
719 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
720 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
722 fs->slice_x /= f->num_h_slices;
723 fs->slice_y /= f->num_v_slices;
724 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
725 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
726 if ((unsigned)fs->slice_width > f->width ||
727 (unsigned)fs->slice_height > f->height)
728 return AVERROR_INVALIDDATA;
729 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
730 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
731 return AVERROR_INVALIDDATA;
734 for (i = 0; i < f->plane_count; i++) {
735 PlaneContext *const p = &fs->plane[i];
737 if (f->version == 2) {
738 int idx = get_symbol(c, state, 0);
739 if (idx > (unsigned)f->quant_table_count) {
740 av_log(f->avctx, AV_LOG_ERROR,
741 "quant_table_index out of range\n");
742 return AVERROR_INVALIDDATA;
744 p->quant_table_index = idx;
745 memcpy(p->quant_table, f->quant_tables[idx],
746 sizeof(p->quant_table));
747 context_count = f->context_count[idx];
749 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
752 if (f->version <= 2) {
753 av_assert0(context_count >= 0);
754 if (p->context_count < context_count) {
756 av_freep(&p->vlc_state);
758 p->context_count = context_count;
765 static av_cold int decode_init(AVCodecContext *avctx)
767 FFV1Context *f = avctx->priv_data;
770 if ((ret = ffv1_common_init(avctx)) < 0)
773 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
776 if ((ret = ffv1_init_slice_contexts(f)) < 0)
779 avctx->internal->allocate_progress = 1;
784 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
786 const uint8_t *buf = avpkt->data;
787 int buf_size = avpkt->size;
788 FFV1Context *f = avctx->priv_data;
789 RangeCoder *const c = &f->slice_context[0]->c;
791 uint8_t keystate = 128;
792 const uint8_t *buf_p;
795 if (f->last_picture.f)
796 ff_thread_release_buffer(avctx, &f->last_picture);
797 FFSWAP(ThreadFrame, f->picture, f->last_picture);
799 f->cur = p = f->picture.f;
801 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
802 /* we have interlaced material flagged in container */
803 p->interlaced_frame = 1;
804 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
805 p->top_field_first = 1;
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_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
831 if (avctx->debug & FF_DEBUG_PICT_INFO)
832 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
833 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
835 ff_thread_finish_setup(avctx);
837 buf_p = buf + buf_size;
838 for (i = f->slice_count - 1; i >= 0; i--) {
839 FFV1Context *fs = f->slice_context[i];
840 int trailer = 3 + 5*!!f->ec;
843 if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
844 else v = buf_p - c->bytestream_start;
845 if (buf_p - c->bytestream_start < v) {
846 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
847 return AVERROR_INVALIDDATA;
852 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
854 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
855 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
856 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
857 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
858 } else if (ts != AV_NOPTS_VALUE) {
859 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
861 av_log(f->avctx, AV_LOG_ERROR, "\n");
863 fs->slice_damaged = 1;
868 ff_init_range_decoder(&fs->c, buf_p, v);
870 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
876 avctx->execute(avctx,
878 &f->slice_context[0],
883 for (i = f->slice_count - 1; i >= 0; i--) {
884 FFV1Context *fs = f->slice_context[i];
886 if (fs->slice_damaged && f->last_picture.f->data[0]) {
887 const uint8_t *src[4];
889 ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
890 for (j = 0; j < 4; j++) {
891 int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
892 int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
893 dst[j] = p->data[j] + p->linesize[j]*
894 (fs->slice_y>>sv) + (fs->slice_x>>sh);
895 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j]*
896 (fs->slice_y>>sv) + (fs->slice_x>>sh);
898 av_image_copy(dst, p->linesize, (const uint8_t **)src,
899 f->last_picture.f->linesize,
905 ff_thread_report_progress(&f->picture, INT_MAX, 0);
909 if (f->last_picture.f)
910 ff_thread_release_buffer(avctx, &f->last_picture);
912 if ((ret = av_frame_ref(data, f->picture.f)) < 0)
920 static int init_thread_copy(AVCodecContext *avctx)
922 FFV1Context *f = avctx->priv_data;
925 f->last_picture.f = NULL;
926 f->sample_buffer = NULL;
927 f->quant_table_count = 0;
933 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
935 FFV1Context *fsrc = src->priv_data;
936 FFV1Context *fdst = dst->priv_data;
942 if (!fdst->picture.f) {
943 memcpy(fdst, fsrc, sizeof(*fdst));
945 for (i = 0; i < fdst->quant_table_count; i++) {
946 fdst->initial_states[i] = av_malloc(fdst->context_count[i] * sizeof(*fdst->initial_states[i]));
947 memcpy(fdst->initial_states[i], fsrc->initial_states[i], fdst->context_count[i] * sizeof(*fdst->initial_states[i]));
950 fdst->picture.f = av_frame_alloc();
951 fdst->last_picture.f = av_frame_alloc();
953 if ((ret = ffv1_init_slice_contexts(fdst)) < 0)
957 av_assert1(fdst->slice_count == fsrc->slice_count);
959 fdst->key_frame_ok = fsrc->key_frame_ok;
961 ff_thread_release_buffer(dst, &fdst->picture);
962 if (fsrc->picture.f->data[0]) {
963 if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
966 for (i = 0; i < fdst->slice_count; i++) {
967 FFV1Context *fsdst = fdst->slice_context[i];
968 FFV1Context *fssrc = fsrc->slice_context[i];
970 fsdst->slice_damaged = fssrc->slice_damaged;
978 AVCodec ff_ffv1_decoder = {
980 .type = AVMEDIA_TYPE_VIDEO,
981 .id = AV_CODEC_ID_FFV1,
982 .priv_data_size = sizeof(FFV1Context),
985 .decode = decode_frame,
986 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
987 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
988 .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
989 CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
990 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),