FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1enc.c
Date: 2024-05-11 02:28:20
Exec Total Coverage
Lines: 524 688 76.2%
Functions: 15 16 93.8%
Branches: 375 542 69.2%

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
43 static const int8_t quant5_10bit[256] = {
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 2, 2, 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, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
60 };
61
62 static const int8_t quant5[256] = {
63 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
64 2, 2, 2, 2, 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, -1, -1, -1,
79 };
80
81 static const int8_t quant9_10bit[256] = {
82 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
83 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
84 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
85 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
86 4, 4, 4, 4, 4, 4, 4, 4, 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, -3, -3, -3, -3, -3, -3, -3,
95 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
96 -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
97 -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
98 };
99
100 static const int8_t quant11[256] = {
101 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
102 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
103 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
104 5, 5, 5, 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, -4, -4,
115 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
116 -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
117 };
118
119 static const uint8_t ver2_state[256] = {
120 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
121 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
122 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
123 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
124 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
125 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
126 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
127 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
128 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
129 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
130 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
131 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
132 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
133 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
134 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
135 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
136 };
137
138 4 static void find_best_state(uint8_t best_state[256][256],
139 const uint8_t one_state[256])
140 {
141 int i, j, k, m;
142 uint32_t l2tab[256];
143
144
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 4 times.
1024 for (i = 1; i < 256; i++)
145 1020 l2tab[i] = -log2(i / 256.0) * ((1U << 31) / 8);
146
147
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
1028 for (i = 0; i < 256; i++) {
148 uint64_t best_len[256];
149
150
2/2
✓ Branch 0 taken 262144 times.
✓ Branch 1 taken 1024 times.
263168 for (j = 0; j < 256; j++)
151 262144 best_len[j] = UINT64_MAX;
152
153
2/2
✓ Branch 0 taken 21020 times.
✓ Branch 1 taken 1024 times.
22044 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
154 21020 uint32_t occ[256] = { 0 };
155 21020 uint64_t len = 0;
156 21020 occ[j] = UINT32_MAX;
157
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21020 times.
21020 if (!one_state[j])
159 continue;
160
161
2/2
✓ Branch 0 taken 5381120 times.
✓ Branch 1 taken 21020 times.
5402140 for (k = 0; k < 256; k++) {
162 5381120 uint32_t newocc[256] = { 0 };
163
2/2
✓ Branch 0 taken 1372185600 times.
✓ Branch 1 taken 5381120 times.
1377566720 for (m = 1; m < 256; m++)
164
2/2
✓ Branch 0 taken 430575762 times.
✓ Branch 1 taken 941609838 times.
1372185600 if (occ[m]) {
165 430575762 len += (occ[m]*(( i *(uint64_t)l2tab[ m]
166 430575762 + (256-i)*(uint64_t)l2tab[256-m])>>8)) >> 8;
167 }
168
2/2
✓ Branch 0 taken 1579284 times.
✓ Branch 1 taken 3801836 times.
5381120 if (len < best_len[k]) {
169 1579284 best_len[k] = len;
170 1579284 best_state[i][k] = j;
171 }
172
2/2
✓ Branch 0 taken 1372185600 times.
✓ Branch 1 taken 5381120 times.
1377566720 for (m = 1; m < 256; m++)
173
2/2
✓ Branch 0 taken 430575762 times.
✓ Branch 1 taken 941609838 times.
1372185600 if (occ[m]) {
174 430575762 newocc[ one_state[ m]] += occ[m] * (uint64_t) i >> 8;
175 430575762 newocc[256 - one_state[256 - m]] += occ[m] * (uint64_t)(256 - i) >> 8;
176 }
177 5381120 memcpy(occ, newocc, sizeof(occ));
178 }
179 }
180 }
181 4 }
182
183 178167061 static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
184 uint8_t *state, int v,
185 int is_signed,
186 uint64_t rc_stat[256][2],
187 uint64_t rc_stat2[32][2])
188 {
189 int i;
190
191 #define put_rac(C, S, B) \
192 do { \
193 if (rc_stat) { \
194 rc_stat[*(S)][B]++; \
195 rc_stat2[(S) - state][B]++; \
196 } \
197 put_rac(C, S, B); \
198 } while (0)
199
200
2/2
✓ Branch 0 taken 111357609 times.
✓ Branch 1 taken 66809452 times.
178167061 if (v) {
201 111357609 const int a = FFABS(v);
202 111357609 const int e = av_log2(a);
203
2/2
✓ Branch 0 taken 19292649 times.
✓ Branch 1 taken 92064960 times.
111357609 put_rac(c, state + 0, 0);
204
2/2
✓ Branch 0 taken 86395431 times.
✓ Branch 1 taken 24962178 times.
111357609 if (e <= 9) {
205
2/2
✓ Branch 0 taken 293149058 times.
✓ Branch 1 taken 86395431 times.
379544489 for (i = 0; i < e; i++)
206
2/2
✓ Branch 0 taken 32190778 times.
✓ Branch 1 taken 260958280 times.
293149058 put_rac(c, state + 1 + i, 1); // 1..10
207
2/2
✓ Branch 0 taken 19292649 times.
✓ Branch 1 taken 67102782 times.
86395431 put_rac(c, state + 1 + i, 0);
208
209
2/2
✓ Branch 0 taken 293149058 times.
✓ Branch 1 taken 86395431 times.
379544489 for (i = e - 1; i >= 0; i--)
210
2/2
✓ Branch 0 taken 32190778 times.
✓ Branch 1 taken 260958280 times.
293149058 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
211
212
2/2
✓ Branch 0 taken 86370789 times.
✓ Branch 1 taken 24642 times.
86395431 if (is_signed)
213
2/2
✓ Branch 0 taken 19292649 times.
✓ Branch 1 taken 67078140 times.
86370789 put_rac(c, state + 11 + e, v < 0); // 11..21
214 } else {
215
2/2
✓ Branch 0 taken 269770289 times.
✓ Branch 1 taken 24962178 times.
294732467 for (i = 0; i < e; i++)
216
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
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24962178 times.
24962178 put_rac(c, state + 1 + 9, 0);
218
219
2/2
✓ Branch 0 taken 269770289 times.
✓ Branch 1 taken 24962178 times.
294732467 for (i = e - 1; i >= 0; i--)
220
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
221
222
1/2
✓ Branch 0 taken 24962178 times.
✗ Branch 1 not taken.
24962178 if (is_signed)
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24962178 times.
24962178 put_rac(c, state + 11 + 10, v < 0); // 11..21
224 }
225 } else {
226
2/2
✓ Branch 0 taken 3607151 times.
✓ Branch 1 taken 63202301 times.
66809452 put_rac(c, state + 0, 1);
227 }
228 #undef put_rac
229 178167061 }
230
231 1034461 static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
232 int v, int is_signed)
233 {
234 1034461 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
235 1034461 }
236
237
238 141199074 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
239 int v, int bits)
240 {
241 int i, k, code;
242 141199074 v = fold(v - state->bias, bits);
243
244 141199074 i = state->count;
245 141199074 k = 0;
246
2/2
✓ Branch 0 taken 235935891 times.
✓ Branch 1 taken 141199074 times.
377134965 while (i < state->error_sum) { // FIXME: optimize
247 235935891 k++;
248 235935891 i += i;
249 }
250
251 av_assert2(k <= 13);
252
253 141199074 code = v ^ ((2 * state->drift + state->count) >> 31);
254
255 ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
256 state->bias, state->error_sum, state->drift, state->count, k);
257 141199074 set_sr_golomb(pb, code, k, 12, bits);
258
259 141199074 update_vlc_state(state, v);
260 141199074 }
261
262 #define TYPE int16_t
263 #define RENAME(name) name
264 #include "ffv1enc_template.c"
265 #undef TYPE
266 #undef RENAME
267
268 #define TYPE int32_t
269 #define RENAME(name) name ## 32
270 #include "ffv1enc_template.c"
271
272 15510 static int encode_plane(FFV1Context *s, const uint8_t *src, int w, int h,
273 int stride, int plane_index, int pixel_stride)
274 {
275 int x, y, i, ret;
276
2/2
✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 10710 times.
15510 const int ring_size = s->context_model ? 3 : 2;
277 int16_t *sample[3];
278 15510 s->run_index = 0;
279
280 15510 memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
281
282
2/2
✓ Branch 0 taken 1455800 times.
✓ Branch 1 taken 15510 times.
1471310 for (y = 0; y < h; y++) {
283
2/2
✓ Branch 0 taken 3271200 times.
✓ Branch 1 taken 1455800 times.
4727000 for (i = 0; i < ring_size; i++)
284 3271200 sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
285
286 1455800 sample[0][-1]= sample[1][0 ];
287 1455800 sample[1][ w]= sample[1][w-1];
288
2/2
✓ Branch 0 taken 895400 times.
✓ Branch 1 taken 560400 times.
1455800 if (s->bits_per_raw_sample <= 8) {
289
2/2
✓ Branch 0 taken 145253900 times.
✓ Branch 1 taken 895400 times.
146149300 for (x = 0; x < w; x++)
290 145253900 sample[0][x] = src[x * pixel_stride + stride * y];
291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 895400 times.
895400 if((ret = encode_line(s, w, sample, plane_index, 8)) < 0)
292 return ret;
293 } else {
294
2/2
✓ Branch 0 taken 291000 times.
✓ Branch 1 taken 269400 times.
560400 if (s->packed_at_lsb) {
295
2/2
✓ Branch 0 taken 39747800 times.
✓ Branch 1 taken 291000 times.
40038800 for (x = 0; x < w; x++) {
296 39747800 sample[0][x] = ((uint16_t*)(src + stride*y))[x];
297 }
298 } else {
299
2/2
✓ Branch 0 taken 45792600 times.
✓ Branch 1 taken 269400 times.
46062000 for (x = 0; x < w; x++) {
300 45792600 sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - s->bits_per_raw_sample);
301 }
302 }
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 560400 times.
560400 if((ret = encode_line(s, w, sample, plane_index, s->bits_per_raw_sample)) < 0)
304 return ret;
305 }
306 }
307 15510 return 0;
308 }
309
310 465 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
311 {
312 465 int last = 0;
313 int i;
314 uint8_t state[CONTEXT_SIZE];
315 465 memset(state, 128, sizeof(state));
316
317
2/2
✓ Branch 0 taken 59055 times.
✓ Branch 1 taken 465 times.
59520 for (i = 1; i < 128; i++)
318
2/2
✓ Branch 0 taken 1364 times.
✓ Branch 1 taken 57691 times.
59055 if (quant_table[i] != quant_table[i - 1]) {
319 1364 put_symbol(c, state, i - last - 1, 0);
320 1364 last = i;
321 }
322 465 put_symbol(c, state, i - last - 1, 0);
323 465 }
324
325 93 static void write_quant_tables(RangeCoder *c,
326 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
327 {
328 int i;
329
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 93 times.
558 for (i = 0; i < 5; i++)
330 465 write_quant_table(c, quant_table[i]);
331 93 }
332
333 68 static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
334 int nb_contexts)
335 {
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!initial_state)
337 return 0;
338
2/2
✓ Branch 0 taken 249538 times.
✓ Branch 1 taken 64 times.
249602 for (int i = 0; i < nb_contexts; i++)
339
2/2
✓ Branch 0 taken 7985092 times.
✓ Branch 1 taken 249534 times.
8234626 for (int j = 0; j < CONTEXT_SIZE; j++)
340
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7985088 times.
7985092 if (initial_state[i][j] != 128)
341 4 return 1;
342 64 return 0;
343 }
344
345 189 static void write_header(FFV1Context *f)
346 {
347 uint8_t state[CONTEXT_SIZE];
348 int i, j;
349 189 RangeCoder *const c = &f->slice_context[0]->c;
350
351 189 memset(state, 128, sizeof(state));
352
353
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 164 times.
189 if (f->version < 2) {
354 25 put_symbol(c, state, f->version, 0);
355 25 put_symbol(c, state, f->ac, 0);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (f->ac == AC_RANGE_CUSTOM_TAB) {
357 for (i = 1; i < 256; i++)
358 put_symbol(c, state,
359 f->state_transition[i] - c->one_state[i], 1);
360 }
361 25 put_symbol(c, state, f->colorspace, 0); //YUV cs type
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (f->version > 0)
363 put_symbol(c, state, f->bits_per_raw_sample, 0);
364 25 put_rac(c, state, f->chroma_planes);
365 25 put_symbol(c, state, f->chroma_h_shift, 0);
366 25 put_symbol(c, state, f->chroma_v_shift, 0);
367 25 put_rac(c, state, f->transparency);
368
369 25 write_quant_tables(c, f->quant_table);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 } else if (f->version < 3) {
371 put_symbol(c, state, f->slice_count, 0);
372 for (i = 0; i < f->slice_count; i++) {
373 FFV1Context *fs = f->slice_context[i];
374 put_symbol(c, state,
375 (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
376 put_symbol(c, state,
377 (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
378 put_symbol(c, state,
379 (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
380 0);
381 put_symbol(c, state,
382 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
383 0);
384 for (j = 0; j < f->plane_count; j++) {
385 put_symbol(c, state, f->plane[j].quant_table_index, 0);
386 av_assert0(f->plane[j].quant_table_index == f->context_model);
387 }
388 }
389 }
390 189 }
391
392 34 static int write_extradata(FFV1Context *f)
393 {
394 34 RangeCoder *const c = &f->c;
395 uint8_t state[CONTEXT_SIZE];
396 int i, j, k;
397 uint8_t state2[32][CONTEXT_SIZE];
398 unsigned v;
399
400 34 memset(state2, 128, sizeof(state2));
401 34 memset(state, 128, sizeof(state));
402
403 34 f->avctx->extradata_size = 10000 + 4 +
404 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
405 34 f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!f->avctx->extradata)
407 return AVERROR(ENOMEM);
408 34 ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
409 34 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
410
411 34 put_symbol(c, state, f->version, 0);
412
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (f->version > 2) {
413
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (f->version == 3) {
414 34 f->micro_version = 4;
415 } else if (f->version == 4)
416 f->micro_version = 2;
417 34 put_symbol(c, state, f->micro_version, 0);
418 }
419
420 34 put_symbol(c, state, f->ac, 0);
421
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 13 times.
34 if (f->ac == AC_RANGE_CUSTOM_TAB)
422
2/2
✓ Branch 0 taken 5355 times.
✓ Branch 1 taken 21 times.
5376 for (i = 1; i < 256; i++)
423 5355 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
424
425 34 put_symbol(c, state, f->colorspace, 0); // YUV cs type
426 34 put_symbol(c, state, f->bits_per_raw_sample, 0);
427 34 put_rac(c, state, f->chroma_planes);
428 34 put_symbol(c, state, f->chroma_h_shift, 0);
429 34 put_symbol(c, state, f->chroma_v_shift, 0);
430 34 put_rac(c, state, f->transparency);
431 34 put_symbol(c, state, f->num_h_slices - 1, 0);
432 34 put_symbol(c, state, f->num_v_slices - 1, 0);
433
434 34 put_symbol(c, state, f->quant_table_count, 0);
435
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
102 for (i = 0; i < f->quant_table_count; i++)
436 68 write_quant_tables(c, f->quant_tables[i]);
437
438
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
102 for (i = 0; i < f->quant_table_count; i++) {
439
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 64 times.
68 if (contains_non_128(f->initial_states[i], f->context_count[i])) {
440 4 put_rac(c, state, 1);
441
2/2
✓ Branch 0 taken 30252 times.
✓ Branch 1 taken 4 times.
30256 for (j = 0; j < f->context_count[i]; j++)
442
2/2
✓ Branch 0 taken 968064 times.
✓ Branch 1 taken 30252 times.
998316 for (k = 0; k < CONTEXT_SIZE; k++) {
443
2/2
✓ Branch 0 taken 967936 times.
✓ Branch 1 taken 128 times.
968064 int pred = j ? f->initial_states[i][j - 1][k] : 128;
444 968064 put_symbol(c, state2[k],
445 968064 (int8_t)(f->initial_states[i][j][k] - pred), 1);
446 }
447 } else {
448 64 put_rac(c, state, 0);
449 }
450 }
451
452
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (f->version > 2) {
453 34 put_symbol(c, state, f->ec, 0);
454 34 put_symbol(c, state, f->intra = (f->avctx->gop_size < 2), 0);
455 }
456
457 34 f->avctx->extradata_size = ff_rac_terminate(c, 0);
458 34 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
459 34 AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
460 34 f->avctx->extradata_size += 4;
461
462 34 return 0;
463 }
464
465 4 static int sort_stt(FFV1Context *s, uint8_t stt[256])
466 {
467 4 int i, i2, changed, print = 0;
468
469 do {
470 19 changed = 0;
471
2/2
✓ Branch 0 taken 4408 times.
✓ Branch 1 taken 19 times.
4427 for (i = 12; i < 244; i++) {
472
4/4
✓ Branch 0 taken 17518 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 13167 times.
✓ Branch 3 taken 4351 times.
17575 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
473
474 #define COST(old, new) \
475 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
476 s->rc_stat[old][1] * -log2((new) / 256.0)
477
478 #define COST2(old, new) \
479 COST(old, new) + COST(256 - (old), 256 - (new))
480
481 13167 double size0 = COST2(i, i) + COST2(i2, i2);
482 13167 double sizeX = COST2(i, i2) + COST2(i2, i);
483
4/6
✓ Branch 0 taken 2341 times.
✓ Branch 1 taken 10826 times.
✓ Branch 2 taken 2341 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2341 times.
✗ Branch 5 not taken.
13167 if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
484 int j;
485 2341 FFSWAP(int, stt[i], stt[i2]);
486 2341 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
487 2341 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
488
2/2
✓ Branch 0 taken 2340 times.
✓ Branch 1 taken 1 times.
2341 if (i != 256 - i2) {
489 2340 FFSWAP(int, stt[256 - i], stt[256 - i2]);
490 2340 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
491 2340 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
492 }
493
2/2
✓ Branch 0 taken 596955 times.
✓ Branch 1 taken 2341 times.
599296 for (j = 1; j < 256; j++) {
494
2/2
✓ Branch 0 taken 2335 times.
✓ Branch 1 taken 594620 times.
596955 if (stt[j] == i)
495 2335 stt[j] = i2;
496
2/2
✓ Branch 0 taken 2356 times.
✓ Branch 1 taken 592264 times.
594620 else if (stt[j] == i2)
497 2356 stt[j] = i;
498
2/2
✓ Branch 0 taken 596700 times.
✓ Branch 1 taken 255 times.
596955 if (i != 256 - i2) {
499
2/2
✓ Branch 0 taken 2364 times.
✓ Branch 1 taken 594336 times.
596700 if (stt[256 - j] == 256 - i)
500 2364 stt[256 - j] = 256 - i2;
501
2/2
✓ Branch 0 taken 2420 times.
✓ Branch 1 taken 591916 times.
594336 else if (stt[256 - j] == 256 - i2)
502 2420 stt[256 - j] = 256 - i;
503 }
504 }
505 2341 print = changed = 1;
506 }
507 }
508 }
509
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 4 times.
19 } while (changed);
510 4 return print;
511 }
512
513 39 static av_cold int encode_init(AVCodecContext *avctx)
514 {
515 39 FFV1Context *s = avctx->priv_data;
516 39 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
517 int i, j, k, m, ret;
518
519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = ff_ffv1_common_init(avctx)) < 0)
520 return ret;
521
522 39 s->version = 0;
523
524
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 8 times.
39 if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
525
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 27 times.
31 avctx->slices > 1)
526 12 s->version = FFMAX(s->version, 2);
527
528 // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
529
6/6
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 13 times.
39 if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
530 1 s->version = FFMAX(s->version, 2);
531
532
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 5 times.
39 if (avctx->level <= 0 && s->version == 2) {
533 13 s->version = 3;
534 }
535
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
39 if (avctx->level >= 0 && avctx->level <= 4) {
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (avctx->level < s->version) {
537 av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
538 return AVERROR(EINVAL);
539 }
540 21 s->version = avctx->level;
541 }
542
543
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (s->ec < 0) {
544 39 s->ec = (s->version >= 3);
545 }
546
547 // CRC requires version 3+
548
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 5 times.
39 if (s->ec)
549 34 s->version = FFMAX(s->version, 3);
550
551
2/6
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
39 if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
552 av_log(avctx, AV_LOG_ERROR, "Version 2 needed for requested features but version 2 is experimental and not enabled\n");
553 return AVERROR_INVALIDDATA;
554 }
555
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (s->ac == 1) // Compatbility with common command line usage
557 s->ac = AC_RANGE_CUSTOM_TAB;
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
559 s->ac = AC_RANGE_DEFAULT_TAB;
560
561 39 s->plane_count = 3;
562
5/16
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 22 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.
39 switch(avctx->pix_fmt) {
563 case AV_PIX_FMT_GRAY9:
564 case AV_PIX_FMT_YUV444P9:
565 case AV_PIX_FMT_YUV422P9:
566 case AV_PIX_FMT_YUV420P9:
567 case AV_PIX_FMT_YUVA444P9:
568 case AV_PIX_FMT_YUVA422P9:
569 case AV_PIX_FMT_YUVA420P9:
570 if (!avctx->bits_per_raw_sample)
571 s->bits_per_raw_sample = 9;
572 case AV_PIX_FMT_GRAY10:
573 case AV_PIX_FMT_YUV444P10:
574 case AV_PIX_FMT_YUV440P10:
575 case AV_PIX_FMT_YUV420P10:
576 case AV_PIX_FMT_YUV422P10:
577 case AV_PIX_FMT_YUVA444P10:
578 case AV_PIX_FMT_YUVA422P10:
579 case AV_PIX_FMT_YUVA420P10:
580
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
581 4 s->bits_per_raw_sample = 10;
582 case AV_PIX_FMT_GRAY12:
583 case AV_PIX_FMT_YUV444P12:
584 case AV_PIX_FMT_YUV440P12:
585 case AV_PIX_FMT_YUV420P12:
586 case AV_PIX_FMT_YUV422P12:
587 case AV_PIX_FMT_YUVA444P12:
588 case AV_PIX_FMT_YUVA422P12:
589
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
5 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
590 s->bits_per_raw_sample = 12;
591 case AV_PIX_FMT_GRAY14:
592 case AV_PIX_FMT_YUV444P14:
593 case AV_PIX_FMT_YUV420P14:
594 case AV_PIX_FMT_YUV422P14:
595
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
5 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
596 s->bits_per_raw_sample = 14;
597 5 s->packed_at_lsb = 1;
598 9 case AV_PIX_FMT_GRAY16:
599 case AV_PIX_FMT_YUV444P16:
600 case AV_PIX_FMT_YUV422P16:
601 case AV_PIX_FMT_YUV420P16:
602 case AV_PIX_FMT_YUVA444P16:
603 case AV_PIX_FMT_YUVA422P16:
604 case AV_PIX_FMT_YUVA420P16:
605
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
9 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
606 4 s->bits_per_raw_sample = 16;
607
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 } else if (!s->bits_per_raw_sample) {
608 1 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
609 }
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->bits_per_raw_sample <= 8) {
611 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
612 return AVERROR_INVALIDDATA;
613 }
614 9 s->version = FFMAX(s->version, 1);
615 31 case AV_PIX_FMT_GRAY8:
616 case AV_PIX_FMT_YA8:
617 case AV_PIX_FMT_YUV444P:
618 case AV_PIX_FMT_YUV440P:
619 case AV_PIX_FMT_YUV422P:
620 case AV_PIX_FMT_YUV420P:
621 case AV_PIX_FMT_YUV411P:
622 case AV_PIX_FMT_YUV410P:
623 case AV_PIX_FMT_YUVA444P:
624 case AV_PIX_FMT_YUVA422P:
625 case AV_PIX_FMT_YUVA420P:
626 31 s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
627 31 s->colorspace = 0;
628 31 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
629
4/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 8 times.
31 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
630 22 s->bits_per_raw_sample = 8;
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 else if (!s->bits_per_raw_sample)
632 s->bits_per_raw_sample = 8;
633 31 break;
634 case AV_PIX_FMT_RGB32:
635 s->colorspace = 1;
636 s->transparency = 1;
637 s->chroma_planes = 1;
638 s->bits_per_raw_sample = 8;
639 break;
640 case AV_PIX_FMT_RGBA64:
641 s->colorspace = 1;
642 s->transparency = 1;
643 s->chroma_planes = 1;
644 s->bits_per_raw_sample = 16;
645 s->use32bit = 1;
646 s->version = FFMAX(s->version, 1);
647 break;
648 4 case AV_PIX_FMT_RGB48:
649 4 s->colorspace = 1;
650 4 s->chroma_planes = 1;
651 4 s->bits_per_raw_sample = 16;
652 4 s->use32bit = 1;
653 4 s->version = FFMAX(s->version, 1);
654 4 break;
655 4 case AV_PIX_FMT_0RGB32:
656 4 s->colorspace = 1;
657 4 s->chroma_planes = 1;
658 4 s->bits_per_raw_sample = 8;
659 4 break;
660 case AV_PIX_FMT_GBRP9:
661 if (!avctx->bits_per_raw_sample)
662 s->bits_per_raw_sample = 9;
663 case AV_PIX_FMT_GBRP10:
664 case AV_PIX_FMT_GBRAP10:
665 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
666 s->bits_per_raw_sample = 10;
667 case AV_PIX_FMT_GBRP12:
668 case AV_PIX_FMT_GBRAP12:
669 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
670 s->bits_per_raw_sample = 12;
671 case AV_PIX_FMT_GBRP14:
672 case AV_PIX_FMT_GBRAP14:
673 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
674 s->bits_per_raw_sample = 14;
675 case AV_PIX_FMT_GBRP16:
676 case AV_PIX_FMT_GBRAP16:
677 if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
678 s->bits_per_raw_sample = 16;
679 else if (!s->bits_per_raw_sample)
680 s->bits_per_raw_sample = avctx->bits_per_raw_sample;
681 s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
682 s->colorspace = 1;
683 s->chroma_planes = 1;
684 if (s->bits_per_raw_sample >= 16) {
685 s->use32bit = 1;
686 }
687 s->version = FFMAX(s->version, 1);
688 break;
689 default:
690 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
691 return AVERROR(ENOSYS);
692 }
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 av_assert0(s->bits_per_raw_sample >= 8);
694
695
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 26 times.
39 if (s->bits_per_raw_sample > 8) {
696
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (s->ac == AC_GOLOMB_RICE) {
697 13 av_log(avctx, AV_LOG_INFO,
698 "bits_per_raw_sample > 8, forcing range coder\n");
699 13 s->ac = AC_RANGE_CUSTOM_TAB;
700 }
701 }
702
703
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18 times.
39 if (s->ac == AC_RANGE_CUSTOM_TAB) {
704
2/2
✓ Branch 0 taken 5355 times.
✓ Branch 1 taken 21 times.
5376 for (i = 1; i < 256; i++)
705 5355 s->state_transition[i] = ver2_state[i];
706 } else {
707 RangeCoder c;
708 18 ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
709
2/2
✓ Branch 0 taken 4590 times.
✓ Branch 1 taken 18 times.
4608 for (i = 1; i < 256; i++)
710 4590 s->state_transition[i] = c.one_state[i];
711 }
712
713
2/2
✓ Branch 0 taken 9984 times.
✓ Branch 1 taken 39 times.
10023 for (i = 0; i < 256; i++) {
714 9984 s->quant_table_count = 2;
715
2/2
✓ Branch 0 taken 6656 times.
✓ Branch 1 taken 3328 times.
9984 if (s->bits_per_raw_sample <= 8) {
716 6656 s->quant_tables[0][0][i]= quant11[i];
717 6656 s->quant_tables[0][1][i]= 11*quant11[i];
718 6656 s->quant_tables[0][2][i]= 11*11*quant11[i];
719 6656 s->quant_tables[1][0][i]= quant11[i];
720 6656 s->quant_tables[1][1][i]= 11*quant11[i];
721 6656 s->quant_tables[1][2][i]= 11*11*quant5 [i];
722 6656 s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
723 6656 s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
724 } else {
725 3328 s->quant_tables[0][0][i]= quant9_10bit[i];
726 3328 s->quant_tables[0][1][i]= 11*quant9_10bit[i];
727 3328 s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
728 3328 s->quant_tables[1][0][i]= quant9_10bit[i];
729 3328 s->quant_tables[1][1][i]= 11*quant9_10bit[i];
730 3328 s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
731 3328 s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
732 3328 s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
733 }
734 }
735 39 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
736 39 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
737 39 memcpy(s->quant_table, s->quant_tables[s->context_model],
738 sizeof(s->quant_table));
739
740
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 39 times.
156 for (i = 0; i < s->plane_count; i++) {
741 117 PlaneContext *const p = &s->plane[i];
742
743 117 memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
744 117 p->quant_table_index = s->context_model;
745 117 p->context_count = s->context_count[p->quant_table_index];
746 }
747
748
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
749 return ret;
750
751
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (!s->transparency)
752 39 s->plane_count = 2;
753
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
39 if (!s->chroma_planes && s->version > 3)
754 s->plane_count--;
755
756 39 ret = av_pix_fmt_get_chroma_sub_sample (avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret)
758 return ret;
759
760 39 s->picture_number = 0;
761
762
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31 times.
39 if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
763
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (i = 0; i < s->quant_table_count; i++) {
764 16 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
765 sizeof(*s->rc_stat2[i]));
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!s->rc_stat2[i])
767 return AVERROR(ENOMEM);
768 }
769 }
770
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 35 times.
39 if (avctx->stats_in) {
771 4 char *p = avctx->stats_in;
772 4 uint8_t (*best_state)[256] = av_malloc_array(256, 256);
773 4 int gob_count = 0;
774 char *next;
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!best_state)
776 return AVERROR(ENOMEM);
777
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 av_assert0(s->version >= 2);
779
780 for (;;) {
781
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
1028 for (j = 0; j < 256; j++)
782
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 1024 times.
3072 for (i = 0; i < 2; i++) {
783 2048 s->rc_stat[j][i] = strtol(p, &next, 0);
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
2048 if (next == p) {
785 av_log(avctx, AV_LOG_ERROR,
786 "2Pass file invalid at %d %d [%s]\n", j, i, p);
787 av_freep(&best_state);
788 return AVERROR_INVALIDDATA;
789 }
790 2048 p = next;
791 }
792
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < s->quant_table_count; i++)
793
2/2
✓ Branch 0 taken 32916 times.
✓ Branch 1 taken 8 times.
32924 for (j = 0; j < s->context_count[i]; j++) {
794
2/2
✓ Branch 0 taken 1053312 times.
✓ Branch 1 taken 32916 times.
1086228 for (k = 0; k < 32; k++)
795
2/2
✓ Branch 0 taken 2106624 times.
✓ Branch 1 taken 1053312 times.
3159936 for (m = 0; m < 2; m++) {
796 2106624 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2106624 times.
2106624 if (next == p) {
798 av_log(avctx, AV_LOG_ERROR,
799 "2Pass file invalid at %d %d %d %d [%s]\n",
800 i, j, k, m, p);
801 av_freep(&best_state);
802 return AVERROR_INVALIDDATA;
803 }
804 2106624 p = next;
805 }
806 }
807 4 gob_count = strtol(p, &next, 0);
808
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (next == p || gob_count <= 0) {
809 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
810 av_freep(&best_state);
811 return AVERROR_INVALIDDATA;
812 }
813 4 p = next;
814
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
8 while (*p == '\n' || *p == ' ')
815 4 p++;
816
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (p[0] == 0)
817 4 break;
818 }
819
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (s->ac == AC_RANGE_CUSTOM_TAB)
820 4 sort_stt(s, s->state_transition);
821
822 4 find_best_state(best_state, s->state_transition);
823
824
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < s->quant_table_count; i++) {
825
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 8 times.
264 for (k = 0; k < 32; k++) {
826 256 double a=0, b=0;
827 256 int jp = 0;
828
2/2
✓ Branch 0 taken 1053312 times.
✓ Branch 1 taken 256 times.
1053568 for (j = 0; j < s->context_count[i]; j++) {
829 1053312 double p = 128;
830
6/6
✓ Branch 0 taken 68511 times.
✓ Branch 1 taken 984801 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 68447 times.
✓ Branch 4 taken 48884 times.
✓ Branch 5 taken 935981 times.
1053312 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
831
2/2
✓ Branch 0 taken 117321 times.
✓ Branch 1 taken 10 times.
117331 if (a+b)
832 117321 p = 256.0 * b / (a + b);
833 117331 s->initial_states[i][jp][k] =
834 117331 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
835
2/2
✓ Branch 0 taken 539642 times.
✓ Branch 1 taken 117331 times.
656973 for(jp++; jp<j; jp++)
836 539642 s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
837 117331 a=b=0;
838 }
839 1053312 a += s->rc_stat2[i][j][k][0];
840 1053312 b += s->rc_stat2[i][j][k][1];
841
2/2
✓ Branch 0 taken 720364 times.
✓ Branch 1 taken 332948 times.
1053312 if (a+b) {
842 720364 p = 256.0 * b / (a + b);
843 }
844 1053312 s->initial_states[i][j][k] =
845 1053312 best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
846 }
847 }
848 }
849 4 av_freep(&best_state);
850 }
851
852
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 5 times.
39 if (s->version > 1) {
853 34 int plane_count = 1 + 2*s->chroma_planes + s->transparency;
854 34 int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
855 34 int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
856
5/6
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 4 times.
34 s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
857
858 34 s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
859
860
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 for (; s->num_v_slices < 32; s->num_v_slices++) {
861
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 4 times.
42 for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
862 38 int maxw = (avctx->width + s->num_h_slices - 1) / s->num_h_slices;
863 38 int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
864
2/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
865 continue;
866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
867 continue;
868
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 4 times.
38 if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES || !avctx->slices)
869 34 goto slices_ok;
870 }
871 }
872 av_log(avctx, AV_LOG_ERROR,
873 "Unsupported number %d of slices requested, please specify a "
874 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
875 avctx->slices);
876 return AVERROR(ENOSYS);
877 34 slices_ok:
878
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if ((ret = write_extradata(s)) < 0)
879 return ret;
880 }
881
882
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
883 return ret;
884 39 s->slice_count = s->max_slice_count;
885
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = ff_ffv1_init_slices_state(s)) < 0)
886 return ret;
887
888 #define STATS_OUT_SIZE 1024 * 1024 * 6
889
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 35 times.
39 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
890 4 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!avctx->stats_out)
892 return AVERROR(ENOMEM);
893
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < s->quant_table_count; i++)
894
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 for (j = 0; j < s->max_slice_count; j++) {
895 32 FFV1Context *sf = s->slice_context[j];
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 av_assert0(!sf->rc_stat2[i]);
897 32 sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
898 sizeof(*sf->rc_stat2[i]));
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!sf->rc_stat2[i])
900 return AVERROR(ENOMEM);
901 }
902 }
903
904 39 return 0;
905 }
906
907 6520 static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
908 {
909 6520 RangeCoder *c = &fs->c;
910 uint8_t state[CONTEXT_SIZE];
911 int j;
912 6520 memset(state, 128, sizeof(state));
913
914 6520 put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
915 6520 put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
916 6520 put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
917 6520 put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
918
2/2
✓ Branch 0 taken 13040 times.
✓ Branch 1 taken 6520 times.
19560 for (j=0; j<f->plane_count; j++) {
919 13040 put_symbol(c, state, f->plane[j].quant_table_index, 0);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13040 times.
13040 av_assert0(f->plane[j].quant_table_index == f->context_model);
921 }
922
1/2
✓ Branch 0 taken 6520 times.
✗ Branch 1 not taken.
6520 if (!(f->cur_enc_frame->flags & AV_FRAME_FLAG_INTERLACED))
923 6520 put_symbol(c, state, 3, 0);
924 else
925 put_symbol(c, state, 1 + !(f->cur_enc_frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST), 0);
926 6520 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.num, 0);
927 6520 put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.den, 0);
928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6520 times.
6520 if (f->version > 3) {
929 put_rac(c, state, fs->slice_coding_mode == 1);
930 if (fs->slice_coding_mode == 1)
931 ff_ffv1_clear_slice_state(f, fs);
932 put_symbol(c, state, fs->slice_coding_mode, 0);
933 if (fs->slice_coding_mode != 1) {
934 put_symbol(c, state, fs->slice_rct_by_coef, 0);
935 put_symbol(c, state, fs->slice_rct_ry_coef, 0);
936 }
937 }
938 6520 }
939
940 static void choose_rct_params(FFV1Context *fs, const uint8_t *src[3], const int stride[3], int w, int h)
941 {
942 #define NB_Y_COEFF 15
943 static const int rct_y_coeff[15][2] = {
944 {0, 0}, // 4G
945 {1, 1}, // R + 2G + B
946 {2, 2}, // 2R + 2B
947 {0, 2}, // 2G + 2B
948 {2, 0}, // 2R + 2G
949 {4, 0}, // 4R
950 {0, 4}, // 4B
951
952 {0, 3}, // 1G + 3B
953 {3, 0}, // 3R + 1G
954 {3, 1}, // 3R + B
955 {1, 3}, // R + 3B
956 {1, 2}, // R + G + 2B
957 {2, 1}, // 2R + G + B
958 {0, 1}, // 3G + B
959 {1, 0}, // R + 3G
960 };
961
962 int stat[NB_Y_COEFF] = {0};
963 int x, y, i, p, best;
964 int16_t *sample[3];
965 int lbd = fs->bits_per_raw_sample <= 8;
966
967 for (y = 0; y < h; y++) {
968 int lastr=0, lastg=0, lastb=0;
969 for (p = 0; p < 3; p++)
970 sample[p] = fs->sample_buffer + p*w;
971
972 for (x = 0; x < w; x++) {
973 int b, g, r;
974 int ab, ag, ar;
975 if (lbd) {
976 unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
977 b = v & 0xFF;
978 g = (v >> 8) & 0xFF;
979 r = (v >> 16) & 0xFF;
980 } else {
981 b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
982 g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
983 r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
984 }
985
986 ar = r - lastr;
987 ag = g - lastg;
988 ab = b - lastb;
989 if (x && y) {
990 int bg = ag - sample[0][x];
991 int bb = ab - sample[1][x];
992 int br = ar - sample[2][x];
993
994 br -= bg;
995 bb -= bg;
996
997 for (i = 0; i<NB_Y_COEFF; i++) {
998 stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
999 }
1000
1001 }
1002 sample[0][x] = ag;
1003 sample[1][x] = ab;
1004 sample[2][x] = ar;
1005
1006 lastr = r;
1007 lastg = g;
1008 lastb = b;
1009 }
1010 }
1011
1012 best = 0;
1013 for (i=1; i<NB_Y_COEFF; i++) {
1014 if (stat[i] < stat[best])
1015 best = i;
1016 }
1017
1018 fs->slice_rct_by_coef = rct_y_coeff[best][1];
1019 fs->slice_rct_ry_coef = rct_y_coeff[best][0];
1020 }
1021
1022 6770 static int encode_slice(AVCodecContext *c, void *arg)
1023 {
1024 6770 FFV1Context *fs = *(void **)arg;
1025 6770 FFV1Context *f = fs->avctx->priv_data;
1026 6770 int width = fs->slice_width;
1027 6770 int height = fs->slice_height;
1028 6770 int x = fs->slice_x;
1029 6770 int y = fs->slice_y;
1030 6770 const AVFrame *const p = f->cur_enc_frame;
1031 6770 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1032 int ret;
1033 6770 RangeCoder c_bak = fs->c;
1034 27080 const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1035
2/2
✓ Branch 0 taken 5170 times.
✓ Branch 1 taken 1600 times.
6770 p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1036
2/2
✓ Branch 0 taken 5170 times.
✓ Branch 1 taken 1600 times.
6770 p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6770 times.
6770 p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1038
1039 6770 fs->slice_coding_mode = 0;
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6770 times.
6770 if (f->version > 3) {
1041 choose_rct_params(fs, planes, p->linesize, width, height);
1042 } else {
1043 6770 fs->slice_rct_by_coef = 1;
1044 6770 fs->slice_rct_ry_coef = 1;
1045 }
1046
1047 6770 retry:
1048
2/2
✓ Branch 0 taken 681 times.
✓ Branch 1 taken 6089 times.
6770 if (f->key_frame)
1049 681 ff_ffv1_clear_slice_state(f, fs);
1050
2/2
✓ Branch 0 taken 6520 times.
✓ Branch 1 taken 250 times.
6770 if (f->version > 2) {
1051 6520 encode_slice_header(f, fs);
1052 }
1053
2/2
✓ Branch 0 taken 2750 times.
✓ Branch 1 taken 4020 times.
6770 if (fs->ac == AC_GOLOMB_RICE) {
1054
4/6
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 2500 times.
✓ Branch 2 taken 250 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 250 times.
✗ Branch 5 not taken.
2750 fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&fs->c, f->version > 2) : 0;
1055 2750 init_put_bits(&fs->pb,
1056 2750 fs->c.bytestream_start + fs->ac_byte_count,
1057 2750 fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
1058 }
1059
1060
3/4
✓ Branch 0 taken 5170 times.
✓ Branch 1 taken 1600 times.
✓ Branch 2 taken 5170 times.
✗ Branch 3 not taken.
11940 if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8) {
1061 5170 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
1062 5170 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1063 5170 const int cx = x >> f->chroma_h_shift;
1064 5170 const int cy = y >> f->chroma_v_shift;
1065
1066 5170 ret = encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
1067
1068
1/2
✓ Branch 0 taken 5170 times.
✗ Branch 1 not taken.
5170 if (f->chroma_planes) {
1069 5170 ret |= encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
1070 5170 ret |= encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
1071 }
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5170 times.
5170 if (fs->transparency)
1073 ret |= encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1);
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
1075 ret = encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
1076 ret |= encode_plane(fs, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
1077
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 } else if (f->use32bit) {
1078 800 ret = encode_rgb_frame32(fs, planes, width, height, p->linesize);
1079 } else {
1080 800 ret = encode_rgb_frame(fs, planes, width, height, p->linesize);
1081 }
1082
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6770 times.
6770 if (ret < 0) {
1084 av_assert0(fs->slice_coding_mode == 0);
1085 if (fs->version < 4 || !fs->ac) {
1086 av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1087 return ret;
1088 }
1089 av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1090 fs->slice_coding_mode = 1;
1091 fs->c = c_bak;
1092 goto retry;
1093 }
1094
1095 6770 return 0;
1096 }
1097
1098 1919 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1099 const AVFrame *pict, int *got_packet)
1100 {
1101 1919 FFV1Context *f = avctx->priv_data;
1102 1919 RangeCoder *const c = &f->slice_context[0]->c;
1103 1919 uint8_t keystate = 128;
1104 uint8_t *buf_p;
1105 int i, ret;
1106 1919 int64_t maxsize = FF_INPUT_BUFFER_MIN_SIZE
1107 1919 + avctx->width*avctx->height*37LL*4;
1108
1109
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1880 times.
1919 if(!pict) {
1110
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 35 times.
39 if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1111 int j, k, m;
1112 4 char *p = avctx->stats_out;
1113 4 char *end = p + STATS_OUT_SIZE;
1114
1115 4 memset(f->rc_stat, 0, sizeof(f->rc_stat));
1116
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < f->quant_table_count; i++)
1117 8 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1118
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 av_assert0(f->slice_count == f->max_slice_count);
1120
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (j = 0; j < f->slice_count; j++) {
1121 16 FFV1Context *fs = f->slice_context[j];
1122
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 16 times.
4112 for (i = 0; i < 256; i++) {
1123 4096 f->rc_stat[i][0] += fs->rc_stat[i][0];
1124 4096 f->rc_stat[i][1] += fs->rc_stat[i][1];
1125 }
1126
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (i = 0; i < f->quant_table_count; i++) {
1127
2/2
✓ Branch 0 taken 131664 times.
✓ Branch 1 taken 32 times.
131696 for (k = 0; k < f->context_count[i]; k++)
1128
2/2
✓ Branch 0 taken 4213248 times.
✓ Branch 1 taken 131664 times.
4344912 for (m = 0; m < 32; m++) {
1129 4213248 f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
1130 4213248 f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
1131 }
1132 }
1133 }
1134
1135
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
1028 for (j = 0; j < 256; j++) {
1136 1024 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1137 f->rc_stat[j][0], f->rc_stat[j][1]);
1138 1024 p += strlen(p);
1139 }
1140 4 snprintf(p, end - p, "\n");
1141
1142
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (i = 0; i < f->quant_table_count; i++) {
1143
2/2
✓ Branch 0 taken 32916 times.
✓ Branch 1 taken 8 times.
32924 for (j = 0; j < f->context_count[i]; j++)
1144
2/2
✓ Branch 0 taken 1053312 times.
✓ Branch 1 taken 32916 times.
1086228 for (m = 0; m < 32; m++) {
1145 1053312 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1146 1053312 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1147 1053312 p += strlen(p);
1148 }
1149 }
1150 4 snprintf(p, end - p, "%d\n", f->gob_count);
1151 }
1152 39 return 0;
1153 }
1154
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1880 times.
1880 if (f->version > 3)
1156 maxsize = FF_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
1157
1158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1880 times.
1880 if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1159 av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1160 maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
1161 }
1162
1163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1880 times.
1880 if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
1164 return ret;
1165
1166 1880 ff_init_range_encoder(c, pkt->data, pkt->size);
1167 1880 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1168
1169 1880 f->cur_enc_frame = pict;
1170
1171
3/4
✓ Branch 0 taken 1880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 1691 times.
1880 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
1172 189 put_rac(c, &keystate, 1);
1173 189 f->key_frame = 1;
1174 189 f->gob_count++;
1175 189 write_header(f);
1176 } else {
1177 1691 put_rac(c, &keystate, 0);
1178 1691 f->key_frame = 0;
1179 }
1180
1181
2/2
✓ Branch 0 taken 1005 times.
✓ Branch 1 taken 875 times.
1880 if (f->ac == AC_RANGE_CUSTOM_TAB) {
1182 int i;
1183
2/2
✓ Branch 0 taken 256275 times.
✓ Branch 1 taken 1005 times.
257280 for (i = 1; i < 256; i++) {
1184 256275 c->one_state[i] = f->state_transition[i];
1185 256275 c->zero_state[256 - i] = 256 - c->one_state[i];
1186 }
1187 }
1188
1189
2/2
✓ Branch 0 taken 6770 times.
✓ Branch 1 taken 1880 times.
8650 for (i = 0; i < f->slice_count; i++) {
1190 6770 FFV1Context *fs = f->slice_context[i];
1191 6770 uint8_t *start = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1192 6770 int len = pkt->size / f->slice_count;
1193
2/2
✓ Branch 0 taken 4890 times.
✓ Branch 1 taken 1880 times.
6770 if (i) {
1194 4890 ff_init_range_encoder(&fs->c, start, len);
1195 } else {
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1880 times.
1880 av_assert0(fs->c.bytestream_end >= fs->c.bytestream_start + len);
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1880 times.
1880 av_assert0(fs->c.bytestream < fs->c.bytestream_start + len);
1198 1880 fs->c.bytestream_end = fs->c.bytestream_start + len;
1199 }
1200 }
1201 1880 avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
1202 f->slice_count, sizeof(void *));
1203
1204 1880 buf_p = pkt->data;
1205
2/2
✓ Branch 0 taken 6770 times.
✓ Branch 1 taken 1880 times.
8650 for (i = 0; i < f->slice_count; i++) {
1206 6770 FFV1Context *fs = f->slice_context[i];
1207 int bytes;
1208
1209
2/2
✓ Branch 0 taken 4020 times.
✓ Branch 1 taken 2750 times.
6770 if (fs->ac != AC_GOLOMB_RICE) {
1210 4020 bytes = ff_rac_terminate(&fs->c, 1);
1211 } else {
1212 2750 flush_put_bits(&fs->pb); // FIXME: nicer padding
1213 2750 bytes = fs->ac_byte_count + put_bytes_output(&fs->pb);
1214 }
1215
4/4
✓ Branch 0 taken 1880 times.
✓ Branch 1 taken 4890 times.
✓ Branch 2 taken 1630 times.
✓ Branch 3 taken 250 times.
6770 if (i > 0 || f->version > 2) {
1216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6520 times.
6520 av_assert0(bytes < pkt->size / f->slice_count);
1217 6520 memmove(buf_p, fs->c.bytestream_start, bytes);
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6520 times.
6520 av_assert0(bytes < (1 << 24));
1219 6520 AV_WB24(buf_p + bytes, bytes);
1220 6520 bytes += 3;
1221 }
1222
2/2
✓ Branch 0 taken 6520 times.
✓ Branch 1 taken 250 times.
6770 if (f->ec) {
1223 unsigned v;
1224 6520 buf_p[bytes++] = 0;
1225 6520 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
1226 6520 AV_WL32(buf_p + bytes, v);
1227 6520 bytes += 4;
1228 }
1229 6770 buf_p += bytes;
1230 }
1231
1232
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1680 times.
1880 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1233 200 avctx->stats_out[0] = '\0';
1234
1235 1880 f->picture_number++;
1236 1880 pkt->size = buf_p - pkt->data;
1237 1880 pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1238 1880 *got_packet = 1;
1239
1240 1880 return 0;
1241 }
1242
1243 #define OFFSET(x) offsetof(FFV1Context, x)
1244 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1245 static const AVOption options[] = {
1246 { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
1247 { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1248 { .i64 = 0 }, -2, 2, VE, .unit = "coder" },
1249 { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1250 { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1251 { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1252 { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1253 { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1254 { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1255 { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
1256 { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1257 { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1258 { .i64 = 0 }, 0, 1, VE },
1259
1260 { NULL }
1261 };
1262
1263 static const AVClass ffv1_class = {
1264 .class_name = "ffv1 encoder",
1265 .item_name = av_default_item_name,
1266 .option = options,
1267 .version = LIBAVUTIL_VERSION_INT,
1268 };
1269
1270 const FFCodec ff_ffv1_encoder = {
1271 .p.name = "ffv1",
1272 CODEC_LONG_NAME("FFmpeg video codec #1"),
1273 .p.type = AVMEDIA_TYPE_VIDEO,
1274 .p.id = AV_CODEC_ID_FFV1,
1275 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1276 AV_CODEC_CAP_SLICE_THREADS |
1277 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1278 .priv_data_size = sizeof(FFV1Context),
1279 .init = encode_init,
1280 FF_CODEC_ENCODE_CB(encode_frame),
1281 .close = ff_ffv1_close,
1282 .p.pix_fmts = (const enum AVPixelFormat[]) {
1283 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
1284 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
1285 AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
1286 AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
1287 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1288 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1289 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
1290 AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12,
1291 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
1292 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
1293 AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
1294 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRAP14,
1295 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
1296 AV_PIX_FMT_YA8,
1297 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
1298 AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48,
1299 AV_PIX_FMT_GBRAP16, AV_PIX_FMT_RGBA64,
1300 AV_PIX_FMT_GRAY9,
1301 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
1302 AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
1303 AV_PIX_FMT_NONE
1304
1305 },
1306 .p.priv_class = &ffv1_class,
1307 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,
1308 };
1309