Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/ffv1enc.c
Line
Count
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
#include "libavutil/qsort.h"
35
36
#include "avcodec.h"
37
#include "encode.h"
38
#include "codec_internal.h"
39
#include "put_bits.h"
40
#include "put_golomb.h"
41
#include "rangecoder.h"
42
#include "ffv1.h"
43
#include "ffv1enc.h"
44
45
static const int8_t quant5_10bit[256] = {
46
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,
47
     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
48
     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
49
     1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
50
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
51
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
52
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
53
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
54
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
55
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
56
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
57
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
58
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
59
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61
    -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
62
};
63
64
static const int8_t quant5[256] = {
65
     0,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
66
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
67
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
68
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
69
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
70
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
71
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
72
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
73
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
74
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
75
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
76
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
77
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
78
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
79
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
80
    -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
81
};
82
83
static const int8_t quant9_10bit[256] = {
84
     0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,
85
     2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,
86
     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,
87
     3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,
88
     4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
89
     4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
90
     4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
91
     4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
92
    -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
93
    -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
94
    -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
95
    -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
96
    -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
97
    -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
98
    -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
99
    -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
100
};
101
102
static const int8_t quant11[256] = {
103
     0,  1,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,
104
     4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
105
     4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
106
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
107
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
108
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
109
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
110
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
111
    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
112
    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
113
    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
114
    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
115
    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
116
    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
117
    -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
118
    -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
119
};
120
121
static const uint8_t ver2_state[256] = {
122
      0,  10,  10,  10,  10,  16,  16,  16, 28,   16,  16,  29,  42,  49,  20,  49,
123
     59,  25,  26,  26,  27,  31,  33,  33, 33,   34,  34,  37,  67,  38,  39,  39,
124
     40,  40,  41,  79,  43,  44,  45,  45, 48,   48,  64,  50,  51,  52,  88,  52,
125
     53,  74,  55,  57,  58,  58,  74,  60, 101,  61,  62,  84,  66,  66,  68,  69,
126
     87,  82,  71,  97,  73,  73,  82,  75, 111,  77,  94,  78,  87,  81,  83,  97,
127
     85,  83,  94,  86,  99,  89,  90,  99, 111,  92,  93,  134, 95,  98, 105,  98,
128
    105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
129
    115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
130
    165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
131
    147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
132
    172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
133
    175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
134
    197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
135
    209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
136
    226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
137
    241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
138
};
139
140
static void find_best_state(uint8_t best_state[256][256],
141
                            const uint8_t one_state[256])
142
0
{
143
0
    int i, j, k, m;
144
0
    uint32_t l2tab[256];
145
146
0
    for (i = 1; i < 256; i++)
147
0
        l2tab[i] = -log2(i / 256.0) * ((1U << 31) / 8);
148
149
0
    for (i = 0; i < 256; i++) {
150
0
        uint64_t best_len[256];
151
152
0
        for (j = 0; j < 256; j++)
153
0
            best_len[j] = UINT64_MAX;
154
155
0
        for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
156
0
            uint32_t occ[256] = { 0 };
157
0
            uint64_t len      = 0;
158
0
            occ[j] = UINT32_MAX;
159
160
0
            if (!one_state[j])
161
0
                continue;
162
163
0
            for (k = 0; k < 256; k++) {
164
0
                uint32_t newocc[256] = { 0 };
165
0
                for (m = 1; m < 256; m++)
166
0
                    if (occ[m]) {
167
0
                        len += (occ[m]*((       i *(uint64_t)l2tab[    m]
168
0
                                         + (256-i)*(uint64_t)l2tab[256-m])>>8)) >> 8;
169
0
                    }
170
0
                if (len < best_len[k]) {
171
0
                    best_len[k]      = len;
172
0
                    best_state[i][k] = j;
173
0
                }
174
0
                for (m = 1; m < 256; m++)
175
0
                    if (occ[m]) {
176
0
                        newocc[      one_state[      m]] += occ[m] * (uint64_t)       i  >> 8;
177
0
                        newocc[256 - one_state[256 - m]] += occ[m] * (uint64_t)(256 - i) >> 8;
178
0
                    }
179
0
                memcpy(occ, newocc, sizeof(occ));
180
0
            }
181
0
        }
182
0
    }
183
0
}
184
185
static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
186
                                                          uint8_t *state, int v,
187
                                                          int is_signed,
188
                                                          uint64_t rc_stat[256][2],
189
                                                          uint64_t rc_stat2[32][2])
190
536M
{
191
536M
    int i;
192
193
536M
#define put_rac(C, S, B)                        \
194
1.85G
    do {                                        \
195
1.85G
        if (rc_stat) {                          \
196
0
            rc_stat[*(S)][B]++;                 \
197
0
            rc_stat2[(S) - state][B]++;         \
198
0
        }                                       \
199
1.85G
        put_rac(C, S, B);                       \
200
1.85G
    } while (0)
201
202
536M
    if (v) {
203
84.8M
        const unsigned a = is_signed ? FFABS(v) : v;
204
84.8M
        const int e = av_log2(a);
205
84.8M
        put_rac(c, state + 0, 0);
206
84.8M
        if (e <= 9) {
207
336M
            for (i = 0; i < e; i++)
208
273M
                put_rac(c, state + 1 + i, 1);  // 1..10
209
62.1M
            put_rac(c, state + 1 + i, 0);
210
211
336M
            for (i = e - 1; i >= 0; i--)
212
273M
                put_rac(c, state + 22 + i, (a >> i) & 1);  // 22..31
213
214
62.1M
            if (is_signed)
215
39.4M
                put_rac(c, state + 11 + e, v < 0);  // 11..21
216
62.1M
        } else {
217
340M
            for (i = 0; i < e; i++)
218
317M
                put_rac(c, state + 1 + FFMIN(i, 9), 1);  // 1..10
219
22.6M
            put_rac(c, state + 1 + 9, 0);
220
221
340M
            for (i = e - 1; i >= 0; i--)
222
317M
                put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1);  // 22..31
223
224
22.6M
            if (is_signed)
225
8.50M
                put_rac(c, state + 11 + 10, v < 0);  // 11..21
226
22.6M
        }
227
451M
    } else {
228
451M
        put_rac(c, state + 0, 1);
229
451M
    }
230
536M
#undef put_rac
231
536M
}
232
233
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
234
                                   int v, int is_signed)
235
2.75M
{
236
2.75M
    put_symbol_inline(c, state, v, is_signed, NULL, NULL);
237
2.75M
}
238
239
240
static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
241
                                  int v, int bits)
242
21.2M
{
243
21.2M
    int i, k, code;
244
21.2M
    v = fold(v - state->bias, bits);
245
246
21.2M
    i = state->count;
247
21.2M
    k = 0;
248
120M
    while (i < state->error_sum) { // FIXME: optimize
249
99.5M
        k++;
250
99.5M
        i += i;
251
99.5M
    }
252
253
21.2M
    av_assert2(k <= 16);
254
255
21.2M
    code = v ^ ((2 * state->drift + state->count) >> 31);
256
257
21.2M
    ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
258
21.2M
            state->bias, state->error_sum, state->drift, state->count, k);
259
21.2M
    set_sr_golomb(pb, code, k, 12, bits);
260
261
21.2M
    update_vlc_state(state, v);
262
21.2M
}
263
264
5.02k
#define TYPE int16_t
265
443M
#define RENAME(name) name
266
#include "ffv1enc_template.c"
267
#undef TYPE
268
#undef RENAME
269
270
13.9k
#define TYPE int32_t
271
55.8k
#define RENAME(name) name ## 32
272
#include "ffv1enc_template.c"
273
274
static int encode_plane(FFV1Context *f, FFV1SliceContext *sc,
275
                        const uint8_t *src, int w, int h,
276
                        int stride, int plane_index, int remap_index, int pixel_stride, int ac)
277
119k
{
278
119k
    int x, y, i, ret;
279
119k
    const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
280
119k
    const int ring_size = f->context_model ? 3 : 2;
281
119k
    int16_t *sample[3];
282
119k
    sc->run_index = 0;
283
284
119k
    sample[2] = sc->sample_buffer; // dummy to avoid UB pointer arithmetic
285
286
119k
    memset(sc->sample_buffer, 0, ring_size * (w + 6) * sizeof(*sc->sample_buffer));
287
288
6.46M
    for (y = 0; y < h; y++) {
289
21.1M
        for (i = 0; i < ring_size; i++)
290
14.8M
            sample[i] = sc->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
291
292
6.34M
        sample[0][-1]= sample[1][0  ];
293
6.34M
        sample[1][ w]= sample[1][w-1];
294
295
6.34M
        if (f->bits_per_raw_sample <= 8) {
296
37.3M
            for (x = 0; x < w; x++)
297
33.2M
                sample[0][x] = src[x * pixel_stride + stride * y];
298
4.02M
            if (sc->remap)
299
0
                for (x = 0; x < w; x++)
300
0
                    sample[0][x] = sc->fltmap[remap_index][ sample[0][x] ];
301
302
4.02M
            if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, 8, ac, pass1)) < 0)
303
0
                return ret;
304
4.02M
        } else {
305
2.32M
            if (f->packed_at_lsb) {
306
20.4M
                for (x = 0; x < w; x++) {
307
19.2M
                    sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride];
308
19.2M
                }
309
1.18M
            } else {
310
41.0M
                for (x = 0; x < w; x++) {
311
39.9M
                    sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample);
312
39.9M
                }
313
1.13M
            }
314
2.32M
            if (sc->remap)
315
29.8M
                for (x = 0; x < w; x++)
316
28.7M
                    sample[0][x] = sc->fltmap[remap_index][ (uint16_t)sample[0][x] ];
317
318
2.32M
            if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, f->bits_per_raw_sample, ac, pass1)) < 0)
319
451
                return ret;
320
2.32M
        }
321
6.34M
    }
322
118k
    return 0;
323
119k
}
324
325
static void load_plane(FFV1Context *f, FFV1SliceContext *sc,
326
                      const uint8_t *src, int w, int h,
327
                      int stride, int remap_index, int pixel_stride)
328
57.0k
{
329
57.0k
    int x, y;
330
331
57.0k
    memset(sc->fltmap[remap_index], 0, 65536 * sizeof(*sc->fltmap[remap_index]));
332
333
1.14M
    for (y = 0; y < h; y++) {
334
1.08M
        if (f->bits_per_raw_sample <= 8) {
335
0
            for (x = 0; x < w; x++)
336
0
                sc->fltmap[remap_index][ src[x * pixel_stride + stride * y] ] = 1;
337
1.08M
        } else {
338
1.08M
            if (f->packed_at_lsb) {
339
0
                for (x = 0; x < w; x++)
340
0
                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] ] = 1;
341
1.08M
            } else {
342
33.6M
                for (x = 0; x < w; x++)
343
32.5M
                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample) ] = 1;
344
1.08M
            }
