2 * FFV1 encoder for libavcodec
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) encoder
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/crc.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/imgutils.h"
38 #include "rangecoder.h"
43 static void find_best_state(uint8_t best_state[256][256],
44 const uint8_t one_state[256])
49 for (i = 1; i < 256; i++)
50 l2tab[i] = log2(i / 256.0);
52 for (i = 0; i < 256; i++) {
56 for (j = 0; j < 256; j++)
57 best_len[j] = 1 << 30;
59 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
60 double occ[256] = { 0 };
63 for (k = 0; k < 256; k++) {
64 double newocc[256] = { 0 };
65 for (m = 1; m < 256; m++)
67 len -= occ[m] * (p * l2tab[m] +
68 (1 - p) * l2tab[256 - m]);
70 if (len < best_len[k]) {
74 for (m = 1; m < 256; m++)
76 newocc[one_state[m]] += occ[m] * p;
77 newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
79 memcpy(occ, newocc, sizeof(occ));
85 static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
86 uint8_t *state, int v,
88 uint64_t rc_stat[256][2],
89 uint64_t rc_stat2[32][2])
93 #define put_rac(C, S, B) \
97 rc_stat2[(S) - state][B]++; \
103 const int a = FFABS(v);
104 const int e = av_log2(a);
105 put_rac(c, state + 0, 0);
107 for (i = 0; i < e; i++)
108 put_rac(c, state + 1 + i, 1); // 1..10
109 put_rac(c, state + 1 + i, 0);
111 for (i = e - 1; i >= 0; i--)
112 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
115 put_rac(c, state + 11 + e, v < 0); // 11..21
117 for (i = 0; i < e; i++)
118 put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
119 put_rac(c, state + 1 + 9, 0);
121 for (i = e - 1; i >= 0; i--)
122 put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
125 put_rac(c, state + 11 + 10, v < 0); // 11..21
128 put_rac(c, state + 0, 1);
133 static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
134 int v, int is_signed)
136 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
139 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
143 v = fold(v - state->bias, bits);
147 while (i < state->error_sum) { // FIXME: optimize
155 if (k == 0 && 2 * state->drift <= -state->count)
160 code = v ^ ((2 * state->drift + state->count) >> 31);
163 ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
164 state->bias, state->error_sum, state->drift, state->count, k);
165 set_sr_golomb(pb, code, k, 12, bits);
167 update_vlc_state(state, v);
170 static av_always_inline int encode_line(FFV1Context *s, int w,
172 int plane_index, int bits)
174 PlaneContext *const p = &s->plane[plane_index];
175 RangeCoder *const c = &s->c;
177 int run_index = s->run_index;
181 if (s->ac != AC_GOLOMB_RICE) {
182 if (c->bytestream_end - c->bytestream < w * 20) {
183 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
184 return AVERROR_INVALIDDATA;
187 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
188 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
189 return AVERROR_INVALIDDATA;
193 for (x = 0; x < w; x++) {
196 context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
197 diff = sample[0][x] - predict(sample[0] + x, sample[1] + x);
204 diff = fold(diff, bits);
206 if (s->ac != AC_GOLOMB_RICE) {
207 if (s->flags & AV_CODEC_FLAG_PASS1) {
208 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
209 s->rc_stat2[p->quant_table_index][context]);
211 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
219 while (run_count >= 1 << ff_log2_run[run_index]) {
220 run_count -= 1 << ff_log2_run[run_index];
222 put_bits(&s->pb, 1, 1);
225 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
237 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
238 run_count, run_index, run_mode, x,
239 (int)put_bits_count(&s->pb));
242 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
246 while (run_count >= 1 << ff_log2_run[run_index]) {
247 run_count -= 1 << ff_log2_run[run_index];
249 put_bits(&s->pb, 1, 1);
253 put_bits(&s->pb, 1, 1);
255 s->run_index = run_index;
260 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
261 int stride, int plane_index)
264 const int ring_size = s->context_model ? 3 : 2;
268 memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
270 for (y = 0; y < h; y++) {
271 for (i = 0; i < ring_size; i++)
272 sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
274 sample[0][-1] = sample[1][0];
275 sample[1][w] = sample[1][w - 1];
277 if (s->bits_per_raw_sample <= 8) {
278 for (x = 0; x < w; x++)
279 sample[0][x] = src[x + stride * y];
280 encode_line(s, w, sample, plane_index, 8);
282 if (s->packed_at_lsb) {
283 for (x = 0; x < w; x++)
284 sample[0][x] = ((uint16_t *)(src + stride * y))[x];
286 for (x = 0; x < w; x++)
288 ((uint16_t *)(src + stride * y))[x] >> (16 - s->bits_per_raw_sample);
290 encode_line(s, w, sample, plane_index, s->bits_per_raw_sample);
292 // STOP_TIMER("encode line") }
296 static void encode_rgb_frame(FFV1Context *s, const uint8_t *src[3],
297 int w, int h, const int stride[3])
300 const int ring_size = s->context_model ? 3 : 2;
301 int16_t *sample[MAX_PLANES][3];
302 int lbd = s->avctx->bits_per_raw_sample <= 8;
303 int bits = s->avctx->bits_per_raw_sample > 0
304 ? s->avctx->bits_per_raw_sample
306 int offset = 1 << bits;
310 memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
311 (w + 6) * sizeof(*s->sample_buffer));
313 for (y = 0; y < h; y++) {
314 for (i = 0; i < ring_size; i++)
315 for (p = 0; p < MAX_PLANES; p++)
316 sample[p][i] = s->sample_buffer + p * ring_size *
318 ((h + i - y) % ring_size) * (w + 6) + 3;
320 for (x = 0; x < w; x++) {
321 int b, g, r, av_uninit(a);
323 unsigned v = *((const uint32_t *)(src[0] + x * 4 + stride[0] * y));
326 r = (v >> 16) & 0xFF;
329 b = *((const uint16_t *)(src[0] + x * 2 + stride[0] * y));
330 g = *((const uint16_t *)(src[1] + x * 2 + stride[1] * y));
331 r = *((const uint16_t *)(src[2] + x * 2 + stride[2] * y));
345 for (p = 0; p < 3 + s->transparency; p++) {
346 sample[p][0][-1] = sample[p][1][0];
347 sample[p][1][w] = sample[p][1][w - 1];
349 encode_line(s, w, sample[p], (p + 1) / 2, 9);
351 encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
357 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
361 uint8_t state[CONTEXT_SIZE];
362 memset(state, 128, sizeof(state));
364 for (i = 1; i < 128; i++)
365 if (quant_table[i] != quant_table[i - 1]) {
366 put_symbol(c, state, i - last - 1, 0);
369 put_symbol(c, state, i - last - 1, 0);
372 static void write_quant_tables(RangeCoder *c,
373 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
376 for (i = 0; i < 5; i++)
377 write_quant_table(c, quant_table[i]);
380 static void write_header(FFV1Context *f)
382 uint8_t state[CONTEXT_SIZE];
384 RangeCoder *const c = &f->slice_context[0]->c;
386 memset(state, 128, sizeof(state));
388 if (f->version < 2) {
389 put_symbol(c, state, f->version, 0);
390 put_symbol(c, state, f->ac, 0);
391 if (f->ac == AC_RANGE_CUSTOM_TAB) {
392 for (i = 1; i < 256; i++)
394 f->state_transition[i] - c->one_state[i], 1);
396 put_symbol(c, state, f->colorspace, 0); // YUV cs type
398 put_symbol(c, state, f->bits_per_raw_sample, 0);
399 put_rac(c, state, f->chroma_planes);
400 put_symbol(c, state, f->chroma_h_shift, 0);
401 put_symbol(c, state, f->chroma_v_shift, 0);
402 put_rac(c, state, f->transparency);
404 write_quant_tables(c, f->quant_table);
408 static int write_extradata(FFV1Context *f)
410 RangeCoder *const c = &f->c;
411 uint8_t state[CONTEXT_SIZE];
413 uint8_t state2[32][CONTEXT_SIZE];
416 memset(state2, 128, sizeof(state2));
417 memset(state, 128, sizeof(state));
419 f->avctx->extradata_size = 10000 + 4 +
420 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
421 f->avctx->extradata = av_malloc(f->avctx->extradata_size);
422 ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
423 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
425 put_symbol(c, state, f->version, 0);
426 if (f->version > 1) {
428 f->minor_version = 2;
429 put_symbol(c, state, f->minor_version, 0);
432 put_symbol(c, state, f->ac, 0);
433 if (f->ac == AC_RANGE_CUSTOM_TAB)
434 for (i = 1; i < 256; i++)
435 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
437 put_symbol(c, state, f->colorspace, 0); // YUV cs type
438 put_symbol(c, state, f->bits_per_raw_sample, 0);
439 put_rac(c, state, f->chroma_planes);
440 put_symbol(c, state, f->chroma_h_shift, 0);
441 put_symbol(c, state, f->chroma_v_shift, 0);
442 put_rac(c, state, f->transparency);
443 put_symbol(c, state, f->num_h_slices - 1, 0);
444 put_symbol(c, state, f->num_v_slices - 1, 0);
446 put_symbol(c, state, f->quant_table_count, 0);
447 for (i = 0; i < f->quant_table_count; i++)
448 write_quant_tables(c, f->quant_tables[i]);
450 for (i = 0; i < f->quant_table_count; i++) {
451 for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
452 if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
454 if (j < f->context_count[i] * CONTEXT_SIZE) {
455 put_rac(c, state, 1);
456 for (j = 0; j < f->context_count[i]; j++)
457 for (k = 0; k < CONTEXT_SIZE; k++) {
458 int pred = j ? f->initial_states[i][j - 1][k] : 128;
459 put_symbol(c, state2[k],
460 (int8_t)(f->initial_states[i][j][k] - pred), 1);
463 put_rac(c, state, 0);
467 if (f->version > 2) {
468 put_symbol(c, state, f->ec, 0);
471 f->avctx->extradata_size = ff_rac_terminate(c);
473 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
474 f->avctx->extradata, f->avctx->extradata_size);
475 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
476 f->avctx->extradata_size += 4;
481 static int sort_stt(FFV1Context *s, uint8_t stt[256])
483 int i, i2, changed, print = 0;
487 for (i = 12; i < 244; i++) {
488 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
490 #define COST(old, new) \
491 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
492 s->rc_stat[old][1] * -log2((new) / 256.0)
494 #define COST2(old, new) \
495 COST(old, new) + COST(256 - (old), 256 - (new))
497 double size0 = COST2(i, i) + COST2(i2, i2);
498 double sizeX = COST2(i, i2) + COST2(i2, i);
499 if (sizeX < size0 && i != 128 && i2 != 128) {
501 FFSWAP(int, stt[i], stt[i2]);
502 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
503 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
505 FFSWAP(int, stt[256 - i], stt[256 - i2]);
506 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
507 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
509 for (j = 1; j < 256; j++) {
512 else if (stt[j] == i2)
515 if (stt[256 - j] == 256 - i)
516 stt[256 - j] = 256 - i2;
517 else if (stt[256 - j] == 256 - i2)
518 stt[256 - j] = 256 - i;
529 static av_cold int init_slices_state(FFV1Context *f)
532 for (i = 0; i < f->slice_count; i++) {
533 FFV1Context *fs = f->slice_context[i];
534 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
535 return AVERROR(ENOMEM);
540 static av_cold int ffv1_encode_init(AVCodecContext *avctx)
542 FFV1Context *s = avctx->priv_data;
543 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
546 ffv1_common_init(avctx);
550 switch (avctx->level) {
554 av_log(avctx, AV_LOG_ERROR,
555 "Version 2 had been deemed non-standard and deprecated "
556 "the support for it had been removed\n");
557 return AVERROR(ENOSYS);
560 if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
561 av_log(avctx, AV_LOG_ERROR,
562 "Multiple pass encoding requires version 3.\n");
563 return AVERROR(ENOSYS);
565 if (avctx->slices > 1) {
566 av_log(avctx, AV_LOG_ERROR,
567 "Multiple slices support requires version 3.\n");
568 return AVERROR(ENOSYS);
571 case FF_LEVEL_UNKNOWN:
572 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
579 av_log(avctx, AV_LOG_ERROR, "Version %d not supported\n",
581 return AVERROR(ENOSYS);
585 s->ec = (s->version >= 3);
588 #if FF_API_CODER_TYPE
589 FF_DISABLE_DEPRECATION_WARNINGS
590 if (avctx->coder_type != -1)
591 s->ac = avctx->coder_type > 0 ? AC_RANGE_CUSTOM_TAB : AC_GOLOMB_RICE;
592 FF_ENABLE_DEPRECATION_WARNINGS
596 switch (avctx->pix_fmt) {
597 case AV_PIX_FMT_YUV444P9:
598 case AV_PIX_FMT_YUV422P9:
599 case AV_PIX_FMT_YUV420P9:
600 if (!avctx->bits_per_raw_sample)
601 s->bits_per_raw_sample = 9;
602 case AV_PIX_FMT_YUV444P10:
603 case AV_PIX_FMT_YUV420P10:
604 case AV_PIX_FMT_YUV422P10:
605 s->packed_at_lsb = 1;
606 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
607 s->bits_per_raw_sample = 10;
608 case AV_PIX_FMT_GRAY16:
609 case AV_PIX_FMT_YUV444P16:
610 case AV_PIX_FMT_YUV422P16:
611 case AV_PIX_FMT_YUV420P16:
612 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
613 s->bits_per_raw_sample = 16;
614 } else if (!s->bits_per_raw_sample) {
615 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
617 if (s->bits_per_raw_sample <= 8) {
618 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
619 return AVERROR_INVALIDDATA;
621 if (s->ac == AC_GOLOMB_RICE) {
622 av_log(avctx, AV_LOG_INFO,
623 "bits_per_raw_sample > 8, forcing range coder\n");
624 s->ac = AC_RANGE_CUSTOM_TAB;
626 s->version = FFMAX(s->version, 1);
627 case AV_PIX_FMT_GRAY8:
628 case AV_PIX_FMT_YUV444P:
629 case AV_PIX_FMT_YUV440P:
630 case AV_PIX_FMT_YUV422P:
631 case AV_PIX_FMT_YUV420P:
632 case AV_PIX_FMT_YUV411P:
633 case AV_PIX_FMT_YUV410P:
634 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
637 case AV_PIX_FMT_YUVA444P:
638 case AV_PIX_FMT_YUVA422P:
639 case AV_PIX_FMT_YUVA420P:
640 s->chroma_planes = 1;
644 case AV_PIX_FMT_RGB32:
648 case AV_PIX_FMT_GBRP9:
649 if (!avctx->bits_per_raw_sample)
650 s->bits_per_raw_sample = 9;
651 case AV_PIX_FMT_GBRP10:
652 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
653 s->bits_per_raw_sample = 10;
654 case AV_PIX_FMT_GBRP16:
655 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
656 s->bits_per_raw_sample = 16;
657 else if (!s->bits_per_raw_sample)
658 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
660 s->chroma_planes = 1;
661 s->version = FFMAX(s->version, 1);
664 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
665 return AVERROR_INVALIDDATA;
667 if (s->transparency) {
669 avctx, AV_LOG_WARNING,
670 "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
672 #if FF_API_PRIVATE_OPT
673 FF_DISABLE_DEPRECATION_WARNINGS
674 if (avctx->context_model)
675 s->context_model = avctx->context_model;
676 if (avctx->context_model > 1U) {
677 av_log(avctx, AV_LOG_ERROR,
678 "Invalid context model %d, valid values are 0 and 1\n",
679 avctx->context_model);
680 return AVERROR(EINVAL);
682 FF_ENABLE_DEPRECATION_WARNINGS
685 if (s->ac == AC_RANGE_CUSTOM_TAB)
686 for (i = 1; i < 256; i++)
687 s->state_transition[i] = ffv1_ver2_state[i];
689 for (i = 0; i < 256; i++) {
690 s->quant_table_count = 2;
691 if (s->bits_per_raw_sample <= 8) {
692 s->quant_tables[0][0][i] = ffv1_quant11[i];
693 s->quant_tables[0][1][i] = ffv1_quant11[i] * 11;
694 s->quant_tables[0][2][i] = ffv1_quant11[i] * 11 * 11;
695 s->quant_tables[1][0][i] = ffv1_quant11[i];
696 s->quant_tables[1][1][i] = ffv1_quant11[i] * 11;
697 s->quant_tables[1][2][i] = ffv1_quant5[i] * 11 * 11;
698 s->quant_tables[1][3][i] = ffv1_quant5[i] * 5 * 11 * 11;
699 s->quant_tables[1][4][i] = ffv1_quant5[i] * 5 * 5 * 11 * 11;
701 s->quant_tables[0][0][i] = ffv1_quant9_10bit[i];
702 s->quant_tables[0][1][i] = ffv1_quant9_10bit[i] * 11;
703 s->quant_tables[0][2][i] = ffv1_quant9_10bit[i] * 11 * 11;
704 s->quant_tables[1][0][i] = ffv1_quant9_10bit[i];
705 s->quant_tables[1][1][i] = ffv1_quant9_10bit[i] * 11;
706 s->quant_tables[1][2][i] = ffv1_quant5_10bit[i] * 11 * 11;
707 s->quant_tables[1][3][i] = ffv1_quant5_10bit[i] * 5 * 11 * 11;
708 s->quant_tables[1][4][i] = ffv1_quant5_10bit[i] * 5 * 5 * 11 * 11;
711 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
712 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
713 memcpy(s->quant_table, s->quant_tables[s->context_model],
714 sizeof(s->quant_table));
716 for (i = 0; i < s->plane_count; i++) {
717 PlaneContext *const p = &s->plane[i];
719 memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
720 p->quant_table_index = s->context_model;
721 p->context_count = s->context_count[p->quant_table_index];
724 if ((ret = ffv1_allocate_initial_states(s)) < 0)
727 #if FF_API_CODED_FRAME
728 FF_DISABLE_DEPRECATION_WARNINGS
729 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
730 FF_ENABLE_DEPRECATION_WARNINGS
733 if (!s->transparency)
736 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
739 s->picture_number = 0;
741 if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
742 for (i = 0; i < s->quant_table_count; i++) {
743 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
744 sizeof(*s->rc_stat2[i]));
746 return AVERROR(ENOMEM);
749 if (avctx->stats_in) {
750 char *p = avctx->stats_in;
751 uint8_t best_state[256][256];
755 av_assert0(s->version > 2);
758 for (j = 0; j < 256; j++)
759 for (i = 0; i < 2; i++) {
760 s->rc_stat[j][i] = strtol(p, &next, 0);
762 av_log(avctx, AV_LOG_ERROR,
763 "2Pass file invalid at %d %d [%s]\n", j, i, p);
764 return AVERROR_INVALIDDATA;
768 for (i = 0; i < s->quant_table_count; i++)
769 for (j = 0; j < s->context_count[i]; j++) {
770 for (k = 0; k < 32; k++)
771 for (m = 0; m < 2; m++) {
772 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
774 av_log(avctx, AV_LOG_ERROR,
775 "2Pass file invalid at %d %d %d %d [%s]\n",
777 return AVERROR_INVALIDDATA;
782 gob_count = strtol(p, &next, 0);
783 if (next == p || gob_count <= 0) {
784 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
785 return AVERROR_INVALIDDATA;
788 while (*p == '\n' || *p == ' ')
793 sort_stt(s, s->state_transition);
795 find_best_state(best_state, s->state_transition);
797 for (i = 0; i < s->quant_table_count; i++) {
798 for (j = 0; j < s->context_count[i]; j++)
799 for (k = 0; k < 32; k++) {
801 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
802 p = 256.0 * s->rc_stat2[i][j][k][1] /
803 (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
805 s->initial_states[i][j][k] =
806 best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
807 s->rc_stat2[i][j][k][1]) /
813 if (s->version > 1) {
814 for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++)
815 for (s->num_h_slices = s->num_v_slices;
816 s->num_h_slices < 2 * s->num_v_slices; s->num_h_slices++)
817 if (avctx->slices == s->num_h_slices * s->num_v_slices &&
818 avctx->slices <= 64 || !avctx->slices)
820 av_log(avctx, AV_LOG_ERROR,
821 "Unsupported number %d of slices requested, please specify a "
822 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
824 return AVERROR(ENOSYS);
829 if ((ret = ffv1_init_slice_contexts(s)) < 0)
831 if ((ret = init_slices_state(s)) < 0)
834 #define STATS_OUT_SIZE 1024 * 1024 * 6
835 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
836 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
837 for (i = 0; i < s->quant_table_count; i++)
838 for (j = 0; j < s->slice_count; j++) {
839 FFV1Context *sf = s->slice_context[j];
840 av_assert0(!sf->rc_stat2[i]);
841 sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
842 sizeof(*sf->rc_stat2[i]));
843 if (!sf->rc_stat2[i])
844 return AVERROR(ENOMEM);
851 static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
853 RangeCoder *c = &fs->c;
854 uint8_t state[CONTEXT_SIZE];
856 memset(state, 128, sizeof(state));
858 put_symbol(c, state, (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
859 put_symbol(c, state, (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
860 put_symbol(c, state, (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
863 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
865 for (j = 0; j < f->plane_count; j++) {
866 put_symbol(c, state, f->plane[j].quant_table_index, 0);
867 av_assert0(f->plane[j].quant_table_index == f->context_model);
869 if (!f->frame->interlaced_frame)
870 put_symbol(c, state, 3, 0);
872 put_symbol(c, state, 1 + !f->frame->top_field_first, 0);
873 put_symbol(c, state, f->frame->sample_aspect_ratio.num, 0);
874 put_symbol(c, state, f->frame->sample_aspect_ratio.den, 0);
877 static int encode_slice(AVCodecContext *c, void *arg)
879 FFV1Context *fs = *(void **)arg;
880 FFV1Context *f = fs->avctx->priv_data;
881 int width = fs->slice_width;
882 int height = fs->slice_height;
885 const AVFrame *const p = f->frame;
886 const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)
887 ? (f->bits_per_raw_sample > 8) + 1
891 ffv1_clear_slice_state(f, fs);
892 if (f->version > 2) {
893 encode_slice_header(f, fs);
895 if (fs->ac == AC_GOLOMB_RICE) {
897 put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
898 fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate( &fs->c) : 0;
899 init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count,
900 fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
903 if (f->colorspace == 0) {
904 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
905 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
906 const int cx = x >> f->chroma_h_shift;
907 const int cy = y >> f->chroma_v_shift;
909 encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],
910 width, height, p->linesize[0], 0);
912 if (f->chroma_planes) {
913 encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
914 chroma_width, chroma_height, p->linesize[1], 1);
915 encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
916 chroma_width, chroma_height, p->linesize[2], 1);
918 if (fs->transparency)
919 encode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
920 height, p->linesize[3], 2);
922 const uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
923 p->data[1] + ps * x + y * p->linesize[1],
924 p->data[2] + ps * x + y * p->linesize[2] };
925 encode_rgb_frame(fs, planes, width, height, p->linesize);
932 static int ffv1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
933 const AVFrame *pict, int *got_packet)
935 FFV1Context *f = avctx->priv_data;
936 RangeCoder *const c = &f->slice_context[0]->c;
938 uint8_t keystate = 128;
944 if ((ret = ff_alloc_packet(pkt, avctx->width * avctx->height *
945 ((8 * 2 + 1 + 1) * 4) / 8 +
946 AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
947 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
951 ff_init_range_encoder(c, pkt->data, pkt->size);
952 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
954 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
955 put_rac(c, &keystate, 1);
960 put_rac(c, &keystate, 0);
964 if (f->ac == AC_RANGE_CUSTOM_TAB) {
966 for (i = 1; i < 256; i++) {
967 c->one_state[i] = f->state_transition[i];
968 c->zero_state[256 - i] = 256 - c->one_state[i];
972 for (i = 1; i < f->slice_count; i++) {
973 FFV1Context *fs = f->slice_context[i];
974 uint8_t *start = pkt->data +
975 (pkt->size - used_count) * (int64_t)i / f->slice_count;
976 int len = pkt->size / f->slice_count;
977 ff_init_range_encoder(&fs->c, start, len);
979 avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
980 f->slice_count, sizeof(void *));
983 for (i = 0; i < f->slice_count; i++) {
984 FFV1Context *fs = f->slice_context[i];
987 if (fs->ac != AC_GOLOMB_RICE) {
989 put_rac(&fs->c, &state, 0);
990 bytes = ff_rac_terminate(&fs->c);
992 flush_put_bits(&fs->pb); // FIXME: nicer padding
993 bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
995 if (i > 0 || f->version > 2) {
996 av_assert0(bytes < pkt->size / f->slice_count);
997 memmove(buf_p, fs->c.bytestream_start, bytes);
998 av_assert0(bytes < (1 << 24));
999 AV_WB24(buf_p + bytes, bytes);
1005 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
1006 AV_WL32(buf_p + bytes, v);
1012 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && (f->picture_number & 31) == 0) {
1014 char *p = avctx->stats_out;
1015 char *end = p + STATS_OUT_SIZE;
1017 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1018 for (i = 0; i < f->quant_table_count; i++)
1019 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1021 for (j = 0; j < f->slice_count; j++) {
1022 FFV1Context *fs = f->slice_context[j];
1023 for (i = 0; i < 256; i++) {
1024 f->rc_stat[i][0] += fs->rc_stat[i][0];
1025 f->rc_stat[i][1] += fs->rc_stat[i][1];
1027 for (i = 0; i < f->quant_table_count; i++) {
1028 for (k = 0; k < f->context_count[i]; k++)
1029 for (m = 0; m < 32; m++) {
1030 f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
1031 f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
1036 for (j = 0; j < 256; j++) {
1037 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1038 f->rc_stat[j][0], f->rc_stat[j][1]);
1041 snprintf(p, end - p, "\n");
1043 for (i = 0; i < f->quant_table_count; i++) {
1044 for (j = 0; j < f->context_count[i]; j++)
1045 for (m = 0; m < 32; m++) {
1046 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1047 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1051 snprintf(p, end - p, "%d\n", f->gob_count);
1052 } else if (avctx->flags & AV_CODEC_FLAG_PASS1)
1053 avctx->stats_out[0] = '\0';
1055 #if FF_API_CODED_FRAME
1056 FF_DISABLE_DEPRECATION_WARNINGS
1057 avctx->coded_frame->key_frame = f->key_frame;
1058 FF_ENABLE_DEPRECATION_WARNINGS
1061 f->picture_number++;
1062 pkt->size = buf_p - pkt->data;
1063 pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1069 static av_cold int ffv1_encode_close(AVCodecContext *avctx)
1075 #define OFFSET(x) offsetof(FFV1Context, x)
1076 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1077 static const AVOption options[] = {
1078 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT,
1079 { .i64 = -1 }, -1, 1, VE },
1080 { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1081 { .i64 = AC_GOLOMB_RICE }, 0, 2, VE, "coder" },
1082 { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1083 { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, "coder" },
1084 { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1085 { .i64 = AC_RANGE_DEFAULT_TAB }, INT_MIN, INT_MAX, VE, "coder" },
1086 { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1087 { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, "coder" },
1088 { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1089 { .i64 = 0 }, 0, 1, VE },
1094 static const AVClass class = {
1095 .class_name = "ffv1 encoder",
1096 .item_name = av_default_item_name,
1098 .version = LIBAVUTIL_VERSION_INT,
1101 #if FF_API_CODER_TYPE
1102 static const AVCodecDefault ffv1_defaults[] = {
1108 AVCodec ff_ffv1_encoder = {
1110 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1111 .type = AVMEDIA_TYPE_VIDEO,
1112 .id = AV_CODEC_ID_FFV1,
1113 .priv_data_size = sizeof(FFV1Context),
1114 .init = ffv1_encode_init,
1115 .encode2 = ffv1_encode_frame,
1116 .close = ffv1_encode_close,
1117 .capabilities = AV_CODEC_CAP_SLICE_THREADS,
1118 .pix_fmts = (const enum AVPixelFormat[]) {
1119 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1120 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
1121 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
1122 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1123 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
1125 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
1126 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
1127 AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8,
1131 #if FF_API_CODER_TYPE
1132 .defaults = ffv1_defaults,
1134 .priv_class = &class,