FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1enc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 581 756 76.9%
Functions: 20 21 95.2%
Branches: 413 590 70.0%

Line Branch Exec Source
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
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"
34
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"
43
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,
61 };
62
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,
80 };
81
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,
99 };
100
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,
118 };
119
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,
137 };
138
139 8 static void find_best_state(uint8_t best_state[256][256],
140 const uint8_t one_state[256])
141 {
142 int i, j, k, m;
143 uint32_t l2tab[256];
144
145
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 8 times.
2048 for (i = 1; i < 256; i++)
146 2040 l2tab[i] = -log2(i / 256.0) * ((1U << 31) / 8);
147
148
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 8 times.
2056 for (i = 0; i < 256; i++) {
149 uint64_t best_len[256];
150
151
2/2
✓ Branch 0 taken 524288 times.
✓ Branch 1 taken 2048 times.
526336 for (j = 0; j < 256; j++)
152 524288 best_len[j] = UINT64_MAX;
153
154
2/2
✓ Branch 0 taken 42040 times.
✓ Branch 1 taken 2048 times.
44088 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
155 42040 uint32_t occ[256] = { 0 };
156 42040 uint64_t len = 0;
157 42040 occ[j] = UINT32_MAX;
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42040 times.
42040 if (!one_state[j])
160 continue;
161
162
2/2
✓ Branch 0 taken 10762240 times.
✓ Branch 1 taken 42040 times.
10804280 for (k = 0; k < 256; k++) {
163 10762240 uint32_t newocc[256] = { 0 };
164
2/2
✓ Branch 0 taken 2744371200 times.
✓ Branch 1 taken 10762240 times.
2755133440 for (m = 1; m < 256; m++)
165
2/2
✓ Branch 0 taken 861994804 times.
✓ Branch 1 taken 1882376396 times.
2744371200 if (occ[m]) {
166 861994804 len += (occ[m]*(( i *(uint64_t)l2tab[ m]
167 861994804 + (256-i)*(uint64_t)l2tab[256-m])>>8)) >> 8;
168 }
169
2/2
✓ Branch 0 taken 3100046 times.
✓ Branch 1 taken 7662194 times.
10762240 if (len < best_len[k]) {
170 3100046 best_len[k] = len;
171 3100046 best_state[i][k] = j;
172 }
173
2/2
✓ Branch 0 taken 2744371200 times.
✓ Branch 1 taken 10762240 times.
2755133440 for (m = 1; m < 256; m++)
174
2/2
✓ Branch 0 taken 861994804 times.
✓ Branch 1 taken 1882376396 times.
2744371200 if (occ[m]) {
175 861994804 newocc[ one_state[ m]] += occ[m] * (uint64_t) i >> 8;
176 861994804 newocc[256 - one_state[256 - m]] += occ[m] * (uint64_t)(256 - i) >> 8;
177 }
178 10762240 memcpy(occ, newocc, sizeof(occ));
179 }
180 }
181 }
182 8 }
183
184 239896249 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])
189 {
190 int i;
191
192 #define put_rac(C, S, B) \
193 do { \
194 if (rc_stat) { \
195 rc_stat[*(S)][B]++; \
196 rc_stat2[(S) - state][B]++; \
197 } \
198 put_rac(C, S, B); \
199 } while (0)
200
201
2/2
✓ Branch 0 taken 150066312 times.
✓ Branch 1 taken 89829937 times.
239896249 if (v) {
202
2/2
✓ Branch 0 taken 150033070 times.
✓ Branch 1 taken 33242 times.
150066312 const unsigned a = is_signed ? FFABS(v) : v;
203 150066312 const int e = av_log2(a);
204
2/2
✓ Branch 0 taken 38585298 times.
✓ Branch 1 taken 111481014 times.
150066312 put_rac(c, state + 0, 0);
205
2/2
✓ Branch 0 taken 125104134 times.
✓ Branch 1 taken 24962178 times.
150066312 if (e <= 9) {
206
2/2
✓ Branch 0 taken 435118149 times.
✓ Branch 1 taken 125104134 times.
560222283 for (i = 0; i < e; i++)
207
2/2
✓ Branch 0 taken 102966854 times.
✓ Branch 1 taken 332151295 times.
435118149 put_rac(c, state + 1 + i, 1); // 1..10
208
2/2
✓ Branch 0 taken 38585298 times.
✓ Branch 1 taken 86518836 times.
125104134 put_rac(c, state + 1 + i, 0);
209
210
2/2
✓ Branch 0 taken 435118149 times.
✓ Branch 1 taken 125104134 times.
560222283 for (i = e - 1; i >= 0; i--)
211
2/2
✓ Branch 0 taken 102966854 times.
✓ Branch 1 taken 332151295 times.
435118149 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
212
213
2/2
✓ Branch 0 taken 125070892 times.
✓ Branch 1 taken 33242 times.
125104134 if (is_signed)
214
2/2
✓ Branch 0 taken 38585298 times.
✓ Branch 1 taken 86485594 times.
125070892 put_rac(c, state + 11 + e, v < 0); // 11..21
215 } else {
216
2/2
✓ Branch 0 taken 269770289 times.
✓ Branch 1 taken 24962178 times.
294732467 for (i = 0; i < e; i++)
217
3/8
✗ Branch 0 not taken.
✓ Branch 1 taken 269770289 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 249621780 times.
✓ Branch 7 taken 20148509 times.
269770289 put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24962178 times.
24962178 put_rac(c, state + 1 + 9, 0);
219
220
2/2
✓ Branch 0 taken 269770289 times.
✓ Branch 1 taken 24962178 times.
294732467 for (i = e - 1; i >= 0; i--)
221
3/8
✗ Branch 0 not taken.
✓ Branch 1 taken 269770289 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 249621780 times.
✓ Branch 7 taken 20148509 times.
269770289 put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
222
223
1/2
✓ Branch 0 taken 24962178 times.
✗ Branch 1 not taken.
24962178 if (is_signed)
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24962178 times.
24962178 put_rac(c, state + 11 + 10, v < 0); // 11..21
225 }
226 } else {
227
2/2
✓ Branch 0 taken 14846302 times.
✓ Branch 1 taken 74983635 times.
89829937 put_rac(c, state + 0, 1);
228 }
229 #undef put_rac
230 239896249 }
231
232 1700049 static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
233 int v, int is_signed)
234 {
235 1700049 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
236 1700049 }
237
238
239 163659841 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
240 int v, int bits)
241 {
242 int i, k, code;
243 163659841 v = fold(v - state->bias, bits);
244
245 163659841 i = state->count;
246 163659841 k = 0;
247
2/2
✓ Branch 0 taken 274103812 times.
✓ Branch 1 taken 163659841 times.
437763653 while (i < state->error_sum) { // FIXME: optimize
248 274103812 k++;
249 274103812 i += i;
250 }
251
252 av_assert2(k <= 16);
253
254 163659841 code = v ^ ((2 * state->drift + state->count) >> 31);
255
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 163659841 set_sr_golomb(pb, code, k, 12, bits);
259
260 163659841 update_vlc_state(state, v);
261 163659841 }
262
263 #define TYPE int16_t
264 #define RENAME(name) name
265 #include "ffv1enc_template.c"
266 #undef TYPE
267 #undef RENAME
268
269 #define TYPE int32_t
270 #define RENAME(name) name ## 32
271 #include "ffv1enc_template.c"
272
273 22710 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)
276 {
277 int x, y, i, ret;
278 22710 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
279
2/2
✓ Branch 0 taken 9600 times.
✓ Branch 1 taken 13110 times.
22710 const int ring_size = f->context_model ? 3 : 2;
280 int16_t *sample[3];
281 22710 sc->run_index = 0;
282
283 22710 memset(sc->sample_buffer, 0, ring_size * (w + 6) * sizeof(*sc->sample_buffer));
284
285
2/2
✓ Branch 0 taken 2174400 times.
✓ Branch 1 taken 22710 times.
2197110 for (y = 0; y < h; y++) {
286
2/2
✓ Branch 0 taken 5247200 times.
✓ Branch 1 taken 2174400 times.
7421600 for (i = 0; i < ring_size; i++)
287 5247200 sample[i] = sc->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
288
289 2174400 sample[0][-1]= sample[1][0 ];
290 2174400 sample[1][ w]= sample[1][w-1];
291
2/2
✓ Branch 0 taken 1075200 times.
✓ Branch 1 taken 1099200 times.
2174400 if (f->bits_per_raw_sample <= 8) {
292
2/2
✓ Branch 0 taken 168153700 times.
✓ Branch 1 taken 1075200 times.
169228900 for (x = 0; x < w; x++)
293 168153700 sample[0][x] = src[x * pixel_stride + stride * y];
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1075200 times.
1075200 if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, 8, ac, pass1)) < 0)
295 return ret;
296 } else {
297
2/2
✓ Branch 0 taken 829800 times.
✓ Branch 1 taken 269400 times.
1099200 if (f->packed_at_lsb) {
298
2/2
✓ Branch 0 taken 100811400 times.
✓ Branch 1 taken 829800 times.
101641200 for (x = 0; x < w; x++) {
299 100811400 sample[0][x] = ((uint16_t*)(src + stride*y))[x];
300 }
301 } else {
302
2/2
✓ Branch 0 taken 45792600 times.
✓ Branch 1 taken 269400 times.
46062000 for (x = 0; x < w; x++) {
303 45792600 sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - f->bits_per_raw_sample);
304 }
305 }
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1099200 times.
1099200 if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, f->bits_per_raw_sample, ac, pass1)) < 0)
307 return ret;
308 }
309 }
310 22710 return 0;
311 }
312
313 585 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
314 {
315 585 int last = 0;
316 int i;
317 uint8_t state[CONTEXT_SIZE];
318 585 memset(state, 128, sizeof(state));
319
320
2/2
✓ Branch 0 taken 74295 times.
✓ Branch 1 taken 585 times.
74880 for (i = 1; i < MAX_QUANT_TABLE_SIZE/2; i++)
321
2/2
✓ Branch 0 taken 1696 times.
✓ Branch 1 taken 72599 times.
74295 if (quant_table[i] != quant_table[i - 1]) {
322 1696 put_symbol(c, state, i - last - 1, 0);
323 1696 last = i;
324 }
325 585 put_symbol(c, state, i - last - 1, 0);
326 585 }
327
328 117 static void write_quant_tables(RangeCoder *c,
329 int16_t quant_table[MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE])
330 {
331 int i;
332
2/2
✓ Branch 0 taken 585 times.
✓ Branch 1 taken 117 times.
702 for (i = 0; i < 5; i++)
333 585 write_quant_table(c, quant_table[i]);
334 117 }
335
336 92 static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
337 int nb_contexts)
338 {
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (!initial_state)
340 return 0;
341
2/2
✓ Branch 0 taken 269217 times.
✓ Branch 1 taken 84 times.
269301 for (int i = 0; i < nb_contexts; i++)
342
2/2
✓ Branch 0 taken 8614696 times.
✓ Branch 1 taken 269209 times.
8883905 for (int j = 0; j < CONTEXT_SIZE; j++)
343
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8614688 times.
8614696 if (initial_state[i][j] != 128)
344 8 return 1;
345 84 return 0;
346 }
347
348 249 static void write_header(FFV1Context *f)
349 {
350 uint8_t state[CONTEXT_SIZE];
351 int i, j;
352 249 RangeCoder *const c = &f->slices[0].c;
353
354 249 memset(state, 128, sizeof(state));
355
356
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 224 times.
249 if (f->version < 2) {
357 25 put_symbol(c, state, f->version, 0);
358 25 put_symbol(c, state, f->ac, 0);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 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);
363 }
364 25 put_symbol(c, state, f->colorspace, 0); //YUV cs type
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (f->version > 0)
366 put_symbol(c, state, f->bits_per_raw_sample, 0);
367 25 put_rac(c, state, f->chroma_planes);
368 25 put_symbol(c, state, f->chroma_h_shift, 0);
369 25 put_symbol(c, state, f->chroma_v_shift, 0);
370 25 put_rac(c, state, f->transparency);
371
372 25 write_quant_tables(c, f->quant_tables[f->context_model]);
373
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 204 times.
224 } else if (f->version < 3) {
374 20 put_symbol(c, state, f->slice_count, 0);
375
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 20 times.
100 for (i = 0; i < f->slice_count; i++) {
376 80 FFV1SliceContext *fs = &f->slices[i];
377 80 put_symbol(c, state,
378 80 (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
379 80 put_symbol(c, state,
380 80 (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
381 80 put_symbol(c, state,
382 80 (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
383 0);
384 80 put_symbol(c, state,
385 80 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
386 0);
387
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 80 times.
240 for (j = 0; j < f->plane_count; j++) {
388 160 put_symbol(c, state, fs->plane[j].quant_table_index, 0);
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 av_assert0(fs->plane[j].quant_table_index == f->context_model);
390 }
391 }
392 }
393 249 }
394
395 46 av_cold int ff_ffv1_write_extradata(AVCodecContext *avctx)
396 {
397 46 FFV1Context *f = avctx->priv_data;
398
399 RangeCoder c;
400 uint8_t state[CONTEXT_SIZE];
401 int i, j, k;
402 uint8_t state2[32][CONTEXT_SIZE];
403 unsigned v;
404
405 46 memset(state2, 128, sizeof(state2));
406 46 memset(state, 128, sizeof(state));
407
408 46 f->avctx->extradata_size = 10000 + 4 +
409 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
410 46 f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (!f->avctx->extradata)
412 return AVERROR(ENOMEM);
413 46 ff_init_range_encoder(&c, f->avctx->extradata, f->avctx->extradata_size);
414 46 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
415
416 46 put_symbol(&c, state, f->version, 0);
417 46 f->combined_version = f->version << 16;
418
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4 times.
46 if (f->version > 2) {
419
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (f->version == 3) {
420 42 f->micro_version = 4;
421 } else if (f->version == 4)
422 f->micro_version = 4;
423 42 f->combined_version += f->micro_version;
424 42 put_symbol(&c, state, f->micro_version, 0);
425 }
426
427 46 put_symbol(&c, state, f->ac, 0);
428
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 17 times.
46 if (f->ac == AC_RANGE_CUSTOM_TAB)
429
2/2
✓ Branch 0 taken 7395 times.
✓ Branch 1 taken 29 times.
7424 for (i = 1; i < 256; i++)
430 7395 put_symbol(&c, state, f->state_transition[i] - c.one_state[i], 1);
431
432 46 put_symbol(&c, state, f->colorspace, 0); // YUV cs type
433 46 put_symbol(&c, state, f->bits_per_raw_sample, 0);
434 46 put_rac(&c, state, f->chroma_planes);
435 46 put_symbol(&c, state, f->chroma_h_shift, 0);
436 46 put_symbol(&c, state, f->chroma_v_shift, 0);
437 46 put_rac(&c, state, f->transparency);
438 46 put_symbol(&c, state, f->num_h_slices - 1, 0);
439 46 put_symbol(&c, state, f->num_v_slices - 1, 0);
440
441 46 put_symbol(&c, state, f->quant_table_count, 0);
442
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 46 times.
138 for (i = 0; i < f->quant_table_count; i++)
443 92 write_quant_tables(&c, f->quant_tables[i]);
444
445
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 46 times.
138 for (i = 0; i < f->quant_table_count; i++) {
446
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 84 times.
92 if (contains_non_128(f->initial_states[i], f->context_count[i])) {
447 8 put_rac(&c, state, 1);
448
2/2
✓ Branch 0 taken 50504 times.
✓ Branch 1 taken 8 times.
50512 for (j = 0; j < f->context_count[i]; j++)
449
2/2
✓ Branch 0 taken 1616128 times.
✓ Branch 1 taken 50504 times.
1666632 for (k = 0; k < CONTEXT_SIZE; k++) {
450
2/2
✓ Branch 0 taken 1615872 times.
✓ Branch 1 taken 256 times.
1616128 int pred = j ? f->initial_states[i][j - 1][k] : 128;
451 1616128 put_symbol(&c, state2[k],
452 1616128 (int8_t)(f->initial_states[i][j][k] - pred), 1);
453 }
454 } else {
455 84 put_rac(&c, state, 0);
456 }
457 }
458
459
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4 times.
46 if (f->version > 2) {
460 42 put_symbol(&c, state, f->ec, 0);
461 42 put_symbol(&c, state, f->intra = (f->avctx->gop_size < 2), 0);
462 }
463
464 46 f->avctx->extradata_size = ff_rac_terminate(&c, 0);
465
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 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 46 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
467 46 f->avctx->extradata_size += 4;
468
469 46 return 0;
470 }
471
472 8 static int sort_stt(FFV1Context *s, uint8_t stt[256])
473 {
474 8 int i, i2, changed, print = 0;
475
476 do {
477 37 changed = 0;
478
2/2
✓ Branch 0 taken 8584 times.
✓ Branch 1 taken 37 times.
8621 for (i = 12; i < 244; i++) {
479
4/4
✓ Branch 0 taken 34114 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 25641 times.
✓ Branch 3 taken 8473 times.
34225 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
480
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)
484
485 #define COST2(old, new) \
486 COST(old, new) + COST(256 - (old), 256 - (new))
487
488 25641 double size0 = COST2(i, i) + COST2(i2, i2);
489 25641 double sizeX = COST2(i, i2) + COST2(i2, i);
490
5/6
✓ Branch 0 taken 5086 times.
✓ Branch 1 taken 20555 times.
✓ Branch 2 taken 5086 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5085 times.
✓ Branch 5 taken 1 times.
25641 if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
491 int j;
492 5085 FFSWAP(int, stt[i], stt[i2]);
493 5085 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
494 5085 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
495
2/2
✓ Branch 0 taken 5084 times.
✓ Branch 1 taken 1 times.
5085 if (i != 256 - i2) {
496 5084 FFSWAP(int, stt[256 - i], stt[256 - i2]);
497 5084 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
498 5084 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
499 }
500
2/2
✓ Branch 0 taken 1296675 times.
✓ Branch 1 taken 5085 times.
1301760 for (j = 1; j < 256; j++) {
501
2/2
✓ Branch 0 taken 5094 times.
✓ Branch 1 taken 1291581 times.
1296675 if (stt[j] == i)
502 5094 stt[j] = i2;
503
2/2
✓ Branch 0 taken 5122 times.
✓ Branch 1 taken 1286459 times.
1291581 else if (stt[j] == i2)
504 5122 stt[j] = i;
505
2/2
✓ Branch 0 taken 1296420 times.
✓ Branch 1 taken 255 times.
1296675 if (i != 256 - i2) {
506
2/2
✓ Branch 0 taken 4906 times.
✓ Branch 1 taken 1291514 times.
1296420 if (stt[256 - j] == 256 - i)
507 4906 stt[256 - j] = 256 - i2;
508
2/2
✓ Branch 0 taken 5124 times.
✓ Branch 1 taken 1286390 times.
1291514 else if (stt[256 - j] == 256 - i2)
509 5124 stt[256 - j] = 256 - i;
510 }
511 }
512 5085 print = changed = 1;
513 }
514 }
515 }
516
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 8 times.
37 } while (changed);
517 8 return print;
518 }
519
520
521 46 int ff_ffv1_encode_determine_slices(AVCodecContext *avctx)
522 {
523 46 FFV1Context *s = avctx->priv_data;
524 46 int plane_count = 1 + 2*s->chroma_planes + s->transparency;
525 46 int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
526 46 int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
527
5/6
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 4 times.
46 s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
528 46 s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
529
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 for (; s->num_v_slices < 32; s->num_v_slices++) {
530
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4 times.
54 for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
531 50 int maxw = (avctx->width + s->num_h_slices - 1) / s->num_h_slices;
532 50 int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
533
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
50 if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
534 continue;
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
536 continue;
537
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (s->version < 4)
538
1/2
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
50 if ( ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
539
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
540 continue;
541
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 4 times.
50 if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES || !avctx->slices)
542 46 return 0;
543 }
544 }
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);
550 }
551
552 51 av_cold int ff_ffv1_encode_init(AVCodecContext *avctx)
553 {
554 51 FFV1Context *s = avctx->priv_data;
555 int i, j, k, m, ret;
556
557
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 16 times.
51 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
558
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31 times.
35 avctx->slices > 1)
559 20 s->version = FFMAX(s->version, 2);
560
561
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
51 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);
564 }
565
566 // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
567
6/6
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 21 times.
51 if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
568 1 s->version = FFMAX(s->version, 2);
569
570
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 5 times.
51 if (avctx->level <= 0 && s->version == 2) {
571 21 s->version = 3;
572 }
573
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
51 if (avctx->level >= 0 && avctx->level <= 4) {
574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 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);
577 }
578 25 s->version = avctx->level;
579 }
580
581
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (s->ec < 0) {
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (s->version >= 4) {
583 s->ec = 2;
584 s->crcref = 0x7a8c4079;
585
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 9 times.
51 } else if (s->version >= 3) {
586 42 s->ec = 1;
587 } else
588 9 s->ec = 0;
589 }
590
591 // CRC requires version 3+
592
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 9 times.
51 if (s->ec == 1)
593 42 s->version = FFMAX(s->version, 3);
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (s->ec == 2)
595 s->version = FFMAX(s->version, 4);
596
597
4/6
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
51 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;
600 }
601
602
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 22 times.
51 if (s->ac == AC_RANGE_CUSTOM_TAB) {
603
2/2
✓ Branch 0 taken 7395 times.
✓ Branch 1 taken 29 times.
7424 for (i = 1; i < 256; i++)
604 7395 s->state_transition[i] = ver2_state[i];
605 } else {
606 RangeCoder c;
607 22 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
608
2/2
✓ Branch 0 taken 5610 times.
✓ Branch 1 taken 22 times.
5632 for (i = 1; i < 256; i++)
609 5610 s->state_transition[i] = c.one_state[i];
610 }
611
612
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 51 times.
13107 for (i = 0; i < 256; i++) {
613 13056 s->quant_table_count = 2;
614
4/6
✓ Branch 0 taken 13056 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5376 times.
✓ Branch 3 taken 7680 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5376 times.
13056 if ((s->qtable == -1 && s->bits_per_raw_sample <= 8) || s->qtable == 1) {
615 7680 s->quant_tables[0][0][i]= quant11[i];
616 7680 s->quant_tables[0][1][i]= 11*quant11[i];
617 7680 s->quant_tables[0][2][i]= 11*11*quant11[i];
618 7680 s->quant_tables[1][0][i]= quant11[i];
619 7680 s->quant_tables[1][1][i]= 11*quant11[i];
620 7680 s->quant_tables[1][2][i]= 11*11*quant5 [i];
621 7680 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
622 7680 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
623 7680 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
624 7680 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
625 } else {
626 5376 s->quant_tables[0][0][i]= quant9_10bit[i];
627 5376 s->quant_tables[0][1][i]= 9*quant9_10bit[i];
628 5376 s->quant_tables[0][2][i]= 9*9*quant9_10bit[i];
629 5376 s->quant_tables[1][0][i]= quant9_10bit[i];
630 5376 s->quant_tables[1][1][i]= 9*quant9_10bit[i];
631 5376 s->quant_tables[1][2][i]= 9*9*quant5_10bit[i];
632 5376 s->quant_tables[1][3][i]= 5*9*9*quant5_10bit[i];
633 5376 s->quant_tables[1][4][i]= 5*5*9*9*quant5_10bit[i];
634 5376 s->context_count[0] = (9 * 9 * 9 + 1) / 2;
635 5376 s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
636 }
637 }
638
639
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
640 return ret;
641
642
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (!s->transparency)
643 51 s->plane_count = 2;
644
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
51 if (!s->chroma_planes && s->version > 3)
645 s->plane_count--;
646
647 51 s->picture_number = 0;
648
649
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 35 times.
51 if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
650
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (i = 0; i < s->quant_table_count; i++) {
651 32 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
652 sizeof(*s->rc_stat2[i]));
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!s->rc_stat2[i])
654 return AVERROR(ENOMEM);
655 }
656 }
657
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 43 times.
51 if (avctx->stats_in) {
658 8 char *p = avctx->stats_in;
659 8 uint8_t (*best_state)[256] = av_malloc_array(256, 256);
660 8 int gob_count = 0;
661 char *next;
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!best_state)
663 return AVERROR(ENOMEM);
664
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 av_assert0(s->version >= 2);
666
667 for (;;) {
668
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 8 times.
2056 for (j = 0; j < 256; j++)
669
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 2048 times.
6144 for (i = 0; i < 2; i++) {
670 4096 s->rc_stat[j][i] = strtol(p, &next, 0);
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4096 times.
4096 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;
676 }
677 4096 p = next;
678 }
679
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < s->quant_table_count; i++)
680
2/2
✓ Branch 0 taken 54628 times.
✓ Branch 1 taken 16 times.
54644 for (j = 0; j < s->context_count[i]; j++) {
681
2/2
✓ Branch 0 taken 1748096 times.
✓ Branch 1 taken 54628 times.
1802724 for (k = 0; k < 32; k++)
682
2/2
✓ Branch 0 taken 3496192 times.
✓ Branch 1 taken 1748096 times.
5244288 for (m = 0; m < 2; m++) {
683 3496192 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3496192 times.
3496192 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;
690 }
691 3496192 p = next;
692 }
693 }
694 8 gob_count = strtol(p, &next, 0);
695
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 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;
699 }
700 8 p = next;
701
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
16 while (*p == '\n' || *p == ' ')
702 8 p++;
703
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (p[0] == 0)
704 8 break;
705 }
706
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->ac == AC_RANGE_CUSTOM_TAB)
707 8 sort_stt(s, s->state_transition);
708
709 8 find_best_state(best_state, s->state_transition);
710
711
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < s->quant_table_count; i++) {
712
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 16 times.
528 for (k = 0; k < 32; k++) {
713 512 double a=0, b=0;
714 512 int jp = 0;
715
2/2
✓ Branch 0 taken 1748096 times.
✓ Branch 1 taken 512 times.
1748608 for (j = 0; j < s->context_count[i]; j++) {
716 1748096 double p = 128;
717
6/6
✓ Branch 0 taken 179054 times.
✓ Branch 1 taken 1569042 times.
✓ Branch 2 taken 154 times.
✓ Branch 3 taken 178900 times.
✓ Branch 4 taken 112423 times.
✓ Branch 5 taken 1456773 times.
1748096 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
718
2/2
✓ Branch 0 taken 291303 times.
✓ Branch 1 taken 20 times.
291323 if (a+b)
719 291303 p = 256.0 * b / (a + b);
720 291323 s->initial_states[i][jp][k] =
721 291323 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
722
2/2
✓ Branch 0 taken 883713 times.
✓ Branch 1 taken 291323 times.
1175036 for(jp++; jp<j; jp++)
723 883713 s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
724 291323 a=b=0;
725 }
726 1748096 a += s->rc_stat2[i][j][k][0];
727 1748096 b += s->rc_stat2[i][j][k][1];
728
2/2
✓ Branch 0 taken 1284515 times.
✓ Branch 1 taken 463581 times.
1748096 if (a+b) {
729 1284515 p = 256.0 * b / (a + b);
730 }
731 1748096 s->initial_states[i][j][k] =
732 1748096 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
733 }
734 }
735 }
736 8 av_freep(&best_state);
737 }
738
739
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 46 times.
51 if (s->version <= 1) {
740 /* Disable slices when the version doesn't support them */
741 5 s->num_h_slices = 1;
742 5 s->num_v_slices = 1;
743 }
744
745 51 return 0;
746 }
747
748 51 av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
749 enum AVPixelFormat pix_fmt)
750 {
751 51 FFV1Context *s = avctx->priv_data;
752 51 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
753
754 51 s->plane_count = 3;
755
5/16
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
51 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
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
13 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
774 12 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
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
13 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
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
13 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
789 s->bits_per_raw_sample = 14;
790 13 s->packed_at_lsb = 1;
791 17 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
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 12 times.
17 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
799 4 s->bits_per_raw_sample = 16;
800
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 } else if (!s->bits_per_raw_sample) {
801 1 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
802 }
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (s->bits_per_raw_sample <= 8) {
804 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
805 return AVERROR_INVALIDDATA;
806 }
807 17 s->version = FFMAX(s->version, 1);
808 43 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 43 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
820 43 s->colorspace = 0;
821 43 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
822
4/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 16 times.
43 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
823 26 s->bits_per_raw_sample = 8;
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 else if (!s->bits_per_raw_sample)
825 s->bits_per_raw_sample = 8;
826 43 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 4 case AV_PIX_FMT_RGB48:
842 4 s->colorspace = 1;
843 4 s->chroma_planes = 1;
844 4 s->bits_per_raw_sample = 16;
845 4 s->use32bit = 1;
846 4 s->version = FFMAX(s->version, 1);
847 4 break;
848 4 case AV_PIX_FMT_0RGB32:
849 4 s->colorspace = 1;
850 4 s->chroma_planes = 1;
851 4 s->bits_per_raw_sample = 8;
852 4 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 case AV_PIX_FMT_GBRPF16:
871 case AV_PIX_FMT_GBRAPF16:
872 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
873 s->bits_per_raw_sample = 16;
874 else if (!s->bits_per_raw_sample)
875 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
876 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
877 s->colorspace = 1;
878 s->chroma_planes = 1;
879 if (s->bits_per_raw_sample >= 16) {
880 s->use32bit = 1;
881 }
882 s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
883 s->version = FFMAX(s->version, 1);
884
885 if (s->flt)
886 s->version = FFMAX(s->version, 4);
887 break;
888 default:
889 av_log(avctx, AV_LOG_ERROR, "format %s not supported\n",
890 av_get_pix_fmt_name(pix_fmt));
891 return AVERROR(ENOSYS);
892 }
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 av_assert0(s->bits_per_raw_sample >= 8);
894
895 51 return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
896 }
897
898 51 static int encode_init_internal(AVCodecContext *avctx)
899 {
900 int ret;
901 51 FFV1Context *s = avctx->priv_data;
902
903
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_common_init(avctx, s)) < 0)
904 return ret;
905
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (s->ac == 1) // Compatbility with common command line usage
907 s->ac = AC_RANGE_CUSTOM_TAB;
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
909 s->ac = AC_RANGE_DEFAULT_TAB;
910
911 51 ret = ff_ffv1_encode_setup_plane_info(avctx, avctx->pix_fmt);
912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ret < 0)
913 return ret;
914
915
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 30 times.
51 if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8)) {
916
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
21 if (s->ac == AC_GOLOMB_RICE) {
917 13 av_log(avctx, AV_LOG_INFO,
918 "high bits_per_raw_sample, forcing range coder\n");
919 13 s->ac = AC_RANGE_CUSTOM_TAB;
920 }
921 }
922
923
924 51 ret = ff_ffv1_encode_init(avctx);
925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ret < 0)
926 return ret;
927
928
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 5 times.
51 if (s->version > 1) {
929
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = ff_ffv1_encode_determine_slices(avctx)) < 0)
930 return ret;
931
932
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = ff_ffv1_write_extradata(avctx)) < 0)
933 return ret;
934 }
935
936
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
937 return ret;
938 51 s->slice_count = s->max_slice_count;
939
940
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 51 times.
240 for (int j = 0; j < s->slice_count; j++) {
941
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 189 times.
567 for (int i = 0; i < s->plane_count; i++) {
942 378 PlaneContext *const p = &s->slices[j].plane[i];
943
944 378 p->quant_table_index = s->context_model;
945 378 p->context_count = s->context_count[p->quant_table_index];
946 }
947
948 189 ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
949
950 189 s->slices[j].remap = s->flt;
951 }
952
953
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_init_slices_state(s)) < 0)
954 return ret;
955
956 #define STATS_OUT_SIZE 1024 * 1024 * 6
957
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 43 times.
51 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
958 8 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!avctx->stats_out)
960 return AVERROR(ENOMEM);
961
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int i = 0; i < s->quant_table_count; i++)
962
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (int j = 0; j < s->max_slice_count; j++) {
963 64 FFV1SliceContext *sc = &s->slices[j];
964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 av_assert0(!sc->rc_stat2[i]);
965 64 sc->rc_stat2[i] = av_mallocz(s->context_count[i] *
966 sizeof(*sc->rc_stat2[i]));
967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (!sc->rc_stat2[i])
968 return AVERROR(ENOMEM);
969 }
970 }
971
972 51 return 0;
973 }
974
975 8120 static void encode_slice_header(FFV1Context *f, FFV1SliceContext *sc)
976 {
977 8120 RangeCoder *c = &sc->c;
978 uint8_t state[CONTEXT_SIZE];
979 int j;
980 8120 memset(state, 128, sizeof(state));
981
982 8120 put_symbol(c, state, sc->sx, 0);
983 8120 put_symbol(c, state, sc->sy, 0);
984 8120 put_symbol(c, state, 0, 0);
985 8120 put_symbol(c, state, 0, 0);
986
2/2
✓ Branch 0 taken 16240 times.
✓ Branch 1 taken 8120 times.
24360 for (j=0; j<f->plane_count; j++) {
987 16240 put_symbol(c, state, sc->plane[j].quant_table_index, 0);
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16240 times.
16240 av_assert0(sc->plane[j].quant_table_index == f->context_model);
989 }
990
1/2
✓ Branch 0 taken 8120 times.
✗ Branch 1 not taken.
8120 if (!(f->cur_enc_frame->flags & AV_FRAME_FLAG_INTERLACED))
991 8120 put_symbol(c, state, 3, 0);
992 else
993 put_symbol(c, state, 1 + !(f->cur_enc_frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST), 0);
994 8120 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.num, 0);
995 8120 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.den, 0);
996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8120 times.
8120 if (f->version > 3) {
997 put_rac(c, state, sc->slice_coding_mode == 1);
998 if (sc->slice_coding_mode == 1)
999 ff_ffv1_clear_slice_state(f, sc);
1000 put_symbol(c, state, sc->slice_coding_mode, 0);
1001 if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
1002 put_symbol(c, state, sc->slice_rct_by_coef, 0);
1003 put_symbol(c, state, sc->slice_rct_ry_coef, 0);
1004 }
1005 put_symbol(c, state, sc->remap, 0);
1006 }
1007 8120 }
1008
1009 static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
1010 const uint8_t *src[3], const int stride[3], int w, int h)
1011 {
1012 #define NB_Y_COEFF 15
1013 static const int rct_y_coeff[15][2] = {
1014 {0, 0}, // 4G
1015 {1, 1}, // R + 2G + B
1016 {2, 2}, // 2R + 2B
1017 {0, 2}, // 2G + 2B
1018 {2, 0}, // 2R + 2G
1019 {4, 0}, // 4R
1020 {0, 4}, // 4B
1021
1022 {0, 3}, // 1G + 3B
1023 {3, 0}, // 3R + 1G
1024 {3, 1}, // 3R + B
1025 {1, 3}, // R + 3B
1026 {1, 2}, // R + G + 2B
1027 {2, 1}, // 2R + G + B
1028 {0, 1}, // 3G + B
1029 {1, 0}, // R + 3G
1030 };
1031
1032 int stat[NB_Y_COEFF] = {0};
1033 int x, y, i, p, best;
1034 int16_t *sample[3];
1035 int lbd = f->bits_per_raw_sample <= 8;
1036 int packed = !src[1];
1037 int transparency = f->transparency;
1038 int packed_size = (3 + transparency)*2;
1039
1040 for (y = 0; y < h; y++) {
1041 int lastr=0, lastg=0, lastb=0;
1042 for (p = 0; p < 3; p++)
1043 sample[p] = sc->sample_buffer + p*w;
1044
1045 for (x = 0; x < w; x++) {
1046 int b, g, r;
1047 int ab, ag, ar;
1048 if (lbd) {
1049 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
1050 b = v & 0xFF;
1051 g = (v >> 8) & 0xFF;
1052 r = (v >> 16) & 0xFF;
1053 } else if (packed) {
1054 const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
1055 r = p[0];
1056 g = p[1];
1057 b = p[2];
1058 } else if (f->use32bit || transparency) {
1059 g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
1060 b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
1061 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
1062 } else {
1063 b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
1064 g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
1065 r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
1066 }
1067
1068 ar = r - lastr;
1069 ag = g - lastg;
1070 ab = b - lastb;
1071 if (x && y) {
1072 int bg = ag - sample[0][x];
1073 int bb = ab - sample[1][x];
1074 int br = ar - sample[2][x];
1075
1076 br -= bg;
1077 bb -= bg;
1078
1079 for (i = 0; i<NB_Y_COEFF; i++) {
1080 stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1081 }
1082
1083 }
1084 sample[0][x] = ag;
1085 sample[1][x] = ab;
1086 sample[2][x] = ar;
1087
1088 lastr = r;
1089 lastg = g;
1090 lastb = b;
1091 }
1092 }
1093
1094 best = 0;
1095 for (i=1; i<NB_Y_COEFF; i++) {
1096 if (stat[i] < stat[best])
1097 best = i;
1098 }
1099
1100 sc->slice_rct_by_coef = rct_y_coeff[best][1];
1101 sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1102 }
1103
1104 9170 static int encode_slice(AVCodecContext *c, void *arg)
1105 {
1106 9170 FFV1SliceContext *sc = arg;
1107 9170 FFV1Context *f = c->priv_data;
1108 9170 int width = sc->slice_width;
1109 9170 int height = sc->slice_height;
1110 9170 int x = sc->slice_x;
1111 9170 int y = sc->slice_y;
1112 9170 const AVFrame *const p = f->cur_enc_frame;
1113 9170 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1114 int ret;
1115 9170 RangeCoder c_bak = sc->c;
1116 36680 const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1117
2/2
✓ Branch 0 taken 7570 times.
✓ Branch 1 taken 1600 times.
9170 p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1118
2/2
✓ Branch 0 taken 7570 times.
✓ Branch 1 taken 1600 times.
9170 p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9170 times.
9170 p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1120 9170 int ac = f->ac;
1121
1122 9170 sc->slice_coding_mode = 0;
1123
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9170 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9170 if (f->version > 3 && f->colorspace == 1) {
1124 choose_rct_params(f, sc, planes, p->linesize, width, height);
1125 } else {
1126 9170 sc->slice_rct_by_coef = 1;
1127 9170 sc->slice_rct_ry_coef = 1;
1128 }
1129
1130 9170 retry:
1131
2/2
✓ Branch 0 taken 921 times.
✓ Branch 1 taken 8249 times.
9170 if (f->key_frame)
1132 921 ff_ffv1_clear_slice_state(f, sc);
1133
2/2
✓ Branch 0 taken 8120 times.
✓ Branch 1 taken 1050 times.
9170 if (f->version > 2) {
1134 8120 encode_slice_header(f, sc);
1135 }
1136
2/2
✓ Branch 0 taken 3550 times.
✓ Branch 1 taken 5620 times.
9170 if (ac == AC_GOLOMB_RICE) {
1137
6/6
✓ Branch 0 taken 1050 times.
✓ Branch 1 taken 2500 times.
✓ Branch 2 taken 650 times.
✓ Branch 3 taken 400 times.
✓ Branch 4 taken 450 times.
✓ Branch 5 taken 200 times.
3550 sc->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&sc->c, f->version > 2) : 0;
1138 3550 init_put_bits(&sc->pb,
1139 3550 sc->c.bytestream_start + sc->ac_byte_count,
1140 3550 sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count);
1141 }
1142
1143
3/4
✓ Branch 0 taken 7570 times.
✓ Branch 1 taken 1600 times.
✓ Branch 2 taken 7570 times.
✗ Branch 3 not taken.
16740 if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8) {
1144 7570 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
1145 7570 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1146 7570 const int cx = x >> f->chroma_h_shift;
1147 7570 const int cy = y >> f->chroma_v_shift;
1148
1149 7570 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1, ac);
1150
1151
1/2
✓ Branch 0 taken 7570 times.
✗ Branch 1 not taken.
7570 if (f->chroma_planes) {
1152 7570 ret |= encode_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, ac);
1153 7570 ret |= encode_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1, ac);
1154 }
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7570 times.
7570 if (f->transparency)
1156 ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1, ac);
1157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
1158 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2, ac);
1159 ret |= encode_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2, ac);
1160
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 } else if (f->use32bit) {
1161 800 ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize);
1162 } else {
1163 800 ret = encode_rgb_frame(f, sc, planes, width, height, p->linesize);
1164 }
1165
1166
2/2
✓ Branch 0 taken 5620 times.
✓ Branch 1 taken 3550 times.
9170 if (ac != AC_GOLOMB_RICE) {
1167 5620 sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
1168 } else {
1169 3550 flush_put_bits(&sc->pb); // FIXME: nicer padding
1170 3550 sc->ac_byte_count += put_bytes_output(&sc->pb);
1171 }
1172
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9170 times.
9170 if (ret < 0) {
1174 av_assert0(sc->slice_coding_mode == 0);
1175 if (f->version < 4) {
1176 av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1177 return ret;
1178 }
1179 av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1180 ac = 1;
1181 sc->slice_coding_mode = 1;
1182 sc->c = c_bak;
1183 goto retry;
1184 }
1185
1186 9170 return 0;
1187 }
1188
1189 2480 size_t ff_ffv1_encode_buffer_size(AVCodecContext *avctx)
1190 {
1191 2480 FFV1Context *f = avctx->priv_data;
1192
1193 2480 size_t maxsize = avctx->width*avctx->height * (1 + f->transparency);
1194
1/2
✓ Branch 0 taken 2480 times.
✗ Branch 1 not taken.
2480 if (f->chroma_planes)
1195 2480 maxsize += AV_CEIL_RSHIFT(avctx->width, f->chroma_h_shift) * AV_CEIL_RSHIFT(f->height, f->chroma_v_shift) * 2;
1196 2480 maxsize += f->slice_count * 800; //for slice header
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (f->version > 3) {
1198 maxsize *= f->bits_per_raw_sample + 1;
1199 } else {
1200 2480 maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
1201 2480 maxsize *= 8*(2*f->bits_per_raw_sample + 5);
1202 }
1203 2480 maxsize >>= 3;
1204 2480 maxsize += FF_INPUT_BUFFER_MIN_SIZE;
1205
1206 2480 return maxsize;
1207 }
1208
1209 2531 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1210 const AVFrame *pict, int *got_packet)
1211 {
1212 2531 FFV1Context *f = avctx->priv_data;
1213 2531 RangeCoder *const c = &f->slices[0].c;
1214 2531 uint8_t keystate = 128;
1215 uint8_t *buf_p;
1216 int i, ret;
1217 int64_t maxsize;
1218
1219
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2480 times.
2531 if(!pict) {
1220
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 43 times.
51 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1221 int j, k, m;
1222 8 char *p = avctx->stats_out;
1223 8 char *end = p + STATS_OUT_SIZE;
1224
1225 8 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1226
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < f->quant_table_count; i++)
1227 16 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1228
1229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 av_assert0(f->slice_count == f->max_slice_count);
1230
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 for (j = 0; j < f->slice_count; j++) {
1231 32 const FFV1SliceContext *sc = &f->slices[j];
1232
2/2
✓ Branch 0 taken 8192 times.
✓ Branch 1 taken 32 times.
8224 for (i = 0; i < 256; i++) {
1233 8192 f->rc_stat[i][0] += sc->rc_stat[i][0];
1234 8192 f->rc_stat[i][1] += sc->rc_stat[i][1];
1235 }
1236
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 for (i = 0; i < f->quant_table_count; i++) {
1237
2/2
✓ Branch 0 taken 218512 times.
✓ Branch 1 taken 64 times.
218576 for (k = 0; k < f->context_count[i]; k++)
1238
2/2
✓ Branch 0 taken 6992384 times.
✓ Branch 1 taken 218512 times.
7210896 for (m = 0; m < 32; m++) {
1239 6992384 f->rc_stat2[i][k][m][0] += sc->rc_stat2[i][k][m][0];
1240 6992384 f->rc_stat2[i][k][m][1] += sc->rc_stat2[i][k][m][1];
1241 }
1242 }
1243 }
1244
1245
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 8 times.
2056 for (j = 0; j < 256; j++) {
1246 2048 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1247 f->rc_stat[j][0], f->rc_stat[j][1]);
1248 2048 p += strlen(p);
1249 }
1250 8 snprintf(p, end - p, "\n");
1251
1252
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < f->quant_table_count; i++) {
1253
2/2
✓ Branch 0 taken 54628 times.
✓ Branch 1 taken 16 times.
54644 for (j = 0; j < f->context_count[i]; j++)
1254
2/2
✓ Branch 0 taken 1748096 times.
✓ Branch 1 taken 54628 times.
1802724 for (m = 0; m < 32; m++) {
1255 1748096 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1256 1748096 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1257 1748096 p += strlen(p);
1258 }
1259 }
1260 8 snprintf(p, end - p, "%d\n", f->gob_count);
1261 }
1262 51 return 0;
1263 }
1264
1265 /* Maximum packet size */
1266 2480 maxsize = ff_ffv1_encode_buffer_size(avctx);
1267
1268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1269 av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1270 maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
1271 }
1272
1273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2480 times.
2480 if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
1274 return ret;
1275
1276 2480 ff_init_range_encoder(c, pkt->data, pkt->size);
1277 2480 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1278
1279 2480 f->cur_enc_frame = pict;
1280
1281
3/4
✓ Branch 0 taken 2480 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 2231 times.
2480 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
1282 249 put_rac(c, &keystate, 1);
1283 249 f->key_frame = 1;
1284 249 f->gob_count++;
1285 249 write_header(f);
1286 } else {
1287 2231 put_rac(c, &keystate, 0);
1288 2231 f->key_frame = 0;
1289 }
1290
1291
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 1075 times.
2480 if (f->ac == AC_RANGE_CUSTOM_TAB) {
1292 int i;
1293
2/2
✓ Branch 0 taken 358275 times.
✓ Branch 1 taken 1405 times.
359680 for (i = 1; i < 256; i++) {
1294 358275 c->one_state[i] = f->state_transition[i];
1295 358275 c->zero_state[256 - i] = 256 - c->one_state[i];
1296 }
1297 }
1298
1299
2/2
✓ Branch 0 taken 9170 times.
✓ Branch 1 taken 2480 times.
11650 for (i = 0; i < f->slice_count; i++) {
1300 9170 FFV1SliceContext *sc = &f->slices[i];
1301 9170 uint8_t *start = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1302 9170 int len = pkt->size / f->slice_count;
1303
2/2
✓ Branch 0 taken 6690 times.
✓ Branch 1 taken 2480 times.
9170 if (i) {
1304 6690 ff_init_range_encoder(&sc->c, start, len);
1305 } else {
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 av_assert0(sc->c.bytestream_end >= sc->c.bytestream_start + len);
1307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 av_assert0(sc->c.bytestream < sc->c.bytestream_start + len);
1308 2480 sc->c.bytestream_end = sc->c.bytestream_start + len;
1309 }
1310 }
1311 2480 avctx->execute(avctx, encode_slice, f->slices, NULL,
1312 f->slice_count, sizeof(*f->slices));
1313
1314 2480 buf_p = pkt->data;
1315
2/2
✓ Branch 0 taken 9170 times.
✓ Branch 1 taken 2480 times.
11650 for (i = 0; i < f->slice_count; i++) {
1316 9170 FFV1SliceContext *sc = &f->slices[i];
1317 9170 int bytes = sc->ac_byte_count;
1318
4/4
✓ Branch 0 taken 2480 times.
✓ Branch 1 taken 6690 times.
✓ Branch 2 taken 2030 times.
✓ Branch 3 taken 450 times.
9170 if (i > 0 || f->version > 2) {
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8720 times.
8720 av_assert0(bytes < pkt->size / f->slice_count);
1320 8720 memmove(buf_p, sc->c.bytestream_start, bytes);
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8720 times.
8720 av_assert0(bytes < (1 << 24));
1322 8720 AV_WB24(buf_p + bytes, bytes);
1323 8720 bytes += 3;
1324 }
1325
2/2
✓ Branch 0 taken 8120 times.
✓ Branch 1 taken 1050 times.
9170 if (f->ec) {
1326 unsigned v;
1327 8120 buf_p[bytes++] = 0;
1328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8120 times.
8120 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, buf_p, bytes) ^ (f->crcref ? 0x8CD88196 : 0);
1329 8120 AV_WL32(buf_p + bytes, v);
1330 8120 bytes += 4;
1331 }
1332 9170 buf_p += bytes;
1333 }
1334
1335
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 2080 times.
2480 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1336 400 avctx->stats_out[0] = '\0';
1337
1338 2480 f->picture_number++;
1339 2480 pkt->size = buf_p - pkt->data;
1340 2480 pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1341 2480 *got_packet = 1;
1342
1343 2480 return 0;
1344 }
1345
1346 51 static av_cold int encode_close(AVCodecContext *avctx)
1347 {
1348 51 FFV1Context *const s = avctx->priv_data;
1349
1350 51 av_freep(&avctx->stats_out);
1351 51 ff_ffv1_close(s);
1352
1353 51 return 0;
1354 }
1355
1356 #define OFFSET(x) offsetof(FFV1Context, x)
1357 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1358 static const AVOption options[] = {
1359 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },
1360 { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1361 { .i64 = 0 }, -2, 2, VE, .unit = "coder" },
1362 { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1363 { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1364 { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1365 { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1366 { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1367 { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1368 { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
1369 { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1370 { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1371 { .i64 = 0 }, 0, 1, VE },
1372 { "qtable", "Quantization table", OFFSET(qtable), AV_OPT_TYPE_INT,
1373 { .i64 = -1 }, -1, 2, VE , .unit = "qtable"},
1374 { "default", NULL, 0, AV_OPT_TYPE_CONST,
1375 { .i64 = QTABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1376 { "8bit", NULL, 0, AV_OPT_TYPE_CONST,
1377 { .i64 = QTABLE_8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1378 { "greater8bit", NULL, 0, AV_OPT_TYPE_CONST,
1379 { .i64 = QTABLE_GT8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1380
1381 { NULL }
1382 };
1383
1384 static const AVClass ffv1_class = {
1385 .class_name = "ffv1 encoder",
1386 .item_name = av_default_item_name,
1387 .option = options,
1388 .version = LIBAVUTIL_VERSION_INT,
1389 };
1390
1391 const FFCodec ff_ffv1_encoder = {
1392 .p.name = "ffv1",
1393 CODEC_LONG_NAME("FFmpeg video codec #1"),
1394 .p.type = AVMEDIA_TYPE_VIDEO,
1395 .p.id = AV_CODEC_ID_FFV1,
1396 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1397 AV_CODEC_CAP_SLICE_THREADS |
1398 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1399 .priv_data_size = sizeof(FFV1Context),
1400 .init = encode_init_internal,
1401 FF_CODEC_ENCODE_CB(encode_frame),
1402 .close = encode_close,
1403 .p.pix_fmts = (const enum AVPixelFormat[]) {
1404 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
1405 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
1406 AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
1407 AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
1408 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1409 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1410 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
1411 AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12,
1412 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
1413 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
1414 AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
1415 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRAP14,
1416 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
1417 AV_PIX_FMT_YA8,
1418 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
1419 AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48,
1420 AV_PIX_FMT_GBRAP16, AV_PIX_FMT_RGBA64,
1421 AV_PIX_FMT_GRAY9,
1422 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
1423 AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
1424 AV_PIX_FMT_GBRPF16,
1425 AV_PIX_FMT_NONE
1426
1427 },
1428 .color_ranges = AVCOL_RANGE_MPEG,
1429 .p.priv_class = &ffv1_class,
1430 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,
1431 };
1432