345
1.08M
        }
346
1.08M
    }
347
57.0k
}
348
349
static void write_quant_table(RangeCoder *c, int16_t *quant_table)
350
48.2k
{
351
48.2k
    int last = 0;
352
48.2k
    int i;
353
48.2k
    uint8_t state[CONTEXT_SIZE];
354
48.2k
    memset(state, 128, sizeof(state));
355
356
6.17M
    for (i = 1; i < MAX_QUANT_TABLE_SIZE/2; i++)
357
6.13M
        if (quant_table[i] != quant_table[i - 1]) {
358
128k
            put_symbol(c, state, i - last - 1, 0);
359
128k
            last = i;
360
128k
        }
361
48.2k
    put_symbol(c, state, i - last - 1, 0);
362
48.2k
}
363
364
static void write_quant_tables(RangeCoder *c,
365
                               int16_t quant_table[MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE])
366
9.65k
{
367
9.65k
    int i;
368
57.9k
    for (i = 0; i < 5; i++)
369
48.2k
        write_quant_table(c, quant_table[i]);
370
9.65k
}
371
372
static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
373
                            int nb_contexts)
374
9.65k
{
375
9.65k
    if (!initial_state)
376
0
        return 0;
377
27.7M
    for (int i = 0; i < nb_contexts; i++)
378
916M
        for (int j = 0; j < CONTEXT_SIZE; j++)
379
888M
            if (initial_state[i][j] != 128)
380
0
                return 1;
381
9.65k
    return 0;
382
9.65k
}
383
384
static void write_header(FFV1Context *f)
385
6.09k
{
386
6.09k
    uint8_t state[CONTEXT_SIZE];
387
6.09k
    int i, j;
388
6.09k
    RangeCoder *const c = &f->slices[0].c;
389
390
6.09k
    memset(state, 128, sizeof(state));
391
392
6.09k
    if (f->version < 2) {
393
0
        put_symbol(c, state, f->version, 0);
394
0
        put_symbol(c, state, f->ac, 0);
395
0
        if (f->ac == AC_RANGE_CUSTOM_TAB) {
396
0
            for (i = 1; i < 256; i++)
397
0
                put_symbol(c, state,
398
0
                           f->state_transition[i] - c->one_state[i], 1);
399
0
        }
400
0
        put_symbol(c, state, f->colorspace, 0); //YUV cs type
401
0
        if (f->version > 0)
402
0
            put_symbol(c, state, f->bits_per_raw_sample, 0);
403
0
        put_rac(c, state, f->chroma_planes);
404
0
        put_symbol(c, state, f->chroma_h_shift, 0);
405
0
        put_symbol(c, state, f->chroma_v_shift, 0);
406
0
        put_rac(c, state, f->transparency);
407
408
0
        write_quant_tables(c, f->quant_tables[f->context_model]);
409
6.09k
    } else if (f->version < 3) {
410
0
        put_symbol(c, state, f->slice_count, 0);
411
0
        for (i = 0; i < f->slice_count; i++) {
412
0
            FFV1SliceContext *fs = &f->slices[i];
413
0
            put_symbol(c, state,
414
0
                       (fs->slice_x      + 1) * f->num_h_slices / f->width, 0);
415
0
            put_symbol(c, state,
416
0
                       (fs->slice_y      + 1) * f->num_v_slices / f->height, 0);
417
0
            put_symbol(c, state,
418
0
                       (fs->slice_width  + 1) * f->num_h_slices / f->width - 1,
419
0
                       0);
420
0
            put_symbol(c, state,
421
0
                       (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
422
0
                       0);
423
0
            for (j = 0; j < f->plane_count; j++) {
424
0
                put_symbol(c, state, fs->plane[j].quant_table_index, 0);
425
0
                av_assert0(fs->plane[j].quant_table_index == f->context_model);
426
0
            }
427
0
        }
428
0
    }
429
6.09k
}
430
431
static void set_micro_version(FFV1Context *f)
432
4.84k
{
433
4.84k
    f->combined_version = f->version << 16;
434
4.84k
    if (f->version > 2) {
435
4.84k
        if (f->version == 3) {
436
1.19k
            f->micro_version = 4;
437
3.64k
        } else if (f->version == 4) {
438
3.64k
            f->micro_version = 9;
439
3.64k
        } else
440
0
            av_assert0(0);
441
442
4.84k
        f->combined_version += f->micro_version;
443
4.84k
    } else
444
0
        av_assert0(f->micro_version == 0);
445
4.84k
}
446
447
av_cold int ff_ffv1_write_extradata(AVCodecContext *avctx)
448
4.82k
{
449
4.82k
    FFV1Context *f = avctx->priv_data;
450
451
4.82k
    RangeCoder c;
452
4.82k
    uint8_t state[CONTEXT_SIZE];
453
4.82k
    int i, j, k;
454
4.82k
    uint8_t state2[32][CONTEXT_SIZE];
455
4.82k
    unsigned v;
456
457
4.82k
    memset(state2, 128, sizeof(state2));
458
4.82k
    memset(state, 128, sizeof(state));
459
460
4.82k
    f->avctx->extradata_size = 10000 + 4 +
461
4.82k
                                    (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
462
4.82k
    f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
463
4.82k
    if (!f->avctx->extradata)
464
0
        return AVERROR(ENOMEM);
465
4.82k
    ff_init_range_encoder(&c, f->avctx->extradata, f->avctx->extradata_size);
466
4.82k
    ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
467
468
4.82k
    put_symbol(&c, state, f->version, 0);
469
4.82k
    if (f->version > 2)
470
4.82k
        put_symbol(&c, state, f->micro_version, 0);
471
472
4.82k
    put_symbol(&c, state, f->ac, 0);
473
4.82k
    if (f->ac == AC_RANGE_CUSTOM_TAB)
474
829k
        for (i = 1; i < 256; i++)
475
826k
            put_symbol(&c, state, f->state_transition[i] - c.one_state[i], 1);
476
477
4.82k
    put_symbol(&c, state, f->colorspace, 0); // YUV cs type
478
4.82k
    put_symbol(&c, state, f->bits_per_raw_sample, 0);
479
4.82k
    put_rac(&c, state, f->chroma_planes);
480
4.82k
    put_symbol(&c, state, f->chroma_h_shift, 0);
481
4.82k
    put_symbol(&c, state, f->chroma_v_shift, 0);
482
4.82k
    put_rac(&c, state, f->transparency);
483
4.82k
    put_symbol(&c, state, f->num_h_slices - 1, 0);
484
4.82k
    put_symbol(&c, state, f->num_v_slices - 1, 0);
485
486
4.82k
    put_symbol(&c, state, f->quant_table_count, 0);
487
14.4k
    for (i = 0; i < f->quant_table_count; i++)
488
9.65k
        write_quant_tables(&c, f->quant_tables[i]);
489
490
14.4k
    for (i = 0; i < f->quant_table_count; i++) {
491
9.65k
        if (contains_non_128(f->initial_states[i], f->context_count[i])) {
492
0
            put_rac(&c, state, 1);
493
0
            for (j = 0; j < f->context_count[i]; j++)
494
0
                for (k = 0; k < CONTEXT_SIZE; k++) {
495
0
                    int pred = j ? f->initial_states[i][j - 1][k] : 128;
496
0
                    put_symbol(&c, state2[k],
497
0
                               (int8_t)(f->initial_states[i][j][k] - pred), 1);
498
0
                }
499
9.65k
        } else {
500
9.65k
            put_rac(&c, state, 0);
501
9.65k
        }
502
9.65k
    }
503
504
4.82k
    if (f->version > 2) {
505
4.82k
        put_symbol(&c, state, f->ec, 0);
506
4.82k
        put_symbol(&c, state, f->intra = (f->avctx->gop_size < 2), 0);
507
4.82k
    }
508
509
4.82k
    f->avctx->extradata_size = ff_rac_terminate(&c, 0);
510
4.82k
    v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, f->avctx->extradata, f->avctx->extradata_size) ^ (f->crcref ? 0x8CD88196 : 0);
511
4.82k
    AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
512
4.82k
    f->avctx->extradata_size += 4;
513
514
4.82k
    return 0;
515
4.82k
}
516
517
static int sort_stt(FFV1Context *s, uint8_t stt[256])
518
0
{
519
0
    int i, i2, changed, print = 0;
520
521
0
    do {
522
0
        changed = 0;
523
0
        for (i = 12; i < 244; i++) {
524
0
            for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
525
526
0
#define COST(old, new)                                      \
527
0
    s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) +     \
528
0
    s->rc_stat[old][1] * -log2((new)         / 256.0)
529
530
0
#define COST2(old, new)                         \
531
0
    COST(old, new) + COST(256 - (old), 256 - (new))
532
533
0
                double size0 = COST2(i,  i) + COST2(i2, i2);
534
0
                double sizeX = COST2(i, i2) + COST2(i2, i);
535
0
                if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
536
0
                    int j;
537
0
                    FFSWAP(int, stt[i], stt[i2]);
538
0
                    FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
539
0
                    FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
540
0
                    if (i != 256 - i2) {
541
0
                        FFSWAP(int, stt[256 - i], stt[256 - i2]);
542
0
                        FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
543
0
                        FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
544
0
                    }
545
0
                    for (j = 1; j < 256; j++) {
546
0
                        if (stt[j] == i)
547
0
                            stt[j] = i2;
548
0
                        else if (stt[j] == i2)
549
0
                            stt[j] = i;
550
0
                        if (i != 256 - i2) {
551
0
                            if (stt[256 - j] == 256 - i)
552
0
                                stt[256 - j] = 256 - i2;
553
0
                            else if (stt[256 - j] == 256 - i2)
554
0
                                stt[256 - j] = 256 - i;
555
0
                        }
556
0
                    }
557
0
                    print = changed = 1;
558
0
                }
559
0
            }
560
0
        }
561
0
    } while (changed);
562
0
    return print;
563
0
}
564
565
566
int ff_ffv1_encode_determine_slices(AVCodecContext *avctx)
567
4.84k
{
568
4.84k
    FFV1Context *s = avctx->priv_data;
569
4.84k
    int plane_count = 1 + 2*s->chroma_planes + s->transparency;
570
4.84k
    int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
571
4.84k
    int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
572
4.84k
    s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
573
4.84k
    s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
574
5.66k
    for (; s->num_v_slices <= 32; s->num_v_slices++) {
575
16.6k
        for (s->num_h_slices = s->num_v_slices; s->num_h_slices <= 2*s->num_v_slices; s->num_h_slices++) {
576
15.8k
            int maxw = (avctx->width  + s->num_h_slices - 1) / s->num_h_slices;
577
15.8k
            int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
578
15.8k
            if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
579
7.47k
                continue;
580
8.34k
            if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
581
0
                continue;
582
8.34k
            if (s->version < 4)
583
4.70k
                if (  ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
584
3.82k
                    ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
585
3.51k
                    continue;
586
4.83k
            if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES)
587
0
                return 0;
588
4.83k
            if (maxw*maxh > 360*288)
589
11
                continue;
590
4.82k
            if (!avctx->slices)
591
4.82k
                return 0;
592
4.82k
        }
593
5.65k
    }
594
13
    av_log(avctx, AV_LOG_ERROR,
595
13
           "Unsupported number %d of slices requested, please specify a "
596
13
           "supported number with -slices (ex:4,6,9,12,16, ...)\n",
597
13
           avctx->slices);
598
13
    return AVERROR(ENOSYS);
599
4.84k
}
600
601
av_cold int ff_ffv1_encode_init(AVCodecContext *avctx)
602
4.84k
{
603
4.84k
    FFV1Context *s = avctx->priv_data;
604
4.84k
    int i, j, k, m, ret;
605
606
4.84k
    if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
607
4.84k
        avctx->slices > 1)
