FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1enc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 570 737 77.3%
Functions: 18 19 94.7%
Branches: 411 582 70.6%

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 150066312 const int a = FFABS(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
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4 times.
46 if (f->version > 2) {
418
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (f->version == 3) {
419 42 f->micro_version = 4;
420 } else if (f->version == 4)
421 f->micro_version = 3;
422 42 put_symbol(&c, state, f->micro_version, 0);
423 }
424
425 46 put_symbol(&c, state, f->ac, 0);
426
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 17 times.
46 if (f->ac == AC_RANGE_CUSTOM_TAB)
427
2/2
✓ Branch 0 taken 7395 times.
✓ Branch 1 taken 29 times.
7424 for (i = 1; i < 256; i++)
428 7395 put_symbol(&c, state, f->state_transition[i] - c.one_state[i], 1);
429
430 46 put_symbol(&c, state, f->colorspace, 0); // YUV cs type
431 46 put_symbol(&c, state, f->bits_per_raw_sample, 0);
432 46 put_rac(&c, state, f->chroma_planes);
433 46 put_symbol(&c, state, f->chroma_h_shift, 0);
434 46 put_symbol(&c, state, f->chroma_v_shift, 0);
435 46 put_rac(&c, state, f->transparency);
436 46 put_symbol(&c, state, f->num_h_slices - 1, 0);
437 46 put_symbol(&c, state, f->num_v_slices - 1, 0);
438
439 46 put_symbol(&c, state, f->quant_table_count, 0);
440
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 46 times.
138 for (i = 0; i < f->quant_table_count; i++)
441 92 write_quant_tables(&c, f->quant_tables[i]);
442
443
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 46 times.
138 for (i = 0; i < f->quant_table_count; i++) {
444
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])) {
445 8 put_rac(&c, state, 1);
446
2/2
✓ Branch 0 taken 50504 times.
✓ Branch 1 taken 8 times.
50512 for (j = 0; j < f->context_count[i]; j++)
447
2/2
✓ Branch 0 taken 1616128 times.
✓ Branch 1 taken 50504 times.
1666632 for (k = 0; k < CONTEXT_SIZE; k++) {
448
2/2
✓ Branch 0 taken 1615872 times.
✓ Branch 1 taken 256 times.
1616128 int pred = j ? f->initial_states[i][j - 1][k] : 128;
449 1616128 put_symbol(&c, state2[k],
450 1616128 (int8_t)(f->initial_states[i][j][k] - pred), 1);
451 }
452 } else {
453 84 put_rac(&c, state, 0);
454 }
455 }
456
457
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 4 times.
46 if (f->version > 2) {
458 42 put_symbol(&c, state, f->ec, 0);
459 42 put_symbol(&c, state, f->intra = (f->avctx->gop_size < 2), 0);
460 }
461
462 46 f->avctx->extradata_size = ff_rac_terminate(&c, 0);
463
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);
464 46 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
465 46 f->avctx->extradata_size += 4;
466
467 46 return 0;
468 }
469
470 8 static int sort_stt(FFV1Context *s, uint8_t stt[256])
471 {
472 8 int i, i2, changed, print = 0;
473
474 do {
475 37 changed = 0;
476
2/2
✓ Branch 0 taken 8584 times.
✓ Branch 1 taken 37 times.
8621 for (i = 12; i < 244; i++) {
477
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++) {
478
479 #define COST(old, new) \
480 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
481 s->rc_stat[old][1] * -log2((new) / 256.0)
482
483 #define COST2(old, new) \
484 COST(old, new) + COST(256 - (old), 256 - (new))
485
486 25641 double size0 = COST2(i, i) + COST2(i2, i2);
487 25641 double sizeX = COST2(i, i2) + COST2(i2, i);
488
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) {
489 int j;
490 5085 FFSWAP(int, stt[i], stt[i2]);
491 5085 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
492 5085 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
493
2/2
✓ Branch 0 taken 5084 times.
✓ Branch 1 taken 1 times.
5085 if (i != 256 - i2) {
494 5084 FFSWAP(int, stt[256 - i], stt[256 - i2]);
495 5084 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
496 5084 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
497 }
498
2/2
✓ Branch 0 taken 1296675 times.
✓ Branch 1 taken 5085 times.
1301760 for (j = 1; j < 256; j++) {
499
2/2
✓ Branch 0 taken 5094 times.
✓ Branch 1 taken 1291581 times.
1296675 if (stt[j] == i)
500 5094 stt[j] = i2;
501
2/2
✓ Branch 0 taken 5122 times.
✓ Branch 1 taken 1286459 times.
1291581 else if (stt[j] == i2)
502 5122 stt[j] = i;
503
2/2
✓ Branch 0 taken 1296420 times.
✓ Branch 1 taken 255 times.
1296675 if (i != 256 - i2) {
504
2/2
✓ Branch 0 taken 4906 times.
✓ Branch 1 taken 1291514 times.
1296420 if (stt[256 - j] == 256 - i)
505 4906 stt[256 - j] = 256 - i2;
506
2/2
✓ Branch 0 taken 5124 times.
✓ Branch 1 taken 1286390 times.
1291514 else if (stt[256 - j] == 256 - i2)
507 5124 stt[256 - j] = 256 - i;
508 }
509 }
510 5085 print = changed = 1;
511 }
512 }
513 }
514
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 8 times.
37 } while (changed);
515 8 return print;
516 }
517
518
519 46 static int encode_determine_slices(AVCodecContext *avctx)
520 {
521 46 FFV1Context *s = avctx->priv_data;
522 46 int plane_count = 1 + 2*s->chroma_planes + s->transparency;
523 46 int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
524 46 int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
525
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;
526 46 s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
527
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 for (; s->num_v_slices < 32; s->num_v_slices++) {
528
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++) {
529 50 int maxw = (avctx->width + s->num_h_slices - 1) / s->num_h_slices;
530 50 int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
531
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)
532 continue;
533
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)
534 continue;
535
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (s->version < 4)
536
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)
537
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))
538 continue;
539
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)
540 46 return 0;
541 }
542 }
543 av_log(avctx, AV_LOG_ERROR,
544 "Unsupported number %d of slices requested, please specify a "
545 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
546 avctx->slices);
547 return AVERROR(ENOSYS);
548 }
549
550 51 av_cold int ff_ffv1_encode_init(AVCodecContext *avctx)
551 {
552 51 FFV1Context *s = avctx->priv_data;
553 int i, j, k, m, ret;
554
555
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 16 times.
51 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
556
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31 times.
35 avctx->slices > 1)
557 20 s->version = FFMAX(s->version, 2);
558
559
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) {
560 av_log(avctx, AV_LOG_ERROR, "2 Pass mode is not possible with golomb coding\n");
561 return AVERROR(EINVAL);
562 }
563
564 // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
565
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)
566 1 s->version = FFMAX(s->version, 2);
567
568
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) {
569 21 s->version = 3;
570 }
571
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) {
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (avctx->level < s->version) {
573 av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
574 return AVERROR(EINVAL);
575 }
576 25 s->version = avctx->level;
577 }
578
579
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (s->ec < 0) {
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (s->version >= 4) {
581 s->ec = 2;
582 s->crcref = 0x7a8c4079;
583
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 9 times.
51 } else if (s->version >= 3) {
584 42 s->ec = 1;
585 } else
586 9 s->ec = 0;
587 }
588
589 // CRC requires version 3+
590
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 9 times.
51 if (s->ec == 1)
591 42 s->version = FFMAX(s->version, 3);
592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (s->ec == 2)
593 s->version = FFMAX(s->version, 4);
594
595
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) {
596 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");
597 return AVERROR_INVALIDDATA;
598 }
599
600
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 22 times.
51 if (s->ac == AC_RANGE_CUSTOM_TAB) {
601
2/2
✓ Branch 0 taken 7395 times.
✓ Branch 1 taken 29 times.
7424 for (i = 1; i < 256; i++)
602 7395 s->state_transition[i] = ver2_state[i];
603 } else {
604 RangeCoder c;
605 22 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
606
2/2
✓ Branch 0 taken 5610 times.
✓ Branch 1 taken 22 times.
5632 for (i = 1; i < 256; i++)
607 5610 s->state_transition[i] = c.one_state[i];
608 }
609
610
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 51 times.
13107 for (i = 0; i < 256; i++) {
611 13056 s->quant_table_count = 2;
612
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) {
613 7680 s->quant_tables[0][0][i]= quant11[i];
614 7680 s->quant_tables[0][1][i]= 11*quant11[i];
615 7680 s->quant_tables[0][2][i]= 11*11*quant11[i];
616 7680 s->quant_tables[1][0][i]= quant11[i];
617 7680 s->quant_tables[1][1][i]= 11*quant11[i];
618 7680 s->quant_tables[1][2][i]= 11*11*quant5 [i];
619 7680 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
620 7680 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
621 7680 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
622 7680 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
623 } else {
624 5376 s->quant_tables[0][0][i]= quant9_10bit[i];
625 5376 s->quant_tables[0][1][i]= 9*quant9_10bit[i];
626 5376 s->quant_tables[0][2][i]= 9*9*quant9_10bit[i];
627 5376 s->quant_tables[1][0][i]= quant9_10bit[i];
628 5376 s->quant_tables[1][1][i]= 9*quant9_10bit[i];
629 5376 s->quant_tables[1][2][i]= 9*9*quant5_10bit[i];
630 5376 s->quant_tables[1][3][i]= 5*9*9*quant5_10bit[i];
631 5376 s->quant_tables[1][4][i]= 5*5*9*9*quant5_10bit[i];
632 5376 s->context_count[0] = (9 * 9 * 9 + 1) / 2;
633 5376 s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
634 }
635 }
636
637
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
638 return ret;
639
640
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (!s->transparency)
641 51 s->plane_count = 2;
642
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)
643 s->plane_count--;
644
645 51 s->picture_number = 0;
646
647
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 35 times.
51 if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
648
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (i = 0; i < s->quant_table_count; i++) {
649 32 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
650 sizeof(*s->rc_stat2[i]));
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!s->rc_stat2[i])
652 return AVERROR(ENOMEM);
653 }
654 }
655
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 43 times.
51 if (avctx->stats_in) {
656 8 char *p = avctx->stats_in;
657 8 uint8_t (*best_state)[256] = av_malloc_array(256, 256);
658 8 int gob_count = 0;
659 char *next;
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!best_state)
661 return AVERROR(ENOMEM);
662
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 av_assert0(s->version >= 2);
664
665 for (;;) {
666
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 8 times.
2056 for (j = 0; j < 256; j++)
667
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 2048 times.
6144 for (i = 0; i < 2; i++) {
668 4096 s->rc_stat[j][i] = strtol(p, &next, 0);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4096 times.
4096 if (next == p) {
670 av_log(avctx, AV_LOG_ERROR,
671 "2Pass file invalid at %d %d [%s]\n", j, i, p);
672 av_freep(&best_state);
673 return AVERROR_INVALIDDATA;
674 }
675 4096 p = next;
676 }
677
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < s->quant_table_count; i++)
678
2/2
✓ Branch 0 taken 54628 times.
✓ Branch 1 taken 16 times.
54644 for (j = 0; j < s->context_count[i]; j++) {
679
2/2
✓ Branch 0 taken 1748096 times.
✓ Branch 1 taken 54628 times.
1802724 for (k = 0; k < 32; k++)
680
2/2
✓ Branch 0 taken 3496192 times.
✓ Branch 1 taken 1748096 times.
5244288 for (m = 0; m < 2; m++) {
681 3496192 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3496192 times.
3496192 if (next == p) {
683 av_log(avctx, AV_LOG_ERROR,
684 "2Pass file invalid at %d %d %d %d [%s]\n",
685 i, j, k, m, p);
686 av_freep(&best_state);
687 return AVERROR_INVALIDDATA;
688 }
689 3496192 p = next;
690 }
691 }
692 8 gob_count = strtol(p, &next, 0);
693
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) {
694 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
695 av_freep(&best_state);
696 return AVERROR_INVALIDDATA;
697 }
698 8 p = next;
699
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 == ' ')
700 8 p++;
701
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (p[0] == 0)
702 8 break;
703 }
704
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->ac == AC_RANGE_CUSTOM_TAB)
705 8 sort_stt(s, s->state_transition);
706
707 8 find_best_state(best_state, s->state_transition);
708
709
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < s->quant_table_count; i++) {
710
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 16 times.
528 for (k = 0; k < 32; k++) {
711 512 double a=0, b=0;
712 512 int jp = 0;
713
2/2
✓ Branch 0 taken 1748096 times.
✓ Branch 1 taken 512 times.
1748608 for (j = 0; j < s->context_count[i]; j++) {
714 1748096 double p = 128;
715
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) {
716
2/2
✓ Branch 0 taken 291303 times.
✓ Branch 1 taken 20 times.
291323 if (a+b)
717 291303 p = 256.0 * b / (a + b);
718 291323 s->initial_states[i][jp][k] =
719 291323 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
720
2/2
✓ Branch 0 taken 883713 times.
✓ Branch 1 taken 291323 times.
1175036 for(jp++; jp<j; jp++)
721 883713 s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
722 291323 a=b=0;
723 }
724 1748096 a += s->rc_stat2[i][j][k][0];
725 1748096 b += s->rc_stat2[i][j][k][1];
726
2/2
✓ Branch 0 taken 1284515 times.
✓ Branch 1 taken 463581 times.
1748096 if (a+b) {
727 1284515 p = 256.0 * b / (a + b);
728 }
729 1748096 s->initial_states[i][j][k] =
730 1748096 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
731 }
732 }
733 }
734 8 av_freep(&best_state);
735 }
736
737
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 46 times.
51 if (s->version <= 1) {
738 /* Disable slices when the version doesn't support them */
739 5 s->num_h_slices = 1;
740 5 s->num_v_slices = 1;
741 }
742
743 51 return 0;
744 }
745
746 51 av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
747 enum AVPixelFormat pix_fmt)
748 {
749 51 FFV1Context *s = avctx->priv_data;
750 51 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
751
752 51 s->plane_count = 3;
753
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) {
754 case AV_PIX_FMT_GRAY9:
755 case AV_PIX_FMT_YUV444P9:
756 case AV_PIX_FMT_YUV422P9:
757 case AV_PIX_FMT_YUV420P9:
758 case AV_PIX_FMT_YUVA444P9:
759 case AV_PIX_FMT_YUVA422P9:
760 case AV_PIX_FMT_YUVA420P9:
761 if (!avctx->bits_per_raw_sample)
762 s->bits_per_raw_sample = 9;
763 case AV_PIX_FMT_GRAY10:
764 case AV_PIX_FMT_YUV444P10:
765 case AV_PIX_FMT_YUV440P10:
766 case AV_PIX_FMT_YUV420P10:
767 case AV_PIX_FMT_YUV422P10:
768 case AV_PIX_FMT_YUVA444P10:
769 case AV_PIX_FMT_YUVA422P10:
770 case AV_PIX_FMT_YUVA420P10:
771
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)
772 12 s->bits_per_raw_sample = 10;
773 case AV_PIX_FMT_GRAY12:
774 case AV_PIX_FMT_YUV444P12:
775 case AV_PIX_FMT_YUV440P12:
776 case AV_PIX_FMT_YUV420P12:
777 case AV_PIX_FMT_YUV422P12:
778 case AV_PIX_FMT_YUVA444P12:
779 case AV_PIX_FMT_YUVA422P12:
780
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)
781 s->bits_per_raw_sample = 12;
782 case AV_PIX_FMT_GRAY14:
783 case AV_PIX_FMT_YUV444P14:
784 case AV_PIX_FMT_YUV420P14:
785 case AV_PIX_FMT_YUV422P14:
786
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)
787 s->bits_per_raw_sample = 14;
788 13 s->packed_at_lsb = 1;
789 17 case AV_PIX_FMT_GRAY16:
790 case AV_PIX_FMT_YUV444P16:
791 case AV_PIX_FMT_YUV422P16:
792 case AV_PIX_FMT_YUV420P16:
793 case AV_PIX_FMT_YUVA444P16:
794 case AV_PIX_FMT_YUVA422P16:
795 case AV_PIX_FMT_YUVA420P16:
796
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) {
797 4 s->bits_per_raw_sample = 16;
798
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 } else if (!s->bits_per_raw_sample) {
799 1 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
800 }
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (s->bits_per_raw_sample <= 8) {
802 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
803 return AVERROR_INVALIDDATA;
804 }
805 17 s->version = FFMAX(s->version, 1);
806 43 case AV_PIX_FMT_GRAY8:
807 case AV_PIX_FMT_YA8:
808 case AV_PIX_FMT_YUV444P:
809 case AV_PIX_FMT_YUV440P:
810 case AV_PIX_FMT_YUV422P:
811 case AV_PIX_FMT_YUV420P:
812 case AV_PIX_FMT_YUV411P:
813 case AV_PIX_FMT_YUV410P:
814 case AV_PIX_FMT_YUVA444P:
815 case AV_PIX_FMT_YUVA422P:
816 case AV_PIX_FMT_YUVA420P:
817 43 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
818 43 s->colorspace = 0;
819 43 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
820
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)
821 26 s->bits_per_raw_sample = 8;
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 else if (!s->bits_per_raw_sample)
823 s->bits_per_raw_sample = 8;
824 43 break;
825 case AV_PIX_FMT_RGB32:
826 s->colorspace = 1;
827 s->transparency = 1;
828 s->chroma_planes = 1;
829 s->bits_per_raw_sample = 8;
830 break;
831 case AV_PIX_FMT_RGBA64:
832 s->colorspace = 1;
833 s->transparency = 1;
834 s->chroma_planes = 1;
835 s->bits_per_raw_sample = 16;
836 s->use32bit = 1;
837 s->version = FFMAX(s->version, 1);
838 break;
839 4 case AV_PIX_FMT_RGB48:
840 4 s->colorspace = 1;
841 4 s->chroma_planes = 1;
842 4 s->bits_per_raw_sample = 16;
843 4 s->use32bit = 1;
844 4 s->version = FFMAX(s->version, 1);
845 4 break;
846 4 case AV_PIX_FMT_0RGB32:
847 4 s->colorspace = 1;
848 4 s->chroma_planes = 1;
849 4 s->bits_per_raw_sample = 8;
850 4 break;
851 case AV_PIX_FMT_GBRP9:
852 if (!avctx->bits_per_raw_sample)
853 s->bits_per_raw_sample = 9;
854 case AV_PIX_FMT_GBRP10:
855 case AV_PIX_FMT_GBRAP10:
856 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
857 s->bits_per_raw_sample = 10;
858 case AV_PIX_FMT_GBRP12:
859 case AV_PIX_FMT_GBRAP12:
860 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
861 s->bits_per_raw_sample = 12;
862 case AV_PIX_FMT_GBRP14:
863 case AV_PIX_FMT_GBRAP14:
864 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
865 s->bits_per_raw_sample = 14;
866 case AV_PIX_FMT_GBRP16:
867 case AV_PIX_FMT_GBRAP16:
868 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
869 s->bits_per_raw_sample = 16;
870 else if (!s->bits_per_raw_sample)
871 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
872 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
873 s->colorspace = 1;
874 s->chroma_planes = 1;
875 if (s->bits_per_raw_sample >= 16) {
876 s->use32bit = 1;
877 }
878 s->version = FFMAX(s->version, 1);
879 break;
880 default:
881 av_log(avctx, AV_LOG_ERROR, "format %s not supported\n",
882 av_get_pix_fmt_name(pix_fmt));
883 return AVERROR(ENOSYS);
884 }
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 av_assert0(s->bits_per_raw_sample >= 8);
886
887 51 return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
888 }
889
890 51 static int encode_init_internal(AVCodecContext *avctx)
891 {
892 int ret;
893 51 FFV1Context *s = avctx->priv_data;
894
895
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_common_init(avctx)) < 0)
896 return ret;
897
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (s->ac == 1) // Compatbility with common command line usage
899 s->ac = AC_RANGE_CUSTOM_TAB;
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
901 s->ac = AC_RANGE_DEFAULT_TAB;
902
903 51 ret = ff_ffv1_encode_setup_plane_info(avctx, avctx->pix_fmt);
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ret < 0)
905 return ret;
906
907
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)) {
908
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
21 if (s->ac == AC_GOLOMB_RICE) {
909 13 av_log(avctx, AV_LOG_INFO,
910 "high bits_per_raw_sample, forcing range coder\n");
911 13 s->ac = AC_RANGE_CUSTOM_TAB;
912 }
913 }
914
915 51 s->version = 0;
916
917 51 ret = ff_ffv1_encode_init(avctx);
918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ret < 0)
919 return ret;
920
921
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 5 times.
51 if (s->version > 1) {
922
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = encode_determine_slices(avctx)) < 0)
923 return ret;
924
925
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = ff_ffv1_write_extradata(avctx)) < 0)
926 return ret;
927 }
928
929
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
930 return ret;
931 51 s->slice_count = s->max_slice_count;
932
933
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 51 times.
240 for (int j = 0; j < s->slice_count; j++) {
934
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 189 times.
567 for (int i = 0; i < s->plane_count; i++) {
935 378 PlaneContext *const p = &s->slices[j].plane[i];
936
937 378 p->quant_table_index = s->context_model;
938 378 p->context_count = s->context_count[p->quant_table_index];
939 }
940
941 189 ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
942 }
943
944
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
51 if ((ret = ff_ffv1_init_slices_state(s)) < 0)
945 return ret;
946
947 #define STATS_OUT_SIZE 1024 * 1024 * 6
948
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 43 times.
51 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
949 8 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!avctx->stats_out)
951 return AVERROR(ENOMEM);
952
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int i = 0; i < s->quant_table_count; i++)
953
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (int j = 0; j < s->max_slice_count; j++) {
954 64 FFV1SliceContext *sc = &s->slices[j];
955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 av_assert0(!sc->rc_stat2[i]);
956 64 sc->rc_stat2[i] = av_mallocz(s->context_count[i] *
957 sizeof(*sc->rc_stat2[i]));
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (!sc->rc_stat2[i])
959 return AVERROR(ENOMEM);
960 }
961 }
962
963 51 return 0;
964 }
965
966 8120 static void encode_slice_header(FFV1Context *f, FFV1SliceContext *sc)
967 {
968 8120 RangeCoder *c = &sc->c;
969 uint8_t state[CONTEXT_SIZE];
970 int j;
971 8120 memset(state, 128, sizeof(state));
972
973 8120 put_symbol(c, state, sc->sx, 0);
974 8120 put_symbol(c, state, sc->sy, 0);
975 8120 put_symbol(c, state, 0, 0);
976 8120 put_symbol(c, state, 0, 0);
977
2/2
✓ Branch 0 taken 16240 times.
✓ Branch 1 taken 8120 times.
24360 for (j=0; j<f->plane_count; j++) {
978 16240 put_symbol(c, state, sc->plane[j].quant_table_index, 0);
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16240 times.
16240 av_assert0(sc->plane[j].quant_table_index == f->context_model);
980 }
981
1/2
✓ Branch 0 taken 8120 times.
✗ Branch 1 not taken.
8120 if (!(f->cur_enc_frame->flags & AV_FRAME_FLAG_INTERLACED))
982 8120 put_symbol(c, state, 3, 0);
983 else
984 put_symbol(c, state, 1 + !(f->cur_enc_frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST), 0);
985 8120 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.num, 0);
986 8120 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.den, 0);
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8120 times.
8120 if (f->version > 3) {
988 put_rac(c, state, sc->slice_coding_mode == 1);
989 if (sc->slice_coding_mode == 1)
990 ff_ffv1_clear_slice_state(f, sc);
991 put_symbol(c, state, sc->slice_coding_mode, 0);
992 if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
993 put_symbol(c, state, sc->slice_rct_by_coef, 0);
994 put_symbol(c, state, sc->slice_rct_ry_coef, 0);
995 }
996 }
997 8120 }
998
999 static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
1000 const uint8_t *src[3], const int stride[3], int w, int h)
1001 {
1002 #define NB_Y_COEFF 15
1003 static const int rct_y_coeff[15][2] = {
1004 {0, 0}, // 4G
1005 {1, 1}, // R + 2G + B
1006 {2, 2}, // 2R + 2B
1007 {0, 2}, // 2G + 2B
1008 {2, 0}, // 2R + 2G
1009 {4, 0}, // 4R
1010 {0, 4}, // 4B
1011
1012 {0, 3}, // 1G + 3B
1013 {3, 0}, // 3R + 1G
1014 {3, 1}, // 3R + B
1015 {1, 3}, // R + 3B
1016 {1, 2}, // R + G + 2B
1017 {2, 1}, // 2R + G + B
1018 {0, 1}, // 3G + B
1019 {1, 0}, // R + 3G
1020 };
1021
1022 int stat[NB_Y_COEFF] = {0};
1023 int x, y, i, p, best;
1024 int16_t *sample[3];
1025 int lbd = f->bits_per_raw_sample <= 8;
1026 int packed = !src[1];
1027 int transparency = f->transparency;
1028 int packed_size = (3 + transparency)*2;
1029
1030 for (y = 0; y < h; y++) {
1031 int lastr=0, lastg=0, lastb=0;
1032 for (p = 0; p < 3; p++)
1033 sample[p] = sc->sample_buffer + p*w;
1034
1035 for (x = 0; x < w; x++) {
1036 int b, g, r;
1037 int ab, ag, ar;
1038 if (lbd) {
1039 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
1040 b = v & 0xFF;
1041 g = (v >> 8) & 0xFF;
1042 r = (v >> 16) & 0xFF;
1043 } else if (packed) {
1044 const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
1045 r = p[0];
1046 g = p[1];
1047 b = p[2];
1048 } else {
1049 b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
1050 g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
1051 r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
1052 }
1053
1054 ar = r - lastr;
1055 ag = g - lastg;
1056 ab = b - lastb;
1057 if (x && y) {
1058 int bg = ag - sample[0][x];
1059 int bb = ab - sample[1][x];
1060 int br = ar - sample[2][x];
1061
1062 br -= bg;
1063 bb -= bg;
1064
1065 for (i = 0; i<NB_Y_COEFF; i++) {
1066 stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1067 }
1068
1069 }
1070 sample[0][x] = ag;
1071 sample[1][x] = ab;
1072 sample[2][x] = ar;
1073
1074 lastr = r;
1075 lastg = g;
1076 lastb = b;
1077 }
1078 }
1079
1080 best = 0;
1081 for (i=1; i<NB_Y_COEFF; i++) {
1082 if (stat[i] < stat[best])
1083 best = i;
1084 }
1085
1086 sc->slice_rct_by_coef = rct_y_coeff[best][1];
1087 sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1088 }
1089
1090 9170 static int encode_slice(AVCodecContext *c, void *arg)
1091 {
1092 9170 FFV1SliceContext *sc = arg;
1093 9170 FFV1Context *f = c->priv_data;
1094 9170 int width = sc->slice_width;
1095 9170 int height = sc->slice_height;
1096 9170 int x = sc->slice_x;
1097 9170 int y = sc->slice_y;
1098 9170 const AVFrame *const p = f->cur_enc_frame;
1099 9170 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1100 int ret;
1101 9170 RangeCoder c_bak = sc->c;
1102 36680 const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1103
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,
1104
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,
1105
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};
1106 9170 int ac = f->ac;
1107
1108 9170 sc->slice_coding_mode = 0;
1109
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) {
1110 choose_rct_params(f, sc, planes, p->linesize, width, height);
1111 } else {
1112 9170 sc->slice_rct_by_coef = 1;
1113 9170 sc->slice_rct_ry_coef = 1;
1114 }
1115
1116 9170 retry:
1117
2/2
✓ Branch 0 taken 921 times.
✓ Branch 1 taken 8249 times.
9170 if (f->key_frame)
1118 921 ff_ffv1_clear_slice_state(f, sc);
1119
2/2
✓ Branch 0 taken 8120 times.
✓ Branch 1 taken 1050 times.
9170 if (f->version > 2) {
1120 8120 encode_slice_header(f, sc);
1121 }
1122
2/2
✓ Branch 0 taken 3550 times.
✓ Branch 1 taken 5620 times.
9170 if (ac == AC_GOLOMB_RICE) {
1123
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;
1124 3550 init_put_bits(&sc->pb,
1125 3550 sc->c.bytestream_start + sc->ac_byte_count,
1126 3550 sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count);
1127 }
1128
1129
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) {
1130 7570 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
1131 7570 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1132 7570 const int cx = x >> f->chroma_h_shift;
1133 7570 const int cy = y >> f->chroma_v_shift;
1134
1135 7570 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1, ac);
1136
1137
1/2
✓ Branch 0 taken 7570 times.
✗ Branch 1 not taken.
7570 if (f->chroma_planes) {
1138 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);
1139 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);
1140 }
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7570 times.
7570 if (f->transparency)
1142 ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1, ac);
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
1144 ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2, ac);
1145 ret |= encode_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2, ac);
1146
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 } else if (f->use32bit) {
1147 800 ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize);
1148 } else {
1149 800 ret = encode_rgb_frame(f, sc, planes, width, height, p->linesize);
1150 }
1151
1152
2/2
✓ Branch 0 taken 5620 times.
✓ Branch 1 taken 3550 times.
9170 if (ac != AC_GOLOMB_RICE) {
1153 5620 sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
1154 } else {
1155 3550 flush_put_bits(&sc->pb); // FIXME: nicer padding
1156 3550 sc->ac_byte_count += put_bytes_output(&sc->pb);
1157 }
1158
1159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9170 times.
9170 if (ret < 0) {
1160 av_assert0(sc->slice_coding_mode == 0);
1161 if (f->version < 4) {
1162 av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1163 return ret;
1164 }
1165 av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1166 ac = 1;
1167 sc->slice_coding_mode = 1;
1168 sc->c = c_bak;
1169 goto retry;
1170 }
1171
1172 9170 return 0;
1173 }
1174
1175 2531 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1176 const AVFrame *pict, int *got_packet)
1177 {
1178 2531 FFV1Context *f = avctx->priv_data;
1179 2531 RangeCoder *const c = &f->slices[0].c;
1180 2531 uint8_t keystate = 128;
1181 uint8_t *buf_p;
1182 int i, ret;
1183 int64_t maxsize;
1184
1185
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 2480 times.
2531 if(!pict) {
1186
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 43 times.
51 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1187 int j, k, m;
1188 8 char *p = avctx->stats_out;
1189 8 char *end = p + STATS_OUT_SIZE;
1190
1191 8 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1192
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < f->quant_table_count; i++)
1193 16 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1194
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 av_assert0(f->slice_count == f->max_slice_count);
1196
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 for (j = 0; j < f->slice_count; j++) {
1197 32 const FFV1SliceContext *sc = &f->slices[j];
1198
2/2
✓ Branch 0 taken 8192 times.
✓ Branch 1 taken 32 times.
8224 for (i = 0; i < 256; i++) {
1199 8192 f->rc_stat[i][0] += sc->rc_stat[i][0];
1200 8192 f->rc_stat[i][1] += sc->rc_stat[i][1];
1201 }
1202
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 for (i = 0; i < f->quant_table_count; i++) {
1203
2/2
✓ Branch 0 taken 218512 times.
✓ Branch 1 taken 64 times.
218576 for (k = 0; k < f->context_count[i]; k++)
1204
2/2
✓ Branch 0 taken 6992384 times.
✓ Branch 1 taken 218512 times.
7210896 for (m = 0; m < 32; m++) {
1205 6992384 f->rc_stat2[i][k][m][0] += sc->rc_stat2[i][k][m][0];
1206 6992384 f->rc_stat2[i][k][m][1] += sc->rc_stat2[i][k][m][1];
1207 }
1208 }
1209 }
1210
1211
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 8 times.
2056 for (j = 0; j < 256; j++) {
1212 2048 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1213 f->rc_stat[j][0], f->rc_stat[j][1]);
1214 2048 p += strlen(p);
1215 }
1216 8 snprintf(p, end - p, "\n");
1217
1218
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < f->quant_table_count; i++) {
1219
2/2
✓ Branch 0 taken 54628 times.
✓ Branch 1 taken 16 times.
54644 for (j = 0; j < f->context_count[i]; j++)
1220
2/2
✓ Branch 0 taken 1748096 times.
✓ Branch 1 taken 54628 times.
1802724 for (m = 0; m < 32; m++) {
1221 1748096 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1222 1748096 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1223 1748096 p += strlen(p);
1224 }
1225 }
1226 8 snprintf(p, end - p, "%d\n", f->gob_count);
1227 }
1228 51 return 0;
1229 }
1230
1231 2480 maxsize = avctx->width*avctx->height * (1 + f->transparency);
1232
1/2
✓ Branch 0 taken 2480 times.
✗ Branch 1 not taken.
2480 if (f->chroma_planes)
1233 2480 maxsize += AV_CEIL_RSHIFT(avctx->width, f->chroma_h_shift) * AV_CEIL_RSHIFT(f->height, f->chroma_v_shift) * 2;
1234 2480 maxsize += f->slice_count * 800; //for slice header
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (f->version > 3) {
1236 maxsize *= f->bits_per_raw_sample + 1;
1237 } else {
1238 2480 maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
1239 2480 maxsize *= 8*(2*f->bits_per_raw_sample + 5);
1240 }
1241 2480 maxsize >>= 3;
1242 2480 maxsize += FF_INPUT_BUFFER_MIN_SIZE;
1243
1244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1245 av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1246 maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
1247 }
1248
1249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2480 times.
2480 if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
1250 return ret;
1251
1252 2480 ff_init_range_encoder(c, pkt->data, pkt->size);
1253 2480 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1254
1255 2480 f->cur_enc_frame = pict;
1256
1257
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) {
1258 249 put_rac(c, &keystate, 1);
1259 249 f->key_frame = 1;
1260 249 f->gob_count++;
1261 249 write_header(f);
1262 } else {
1263 2231 put_rac(c, &keystate, 0);
1264 2231 f->key_frame = 0;
1265 }
1266
1267
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 1075 times.
2480 if (f->ac == AC_RANGE_CUSTOM_TAB) {
1268 int i;
1269
2/2
✓ Branch 0 taken 358275 times.
✓ Branch 1 taken 1405 times.
359680 for (i = 1; i < 256; i++) {
1270 358275 c->one_state[i] = f->state_transition[i];
1271 358275 c->zero_state[256 - i] = 256 - c->one_state[i];
1272 }
1273 }
1274
1275
2/2
✓ Branch 0 taken 9170 times.
✓ Branch 1 taken 2480 times.
11650 for (i = 0; i < f->slice_count; i++) {
1276 9170 FFV1SliceContext *sc = &f->slices[i];
1277 9170 uint8_t *start = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1278 9170 int len = pkt->size / f->slice_count;
1279
2/2
✓ Branch 0 taken 6690 times.
✓ Branch 1 taken 2480 times.
9170 if (i) {
1280 6690 ff_init_range_encoder(&sc->c, start, len);
1281 } else {
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 av_assert0(sc->c.bytestream_end >= sc->c.bytestream_start + len);
1283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 av_assert0(sc->c.bytestream < sc->c.bytestream_start + len);
1284 2480 sc->c.bytestream_end = sc->c.bytestream_start + len;
1285 }
1286 }
1287 2480 avctx->execute(avctx, encode_slice, f->slices, NULL,
1288 f->slice_count, sizeof(*f->slices));
1289
1290 2480 buf_p = pkt->data;
1291
2/2
✓ Branch 0 taken 9170 times.
✓ Branch 1 taken 2480 times.
11650 for (i = 0; i < f->slice_count; i++) {
1292 9170 FFV1SliceContext *sc = &f->slices[i];
1293 9170 int bytes = sc->ac_byte_count;
1294
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) {
1295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8720 times.
8720 av_assert0(bytes < pkt->size / f->slice_count);
1296 8720 memmove(buf_p, sc->c.bytestream_start, bytes);
1297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8720 times.
8720 av_assert0(bytes < (1 << 24));
1298 8720 AV_WB24(buf_p + bytes, bytes);
1299 8720 bytes += 3;
1300 }
1301
2/2
✓ Branch 0 taken 8120 times.
✓ Branch 1 taken 1050 times.
9170 if (f->ec) {
1302 unsigned v;
1303 8120 buf_p[bytes++] = 0;
1304
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);
1305 8120 AV_WL32(buf_p + bytes, v);
1306 8120 bytes += 4;
1307 }
1308 9170 buf_p += bytes;
1309 }
1310
1311
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 2080 times.
2480 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1312 400 avctx->stats_out[0] = '\0';
1313
1314 2480 f->picture_number++;
1315 2480 pkt->size = buf_p - pkt->data;
1316 2480 pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1317 2480 *got_packet = 1;
1318
1319 2480 return 0;
1320 }
1321
1322 #define OFFSET(x) offsetof(FFV1Context, x)
1323 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1324 static const AVOption options[] = {
1325 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },
1326 { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1327 { .i64 = 0 }, -2, 2, VE, .unit = "coder" },
1328 { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1329 { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1330 { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1331 { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1332 { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1333 { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1334 { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
1335 { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1336 { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1337 { .i64 = 0 }, 0, 1, VE },
1338 { "qtable", "Quantization table", OFFSET(qtable), AV_OPT_TYPE_INT,
1339 { .i64 = -1 }, -1, 2, VE },
1340
1341 { NULL }
1342 };
1343
1344 static const AVClass ffv1_class = {
1345 .class_name = "ffv1 encoder",
1346 .item_name = av_default_item_name,
1347 .option = options,
1348 .version = LIBAVUTIL_VERSION_INT,
1349 };
1350
1351 const FFCodec ff_ffv1_encoder = {
1352 .p.name = "ffv1",
1353 CODEC_LONG_NAME("FFmpeg video codec #1"),
1354 .p.type = AVMEDIA_TYPE_VIDEO,
1355 .p.id = AV_CODEC_ID_FFV1,
1356 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1357 AV_CODEC_CAP_SLICE_THREADS |
1358 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1359 .priv_data_size = sizeof(FFV1Context),
1360 .init = encode_init_internal,
1361 FF_CODEC_ENCODE_CB(encode_frame),
1362 .close = ff_ffv1_close,
1363 .p.pix_fmts = (const enum AVPixelFormat[]) {
1364 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
1365 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
1366 AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
1367 AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
1368 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1369 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1370 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
1371 AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12,
1372 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
1373 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
1374 AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
1375 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRAP14,
1376 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
1377 AV_PIX_FMT_YA8,
1378 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
1379 AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48,
1380 AV_PIX_FMT_GBRAP16, AV_PIX_FMT_RGBA64,
1381 AV_PIX_FMT_GRAY9,
1382 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
1383 AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
1384 AV_PIX_FMT_NONE
1385
1386 },
1387 .color_ranges = AVCOL_RANGE_MPEG,
1388 .p.priv_class = &ffv1_class,
1389 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,
1390 };
1391