lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / ffv1enc.c
blob34191b7d843c986ee5d239a09bf5a2ce2c61d032
1 /*
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
23 /**
24 * @file
25 * FF Video Codec 1 (a lossless codec) encoder
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 "avcodec.h"
34 #include "internal.h"
35 #include "get_bits.h"
36 #include "put_bits.h"
37 #include "rangecoder.h"
38 #include "golomb.h"
39 #include "mathops.h"
40 #include "ffv1.h"
42 static void find_best_state(uint8_t best_state[256][256],
43 const uint8_t one_state[256])
45 int i, j, k, m;
46 double l2tab[256];
48 for (i = 1; i < 256; i++)
49 l2tab[i] = log2(i / 256.0);
51 for (i = 0; i < 256; i++) {
52 double best_len[256];
53 double p = i / 256.0;
55 for (j = 0; j < 256; j++)
56 best_len[j] = 1 << 30;
58 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
59 double occ[256] = { 0 };
60 double len = 0;
61 occ[j] = 1.0;
62 for (k = 0; k < 256; k++) {
63 double newocc[256] = { 0 };
64 for (m = 1; m < 256; m++)
65 if (occ[m]) {
66 len -= occ[m] * (p * l2tab[m] +
67 (1 - p) * l2tab[256 - m]);
69 if (len < best_len[k]) {
70 best_len[k] = len;
71 best_state[i][k] = j;
73 for (m = 0; m < 256; m++)
74 if (occ[m]) {
75 newocc[one_state[m]] += occ[m] * p;
76 newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
78 memcpy(occ, newocc, sizeof(occ));
84 static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
85 uint8_t *state, int v,
86 int is_signed,
87 uint64_t rc_stat[256][2],
88 uint64_t rc_stat2[32][2])
90 int i;
92 #define put_rac(C, S, B) \
93 do { \
94 if (rc_stat) { \
95 rc_stat[*(S)][B]++; \
96 rc_stat2[(S) - state][B]++; \
97 } \
98 put_rac(C, S, B); \
99 } while (0)
101 if (v) {
102 const int a = FFABS(v);
103 const int e = av_log2(a);
104 put_rac(c, state + 0, 0);
105 if (e <= 9) {
106 for (i = 0; i < e; i++)
107 put_rac(c, state + 1 + i, 1); // 1..10
108 put_rac(c, state + 1 + i, 0);
110 for (i = e - 1; i >= 0; i--)
111 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
113 if (is_signed)
114 put_rac(c, state + 11 + e, v < 0); // 11..21
115 } else {
116 for (i = 0; i < e; i++)
117 put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
118 put_rac(c, state + 1 + 9, 0);
120 for (i = e - 1; i >= 0; i--)
121 put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
123 if (is_signed)
124 put_rac(c, state + 11 + 10, v < 0); // 11..21
126 } else {
127 put_rac(c, state + 0, 1);
129 #undef put_rac
132 static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
133 int v, int is_signed)
135 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
138 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
139 int v, int bits)
141 int i, k, code;
142 v = fold(v - state->bias, bits);
144 i = state->count;
145 k = 0;
146 while (i < state->error_sum) { // FIXME: optimize
147 k++;
148 i += i;
151 assert(k <= 13);
153 #if 0 // JPEG LS
154 if (k == 0 && 2 * state->drift <= -state->count)
155 code = v ^ (-1);
156 else
157 code = v;
158 #else
159 code = v ^ ((2 * state->drift + state->count) >> 31);
160 #endif
162 av_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
163 state->bias, state->error_sum, state->drift, state->count, k);
164 set_sr_golomb(pb, code, k, 12, bits);
166 update_vlc_state(state, v);
169 static av_always_inline int encode_line(FFV1Context *s, int w,
170 int16_t *sample[3],
171 int plane_index, int bits)
173 PlaneContext *const p = &s->plane[plane_index];
174 RangeCoder *const c = &s->c;
175 int x;
176 int run_index = s->run_index;
177 int run_count = 0;
178 int run_mode = 0;
180 if (s->ac) {
181 if (c->bytestream_end - c->bytestream < w * 20) {
182 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
183 return AVERROR_INVALIDDATA;
185 } else {
186 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
187 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
188 return AVERROR_INVALIDDATA;
192 for (x = 0; x < w; x++) {
193 int diff, context;
195 context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
196 diff = sample[0][x] - predict(sample[0] + x, sample[1] + x);
198 if (context < 0) {
199 context = -context;
200 diff = -diff;
203 diff = fold(diff, bits);
205 if (s->ac) {
206 if (s->flags & CODEC_FLAG_PASS1) {
207 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
208 s->rc_stat2[p->quant_table_index][context]);
209 } else {
210 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
212 } else {
213 if (context == 0)
214 run_mode = 1;
216 if (run_mode) {
217 if (diff) {
218 while (run_count >= 1 << ff_log2_run[run_index]) {
219 run_count -= 1 << ff_log2_run[run_index];
220 run_index++;
221 put_bits(&s->pb, 1, 1);
224 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
225 if (run_index)
226 run_index--;
227 run_count = 0;
228 run_mode = 0;
229 if (diff > 0)
230 diff--;
231 } else {
232 run_count++;
236 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
237 run_count, run_index, run_mode, x,
238 (int)put_bits_count(&s->pb));
240 if (run_mode == 0)
241 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
244 if (run_mode) {
245 while (run_count >= 1 << ff_log2_run[run_index]) {
246 run_count -= 1 << ff_log2_run[run_index];
247 run_index++;
248 put_bits(&s->pb, 1, 1);
251 if (run_count)
252 put_bits(&s->pb, 1, 1);
254 s->run_index = run_index;
256 return 0;
259 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
260 int stride, int plane_index)
262 int x, y, i;
263 const int ring_size = s->avctx->context_model ? 3 : 2;
264 int16_t *sample[3];
265 s->run_index = 0;
267 memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
269 for (y = 0; y < h; y++) {
270 for (i = 0; i < ring_size; i++)
271 sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
273 sample[0][-1] = sample[1][0];
274 sample[1][w] = sample[1][w - 1];
275 // { START_TIMER
276 if (s->bits_per_raw_sample <= 8) {
277 for (x = 0; x < w; x++)
278 sample[0][x] = src[x + stride * y];
279 encode_line(s, w, sample, plane_index, 8);
280 } else {
281 if (s->packed_at_lsb) {
282 for (x = 0; x < w; x++)
283 sample[0][x] = ((uint16_t *)(src + stride * y))[x];
284 } else {
285 for (x = 0; x < w; x++)
286 sample[0][x] =
287 ((uint16_t *)(src + stride * y))[x] >> (16 - s->bits_per_raw_sample);
289 encode_line(s, w, sample, plane_index, s->bits_per_raw_sample);
291 // STOP_TIMER("encode line") }
295 static void encode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
296 int stride[3])
298 int x, y, p, i;
299 const int ring_size = s->avctx->context_model ? 3 : 2;
300 int16_t *sample[MAX_PLANES][3];
301 int lbd = s->avctx->bits_per_raw_sample <= 8;
302 int bits = s->avctx->bits_per_raw_sample > 0
303 ? s->avctx->bits_per_raw_sample
304 : 8;
305 int offset = 1 << bits;
307 s->run_index = 0;
309 memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
310 (w + 6) * sizeof(*s->sample_buffer));
312 for (y = 0; y < h; y++) {
313 for (i = 0; i < ring_size; i++)
314 for (p = 0; p < MAX_PLANES; p++)
315 sample[p][i] = s->sample_buffer + p * ring_size *
316 (w + 6) +
317 ((h + i - y) % ring_size) * (w + 6) + 3;
319 for (x = 0; x < w; x++) {
320 int b, g, r, av_uninit(a);
321 if (lbd) {
322 unsigned v = *((uint32_t *)(src[0] + x * 4 + stride[0] * y));
323 b = v & 0xFF;
324 g = (v >> 8) & 0xFF;
325 r = (v >> 16) & 0xFF;
326 a = v >> 24;
327 } else {
328 b = *((uint16_t *)(src[0] + x * 2 + stride[0] * y));
329 g = *((uint16_t *)(src[1] + x * 2 + stride[1] * y));
330 r = *((uint16_t *)(src[2] + x * 2 + stride[2] * y));
333 b -= g;
334 r -= g;
335 g += (b + r) >> 2;
336 b += offset;
337 r += offset;
339 sample[0][0][x] = g;
340 sample[1][0][x] = b;
341 sample[2][0][x] = r;
342 sample[3][0][x] = a;
344 for (p = 0; p < 3 + s->transparency; p++) {
345 sample[p][0][-1] = sample[p][1][0];
346 sample[p][1][w] = sample[p][1][w - 1];
347 if (lbd)
348 encode_line(s, w, sample[p], (p + 1) / 2, 9);
349 else
350 encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
356 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
358 int last = 0;
359 int i;
360 uint8_t state[CONTEXT_SIZE];
361 memset(state, 128, sizeof(state));
363 for (i = 1; i < 128; i++)
364 if (quant_table[i] != quant_table[i - 1]) {
365 put_symbol(c, state, i - last - 1, 0);
366 last = i;
368 put_symbol(c, state, i - last - 1, 0);
371 static void write_quant_tables(RangeCoder *c,
372 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
374 int i;
375 for (i = 0; i < 5; i++)
376 write_quant_table(c, quant_table[i]);
379 static void write_header(FFV1Context *f)
381 uint8_t state[CONTEXT_SIZE];
382 int i, j;
383 RangeCoder *const c = &f->slice_context[0]->c;
385 memset(state, 128, sizeof(state));
387 if (f->version < 2) {
388 put_symbol(c, state, f->version, 0);
389 put_symbol(c, state, f->ac, 0);
390 if (f->ac > 1) {
391 for (i = 1; i < 256; i++)
392 put_symbol(c, state,
393 f->state_transition[i] - c->one_state[i], 1);
395 put_symbol(c, state, f->colorspace, 0); // YUV cs type
396 if (f->version > 0)
397 put_symbol(c, state, f->bits_per_raw_sample, 0);
398 put_rac(c, state, f->chroma_planes);
399 put_symbol(c, state, f->chroma_h_shift, 0);
400 put_symbol(c, state, f->chroma_v_shift, 0);
401 put_rac(c, state, f->transparency);
403 write_quant_tables(c, f->quant_table);
404 } else if (f->version < 3) {
405 put_symbol(c, state, f->slice_count, 0);
406 for (i = 0; i < f->slice_count; i++) {
407 FFV1Context *fs = f->slice_context[i];
408 put_symbol(c, state,
409 (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
410 put_symbol(c, state,
411 (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
412 put_symbol(c, state,
413 (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
415 put_symbol(c, state,
416 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
418 for (j = 0; j < f->plane_count; j++) {
419 put_symbol(c, state, f->plane[j].quant_table_index, 0);
420 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
426 static int write_extradata(FFV1Context *f)
428 RangeCoder *const c = &f->c;
429 uint8_t state[CONTEXT_SIZE];
430 int i, j, k;
431 uint8_t state2[32][CONTEXT_SIZE];
432 unsigned v;
434 memset(state2, 128, sizeof(state2));
435 memset(state, 128, sizeof(state));
437 f->avctx->extradata_size = 10000 + 4 +
438 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
439 f->avctx->extradata = av_malloc(f->avctx->extradata_size);
440 ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
441 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
443 put_symbol(c, state, f->version, 0);
444 if (f->version > 2) {
445 if (f->version == 3)
446 f->minor_version = 2;
447 put_symbol(c, state, f->minor_version, 0);
450 put_symbol(c, state, f->ac, 0);
451 if (f->ac > 1)
452 for (i = 1; i < 256; i++)
453 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
455 put_symbol(c, state, f->colorspace, 0); // YUV cs type
456 put_symbol(c, state, f->bits_per_raw_sample, 0);
457 put_rac(c, state, f->chroma_planes);
458 put_symbol(c, state, f->chroma_h_shift, 0);
459 put_symbol(c, state, f->chroma_v_shift, 0);
460 put_rac(c, state, f->transparency);
461 put_symbol(c, state, f->num_h_slices - 1, 0);
462 put_symbol(c, state, f->num_v_slices - 1, 0);
464 put_symbol(c, state, f->quant_table_count, 0);
465 for (i = 0; i < f->quant_table_count; i++)
466 write_quant_tables(c, f->quant_tables[i]);
468 for (i = 0; i < f->quant_table_count; i++) {
469 for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
470 if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
471 break;
472 if (j < f->context_count[i] * CONTEXT_SIZE) {
473 put_rac(c, state, 1);
474 for (j = 0; j < f->context_count[i]; j++)
475 for (k = 0; k < CONTEXT_SIZE; k++) {
476 int pred = j ? f->initial_states[i][j - 1][k] : 128;
477 put_symbol(c, state2[k],
478 (int8_t)(f->initial_states[i][j][k] - pred), 1);
480 } else {
481 put_rac(c, state, 0);
485 if (f->version > 2) {
486 put_symbol(c, state, f->ec, 0);
489 f->avctx->extradata_size = ff_rac_terminate(c);
491 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
492 f->avctx->extradata, f->avctx->extradata_size);
493 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
494 f->avctx->extradata_size += 4;
496 return 0;
499 static int sort_stt(FFV1Context *s, uint8_t stt[256])
501 int i, i2, changed, print = 0;
503 do {
504 changed = 0;
505 for (i = 12; i < 244; i++) {
506 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
508 #define COST(old, new) \
509 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
510 s->rc_stat[old][1] * -log2((new) / 256.0)
512 #define COST2(old, new) \
513 COST(old, new) + COST(256 - (old), 256 - (new))
515 double size0 = COST2(i, i) + COST2(i2, i2);
516 double sizeX = COST2(i, i2) + COST2(i2, i);
517 if (sizeX < size0 && i != 128 && i2 != 128) {
518 int j;
519 FFSWAP(int, stt[i], stt[i2]);
520 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
521 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
522 if (i != 256 - i2) {
523 FFSWAP(int, stt[256 - i], stt[256 - i2]);
524 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
525 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
527 for (j = 1; j < 256; j++) {
528 if (stt[j] == i)
529 stt[j] = i2;
530 else if (stt[j] == i2)
531 stt[j] = i;
532 if (i != 256 - i2) {
533 if (stt[256 - j] == 256 - i)
534 stt[256 - j] = 256 - i2;
535 else if (stt[256 - j] == 256 - i2)
536 stt[256 - j] = 256 - i;
539 print = changed = 1;
543 } while (changed);
544 return print;
547 static int init_slices_state(FFV1Context *f)
549 int i, ret;
550 for (i = 0; i < f->slice_count; i++) {
551 FFV1Context *fs = f->slice_context[i];
552 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
553 return AVERROR(ENOMEM);
555 return 0;
558 static av_cold int ffv1_encode_init(AVCodecContext *avctx)
560 FFV1Context *s = avctx->priv_data;
561 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
562 int i, j, k, m, ret;
564 ffv1_common_init(avctx);
566 s->version = 0;
568 if ((avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) ||
569 avctx->slices > 1)
570 s->version = FFMAX(s->version, 2);
572 if (avctx->level == 3) {
573 s->version = 3;
576 if (s->ec < 0) {
577 s->ec = (s->version >= 3);
580 if (s->version >= 2 &&
581 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
582 av_log(avctx, AV_LOG_ERROR,
583 "Version %d requested, please set -strict experimental in "
584 "order to enable it\n",
585 s->version);
586 return AVERROR(ENOSYS);
589 s->ac = avctx->coder_type > 0 ? 2 : 0;
591 s->plane_count = 3;
592 switch (avctx->pix_fmt) {
593 case AV_PIX_FMT_YUV444P9:
594 case AV_PIX_FMT_YUV422P9:
595 case AV_PIX_FMT_YUV420P9:
596 if (!avctx->bits_per_raw_sample)
597 s->bits_per_raw_sample = 9;
598 case AV_PIX_FMT_YUV444P10:
599 case AV_PIX_FMT_YUV420P10:
600 case AV_PIX_FMT_YUV422P10:
601 s->packed_at_lsb = 1;
602 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
603 s->bits_per_raw_sample = 10;
604 case AV_PIX_FMT_GRAY16:
605 case AV_PIX_FMT_YUV444P16:
606 case AV_PIX_FMT_YUV422P16:
607 case AV_PIX_FMT_YUV420P16:
608 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
609 s->bits_per_raw_sample = 16;
610 } else if (!s->bits_per_raw_sample) {
611 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
613 if (s->bits_per_raw_sample <= 8) {
614 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
615 return AVERROR_INVALIDDATA;
617 if (!s->ac && avctx->coder_type == -1) {
618 av_log(avctx, AV_LOG_INFO,
619 "bits_per_raw_sample > 8, forcing coder 1\n");
620 s->ac = 2;
622 if (!s->ac) {
623 av_log(
624 avctx, AV_LOG_ERROR,
625 "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
626 return AVERROR_INVALIDDATA;
628 s->version = FFMAX(s->version, 1);
629 case AV_PIX_FMT_GRAY8:
630 case AV_PIX_FMT_YUV444P:
631 case AV_PIX_FMT_YUV440P:
632 case AV_PIX_FMT_YUV422P:
633 case AV_PIX_FMT_YUV420P:
634 case AV_PIX_FMT_YUV411P:
635 case AV_PIX_FMT_YUV410P:
636 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
637 s->colorspace = 0;
638 break;
639 case AV_PIX_FMT_YUVA444P:
640 case AV_PIX_FMT_YUVA422P:
641 case AV_PIX_FMT_YUVA420P:
642 s->chroma_planes = 1;
643 s->colorspace = 0;
644 s->transparency = 1;
645 break;
646 case AV_PIX_FMT_RGB32:
647 s->colorspace = 1;
648 s->transparency = 1;
649 break;
650 case AV_PIX_FMT_GBRP9:
651 if (!avctx->bits_per_raw_sample)
652 s->bits_per_raw_sample = 9;
653 case AV_PIX_FMT_GBRP10:
654 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
655 s->bits_per_raw_sample = 10;
656 case AV_PIX_FMT_GBRP16:
657 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
658 s->bits_per_raw_sample = 16;
659 else if (!s->bits_per_raw_sample)
660 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
661 s->colorspace = 1;
662 s->chroma_planes = 1;
663 s->version = FFMAX(s->version, 1);
664 break;
665 default:
666 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
667 return AVERROR_INVALIDDATA;
669 if (s->transparency) {
670 av_log(
671 avctx, AV_LOG_WARNING,
672 "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
674 if (avctx->context_model > 1U) {
675 av_log(avctx, AV_LOG_ERROR,
676 "Invalid context model %d, valid values are 0 and 1\n",
677 avctx->context_model);
678 return AVERROR(EINVAL);
681 if (s->ac > 1)
682 for (i = 1; i < 256; i++)
683 s->state_transition[i] = ffv1_ver2_state[i];
685 for (i = 0; i < 256; i++) {
686 s->quant_table_count = 2;
687 if (s->bits_per_raw_sample <= 8) {
688 s->quant_tables[0][0][i] = ffv1_quant11[i];
689 s->quant_tables[0][1][i] = ffv1_quant11[i] * 11;
690 s->quant_tables[0][2][i] = ffv1_quant11[i] * 11 * 11;
691 s->quant_tables[1][0][i] = ffv1_quant11[i];
692 s->quant_tables[1][1][i] = ffv1_quant11[i] * 11;
693 s->quant_tables[1][2][i] = ffv1_quant5[i] * 11 * 11;
694 s->quant_tables[1][3][i] = ffv1_quant5[i] * 5 * 11 * 11;
695 s->quant_tables[1][4][i] = ffv1_quant5[i] * 5 * 5 * 11 * 11;
696 } else {
697 s->quant_tables[0][0][i] = ffv1_quant9_10bit[i];
698 s->quant_tables[0][1][i] = ffv1_quant9_10bit[i] * 11;
699 s->quant_tables[0][2][i] = ffv1_quant9_10bit[i] * 11 * 11;
700 s->quant_tables[1][0][i] = ffv1_quant9_10bit[i];
701 s->quant_tables[1][1][i] = ffv1_quant9_10bit[i] * 11;
702 s->quant_tables[1][2][i] = ffv1_quant5_10bit[i] * 11 * 11;
703 s->quant_tables[1][3][i] = ffv1_quant5_10bit[i] * 5 * 11 * 11;
704 s->quant_tables[1][4][i] = ffv1_quant5_10bit[i] * 5 * 5 * 11 * 11;
707 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
708 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
709 memcpy(s->quant_table, s->quant_tables[avctx->context_model],
710 sizeof(s->quant_table));
712 for (i = 0; i < s->plane_count; i++) {
713 PlaneContext *const p = &s->plane[i];
715 memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
716 p->quant_table_index = avctx->context_model;
717 p->context_count = s->context_count[p->quant_table_index];
720 if ((ret = ffv1_allocate_initial_states(s)) < 0)
721 return ret;
723 avctx->coded_frame = &s->picture;
724 if (!s->transparency)
725 s->plane_count = 2;
727 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
728 &s->chroma_v_shift);
730 s->picture_number = 0;
732 if (avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
733 for (i = 0; i < s->quant_table_count; i++) {
734 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
735 sizeof(*s->rc_stat2[i]));
736 if (!s->rc_stat2[i])
737 return AVERROR(ENOMEM);
740 if (avctx->stats_in) {
741 char *p = avctx->stats_in;
742 uint8_t best_state[256][256];
743 int gob_count = 0;
744 char *next;
746 av_assert0(s->version >= 2);
748 for (;; ) {
749 for (j = 0; j < 256; j++)
750 for (i = 0; i < 2; i++) {
751 s->rc_stat[j][i] = strtol(p, &next, 0);
752 if (next == p) {
753 av_log(avctx, AV_LOG_ERROR,
754 "2Pass file invalid at %d %d [%s]\n", j, i, p);
755 return AVERROR_INVALIDDATA;
757 p = next;
759 for (i = 0; i < s->quant_table_count; i++)
760 for (j = 0; j < s->context_count[i]; j++) {
761 for (k = 0; k < 32; k++)
762 for (m = 0; m < 2; m++) {
763 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
764 if (next == p) {
765 av_log(avctx, AV_LOG_ERROR,
766 "2Pass file invalid at %d %d %d %d [%s]\n",
767 i, j, k, m, p);
768 return AVERROR_INVALIDDATA;
770 p = next;
773 gob_count = strtol(p, &next, 0);
774 if (next == p || gob_count <= 0) {
775 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
776 return AVERROR_INVALIDDATA;
778 p = next;
779 while (*p == '\n' || *p == ' ')
780 p++;
781 if (p[0] == 0)
782 break;
784 sort_stt(s, s->state_transition);
786 find_best_state(best_state, s->state_transition);
788 for (i = 0; i < s->quant_table_count; i++) {
789 for (j = 0; j < s->context_count[i]; j++)
790 for (k = 0; k < 32; k++) {
791 double p = 128;
792 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
793 p = 256.0 * s->rc_stat2[i][j][k][1] /
794 (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
796 s->initial_states[i][j][k] =
797 best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
798 s->rc_stat2[i][j][k][1]) /
799 gob_count, 0, 255)];
804 if (s->version > 1) {
805 for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++)
806 for (s->num_h_slices = s->num_v_slices;
807 s->num_h_slices < 2 * s->num_v_slices; s->num_h_slices++)
808 if (avctx->slices == s->num_h_slices * s->num_v_slices &&
809 avctx->slices <= 64 || !avctx->slices)
810 goto slices_ok;
811 av_log(avctx, AV_LOG_ERROR,
812 "Unsupported number %d of slices requested, please specify a "
813 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
814 avctx->slices);
815 return AVERROR(ENOSYS);
816 slices_ok:
817 write_extradata(s);
820 if ((ret = ffv1_init_slice_contexts(s)) < 0)
821 return ret;
822 if ((ret = init_slices_state(s)) < 0)
823 return ret;
825 #define STATS_OUT_SIZE 1024 * 1024 * 6
826 if (avctx->flags & CODEC_FLAG_PASS1) {
827 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
828 for (i = 0; i < s->quant_table_count; i++)
829 for (j = 0; j < s->slice_count; j++) {
830 FFV1Context *sf = s->slice_context[j];
831 av_assert0(!sf->rc_stat2[i]);
832 sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
833 sizeof(*sf->rc_stat2[i]));
834 if (!sf->rc_stat2[i])
835 return AVERROR(ENOMEM);
839 return 0;
842 static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
844 RangeCoder *c = &fs->c;
845 uint8_t state[CONTEXT_SIZE];
846 int j;
847 memset(state, 128, sizeof(state));
849 put_symbol(c, state, (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
850 put_symbol(c, state, (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
851 put_symbol(c, state, (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
853 put_symbol(c, state,
854 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
856 for (j = 0; j < f->plane_count; j++) {
857 put_symbol(c, state, f->plane[j].quant_table_index, 0);
858 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
860 if (!f->picture.interlaced_frame)
861 put_symbol(c, state, 3, 0);
862 else
863 put_symbol(c, state, 1 + !f->picture.top_field_first, 0);
864 put_symbol(c, state, f->picture.sample_aspect_ratio.num, 0);
865 put_symbol(c, state, f->picture.sample_aspect_ratio.den, 0);
868 static int encode_slice(AVCodecContext *c, void *arg)
870 FFV1Context *fs = *(void **)arg;
871 FFV1Context *f = fs->avctx->priv_data;
872 int width = fs->slice_width;
873 int height = fs->slice_height;
874 int x = fs->slice_x;
875 int y = fs->slice_y;
876 AVFrame *const p = &f->picture;
877 const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
878 ? (f->bits_per_raw_sample > 8) + 1
879 : 4;
881 if (p->key_frame)
882 ffv1_clear_slice_state(f, fs);
883 if (f->version > 2) {
884 encode_slice_header(f, fs);
886 if (!fs->ac) {
887 if (f->version > 2)
888 put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
889 fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate( &fs->c) : 0;
890 init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count,
891 fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
894 if (f->colorspace == 0) {
895 const int chroma_width = -((-width) >> f->chroma_h_shift);
896 const int chroma_height = -((-height) >> f->chroma_v_shift);
897 const int cx = x >> f->chroma_h_shift;
898 const int cy = y >> f->chroma_v_shift;
900 encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],
901 width, height, p->linesize[0], 0);
903 if (f->chroma_planes) {
904 encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
905 chroma_width, chroma_height, p->linesize[1], 1);
906 encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
907 chroma_width, chroma_height, p->linesize[2], 1);
909 if (fs->transparency)
910 encode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
911 height, p->linesize[3], 2);
912 } else {
913 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
914 p->data[1] + ps * x + y * p->linesize[1],
915 p->data[2] + ps * x + y * p->linesize[2] };
916 encode_rgb_frame(fs, planes, width, height, p->linesize);
918 emms_c();
920 return 0;
923 static int ffv1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
924 const AVFrame *pict, int *got_packet)
926 FFV1Context *f = avctx->priv_data;
927 RangeCoder *const c = &f->slice_context[0]->c;
928 AVFrame *const p = &f->picture;
929 int used_count = 0;
930 uint8_t keystate = 128;
931 uint8_t *buf_p;
932 int i, ret;
934 if ((ret = ff_alloc_packet(pkt, avctx->width * avctx->height *
935 ((8 * 2 + 1 + 1) * 4) / 8 +
936 FF_MIN_BUFFER_SIZE)) < 0) {
937 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
938 return ret;
941 ff_init_range_encoder(c, pkt->data, pkt->size);
942 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
944 *p = *pict;
945 p->pict_type = AV_PICTURE_TYPE_I;
947 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
948 put_rac(c, &keystate, 1);
949 p->key_frame = 1;
950 f->gob_count++;
951 write_header(f);
952 } else {
953 put_rac(c, &keystate, 0);
954 p->key_frame = 0;
957 if (f->ac > 1) {
958 int i;
959 for (i = 1; i < 256; i++) {
960 c->one_state[i] = f->state_transition[i];
961 c->zero_state[256 - i] = 256 - c->one_state[i];
965 for (i = 1; i < f->slice_count; i++) {
966 FFV1Context *fs = f->slice_context[i];
967 uint8_t *start = pkt->data +
968 (pkt->size - used_count) * (int64_t)i / f->slice_count;
969 int len = pkt->size / f->slice_count;
970 ff_init_range_encoder(&fs->c, start, len);
972 avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
973 f->slice_count, sizeof(void *));
975 buf_p = pkt->data;
976 for (i = 0; i < f->slice_count; i++) {
977 FFV1Context *fs = f->slice_context[i];
978 int bytes;
980 if (fs->ac) {
981 uint8_t state = 129;
982 put_rac(&fs->c, &state, 0);
983 bytes = ff_rac_terminate(&fs->c);
984 } else {
985 flush_put_bits(&fs->pb); // FIXME: nicer padding
986 bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
988 if (i > 0 || f->version > 2) {
989 av_assert0(bytes < pkt->size / f->slice_count);
990 memmove(buf_p, fs->c.bytestream_start, bytes);
991 av_assert0(bytes < (1 << 24));
992 AV_WB24(buf_p + bytes, bytes);
993 bytes += 3;
995 if (f->ec) {
996 unsigned v;
997 buf_p[bytes++] = 0;
998 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
999 AV_WL32(buf_p + bytes, v);
1000 bytes += 4;
1002 buf_p += bytes;
1005 if ((avctx->flags & CODEC_FLAG_PASS1) && (f->picture_number & 31) == 0) {
1006 int j, k, m;
1007 char *p = avctx->stats_out;
1008 char *end = p + STATS_OUT_SIZE;
1010 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1011 for (i = 0; i < f->quant_table_count; i++)
1012 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1014 for (j = 0; j < f->slice_count; j++) {
1015 FFV1Context *fs = f->slice_context[j];
1016 for (i = 0; i < 256; i++) {
1017 f->rc_stat[i][0] += fs->rc_stat[i][0];
1018 f->rc_stat[i][1] += fs->rc_stat[i][1];
1020 for (i = 0; i < f->quant_table_count; i++) {
1021 for (k = 0; k < f->context_count[i]; k++)
1022 for (m = 0; m < 32; m++) {
1023 f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
1024 f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
1029 for (j = 0; j < 256; j++) {
1030 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1031 f->rc_stat[j][0], f->rc_stat[j][1]);
1032 p += strlen(p);
1034 snprintf(p, end - p, "\n");
1036 for (i = 0; i < f->quant_table_count; i++) {
1037 for (j = 0; j < f->context_count[i]; j++)
1038 for (m = 0; m < 32; m++) {
1039 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1040 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1041 p += strlen(p);
1044 snprintf(p, end - p, "%d\n", f->gob_count);
1045 } else if (avctx->flags & CODEC_FLAG_PASS1)
1046 avctx->stats_out[0] = '\0';
1048 f->picture_number++;
1049 pkt->size = buf_p - pkt->data;
1050 pkt->flags |= AV_PKT_FLAG_KEY * p->key_frame;
1051 *got_packet = 1;
1053 return 0;
1056 #define OFFSET(x) offsetof(FFV1Context, x)
1057 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1058 static const AVOption options[] = {
1059 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT,
1060 { .i64 = -1 }, -1, 1, VE },
1061 { NULL }
1064 static const AVClass class = {
1065 .class_name = "ffv1 encoder",
1066 .item_name = av_default_item_name,
1067 .option = options,
1068 .version = LIBAVUTIL_VERSION_INT,
1071 static const AVCodecDefault ffv1_defaults[] = {
1072 { "coder", "-1" },
1073 { NULL },
1076 AVCodec ff_ffv1_encoder = {
1077 .name = "ffv1",
1078 .type = AVMEDIA_TYPE_VIDEO,
1079 .id = AV_CODEC_ID_FFV1,
1080 .priv_data_size = sizeof(FFV1Context),
1081 .init = ffv1_encode_init,
1082 .encode2 = ffv1_encode_frame,
1083 .close = ffv1_close,
1084 .capabilities = CODEC_CAP_SLICE_THREADS,
1085 .pix_fmts = (const enum AVPixelFormat[]) {
1086 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1087 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
1088 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
1089 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1090 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
1091 AV_PIX_FMT_RGB32,
1092 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
1093 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
1094 AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8,
1095 AV_PIX_FMT_NONE
1098 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1099 .defaults = ffv1_defaults,
1100 .priv_class = &class,