608
0
        s->version = FFMAX(s->version, 2);
609
610
4.84k
    if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) && s->ac == AC_GOLOMB_RICE) {
611
0
        av_log(avctx, AV_LOG_ERROR, "2 Pass mode is not possible with golomb coding\n");
612
0
        return AVERROR(EINVAL);
613
0
    }
614
615
    // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
616
4.84k
    if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
617
0
        s->version = FFMAX(s->version, 2);
618
619
4.84k
    if (avctx->level <= 0 && s->version == 2) {
620
0
        s->version = 3;
621
0
    }
622
4.84k
    if (avctx->level >= 0 && avctx->level <= 4) {
623
0
        if (avctx->level < s->version) {
624
0
            av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
625
0
            return AVERROR(EINVAL);
626
0
        }
627
0
        s->version = avctx->level;
628
4.84k
    } else if (s->version < 3)
629
1.19k
        s->version = 3;
630
631
4.84k
    if (s->ec < 0) {
632
0
        if (s->version >= 4) {
633
0
            s->ec = 2;
634
0
        } else if (s->version >= 3) {
635
0
            s->ec = 1;
636
0
        } else
637
0
            s->ec = 0;
638
0
    }
639
640
    // CRC requires version 3+
641
4.84k
    if (s->ec == 1)
642
1.60k
        s->version = FFMAX(s->version, 3);
643
4.84k
    if (s->ec == 2) {
644
0
        s->version = FFMAX(s->version, 4);
645
0
        s->crcref = 0x7a8c4079;
646
0
    }
647
648
4.84k
    if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
649
2
        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");
650
2
        return AVERROR_INVALIDDATA;
651
2
    }
652
653
4.84k
    if (s->ac == AC_RANGE_CUSTOM_TAB) {
654
831k
        for (i = 1; i < 256; i++)
655
828k
            s->state_transition[i] = ver2_state[i];
656
3.24k
    } else {
657
1.59k
        RangeCoder c;
658
1.59k
        ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
659
407k
        for (i = 1; i < 256; i++)
660
405k
            s->state_transition[i] = c.one_state[i];
661
1.59k
    }
662
663
1.24M
    for (i = 0; i < 256; i++) {
664
1.23M
        s->quant_table_count = 2;
665
1.23M
        if ((s->qtable == -1 && s->bits_per_raw_sample <= 8) || s->qtable == 1) {
666
144k
            s->quant_tables[0][0][i]=           quant11[i];
667
144k
            s->quant_tables[0][1][i]=        11*quant11[i];
668
144k
            s->quant_tables[0][2][i]=     11*11*quant11[i];
669
144k
            s->quant_tables[1][0][i]=           quant11[i];
670
144k
            s->quant_tables[1][1][i]=        11*quant11[i];
671
144k
            s->quant_tables[1][2][i]=     11*11*quant5 [i];
672
144k
            s->quant_tables[1][3][i]=   5*11*11*quant5 [i];
673
144k
            s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
674
144k
            s->context_count[0] = (11 * 11 * 11        + 1) / 2;
675
144k
            s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
676
1.09M
        } else {
677
1.09M
            s->quant_tables[0][0][i]=           quant9_10bit[i];
678
1.09M
            s->quant_tables[0][1][i]=         9*quant9_10bit[i];
679
1.09M
            s->quant_tables[0][2][i]=       9*9*quant9_10bit[i];
680
1.09M
            s->quant_tables[1][0][i]=           quant9_10bit[i];
681
1.09M
            s->quant_tables[1][1][i]=         9*quant9_10bit[i];
682
1.09M
            s->quant_tables[1][2][i]=       9*9*quant5_10bit[i];
683
1.09M
            s->quant_tables[1][3][i]=     5*9*9*quant5_10bit[i];
684
1.09M
            s->quant_tables[1][4][i]=   5*5*9*9*quant5_10bit[i];
685
1.09M
            s->context_count[0] = (9 * 9 * 9         + 1) / 2;
686
1.09M
            s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
687
1.09M
        }
688
1.23M
    }
689
690
4.84k
    if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
691
0
        return ret;
692
693
4.84k
    if (!s->transparency)
694
4.00k
        s->plane_count = 2;
695
4.84k
    if (!s->chroma_planes && s->version > 3)
696
432
        s->plane_count--;
697
698
4.84k
    s->picture_number = 0;
699
700
4.84k
    if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
701
0
        for (i = 0; i < s->quant_table_count; i++) {
702
0
            s->rc_stat2[i] = av_mallocz(s->context_count[i] *
703
0
                                        sizeof(*s->rc_stat2[i]));
704
0
            if (!s->rc_stat2[i])
705
0
                return AVERROR(ENOMEM);
706
0
        }
707
0
    }
708
4.84k
    if (avctx->stats_in) {
709
0
        char *p = avctx->stats_in;
710
0
        uint8_t (*best_state)[256] = av_malloc_array(256, 256);
711
0
        int gob_count = 0;
712
0
        char *next;
713
0
        if (!best_state)
714
0
            return AVERROR(ENOMEM);
715
716
0
        av_assert0(s->version >= 2);
717
718
0
        for (;;) {
719
0
            for (j = 0; j < 256; j++)
720
0
                for (i = 0; i < 2; i++) {
721
0
                    s->rc_stat[j][i] = strtol(p, &next, 0);
722
0
                    if (next == p) {
723
0
                        av_log(avctx, AV_LOG_ERROR,
724
0
                               "2Pass file invalid at %d %d [%s]\n", j, i, p);
725
0
                        av_freep(&best_state);
726
0
                        return AVERROR_INVALIDDATA;
727
0
                    }
728
0
                    p = next;
729
0
                }
730
0
            for (i = 0; i < s->quant_table_count; i++)
731
0
                for (j = 0; j < s->context_count[i]; j++) {
732
0
                    for (k = 0; k < 32; k++)
733
0
                        for (m = 0; m < 2; m++) {
734
0
                            s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
735
0
                            if (next == p) {
736
0
                                av_log(avctx, AV_LOG_ERROR,
737
0
                                       "2Pass file invalid at %d %d %d %d [%s]\n",
738
0
                                       i, j, k, m, p);
739
0
                                av_freep(&best_state);
740
0
                                return AVERROR_INVALIDDATA;
741
0
                            }
742
0
                            p = next;
743
0
                        }
744
0
                }
745
0
            gob_count = strtol(p, &next, 0);
746
0
            if (next == p || gob_count <= 0) {
747
0
                av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
748
0
                av_freep(&best_state);
749
0
                return AVERROR_INVALIDDATA;
750
0
            }
751
0
            p = next;
752
0
            while (*p == '\n' || *p == ' ')
753
0
                p++;
754
0
            if (p[0] == 0)
755
0
                break;
756
0
        }
757
0
        if (s->ac == AC_RANGE_CUSTOM_TAB)
758
0
            sort_stt(s, s->state_transition);
759
760
0
        find_best_state(best_state, s->state_transition);
761
762
0
        for (i = 0; i < s->quant_table_count; i++) {
763
0
            for (k = 0; k < 32; k++) {
764
0
                double a=0, b=0;
765
0
                int jp = 0;
766
0
                for (j = 0; j < s->context_count[i]; j++) {
767
0
                    double p = 128;
768
0
                    if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
769
0
                        if (a+b)
770
0
                            p = 256.0 * b / (a + b);
771
0
                        s->initial_states[i][jp][k] =
772
0
                            best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
773
0
                        for(jp++; jp<j; jp++)
774
0
                            s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
775
0
                        a=b=0;
776
0
                    }
777
0
                    a += s->rc_stat2[i][j][k][0];
778
0
                    b += s->rc_stat2[i][j][k][1];
779
0
                    if (a+b) {
780
0
                        p = 256.0 * b / (a + b);
781
0
                    }
782
0
                    s->initial_states[i][j][k] =
783
0
                        best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
784
0
                }
785
0
            }
786
0
        }
787
0
        av_freep(&best_state);
788
0
    }
789
790
4.84k
    if (s->version <= 1) {
791
        /* Disable slices when the version doesn't support them */
792
0
        s->num_h_slices = 1;
793
0
        s->num_v_slices = 1;
794
0
    }
795
796
4.84k
    set_micro_version(s);
797
798
4.84k
    return 0;
799
4.84k
}
800
801
av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
802
                                            enum AVPixelFormat pix_fmt)
