FFmpeg
Loading...
Searching...
No Matches
ffv1enc.c
Go to the documentation of this file.
1/*
2 * FFV1 encoder
3 *
4 * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23/**
24 * @file
25 * FF Video Codec 1 (a lossless codec) encoder
26 */
27
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"
34#include "libavutil/qsort.h"
35
36#include "avcodec.h"
37#include "encode.h"
38#include "codec_internal.h"
39#include "put_bits.h"
40#include "put_golomb.h"
41#include "rangecoder.h"
42#include "ffv1.h"
43#include "ffv1enc.h"
44
45static const int8_t quant5_10bit[256] = {
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49 1, 1, 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, -2,
58 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
62};
63
64static const int8_t quant5[256] = {
65 0, 1, 1, 1, 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, -2, -2, -2,
80 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
81};
82
83static const int8_t quant9_10bit[256] = {
84 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
86 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
87 3, 3, 3, 3, 3, 3, 3, 3, 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, -4, -4, -4, -4, -4, -4, -4,
96 -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
97 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
98 -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
99 -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
100};
101
102static const int8_t quant11[256] = {
103 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
104 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
105 4, 4, 4, 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, -5, -5,
116 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
117 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
118 -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
119};
120
121static const uint8_t ver2_state[256] = {
122 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
123 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
124 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
125 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
126 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
127 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
128 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
129 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
130 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
131 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
132 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
133 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
134 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
135 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
136 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
137 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
138};
139
140static void find_best_state(uint8_t best_state[256][256],
141 const uint8_t one_state[256])
142{
143 int i, j, k, m;
144 uint32_t l2tab[256];
145
146 for (i = 1; i < 256; i++)
147 l2tab[i] = -log2(i / 256.0) * ((1U << 31) / 8);
148
149 for (i = 0; i < 256; i++) {
150 uint64_t best_len[256];
151
152 for (j = 0; j < 256; j++)
153 best_len[j] = UINT64_MAX;
154
155 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
156 uint32_t occ[256] = { 0 };
157 uint64_t len = 0;
158 occ[j] = UINT32_MAX;
159
160 if (!one_state[j])
161 continue;
162
163 for (k = 0; k < 256; k++) {
164 uint32_t newocc[256] = { 0 };
165 for (m = 1; m < 256; m++)
166 if (occ[m]) {
167 len += (occ[m]*(( i *(uint64_t)l2tab[ m]
168 + (256-i)*(uint64_t)l2tab[256-m])>>8)) >> 8;
169 }
170 if (len < best_len[k]) {
171 best_len[k] = len;
172 best_state[i][k] = j;
173 }
174 for (m = 1; m < 256; m++)
175 if (occ[m]) {
176 newocc[ one_state[ m]] += occ[m] * (uint64_t) i >> 8;
177 newocc[256 - one_state[256 - m]] += occ[m] * (uint64_t)(256 - i) >> 8;
178 }
179 memcpy(occ, newocc, sizeof(occ));
180 }
181 }
182 }
183}
184
186 uint8_t *state, int v,
187 int is_signed,
188 uint64_t rc_stat[256][2],
189 uint64_t rc_stat2[32][2])
190{
191 int i;
192
193#define put_rac(C, S, B) \
194 do { \
195 if (rc_stat) { \
196 rc_stat[*(S)][B]++; \
197 rc_stat2[(S) - state][B]++; \
198 } \
199 put_rac(C, S, B); \
200 } while (0)
201
202 if (v) {
203 const unsigned a = is_signed ? FFABS(v) : v;
204 const int e = av_log2(a);
205 put_rac(c, state + 0, 0);
206 if (e <= 9) {
207 for (i = 0; i < e; i++)
208 put_rac(c, state + 1 + i, 1); // 1..10
209 put_rac(c, state + 1 + i, 0);
210
211 for (i = e - 1; i >= 0; i--)
212 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
213
214 if (is_signed)
215 put_rac(c, state + 11 + e, v < 0); // 11..21
216 } else {
217 for (i = 0; i < e; i++)
218 put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
219 put_rac(c, state + 1 + 9, 0);
220
221 for (i = e - 1; i >= 0; i--)
222 put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
223
224 if (is_signed)
225 put_rac(c, state + 11 + 10, v < 0); // 11..21
226 }
227 } else {
228 put_rac(c, state + 0, 1);
229 }
230#undef put_rac
231}
232
233static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
234 int v, int is_signed)
235{
236 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
237}
238
239
240static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
241 int v, int bits)
242{
243 int i, k, code;
244 v = fold(v - state->bias, bits);
245
246 i = state->count;
247 k = 0;
248 while (i < state->error_sum) { // FIXME: optimize
249 k++;
250 i += i;
251 }
252
253 av_assert2(k <= 19);
254
255 code = v ^ ((2 * state->drift + state->count) >> 31);
256
257 ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
258 state->bias, state->error_sum, state->drift, state->count, k);
259 set_sr_golomb(pb, code, k, 12, bits);
260
262}
263
264#define TYPE int16_t
265#define RENAME(name) name
266#include "ffv1enc_template.c"
267#undef TYPE
268#undef RENAME
269
270#define TYPE int32_t
271#define RENAME(name) name ## 32
272#include "ffv1enc_template.c"
273
275 const uint8_t *src, int w, int h,
276 int stride, int plane_index, int remap_index, int pixel_stride, int ac)
277{
278 int x, y, i, ret;
279 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
280 const int ring_size = f->context_model ? 3 : 2;
281 int16_t *sample[3];
282 sc->run_index = 0;
283
284 sample[2] = sc->sample_buffer; // dummy to avoid UB pointer arithmetic
285
286 memset(sc->sample_buffer, 0, ring_size * (w + 6) * sizeof(*sc->sample_buffer));
287
288 for (y = 0; y < h; y++) {
289 for (i = 0; i < ring_size; i++)
290 sample[i] = sc->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
291
292 sample[0][-1]= sample[1][0 ];
293 sample[1][ w]= sample[1][w-1];
294
295 if (f->bits_per_raw_sample <= 8) {
296 for (x = 0; x < w; x++)
297 sample[0][x] = src[x * pixel_stride + stride * y];
298 if (sc->remap)
299 for (x = 0; x < w; x++)
300 sample[0][x] = sc->fltmap[remap_index][ sample[0][x] ];
301
302 if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, 8, ac, pass1)) < 0)
303 return ret;
304 } else {
305 if (f->packed_at_lsb) {
306 for (x = 0; x < w; x++) {
307 sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride];
308 }
309 } else {
310 for (x = 0; x < w; x++) {
311 sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample);
312 }
313 }
314 if (sc->remap)
315 for (x = 0; x < w; x++)
316 sample[0][x] = sc->fltmap[remap_index][ (uint16_t)sample[0][x] ];
317
318 if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, f->bits_per_raw_sample, ac, pass1)) < 0)
319 return ret;
320 }
321 }
322 return 0;
323}
324
326 const uint8_t *src, int w, int h,
327 int stride, int remap_index, int pixel_stride)
328{
329 int x, y;
330
331 memset(sc->fltmap[remap_index], 0, 65536 * sizeof(*sc->fltmap[remap_index]));
332
333 for (y = 0; y < h; y++) {
334 if (f->bits_per_raw_sample <= 8) {
335 for (x = 0; x < w; x++)
336 sc->fltmap[remap_index][ src[x * pixel_stride + stride * y] ] = 1;
337 } else {
338 if (f->packed_at_lsb) {
339 for (x = 0; x < w; x++)
340 sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] ] = 1;
341 } else {
342 for (x = 0; x < w; x++)
343 sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample) ] = 1;
344 }
345 }
346 }
347}
348
350{
351 int last = 0;
352 int i;
353 uint8_t state[CONTEXT_SIZE];
354 memset(state, 128, sizeof(state));
355
356 for (i = 1; i < MAX_QUANT_TABLE_SIZE/2; i++)
357 if (quant_table[i] != quant_table[i - 1]) {
358 put_symbol(c, state, i - last - 1, 0);
359 last = i;
360 }
361 put_symbol(c, state, i - last - 1, 0);
362}
363
366{
367 int i;
368 for (i = 0; i < 5; i++)
370}
371
372static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
373 int nb_contexts)
374{
375 if (!initial_state)
376 return 0;
377 for (int i = 0; i < nb_contexts; i++)
378 for (int j = 0; j < CONTEXT_SIZE; j++)
379 if (initial_state[i][j] != 128)
380 return 1;
381 return 0;
382}
383
385{
386 uint8_t state[CONTEXT_SIZE];
387 int i, j;
388 RangeCoder *const c = &f->slices[0].c;
389
390 memset(state, 128, sizeof(state));
391
392 if (f->version < 2) {
393 put_symbol(c, state, f->version, 0);
394 put_symbol(c, state, f->ac, 0);
395 if (f->ac == AC_RANGE_CUSTOM_TAB) {
396 for (i = 1; i < 256; i++)
398 f->state_transition[i] - c->one_state[i], 1);
399 }
400 put_symbol(c, state, f->colorspace, 0); //YUV cs type
401 if (f->version > 0)
402 put_symbol(c, state, f->bits_per_raw_sample, 0);
403 put_rac(c, state, f->chroma_planes);
404 put_symbol(c, state, f->chroma_h_shift, 0);
405 put_symbol(c, state, f->chroma_v_shift, 0);
406 put_rac(c, state, f->transparency);
407
408 write_quant_tables(c, f->quant_tables[f->context_model]);
409 } else if (f->version < 3) {
410 put_symbol(c, state, f->slice_count, 0);
411 for (i = 0; i < f->slice_count; i++) {
412 FFV1SliceContext *fs = &f->slices[i];
414 (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
416 (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
418 (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
419 0);
421 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
422 0);
423 for (j = 0; j < f->plane_count; j++) {
424 put_symbol(c, state, fs->plane[j].quant_table_index, 0);
425 av_assert0(fs->plane[j].quant_table_index == f->context_model);
426 }
427 }
428 }
429}
430
432{
433 f->combined_version = f->version << 16;
434 if (f->version > 2) {
435 if (f->version == 3) {
436 f->micro_version = 4;
437 } else if (f->version == 4) {
438 f->micro_version = 10;
439 } else
440 av_assert0(0);
441
442 f->combined_version += f->micro_version;
443 } else
444 av_assert0(f->micro_version == 0);
445}
446
448{
449 FFV1Context *f = avctx->priv_data;
450
452 uint8_t state[CONTEXT_SIZE];
453 int i, j, k;
454 uint8_t state2[32][CONTEXT_SIZE];
455 unsigned v;
456
457 memset(state2, 128, sizeof(state2));
458 memset(state, 128, sizeof(state));
459
460 f->avctx->extradata_size = 10000 + 4 +
461 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
462 f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
463 if (!f->avctx->extradata)
464 return AVERROR(ENOMEM);
465 ff_init_range_encoder(&c, f->avctx->extradata, f->avctx->extradata_size);
466 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
467
468 put_symbol(&c, state, f->version, 0);
469 if (f->version > 2)
470 put_symbol(&c, state, f->micro_version, 0);
471
472 put_symbol(&c, state, f->ac, 0);
473 if (f->ac == AC_RANGE_CUSTOM_TAB)
474 for (i = 1; i < 256; i++)
475 put_symbol(&c, state, f->state_transition[i] - c.one_state[i], 1);
476
477 put_symbol(&c, state, f->colorspace, 0); // YUV cs type
478 put_symbol(&c, state, f->bits_per_raw_sample, 0);
479 put_rac(&c, state, f->chroma_planes);
480 put_symbol(&c, state, f->chroma_h_shift, 0);
481 put_symbol(&c, state, f->chroma_v_shift, 0);
482 put_rac(&c, state, f->transparency);
483 if (f->colorspace == 2)
484 put_symbol(&c, state, f->bayer_order, 0); /* 0 = RGGB */
485 put_symbol(&c, state, f->num_h_slices - 1, 0);
486 put_symbol(&c, state, f->num_v_slices - 1, 0);
487
488 put_symbol(&c, state, f->quant_table_count, 0);
489 for (i = 0; i < f->quant_table_count; i++)
490 write_quant_tables(&c, f->quant_tables[i]);
491
492 for (i = 0; i < f->quant_table_count; i++) {
493 if (contains_non_128(f->initial_states[i], f->context_count[i])) {
494 put_rac(&c, state, 1);
495 for (j = 0; j < f->context_count[i]; j++)
496 for (k = 0; k < CONTEXT_SIZE; k++) {
497 int pred = j ? f->initial_states[i][j - 1][k] : 128;
498 put_symbol(&c, state2[k],
499 (int8_t)(f->initial_states[i][j][k] - pred), 1);
500 }
501 } else {
502 put_rac(&c, state, 0);
503 }
504 }
505
506 if (f->version > 2) {
507 put_symbol(&c, state, f->ec, 0);
508 put_symbol(&c, state, f->intra = (f->avctx->gop_size < 2), 0);
509 if (f->combined_version >= 0x40004)
510 put_symbol(&c, state, f->flt, 0);
511 }
512
513 f->avctx->extradata_size = ff_rac_terminate(&c, 0);
514 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, f->avctx->extradata, f->avctx->extradata_size) ^ (f->crcref ? 0x8CD88196 : 0);
515 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
516 f->avctx->extradata_size += 4;
517
518 return 0;
519}
520
521static int sort_stt(FFV1Context *s, uint8_t stt[256])
522{
523 int i, i2, changed, print = 0;
524
525 do {
526 changed = 0;
527 for (i = 12; i < 244; i++) {
528 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
529
530#define COST(old, new) \
531 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
532 s->rc_stat[old][1] * -log2((new) / 256.0)
533
534#define COST2(old, new) \
535 COST(old, new) + COST(256 - (old), 256 - (new))
536
537 double size0 = COST2(i, i) + COST2(i2, i2);
538 double sizeX = COST2(i, i2) + COST2(i2, i);
539 if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
540 int j;
541 FFSWAP(int, stt[i], stt[i2]);
542 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
543 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
544 if (i != 256 - i2) {
545 FFSWAP(int, stt[256 - i], stt[256 - i2]);
546 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
547 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
548 }
549 for (j = 1; j < 256; j++) {
550 if (stt[j] == i)
551 stt[j] = i2;
552 else if (stt[j] == i2)
553 stt[j] = i;
554 if (i != 256 - i2) {
555 if (stt[256 - j] == 256 - i)
556 stt[256 - j] = 256 - i2;
557 else if (stt[256 - j] == 256 - i2)
558 stt[256 - j] = 256 - i;
559 }
560 }
561 print = changed = 1;
562 }
563 }
564 }
565 } while (changed);
566 return print;
567}
568
569
571{
572 FFV1Context *s = avctx->priv_data;
573 int plane_count = 1 + 2*s->chroma_planes + s->bayer + s->transparency;
574 int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->bayer ? 1 : s->chroma_h_shift);
575 int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->bayer ? 1 : s->chroma_v_shift);
576 s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
577 s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
578 for (; s->num_v_slices <= 32; s->num_v_slices++) {
579 for (s->num_h_slices = s->num_v_slices; s->num_h_slices <= 2*s->num_v_slices; s->num_h_slices++) {
580 int maxw = (avctx->width + s->num_h_slices - 1) / s->num_h_slices;
581 int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
582 if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
583 continue;
584 if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
585 continue;
586 if (s->version < 4)
587 if ( ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
588 ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
589 continue;
590 if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES)
591 return 0;
592 if (maxw*maxh > 360*288)
593 continue;
594 if (!avctx->slices)
595 return 0;
596 }
597 }
598 av_log(avctx, AV_LOG_ERROR,
599 "Unsupported number %d of slices requested, please specify a "
600 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
601 avctx->slices);
602 return AVERROR(ENOSYS);
603}
604
606{
607 FFV1Context *s = avctx->priv_data;
608 int i, j, k, m, ret;
609
610 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
611 avctx->slices > 1)
612 s->version = FFMAX(s->version, 2);
613
614 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) && s->ac == AC_GOLOMB_RICE) {
615 av_log(avctx, AV_LOG_ERROR, "2 Pass mode is not possible with golomb coding\n");
616 return AVERROR(EINVAL);
617 }
618
619 // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
620 if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
621 s->version = FFMAX(s->version, 2);
622
623 if (avctx->level <= 0 && s->version == 2) {
624 s->version = 3;
625 }
626 if (avctx->level >= 0 && avctx->level <= 4) {
627 if (avctx->level < s->version) {
628 av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
629 return AVERROR(EINVAL);
630 }
631 s->version = avctx->level;
632 } else if (s->version < 3)
633 s->version = 3;
634
635 if (s->ec < 0) {
636 if (s->version >= 4) {
637 s->ec = 2;
638 } else if (s->version >= 3) {
639 s->ec = 1;
640 } else
641 s->ec = 0;
642 }
643
644 // CRC requires version 3+
645 if (s->ec == 1)
646 s->version = FFMAX(s->version, 3);
647 if (s->ec == 2) {
648 s->version = FFMAX(s->version, 4);
649 s->crcref = 0x7a8c4079;
650 }
651
652 if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
653 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");
654 return AVERROR_INVALIDDATA;
655 }
656
657 if (s->ac == AC_RANGE_CUSTOM_TAB) {
658 for (i = 1; i < 256; i++)
659 s->state_transition[i] = ver2_state[i];
660 } else {
662 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
663 for (i = 1; i < 256; i++)
664 s->state_transition[i] = c.one_state[i];
665 }
666
667 for (i = 0; i < 256; i++) {
668 s->quant_table_count = 2;
669 if ((s->qtable == -1 && s->bits_per_raw_sample <= 8) || s->qtable == 1) {
670 s->quant_tables[0][0][i]= quant11[i];
671 s->quant_tables[0][1][i]= 11*quant11[i];
672 s->quant_tables[0][2][i]= 11*11*quant11[i];
673 s->quant_tables[1][0][i]= quant11[i];
674 s->quant_tables[1][1][i]= 11*quant11[i];
675 s->quant_tables[1][2][i]= 11*11*quant5 [i];
676 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
677 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
678 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
679 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
680 } else {
681 s->quant_tables[0][0][i]= quant9_10bit[i];
682 s->quant_tables[0][1][i]= 9*quant9_10bit[i];
683 s->quant_tables[0][2][i]= 9*9*quant9_10bit[i];
684 s->quant_tables[1][0][i]= quant9_10bit[i];
685 s->quant_tables[1][1][i]= 9*quant9_10bit[i];
686 s->quant_tables[1][2][i]= 9*9*quant5_10bit[i];
687 s->quant_tables[1][3][i]= 5*9*9*quant5_10bit[i];
688 s->quant_tables[1][4][i]= 5*5*9*9*quant5_10bit[i];
689 s->context_count[0] = (9 * 9 * 9 + 1) / 2;
690 s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
691 }
692 }
693
694 /* Small context set: only the two nearest gradients, coarsely quantized.
695 * With many slices there is too little data per slice for large context
696 * sets to adapt, and this codes smaller while using a fraction of the
697 * state. */
698 if (s->context_model == 2) {
699 const int8_t *q5 = (s->qtable == -1 && s->bits_per_raw_sample <= 8) || s->qtable == 1 ?
701 s->quant_table_count = 3;
702 for (i = 0; i < 256; i++) {
703 s->quant_tables[2][0][i] = q5[i];
704 s->quant_tables[2][1][i] = 5*q5[i];
705 }
706 s->context_count[2] = (5 * 5 + 1) / 2;
707 }
708
710 return ret;
711
712 if (!s->transparency)
713 s->plane_count = 2;
714 if (!s->chroma_planes && s->version > 3)
715 s->plane_count--;
716 if (s->bayer)
717 s->plane_count = 3;
718
719 s->picture_number = 0;
720
722 for (i = 0; i < s->quant_table_count; i++) {
723 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
724 sizeof(*s->rc_stat2[i]));
725 if (!s->rc_stat2[i])
726 return AVERROR(ENOMEM);
727 }
728 }
729 if (avctx->stats_in) {
730 char *p = avctx->stats_in;
731 uint8_t (*best_state)[256] = av_malloc_array(256, 256);
732 int gob_count = 0;
733 char *next;
734 if (!best_state)
735 return AVERROR(ENOMEM);
736
737 av_assert0(s->version >= 2);
738
739 for (;;) {
740 for (j = 0; j < 256; j++)
741 for (i = 0; i < 2; i++) {
742 s->rc_stat[j][i] = strtol(p, &next, 0);
743 if (next == p) {
744 av_log(avctx, AV_LOG_ERROR,
745 "2Pass file invalid at %d %d [%s]\n", j, i, p);
746 av_freep(&best_state);
747 return AVERROR_INVALIDDATA;
748 }
749 p = next;
750 }
751 for (i = 0; i < s->quant_table_count; i++)
752 for (j = 0; j < s->context_count[i]; j++) {
753 for (k = 0; k < 32; k++)
754 for (m = 0; m < 2; m++) {
755 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
756 if (next == p) {
757 av_log(avctx, AV_LOG_ERROR,
758 "2Pass file invalid at %d %d %d %d [%s]\n",
759 i, j, k, m, p);
760 av_freep(&best_state);
761 return AVERROR_INVALIDDATA;
762 }
763 p = next;
764 }
765 }
766 gob_count = strtol(p, &next, 0);
767 if (next == p || gob_count <= 0) {
768 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
769 av_freep(&best_state);
770 return AVERROR_INVALIDDATA;
771 }
772 p = next;
773 while (*p == '\n' || *p == ' ')
774 p++;
775 if (p[0] == 0)
776 break;
777 }
778 if (s->ac == AC_RANGE_CUSTOM_TAB)
779 sort_stt(s, s->state_transition);
780
781 find_best_state(best_state, s->state_transition);
782
783 for (i = 0; i < s->quant_table_count; i++) {
784 for (k = 0; k < 32; k++) {
785 double a=0, b=0;
786 int jp = 0;
787 for (j = 0; j < s->context_count[i]; j++) {
788 double p = 128;
789 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
790 if (a+b)
791 p = 256.0 * b / (a + b);
792 s->initial_states[i][jp][k] =
793 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
794 for(jp++; jp<j; jp++)
795 s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
796 a=b=0;
797 }
798 a += s->rc_stat2[i][j][k][0];
799 b += s->rc_stat2[i][j][k][1];
800 if (a+b) {
801 p = 256.0 * b / (a + b);
802 }
803 s->initial_states[i][j][k] =
804 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
805 }
806 }
807 }
808 av_freep(&best_state);
809 }
810
811 if (s->version <= 1) {
812 /* Disable slices when the version doesn't support them */
813 s->num_h_slices = 1;
814 s->num_v_slices = 1;
815 }
816
818
819 return 0;
820}
821
824{
825 FFV1Context *s = avctx->priv_data;
827
828 s->bayer = 0;
829 s->plane_count = 3;
830 switch(pix_fmt) {
831 case AV_PIX_FMT_GRAY9:
838 if (!avctx->bits_per_raw_sample)
839 s->bits_per_raw_sample = 9;
849 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
850 s->bits_per_raw_sample = 10;
859 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
860 s->bits_per_raw_sample = 12;
866 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
867 s->bits_per_raw_sample = 14;
868 s->packed_at_lsb = 1;
871 case AV_PIX_FMT_P016:
872 case AV_PIX_FMT_P216:
873 case AV_PIX_FMT_P416:
881 case AV_PIX_FMT_YAF16:
882 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
883 s->bits_per_raw_sample = 16;
884 } else if (!s->bits_per_raw_sample) {
885 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
886 }
887 if (s->bits_per_raw_sample <= 8) {
888 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
889 return AVERROR_INVALIDDATA;
890 }
891 s->version = FFMAX(s->version, 1);
893 case AV_PIX_FMT_GRAY8:
894 case AV_PIX_FMT_YA8:
895 case AV_PIX_FMT_NV12:
896 case AV_PIX_FMT_NV16:
897 case AV_PIX_FMT_NV24:
907 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
908 s->colorspace = 0;
909 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
910 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
911 s->bits_per_raw_sample = 8;
912 else if (!s->bits_per_raw_sample)
913 s->bits_per_raw_sample = 8;
914 break;
915 case AV_PIX_FMT_RGB32:
916 s->colorspace = 1;
917 s->transparency = 1;
918 s->chroma_planes = 1;
919 s->bits_per_raw_sample = 8;
920 break;
922 s->colorspace = 1;
923 s->transparency = 1;
924 s->chroma_planes = 1;
925 s->bits_per_raw_sample = 16;
926 s->use32bit = 1;
927 s->version = FFMAX(s->version, 1);
928 break;
929 case AV_PIX_FMT_RGB48:
930 s->colorspace = 1;
931 s->chroma_planes = 1;
932 s->bits_per_raw_sample = 16;
933 s->use32bit = 1;
934 s->version = FFMAX(s->version, 1);
935 break;
937 s->colorspace = 2;
938 s->chroma_planes = 1;
939 s->bits_per_raw_sample = 16;
940 s->use32bit = 1;
941 s->version = FFMAX(s->version, 4);
942 s->bayer = 1;
943 break;
944 case AV_PIX_FMT_GBRP:
946 s->colorspace = 1;
947 s->chroma_planes = 1;
948 s->bits_per_raw_sample = 8;
949 break;
950 case AV_PIX_FMT_GBRP9:
951 if (!avctx->bits_per_raw_sample)
952 s->bits_per_raw_sample = 9;
958 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
959 s->bits_per_raw_sample = 10;
963 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
964 s->bits_per_raw_sample = 12;
968 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
969 s->bits_per_raw_sample = 14;
975 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
976 s->bits_per_raw_sample = 16;
980 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
981 s->bits_per_raw_sample = 32;
982 else if (!s->bits_per_raw_sample)
983 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
984 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
985 s->colorspace = 1;
986 s->chroma_planes = 1;
987 if (s->bits_per_raw_sample >= 16) {
988 s->use32bit = 1;
989 }
990 s->version = FFMAX(s->version, 1);
991 break;
992 default:
993 av_log(avctx, AV_LOG_ERROR, "format %s not supported\n",
995 return AVERROR(ENOSYS);
996 }
997 s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
998 if (s->flt || s->remap_mode > 0)
999 s->version = FFMAX(s->version, 4);
1000 av_assert0(s->bits_per_raw_sample >= 8);
1001
1002 if (s->remap_mode < 0)
1003 s->remap_mode = s->flt ? 2 : 0;
1004 if (s->remap_mode == 0 && s->bits_per_raw_sample == 32) {
1005 av_log(avctx, AV_LOG_ERROR, "32bit requires remap\n");
1006 return AVERROR(EINVAL);
1007 }
1008 if (s->remap_mode == 2 &&
1009 !((s->bits_per_raw_sample == 16 || s->bits_per_raw_sample == 32 || s->bits_per_raw_sample == 64) && s->flt)) {
1010 av_log(avctx, AV_LOG_ERROR, "remap 2 is for float16/32/64 only\n");
1011 return AVERROR(EINVAL);
1012 }
1013
1014 return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
1015}
1016
1018{
1019 int ret;
1020 FFV1Context *s = avctx->priv_data;
1021
1022 if ((ret = ff_ffv1_common_init(avctx, s)) < 0)
1023 return ret;
1024
1025 if (s->ac == 1) // Compatibility with common command line usage
1026 s->ac = AC_RANGE_CUSTOM_TAB;
1027 else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
1028 s->ac = AC_RANGE_DEFAULT_TAB;
1029
1031 if (ret < 0)
1032 return ret;
1033
1034 if (s->bayer && (avctx->width & 1 || avctx->height & 1)) {
1035 av_log(avctx, AV_LOG_ERROR, "bayer requires even dimensions\n");
1036 return AVERROR(EINVAL);
1037 }
1038
1039 if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8) && !s->remap_mode) {
1040 if (s->ac == AC_GOLOMB_RICE) {
1041 av_log(avctx, AV_LOG_INFO,
1042 "high bits_per_raw_sample, forcing range coder\n");
1043 s->ac = AC_RANGE_CUSTOM_TAB;
1044 }
1045 }
1046
1047
1048 ret = ff_ffv1_encode_init(avctx);
1049 if (ret < 0)
1050 return ret;
1051
1052 if (s->version > 1) {
1053 if ((ret = ff_ffv1_encode_determine_slices(avctx)) < 0)
1054 return ret;
1055
1056 /* The Golomb-Rice escape code is 12 + bits long and put_bits()
1057 * writes at most 31 bits, so bits must stay below 20. The RCT
1058 * planes of remapped 32bit floats need log2 of the summed remap
1059 * counts of two planes, which a slice of at most 1<<18 pixels
1060 * keeps within 19 bits. */
1061 if (s->ac == AC_GOLOMB_RICE && s->bits_per_raw_sample == 32 &&
1062 ((avctx->width + s->num_h_slices - 1) / s->num_h_slices) *
1063 (int64_t)((avctx->height + s->num_v_slices - 1) / s->num_v_slices) > 1 << 18) {
1064 av_log(avctx, AV_LOG_ERROR, "Slices larger than 262144 pixels are not supported with 32bit floats and the Golomb-Rice coder\n");
1065 return AVERROR(EINVAL);
1066 }
1067
1068 if ((ret = ff_ffv1_write_extradata(avctx)) < 0)
1069 return ret;
1070 }
1071
1072 if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
1073 return ret;
1074 s->slice_count = s->max_slice_count;
1075
1076 for (int j = 0; j < s->slice_count; j++) {
1077 FFV1SliceContext *sc = &s->slices[j];
1078
1079 for (int i = 0; i < s->plane_count; i++) {
1080 PlaneContext *const p = &s->slices[j].plane[i];
1081
1082 p->quant_table_index = s->context_model;
1083 p->context_count = s->context_count[p->quant_table_index];
1084 }
1085 av_assert0(s->remap_mode >= 0);
1086 if (s->remap_mode) {
1087 for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) {
1088 if (s->bits_per_raw_sample == 32) {
1089 sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
1090 if (!sc->unit[p])
1091 return AVERROR(ENOMEM);
1092 sc->bitmap[p] = av_malloc_array(sc->slice_width * sc->slice_height, sizeof(*sc->bitmap[p]));
1093 if (!sc->bitmap[p])
1094 return AVERROR(ENOMEM);
1095 } else {
1096 sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
1097 if (!sc->fltmap[p])
1098 return AVERROR(ENOMEM);
1099 }
1100 }
1101 }
1102
1103 ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
1104
1105 s->slices[j].remap = s->remap_mode;
1106 }
1107
1108 if ((ret = ff_ffv1_init_slices_state(s)) < 0)
1109 return ret;
1110
1111#define STATS_OUT_SIZE 1024 * 1024 * 6
1112 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1114 if (!avctx->stats_out)
1115 return AVERROR(ENOMEM);
1116 for (int i = 0; i < s->quant_table_count; i++)
1117 for (int j = 0; j < s->max_slice_count; j++) {
1118 FFV1SliceContext *sc = &s->slices[j];
1119 av_assert0(!sc->rc_stat2[i]);
1120 sc->rc_stat2[i] = av_mallocz(s->context_count[i] *
1121 sizeof(*sc->rc_stat2[i]));
1122 if (!sc->rc_stat2[i])
1123 return AVERROR(ENOMEM);
1124 }
1125 }
1126
1127 return 0;
1128}
1129
1131{
1132 RangeCoder *c = &sc->c;
1133 uint8_t state[CONTEXT_SIZE];
1134 int j;
1135 memset(state, 128, sizeof(state));
1136
1137 put_symbol(c, state, sc->sx, 0);
1138 put_symbol(c, state, sc->sy, 0);
1139 put_symbol(c, state, 0, 0);
1140 put_symbol(c, state, 0, 0);
1141 for (j=0; j<f->plane_count; j++) {
1143 av_assert0(sc->plane[j].quant_table_index == f->context_model);
1144 }
1145 if (!(f->cur_enc_frame->flags & AV_FRAME_FLAG_INTERLACED))
1146 put_symbol(c, state, 3, 0);
1147 else
1148 put_symbol(c, state, 1 + !(f->cur_enc_frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST), 0);
1149 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.num, 0);
1150 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.den, 0);
1151 if (f->version > 3) {
1152 put_rac(c, state, sc->slice_coding_mode == 1);
1153 if (sc->slice_coding_mode == 1)
1156 if (sc->slice_coding_mode != 1 && f->colorspace != 0) {
1159 }
1160 put_symbol(c, state, sc->remap, 0);
1161 }
1162}
1163
1164#define NB_Y_COEFF 15
1165
1166/* Each contribution to the partial sums is at most 98303,
1167 * so up to 4096 of them fit in an int. */
1169 int partial[NB_Y_COEFF])
1170{
1171 for (int i = 0; i < NB_Y_COEFF; i++) {
1172 stat[i] += partial[i];
1173 partial[i] = 0;
1174 }
1175}
1176
1178 const uint8_t *src[3], const int stride[3], int w, int h)
1179{
1180 static const int rct_y_coeff[15][2] = {
1181 {0, 0}, // 4G
1182 {1, 1}, // R + 2G + B
1183 {2, 2}, // 2R + 2B
1184 {0, 2}, // 2G + 2B
1185 {2, 0}, // 2R + 2G
1186 {4, 0}, // 4R
1187 {0, 4}, // 4B
1188
1189 {0, 3}, // 1G + 3B
1190 {3, 0}, // 3R + 1G
1191 {3, 1}, // 3R + B
1192 {1, 3}, // R + 3B
1193 {1, 2}, // R + G + 2B
1194 {2, 1}, // 2R + G + B
1195 {0, 1}, // 3G + B
1196 {1, 0}, // R + 3G
1197 };
1198
1199 int64_t stat[NB_Y_COEFF] = {0};
1200 int partial[NB_Y_COEFF] = {0};
1201 int x, y, i, p, best;
1202 int16_t *sample[3];
1203 int lbd = f->bits_per_raw_sample <= 8;
1204 int packed = !src[1];
1205 int transparency = f->transparency;
1206 int packed_size = (3 + transparency)*2;
1207
1208 for (y = 0; y < h; y++) {
1209 int lastr=0, lastg=0, lastb=0;
1210 for (p = 0; p < 3; p++)
1211 sample[p] = sc->sample_buffer + p*w;
1212
1213 for (x = 0; x < w; x++) {
1214 int b, g, r;
1215 int ab, ag, ar;
1216 if (lbd) {
1217 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
1218 b = v & 0xFF;
1219 g = (v >> 8) & 0xFF;
1220 r = (v >> 16) & 0xFF;
1221 } else if (packed) {
1222 const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
1223 r = p[0];
1224 g = p[1];
1225 b = p[2];
1226 } else if (f->use32bit || transparency) {
1227 g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
1228 b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
1229 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
1230 } else {
1231 b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
1232 g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
1233 r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
1234 }
1235
1236 ar = r - lastr;
1237 ag = g - lastg;
1238 ab = b - lastb;
1239 if (x && y) {
1240 int bg = ag - sample[0][x];
1241 int bb = ab - sample[1][x];
1242 int br = ar - sample[2][x];
1243
1244 br -= bg;
1245 bb -= bg;
1246
1247 for (i = 0; i<NB_Y_COEFF; i++) {
1248 partial[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1249 }
1250
1251 }
1252 sample[0][x] = ag;
1253 sample[1][x] = ab;
1254 sample[2][x] = ar;
1255
1256 lastr = r;
1257 lastg = g;
1258 lastb = b;
1259 if ((x & 4095) == 4095)
1260 accumulate_rct_stats(stat, partial);
1261 }
1262 accumulate_rct_stats(stat, partial);
1263 }
1264
1265 best = 0;
1266 for (i=1; i<NB_Y_COEFF; i++) {
1267 if (stat[i] < stat[best])
1268 best = i;
1269 }
1270
1271 sc->slice_rct_by_coef = rct_y_coeff[best][1];
1272 sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1273}
1274
1276 const uint8_t *src[4], const int stride[4],
1277 int w, int h)
1278{
1279 static const int rct_y_coeff[NB_Y_COEFF][2] = {
1280 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 0, 2 }, { 2, 0 }, { 4, 0 }, { 0, 4 }, { 0, 3 },
1281 { 3, 0 }, { 3, 1 }, { 1, 3 }, { 1, 2 }, { 2, 1 }, { 0, 1 }, { 1, 0 },
1282 };
1283 int64_t stat[NB_Y_COEFF] = {0};
1284 int partial[NB_Y_COEFF] = {0};
1285 int16_t *sample[3];
1286 int i, best;
1287
1288 /* Walk in 2x2 blocks, build per-block gm/b/r, evaluate prediction-error */
1289 w >>= 1;
1290 for (i = 0; i < 3; i++)
1291 sample[i] = sc->sample_buffer + i*w;
1292
1293 for (int y = 0; y < h; y += 2) {
1294 int last_gm = 0, last_b = 0, last_r = 0;
1295 for (int x = 0; x < w; x++) {
1296 const uint16_t *l1 = (const uint16_t *)(src[0] + stride[0]*(y + 0) + x*2*2);
1297 const uint16_t *l2 = (const uint16_t *)(src[0] + stride[0]*(y + 1) + x*2*2);
1298 int r = l1[0];
1299 int gr = l1[1];
1300 int gb = l2[0];
1301 int b = l2[1];
1302 int gd = gr - gb;
1303 int gm = gb + (gd >> 1);
1304
1305 int agm = gm - last_gm;
1306 int ab = b - last_b;
1307 int ar = r - last_r;
1308
1309 if (x && y) {
1310 int bgm = agm - sample[0][x];
1311 int bb = ab - sample[1][x];
1312 int br = ar - sample[2][x];
1313
1314 br -= bgm;
1315 bb -= bgm;
1316
1317 for (i = 0; i < NB_Y_COEFF; i++)
1318 partial[i] += FFABS(bgm + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1]) >> 2));
1319 }
1320 sample[0][x] = agm;
1321 sample[1][x] = ab;
1322 sample[2][x] = ar;
1323
1324 last_gm = gm;
1325 last_b = b;
1326 last_r = r;
1327 if ((x & 4095) == 4095)
1328 accumulate_rct_stats(stat, partial);
1329 }
1330 accumulate_rct_stats(stat, partial);
1331 }
1332
1333 best = 0;
1334 for (i = 1; i < NB_Y_COEFF; i++)
1335 if (stat[i] < stat[best])
1336 best = i;
1337
1338 sc->slice_rct_by_coef = rct_y_coeff[best][1];
1339 sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1340}
1341
1343{
1344 int len = 1 << f->bits_per_raw_sample;
1345 int flip = sc->remap == 2 ? 0x7FFF : 0;
1346
1347 for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
1348 int j = 0;
1349 int lu = 0;
1350 uint8_t state[2][32];
1351 int run = 0;
1352
1353 memset(state, 128, sizeof(state));
1354 put_symbol(&sc->c, state[0], 0, 0);
1355 memset(state, 128, sizeof(state));
1356 for (int i= 0; i<len; i++) {
1357 int ri = i ^ ((i&0x8000) ? 0 : flip);
1358 int u = sc->fltmap[p][ri];
1359 sc->fltmap[p][ri] = j;
1360 j+= u;
1361
1362 if (lu == u) {
1363 run ++;
1364 } else {
1365 put_symbol_inline(&sc->c, state[lu], run, 0, NULL, NULL);
1366 if (run == 0)
1367 lu = u;
1368 run = 0;
1369 }
1370 }
1371 if (run)
1372 put_symbol(&sc->c, state[lu], run, 0);
1373 sc->remap_count[p] = j;
1374 }
1375}
1376
1378 const uint8_t *src[4],
1379 int w, int h, const int stride[4])
1380{
1381 int x, y;
1382 int transparency = f->transparency;
1383 int i = 0;
1384
1385 for (y = 0; y < h; y++) {
1386 for (x = 0; x < w; x++) {
1387 int b, g, r, av_uninit(a);
1388
1389 g = *((const uint32_t *)(src[0] + x*4 + stride[0]*y));
1390 b = *((const uint32_t *)(src[1] + x*4 + stride[1]*y));
1391 r = *((const uint32_t *)(src[2] + x*4 + stride[2]*y));
1392 if (transparency)
1393 a = *((const uint32_t *)(src[3] + x*4 + stride[3]*y));
1394
1395 if (sc->remap == 2) {
1396#define FLIP(f) (((f)&0x80000000) ? (f) : (f)^0x7FFFFFFF);
1397 g = FLIP(g);
1398 b = FLIP(b);
1399 r = FLIP(r);
1400 }
1401 // We cannot build a histogram as we do for 16bit, we need a bit of magic here
1402 // Its possible to reduce the memory needed at the cost of more dereferencing
1403 sc->unit[0][i].val = g;
1404 sc->unit[0][i].ndx = x + y*w;
1405
1406 sc->unit[1][i].val = b;
1407 sc->unit[1][i].ndx = x + y*w;
1408
1409 sc->unit[2][i].val = r;
1410 sc->unit[2][i].ndx = x + y*w;
1411
1412 if (transparency) {
1413 sc->unit[3][i].val = a;
1414 sc->unit[3][i].ndx = x + y*w;
1415 }
1416 i++;
1417 }
1418 }
1419
1420 //TODO switch to radix sort
1421#define CMP(A,B) ((A)->val - (int64_t)(B)->val)
1422 AV_QSORT(sc->unit[0], i, struct Unit, CMP);
1423 AV_QSORT(sc->unit[1], i, struct Unit, CMP);
1424 AV_QSORT(sc->unit[2], i, struct Unit, CMP);
1425 if (transparency)
1426 AV_QSORT(sc->unit[3], i, struct Unit, CMP);
1427}
1428
1430 int p, int mul_count, int *mul_tab, int update, int final)
1431{
1432 const int pixel_num = sc->slice_width * sc->slice_height;
1433 uint8_t state[2][3][32];
1434 int mul[4096+1];
1435 RangeCoder rc = sc->c;
1436 int lu = 0;
1437 int run = 0;
1438 int64_t last_val = -1;
1439 int compact_index = -1;
1440 int i = 0;
1441 int current_mul_index = -1;
1442 int run1final = 0;
1443 int run1start_i;
1444 int run1start_last_val;
1445 int run1start_mul_index;
1446
1447 memcpy(mul, mul_tab, sizeof(*mul_tab)*(mul_count+1));
1448 memset(state, 128, sizeof(state));
1449 put_symbol(&rc, state[0][0], mul_count, 0);
1450 memset(state, 128, sizeof(state));
1451
1452 for (; i < pixel_num+1; i++) {
1453 int current_mul = current_mul_index < 0 ? 1 : FFABS(mul[current_mul_index]);
1454 int64_t val;
1455 if (i == pixel_num) {
1456 if (last_val == 0xFFFFFFFF && (!run || run1final)) {
1457 break;
1458 } else {
1459 val = last_val + ((1LL<<32) - last_val + current_mul - 1) / current_mul * current_mul;
1460 av_assert2(val >= (1LL<<32));
1461 val += lu * current_mul; //ensure a run1 ends
1462 }
1463 } else
1464 val = sc->unit[p][i].val;
1465
1466 if (last_val != val) {
1467 int64_t delta = val - last_val;
1468 int64_t step = FFMAX(1, (delta + current_mul/2) / current_mul);
1469 av_assert2(last_val < val);
1470 av_assert2(current_mul > 0);
1471
1472 delta -= step*current_mul;
1473 av_assert2(delta <= current_mul/2);
1474 av_assert2(delta > -current_mul);
1475
1476 av_assert2(step > 0);
1477 if (lu) {
1478 if (!run) {
1479 run1start_i = i - 1;
1480 run1start_last_val = last_val;
1481 run1start_mul_index= current_mul_index;
1482 }
1483 if (step == 1) {
1484 if (run1final) {
1485 if (current_mul>1)
1486 put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
1487 }
1488 run ++;
1489 av_assert2(last_val + current_mul + delta == val);
1490 } else {
1491 if (run1final) {
1492 if (run == 0)
1493 lu ^= 1;
1494 i--; // we did not encode val so we need to backstep
1495 last_val += current_mul;
1496 } else {
1497 put_symbol_inline(&rc, state[lu][0], run, 0, NULL, NULL);
1498 i = run1start_i;
1499 last_val = run1start_last_val; // we could compute this instead of storing
1500 current_mul_index = run1start_mul_index;
1501 }
1502 run1final ^= 1;
1503
1504 run = 0;
1505 continue;
1506 }
1507 } else {
1508 av_assert2(run == 0);
1509 av_assert2(run1final == 0);
1510 put_symbol_inline(&rc, state[lu][0], step - 1, 0, NULL, NULL);
1511
1512 if (current_mul > 1)
1513 put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
1514 if (step == 1)
1515 lu ^= 1;
1516
1517 av_assert2(last_val + step * current_mul + delta == val);
1518 }
1519 last_val = val;
1520 current_mul_index = ((last_val + 1) * mul_count) >> 32;
1521 if (!run || run1final) {
1522 av_assert2(mul[ current_mul_index ]);
1523 if (mul[ current_mul_index ] < 0) {
1524 av_assert2(i < pixel_num);
1525 mul[ current_mul_index ] *= -1;
1526 put_symbol_inline(&rc, state[0][2], mul[ current_mul_index ], 0, NULL, NULL);
1527 }
1528 if (i < pixel_num)
1529 compact_index ++;
1530 }
1531 }
1532 if (!run || run1final)
1533 if (final && i < pixel_num)
1534 sc->bitmap[p][sc->unit[p][i].ndx] = compact_index;
1535 }
1536
1537 if (update) {
1538 sc->c = rc;
1539 sc->remap_count[p] = compact_index + 1;
1540 }
1541 return get_rac_count(&rc);
1542}
1543
1545 const uint8_t *src[4])
1546{
1547 int pixel_num = sc->slice_width * sc->slice_height;
1548 const int max_log2_mul_count = ((int[]){ 1, 1, 1, 9, 9, 10})[f->remap_optimizer];
1549 const int log2_mul_count_step = ((int[]){ 1, 1, 1, 9, 9, 1})[f->remap_optimizer];
1550 const int max_log2_mul = ((int[]){ 1, 8, 8, 9, 22, 22})[f->remap_optimizer];
1551 const int log2_mul_step = ((int[]){ 1, 8, 1, 1, 1, 1})[f->remap_optimizer];
1552 const int bruteforce_count = ((int[]){ 0, 0, 0, 1, 1, 1})[f->remap_optimizer];
1553 const int stair_mode = ((int[]){ 0, 0, 0, 1, 0, 0})[f->remap_optimizer];
1554 const int magic_log2 = ((int[]){ 1, 1, 1, 1, 0, 0})[f->remap_optimizer];
1555
1556 for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
1557 int best_log2_mul_count = 0;
1558 float score_sum[11] = {0};
1559 int mul_all[11][1025];
1560
1561 for (int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; log2_mul_count += log2_mul_count_step) {
1562 float score_tab_all[1025][23] = {0};
1563 int64_t last_val = -1;
1564 int *mul_tab = mul_all[log2_mul_count];
1565 int last_mul_index = -1;
1566 int mul_count = 1 << log2_mul_count;
1567
1568 score_sum[log2_mul_count] = 2 * log2_mul_count;
1569 if (magic_log2)
1570 score_sum[log2_mul_count] = av_float2int((float)mul_count * mul_count);
1571 for (int i= 0; i<pixel_num; i++) {
1572 int64_t val = sc->unit[p][i].val;
1573 int mul_index = (val + 1LL)*mul_count >> 32;
1574 if (val != last_val) {
1575 float *score_tab = score_tab_all[(last_val + 1LL)*mul_count >> 32];
1576 av_assert2(last_val < val);
1577 for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1578 int64_t delta = val - last_val;
1579 int mul;
1580 int64_t cost;
1581
1582 if (last_val < 0) {
1583 mul = 1;
1584 } else if (stair_mode && mul_count == 512 && si == max_log2_mul ) {
1585 if (mul_index >= 0x378/8 && mul_index <= 23 + 0x378/8) {
1586 mul = (0x800080 >> (mul_index - 0x378/8));
1587 } else
1588 mul = 1;
1589 } else {
1590 mul = (0x10001LL)<<si >> 16;
1591 }
1592
1593 cost = FFMAX((delta + mul/2) / mul, 1);
1594 float score = 1;
1595 if (mul > 1) {
1596 score *= (FFABS(delta - cost*mul)+1);
1597 if (mul_count > 1)
1598 score *= score;
1599 }
1600 score *= cost;
1601 score *= score;
1602 if (mul_index != last_mul_index)
1603 score *= mul;
1604 if (magic_log2) {
1605 score_tab[si] += av_float2int(score);
1606 } else
1607 score_tab[si] += log2f(score);
1608 }
1609 }
1610 last_val = val;
1611 last_mul_index = mul_index;
1612 }
1613 for(int i= 0; i<mul_count; i++) {
1614 int best_index = 0;
1615 float *score_tab = score_tab_all[i];
1616 for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1617 if (score_tab[si] < score_tab[ best_index ])
1618 best_index = si;
1619 }
1620 if (stair_mode && mul_count == 512 && best_index == max_log2_mul ) {
1621 if (i >= 0x378/8 && i <= 23 + 0x378/8) {
1622 mul_tab[i] = -(0x800080 >> (i - 0x378/8));
1623 } else
1624 mul_tab[i] = -1;
1625 } else
1626 mul_tab[i] = -((0x10001LL)<<best_index >> 16);
1627 score_sum[log2_mul_count] += score_tab[ best_index ];
1628 }
1629 mul_tab[mul_count] = 1;
1630
1631 if (bruteforce_count)
1632 score_sum[log2_mul_count] = encode_float32_remap_segment(sc, p, mul_count, mul_all[log2_mul_count], 0, 0);
1633
1634 if (score_sum[log2_mul_count] < score_sum[best_log2_mul_count])
1635 best_log2_mul_count = log2_mul_count;
1636 }
1637
1638 encode_float32_remap_segment(sc, p, 1<<best_log2_mul_count, mul_all[best_log2_mul_count], 1, 1);
1639 }
1640}
1641
1643 const uint8_t *src[4],
1644 int w, int h, const int stride[4], int ac)
1645{
1646 int x, y, p, i;
1647 const int ring_size = f->context_model ? 3 : 2;
1648 int32_t *sample[4][3];
1649 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
1650 int bits[4], offset;
1651 int transparency = f->transparency;
1652
1653 ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
1654
1655 sc->run_index = 0;
1656
1657 for (int p = 0; p < MAX_PLANES; ++p)
1658 sample[p][2] = sc->sample_buffer32; // dummy to avoid UB pointer arithmetic
1659
1660 memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
1661 (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
1662
1663 for (y = 0; y < h; y++) {
1664 for (i = 0; i < ring_size; i++)
1665 for (p = 0; p < MAX_PLANES; p++)
1666 sample[p][i]= RENAME(sc->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
1667
1668 for (x = 0; x < w; x++) {
1669 int b, g, r, av_uninit(a);
1670 g = sc->bitmap[0][x + w*y];
1671 b = sc->bitmap[1][x + w*y];
1672 r = sc->bitmap[2][x + w*y];
1673 if (transparency)
1674 a = sc->bitmap[3][x + w*y];
1675
1676 if (sc->slice_coding_mode != 1) {
1677 b -= g;
1678 r -= g;
1679 g += (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
1680 b += offset;
1681 r += offset;
1682 }
1683
1684 sample[0][0][x] = g;
1685 sample[1][0][x] = b;
1686 sample[2][0][x] = r;
1687 sample[3][0][x] = a;
1688 }
1689 for (p = 0; p < 3 + transparency; p++) {
1690 int ret;
1691 sample[p][0][-1] = sample[p][1][0 ];
1692 sample[p][1][ w] = sample[p][1][w-1];
1693 ret = encode_line32(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
1694 bits[p], ac, pass1);
1695 if (ret < 0)
1696 return ret;
1697 }
1698 }
1699 return 0;
1700}
1701
1703 const uint8_t *src[4],
1704 int w, int h, const int stride[4], int ac)
1705{
1706 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
1707 const int ring_size = f->context_model ? 3 : 2;
1708 TYPE *sample[4][3];
1709
1710 int bits[4], offset;
1711 ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
1712
1713 w >>= 1;
1714
1715 sc->run_index = 0;
1716
1717 for (int p = 0; p < MAX_PLANES; ++p)
1718 sample[p][2] = RENAME(sc->sample_buffer);
1719
1720 memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
1721 (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
1722
1723 for (int y = 0; y < h; y += 2) {
1724 for (int i = 0; i < ring_size; i++)
1725 for (int p = 0; p < MAX_PLANES; p++)
1726 sample[p][i] = RENAME(sc->sample_buffer) + p*ring_size*(w+6) +
1727 ((h+i-y/2) % ring_size)*(w+6) + 3;
1728
1729 for (int x = 0; x < w; x++) {
1730 const uint16_t *l1 = ((const uint16_t*)(src[0] + stride[0]*(y + 0) + x*2*2));
1731 const uint16_t *l2 = ((const uint16_t*)(src[0] + stride[0]*(y + 1) + x*2*2));
1732
1733 int r, gr, gb, b;
1734 r = l1[0];
1735 gr = l1[1];
1736 gb = l2[0];
1737 b = l2[1];
1738
1739 if (sc->slice_coding_mode != 1) {
1740 /**
1741 * Bayer 2x2 RCT, based on:
1742 * "Reversible color transform for Bayer color filter array images", S. Poomrittigul et al,
1743 * APSIPA Transactions on Signal and Information Processing (2013) 2 (1): 1-10,
1744 * doi:10.1017/ATSIP.2013.6 */
1745 int gd = gr - gb;
1746 int gm = gb + (gd >> 1);
1747
1748 b -= gm;
1749 r -= gm;
1750 gm += (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
1751 b += offset;
1752 r += offset;
1753 gd += offset;
1754
1755 gr = gm;
1756 gb = gd;
1757 }
1758
1759 sample[0][0][x] = gr;
1760 sample[1][0][x] = gb;
1761 sample[2][0][x] = b;
1762 sample[3][0][x] = r;
1763 }
1764
1765 for (int p = 0; p < 4; p++) {
1766 int ret;
1767 sample[p][0][-1] = sample[p][1][0 ];
1768 sample[p][1][ w] = sample[p][1][w-1];
1769 /* Plane contexts: gm=0 (luma), b-gm/r-gm=1 (chroma diff from
1770 * green), gd=2 (own context - green-green diff has different
1771 * statistics from both luma and chroma). */
1772 ret = RENAME(encode_line)(f, sc, f->avctx, w, sample[p],
1773 p == 1 ? 2 : (p > 1),
1774 bits[p], ac, pass1);
1775 if (ret < 0)
1776 return ret;
1777 }
1778 }
1779
1780 return 0;
1781}
1782
1784{
1785 FFV1SliceContext *sc = arg;
1786 FFV1Context *f = c->priv_data;
1787 int width = sc->slice_width;
1788 int height = sc->slice_height;
1789 int x = sc->slice_x;
1790 int y = sc->slice_y;
1791 const AVFrame *const p = f->cur_enc_frame;
1792 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1793 int ret;
1794 RangeCoder c_bak = sc->c;
1795 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
1796 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1797 const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1798 p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1799 p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1800 p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1801 int ac = f->ac;
1802
1803 sc->slice_coding_mode = 0;
1804 if (f->version > 3 && f->colorspace == 1) {
1805 choose_rct_params(f, sc, planes, p->linesize, width, height);
1806 } else if (f->bayer) {
1807 choose_rct_params_bayer(f, sc, planes, p->linesize, width, height);
1808 } else {
1809 sc->slice_rct_by_coef = 1;
1810 sc->slice_rct_ry_coef = 1;
1811 }
1812
1813retry:
1814 if (f->key_frame)
1816 if (f->version > 2) {
1818 }
1819
1820 if (sc->remap) {
1821 //Both the 16bit and 32bit remap do exactly the same thing but with 16bits we can
1822 //Implement this using a "histogram" while for 32bit that would be gb sized, thus a more
1823 //complex implementation sorting pairs is used.
1824 if (f->bits_per_raw_sample != 32) {
1825 if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
1826 const int cx = x >> f->chroma_h_shift;
1827 const int cy = y >> f->chroma_v_shift;
1828
1829 //TODO decide on the order for the encoded remaps and loads. with golomb rice it
1830 // easier to have all range coded ones together, otherwise it may be nicer to handle each plane as a whole?
1831
1832 load_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
1833
1834 if (f->chroma_planes) {
1835 load_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
1836 load_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 2, 1);
1837 }
1838 if (f->transparency)
1839 load_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 3, 1);
1840 } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
1841 load_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
1842 load_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
1843 } else if (f->use32bit) {
1844 load_rgb_frame32(f, sc, planes, width, height, p->linesize);
1845 } else
1846 load_rgb_frame (f, sc, planes, width, height, p->linesize);
1847
1849 } else {
1850 load_rgb_float32_frame(f, sc, planes, width, height, p->linesize);
1852 }
1853 }
1854
1855 if (ac == AC_GOLOMB_RICE) {
1856 sc->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&sc->c, f->version > 2) : 0;
1857 init_put_bits(&sc->pb,
1860 }
1861
1862 if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
1863 const int cx = x >> f->chroma_h_shift;
1864 const int cy = y >> f->chroma_v_shift;
1865
1866 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 1, ac);
1867
1868 if (f->chroma_planes) {
1869 ret |= encode_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, 1, ac);
1870 ret |= encode_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 2, 1, ac);
1871 }
1872 if (f->transparency)
1873 ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 3, 1, ac);
1874 } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
1875 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 2, ac);
1876 ret |= encode_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 1, 2, ac);
1877 } else if (f->bits_per_raw_sample == 32) {
1878 ret = encode_float32_rgb_frame(f, sc, planes, width, height, p->linesize, ac);
1879 } else if (f->bayer) {
1880 ret = encode_bayer_frame(f, sc, planes, width, height, p->linesize, ac);
1881 } else if (f->use32bit) {
1882 ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize, ac);
1883 } else {
1884 ret = encode_rgb_frame(f, sc, planes, width, height, p->linesize, ac);
1885 }
1886
1887 if (ac != AC_GOLOMB_RICE) {
1888 sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
1889 } else {
1890 flush_put_bits(&sc->pb); // FIXME: nicer padding
1891 sc->ac_byte_count += put_bytes_output(&sc->pb);
1892 }
1893
1894 if (ret < 0) {
1895 av_assert0(sc->slice_coding_mode == 0);
1896 if (f->version < 4) {
1897 av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1898 return ret;
1899 }
1900 av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1901 ac = 1;
1902 sc->slice_coding_mode = 1;
1903 sc->c = c_bak;
1904 goto retry;
1905 }
1906
1907 return 0;
1908}
1909
1911{
1912 FFV1Context *f = avctx->priv_data;
1913
1914 int w = avctx->width + f->num_h_slices;
1915 int h = avctx->height + f->num_v_slices;
1916 size_t maxsize = w*h * (1 + f->transparency);
1917 if (f->chroma_planes)
1918 maxsize += AV_CEIL_RSHIFT(w, f->chroma_h_shift) * AV_CEIL_RSHIFT(h, f->chroma_v_shift) * 2;
1919 maxsize += f->slice_count * 800; //for slice header
1920 if (f->version > 3) {
1921 maxsize *= f->bits_per_raw_sample + 1;
1922 if (f->remap_mode)
1923 maxsize += f->slice_count * 70000 * (1 + 2*f->chroma_planes + f->bayer + f->transparency);
1924 } else {
1925 maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
1926 maxsize *= 8*(2*f->bits_per_raw_sample + 5);
1927 }
1928 maxsize >>= 3;
1929 maxsize += FF_INPUT_BUFFER_MIN_SIZE;
1930
1931 return maxsize;
1932}
1933
1935 const AVFrame *pict, int *got_packet)
1936{
1937 FFV1Context *f = avctx->priv_data;
1938 RangeCoder *const c = &f->slices[0].c;
1939 uint8_t keystate = 128;
1940 uint8_t *buf_p;
1941 int i, ret;
1942 int64_t maxsize;
1943
1944 if(!pict) {
1945 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1946 int j, k, m;
1947 char *p = avctx->stats_out;
1948 char *end = p + STATS_OUT_SIZE;
1949
1950 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1951 for (i = 0; i < f->quant_table_count; i++)
1952 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1953
1954 av_assert0(f->slice_count == f->max_slice_count);
1955 for (j = 0; j < f->slice_count; j++) {
1956 const FFV1SliceContext *sc = &f->slices[j];
1957 for (i = 0; i < 256; i++) {
1958 f->rc_stat[i][0] += sc->rc_stat[i][0];
1959 f->rc_stat[i][1] += sc->rc_stat[i][1];
1960 }
1961 for (i = 0; i < f->quant_table_count; i++) {
1962 for (k = 0; k < f->context_count[i]; k++)
1963 for (m = 0; m < 32; m++) {
1964 f->rc_stat2[i][k][m][0] += sc->rc_stat2[i][k][m][0];
1965 f->rc_stat2[i][k][m][1] += sc->rc_stat2[i][k][m][1];
1966 }
1967 }
1968 }
1969
1970 for (j = 0; j < 256; j++) {
1971 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1972 f->rc_stat[j][0], f->rc_stat[j][1]);
1973 p += strlen(p);
1974 }
1975 snprintf(p, end - p, "\n");
1976
1977 for (i = 0; i < f->quant_table_count; i++) {
1978 for (j = 0; j < f->context_count[i]; j++)
1979 for (m = 0; m < 32; m++) {
1980 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1981 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1982 p += strlen(p);
1983 }
1984 }
1985 snprintf(p, end - p, "%d\n", f->gob_count);
1986 }
1987 return 0;
1988 }
1989
1990 /* Maximum packet size */
1991 maxsize = ff_ffv1_encode_buffer_size(avctx);
1992
1993 if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1994 FFV1Context *f = avctx->priv_data;
1995 if (!f->maxsize_warned) {
1996 av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1997 f->maxsize_warned++;
1998 }
1999 maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
2000 }
2001
2002 if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
2003 return ret;
2004
2005 ff_init_range_encoder(c, pkt->data, pkt->size);
2006 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
2007
2008 f->cur_enc_frame = pict;
2009
2010 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
2011 put_rac(c, &keystate, 1);
2012 f->key_frame = 1;
2013 f->gob_count++;
2014 write_header(f);
2015 } else {
2016 put_rac(c, &keystate, 0);
2017 f->key_frame = 0;
2018 }
2019
2020 if (f->ac == AC_RANGE_CUSTOM_TAB) {
2021 int i;
2022 for (i = 1; i < 256; i++) {
2023 c->one_state[i] = f->state_transition[i];
2024 c->zero_state[256 - i] = 256 - c->one_state[i];
2025 }
2026 }
2027
2028 for (i = 0; i < f->slice_count; i++) {
2029 FFV1SliceContext *sc = &f->slices[i];
2030 uint8_t *start = pkt->data + pkt->size * (int64_t)i / f->slice_count;
2031 int len = pkt->size / f->slice_count;
2032 if (i) {
2033 ff_init_range_encoder(&sc->c, start, len);
2034 } else {
2038 }
2039 }
2040 avctx->execute(avctx, encode_slice, f->slices, NULL,
2041 f->slice_count, sizeof(*f->slices));
2042
2043 buf_p = pkt->data;
2044 for (i = 0; i < f->slice_count; i++) {
2045 FFV1SliceContext *sc = &f->slices[i];
2046 int bytes = sc->ac_byte_count;
2047 if (i > 0 || f->version > 2) {
2048 av_assert0(bytes < pkt->size / f->slice_count);
2049 memmove(buf_p, sc->c.bytestream_start, bytes);
2050 av_assert0(bytes < (1 << 24));
2051 AV_WB24(buf_p + bytes, bytes);
2052 bytes += 3;
2053 }
2054 if (f->ec) {
2055 unsigned v;
2056 buf_p[bytes++] = 0;
2057 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, buf_p, bytes) ^ (f->crcref ? 0x8CD88196 : 0);
2058 AV_WL32(buf_p + bytes, v);
2059 bytes += 4;
2060 }
2061 buf_p += bytes;
2062 }
2063
2064 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2065 avctx->stats_out[0] = '\0';
2066
2067 f->picture_number++;
2068 pkt->size = buf_p - pkt->data;
2069 pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
2070 *got_packet = 1;
2071
2072 return 0;
2073}
2074
2076{
2077 FFV1Context *const s = avctx->priv_data;
2078
2079 for (int j = 0; j < s->max_slice_count; j++) {
2080 FFV1SliceContext *sc = &s->slices[j];
2081
2082 for(int p = 0; p<4; p++) {
2083 av_freep(&sc->unit[p]);
2084 av_freep(&sc->bitmap[p]);
2085 }
2086 }
2087
2088 av_freep(&avctx->stats_out);
2090
2091 return 0;
2092}
2093
2094#define OFFSET(x) offsetof(FFV1Context, x)
2095#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2096static const AVOption options[] = {
2097 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },
2098 { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
2099 { .i64 = 0 }, -2, 2, VE, .unit = "coder" },
2100 { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
2101 { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
2102 { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
2103 { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
2104 { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
2105 { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, .unit = "coder" },
2106 { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
2107 { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" },
2108 { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
2109 { .i64 = 0 }, 0, 2, VE },
2110 { "qtable", "Quantization table", OFFSET(qtable), AV_OPT_TYPE_INT,
2111 { .i64 = -1 }, -1, 2, VE , .unit = "qtable"},
2112 { "default", NULL, 0, AV_OPT_TYPE_CONST,
2113 { .i64 = QTABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
2114 { "8bit", NULL, 0, AV_OPT_TYPE_CONST,
2115 { .i64 = QTABLE_8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
2116 { "greater8bit", NULL, 0, AV_OPT_TYPE_CONST,
2117 { .i64 = QTABLE_GT8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
2118 { "remap_mode", "Remap Mode", OFFSET(remap_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE, .unit = "remap_mode" },
2119 { "auto", "Automatic", 0, AV_OPT_TYPE_CONST,
2120 { .i64 = -1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
2121 { "off", "Disabled", 0, AV_OPT_TYPE_CONST,
2122 { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
2123 { "dualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST,
2124 { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
2125 { "flipdualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST,
2126 { .i64 = 2 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
2127 { "remap_optimizer", "Remap Optimizer", OFFSET(remap_optimizer), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 5, VE, .unit = "remap_optimizer" },
2128
2129 { NULL }
2130};
2131
2132static const AVClass ffv1_class = {
2133 .class_name = "ffv1 encoder",
2134 .item_name = av_default_item_name,
2135 .option = options,
2136 .version = LIBAVUTIL_VERSION_INT,
2137};
2138
2140 .p.name = "ffv1",
2141 CODEC_LONG_NAME("FFmpeg video codec #1"),
2142 .p.type = AVMEDIA_TYPE_VIDEO,
2143 .p.id = AV_CODEC_ID_FFV1,
2144 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
2147 .priv_data_size = sizeof(FFV1Context),
2150 .close = encode_close,
2176 .color_ranges = AVCOL_RANGE_MPEG,
2177 .p.priv_class = &ffv1_class,
2179};
#define RENAME(element)
static double val(void *priv, double ch)
Definition aeval.c:77
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
const FFCodec ff_ffv1_encoder
Definition ffv1enc.c:2139
#define VE
Definition amfenc_av1.c:30
#define log2(x)
Definition math.h:26
#define log2f(x)
Definition math.h:27
static int ring_size(RingBuffer *ring)
Definition async.c:108
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FF_CODEC_CAP_EOF_FLUSH
The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it only wants to be flushe...
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip
Definition common.h:100
#define av_clip_uint8
Definition common.h:106
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
#define MAX_SLICES
static av_cold int encode_close(AVCodecContext *avctx)
Definition dcaenc.c:354
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition defs.h:62
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:63
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition encode.h:34
static const uint8_t bits[8]
Definition fastaudio.c:100
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition ffmpeg_enc.c:683
int ret
Definition ffplay.c:391
#define CMP(a, b)
av_cold int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s)
Definition ffv1.c:36
av_cold int ff_ffv1_init_slices_state(FFV1Context *f)
Definition ffv1.c:110
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift)
Definition ffv1.c:120
av_cold void ff_ffv1_close(FFV1Context *s)
Definition ffv1.c:268
int ff_ffv1_allocate_initial_states(FFV1Context *f)
Definition ffv1.c:185
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1.c:200
void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample)
Definition ffv1.c:224
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
Definition ffv1.c:142
FF Video Codec 1 (a lossless codec)
#define CONTEXT_SIZE
Definition ffv1.h:45
#define MAX_PLANES
Definition ffv1.h:44
static void update_vlc_state(VlcState *const state, const int v)
Definition ffv1.h:227
#define MAX_QUANT_TABLE_SIZE
Definition ffv1.h:48
#define AC_GOLOMB_RICE
Definition ffv1.h:52
#define AC_RANGE_DEFAULT_TAB_FORCE
Definition ffv1.h:55
#define AC_RANGE_CUSTOM_TAB
Definition ffv1.h:54
#define AC_RANGE_DEFAULT_TAB
Definition ffv1.h:53
static av_always_inline int fold(int diff, int bits)
Definition ffv1.h:216
#define MAX_CONTEXT_INPUTS
Definition ffv1.h:50
#define TYPE
Definition ffv1dec.c:92
static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE])
Definition ffv1enc.c:364
static int encode_float32_rgb_frame(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4], int w, int h, const int stride[4], int ac)
Definition ffv1enc.c:1642
int ff_ffv1_encode_determine_slices(AVCodecContext *avctx)
Definition ffv1enc.c:570
static void write_quant_table(RangeCoder *c, int16_t *quant_table)
Definition ffv1enc.c:349
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
Definition ffv1enc.c:233
#define COST2(old, new)
static int contains_non_128(uint8_t(*initial_state)[CONTEXT_SIZE], int nb_contexts)
Definition ffv1enc.c:372
static int encode_slice(AVCodecContext *c, void *arg)
Definition ffv1enc.c:1783
static void set_micro_version(FFV1Context *f)
Definition ffv1enc.c:431
static void choose_rct_params_bayer(const FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4], const int stride[4], int w, int h)
Definition ffv1enc.c:1275
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
Definition ffv1enc.c:240
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
#define put_rac(C, S, B)
av_cold int ff_ffv1_encode_init(AVCodecContext *avctx)
Definition ffv1enc.c:605
static av_cold int encode_close(AVCodecContext *avctx)
Definition ffv1enc.c:2075
static const int8_t quant9_10bit[256]
Definition ffv1enc.c:83
static int encode_float32_remap_segment(FFV1SliceContext *sc, int p, int mul_count, int *mul_tab, int update, int final)
Definition ffv1enc.c:1429
#define FLIP(f)
static const int8_t quant5[256]
Definition ffv1enc.c:64
#define NB_Y_COEFF
Definition ffv1enc.c:1164
static int encode_bayer_frame(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4], int w, int h, const int stride[4], int ac)
Definition ffv1enc.c:1702
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition ffv1enc.c:1934
#define RENAME(name)
Definition ffv1enc.c:265
static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4], int w, int h, const int stride[4])
Definition ffv1enc.c:1377
static const int8_t quant5_10bit[256]
Definition ffv1enc.c:45
static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1enc.c:1342
#define STATS_OUT_SIZE
static int sort_stt(FFV1Context *s, uint8_t stt[256])
Definition ffv1enc.c:521
static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[3], const int stride[3], int w, int h)
Definition ffv1enc.c:1177
static const AVClass ffv1_class
Definition ffv1enc.c:2132
static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2])
Definition ffv1enc.c:185
static av_cold int encode_init_internal(AVCodecContext *avctx)
Definition ffv1enc.c:1017
static void encode_slice_header(FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1enc.c:1130
#define OFFSET(x)
Definition ffv1enc.c:2094
size_t ff_ffv1_encode_buffer_size(AVCodecContext *avctx)
Definition ffv1enc.c:1910
av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx, enum AVPixelFormat pix_fmt)
Definition ffv1enc.c:822
static const int8_t quant11[256]
Definition ffv1enc.c:102
static av_always_inline void accumulate_rct_stats(int64_t stat[NB_Y_COEFF], int partial[NB_Y_COEFF])
Definition ffv1enc.c:1168
static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256])
Definition ffv1enc.c:140
av_cold int ff_ffv1_write_extradata(AVCodecContext *avctx)
Definition ffv1enc.c:447
static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4])
Definition ffv1enc.c:1544
static void load_plane(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src, int w, int h, int stride, int remap_index, int pixel_stride)
Definition ffv1enc.c:325
static int encode_plane(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src, int w, int h, int stride, int plane_index, int remap_index, int pixel_stride, int ac)
Definition ffv1enc.c:274
static const uint8_t ver2_state[256]
Definition ffv1enc.c:121
@ QTABLE_DEFAULT
Definition ffv1enc.h:29
@ QTABLE_8BIT
Definition ffv1enc.h:30
@ QTABLE_GT8BIT
Definition ffv1enc.h:31
#define sample
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition avcodec.h:294
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition avcodec.h:290
@ AV_CODEC_ID_FFV1
Definition codec_id.h:83
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_32_IEEE
Definition crc.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:702
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition frame.h:707
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
Definition intfloat.h:50
#define av_log2
Definition intmath.h:84
static const int16_t quant_table[64]
Definition intrax8.c:511
#define AV_WB24(p, d)
#define AV_WL32(p, v)
unsigned offset
Definition libaomenc.c:763
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
const char * arg
Definition jacosubdec.c:66
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition rawdec.c:131
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_noinline
Definition attributes.h:97
#define av_fallthrough
Definition attributes.h:67
#define av_flatten
Definition attributes.h:123
#define av_uninit(x)
Definition attributes.h:187
#define av_cold
Definition attributes.h:117
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
static void encode_line(AVCodecContext *avctx, uint8_t **data, const uint8_t *line, int length)
Definition msrleenc.c:142
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3512
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition pixdesc.h:158
#define AV_PIX_FMT_0RGB32
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:572
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:559
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:587
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:555
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:547
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
#define AV_PIX_FMT_GRAYF16
Definition pixfmt.h:590
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:527
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:574
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:566
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:546
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:601
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:604
#define AV_PIX_FMT_P216
Definition pixfmt.h:631
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:599
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:553
#define AV_PIX_FMT_GBRAP14
Definition pixfmt.h:573
#define AV_PIX_FMT_P016
Definition pixfmt.h:615
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:529
#define AV_PIX_FMT_GBRAPF16
Definition pixfmt.h:586
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:538
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:568
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:545
#define AV_PIX_FMT_RGB48
Definition pixfmt.h:534
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:596
#define AV_PIX_FMT_BAYER_RGGB16
Definition pixfmt.h:581
#define AV_PIX_FMT_YAF16
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:600
#define AV_PIX_FMT_X2RGB10
Definition pixfmt.h:624
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:556
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_NV24
planar YUV 4:4:4, 24bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:371
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:198
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition pixfmt.h:140
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:602
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:557
#define AV_PIX_FMT_P416
Definition pixfmt.h:632
#define AV_PIX_FMT_X2BGR10
Definition pixfmt.h:625
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:528
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:530
#define AV_PIX_FMT_GBRPF16
Definition pixfmt.h:585
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:560
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:550
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:531
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:571
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:606
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:605
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:570
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:558
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:598
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:569
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:603
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:588
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
#define AV_PIX_FMT_RGB32
Definition pixfmt.h:520
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:551
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
exp golomb vlc writing stuff
static void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (ffv1).
Definition put_golomb.h:143
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition qsort.h:33
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition rangecoder.c:109
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition rangecoder.c:68
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition rangecoder.c:42
Range coder.
static int get_rac_count(RangeCoder *c)
Definition rangecoder.h:79
static const float pred[4]
Definition siprdata.h:259
const uint8_t * code
Definition spdifenc.c:433
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
char * stats_out
pass1 encoding statistics output buffer
Definition avcodec.h:1335
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1381
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition avcodec.h:1343
int level
Encoding level descriptor.
Definition avcodec.h:1656
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition avcodec.h:1619
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1581
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1025
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
int slices
Number of slices.
Definition avcodec.h:1041
int step
Number of elements between 2 horizontally consecutive pixels.
Definition pixdesc.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
int slice_rct_ry_coef
Definition ffv1.h:86
int16_t * sample_buffer
Definition ffv1.h:74
PutBitContext pb
Definition ffv1.h:91
int ac_byte_count
number of bytes used for AC coding
Definition ffv1.h:94
uint32_t * bitmap[4]
Definition ffv1.h:111
RangeCoder c
Definition ffv1.h:92
uint64_t rc_stat[256][2]
Definition ffv1.h:105
PlaneContext * plane
Definition ffv1.h:90
uint16_t * fltmap[4]
Definition ffv1.h:112
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition ffv1.h:106
int32_t * sample_buffer32
Definition ffv1.h:75
int run_index
Definition ffv1.h:83
int remap_count[4]
Definition ffv1.h:109
int slice_height
Definition ffv1.h:78
int slice_width
Definition ffv1.h:77
int slice_coding_mode
Definition ffv1.h:84
struct FFV1SliceContext::Unit * unit[4]
int slice_rct_by_coef
Definition ffv1.h:85
int quant_table_index
Definition ffv1.h:65
uint8_t * bytestream_start
Definition rangecoder.h:42
uint8_t * bytestream
Definition rangecoder.h:43
uint8_t * bytestream_end
Definition rangecoder.h:44
uint8_t run
Definition svq3.c:207
#define stride
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static struct @232240247175332074307271266321204261331170211232 state
static void print(AVTreeNode *t, int depth)
Definition tree.c:45
int size
const char * g
Definition vf_curves.c:128
float delta
int len
static double c[64]