avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / ffv1enc.c
blob2e3262db547cd928be53e261e9b7983dad1f3784
1 /*
2 * FFV1 encoder
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
23 /**
24 * @file
25 * FF Video Codec 1 (a lossless codec) encoder
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
35 #include "avcodec.h"
36 #include "encode.h"
37 #include "codec_internal.h"
38 #include "put_bits.h"
39 #include "put_golomb.h"
40 #include "rangecoder.h"
41 #include "ffv1.h"
42 #include "ffv1enc.h"
44 static const int8_t quant5_10bit[256] = {
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
54 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
55 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
56 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
57 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
63 static const int8_t quant5[256] = {
64 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
65 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
66 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
67 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
68 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
69 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
70 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
73 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
74 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
75 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
76 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
77 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
78 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
79 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
82 static const int8_t quant9_10bit[256] = {
83 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
84 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
85 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
86 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
87 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
88 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
89 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
90 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
91 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
92 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
93 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
94 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
95 -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
96 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
97 -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
98 -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
101 static const int8_t quant11[256] = {
102 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
103 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
104 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
105 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
106 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
107 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
108 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
109 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
110 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
111 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
112 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
113 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
114 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
115 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
116 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
117 -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
120 static const uint8_t ver2_state[256] = {
121 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
122 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
123 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
124 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
125 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
126 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
127 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
128 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
129 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
130 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
131 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
132 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
133 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
134 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
135 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
136 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
139 static void find_best_state(uint8_t best_state[256][256],
140 const uint8_t one_state[256])
142 int i, j, k, m;
143 uint32_t l2tab[256];
145 for (i = 1; i < 256; i++)
146 l2tab[i] = -log2(i / 256.0) * ((1U << 31) / 8);
148 for (i = 0; i < 256; i++) {
149 uint64_t best_len[256];
151 for (j = 0; j < 256; j++)
152 best_len[j] = UINT64_MAX;
154 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
155 uint32_t occ[256] = { 0 };
156 uint64_t len = 0;
157 occ[j] = UINT32_MAX;
159 if (!one_state[j])
160 continue;
162 for (k = 0; k < 256; k++) {
163 uint32_t newocc[256] = { 0 };
164 for (m = 1; m < 256; m++)
165 if (occ[m]) {
166 len += (occ[m]*(( i *(uint64_t)l2tab[ m]
167 + (256-i)*(uint64_t)l2tab[256-m])>>8)) >> 8;
169 if (len < best_len[k]) {
170 best_len[k] = len;
171 best_state[i][k] = j;
173 for (m = 1; m < 256; m++)
174 if (occ[m]) {
175 newocc[ one_state[ m]] += occ[m] * (uint64_t) i >> 8;
176 newocc[256 - one_state[256 - m]] += occ[m] * (uint64_t)(256 - i) >> 8;
178 memcpy(occ, newocc, sizeof(occ));
184 static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
185 uint8_t *state, int v,
186 int is_signed,
187 uint64_t rc_stat[256][2],
188 uint64_t rc_stat2[32][2])
190 int i;
192 #define put_rac(C, S, B) \
193 do { \
194 if (rc_stat) { \
195 rc_stat[*(S)][B]++; \
196 rc_stat2[(S) - state][B]++; \
198 put_rac(C, S, B); \
199 } while (0)
201 if (v) {
202 const unsigned a = is_signed ? FFABS(v) : v;
203 const int e = av_log2(a);
204 put_rac(c, state + 0, 0);
205 if (e <= 9) {
206 for (i = 0; i < e; i++)
207 put_rac(c, state + 1 + i, 1); // 1..10
208 put_rac(c, state + 1 + i, 0);
210 for (i = e - 1; i >= 0; i--)
211 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
213 if (is_signed)
214 put_rac(c, state + 11 + e, v < 0); // 11..21
215 } else {
216 for (i = 0; i < e; i++)
217 put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
218 put_rac(c, state + 1 + 9, 0);
220 for (i = e - 1; i >= 0; i--)
221 put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
223 if (is_signed)
224 put_rac(c, state + 11 + 10, v < 0); // 11..21
226 } else {
227 put_rac(c, state + 0, 1);
229 #undef put_rac
232 static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
233 int v, int is_signed)
235 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
239 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
240 int v, int bits)
242 int i, k, code;
243 v = fold(v - state->bias, bits);
245 i = state->count;
246 k = 0;
247 while (i < state->error_sum) { // FIXME: optimize
248 k++;
249 i += i;
252 av_assert2(k <= 16);
254 code = v ^ ((2 * state->drift + state->count) >> 31);
256 ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
257 state->bias, state->error_sum, state->drift, state->count, k);
258 set_sr_golomb(pb, code, k, 12, bits);
260 update_vlc_state(state, v);
263 #define TYPE int16_t
264 #define RENAME(name) name
265 #include "ffv1enc_template.c"
266 #undef TYPE
267 #undef RENAME
269 #define TYPE int32_t
270 #define RENAME(name) name ## 32
271 #include "ffv1enc_template.c"
273 static int encode_plane(FFV1Context *f, FFV1SliceContext *sc,
274 const uint8_t *src, int w, int h,
275 int stride, int plane_index, int pixel_stride, int ac)
277 int x, y, i, ret;
278 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
279 const int ring_size = f->context_model ? 3 : 2;
280 int16_t *sample[3];
281 sc->run_index = 0;
283 memset(sc->sample_buffer, 0, ring_size * (w + 6) * sizeof(*sc->sample_buffer));
285 for (y = 0; y < h; y++) {
286 for (i = 0; i < ring_size; i++)
287 sample[i] = sc->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
289 sample[0][-1]= sample[1][0 ];
290 sample[1][ w]= sample[1][w-1];
291 if (f->bits_per_raw_sample <= 8) {
292 for (x = 0; x < w; x++)
293 sample[0][x] = src[x * pixel_stride + stride * y];
294 if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, 8, ac, pass1)) < 0)
295 return ret;
296 } else {
297 if (f->packed_at_lsb) {
298 for (x = 0; x < w; x++) {
299 sample[0][x] = ((uint16_t*)(src + stride*y))[x];
301 } else {
302 for (x = 0; x < w; x++) {
303 sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - f->bits_per_raw_sample);
306 if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, f->bits_per_raw_sample, ac, pass1)) < 0)
307 return ret;
310 return 0;
313 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
315 int last = 0;
316 int i;
317 uint8_t state[CONTEXT_SIZE];
318 memset(state, 128, sizeof(state));
320 for (i = 1; i < MAX_QUANT_TABLE_SIZE/2; i++)
321 if (quant_table[i] != quant_table[i - 1]) {
322 put_symbol(c, state, i - last - 1, 0);
323 last = i;
325 put_symbol(c, state, i - last - 1, 0);
328 static void write_quant_tables(RangeCoder *c,
329 int16_t quant_table[MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE])
331 int i;
332 for (i = 0; i < 5; i++)
333 write_quant_table(c, quant_table[i]);
336 static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
337 int nb_contexts)
339 if (!initial_state)
340 return 0;
341 for (int i = 0; i < nb_contexts; i++)
342 for (int j = 0; j < CONTEXT_SIZE; j++)
343 if (initial_state[i][j] != 128)
344 return 1;
345 return 0;
348 static void write_header(FFV1Context *f)
350 uint8_t state[CONTEXT_SIZE];
351 int i, j;
352 RangeCoder *const c = &f->slices[0].c;
354 memset(state, 128, sizeof(state));
356 if (f->version < 2) {
357 put_symbol(c, state, f->version, 0);
358 put_symbol(c, state, f->ac, 0);
359 if (f->ac == AC_RANGE_CUSTOM_TAB) {
360 for (i = 1; i < 256; i++)
361 put_symbol(c, state,
362 f->state_transition[i] - c->one_state[i], 1);
364 put_symbol(c, state, f->colorspace, 0); //YUV cs type
365 if (f->version > 0)
366 put_symbol(c, state, f->bits_per_raw_sample, 0);
367 put_rac(c, state, f->chroma_planes);
368 put_symbol(c, state, f->chroma_h_shift, 0);
369 put_symbol(c, state, f->chroma_v_shift, 0);
370 put_rac(c, state, f->transparency);
372 write_quant_tables(c, f->quant_tables[f->context_model]);
373 } else if (f->version < 3) {
374 put_symbol(c, state, f->slice_count, 0);
375 for (i = 0; i < f->slice_count; i++) {
376 FFV1SliceContext *fs = &f->slices[i];
377 put_symbol(c, state,
378 (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
379 put_symbol(c, state,
380 (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
381 put_symbol(c, state,
382 (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
384 put_symbol(c, state,
385 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
387 for (j = 0; j < f->plane_count; j++) {
388 put_symbol(c, state, fs->plane[j].quant_table_index, 0);
389 av_assert0(fs->plane[j].quant_table_index == f->context_model);
395 av_cold int ff_ffv1_write_extradata(AVCodecContext *avctx)
397 FFV1Context *f = avctx->priv_data;
399 RangeCoder c;
400 uint8_t state[CONTEXT_SIZE];
401 int i, j, k;
402 uint8_t state2[32][CONTEXT_SIZE];
403 unsigned v;
405 memset(state2, 128, sizeof(state2));
406 memset(state, 128, sizeof(state));
408 f->avctx->extradata_size = 10000 + 4 +
409 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
410 f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
411 if (!f->avctx->extradata)
412 return AVERROR(ENOMEM);
413 ff_init_range_encoder(&c, f->avctx->extradata, f->avctx->extradata_size);
414 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
416 put_symbol(&c, state, f->version, 0);
417 f->combined_version = f->version << 16;
418 if (f->version > 2) {
419 if (f->version == 3) {
420 f->micro_version = 4;
421 } else if (f->version == 4)
422 f->micro_version = 3;
423 f->combined_version += f->micro_version;
424 put_symbol(&c, state, f->micro_version, 0);
427 put_symbol(&c, state, f->ac, 0);
428 if (f->ac == AC_RANGE_CUSTOM_TAB)
429 for (i = 1; i < 256; i++)
430 put_symbol(&c, state, f->state_transition[i] - c.one_state[i], 1);
432 put_symbol(&c, state, f->colorspace, 0); // YUV cs type
433 put_symbol(&c, state, f->bits_per_raw_sample, 0);
434 put_rac(&c, state, f->chroma_planes);
435 put_symbol(&c, state, f->chroma_h_shift, 0);
436 put_symbol(&c, state, f->chroma_v_shift, 0);
437 put_rac(&c, state, f->transparency);
438 put_symbol(&c, state, f->num_h_slices - 1, 0);
439 put_symbol(&c, state, f->num_v_slices - 1, 0);
441 put_symbol(&c, state, f->quant_table_count, 0);
442 for (i = 0; i < f->quant_table_count; i++)
443 write_quant_tables(&c, f->quant_tables[i]);
445 for (i = 0; i < f->quant_table_count; i++) {
446 if (contains_non_128(f->initial_states[i], f->context_count[i])) {
447 put_rac(&c, state, 1);
448 for (j = 0; j < f->context_count[i]; j++)
449 for (k = 0; k < CONTEXT_SIZE; k++) {
450 int pred = j ? f->initial_states[i][j - 1][k] : 128;
451 put_symbol(&c, state2[k],
452 (int8_t)(f->initial_states[i][j][k] - pred), 1);
454 } else {
455 put_rac(&c, state, 0);
459 if (f->version > 2) {
460 put_symbol(&c, state, f->ec, 0);
461 put_symbol(&c, state, f->intra = (f->avctx->gop_size < 2), 0);
464 f->avctx->extradata_size = ff_rac_terminate(&c, 0);
465 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, f->avctx->extradata, f->avctx->extradata_size) ^ (f->crcref ? 0x8CD88196 : 0);
466 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
467 f->avctx->extradata_size += 4;
469 return 0;
472 static int sort_stt(FFV1Context *s, uint8_t stt[256])
474 int i, i2, changed, print = 0;
476 do {
477 changed = 0;
478 for (i = 12; i < 244; i++) {
479 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
481 #define COST(old, new) \
482 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
483 s->rc_stat[old][1] * -log2((new) / 256.0)
485 #define COST2(old, new) \
486 COST(old, new) + COST(256 - (old), 256 - (new))
488 double size0 = COST2(i, i) + COST2(i2, i2);
489 double sizeX = COST2(i, i2) + COST2(i2, i);
490 if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
491 int j;
492 FFSWAP(int, stt[i], stt[i2]);
493 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
494 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
495 if (i != 256 - i2) {
496 FFSWAP(int, stt[256 - i], stt[256 - i2]);
497 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
498 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
500 for (j = 1; j < 256; j++) {
501 if (stt[j] == i)
502 stt[j] = i2;
503 else if (stt[j] == i2)
504 stt[j] = i;
505 if (i != 256 - i2) {
506 if (stt[256 - j] == 256 - i)
507 stt[256 - j] = 256 - i2;
508 else if (stt[256 - j] == 256 - i2)
509 stt[256 - j] = 256 - i;
512 print = changed = 1;
516 } while (changed);
517 return print;
521 int ff_ffv1_encode_determine_slices(AVCodecContext *avctx)
523 FFV1Context *s = avctx->priv_data;
524 int plane_count = 1 + 2*s->chroma_planes + s->transparency;
525 int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
526 int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
527 s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
528 s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
529 for (; s->num_v_slices < 32; s->num_v_slices++) {
530 for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
531 int maxw = (avctx->width + s->num_h_slices - 1) / s->num_h_slices;
532 int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
533 if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
534 continue;
535 if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
536 continue;
537 if (s->version < 4)
538 if ( ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
539 ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
540 continue;
541 if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES || !avctx->slices)
542 return 0;
545 av_log(avctx, AV_LOG_ERROR,
546 "Unsupported number %d of slices requested, please specify a "
547 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
548 avctx->slices);
549 return AVERROR(ENOSYS);
552 av_cold int ff_ffv1_encode_init(AVCodecContext *avctx)
554 FFV1Context *s = avctx->priv_data;
555 int i, j, k, m, ret;
557 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
558 avctx->slices > 1)
559 s->version = FFMAX(s->version, 2);
561 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) && s->ac == AC_GOLOMB_RICE) {
562 av_log(avctx, AV_LOG_ERROR, "2 Pass mode is not possible with golomb coding\n");
563 return AVERROR(EINVAL);
566 // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
567 if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
568 s->version = FFMAX(s->version, 2);
570 if (avctx->level <= 0 && s->version == 2) {
571 s->version = 3;
573 if (avctx->level >= 0 && avctx->level <= 4) {
574 if (avctx->level < s->version) {
575 av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
576 return AVERROR(EINVAL);
578 s->version = avctx->level;
581 if (s->ec < 0) {
582 if (s->version >= 4) {
583 s->ec = 2;
584 s->crcref = 0x7a8c4079;
585 } else if (s->version >= 3) {
586 s->ec = 1;
587 } else
588 s->ec = 0;
591 // CRC requires version 3+
592 if (s->ec == 1)
593 s->version = FFMAX(s->version, 3);
594 if (s->ec == 2)
595 s->version = FFMAX(s->version, 4);
597 if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
598 av_log(avctx, AV_LOG_ERROR, "Version 2 or 4 needed for requested features but version 2 or 4 is experimental and not enabled\n");
599 return AVERROR_INVALIDDATA;
602 if (s->ac == AC_RANGE_CUSTOM_TAB) {
603 for (i = 1; i < 256; i++)
604 s->state_transition[i] = ver2_state[i];
605 } else {
606 RangeCoder c;
607 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
608 for (i = 1; i < 256; i++)
609 s->state_transition[i] = c.one_state[i];
612 for (i = 0; i < 256; i++) {
613 s->quant_table_count = 2;
614 if ((s->qtable == -1 && s->bits_per_raw_sample <= 8) || s->qtable == 1) {
615 s->quant_tables[0][0][i]= quant11[i];
616 s->quant_tables[0][1][i]= 11*quant11[i];
617 s->quant_tables[0][2][i]= 11*11*quant11[i];
618 s->quant_tables[1][0][i]= quant11[i];
619 s->quant_tables[1][1][i]= 11*quant11[i];
620 s->quant_tables[1][2][i]= 11*11*quant5 [i];
621 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
622 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
623 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
624 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
625 } else {
626 s->quant_tables[0][0][i]= quant9_10bit[i];
627 s->quant_tables[0][1][i]= 9*quant9_10bit[i];
628 s->quant_tables[0][2][i]= 9*9*quant9_10bit[i];
629 s->quant_tables[1][0][i]= quant9_10bit[i];
630 s->quant_tables[1][1][i]= 9*quant9_10bit[i];
631 s->quant_tables[1][2][i]= 9*9*quant5_10bit[i];
632 s->quant_tables[1][3][i]= 5*9*9*quant5_10bit[i];
633 s->quant_tables[1][4][i]= 5*5*9*9*quant5_10bit[i];
634 s->context_count[0] = (9 * 9 * 9 + 1) / 2;
635 s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
639 if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
640 return ret;
642 if (!s->transparency)
643 s->plane_count = 2;
644 if (!s->chroma_planes && s->version > 3)
645 s->plane_count--;
647 s->picture_number = 0;
649 if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
650 for (i = 0; i < s->quant_table_count; i++) {
651 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
652 sizeof(*s->rc_stat2[i]));
653 if (!s->rc_stat2[i])
654 return AVERROR(ENOMEM);
657 if (avctx->stats_in) {
658 char *p = avctx->stats_in;
659 uint8_t (*best_state)[256] = av_malloc_array(256, 256);
660 int gob_count = 0;
661 char *next;
662 if (!best_state)
663 return AVERROR(ENOMEM);
665 av_assert0(s->version >= 2);
667 for (;;) {
668 for (j = 0; j < 256; j++)
669 for (i = 0; i < 2; i++) {
670 s->rc_stat[j][i] = strtol(p, &next, 0);
671 if (next == p) {
672 av_log(avctx, AV_LOG_ERROR,
673 "2Pass file invalid at %d %d [%s]\n", j, i, p);
674 av_freep(&best_state);
675 return AVERROR_INVALIDDATA;
677 p = next;
679 for (i = 0; i < s->quant_table_count; i++)
680 for (j = 0; j < s->context_count[i]; j++) {
681 for (k = 0; k < 32; k++)
682 for (m = 0; m < 2; m++) {
683 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
684 if (next == p) {
685 av_log(avctx, AV_LOG_ERROR,
686 "2Pass file invalid at %d %d %d %d [%s]\n",
687 i, j, k, m, p);
688 av_freep(&best_state);
689 return AVERROR_INVALIDDATA;
691 p = next;
694 gob_count = strtol(p, &next, 0);
695 if (next == p || gob_count <= 0) {
696 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
697 av_freep(&best_state);
698 return AVERROR_INVALIDDATA;
700 p = next;
701 while (*p == '\n' || *p == ' ')
702 p++;
703 if (p[0] == 0)
704 break;
706 if (s->ac == AC_RANGE_CUSTOM_TAB)
707 sort_stt(s, s->state_transition);
709 find_best_state(best_state, s->state_transition);
711 for (i = 0; i < s->quant_table_count; i++) {
712 for (k = 0; k < 32; k++) {
713 double a=0, b=0;
714 int jp = 0;
715 for (j = 0; j < s->context_count[i]; j++) {
716 double p = 128;
717 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
718 if (a+b)
719 p = 256.0 * b / (a + b);
720 s->initial_states[i][jp][k] =
721 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
722 for(jp++; jp<j; jp++)
723 s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
724 a=b=0;
726 a += s->rc_stat2[i][j][k][0];
727 b += s->rc_stat2[i][j][k][1];
728 if (a+b) {
729 p = 256.0 * b / (a + b);
731 s->initial_states[i][j][k] =
732 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
736 av_freep(&best_state);
739 if (s->version <= 1) {
740 /* Disable slices when the version doesn't support them */
741 s->num_h_slices = 1;
742 s->num_v_slices = 1;
745 return 0;
748 av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
749 enum AVPixelFormat pix_fmt)
751 FFV1Context *s = avctx->priv_data;
752 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
754 s->plane_count = 3;
755 switch(pix_fmt) {
756 case AV_PIX_FMT_GRAY9:
757 case AV_PIX_FMT_YUV444P9:
758 case AV_PIX_FMT_YUV422P9:
759 case AV_PIX_FMT_YUV420P9:
760 case AV_PIX_FMT_YUVA444P9:
761 case AV_PIX_FMT_YUVA422P9:
762 case AV_PIX_FMT_YUVA420P9:
763 if (!avctx->bits_per_raw_sample)
764 s->bits_per_raw_sample = 9;
765 case AV_PIX_FMT_GRAY10:
766 case AV_PIX_FMT_YUV444P10:
767 case AV_PIX_FMT_YUV440P10:
768 case AV_PIX_FMT_YUV420P10:
769 case AV_PIX_FMT_YUV422P10:
770 case AV_PIX_FMT_YUVA444P10:
771 case AV_PIX_FMT_YUVA422P10:
772 case AV_PIX_FMT_YUVA420P10:
773 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
774 s->bits_per_raw_sample = 10;
775 case AV_PIX_FMT_GRAY12:
776 case AV_PIX_FMT_YUV444P12:
777 case AV_PIX_FMT_YUV440P12:
778 case AV_PIX_FMT_YUV420P12:
779 case AV_PIX_FMT_YUV422P12:
780 case AV_PIX_FMT_YUVA444P12:
781 case AV_PIX_FMT_YUVA422P12:
782 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
783 s->bits_per_raw_sample = 12;
784 case AV_PIX_FMT_GRAY14:
785 case AV_PIX_FMT_YUV444P14:
786 case AV_PIX_FMT_YUV420P14:
787 case AV_PIX_FMT_YUV422P14:
788 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
789 s->bits_per_raw_sample = 14;
790 s->packed_at_lsb = 1;
791 case AV_PIX_FMT_GRAY16:
792 case AV_PIX_FMT_YUV444P16:
793 case AV_PIX_FMT_YUV422P16:
794 case AV_PIX_FMT_YUV420P16:
795 case AV_PIX_FMT_YUVA444P16:
796 case AV_PIX_FMT_YUVA422P16:
797 case AV_PIX_FMT_YUVA420P16:
798 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
799 s->bits_per_raw_sample = 16;
800 } else if (!s->bits_per_raw_sample) {
801 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
803 if (s->bits_per_raw_sample <= 8) {
804 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
805 return AVERROR_INVALIDDATA;
807 s->version = FFMAX(s->version, 1);
808 case AV_PIX_FMT_GRAY8:
809 case AV_PIX_FMT_YA8:
810 case AV_PIX_FMT_YUV444P:
811 case AV_PIX_FMT_YUV440P:
812 case AV_PIX_FMT_YUV422P:
813 case AV_PIX_FMT_YUV420P:
814 case AV_PIX_FMT_YUV411P:
815 case AV_PIX_FMT_YUV410P:
816 case AV_PIX_FMT_YUVA444P:
817 case AV_PIX_FMT_YUVA422P:
818 case AV_PIX_FMT_YUVA420P:
819 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
820 s->colorspace = 0;
821 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
822 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
823 s->bits_per_raw_sample = 8;
824 else if (!s->bits_per_raw_sample)
825 s->bits_per_raw_sample = 8;
826 break;
827 case AV_PIX_FMT_RGB32:
828 s->colorspace = 1;
829 s->transparency = 1;
830 s->chroma_planes = 1;
831 s->bits_per_raw_sample = 8;
832 break;
833 case AV_PIX_FMT_RGBA64:
834 s->colorspace = 1;
835 s->transparency = 1;
836 s->chroma_planes = 1;
837 s->bits_per_raw_sample = 16;
838 s->use32bit = 1;
839 s->version = FFMAX(s->version, 1);
840 break;
841 case AV_PIX_FMT_RGB48:
842 s->colorspace = 1;
843 s->chroma_planes = 1;
844 s->bits_per_raw_sample = 16;
845 s->use32bit = 1;
846 s->version = FFMAX(s->version, 1);
847 break;
848 case AV_PIX_FMT_0RGB32:
849 s->colorspace = 1;
850 s->chroma_planes = 1;
851 s->bits_per_raw_sample = 8;
852 break;
853 case AV_PIX_FMT_GBRP9:
854 if (!avctx->bits_per_raw_sample)
855 s->bits_per_raw_sample = 9;
856 case AV_PIX_FMT_GBRP10:
857 case AV_PIX_FMT_GBRAP10:
858 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
859 s->bits_per_raw_sample = 10;
860 case AV_PIX_FMT_GBRP12:
861 case AV_PIX_FMT_GBRAP12:
862 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
863 s->bits_per_raw_sample = 12;
864 case AV_PIX_FMT_GBRP14:
865 case AV_PIX_FMT_GBRAP14:
866 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
867 s->bits_per_raw_sample = 14;
868 case AV_PIX_FMT_GBRP16:
869 case AV_PIX_FMT_GBRAP16:
870 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
871 s->bits_per_raw_sample = 16;
872 else if (!s->bits_per_raw_sample)
873 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
874 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
875 s->colorspace = 1;
876 s->chroma_planes = 1;
877 if (s->bits_per_raw_sample >= 16) {
878 s->use32bit = 1;
880 s->version = FFMAX(s->version, 1);
881 break;
882 default:
883 av_log(avctx, AV_LOG_ERROR, "format %s not supported\n",
884 av_get_pix_fmt_name(pix_fmt));
885 return AVERROR(ENOSYS);
887 av_assert0(s->bits_per_raw_sample >= 8);
889 return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
892 static int encode_init_internal(AVCodecContext *avctx)
894 int ret;
895 FFV1Context *s = avctx->priv_data;
897 if ((ret = ff_ffv1_common_init(avctx, s)) < 0)
898 return ret;
900 if (s->ac == 1) // Compatbility with common command line usage
901 s->ac = AC_RANGE_CUSTOM_TAB;
902 else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
903 s->ac = AC_RANGE_DEFAULT_TAB;
905 ret = ff_ffv1_encode_setup_plane_info(avctx, avctx->pix_fmt);
906 if (ret < 0)
907 return ret;
909 if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8)) {
910 if (s->ac == AC_GOLOMB_RICE) {
911 av_log(avctx, AV_LOG_INFO,
912 "high bits_per_raw_sample, forcing range coder\n");
913 s->ac = AC_RANGE_CUSTOM_TAB;
918 ret = ff_ffv1_encode_init(avctx);
919 if (ret < 0)
920 return ret;
922 if (s->version > 1) {
923 if ((ret = ff_ffv1_encode_determine_slices(avctx)) < 0)
924 return ret;
926 if ((ret = ff_ffv1_write_extradata(avctx)) < 0)
927 return ret;
930 if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
931 return ret;
932 s->slice_count = s->max_slice_count;
934 for (int j = 0; j < s->slice_count; j++) {
935 for (int i = 0; i < s->plane_count; i++) {
936 PlaneContext *const p = &s->slices[j].plane[i];
938 p->quant_table_index = s->context_model;
939 p->context_count = s->context_count[p->quant_table_index];
942 ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
945 if ((ret = ff_ffv1_init_slices_state(s)) < 0)
946 return ret;
948 #define STATS_OUT_SIZE 1024 * 1024 * 6
949 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
950 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
951 if (!avctx->stats_out)
952 return AVERROR(ENOMEM);
953 for (int i = 0; i < s->quant_table_count; i++)
954 for (int j = 0; j < s->max_slice_count; j++) {
955 FFV1SliceContext *sc = &s->slices[j];
956 av_assert0(!sc->rc_stat2[i]);
957 sc->rc_stat2[i] = av_mallocz(s->context_count[i] *
958 sizeof(*sc->rc_stat2[i]));
959 if (!sc->rc_stat2[i])
960 return AVERROR(ENOMEM);
964 return 0;
967 static void encode_slice_header(FFV1Context *f, FFV1SliceContext *sc)
969 RangeCoder *c = &sc->c;
970 uint8_t state[CONTEXT_SIZE];
971 int j;
972 memset(state, 128, sizeof(state));
974 put_symbol(c, state, sc->sx, 0);
975 put_symbol(c, state, sc->sy, 0);
976 put_symbol(c, state, 0, 0);
977 put_symbol(c, state, 0, 0);
978 for (j=0; j<f->plane_count; j++) {
979 put_symbol(c, state, sc->plane[j].quant_table_index, 0);
980 av_assert0(sc->plane[j].quant_table_index == f->context_model);
982 if (!(f->cur_enc_frame->flags & AV_FRAME_FLAG_INTERLACED))
983 put_symbol(c, state, 3, 0);
984 else
985 put_symbol(c, state, 1 + !(f->cur_enc_frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST), 0);
986 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.num, 0);
987 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.den, 0);
988 if (f->version > 3) {
989 put_rac(c, state, sc->slice_coding_mode == 1);
990 if (sc->slice_coding_mode == 1)
991 ff_ffv1_clear_slice_state(f, sc);
992 put_symbol(c, state, sc->slice_coding_mode, 0);
993 if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
994 put_symbol(c, state, sc->slice_rct_by_coef, 0);
995 put_symbol(c, state, sc->slice_rct_ry_coef, 0);
1000 static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
1001 const uint8_t *src[3], const int stride[3], int w, int h)
1003 #define NB_Y_COEFF 15
1004 static const int rct_y_coeff[15][2] = {
1005 {0, 0}, // 4G
1006 {1, 1}, // R + 2G + B
1007 {2, 2}, // 2R + 2B
1008 {0, 2}, // 2G + 2B
1009 {2, 0}, // 2R + 2G
1010 {4, 0}, // 4R
1011 {0, 4}, // 4B
1013 {0, 3}, // 1G + 3B
1014 {3, 0}, // 3R + 1G
1015 {3, 1}, // 3R + B
1016 {1, 3}, // R + 3B
1017 {1, 2}, // R + G + 2B
1018 {2, 1}, // 2R + G + B
1019 {0, 1}, // 3G + B
1020 {1, 0}, // R + 3G
1023 int stat[NB_Y_COEFF] = {0};
1024 int x, y, i, p, best;
1025 int16_t *sample[3];
1026 int lbd = f->bits_per_raw_sample <= 8;
1027 int packed = !src[1];
1028 int transparency = f->transparency;
1029 int packed_size = (3 + transparency)*2;
1031 for (y = 0; y < h; y++) {
1032 int lastr=0, lastg=0, lastb=0;
1033 for (p = 0; p < 3; p++)
1034 sample[p] = sc->sample_buffer + p*w;
1036 for (x = 0; x < w; x++) {
1037 int b, g, r;
1038 int ab, ag, ar;
1039 if (lbd) {
1040 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
1041 b = v & 0xFF;
1042 g = (v >> 8) & 0xFF;
1043 r = (v >> 16) & 0xFF;
1044 } else if (packed) {
1045 const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
1046 r = p[0];
1047 g = p[1];
1048 b = p[2];
1049 } else if (f->use32bit || transparency) {
1050 g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
1051 b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
1052 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
1053 } else {
1054 b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
1055 g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
1056 r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
1059 ar = r - lastr;
1060 ag = g - lastg;
1061 ab = b - lastb;
1062 if (x && y) {
1063 int bg = ag - sample[0][x];
1064 int bb = ab - sample[1][x];
1065 int br = ar - sample[2][x];
1067 br -= bg;
1068 bb -= bg;
1070 for (i = 0; i<NB_Y_COEFF; i++) {
1071 stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1075 sample[0][x] = ag;
1076 sample[1][x] = ab;
1077 sample[2][x] = ar;
1079 lastr = r;
1080 lastg = g;
1081 lastb = b;
1085 best = 0;
1086 for (i=1; i<NB_Y_COEFF; i++) {
1087 if (stat[i] < stat[best])
1088 best = i;
1091 sc->slice_rct_by_coef = rct_y_coeff[best][1];
1092 sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1095 static int encode_slice(AVCodecContext *c, void *arg)
1097 FFV1SliceContext *sc = arg;
1098 FFV1Context *f = c->priv_data;
1099 int width = sc->slice_width;
1100 int height = sc->slice_height;
1101 int x = sc->slice_x;
1102 int y = sc->slice_y;
1103 const AVFrame *const p = f->cur_enc_frame;
1104 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1105 int ret;
1106 RangeCoder c_bak = sc->c;
1107 const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1108 p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1109 p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1110 p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1111 int ac = f->ac;
1113 sc->slice_coding_mode = 0;
1114 if (f->version > 3 && f->colorspace == 1) {
1115 choose_rct_params(f, sc, planes, p->linesize, width, height);
1116 } else {
1117 sc->slice_rct_by_coef = 1;
1118 sc->slice_rct_ry_coef = 1;
1121 retry:
1122 if (f->key_frame)
1123 ff_ffv1_clear_slice_state(f, sc);
1124 if (f->version > 2) {
1125 encode_slice_header(f, sc);
1127 if (ac == AC_GOLOMB_RICE) {
1128 sc->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&sc->c, f->version > 2) : 0;
1129 init_put_bits(&sc->pb,
1130 sc->c.bytestream_start + sc->ac_byte_count,
1131 sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count);
1134 if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8) {
1135 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
1136 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1137 const int cx = x >> f->chroma_h_shift;
1138 const int cy = y >> f->chroma_v_shift;
1140 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1, ac);
1142 if (f->chroma_planes) {
1143 ret |= encode_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, ac);
1144 ret |= encode_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1, ac);
1146 if (f->transparency)
1147 ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1, ac);
1148 } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
1149 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2, ac);
1150 ret |= encode_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2, ac);
1151 } else if (f->use32bit) {
1152 ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize);
1153 } else {
1154 ret = encode_rgb_frame(f, sc, planes, width, height, p->linesize);
1157 if (ac != AC_GOLOMB_RICE) {
1158 sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
1159 } else {
1160 flush_put_bits(&sc->pb); // FIXME: nicer padding
1161 sc->ac_byte_count += put_bytes_output(&sc->pb);
1164 if (ret < 0) {
1165 av_assert0(sc->slice_coding_mode == 0);
1166 if (f->version < 4) {
1167 av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1168 return ret;
1170 av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1171 ac = 1;
1172 sc->slice_coding_mode = 1;
1173 sc->c = c_bak;
1174 goto retry;
1177 return 0;
1180 size_t ff_ffv1_encode_buffer_size(AVCodecContext *avctx)
1182 FFV1Context *f = avctx->priv_data;
1184 size_t maxsize = avctx->width*avctx->height * (1 + f->transparency);
1185 if (f->chroma_planes)
1186 maxsize += AV_CEIL_RSHIFT(avctx->width, f->chroma_h_shift) * AV_CEIL_RSHIFT(f->height, f->chroma_v_shift) * 2;
1187 maxsize += f->slice_count * 800; //for slice header
1188 if (f->version > 3) {
1189 maxsize *= f->bits_per_raw_sample + 1;
1190 } else {
1191 maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
1192 maxsize *= 8*(2*f->bits_per_raw_sample + 5);
1194 maxsize >>= 3;
1195 maxsize += FF_INPUT_BUFFER_MIN_SIZE;
1197 return maxsize;
1200 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1201 const AVFrame *pict, int *got_packet)
1203 FFV1Context *f = avctx->priv_data;
1204 RangeCoder *const c = &f->slices[0].c;
1205 uint8_t keystate = 128;
1206 uint8_t *buf_p;
1207 int i, ret;
1208 int64_t maxsize;
1210 if(!pict) {
1211 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1212 int j, k, m;
1213 char *p = avctx->stats_out;
1214 char *end = p + STATS_OUT_SIZE;
1216 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1217 for (i = 0; i < f->quant_table_count; i++)
1218 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1220 av_assert0(f->slice_count == f->max_slice_count);
1221 for (j = 0; j < f->slice_count; j++) {
1222 const FFV1SliceContext *sc = &f->slices[j];
1223 for (i = 0; i < 256; i++) {
1224 f->rc_stat[i][0] += sc->rc_stat[i][0];
1225 f->rc_stat[i][1] += sc->rc_stat[i][1];
1227 for (i = 0; i < f->quant_table_count; i++) {
1228 for (k = 0; k < f->context_count[i]; k++)
1229 for (m = 0; m < 32; m++) {
1230 f->rc_stat2[i][k][m][0] += sc->rc_stat2[i][k][m][0];
1231 f->rc_stat2[i][k][m][1] += sc->rc_stat2[i][k][m][1];
1236 for (j = 0; j < 256; j++) {
1237 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1238 f->rc_stat[j][0], f->rc_stat[j][1]);
1239 p += strlen(p);
1241 snprintf(p, end - p, "\n");
1243 for (i = 0; i < f->quant_table_count; i++) {
1244 for (j = 0; j < f->context_count[i]; j++)
1245 for (m = 0; m < 32; m++) {
1246 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1247 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1248 p += strlen(p);
1251 snprintf(p, end - p, "%d\n", f->gob_count);
1253 return 0;
1256 /* Maximum packet size */
1257 maxsize = ff_ffv1_encode_buffer_size(avctx);
1259 if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1260 av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1261 maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
1264 if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
1265 return ret;
1267 ff_init_range_encoder(c, pkt->data, pkt->size);
1268 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1270 f->cur_enc_frame = pict;
1272 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
1273 put_rac(c, &keystate, 1);
1274 f->key_frame = 1;
1275 f->gob_count++;
1276 write_header(f);
1277 } else {
1278 put_rac(c, &keystate, 0);
1279 f->key_frame = 0;
1282 if (f->ac == AC_RANGE_CUSTOM_TAB) {
1283 int i;
1284 for (i = 1; i < 256; i++) {
1285 c->one_state[i] = f->state_transition[i];
1286 c->zero_state[256 - i] = 256 - c->one_state[i];
1290 for (i = 0; i < f->slice_count; i++) {
1291 FFV1SliceContext *sc = &f->slices[i];
1292 uint8_t *start = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1293 int len = pkt->size / f->slice_count;
1294 if (i) {
1295 ff_init_range_encoder(&sc->c, start, len);
1296 } else {
1297 av_assert0(sc->c.bytestream_end >= sc->c.bytestream_start + len);
1298 av_assert0(sc->c.bytestream < sc->c.bytestream_start + len);
1299 sc->c.bytestream_end = sc->c.bytestream_start + len;
1302 avctx->execute(avctx, encode_slice, f->slices, NULL,
1303 f->slice_count, sizeof(*f->slices));
1305 buf_p = pkt->data;
1306 for (i = 0; i < f->slice_count; i++) {
1307 FFV1SliceContext *sc = &f->slices[i];
1308 int bytes = sc->ac_byte_count;
1309 if (i > 0 || f->version > 2) {
1310 av_assert0(bytes < pkt->size / f->slice_count);
1311 memmove(buf_p, sc->c.bytestream_start, bytes);
1312 av_assert0(bytes < (1 << 24));
1313 AV_WB24(buf_p + bytes, bytes);
1314 bytes += 3;
1316 if (f->ec) {
1317 unsigned v;
1318 buf_p[bytes++] = 0;
1319 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, buf_p, bytes) ^ (f->crcref ? 0x8CD88196 : 0);
1320 AV_WL32(buf_p + bytes, v);
1321 bytes += 4;
1323 buf_p += bytes;
1326 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1327 avctx->stats_out[0] = '\0';
1329 f->picture_number++;
1330 pkt->size = buf_p - pkt->data;
1331 pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1332 *got_packet = 1;
1334 return 0;
1337 static av_cold int encode_close(AVCodecContext *avctx)
1339 FFV1Context *const s = avctx->priv_data;
1341 av_freep(&avctx->stats_out);
1342 ff_ffv1_close(s);
1344 return 0;
1347 #define OFFSET(x) offsetof(FFV1Context, x)
1348 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1349 static const AVOption options[] = {
1350 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },
1351 { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1352 { .i64 = 0 }, -2, 2, VE, .unit = "coder" },
1353 { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1354 { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1355 { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1356 { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1357 { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1358 { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1359 { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
1360 { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1361 { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1362 { .i64 = 0 }, 0, 1, VE },
1363 { "qtable", "Quantization table", OFFSET(qtable), AV_OPT_TYPE_INT,
1364 { .i64 = -1 }, -1, 2, VE , .unit = "qtable"},
1365 { "default", NULL, 0, AV_OPT_TYPE_CONST,
1366 { .i64 = QTABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1367 { "8bit", NULL, 0, AV_OPT_TYPE_CONST,
1368 { .i64 = QTABLE_8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1369 { "greater8bit", NULL, 0, AV_OPT_TYPE_CONST,
1370 { .i64 = QTABLE_GT8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1372 { NULL }
1375 static const AVClass ffv1_class = {
1376 .class_name = "ffv1 encoder",
1377 .item_name = av_default_item_name,
1378 .option = options,
1379 .version = LIBAVUTIL_VERSION_INT,
1382 const FFCodec ff_ffv1_encoder = {
1383 .p.name = "ffv1",
1384 CODEC_LONG_NAME("FFmpeg video codec #1"),
1385 .p.type = AVMEDIA_TYPE_VIDEO,
1386 .p.id = AV_CODEC_ID_FFV1,
1387 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1388 AV_CODEC_CAP_SLICE_THREADS |
1389 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1390 .priv_data_size = sizeof(FFV1Context),
1391 .init = encode_init_internal,
1392 FF_CODEC_ENCODE_CB(encode_frame),
1393 .close = encode_close,
1394 .p.pix_fmts = (const enum AVPixelFormat[]) {
1395 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
1396 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
1397 AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
1398 AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
1399 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1400 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1401 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
1402 AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12,
1403 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
1404 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
1405 AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
1406 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRAP14,
1407 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
1408 AV_PIX_FMT_YA8,
1409 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
1410 AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48,
1411 AV_PIX_FMT_GBRAP16, AV_PIX_FMT_RGBA64,
1412 AV_PIX_FMT_GRAY9,
1413 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
1414 AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
1415 AV_PIX_FMT_NONE
1418 .color_ranges = AVCOL_RANGE_MPEG,
1419 .p.priv_class = &ffv1_class,
1420 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,