803
4.84k
{
804
4.84k
    FFV1Context *s = avctx->priv_data;
805
4.84k
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
806
807
4.84k
    s->plane_count = 3;
808
4.84k
    switch(pix_fmt) {
809
3
    case AV_PIX_FMT_GRAY9:
810
7
    case AV_PIX_FMT_YUV444P9:
811
8
    case AV_PIX_FMT_YUV422P9:
812
10
    case AV_PIX_FMT_YUV420P9:
813
12
    case AV_PIX_FMT_YUVA444P9:
814
16
    case AV_PIX_FMT_YUVA422P9:
815
27
    case AV_PIX_FMT_YUVA420P9:
816
27
        if (!avctx->bits_per_raw_sample)
817
27
            s->bits_per_raw_sample = 9;
818
27
        av_fallthrough;
819
29
    case AV_PIX_FMT_GRAY10:
820
31
    case AV_PIX_FMT_YUV444P10:
821
36
    case AV_PIX_FMT_YUV440P10:
822
39
    case AV_PIX_FMT_YUV420P10:
823
42
    case AV_PIX_FMT_YUV422P10:
824
54
    case AV_PIX_FMT_YUVA444P10:
825
56
    case AV_PIX_FMT_YUVA422P10:
826
63
    case AV_PIX_FMT_YUVA420P10:
827
63
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
828
36
            s->bits_per_raw_sample = 10;
829
63
        av_fallthrough;
830
76
    case AV_PIX_FMT_GRAY12:
831
79
    case AV_PIX_FMT_YUV444P12:
832
84
    case AV_PIX_FMT_YUV440P12:
833
86
    case AV_PIX_FMT_YUV420P12:
834
89
    case AV_PIX_FMT_YUV422P12:
835
93
    case AV_PIX_FMT_YUVA444P12:
836
96
    case AV_PIX_FMT_YUVA422P12:
837
96
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
838
33
            s->bits_per_raw_sample = 12;
839
96
        av_fallthrough;
840
105
    case AV_PIX_FMT_GRAY14:
841
112
    case AV_PIX_FMT_YUV444P14:
842
135
    case AV_PIX_FMT_YUV420P14:
843
139
    case AV_PIX_FMT_YUV422P14:
844
139
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
845
43
            s->bits_per_raw_sample = 14;
846
139
        s->packed_at_lsb = 1;
847
139
        av_fallthrough;
848
171
    case AV_PIX_FMT_GRAY16:
849
171
    case AV_PIX_FMT_P016:
850
171
    case AV_PIX_FMT_P216:
851
171
    case AV_PIX_FMT_P416:
852
179
    case AV_PIX_FMT_YUV444P16:
853
188
    case AV_PIX_FMT_YUV422P16:
854
197
    case AV_PIX_FMT_YUV420P16:
855
214
    case AV_PIX_FMT_YUVA444P16:
856
215
    case AV_PIX_FMT_YUVA422P16:
857
229
    case AV_PIX_FMT_YUVA420P16:
858
412
    case AV_PIX_FMT_GRAYF16:
859
661
    case AV_PIX_FMT_YAF16:
860
661
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
861
522
            s->bits_per_raw_sample = 16;
862
522
        } else if (!s->bits_per_raw_sample) {
863
0
            s->bits_per_raw_sample = avctx->bits_per_raw_sample;
864
0
        }
865
661
        if (s->bits_per_raw_sample <= 8) {
866
0
            av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
867
0
            return AVERROR_INVALIDDATA;
868
0
        }
869
661
        s->version = FFMAX(s->version, 1);
870
661
        av_fallthrough;
871
671
    case AV_PIX_FMT_GRAY8:
872
695
    case AV_PIX_FMT_YA8:
873
695
    case AV_PIX_FMT_NV12:
874
695
    case AV_PIX_FMT_NV16:
875
695
    case AV_PIX_FMT_NV24:
876
714
    case AV_PIX_FMT_YUV444P:
877
726
    case AV_PIX_FMT_YUV440P:
878
735
    case AV_PIX_FMT_YUV422P:
879
835
    case AV_PIX_FMT_YUV420P:
880
866
    case AV_PIX_FMT_YUV411P:
881
910
    case AV_PIX_FMT_YUV410P:
882
917
    case AV_PIX_FMT_YUVA444P:
883
926
    case AV_PIX_FMT_YUVA422P:
884
971
    case AV_PIX_FMT_YUVA420P:
885
971
        s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
886
971
        s->colorspace = 0;
887
971
        s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
888
971
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
889
310
            s->bits_per_raw_sample = 8;
890
661
        else if (!s->bits_per_raw_sample)
891
0
            s->bits_per_raw_sample = 8;
892
971
        break;
893
182
    case AV_PIX_FMT_RGB32:
894
182
        s->colorspace = 1;
895
182
        s->transparency = 1;
896
182
        s->chroma_planes = 1;
897
182
        s->bits_per_raw_sample = 8;
898
182
        break;
899
49
    case AV_PIX_FMT_RGBA64:
900
49
        s->colorspace = 1;
901
49
        s->transparency = 1;
902
49
        s->chroma_planes = 1;
903
49
        s->bits_per_raw_sample = 16;
904
49
        s->use32bit = 1;
905
49
        s->version = FFMAX(s->version, 1);
906
49
        break;
907
26
    case AV_PIX_FMT_RGB48:
908
26
        s->colorspace = 1;
909
26
        s->chroma_planes = 1;
910
26
        s->bits_per_raw_sample = 16;
911
26
        s->use32bit = 1;
912
26
        s->version = FFMAX(s->version, 1);
913
26
        break;
914
0
    case AV_PIX_FMT_GBRP:
915
74
    case AV_PIX_FMT_0RGB32:
916
74
        s->colorspace = 1;
917
74
        s->chroma_planes = 1;
918
74
        s->bits_per_raw_sample = 8;
919
74
        break;
920
8
    case AV_PIX_FMT_GBRP9:
921
8
        if (!avctx->bits_per_raw_sample)
922
8
            s->bits_per_raw_sample = 9;
923
8
        av_fallthrough;
924
8
    case AV_PIX_FMT_X2BGR10:
925
8
    case AV_PIX_FMT_X2RGB10:
926
16
    case AV_PIX_FMT_GBRP10:
927
55
    case AV_PIX_FMT_GBRAP10:
928
55
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
929
47
            s->bits_per_raw_sample = 10;
930
55
        av_fallthrough;
931
64
    case AV_PIX_FMT_GBRP12:
932
106
    case AV_PIX_FMT_GBRAP12:
933
106
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
934
51
            s->bits_per_raw_sample = 12;
935
106
        av_fallthrough;
936
141
    case AV_PIX_FMT_GBRP14:
937
203
    case AV_PIX_FMT_GBRAP14:
938
203
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
939
97
            s->bits_per_raw_sample = 14;
940
203
        av_fallthrough;
941
272
    case AV_PIX_FMT_GBRP16:
942
324
    case AV_PIX_FMT_GBRAP16:
943
1.04k
    case AV_PIX_FMT_GBRPF16:
944
1.04k
    case AV_PIX_FMT_GBRAPF16:
945
1.04k
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
946
841
            s->bits_per_raw_sample = 16;
947
1.04k
        av_fallthrough;
948
3.54k
    case AV_PIX_FMT_GBRPF32:
949
3.54k
    case AV_PIX_FMT_GBRAPF32:
950
3.54k
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
951
2.49k
            s->bits_per_raw_sample = 32;
952
1.04k
        else if (!s->bits_per_raw_sample)
953
0
            s->bits_per_raw_sample = avctx->bits_per_raw_sample;
954
3.54k
        s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
955
3.54k
        s->colorspace = 1;
956
3.54k
        s->chroma_planes = 1;
957
3.54k
        if (s->bits_per_raw_sample >= 16) {
958
3.33k
            s->use32bit = 1;
959
3.33k
        }
960
3.54k
        s->version = FFMAX(s->version, 1);
961
3.54k
        break;
962
0
    default:
963
0
        av_log(avctx, AV_LOG_ERROR, "format %s not supported\n",
964
0
               av_get_pix_fmt_name(pix_fmt));
965
0
        return AVERROR(ENOSYS);
966
4.84k
    }
967
4.84k
    s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
968
4.84k
    if (s->flt || s->remap_mode > 0)
969
3.64k
        s->version = FFMAX(s->version, 4);
970
4.84k
    av_assert0(s->bits_per_raw_sample >= 8);
971
972
4.84k
    if (s->remap_mode < 0)
973
4.84k
        s->remap_mode = s->flt ? 2 : 0;
974
4.84k
    if (s->remap_mode == 0 && s->bits_per_raw_sample == 32) {
975
0
        av_log(avctx, AV_LOG_ERROR, "32bit requires remap\n");
976
0
        return AVERROR(EINVAL);
977
0
    }
978
4.84k
    if (s->remap_mode == 2 &&
979
3.64k
        !((s->bits_per_raw_sample == 16 || s->bits_per_raw_sample == 32 || s->bits_per_raw_sample == 64) && s->flt)) {
980
0
        av_log(avctx, AV_LOG_ERROR, "remap 2 is for float16/32/64 only\n");
981
0
        return AVERROR(EINVAL);
982
0
    }
983
984
4.84k
    return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
985
4.84k
}
986
987
static av_cold int encode_init_internal(AVCodecContext *avctx)
988
4.84k
{
989
4.84k
    int ret;
990
4.84k
    FFV1Context *s = avctx->priv_data;
991
992
4.84k
    if ((ret = ff_ffv1_common_init(avctx, s)) < 0)
993
0
        return ret;
994
995
4.84k
    if (s->ac == 1) // Compatibility with common command line usage
996
827
        s->ac = AC_RANGE_CUSTOM_TAB;
997
4.01k
    else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
998
562
        s->ac = AC_RANGE_DEFAULT_TAB;
999
1000
4.84k
    ret = ff_ffv1_encode_setup_plane_info(avctx, avctx->pix_fmt);
1001
4.84k
    if (ret < 0)
1002
0
        return ret;
1003
1004
4.84k
    if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8) && !s->remap_mode) {
1005
628
        if (s->ac == AC_GOLOMB_RICE) {
1006
184
            av_log(avctx, AV_LOG_INFO,
1007
184
                    "high bits_per_raw_sample, forcing range coder\n");
1008
184
            s->ac = AC_RANGE_CUSTOM_TAB;
1009
184
        }
1010
628
    }
1011
1012
1013
4.84k
    ret = ff_ffv1_encode_init(avctx);
1014
4.84k
    if (ret < 0)
1015
2
        return ret;
1016
1017
4.84k
    if (s->version > 1) {
1018
4.84k
        if ((ret = ff_ffv1_encode_determine_slices(avctx)) < 0)
1019
13
            return ret;
1020
1021
4.82k
        if ((ret = ff_ffv1_write_extradata(avctx)) < 0)
1022
0
            return ret;
1023
4.82k
    }
1024
1025
4.82k
    if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
1026
0
        return ret;
1027
4.82k
    s->slice_count = s->max_slice_count;
1028
1029
29.7k
    for (int j = 0; j < s->slice_count; j++) {
1030
24.9k
        FFV1SliceContext *sc = &s->slices[j];
1031
1032
78.8k
        for (int i = 0; i < s->plane_count; i++) {
1033
53.9k
            PlaneContext *const p = &s->slices[j].plane[i];
1034
1035
53.9k
            p->quant_table_index = s->context_model;
1036
53.9k
            p->context_count     = s->context_count[p->quant_table_index];
1037
53.9k
        }
1038
24.9k
        av_assert0(s->remap_mode >= 0);
1039
24.9k
        if (s->remap_mode) {
1040
51.1k
            for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) {
1041
37.8k
                if (s->bits_per_raw_sample == 32) {
1042
28.0k
                    sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
1043
28.0k
                    if (!sc->unit[p])
1044
0
                        return AVERROR(ENOMEM);
1045
28.0k
                    sc->bitmap[p] = av_malloc_array(sc->slice_width * sc->slice_height, sizeof(*sc->bitmap[p]));
1046
28.0k
                    if (!sc->bitmap[p])
1047
0
                        return AVERROR(ENOMEM);
1048
28.0k
                } else {
1049
9.74k
                    sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
1050
9.74k
                    if (!sc->fltmap[p])
1051
0
                        return AVERROR(ENOMEM);
1052
9.74k
                }
1053
37.8k
            }
1054
13.3k
        }
1055
1056
24.9k
        ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
1057
1058
24.9k
        s->slices[j].remap = s->remap_mode;
1059
24.9k
    }
