4 * Copyright (c) 2003-2013 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
53 return AVERROR_INVALIDDATA;
57 for (i = e - 1; i >= 0; i--)
58 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
60 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
65 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
67 return get_symbol_inline(c, state, is_signed);
70 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
77 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);
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 if (s->slice_coding_mode == 1) {
113 for (x = 0; x < w; x++) {
115 for (i=0; i<bits; i++) {
117 v += v + get_rac(c, &state);
124 for (x = 0; x < w; x++) {
125 int diff, context, sign;
127 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
134 av_assert2(context < p->context_count);
137 diff = get_symbol_inline(c, p->state[context], 1);
139 if (context == 0 && run_mode == 0)
143 if (run_count == 0 && run_mode == 1) {
144 if (get_bits1(&s->gb)) {
145 run_count = 1 << ff_log2_run[run_index];
146 if (x + run_count <= w)
149 if (ff_log2_run[run_index])
150 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
162 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
169 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
171 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
172 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
178 sample[1][x] = av_mod_uintp2(predict(sample[1] + x, sample[0] + x) + diff, bits);
180 s->run_index = run_index;
183 static void decode_plane(FFV1Context *s, uint8_t *src,
184 int w, int h, int stride, int plane_index)
188 sample[0] = s->sample_buffer + 3;
189 sample[1] = s->sample_buffer + w + 6 + 3;
193 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
195 for (y = 0; y < h; y++) {
196 int16_t *temp = sample[0]; // FIXME: try a normal buffer
198 sample[0] = sample[1];
201 sample[1][-1] = sample[0][0];
202 sample[0][w] = sample[0][w - 1];
205 if (s->avctx->bits_per_raw_sample <= 8) {
206 decode_line(s, w, sample, plane_index, 8);
207 for (x = 0; x < w; x++)
208 src[x + stride * y] = sample[1][x];
210 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
211 if (s->packed_at_lsb) {
212 for (x = 0; x < w; x++) {
213 ((uint16_t*)(src + stride*y))[x] = sample[1][x];
216 for (x = 0; x < w; x++) {
217 ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
221 // STOP_TIMER("decode-line") }
225 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
228 int16_t *sample[4][2];
229 int lbd = s->avctx->bits_per_raw_sample <= 8;
230 int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
231 int offset = 1 << bits;
233 for (x = 0; x < 4; x++) {
234 sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
235 sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
240 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
242 for (y = 0; y < h; y++) {
243 for (p = 0; p < 3 + s->transparency; p++) {
244 int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
246 sample[p][0] = sample[p][1];
249 sample[p][1][-1]= sample[p][0][0 ];
250 sample[p][0][ w]= sample[p][0][w-1];
251 if (lbd && s->slice_coding_mode == 0)
252 decode_line(s, w, sample[p], (p + 1)/2, 9);
254 decode_line(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
256 for (x = 0; x < w; x++) {
257 int g = sample[0][1][x];
258 int b = sample[1][1][x];
259 int r = sample[2][1][x];
260 int a = sample[3][1][x];
262 if (s->slice_coding_mode != 1) {
265 g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
271 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
273 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
274 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
275 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
281 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
283 RangeCoder *c = &fs->c;
284 uint8_t state[CONTEXT_SIZE];
285 unsigned ps, i, context_count;
286 memset(state, 128, sizeof(state));
288 av_assert0(f->version > 2);
290 fs->slice_x = get_symbol(c, state, 0) * f->width ;
291 fs->slice_y = get_symbol(c, state, 0) * f->height;
292 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
293 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
295 fs->slice_x /= f->num_h_slices;
296 fs->slice_y /= f->num_v_slices;
297 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
298 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
299 if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
301 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
302 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
305 for (i = 0; i < f->plane_count; i++) {
306 PlaneContext * const p = &fs->plane[i];
307 int idx = get_symbol(c, state, 0);
308 if (idx > (unsigned)f->quant_table_count) {
309 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
312 p->quant_table_index = idx;
313 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
314 context_count = f->context_count[idx];
316 if (p->context_count < context_count) {
318 av_freep(&p->vlc_state);
320 p->context_count = context_count;
323 ps = get_symbol(c, state, 0);
325 f->cur->interlaced_frame = 1;
326 f->cur->top_field_first = 1;
327 } else if (ps == 2) {
328 f->cur->interlaced_frame = 1;
329 f->cur->top_field_first = 0;
330 } else if (ps == 3) {
331 f->cur->interlaced_frame = 0;
333 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
334 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
336 if (av_image_check_sar(f->width, f->height,
337 f->cur->sample_aspect_ratio) < 0) {
338 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
339 f->cur->sample_aspect_ratio.num,
340 f->cur->sample_aspect_ratio.den);
341 f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
344 if (fs->version > 3) {
345 fs->slice_reset_contexts = get_rac(c, state);
346 fs->slice_coding_mode = get_symbol(c, state, 0);
347 if (fs->slice_coding_mode != 1) {
348 fs->slice_rct_by_coef = get_symbol(c, state, 0);
349 fs->slice_rct_ry_coef = get_symbol(c, state, 0);
350 if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
351 av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
352 return AVERROR_INVALIDDATA;
360 static int decode_slice(AVCodecContext *c, void *arg)
362 FFV1Context *fs = *(void **)arg;
363 FFV1Context *f = fs->avctx->priv_data;
364 int width, height, x, y, ret;
365 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
366 AVFrame * const p = f->cur;
369 for( si=0; fs != f->slice_context[si]; si ++)
372 if(f->fsrc && !p->key_frame)
373 ff_thread_await_progress(&f->last_picture, si, 0);
375 if(f->fsrc && !p->key_frame) {
376 FFV1Context *fssrc = f->fsrc->slice_context[si];
377 FFV1Context *fsdst = f->slice_context[si];
378 av_assert1(fsdst->plane_count == fssrc->plane_count);
379 av_assert1(fsdst == fs);
382 fsdst->slice_damaged |= fssrc->slice_damaged;
384 for (i = 0; i < f->plane_count; i++) {
385 PlaneContext *psrc = &fssrc->plane[i];
386 PlaneContext *pdst = &fsdst->plane[i];
388 av_free(pdst->state);
389 av_free(pdst->vlc_state);
390 memcpy(pdst, psrc, sizeof(*pdst));
392 pdst->vlc_state = NULL;
395 pdst->state = av_malloc_array(CONTEXT_SIZE, psrc->context_count);
396 memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
398 pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
399 memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
404 fs->slice_rct_by_coef = 1;
405 fs->slice_rct_ry_coef = 1;
407 if (f->version > 2) {
408 if (ff_ffv1_init_slice_state(f, fs) < 0)
409 return AVERROR(ENOMEM);
410 if (decode_slice_header(f, fs) < 0) {
411 fs->slice_damaged = 1;
412 return AVERROR_INVALIDDATA;
415 if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
417 if (f->cur->key_frame || fs->slice_reset_contexts)
418 ff_ffv1_clear_slice_state(f, fs);
420 width = fs->slice_width;
421 height = fs->slice_height;
426 if (f->version == 3 && f->micro_version > 1 || f->version > 3)
427 get_rac(&fs->c, (uint8_t[]) { 129 });
428 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
429 init_get_bits(&fs->gb,
430 fs->c.bytestream_start + fs->ac_byte_count,
431 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
434 av_assert1(width && height);
435 if (f->colorspace == 0) {
436 const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
437 const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
438 const int cx = x >> f->chroma_h_shift;
439 const int cy = y >> f->chroma_v_shift;
440 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
442 if (f->chroma_planes) {
443 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
444 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
446 if (fs->transparency)
447 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2);
449 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
450 p->data[1] + ps * x + y * p->linesize[1],
451 p->data[2] + ps * x + y * p->linesize[2] };
452 decode_rgb_frame(fs, planes, width, height, p->linesize);
454 if (fs->ac && f->version > 2) {
456 get_rac(&fs->c, (uint8_t[]) { 129 });
457 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
459 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
460 fs->slice_damaged = 1;
466 ff_thread_report_progress(&f->picture, si, 0);
471 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
475 uint8_t state[CONTEXT_SIZE];
477 memset(state, 128, sizeof(state));
479 for (v = 0; i < 128; v++) {
480 unsigned len = get_symbol(c, state, 0) + 1;
482 if (len > 128 - i || !len)
483 return AVERROR_INVALIDDATA;
486 quant_table[i] = scale * v;
491 for (i = 1; i < 128; i++)
492 quant_table[256 - i] = -quant_table[i];
493 quant_table[128] = -quant_table[127];
498 static int read_quant_tables(RangeCoder *c,
499 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
502 int context_count = 1;
504 for (i = 0; i < 5; i++) {
505 context_count *= read_quant_table(c, quant_table[i], context_count);
506 if (context_count > 32768U) {
507 return AVERROR_INVALIDDATA;
510 return (context_count + 1) / 2;
513 static int read_extra_header(FFV1Context *f)
515 RangeCoder *const c = &f->c;
516 uint8_t state[CONTEXT_SIZE];
518 uint8_t state2[32][CONTEXT_SIZE];
521 memset(state2, 128, sizeof(state2));
522 memset(state, 128, sizeof(state));
524 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
525 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
527 f->version = get_symbol(c, state, 0);
528 if (f->version < 2) {
529 av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
530 return AVERROR_INVALIDDATA;
532 if (f->version > 2) {
533 c->bytestream_end -= 4;
534 f->micro_version = get_symbol(c, state, 0);
535 if (f->micro_version < 0)
536 return AVERROR_INVALIDDATA;
538 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
540 for (i = 1; i < 256; i++)
541 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
544 f->colorspace = get_symbol(c, state, 0); //YUV cs type
545 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
546 f->chroma_planes = get_rac(c, state);
547 f->chroma_h_shift = get_symbol(c, state, 0);
548 f->chroma_v_shift = get_symbol(c, state, 0);
549 f->transparency = get_rac(c, state);
550 f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
551 f->num_h_slices = 1 + get_symbol(c, state, 0);
552 f->num_v_slices = 1 + get_symbol(c, state, 0);
554 if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
555 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
556 f->chroma_h_shift, f->chroma_v_shift);
557 return AVERROR_INVALIDDATA;
560 if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
561 f->num_v_slices > (unsigned)f->height || !f->num_v_slices
563 av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
564 return AVERROR_INVALIDDATA;
567 f->quant_table_count = get_symbol(c, state, 0);
568 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
569 return AVERROR_INVALIDDATA;
571 for (i = 0; i < f->quant_table_count; i++) {
572 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
573 if (f->context_count[i] < 0) {
574 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
575 return AVERROR_INVALIDDATA;
578 if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
581 for (i = 0; i < f->quant_table_count; i++)
582 if (get_rac(c, state)) {
583 for (j = 0; j < f->context_count[i]; j++)
584 for (k = 0; k < CONTEXT_SIZE; k++) {
585 int pred = j ? f->initial_states[i][j - 1][k] : 128;
586 f->initial_states[i][j][k] =
587 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
591 if (f->version > 2) {
592 f->ec = get_symbol(c, state, 0);
593 if (f->micro_version > 2)
594 f->intra = get_symbol(c, state, 0);
597 if (f->version > 2) {
599 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
600 f->avctx->extradata, f->avctx->extradata_size);
601 if (v || f->avctx->extradata_size < 4) {
602 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
603 return AVERROR_INVALIDDATA;
605 crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
608 if (f->avctx->debug & FF_DEBUG_PICT_INFO)
609 av_log(f->avctx, AV_LOG_DEBUG,
610 "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 CRC:0x%08X\n",
611 f->version, f->micro_version,
614 f->avctx->bits_per_raw_sample,
615 f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
617 f->num_h_slices, f->num_v_slices,
618 f->quant_table_count,
626 static int read_header(FFV1Context *f)
628 uint8_t state[CONTEXT_SIZE];
629 int i, j, context_count = -1; //-1 to avoid warning
630 RangeCoder *const c = &f->slice_context[0]->c;
632 memset(state, 128, sizeof(state));
634 if (f->version < 2) {
635 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
636 unsigned v= get_symbol(c, state, 0);
638 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
639 return AVERROR_INVALIDDATA;
642 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
644 for (i = 1; i < 256; i++)
645 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
648 colorspace = get_symbol(c, state, 0); //YUV cs type
649 bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
650 chroma_planes = get_rac(c, state);
651 chroma_h_shift = get_symbol(c, state, 0);
652 chroma_v_shift = get_symbol(c, state, 0);
653 transparency = get_rac(c, state);
654 if (colorspace == 0 && f->avctx->skip_alpha)
657 if (f->plane_count) {
658 if (colorspace != f->colorspace ||
659 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
660 chroma_planes != f->chroma_planes ||
661 chroma_h_shift != f->chroma_h_shift ||
662 chroma_v_shift != f->chroma_v_shift ||
663 transparency != f->transparency) {
664 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
665 return AVERROR_INVALIDDATA;
669 if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
670 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
671 chroma_h_shift, chroma_v_shift);
672 return AVERROR_INVALIDDATA;
675 f->colorspace = colorspace;
676 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
677 f->chroma_planes = chroma_planes;
678 f->chroma_h_shift = chroma_h_shift;
679 f->chroma_v_shift = chroma_v_shift;
680 f->transparency = transparency;
682 f->plane_count = 2 + f->transparency;
685 if (f->colorspace == 0) {
686 if (!f->transparency && !f->chroma_planes) {
687 if (f->avctx->bits_per_raw_sample <= 8)
688 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
690 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
691 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
692 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
693 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
694 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
695 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
696 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
697 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
698 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
700 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
701 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
702 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
703 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
704 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
706 } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
707 f->packed_at_lsb = 1;
708 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
709 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
710 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
711 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
713 } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
714 f->packed_at_lsb = 1;
715 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
716 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
717 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
718 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
720 } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
721 f->packed_at_lsb = 1;
722 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
723 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
724 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
725 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
727 } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
728 f->packed_at_lsb = 1;
729 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
730 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
731 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
732 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
734 } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
735 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
736 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
737 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
738 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
740 } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
741 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
742 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
743 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
744 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
747 } else if (f->colorspace == 1) {
748 if (f->chroma_h_shift || f->chroma_v_shift) {
749 av_log(f->avctx, AV_LOG_ERROR,
750 "chroma subsampling not supported in this colorspace\n");
751 return AVERROR(ENOSYS);
753 if ( f->avctx->bits_per_raw_sample == 9)
754 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
755 else if (f->avctx->bits_per_raw_sample == 10)
756 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
757 else if (f->avctx->bits_per_raw_sample == 12)
758 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
759 else if (f->avctx->bits_per_raw_sample == 14)
760 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
762 if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
763 else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
765 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
766 return AVERROR(ENOSYS);
768 if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
769 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
770 return AVERROR(ENOSYS);
773 ff_dlog(f->avctx, "%d %d %d\n",
774 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
775 if (f->version < 2) {
776 context_count = read_quant_tables(c, f->quant_table);
777 if (context_count < 0) {
778 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
779 return AVERROR_INVALIDDATA;
781 f->slice_count = f->max_slice_count;
782 } else if (f->version < 3) {
783 f->slice_count = get_symbol(c, state, 0);
785 const uint8_t *p = c->bytestream_end;
786 for (f->slice_count = 0;
787 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
789 int trailer = 3 + 5*!!f->ec;
790 int size = AV_RB24(p-trailer);
791 if (size + trailer > p - c->bytestream_start)
796 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
797 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
798 return AVERROR_INVALIDDATA;
801 for (j = 0; j < f->slice_count; j++) {
802 FFV1Context *fs = f->slice_context[j];
804 fs->packed_at_lsb = f->packed_at_lsb;
806 fs->slice_damaged = 0;
808 if (f->version == 2) {
809 fs->slice_x = get_symbol(c, state, 0) * f->width ;
810 fs->slice_y = get_symbol(c, state, 0) * f->height;
811 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
812 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
814 fs->slice_x /= f->num_h_slices;
815 fs->slice_y /= f->num_v_slices;
816 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
817 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
818 if ((unsigned)fs->slice_width > f->width ||
819 (unsigned)fs->slice_height > f->height)
820 return AVERROR_INVALIDDATA;
821 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
822 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
823 return AVERROR_INVALIDDATA;
826 for (i = 0; i < f->plane_count; i++) {
827 PlaneContext *const p = &fs->plane[i];
829 if (f->version == 2) {
830 int idx = get_symbol(c, state, 0);
831 if (idx > (unsigned)f->quant_table_count) {
832 av_log(f->avctx, AV_LOG_ERROR,
833 "quant_table_index out of range\n");
834 return AVERROR_INVALIDDATA;
836 p->quant_table_index = idx;
837 memcpy(p->quant_table, f->quant_tables[idx],
838 sizeof(p->quant_table));
839 context_count = f->context_count[idx];
841 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
844 if (f->version <= 2) {
845 av_assert0(context_count >= 0);
846 if (p->context_count < context_count) {
848 av_freep(&p->vlc_state);
850 p->context_count = context_count;
857 static av_cold int decode_init(AVCodecContext *avctx)
859 FFV1Context *f = avctx->priv_data;
862 if ((ret = ff_ffv1_common_init(avctx)) < 0)
865 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
868 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
871 avctx->internal->allocate_progress = 1;
876 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
878 uint8_t *buf = avpkt->data;
879 int buf_size = avpkt->size;
880 FFV1Context *f = avctx->priv_data;
881 RangeCoder *const c = &f->slice_context[0]->c;
883 uint8_t keystate = 128;
887 if (f->last_picture.f)
888 ff_thread_release_buffer(avctx, &f->last_picture);
889 FFSWAP(ThreadFrame, f->picture, f->last_picture);
891 f->cur = p = f->picture.f;
893 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
894 /* we have interlaced material flagged in container */
895 p->interlaced_frame = 1;
896 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
897 p->top_field_first = 1;
901 ff_init_range_decoder(c, buf, buf_size);
902 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
904 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
905 if (get_rac(c, &keystate)) {
908 if ((ret = read_header(f)) < 0)
912 if (!f->key_frame_ok) {
913 av_log(avctx, AV_LOG_ERROR,
914 "Cannot decode non-keyframe without valid keyframe\n");
915 return AVERROR_INVALIDDATA;
920 if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
923 if (avctx->debug & FF_DEBUG_PICT_INFO)
924 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
925 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
927 ff_thread_finish_setup(avctx);
929 buf_p = buf + buf_size;
930 for (i = f->slice_count - 1; i >= 0; i--) {
931 FFV1Context *fs = f->slice_context[i];
932 int trailer = 3 + 5*!!f->ec;
935 if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
936 else v = buf_p - c->bytestream_start;
937 if (buf_p - c->bytestream_start < v) {
938 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
939 return AVERROR_INVALIDDATA;
944 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
946 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
947 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
948 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
949 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
950 } else if (ts != AV_NOPTS_VALUE) {
951 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
953 av_log(f->avctx, AV_LOG_ERROR, "\n");
955 fs->slice_damaged = 1;
957 if (avctx->debug & FF_DEBUG_PICT_INFO) {
958 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08X\n", i, AV_RB32(buf_p + v - 4));
963 ff_init_range_decoder(&fs->c, buf_p, v);
965 fs->c.bytestream_end = buf_p + v;
971 avctx->execute(avctx,
973 &f->slice_context[0],
978 for (i = f->slice_count - 1; i >= 0; i--) {
979 FFV1Context *fs = f->slice_context[i];
981 if (fs->slice_damaged && f->last_picture.f->data[0]) {
982 const uint8_t *src[4];
984 ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
985 for (j = 0; j < 4; j++) {
986 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
987 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
988 dst[j] = p->data[j] + p->linesize[j] *
989 (fs->slice_y >> sv) + (fs->slice_x >> sh);
990 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
991 (fs->slice_y >> sv) + (fs->slice_x >> sh);
993 av_image_copy(dst, p->linesize, src,
994 f->last_picture.f->linesize,
1000 ff_thread_report_progress(&f->picture, INT_MAX, 0);
1002 f->picture_number++;
1004 if (f->last_picture.f)
1005 ff_thread_release_buffer(avctx, &f->last_picture);
1007 if ((ret = av_frame_ref(data, f->picture.f)) < 0)
1015 static int init_thread_copy(AVCodecContext *avctx)
1017 FFV1Context *f = avctx->priv_data;
1020 f->picture.f = NULL;
1021 f->last_picture.f = NULL;
1022 f->sample_buffer = NULL;
1023 f->max_slice_count = 0;
1026 for (i = 0; i < f->quant_table_count; i++) {
1027 av_assert0(f->version > 1);
1028 f->initial_states[i] = av_memdup(f->initial_states[i],
1029 f->context_count[i] * sizeof(*f->initial_states[i]));
1032 f->picture.f = av_frame_alloc();
1033 f->last_picture.f = av_frame_alloc();
1035 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
1041 static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
1043 fsdst->version = fsrc->version;
1044 fsdst->micro_version = fsrc->micro_version;
1045 fsdst->chroma_planes = fsrc->chroma_planes;
1046 fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1047 fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1048 fsdst->transparency = fsrc->transparency;
1049 fsdst->plane_count = fsrc->plane_count;
1050 fsdst->ac = fsrc->ac;
1051 fsdst->colorspace = fsrc->colorspace;
1053 fsdst->ec = fsrc->ec;
1054 fsdst->intra = fsrc->intra;
1055 fsdst->slice_damaged = fssrc->slice_damaged;
1056 fsdst->key_frame_ok = fsrc->key_frame_ok;
1058 fsdst->bits_per_raw_sample = fsrc->bits_per_raw_sample;
1059 fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1060 fsdst->slice_count = fsrc->slice_count;
1061 if (fsrc->version<3){
1062 fsdst->slice_x = fssrc->slice_x;
1063 fsdst->slice_y = fssrc->slice_y;
1064 fsdst->slice_width = fssrc->slice_width;
1065 fsdst->slice_height = fssrc->slice_height;
1069 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1071 FFV1Context *fsrc = src->priv_data;
1072 FFV1Context *fdst = dst->priv_data;
1079 ThreadFrame picture = fdst->picture, last_picture = fdst->last_picture;
1080 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
1081 struct FFV1Context *slice_context[MAX_SLICES];
1082 memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
1083 memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
1085 memcpy(fdst, fsrc, sizeof(*fdst));
1086 memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
1087 memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
1088 fdst->picture = picture;
1089 fdst->last_picture = last_picture;
1090 for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
1091 FFV1Context *fssrc = fsrc->slice_context[i];
1092 FFV1Context *fsdst = fdst->slice_context[i];
1093 copy_fields(fsdst, fssrc, fsrc);
1095 av_assert0(!fdst->plane[0].state);
1096 av_assert0(!fdst->sample_buffer);
1099 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
1102 ff_thread_release_buffer(dst, &fdst->picture);
1103 if (fsrc->picture.f->data[0]) {
1104 if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1113 AVCodec ff_ffv1_decoder = {
1115 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1116 .type = AVMEDIA_TYPE_VIDEO,
1117 .id = AV_CODEC_ID_FFV1,
1118 .priv_data_size = sizeof(FFV1Context),
1119 .init = decode_init,
1120 .close = ff_ffv1_close,
1121 .decode = decode_frame,
1122 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
1123 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1124 .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
1125 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,