1060
1061
4.82k
    if ((ret = ff_ffv1_init_slices_state(s)) < 0)
1062
0
        return ret;
1063
1064
4.82k
#define STATS_OUT_SIZE 1024 * 1024 * 6
1065
4.82k
    if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1066
0
        avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
1067
0
        if (!avctx->stats_out)
1068
0
            return AVERROR(ENOMEM);
1069
0
        for (int i = 0; i < s->quant_table_count; i++)
1070
0
            for (int j = 0; j < s->max_slice_count; j++) {
1071
0
                FFV1SliceContext *sc = &s->slices[j];
1072
0
                av_assert0(!sc->rc_stat2[i]);
1073
0
                sc->rc_stat2[i] = av_mallocz(s->context_count[i] *
1074
0
                                             sizeof(*sc->rc_stat2[i]));
1075
0
                if (!sc->rc_stat2[i])
1076
0
                    return AVERROR(ENOMEM);
1077
0
            }
1078
0
    }
1079
1080
4.82k
    return 0;
1081
4.82k
}
1082
1083
static void encode_slice_header(FFV1Context *f, FFV1SliceContext *sc)
1084
114k
{
1085
114k
    RangeCoder *c = &sc->c;
1086
114k
    uint8_t state[CONTEXT_SIZE];
1087
114k
    int j;
1088
114k
    memset(state, 128, sizeof(state));
1089
1090
114k
    put_symbol(c, state, sc->sx, 0);
1091
114k
    put_symbol(c, state, sc->sy, 0);
1092
114k
    put_symbol(c, state, 0, 0);
1093
114k
    put_symbol(c, state, 0, 0);
1094
340k
    for (j=0; j<f->plane_count; j++) {
1095
225k
        put_symbol(c, state, sc->plane[j].quant_table_index, 0);
1096
225k
        av_assert0(sc->plane[j].quant_table_index == f->context_model);
1097
225k
    }
1098
114k
    if (!(f->cur_enc_frame->flags & AV_FRAME_FLAG_INTERLACED))
1099
114k
        put_symbol(c, state, 3, 0);
1100
0
    else
1101
0
        put_symbol(c, state, 1 + !(f->cur_enc_frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST), 0);
1102
114k
    put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.num, 0);
1103
114k
    put_symbol(c, state, f->cur_enc_frame->sample_aspect_ratio.den, 0);
1104
114k
    if (f->version > 3) {
1105
78.1k
        put_rac(c, state, sc->slice_coding_mode == 1);
1106
78.1k
        if (sc->slice_coding_mode == 1)
1107
2.41k
            ff_ffv1_clear_slice_state(f, sc);
1108
78.1k
        put_symbol(c, state, sc->slice_coding_mode, 0);
1109
78.1k
        if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
1110
37.2k
            put_symbol(c, state, sc->slice_rct_by_coef, 0);
1111
37.2k
            put_symbol(c, state, sc->slice_rct_ry_coef, 0);
1112
37.2k
        }
1113
78.1k
        put_symbol(c, state, sc->remap, 0);
1114
78.1k
    }
1115
114k
}
1116
1117
static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
1118
                              const uint8_t *src[3], const int stride[3], int w, int h)
1119
37.2k
{
1120
1.84G
#define NB_Y_COEFF 15
1121
37.2k
    static const int rct_y_coeff[15][2] = {
1122
37.2k
        {0, 0}, //      4G
1123
37.2k
        {1, 1}, //  R + 2G + B
1124
37.2k
        {2, 2}, // 2R      + 2B
1125
37.2k
        {0, 2}, //      2G + 2B
1126
37.2k
        {2, 0}, // 2R + 2G
1127
37.2k
        {4, 0}, // 4R
1128
37.2k
        {0, 4}, //           4B
1129
1130
37.2k
        {0, 3}, //      1G + 3B
1131
37.2k
        {3, 0}, // 3R + 1G
1132
37.2k
        {3, 1}, // 3R      +  B
1133
37.2k
        {1, 3}, //  R      + 3B
1134
37.2k
        {1, 2}, //  R +  G + 2B
1135
37.2k
        {2, 1}, // 2R +  G +  B
1136
37.2k
        {0, 1}, //      3G +  B
1137
37.2k
        {1, 0}, //  R + 3G
1138
37.2k
    };
1139
1140
37.2k
    int stat[NB_Y_COEFF] = {0};
1141
37.2k
    int x, y, i, p, best;
1142
37.2k
    int16_t *sample[3];
1143
37.2k
    int lbd = f->bits_per_raw_sample <= 8;
1144
37.2k
    int packed = !src[1];
1145
37.2k
    int transparency = f->transparency;
1146
37.2k
    int packed_size = (3 + transparency)*2;
1147
1148
2.43M
    for (y = 0; y < h; y++) {
1149
2.39M
        int lastr=0, lastg=0, lastb=0;
1150
9.59M
        for (p = 0; p < 3; p++)
1151
7.19M
            sample[p] = sc->sample_buffer + p*w;
1152
1153
190M
        for (x = 0; x < w; x++) {
1154
187M
            int b, g, r;
1155
187M
            int ab, ag, ar;
1156
187M
            if (lbd) {
1157
0
                unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
1158
0
                b =  v        & 0xFF;
1159
0
                g = (v >>  8) & 0xFF;
1160
0
                r = (v >> 16) & 0xFF;
1161
187M
            } else if (packed) {
1162
0
                const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
1163
0
                r = p[0];
1164
0
                g = p[1];
1165
0
                b = p[2];
1166
187M
            } else if (f->use32bit || transparency) {
1167
187M
                g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
1168
187M
                b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
1169
187M
                r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
1170
187M
            } else {
1171
0
                b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
1172
0
                g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
1173
0
                r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
1174
0
            }
1175
1176
187M
            ar = r - lastr;
1177
187M
            ag = g - lastg;
1178
187M
            ab = b - lastb;
1179
187M
            if (x && y) {
1180
115M
                int bg = ag - sample[0][x];
1181
115M
                int bb = ab - sample[1][x];
1182
115M
                int br = ar - sample[2][x];
1183
1184
115M
                br -= bg;
1185
115M
                bb -= bg;
1186
1187
1.84G
                for (i = 0; i<NB_Y_COEFF; i++) {
1188
1.72G
                    stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1189
1.72G
                }
1190
1191
115M
            }
1192
187M
            sample[0][x] = ag;
1193
187M
            sample[1][x] = ab;
1194
187M
            sample[2][x] = ar;
1195
1196
187M
            lastr = r;
1197
187M
            lastg = g;
1198
187M
            lastb = b;
1199
187M
        }
1200
2.39M
    }
1201
1202
37.2k
    best = 0;
1203
558k
    for (i=1; i<NB_Y_COEFF; i++) {
1204
521k
        if (stat[i] < stat[best])
1205
49.2k
            best = i;
1206
521k
    }
1207
1208
37.2k
    sc->slice_rct_by_coef = rct_y_coeff[best][1];
1209
37.2k
    sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1210
37.2k
}
1211
1212
static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc)
1213
45.9k
{
1214
45.9k
    int len = 1 << f->bits_per_raw_sample;
1215
45.9k
    int flip = sc->remap == 2 ? 0x7FFF : 0;
1216
1217
124k
    for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
1218
78.2k
        int j = 0;
1219
78.2k
        int lu = 0;
1220
78.2k
        uint8_t state[2][32];
1221
78.2k
        int run = 0;
1222
1223
78.2k
        memset(state, 128, sizeof(state));
1224
78.2k
        put_symbol(&sc->c, state[0], 0, 0);
1225
78.2k
        memset(state, 128, sizeof(state));
1226
5.13G
        for (int i= 0; i<len; i++) {
1227
5.13G
            int ri = i ^ ((i&0x8000) ? 0 : flip);
1228
5.13G
            int u = sc->fltmap[p][ri];
1229
5.13G
            sc->fltmap[p][ri] = j;
1230
5.13G
            j+= u;
1231
1232
5.13G
            if (lu == u) {
1233
5.11G
                run ++;
1234
5.11G
            } else {
1235
11.6M
                put_symbol_inline(&sc->c, state[lu], run, 0, NULL, NULL);
1236
11.6M
                if (run == 0)
1237
3.41M
                    lu = u;
1238
11.6M
                run = 0;
1239
11.6M
            }
1240
5.13G
        }
1241
78.2k
        if (run)
1242
65.0k
            put_symbol(&sc->c, state[lu], run, 0);
1243
78.2k
        sc->remap_count[p] = j;
1244
78.2k
    }
1245
45.9k
}
1246
1247
static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc,
1248
                                   const uint8_t *src[4],
1249
                                   int w, int h, const int stride[4])
1250
32.2k
{
1251
32.2k
    int x, y;
1252
32.2k
    int transparency = f->transparency;
1253
32.2k
    int i = 0;
1254
1255
1.51M
    for (y = 0; y < h; y++) {
1256
170M
        for (x = 0; x < w; x++) {
1257
169M
            int b, g, r, av_uninit(a);
1258
1259
169M
            g = *((const uint32_t *)(src[0] + x*4 + stride[0]*y));
1260
169M
            b = *((const uint32_t *)(src[1] + x*4 + stride[1]*y));
1261
169M
            r = *((const uint32_t *)(src[2] + x*4 + stride[2]*y));
1262
169M
            if (transparency)
1263
0
                a = *((const uint32_t *)(src[3] + x*4 + stride[3]*y));
1264
1265
169M
            if (sc->remap == 2) {
1266
508M
#define FLIP(f) (((f)&0x80000000) ? (f) : (f)^0x7FFFFFFF);
1267
169M
                g = FLIP(g);
1268
169M
                b = FLIP(b);
1269
169M
                r = FLIP(r);
1270
169M
            }
1271
            // We cannot build a histogram as we do for 16bit, we need a bit of magic here
1272
            // Its possible to reduce the memory needed at the cost of more dereferencing
1273
169M
            sc->unit[0][i].val = g;
1274
169M
            sc->unit[0][i].ndx = x + y*w;
1275
1276
169M
            sc->unit[1][i].val = b;
1277
169M
            sc->unit[1][i].ndx = x + y*w;
1278
1279
169M
            sc->unit[2][i].val = r;
1280
169M
            sc->unit[2][i].ndx = x + y*w;
1281
1282
169M
            if (transparency) {
1283
0
                sc->unit[3][i].val = a;
1284
0
                sc->unit[3][i].ndx = x + y*w;
1285
0
            }
1286
169M
            i++;
1287
169M
        }
1288
1.47M
    }
1289
1290
    //TODO switch to radix sort
1291
1.35G
#define CMP(A,B) ((A)->val - (int64_t)(B)->val)
1292
641M
    AV_QSORT(sc->unit[0], i, struct Unit, CMP);
1293
355M
    AV_QSORT(sc->unit[1], i, struct Unit, CMP);
1294
353M
    AV_QSORT(sc->unit[2], i, struct Unit, CMP);
1295
32.2k
    if (transparency)
1296
0
        AV_QSORT(sc->unit[3], i, struct Unit, CMP);
1297
32.2k
}
1298
1299
static int encode_float32_remap_segment(FFV1SliceContext *sc,
1300
                                        int p, int mul_count, int *mul_tab, int update, int final)
1301
290k
{
1302
290k
    const int pixel_num = sc->slice_width * sc->slice_height;
1303
290k
    uint8_t state[2][3][32];
1304
290k
    int mul[4096+1];
1305
290k
    RangeCoder rc = sc->c;
1306
290k
    int lu = 0;
1307
290k
    int run = 0;
1308
290k
    int64_t last_val = -1;
1309
290k
    int compact_index = -1;
1310
290k
    int i = 0;
1311
290k
    int current_mul_index = -1;
1312
290k
    int run1final = 0;
1313
290k
    int run1start_i;
1314
290k
    int run1start_last_val;
1315
290k
    int run1start_mul_index;
1316
1317
290k
    memcpy(mul, mul_tab, sizeof(*mul_tab)*(mul_count+1));
1318
290k
    memset(state, 128, sizeof(state));
1319
290k
    put_symbol(&rc, state[0][0], mul_count, 0);
1320
290k
    memset(state, 128, sizeof(state));
1321
1322
1.66G
    for (; i < pixel_num+1; i++) {
1323
1.66G
        int current_mul = current_mul_index < 0 ? 1 : FFABS(mul[current_mul_index]);
1324
1.66G
        int64_t val;
1325
1.66G
        if (i == pixel_num) {
1326
319k
            if (last_val == 0xFFFFFFFF && (!run || run1final)) {
1327
100k
                break;
1328
219k
            } else {
1329
219k
                val = last_val + ((1LL<<32) - last_val + current_mul - 1) / current_mul * current_mul;
1330
219k
                av_assert2(val >= (1LL<<32));
1331
219k
                val += lu * current_mul; //ensure a run1 ends
1332
219k
            }
1333
319k
        } else
1334
1.66G
            val = sc->unit[p][i].val;
1335
1336
1.66G
        if (last_val != val) {
1337
33.8M
            int64_t delta = val - last_val;
1338
33.8M
            int64_t step  = FFMAX(1, (delta + current_mul/2) / current_mul);
1339
33.8M
            av_assert2(last_val < val);
1340
33.8M
            av_assert2(current_mul > 0);
1341
1342
33.8M
            delta -= step*current_mul;
1343
33.8M
            av_assert2(delta <= current_mul/2);
1344
33.8M
            av_assert2(delta > -current_mul);
1345
1346
33.8M
            av_assert2(step > 0);
1347
33.8M
            if (lu) {
1348
7.47M
                if (!run) {
1349
3.99M
                    run1start_i        = i - 1;
1350
3.99M
                    run1start_last_val = last_val;
1351
3.99M
                    run1start_mul_index= current_mul_index;
1352
3.99M
                }
1353
7.47M
                if (step == 1) {
1354
3.48M
                    if (run1final) {
1355
1.57M
                        if (current_mul>1)
1356
1.55M
                            put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
1357
1.57M
                    }
1358
3.48M
                    run ++;
1359
3.48M
                    av_assert2(last_val + current_mul + delta == val);
1360
3.99M
                } else {
1361
3.99M
                    if (run1final) {
1362
1.99M
                        if (run == 0)
1363
1.38M
                            lu ^= 1;
1364
1.99M
                        i--; // we did not encode val so we need to backstep
1365
1.99M
                        last_val += current_mul;
1366
1.99M
                    } else {
1367
1.99M
                        put_symbol_inline(&rc, state[lu][0], run, 0, NULL, NULL);
1368
1.99M
                        i                 = run1start_i;
1369
1.99M
                        last_val          = run1start_last_val; // we could compute this instead of storing
1370
1.99M
                        current_mul_index = run1start_mul_index;
1371
1.99M
                    }
1372
3.99M
                    run1final ^= 1;
1373
1374
3.99M
                    run = 0;
1375
3.99M
                    continue;
1376
3.99M
                }
1377
26.3M
            } else {
1378
26.3M
                av_assert2(run == 0);
1379
26.3M
                av_assert2(run1final == 0);
1380
26.3M
                put_symbol_inline(&rc, state[lu][0], step - 1, 0, NULL, NULL);
1381
1382
26.3M
                if (current_mul > 1)
1383
24.8M
                    put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
1384
26.3M
                if (step == 1)
1385
1.42M
                    lu ^= 1;
1386
1387
26.3M
                av_assert2(last_val + step * current_mul + delta == val);
1388
26.3M
            }
1389
29.8M
            last_val = val;
1390
29.8M
            current_mul_index = ((last_val + 1) * mul_count) >> 32;
1391
29.8M
            if (!run || run1final) {
1392
27.9M
                av_assert2(mul[ current_mul_index ]);
1393
27.9M
                if (mul[ current_mul_index ] < 0) {
1394
1.91M
                    av_assert2(i < pixel_num);
1395
1.91M
                    mul[ current_mul_index ] *= -1;
1396
1.91M
                    put_symbol_inline(&rc, state[0][2], mul[ current_mul_index ], 0, NULL, NULL);
1397
1.91M
                }
1398
27.9M
                if (i < pixel_num)
1399
27.7M
                    compact_index ++;
1400
27.9M
            }
1401
29.8M
        }
1402
1.65G
        if (!run || run1final)
1403
1.52G
            if (final && i < pixel_num)
1404
508M
                sc->bitmap[p][sc->unit[p][i].ndx] = compact_index;
1405
1.65G
    }
1406
1407
290k
    if (update) {
1408
96.8k
        sc->c = rc;
1409
96.8k
        sc->remap_count[p] = compact_index + 1;
1410
96.8k
    }
1411
290k
    return get_rac_count(&rc);
1412
290k
}
1413
1414
static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
1415
                                 const uint8_t *src[4])
1416
32.2k
{
1417
32.2k
    int pixel_num = sc->slice_width * sc->slice_height;
1418
32.2k
    const int max_log2_mul_count  = ((int[]){  1,  1,  1,  9,  9,  10})[f->remap_optimizer];
1419
32.2k
    const int log2_mul_count_step = ((int[]){  1,  1,  1,  9,  9,   1})[f->remap_optimizer];
1420
32.2k
    const int max_log2_mul        = ((int[]){  1,  8,  8,  9, 22,  22})[f->remap_optimizer];
1421
32.2k
    const int log2_mul_step       = ((int[]){  1,  8,  1,  1,  1,   1})[f->remap_optimizer];
1422
32.2k
    const int bruteforce_count    = ((int[]){  0,  0,  0,  1,  1,   1})[f->remap_optimizer];
1423
32.2k
    const int stair_mode          = ((int[]){  0,  0,  0,  1,  0,   0})[f->remap_optimizer];
1424
32.2k
    const int magic_log2          = ((int[]){  1,  1,  1,  1,  0,   0})[f->remap_optimizer];
1425
1426
129k
    for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
1427
96.8k
        int best_log2_mul_count = 0;
1428
96.8k
        float score_sum[11] = {0};
1429
96.8k
        int mul_all[11][1025];
1430
1431
290k
        for (int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; log2_mul_count += log2_mul_count_step) {
1432
193k
            float score_tab_all[1025][23] = {0};
1433
193k
            int64_t last_val = -1;
1434
193k
            int *mul_tab = mul_all[log2_mul_count];
1435
193k
            int last_mul_index = -1;
1436
193k
            int mul_count = 1 << log2_mul_count;
1437
1438
193k
            score_sum[log2_mul_count] = 2 * log2_mul_count;
1439
193k
            if (magic_log2)
1440
193k
                score_sum[log2_mul_count] = av_float2int((float)mul_count * mul_count);
1441
1.01G
            for (int i= 0; i<pixel_num; i++) {
1442
1.01G
                int64_t val = sc->unit[p][i].val;
1443
1.01G
                int mul_index = (val + 1LL)*mul_count >> 32;
1444
1.01G
                if (val != last_val) {
1445
18.5M
                    float *score_tab = score_tab_all[(last_val + 1LL)*mul_count >> 32];
1446
18.5M
                    av_assert2(last_val < val);
1447
203M
                    for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1448
185M
                        int64_t delta = val - last_val;
1449
185M
                        int mul;
1450
185M
                        int64_t cost;
1451
1452
185M
                        if (last_val < 0) {
1453
1.93M
                            mul = 1;
1454
183M
                        } else if (stair_mode && mul_count == 512 && si == max_log2_mul ) {
1455
9.15M
                            if (mul_index >= 0x378/8 && mul_index <= 23 + 0x378/8) {
1456
486k
                                mul = (0x800080 >> (mul_index - 0x378/8));
1457
486k
                            } else
1458
8.67M
                                mul = 1;
1459
174M
                        } else {
1460
174M
                            mul = (0x10001LL)<<si >> 16;
1461
174M
                        }
1462
1463
185M
                        cost = FFMAX((delta + mul/2)  / mul, 1);
1464
185M
                        float score = 1;
1465
185M
                        if (mul > 1) {
1466
156M
                            score *= (FFABS(delta - cost*mul)+1);
1467
156M
                            if (mul_count > 1)
1468
73.7M
                                score *= score;
1469
156M
                        }
1470
185M
                        score *= cost;
1471
185M
                        score *= score;
1472
185M
                        if (mul_index != last_mul_index)
1473
18.6M
                            score *= mul;
1474
185M
                        if (magic_log2) {
1475
185M
                            score_tab[si] += av_float2int(score);
1476
185M
                        } else
1477
0
                            score_tab[si] += log2f(score);
1478
185M
                    }
1479
18.5M
                }
1480
1.01G
                last_val = val;
1481
1.01G
                last_mul_index = mul_index;
1482
1.01G
            }
1483
49.8M
            for(int i= 0; i<mul_count; i++) {
1484
49.6M
                int best_index = 0;
1485
49.6M
                float *score_tab = score_tab_all[i];
1486
546M
                for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1487
496M
                    if (score_tab[si] < score_tab[ best_index ])
1488
4.11M
                        best_index = si;
1489
496M
                }
1490
49.6M
                if (stair_mode && mul_count == 512 && best_index == max_log2_mul ) {
1491
15.9k
                    if (i >= 0x378/8 && i <= 23 + 0x378/8) {
1492
15.7k
                        mul_tab[i] = -(0x800080 >> (i - 0x378/8));
1493
15.7k
                    } else
1494
229
                        mul_tab[i] = -1;
1495
15.9k
                } else
1496
49.6M
                    mul_tab[i] = -((0x10001LL)<<best_index >> 16);
1497
49.6M
                score_sum[log2_mul_count] += score_tab[ best_index ];
1498
49.6M
            }
1499
193k
            mul_tab[mul_count] = 1;
1500
1501
193k
            if (bruteforce_count)
1502
193k
                score_sum[log2_mul_count] = encode_float32_remap_segment(sc, p, mul_count, mul_all[log2_mul_count], 0, 0);
1503
1504
193k
            if (score_sum[log2_mul_count] < score_sum[best_log2_mul_count])
1505
1.30k
                best_log2_mul_count = log2_mul_count;
1506
193k
        }
1507
1508
96.8k
        encode_float32_remap_segment(sc, p, 1<<best_log2_mul_count, mul_all[best_log2_mul_count], 1, 1);
1509
96.8k
    }
1510
32.2k
}
1511
1512
static int encode_float32_rgb_frame(FFV1Context *f, FFV1SliceContext *sc,
1513
                                    const uint8_t *src[4],
1514
                                    int w, int h, const int stride[4], int ac)
1515
32.2k
{
1516
32.2k
    int x, y, p, i;
1517
32.2k
    const int ring_size = f->context_model ? 3 : 2;
1518
32.2k
    int32_t *sample[4][3];
1519
32.2k
    const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
1520
32.2k
    int bits[4], offset;
1521
32.2k
    int transparency = f->transparency;
1522
1523
32.2k
    ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
1524
1525
32.2k
    sc->run_index = 0;
1526
1527
161k
    for (int p = 0; p < MAX_PLANES; ++p)
1528
129k
        sample[p][2] = sc->sample_buffer32; // dummy to avoid UB pointer arithmetic
1529
1530
32.2k
    memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
1531
32.2k
           (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
1532
1533
1.50M
    for (y = 0; y < h; y++) {
1534
4.78M
        for (i = 0; i < ring_size; i++)
1535
16.5M
            for (p = 0; p < MAX_PLANES; p++)
1536
13.2M
                sample[p][i]= RENAME(sc->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
1537
1538
169M
        for (x = 0; x < w; x++) {
1539
167M
            int b, g, r, av_uninit(a);
1540
167M
            g = sc->bitmap[0][x + w*y];
1541
167M
            b = sc->bitmap[1][x + w*y];
1542
167M
            r = sc->bitmap[2][x + w*y];
1543
167M
            if (transparency)
1544
0
                a = sc->bitmap[3][x + w*y];
1545
1546
167M
            if (sc->slice_coding_mode != 1) {
1547
126M
                b -= g;
1548
126M
                r -= g;
1549
126M
                g += (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
1550
126M
                b += offset;
1551
126M
                r += offset;
1552
126M
            }
1553
1554
167M
            sample[0][0][x] = g;
1555
167M
            sample[1][0][x] = b;
1556
167M
            sample[2][0][x] = r;
1557
167M
            sample[3][0][x] = a;
1558
167M
        }
1559
5.90M
        for (p = 0; p < 3 + transparency; p++) {
1560
4.43M
            int ret;
1561
4.43M
            sample[p][0][-1] = sample[p][1][0  ];
1562
4.43M
            sample[p][1][ w] = sample[p][1][w-1];
1563
4.43M
            ret = encode_line32(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
1564
4.43M
                                bits[p], ac, pass1);
1565
4.43M
            if (ret < 0)
1566
1.78k
                return ret;
1567
4.43M
        }
1568
1.47M
    }
1569
30.4k
    return 0;
1570
32.2k
}
1571
1572
1573
static int encode_slice(AVCodecContext *c, void *arg)
1574
112k
{
1575
112k
    FFV1SliceContext *sc = arg;
1576
112k
    FFV1Context *f   = c->priv_data;
1577
112k
    int width        = sc->slice_width;
1578
112k
    int height       = sc->slice_height;
1579
112k
    int x            = sc->slice_x;
1580
112k
    int y            = sc->slice_y;
1581
112k
    const AVFrame *const p = f->cur_enc_frame;
1582
112k
    const int ps     = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1583
112k
    int ret;
1584
112k
    RangeCoder c_bak = sc->c;
1585
112k
    const int chroma_width  = AV_CEIL_RSHIFT(width,  f->chroma_h_shift);
1586
112k
    const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1587
112k
    const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1588
112k
                                p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1589
112k
                                p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1590
112k
                                p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1591
112k
    int ac = f->ac;
1592
1593
112k
    sc->slice_coding_mode = 0;
1594
112k
    if (f->version > 3 && f->colorspace == 1) {
1595
37.2k
        choose_rct_params(f, sc, planes, p->linesize, width, height);
1596
75.1k
    } else {
1597
75.1k
        sc->slice_rct_by_coef = 1;
1598
75.1k
        sc->slice_rct_ry_coef = 1;
1599
75.1k
    }
1600
1601
114k
retry:
1602
114k
    if (f->key_frame)
1603
31.3k
        ff_ffv1_clear_slice_state(f, sc);
1604
114k
    if (f->version > 2) {
1605
114k
        encode_slice_header(f, sc);
1606
114k
    }
1607
1608
114k
    if (sc->remap) {
1609
      //Both the 16bit and 32bit remap do exactly the same thing but with 16bits we can
1610
      //Implement this using a "histogram" while for 32bit that would be gb sized, thus a more
1611
      //complex implementation sorting pairs is used.
1612
78.1k
      if (f->bits_per_raw_sample != 32) {
1613
45.9k
        if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
1614
20.6k
            const int cx            = x >> f->chroma_h_shift;
1615
20.6k
            const int cy            = y >> f->chroma_v_shift;
1616
1617
            //TODO decide on the order for the encoded remaps and loads. with golomb rice it
1618
            // easier to have all range coded ones together, otherwise it may be nicer to handle each plane as a whole?
1619
1620
20.6k
            load_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
1621
1622
20.6k
            if (f->chroma_planes) {
1623
0
                load_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
1624
0
                load_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 2, 1);
1625
0
            }
1626
20.6k
            if (f->transparency)
1627
0
                load_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 3, 1);
1628
25.2k
        } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
1629
18.2k
            load_plane(f, sc, p->data[0] +           ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
1630
18.2k
            load_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
1631
18.2k
        } else if (f->use32bit) {
1632
7.07k
            load_rgb_frame32(f, sc, planes, width, height, p->linesize);
1633
7.07k
        } else
1634
0
            load_rgb_frame  (f, sc, planes, width, height, p->linesize);
1635
1636
45.9k
        encode_histogram_remap(f, sc);
1637
45.9k
      } else {
1638
32.2k
            load_rgb_float32_frame(f, sc, planes, width, height, p->linesize);
1639
32.2k
            encode_float32_remap(f, sc, planes);
1640
32.2k
      }
1641
78.1k
    }
1642
1643
114k
    if (ac == AC_GOLOMB_RICE) {
1644
43.3k
        sc->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&sc->c, f->version > 2) : 0;
1645
43.3k
        init_put_bits(&sc->pb,
1646
43.3k
                      sc->c.bytestream_start + sc->ac_byte_count,
1647
43.3k
                      sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count);
1648
43.3k
    }
1649
1650
114k
    if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
1651
44.3k
        const int cx            = x >> f->chroma_h_shift;
1652
44.3k
        const int cy            = y >> f->chroma_v_shift;
1653
1654
44.3k
        ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 1, ac);
1655
1656
44.3k
        if (f->chroma_planes) {
1657
15.6k
            ret |= encode_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, 1, ac);
1658
15.6k
            ret |= encode_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 2, 1, ac);
1659
15.6k
        }
1660
44.3k
        if (f->transparency)
1661
5.25k
            ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 3, 1, ac);
1662
70.4k
    } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
1663
19.1k
        ret  = encode_plane(f, sc, p->data[0] +           ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 2, ac);
1664
19.1k
        ret |= encode_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 1, 2, ac);
1665
51.2k
    } else if (f->bits_per_raw_sample == 32) {
1666
32.2k
        ret = encode_float32_rgb_frame(f, sc, planes, width, height, p->linesize, ac);
1667
32.2k
    } else if (f->use32bit) {
1668
13.9k
        ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize, ac);
1669
13.9k
    } else {
1670
5.02k
        ret = encode_rgb_frame(f, sc, planes, width, height, p->linesize, ac);
1671
5.02k
    }
1672
1673
114k
    if (ac != AC_GOLOMB_RICE) {
1674
71.4k
        sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
1675
71.4k
    } else {
1676
43.3k
        flush_put_bits(&sc->pb); // FIXME: nicer padding
1677
43.3k
        sc->ac_byte_count += put_bytes_output(&sc->pb);
1678
43.3k
    }
1679
1680
114k
    if (ret < 0) {
1681
2.41k
        av_assert0(sc->slice_coding_mode == 0);
1682
2.41k
        if (f->version < 4) {
1683
0
            av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1684
0
            return ret;
1685
0
        }
1686
2.41k
        av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1687
2.41k
        ac = 1;
1688
2.41k
        sc->slice_coding_mode = 1;
1689
2.41k
        sc->c = c_bak;
1690
2.41k
        goto retry;
1691
2.41k
    }
1692
1693
112k
    return 0;
1694
114k
}
1695
1696
size_t ff_ffv1_encode_buffer_size(AVCodecContext *avctx)
1697
28.4k
{
1698
28.4k
    FFV1Context *f = avctx->priv_data;
1699
1700
28.4k
    int w = avctx->width  + f->num_h_slices;
1701
28.4k
    int h = avctx->height + f->num_v_slices;
1702
28.4k
    size_t maxsize = w*h * (1 + f->transparency);
1703
28.4k
    if (f->chroma_planes)
1704
16.4k
        maxsize += AV_CEIL_RSHIFT(w, f->chroma_h_shift) * AV_CEIL_RSHIFT(h, f->chroma_v_shift) * 2;
1705
28.4k
    maxsize += f->slice_count * 800; //for slice header
1706
28.4k
    if (f->version > 3) {
1707
20.4k
        maxsize *= f->bits_per_raw_sample + 1;
1708
20.4k
        if (f->remap_mode)
1709
20.4k
            maxsize += f->slice_count * 70000 * (1 + 2*f->chroma_planes + f->transparency);
1710
20.4k
    } else {
1711
8.05k
        maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
1712
8.05k
        maxsize *= 8*(2*f->bits_per_raw_sample + 5);
1713
8.05k
    }
1714
28.4k
    maxsize >>= 3;
1715
28.4k
    maxsize += FF_INPUT_BUFFER_MIN_SIZE;
1716
1717
28.4k
    return maxsize;
1718
28.4k
}
1719
1720
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1721
                        const AVFrame *pict, int *got_packet)
1722
33.3k
{
1723
33.3k
    FFV1Context *f      = avctx->priv_data;
1724
33.3k
    RangeCoder *const c = &f->slices[0].c;
1725
33.3k
    uint8_t keystate    = 128;
1726
33.3k
    uint8_t *buf_p;
1727
33.3k
    int i, ret;
1728
33.3k
    int64_t maxsize;
1729
1730
33.3k
    if(!pict) {
1731
4.82k
        if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1732
0
            int j, k, m;
1733
0
            char *p   = avctx->stats_out;
1734
0
            char *end = p + STATS_OUT_SIZE;
1735
1736
0
            memset(f->rc_stat, 0, sizeof(f->rc_stat));
1737
0
            for (i = 0; i < f->quant_table_count; i++)
1738
0
                memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1739
1740
0
            av_assert0(f->slice_count == f->max_slice_count);
1741
0
            for (j = 0; j < f->slice_count; j++) {
1742
0
                const FFV1SliceContext *sc = &f->slices[j];
1743
0
                for (i = 0; i < 256; i++) {
1744
0
                    f->rc_stat[i][0] += sc->rc_stat[i][0];
1745
0
                    f->rc_stat[i][1] += sc->rc_stat[i][1];
1746
0
                }
1747
0
                for (i = 0; i < f->quant_table_count; i++) {
1748
0
                    for (k = 0; k < f->context_count[i]; k++)
1749
0
                        for (m = 0; m < 32; m++) {
1750
0
                            f->rc_stat2[i][k][m][0] += sc->rc_stat2[i][k][m][0];
1751
0
                            f->rc_stat2[i][k][m][1] += sc->rc_stat2[i][k][m][1];
1752
0
                        }
1753
0
                }
1754
0
            }
1755
1756
0
            for (j = 0; j < 256; j++) {
1757
0
                snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1758
0
                        f->rc_stat[j][0], f->rc_stat[j][1]);
1759
0
                p += strlen(p);
1760
0
            }
1761
0
            snprintf(p, end - p, "\n");
1762
1763
0
            for (i = 0; i < f->quant_table_count; i++) {
1764
0
                for (j = 0; j < f->context_count[i]; j++)
1765
0
                    for (m = 0; m < 32; m++) {
1766
0
                        snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1767
0
                                f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1768
0
                        p += strlen(p);
1769
0
                    }
1770
0
            }
1771
0
            snprintf(p, end - p, "%d\n", f->gob_count);
1772
0
        }
1773
4.82k
        return 0;
1774
4.82k
    }
1775
1776
    /* Maximum packet size */
1777
28.4k
    maxsize = ff_ffv1_encode_buffer_size(avctx);
1778
1779
28.4k
    if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1780
0
        FFV1Context *f = avctx->priv_data;
1781
0
        if (!f->maxsize_warned) {
1782
0
            av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1783
0
            f->maxsize_warned++;
1784
0
        }
1785
0
        maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
1786
0
    }
1787
1788
28.4k
    if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
1789
0
        return ret;
1790
1791
28.4k
    ff_init_range_encoder(c, pkt->data, pkt->size);
1792
28.4k
    ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1793
1794
28.4k
    f->cur_enc_frame = pict;
1795
1796
28.4k
    if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
1797
6.09k
        put_rac(c, &keystate, 1);
1798
6.09k
        f->key_frame = 1;
1799
6.09k
        f->gob_count++;
1800
6.09k
        write_header(f);
1801
22.3k
    } else {
1802
22.3k
        put_rac(c, &keystate, 0);
1803
22.3k
        f->key_frame = 0;
1804
22.3k
    }
1805
1806
28.4k
    if (f->ac == AC_RANGE_CUSTOM_TAB) {
1807
14.9k
        int i;
1808
3.83M
        for (i = 1; i < 256; i++) {
1809
3.81M
            c->one_state[i]        = f->state_transition[i];
1810
3.81M
            c->zero_state[256 - i] = 256 - c->one_state[i];
1811
3.81M
        }
1812
14.9k
    }
1813
1814
140k
    for (i = 0; i < f->slice_count; i++) {
1815
112k
        FFV1SliceContext *sc = &f->slices[i];
1816
112k
        uint8_t *start  = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1817
112k
        int len         = pkt->size / f->slice_count;
1818
112k
        if (i) {
1819
83.9k
            ff_init_range_encoder(&sc->c, start, len);
1820
83.9k
        } else {
1821
28.4k
            av_assert0(sc->c.bytestream_end >= sc->c.bytestream_start + len);
1822
28.4k
            av_assert0(sc->c.bytestream < sc->c.bytestream_start + len);
1823
28.4k
            sc->c.bytestream_end = sc->c.bytestream_start + len;
1824
28.4k
        }
1825
112k
    }
1826
28.4k
    avctx->execute(avctx, encode_slice, f->slices, NULL,
1827
28.4k
                   f->slice_count, sizeof(*f->slices));
1828
1829
28.4k
    buf_p = pkt->data;
1830
140k
    for (i = 0; i < f->slice_count; i++) {
1831
112k
        FFV1SliceContext *sc = &f->slices[i];
1832
112k
        int bytes = sc->ac_byte_count;
1833
112k
        if (i > 0 || f->version > 2) {
1834
112k
            av_assert0(bytes < pkt->size / f->slice_count);
1835
112k
            memmove(buf_p, sc->c.bytestream_start, bytes);
1836
112k
            av_assert0(bytes < (1 << 24));
1837
112k
            AV_WB24(buf_p + bytes, bytes);
1838
112k
            bytes += 3;
1839
112k
        }
1840
112k
        if (f->ec) {
1841
34.9k
            unsigned v;
1842
34.9k
            buf_p[bytes++] = 0;
1843
34.9k
            v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, buf_p, bytes) ^ (f->crcref ? 0x8CD88196 : 0);
1844
34.9k
            AV_WL32(buf_p + bytes, v);
1845
34.9k
            bytes += 4;
1846
34.9k
        }
1847
112k
        buf_p += bytes;
1848
112k
    }
1849
1850
28.4k
    if (avctx->flags & AV_CODEC_FLAG_PASS1)
1851
0
        avctx->stats_out[0] = '\0';
1852
1853
28.4k
    f->picture_number++;
1854
28.4k
    pkt->size   = buf_p - pkt->data;
1855
28.4k
    pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1856
28.4k
    *got_packet = 1;
1857
1858
28.4k
    return 0;
1859
28.4k
}
1860
1861
static av_cold int encode_close(AVCodecContext *avctx)
1862
4.84k
{
1863
4.84k
    FFV1Context *const s = avctx->priv_data;
1864
1865
29.7k
    for (int j = 0; j < s->max_slice_count; j++) {
1866
24.9k
        FFV1SliceContext *sc = &s->slices[j];
1867
1868
124k
        for(int p = 0; p<4; p++) {
1869
99.7k
            av_freep(&sc->unit[p]);
1870
99.7k
            av_freep(&sc->bitmap[p]);
1871
99.7k
        }
1872
24.9k
    }
1873
1874
4.84k
    av_freep(&avctx->stats_out);
1875
4.84k
    ff_ffv1_close(s);
1876
1877
4.84k
    return 0;
1878
4.84k
}
1879
1880
#define OFFSET(x) offsetof(FFV1Context, x)
1881
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1882
static const AVOption options[] = {
1883
    { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },
1884
    { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1885
            { .i64 = 0 }, -2, 2, VE, .unit = "coder" },
1886
        { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1887
            { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1888
        { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1889
            { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1890
        { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1891
            { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1892
        { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
1893
            { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "coder" },
1894
    { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1895
            { .i64 = 0 }, 0, 1, VE },
1896
    { "qtable", "Quantization table", OFFSET(qtable), AV_OPT_TYPE_INT,
1897
            { .i64 = -1 }, -1, 2, VE , .unit = "qtable"},
1898
        { "default", NULL, 0, AV_OPT_TYPE_CONST,
1899
            { .i64 = QTABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1900
        { "8bit", NULL, 0, AV_OPT_TYPE_CONST,
1901
            { .i64 = QTABLE_8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1902
        { "greater8bit", NULL, 0, AV_OPT_TYPE_CONST,
1903
            { .i64 = QTABLE_GT8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
1904
    { "remap_mode", "Remap Mode", OFFSET(remap_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE, .unit = "remap_mode" },
1905
        { "auto", "Automatic", 0, AV_OPT_TYPE_CONST,
1906
            { .i64 = -1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
1907
        { "off", "Disabled", 0, AV_OPT_TYPE_CONST,
1908
            { .i64 =  0 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
1909
        { "dualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST,
1910
            { .i64 =  1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
1911
        { "flipdualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST,
1912
            { .i64 =  2 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
1913
    { "remap_optimizer", "Remap Optimizer", OFFSET(remap_optimizer), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 5, VE, .unit = "remap_optimizer" },
1914
1915
    { NULL }
1916
};
1917
1918
static const AVClass ffv1_class = {
1919
    .class_name = "ffv1 encoder",
1920
    .item_name  = av_default_item_name,
1921
    .option     = options,
1922
    .version    = LIBAVUTIL_VERSION_INT,
1923
};
1924
1925
const FFCodec ff_ffv1_encoder = {
1926
    .p.name         = "ffv1",
1927
    CODEC_LONG_NAME("FFmpeg video codec #1"),
1928
    .p.type         = AVMEDIA_TYPE_VIDEO,
1929
    .p.id           = AV_CODEC_ID_FFV1,
1930
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1931
                      AV_CODEC_CAP_SLICE_THREADS |
1932
                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1933
    .priv_data_size = sizeof(FFV1Context),
1934
    .init           = encode_init_internal,
1935
    FF_CODEC_ENCODE_CB(encode_frame),
1936
    .close          = encode_close,
1937
    CODEC_PIXFMTS(
1938
        AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUVA420P,  AV_PIX_FMT_YUVA422P,  AV_PIX_FMT_YUV444P,
1939
        AV_PIX_FMT_YUVA444P,  AV_PIX_FMT_YUV440P,   AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV411P,
1940
        AV_PIX_FMT_YUV410P,   AV_PIX_FMT_0RGB32,    AV_PIX_FMT_RGB32,     AV_PIX_FMT_YUV420P16,
1941
        AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,
1942
        AV_PIX_FMT_YUV420P9,  AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
1943
        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
1944
        AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
1945
        AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12,
1946
        AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
1947
        AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
1948
        AV_PIX_FMT_GRAY16,    AV_PIX_FMT_GRAY8,     AV_PIX_FMT_GBRP9,     AV_PIX_FMT_GBRP10,
1949
        AV_PIX_FMT_GBRP12,    AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRAP14,
1950
        AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
1951
        AV_PIX_FMT_YA8,
1952
        AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
1953
        AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48,
1954
        AV_PIX_FMT_GBRAP16, AV_PIX_FMT_RGBA64,
1955
        AV_PIX_FMT_GRAY9,
1956
        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
1957
        AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
1958
        AV_PIX_FMT_YAF16,
1959
        AV_PIX_FMT_GRAYF16,
1960
        AV_PIX_FMT_GBRPF16, AV_PIX_FMT_GBRPF32),
1961
    .color_ranges   = AVCOL_RANGE_MPEG,
1962
    .p.priv_class   = &ffv1_class,
1963
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,
1964
};