Coverage Report

Created: 2026-05-16 07:49

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
527M
{
191
527M
    int i;
192
193
527M
#define put_rac(C, S, B)                        \
194
1.83G
    do {                                        \
195
1.83G
        if (rc_stat) {                          \
196
0
            rc_stat[*(S)][B]++;                 \
197
0
            rc_stat2[(S) - state][B]++;         \
198
0
        }                                       \
199
1.83G
        put_rac(C, S, B);                       \
200
1.83G
    } while (0)
201
202
527M
    if (v) {
203
84.6M
        const unsigned a = is_signed ? FFABS(v) : v;
204
84.6M
        const int e = av_log2(a);
205
84.6M
        put_rac(c, state + 0, 0);
206
84.6M
        if (e <= 9) {
207
335M
            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
335M
            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.3M
                put_rac(c, state + 11 + e, v < 0);  // 11..21
216
62.1M
        } else {
217
338M
            for (i = 0; i < e; i++)
218
316M
                put_rac(c, state + 1 + FFMIN(i, 9), 1);  // 1..10
219
22.5M
            put_rac(c, state + 1 + 9, 0);
220
221
338M
            for (i = e - 1; i >= 0; i--)
222
316M
                put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1);  // 22..31
223
224
22.5M
            if (is_signed)
225
8.42M
                put_rac(c, state + 11 + 10, v < 0);  // 11..21
226
22.5M
        }
227
442M
    } else {
228
442M
        put_rac(c, state + 0, 1);
229
442M
    }
230
527M
#undef put_rac
231
527M
}
232
233
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
234
                                   int v, int is_signed)
235
2.76M
{
236
2.76M
    put_symbol_inline(c, state, v, is_signed, NULL, NULL);
237
2.76M
}
238
239
240
static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
241
                                  int v, int bits)
242
22.1M
{
243
22.1M
    int i, k, code;
244
22.1M
    v = fold(v - state->bias, bits);
245
246
22.1M
    i = state->count;
247
22.1M
    k = 0;
248
125M
    while (i < state->error_sum) { // FIXME: optimize
249
103M
        k++;
250
103M
        i += i;
251
103M
    }
252
253
22.1M
    av_assert2(k <= 16);
254
255
22.1M
    code = v ^ ((2 * state->drift + state->count) >> 31);
256
257
22.1M
    ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
258
22.1M
            state->bias, state->error_sum, state->drift, state->count, k);
259
22.1M
    set_sr_golomb(pb, code, k, 12, bits);
260
261
22.1M
    update_vlc_state(state, v);
262
22.1M
}
263
264
5.02k
#define TYPE int16_t
265
445M
#define RENAME(name) name
266
#include "ffv1enc_template.c"
267
#undef TYPE
268
#undef RENAME
269
270
12.6k
#define TYPE int32_t
271
50.4k
#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
120k
{
278
120k
    int x, y, i, ret;
279
120k
    const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
280
120k
    const int ring_size = f->context_model ? 3 : 2;
281
120k
    int16_t *sample[3];
282
120k
    sc->run_index = 0;
283
284
120k
    sample[2] = sc->sample_buffer; // dummy to avoid UB pointer arithmetic
285
286
120k
    memset(sc->sample_buffer, 0, ring_size * (w + 6) * sizeof(*sc->sample_buffer));
287
288
6.62M
    for (y = 0; y < h; y++) {
289
21.8M
        for (i = 0; i < ring_size; i++)
290
15.2M
            sample[i] = sc->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
291
292
6.50M
        sample[0][-1]= sample[1][0  ];
293
6.50M
        sample[1][ w]= sample[1][w-1];
294
295
6.50M
        if (f->bits_per_raw_sample <= 8) {
296
36.8M
            for (x = 0; x < w; x++)
297
32.6M
                sample[0][x] = src[x * pixel_stride + stride * y];
298
4.19M
            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.19M
            if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, 8, ac, pass1)) < 0)
303
0
                return ret;
304
4.19M
        } else {
305
2.31M
            if (f->packed_at_lsb) {
306
20.3M
                for (x = 0; x < w; x++) {
307
19.1M
                    sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride];
308
19.1M
                }
309
1.18M
            } else {
310
43.3M
                for (x = 0; x < w; x++) {
311
42.2M
                    sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample);
312
42.2M
                }
313
1.12M
            }
314
2.31M
            if (sc->remap)
315
30.3M
                for (x = 0; x < w; x++)
316
29.3M
                    sample[0][x] = sc->fltmap[remap_index][ (uint16_t)sample[0][x] ];
317
318
2.31M
            if((ret = encode_line(f, sc, f->avctx, w, sample, plane_index, f->bits_per_raw_sample, ac, pass1)) < 0)
319
463
                return ret;
320
2.31M
        }
321
6.50M
    }
322
119k
    return 0;
323
120k
}
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.7k
{
329
57.7k
    int x, y;
330
331
57.7k
    memset(sc->fltmap[remap_index], 0, 65536 * sizeof(*sc->fltmap[remap_index]));
332
333
1.12M
    for (y = 0; y < h; y++) {
334
1.07M
        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.07M
        } else {
338
1.07M
            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.07M
            } else {
342
34.1M
                for (x = 0; x < w; x++)
343
33.1M
                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample) ] = 1;
344
1.07M
            }
345
1.07M
        }
346
1.07M
    }
347
57.7k
}
348
349
static void write_quant_table(RangeCoder *c, int16_t *quant_table)
350
48.9k
{
351
48.9k
    int last = 0;
352
48.9k
    int i;
353
48.9k
    uint8_t state[CONTEXT_SIZE];
354
48.9k
    memset(state, 128, sizeof(state));
355
356
6.26M
    for (i = 1; i < MAX_QUANT_TABLE_SIZE/2; i++)
357
6.21M
        if (quant_table[i] != quant_table[i - 1]) {
358
130k
            put_symbol(c, state, i - last - 1, 0);
359
130k
            last = i;
360
130k
        }
361
48.9k
    put_symbol(c, state, i - last - 1, 0);
362
48.9k
}
363
364
static void write_quant_tables(RangeCoder *c,
365
                               int16_t quant_table[MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE])
366
9.78k
{
367
9.78k
    int i;
368
58.7k
    for (i = 0; i < 5; i++)
369
48.9k
        write_quant_table(c, quant_table[i]);
370
9.78k
}
371
372
static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
373
                            int nb_contexts)
374
9.78k
{
375
9.78k
    if (!initial_state)
376
0
        return 0;
377
28.1M
    for (int i = 0; i < nb_contexts; i++)
378
928M
        for (int j = 0; j < CONTEXT_SIZE; j++)
379
900M
            if (initial_state[i][j] != 128)
380
0
                return 1;
381
9.78k
    return 0;
382
9.78k
}
383
384
static void write_header(FFV1Context *f)
385
6.17k
{
386
6.17k
    uint8_t state[CONTEXT_SIZE];
387
6.17k
    int i, j;
388
6.17k
    RangeCoder *const c = &f->slices[0].c;
389
390
6.17k
    memset(state, 128, sizeof(state));
391
392
6.17k
    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.17k
    } 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.17k
}
430
431
static void set_micro_version(FFV1Context *f)
432
4.90k
{
433
4.90k
    f->combined_version = f->version << 16;
434
4.90k
    if (f->version > 2) {
435
4.90k
        if (f->version == 3) {
436
1.20k
            f->micro_version = 4;
437
3.69k
        } else if (f->version == 4) {
438
3.69k
            f->micro_version = 9;
439
3.69k
        } else
440
0
            av_assert0(0);
441
442
4.90k
        f->combined_version += f->micro_version;
443
4.90k
    } else
444
0
        av_assert0(f->micro_version == 0);
445
4.90k
}
446
447
av_cold int ff_ffv1_write_extradata(AVCodecContext *avctx)
448
4.89k
{
449
4.89k
    FFV1Context *f = avctx->priv_data;
450
451
4.89k
    RangeCoder c;
452
4.89k
    uint8_t state[CONTEXT_SIZE];
453
4.89k
    int i, j, k;
454
4.89k
    uint8_t state2[32][CONTEXT_SIZE];
455
4.89k
    unsigned v;
456
457
4.89k
    memset(state2, 128, sizeof(state2));
458
4.89k
    memset(state, 128, sizeof(state));
459
460
4.89k
    f->avctx->extradata_size = 10000 + 4 +
461
4.89k
                                    (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
462
4.89k
    f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
463
4.89k
    if (!f->avctx->extradata)
464
0
        return AVERROR(ENOMEM);
465
4.89k
    ff_init_range_encoder(&c, f->avctx->extradata, f->avctx->extradata_size);
466
4.89k
    ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
467
468
4.89k
    put_symbol(&c, state, f->version, 0);
469
4.89k
    if (f->version > 2)
470
4.89k
        put_symbol(&c, state, f->micro_version, 0);
471
472
4.89k
    put_symbol(&c, state, f->ac, 0);
473
4.89k
    if (f->ac == AC_RANGE_CUSTOM_TAB)
474
839k
        for (i = 1; i < 256; i++)
475
836k
            put_symbol(&c, state, f->state_transition[i] - c.one_state[i], 1);
476
477
4.89k
    put_symbol(&c, state, f->colorspace, 0); // YUV cs type
478
4.89k
    put_symbol(&c, state, f->bits_per_raw_sample, 0);
479
4.89k
    put_rac(&c, state, f->chroma_planes);
480
4.89k
    put_symbol(&c, state, f->chroma_h_shift, 0);
481
4.89k
    put_symbol(&c, state, f->chroma_v_shift, 0);
482
4.89k
    put_rac(&c, state, f->transparency);
483
4.89k
    put_symbol(&c, state, f->num_h_slices - 1, 0);
484
4.89k
    put_symbol(&c, state, f->num_v_slices - 1, 0);
485
486
4.89k
    put_symbol(&c, state, f->quant_table_count, 0);
487
14.6k
    for (i = 0; i < f->quant_table_count; i++)
488
9.78k
        write_quant_tables(&c, f->quant_tables[i]);
489
490
14.6k
    for (i = 0; i < f->quant_table_count; i++) {
491
9.78k
        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.78k
        } else {
500
9.78k
            put_rac(&c, state, 0);
501
9.78k
        }
502
9.78k
    }
503
504
4.89k
    if (f->version > 2) {
505
4.89k
        put_symbol(&c, state, f->ec, 0);
506
4.89k
        put_symbol(&c, state, f->intra = (f->avctx->gop_size < 2), 0);
507
4.89k
    }
508
509
4.89k
    f->avctx->extradata_size = ff_rac_terminate(&c, 0);
510
4.89k
    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.89k
    AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
512
4.89k
    f->avctx->extradata_size += 4;
513
514
4.89k
    return 0;
515
4.89k
}
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.90k
{
568
4.90k
    FFV1Context *s = avctx->priv_data;
569
4.90k
    int plane_count = 1 + 2*s->chroma_planes + s->transparency;
570
4.90k
    int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
571
4.90k
    int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
572
4.90k
    s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
573
4.90k
    s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
574
5.77k
    for (; s->num_v_slices <= 32; s->num_v_slices++) {
575
17.3k
        for (s->num_h_slices = s->num_v_slices; s->num_h_slices <= 2*s->num_v_slices; s->num_h_slices++) {
576
16.4k
            int maxw = (avctx->width  + s->num_h_slices - 1) / s->num_h_slices;
577
16.4k
            int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
578
16.4k
            if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
579
8.02k
                continue;
580
8.45k
            if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
581
0
                continue;
582
8.45k
            if (s->version < 4)
583
4.75k
                if (  ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
584
3.86k
                    ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
585
3.55k
                    continue;
586
4.90k
            if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES)
587
0
                return 0;
588
4.90k
            if (maxw*maxh > 360*288)
589
11
                continue;
590
4.89k
            if (!avctx->slices)
591
4.89k
                return 0;
592
4.89k
        }
593
5.76k
    }
594
14
    av_log(avctx, AV_LOG_ERROR,
595
14
           "Unsupported number %d of slices requested, please specify a "
596
14
           "supported number with -slices (ex:4,6,9,12,16, ...)\n",
597
14
           avctx->slices);
598
14
    return AVERROR(ENOSYS);
599
4.90k
}
600
601
av_cold int ff_ffv1_encode_init(AVCodecContext *avctx)
602
4.90k
{
603
4.90k
    FFV1Context *s = avctx->priv_data;
604
4.90k
    int i, j, k, m, ret;
605
606
4.90k
    if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
607
4.90k
        avctx->slices > 1)
608
0
        s->version = FFMAX(s->version, 2);
609
610
4.90k
    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.90k
    if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
617
0
        s->version = FFMAX(s->version, 2);
618
619
4.90k
    if (avctx->level <= 0 && s->version == 2) {
620
0
        s->version = 3;
621
0
    }
622
4.90k
    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.90k
    } else if (s->version < 3)
629
1.20k
        s->version = 3;
630
631
4.90k
    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.90k
    if (s->ec == 1)
642
1.64k
        s->version = FFMAX(s->version, 3);
643
4.90k
    if (s->ec == 2) {
644
0
        s->version = FFMAX(s->version, 4);
645
0
        s->crcref = 0x7a8c4079;
646
0
    }
647
648
4.90k
    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.90k
    if (s->ac == AC_RANGE_CUSTOM_TAB) {
654
841k
        for (i = 1; i < 256; i++)
655
838k
            s->state_transition[i] = ver2_state[i];
656
3.28k
    } else {
657
1.61k
        RangeCoder c;
658
1.61k
        ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
659
414k
        for (i = 1; i < 256; i++)
660
412k
            s->state_transition[i] = c.one_state[i];
661
1.61k
    }
662
663
1.26M
    for (i = 0; i < 256; i++) {
664
1.25M
        s->quant_table_count = 2;
665
1.25M
        if ((s->qtable == -1 && s->bits_per_raw_sample <= 8) || s->qtable == 1) {
666
147k
            s->quant_tables[0][0][i]=           quant11[i];
667
147k
            s->quant_tables[0][1][i]=        11*quant11[i];
668
147k
            s->quant_tables[0][2][i]=     11*11*quant11[i];
669
147k
            s->quant_tables[1][0][i]=           quant11[i];
670
147k
            s->quant_tables[1][1][i]=        11*quant11[i];
671
147k
            s->quant_tables[1][2][i]=     11*11*quant5 [i];
672
147k
            s->quant_tables[1][3][i]=   5*11*11*quant5 [i];
673
147k
            s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
674
147k
            s->context_count[0] = (11 * 11 * 11        + 1) / 2;
675
147k
            s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
676
1.10M
        } else {
677
1.10M
            s->quant_tables[0][0][i]=           quant9_10bit[i];
678
1.10M
            s->quant_tables[0][1][i]=         9*quant9_10bit[i];
679
1.10M
            s->quant_tables[0][2][i]=       9*9*quant9_10bit[i];
680
1.10M
            s->quant_tables[1][0][i]=           quant9_10bit[i];
681
1.10M
            s->quant_tables[1][1][i]=         9*quant9_10bit[i];
682
1.10M
            s->quant_tables[1][2][i]=       9*9*quant5_10bit[i];
683
1.10M
            s->quant_tables[1][3][i]=     5*9*9*quant5_10bit[i];
684
1.10M
            s->quant_tables[1][4][i]=   5*5*9*9*quant5_10bit[i];
685
1.10M
            s->context_count[0] = (9 * 9 * 9         + 1) / 2;
686
1.10M
            s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
687
1.10M
        }
688
1.25M
    }
689
690
4.90k
    if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
691
0
        return ret;
692
693
4.90k
    if (!s->transparency)
694
4.05k
        s->plane_count = 2;
695
4.90k
    if (!s->chroma_planes && s->version > 3)
696
447
        s->plane_count--;
697
698
4.90k
    s->picture_number = 0;
699
700
4.90k
    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.90k
    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.90k
    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.90k
    set_micro_version(s);
797
798
4.90k
    return 0;
799
4.90k
}
800
801
av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
802
                                            enum AVPixelFormat pix_fmt)
803
4.90k
{
804
4.90k
    FFV1Context *s = avctx->priv_data;
805
4.90k
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
806
807
4.90k
    s->plane_count = 3;
808
4.90k
    switch(pix_fmt) {
809
3
    case AV_PIX_FMT_GRAY9:
810
8
    case AV_PIX_FMT_YUV444P9:
811
9
    case AV_PIX_FMT_YUV422P9:
812
12
    case AV_PIX_FMT_YUV420P9:
813
14
    case AV_PIX_FMT_YUVA444P9:
814
19
    case AV_PIX_FMT_YUVA422P9:
815
30
    case AV_PIX_FMT_YUVA420P9:
816
30
        if (!avctx->bits_per_raw_sample)
817
30
            s->bits_per_raw_sample = 9;
818
30
        av_fallthrough;
819
32
    case AV_PIX_FMT_GRAY10:
820
35
    case AV_PIX_FMT_YUV444P10:
821
40
    case AV_PIX_FMT_YUV440P10:
822
43
    case AV_PIX_FMT_YUV420P10:
823
46
    case AV_PIX_FMT_YUV422P10:
824
59
    case AV_PIX_FMT_YUVA444P10:
825
62
    case AV_PIX_FMT_YUVA422P10:
826
69
    case AV_PIX_FMT_YUVA420P10:
827
69
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
828
39
            s->bits_per_raw_sample = 10;
829
69
        av_fallthrough;
830
82
    case AV_PIX_FMT_GRAY12:
831
85
    case AV_PIX_FMT_YUV444P12:
832
91
    case AV_PIX_FMT_YUV440P12:
833
93
    case AV_PIX_FMT_YUV420P12:
834
96
    case AV_PIX_FMT_YUV422P12:
835
99
    case AV_PIX_FMT_YUVA444P12:
836
102
    case AV_PIX_FMT_YUVA422P12:
837
102
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
838
33
            s->bits_per_raw_sample = 12;
839
102
        av_fallthrough;
840
112
    case AV_PIX_FMT_GRAY14:
841
119
    case AV_PIX_FMT_YUV444P14:
842
142
    case AV_PIX_FMT_YUV420P14:
843
147
    case AV_PIX_FMT_YUV422P14:
844
147
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
845
45
            s->bits_per_raw_sample = 14;
846
147
        s->packed_at_lsb = 1;
847
147
        av_fallthrough;
848
178
    case AV_PIX_FMT_GRAY16:
849
178
    case AV_PIX_FMT_P016:
850
178
    case AV_PIX_FMT_P216:
851
178
    case AV_PIX_FMT_P416:
852
186
    case AV_PIX_FMT_YUV444P16:
853
195
    case AV_PIX_FMT_YUV422P16:
854
206
    case AV_PIX_FMT_YUV420P16:
855
223
    case AV_PIX_FMT_YUVA444P16:
856
224
    case AV_PIX_FMT_YUVA422P16:
857
237
    case AV_PIX_FMT_YUVA420P16:
858
423
    case AV_PIX_FMT_GRAYF16:
859
684
    case AV_PIX_FMT_YAF16:
860
684
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
861
537
            s->bits_per_raw_sample = 16;
862
537
        } else if (!s->bits_per_raw_sample) {
863
0
            s->bits_per_raw_sample = avctx->bits_per_raw_sample;
864
0
        }
865
684
        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
684
        s->version = FFMAX(s->version, 1);
870
684
        av_fallthrough;
871
694
    case AV_PIX_FMT_GRAY8:
872
718
    case AV_PIX_FMT_YA8:
873
718
    case AV_PIX_FMT_NV12:
874
718
    case AV_PIX_FMT_NV16:
875
718
    case AV_PIX_FMT_NV24:
876
736
    case AV_PIX_FMT_YUV444P:
877
748
    case AV_PIX_FMT_YUV440P:
878
760
    case AV_PIX_FMT_YUV422P:
879
859
    case AV_PIX_FMT_YUV420P:
880
889
    case AV_PIX_FMT_YUV411P:
881
933
    case AV_PIX_FMT_YUV410P:
882
941
    case AV_PIX_FMT_YUVA444P:
883
950
    case AV_PIX_FMT_YUVA422P:
884
996
    case AV_PIX_FMT_YUVA420P:
885
996
        s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
886
996
        s->colorspace = 0;
887
996
        s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
888
996
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
889
312
            s->bits_per_raw_sample = 8;
890
684
        else if (!s->bits_per_raw_sample)
891
0
            s->bits_per_raw_sample = 8;
892
996
        break;
893
192
    case AV_PIX_FMT_RGB32:
894
192
        s->colorspace = 1;
895
192
        s->transparency = 1;
896
192
        s->chroma_planes = 1;
897
192
        s->bits_per_raw_sample = 8;
898
192
        break;
899
50
    case AV_PIX_FMT_RGBA64:
900
50
        s->colorspace = 1;
901
50
        s->transparency = 1;
902
50
        s->chroma_planes = 1;
903
50
        s->bits_per_raw_sample = 16;
904
50
        s->use32bit = 1;
905
50
        s->version = FFMAX(s->version, 1);
906
50
        break;
907
24
    case AV_PIX_FMT_RGB48:
908
24
        s->colorspace = 1;
909
24
        s->chroma_planes = 1;
910
24
        s->bits_per_raw_sample = 16;
911
24
        s->use32bit = 1;
912
24
        s->version = FFMAX(s->version, 1);
913
24
        break;
914
0
    case AV_PIX_FMT_GBRP:
915
73
    case AV_PIX_FMT_0RGB32:
916
73
        s->colorspace = 1;
917
73
        s->chroma_planes = 1;
918
73
        s->bits_per_raw_sample = 8;
919
73
        break;
920
7
    case AV_PIX_FMT_GBRP9:
921
7
        if (!avctx->bits_per_raw_sample)
922
7
            s->bits_per_raw_sample = 9;
923
7
        av_fallthrough;
924
7
    case AV_PIX_FMT_X2BGR10:
925
7
    case AV_PIX_FMT_X2RGB10:
926
15
    case AV_PIX_FMT_GBRP10:
927
52
    case AV_PIX_FMT_GBRAP10:
928
52
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
929
45
            s->bits_per_raw_sample = 10;
930
52
        av_fallthrough;
931
64
    case AV_PIX_FMT_GBRP12:
932
100
    case AV_PIX_FMT_GBRAP12:
933
100
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
934
48
            s->bits_per_raw_sample = 12;
935
100
        av_fallthrough;
936
137
    case AV_PIX_FMT_GBRP14:
937
200
    case AV_PIX_FMT_GBRAP14:
938
200
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
939
100
            s->bits_per_raw_sample = 14;
940
200
        av_fallthrough;
941
269
    case AV_PIX_FMT_GBRP16:
942
321
    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
847
            s->bits_per_raw_sample = 16;
947
1.04k
        av_fallthrough;
948
3.57k
    case AV_PIX_FMT_GBRPF32:
949
3.57k
    case AV_PIX_FMT_GBRAPF32:
950
3.57k
        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
951
2.52k
            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.57k
        s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
955
3.57k
        s->colorspace = 1;
956
3.57k
        s->chroma_planes = 1;
957
3.57k
        if (s->bits_per_raw_sample >= 16) {
958
3.37k
            s->use32bit = 1;
959
3.37k
        }
960
3.57k
        s->version = FFMAX(s->version, 1);
961
3.57k
        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.90k
    }
967
4.90k
    s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
968
4.90k
    if (s->flt || s->remap_mode > 0)
969
3.69k
        s->version = FFMAX(s->version, 4);
970
4.90k
    av_assert0(s->bits_per_raw_sample >= 8);
971
972
4.90k
    if (s->remap_mode < 0)
973
4.90k
        s->remap_mode = s->flt ? 2 : 0;
974
4.90k
    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.90k
    if (s->remap_mode == 2 &&
979
3.69k
        !((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.90k
    return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
985
4.90k
}
986
987
static av_cold int encode_init_internal(AVCodecContext *avctx)
988
4.90k
{
989
4.90k
    int ret;
990
4.90k
    FFV1Context *s = avctx->priv_data;
991
992
4.90k
    if ((ret = ff_ffv1_common_init(avctx, s)) < 0)
993
0
        return ret;
994
995
4.90k
    if (s->ac == 1) // Compatibility with common command line usage
996
813
        s->ac = AC_RANGE_CUSTOM_TAB;
997
4.09k
    else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
998
559
        s->ac = AC_RANGE_DEFAULT_TAB;
999
1000
4.90k
    ret = ff_ffv1_encode_setup_plane_info(avctx, avctx->pix_fmt);
1001
4.90k
    if (ret < 0)
1002
0
        return ret;
1003
1004
4.90k
    if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8) && !s->remap_mode) {
1005
632
        if (s->ac == AC_GOLOMB_RICE) {
1006
190
            av_log(avctx, AV_LOG_INFO,
1007
190
                    "high bits_per_raw_sample, forcing range coder\n");
1008
190
            s->ac = AC_RANGE_CUSTOM_TAB;
1009
190
        }
1010
632
    }
1011
1012
1013
4.90k
    ret = ff_ffv1_encode_init(avctx);
1014
4.90k
    if (ret < 0)
1015
2
        return ret;
1016
1017
4.90k
    if (s->version > 1) {
1018
4.90k
        if ((ret = ff_ffv1_encode_determine_slices(avctx)) < 0)
1019
14
            return ret;
1020
1021
4.89k
        if ((ret = ff_ffv1_write_extradata(avctx)) < 0)
1022
0
            return ret;
1023
4.89k
    }
1024
1025
4.89k
    if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
1026
0
        return ret;
1027
4.89k
    s->slice_count = s->max_slice_count;
1028
1029
30.0k
    for (int j = 0; j < s->slice_count; j++) {
1030
25.2k
        FFV1SliceContext *sc = &s->slices[j];
1031
1032
79.6k
        for (int i = 0; i < s->plane_count; i++) {
1033
54.4k
            PlaneContext *const p = &s->slices[j].plane[i];
1034
1035
54.4k
            p->quant_table_index = s->context_model;
1036
54.4k
            p->context_count     = s->context_count[p->quant_table_index];
1037
54.4k
        }
1038
25.2k
        av_assert0(s->remap_mode >= 0);
1039
25.2k
        if (s->remap_mode) {
1040
51.7k
            for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) {
1041
38.2k
                if (s->bits_per_raw_sample == 32) {
1042
28.4k
                    sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
1043
28.4k
                    if (!sc->unit[p])
1044
0
                        return AVERROR(ENOMEM);
1045
28.4k
                    sc->bitmap[p] = av_malloc_array(sc->slice_width * sc->slice_height, sizeof(*sc->bitmap[p]));
1046
28.4k
                    if (!sc->bitmap[p])
1047
0
                        return AVERROR(ENOMEM);
1048
28.4k
                } else {
1049
9.85k
                    sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
1050
9.85k
                    if (!sc->fltmap[p])
1051
0
                        return AVERROR(ENOMEM);
1052
9.85k
                }
1053
38.2k
            }
1054
13.4k
        }
1055
1056
25.2k
        ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
1057
1058
25.2k
        s->slices[j].remap = s->remap_mode;
1059
25.2k
    }
1060
1061
4.89k
    if ((ret = ff_ffv1_init_slices_state(s)) < 0)
1062
0
        return ret;
1063
1064
4.89k
#define STATS_OUT_SIZE 1024 * 1024 * 6
1065
4.89k
    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.89k
    return 0;
1081
4.89k
}
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
339k
    for (j=0; j<f->plane_count; j++) {
1095
224k
        put_symbol(c, state, sc->plane[j].quant_table_index, 0);
1096
224k
        av_assert0(sc->plane[j].quant_table_index == f->context_model);
1097
224k
    }
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
77.7k
        put_rac(c, state, sc->slice_coding_mode == 1);
1106
77.7k
        if (sc->slice_coding_mode == 1)
1107
2.47k
            ff_ffv1_clear_slice_state(f, sc);
1108
77.7k
        put_symbol(c, state, sc->slice_coding_mode, 0);
1109
77.7k
        if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
1110
36.2k
            put_symbol(c, state, sc->slice_rct_by_coef, 0);
1111
36.2k
            put_symbol(c, state, sc->slice_rct_ry_coef, 0);
1112
36.2k
        }
1113
77.7k
        put_symbol(c, state, sc->remap, 0);
1114
77.7k
    }
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
36.2k
{
1120
1.78G
#define NB_Y_COEFF 15
1121
36.2k
    static const int rct_y_coeff[15][2] = {
1122
36.2k
        {0, 0}, //      4G
1123
36.2k
        {1, 1}, //  R + 2G + B
1124
36.2k
        {2, 2}, // 2R      + 2B
1125
36.2k
        {0, 2}, //      2G + 2B
1126
36.2k
        {2, 0}, // 2R + 2G
1127
36.2k
        {4, 0}, // 4R
1128
36.2k
        {0, 4}, //           4B
1129
1130
36.2k
        {0, 3}, //      1G + 3B
1131
36.2k
        {3, 0}, // 3R + 1G
1132
36.2k
        {3, 1}, // 3R      +  B
1133
36.2k
        {1, 3}, //  R      + 3B
1134
36.2k
        {1, 2}, //  R +  G + 2B
1135
36.2k
        {2, 1}, // 2R +  G +  B
1136
36.2k
        {0, 1}, //      3G +  B
1137
36.2k
        {1, 0}, //  R + 3G
1138
36.2k
    };
1139
1140
36.2k
    int stat[NB_Y_COEFF] = {0};
1141
36.2k
    int x, y, i, p, best;
1142
36.2k
    int16_t *sample[3];
1143
36.2k
    int lbd = f->bits_per_raw_sample <= 8;
1144
36.2k
    int packed = !src[1];
1145
36.2k
    int transparency = f->transparency;
1146
36.2k
    int packed_size = (3 + transparency)*2;
1147
1148
2.20M
    for (y = 0; y < h; y++) {
1149
2.16M
        int lastr=0, lastg=0, lastb=0;
1150
8.66M
        for (p = 0; p < 3; p++)
1151
6.50M
            sample[p] = sc->sample_buffer + p*w;
1152
1153
188M
        for (x = 0; x < w; x++) {
1154
186M
            int b, g, r;
1155
186M
            int ab, ag, ar;
1156
186M
            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
186M
            } 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
186M
            } else if (f->use32bit || transparency) {
1167
186M
                g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
1168
186M
                b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
1169
186M
                r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
1170
186M
            } 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
186M
            ar = r - lastr;
1177
186M
            ag = g - lastg;
1178
186M
            ab = b - lastb;
1179
186M
            if (x && y) {
1180
111M
                int bg = ag - sample[0][x];
1181
111M
                int bb = ab - sample[1][x];
1182
111M
                int br = ar - sample[2][x];
1183
1184
111M
                br -= bg;
1185
111M
                bb -= bg;
1186
1187
1.78G
                for (i = 0; i<NB_Y_COEFF; i++) {
1188
1.67G
                    stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1189
1.67G
                }
1190
1191
111M
            }
1192
186M
            sample[0][x] = ag;
1193
186M
            sample[1][x] = ab;
1194
186M
            sample[2][x] = ar;
1195
1196
186M
            lastr = r;
1197
186M
            lastg = g;
1198
186M
            lastb = b;
1199
186M
        }
1200
2.16M
    }
1201
1202
36.2k
    best = 0;
1203
544k
    for (i=1; i<NB_Y_COEFF; i++) {
1204
507k
        if (stat[i] < stat[best])
1205
49.9k
            best = i;
1206
507k
    }
1207
1208
36.2k
    sc->slice_rct_by_coef = rct_y_coeff[best][1];
1209
36.2k
    sc->slice_rct_ry_coef = rct_y_coeff[best][0];
1210
36.2k
}
1211
1212
static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc)
1213
45.0k
{
1214
45.0k
    int len = 1 << f->bits_per_raw_sample;
1215
45.0k
    int flip = sc->remap == 2 ? 0x7FFF : 0;
1216
1217
120k
    for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
1218
74.9k
        int j = 0;
1219
74.9k
        int lu = 0;
1220
74.9k
        uint8_t state[2][32];
1221
74.9k
        int run = 0;
1222
1223
74.9k
        memset(state, 128, sizeof(state));
1224
74.9k
        put_symbol(&sc->c, state[0], 0, 0);
1225
74.9k
        memset(state, 128, sizeof(state));
1226
4.91G
        for (int i= 0; i<len; i++) {
1227
4.91G
            int ri = i ^ ((i&0x8000) ? 0 : flip);
1228
4.91G
            int u = sc->fltmap[p][ri];
1229
4.91G
            sc->fltmap[p][ri] = j;
1230
4.91G
            j+= u;
1231
1232
4.91G
            if (lu == u) {
1233
4.90G
                run ++;
1234
4.90G
            } else {
1235
11.6M
                put_symbol_inline(&sc->c, state[lu], run, 0, NULL, NULL);
1236
11.6M
                if (run == 0)
1237
3.39M
                    lu = u;
1238
11.6M
                run = 0;
1239
11.6M
            }
1240
4.91G
        }
1241
74.9k
        if (run)
1242
63.5k
            put_symbol(&sc->c, state[lu], run, 0);
1243
74.9k
        sc->remap_count[p] = j;
1244
74.9k
    }
1245
45.0k
}
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.7k
{
1251
32.7k
    int x, y;
1252
32.7k
    int transparency = f->transparency;
1253
32.7k
    int i = 0;
1254
1255
1.54M
    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
507M
#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.51M
    }
1289
1290
    //TODO switch to radix sort
1291
1.35G
#define CMP(A,B) ((A)->val - (int64_t)(B)->val)
1292
644M
    AV_QSORT(sc->unit[0], i, struct Unit, CMP);
1293
355M
    AV_QSORT(sc->unit[1], i, struct Unit, CMP);
1294
351M
    AV_QSORT(sc->unit[2], i, struct Unit, CMP);
1295
32.7k
    if (transparency)
1296
0
        AV_QSORT(sc->unit[3], i, struct Unit, CMP);
1297
32.7k
}
1298
1299
static int encode_float32_remap_segment(FFV1SliceContext *sc,
1300
                                        int p, int mul_count, int *mul_tab, int update, int final)
1301
294k
{
1302
294k
    const int pixel_num = sc->slice_width * sc->slice_height;
1303
294k
    uint8_t state[2][3][32];
1304
294k
    int mul[4096+1];
1305
294k
    RangeCoder rc = sc->c;
1306
294k
    int lu = 0;
1307
294k
    int run = 0;
1308
294k
    int64_t last_val = -1;
1309
294k
    int compact_index = -1;
1310
294k
    int i = 0;
1311
294k
    int current_mul_index = -1;
1312
294k
    int run1final = 0;
1313
294k
    int run1start_i;
1314
294k
    int run1start_last_val;
1315
294k
    int run1start_mul_index;
1316
1317
294k
    memcpy(mul, mul_tab, sizeof(*mul_tab)*(mul_count+1));
1318
294k
    memset(state, 128, sizeof(state));
1319
294k
    put_symbol(&rc, state[0][0], mul_count, 0);
1320
294k
    memset(state, 128, sizeof(state));
1321
1322
1.65G
    for (; i < pixel_num+1; i++) {
1323
1.65G
        int current_mul = current_mul_index < 0 ? 1 : FFABS(mul[current_mul_index]);
1324
1.65G
        int64_t val;
1325
1.65G
        if (i == pixel_num) {
1326
324k
            if (last_val == 0xFFFFFFFF && (!run || run1final)) {
1327
99.5k
                break;
1328
224k
            } else {
1329
224k
                val = last_val + ((1LL<<32) - last_val + current_mul - 1) / current_mul * current_mul;
1330
224k
                av_assert2(val >= (1LL<<32));
1331
224k
                val += lu * current_mul; //ensure a run1 ends
1332
224k
            }
1333
324k
        } else
1334
1.65G
            val = sc->unit[p][i].val;
1335
1336
1.65G
        if (last_val != val) {
1337
33.9M
            int64_t delta = val - last_val;
1338
33.9M
            int64_t step  = FFMAX(1, (delta + current_mul/2) / current_mul);
1339
33.9M
            av_assert2(last_val < val);
1340
33.9M
            av_assert2(current_mul > 0);
1341
1342
33.9M
            delta -= step*current_mul;
1343
33.9M
            av_assert2(delta <= current_mul/2);
1344
33.9M
            av_assert2(delta > -current_mul);
1345
1346
33.9M
            av_assert2(step > 0);
1347
33.9M
            if (lu) {
1348
7.63M
                if (!run) {
1349
4.07M
                    run1start_i        = i - 1;
1350
4.07M
                    run1start_last_val = last_val;
1351
4.07M
                    run1start_mul_index= current_mul_index;
1352
4.07M
                }
1353
7.63M
                if (step == 1) {
1354
3.55M
                    if (run1final) {
1355
1.60M
                        if (current_mul>1)
1356
1.58M
                            put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
1357
1.60M
                    }
1358
3.55M
                    run ++;
1359
3.55M
                    av_assert2(last_val + current_mul + delta == val);
1360
4.07M
                } else {
1361
4.07M
                    if (run1final) {
1362
2.03M
                        if (run == 0)
1363
1.41M
                            lu ^= 1;
1364
2.03M
                        i--; // we did not encode val so we need to backstep
1365
2.03M
                        last_val += current_mul;
1366
2.03M
                    } else {
1367
2.03M
                        put_symbol_inline(&rc, state[lu][0], run, 0, NULL, NULL);
1368
2.03M
                        i                 = run1start_i;
1369
2.03M
                        last_val          = run1start_last_val; // we could compute this instead of storing
1370
2.03M
                        current_mul_index = run1start_mul_index;
1371
2.03M
                    }
1372
4.07M
                    run1final ^= 1;
1373
1374
4.07M
                    run = 0;
1375
4.07M
                    continue;
1376
4.07M
                }
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.7M
                    put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
1384
26.3M
                if (step == 1)
1385
1.44M
                    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.94M
                    av_assert2(i < pixel_num);
1395
1.94M
                    mul[ current_mul_index ] *= -1;
1396
1.94M
                    put_symbol_inline(&rc, state[0][2], mul[ current_mul_index ], 0, NULL, NULL);
1397
1.94M
                }
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
507M
                sc->bitmap[p][sc->unit[p][i].ndx] = compact_index;
1405
1.65G
    }
1406
1407
294k
    if (update) {
1408
98.1k
        sc->c = rc;
1409
98.1k
        sc->remap_count[p] = compact_index + 1;
1410
98.1k
    }
1411
294k
    return get_rac_count(&rc);
1412
294k
}
1413
1414
static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
1415
                                 const uint8_t *src[4])
1416
32.7k
{
1417
32.7k
    int pixel_num = sc->slice_width * sc->slice_height;
1418
32.7k
    const int max_log2_mul_count  = ((int[]){  1,  1,  1,  9,  9,  10})[f->remap_optimizer];
1419
32.7k
    const int log2_mul_count_step = ((int[]){  1,  1,  1,  9,  9,   1})[f->remap_optimizer];
1420
32.7k
    const int max_log2_mul        = ((int[]){  1,  8,  8,  9, 22,  22})[f->remap_optimizer];
1421
32.7k
    const int log2_mul_step       = ((int[]){  1,  8,  1,  1,  1,   1})[f->remap_optimizer];
1422
32.7k
    const int bruteforce_count    = ((int[]){  0,  0,  0,  1,  1,   1})[f->remap_optimizer];
1423
32.7k
    const int stair_mode          = ((int[]){  0,  0,  0,  1,  0,   0})[f->remap_optimizer];
1424
32.7k
    const int magic_log2          = ((int[]){  1,  1,  1,  1,  0,   0})[f->remap_optimizer];
1425
1426
130k
    for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
1427
98.1k
        int best_log2_mul_count = 0;
1428
98.1k
        float score_sum[11] = {0};
1429
98.1k
        int mul_all[11][1025];
1430
1431
294k
        for (int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; log2_mul_count += log2_mul_count_step) {
1432
196k
            float score_tab_all[1025][23] = {0};
1433
196k
            int64_t last_val = -1;
1434
196k
            int *mul_tab = mul_all[log2_mul_count];
1435
196k
            int last_mul_index = -1;
1436
196k
            int mul_count = 1 << log2_mul_count;
1437
1438
196k
            score_sum[log2_mul_count] = 2 * log2_mul_count;
1439
196k
            if (magic_log2)
1440
196k
                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.4M
                    float *score_tab = score_tab_all[(last_val + 1LL)*mul_count >> 32];
1446
18.4M
                    av_assert2(last_val < val);
1447
203M
                    for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1448
184M
                        int64_t delta = val - last_val;
1449
184M
                        int mul;
1450
184M
                        int64_t cost;
1451
1452
184M
                        if (last_val < 0) {
1453
1.96M
                            mul = 1;
1454
182M
                        } else if (stair_mode && mul_count == 512 && si == max_log2_mul ) {
1455
9.14M
                            if (mul_index >= 0x378/8 && mul_index <= 23 + 0x378/8) {
1456
484k
                                mul = (0x800080 >> (mul_index - 0x378/8));
1457
484k
                            } else
1458
8.66M
                                mul = 1;
1459
173M
                        } else {
1460
173M
                            mul = (0x10001LL)<<si >> 16;
1461
173M
                        }
1462
1463
184M
                        cost = FFMAX((delta + mul/2)  / mul, 1);
1464
184M
                        float score = 1;
1465
184M
                        if (mul > 1) {
1466
155M
                            score *= (FFABS(delta - cost*mul)+1);
1467
155M
                            if (mul_count > 1)
1468
73.6M
                                score *= score;
1469
155M
                        }
1470
184M
                        score *= cost;
1471
184M
                        score *= score;
1472
184M
                        if (mul_index != last_mul_index)
1473
18.8M
                            score *= mul;
1474
184M
                        if (magic_log2) {
1475
184M
                            score_tab[si] += av_float2int(score);
1476
184M
                        } else
1477
0
                            score_tab[si] += log2f(score);
1478
184M
                    }
1479
18.4M
                }
1480
1.01G
                last_val = val;
1481
1.01G
                last_mul_index = mul_index;
1482
1.01G
            }
1483
50.5M
            for(int i= 0; i<mul_count; i++) {
1484
50.3M
                int best_index = 0;
1485
50.3M
                float *score_tab = score_tab_all[i];
1486
553M
                for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1487
503M
                    if (score_tab[si] < score_tab[ best_index ])
1488
4.15M
                        best_index = si;
1489
503M
                }
1490
50.3M
                if (stair_mode && mul_count == 512 && best_index == max_log2_mul ) {
1491
15.8k
                    if (i >= 0x378/8 && i <= 23 + 0x378/8) {
1492
15.5k
                        mul_tab[i] = -(0x800080 >> (i - 0x378/8));
1493
15.5k
                    } else
1494
240
                        mul_tab[i] = -1;
1495
15.8k
                } else
1496
50.3M
                    mul_tab[i] = -((0x10001LL)<<best_index >> 16);
1497
50.3M
                score_sum[log2_mul_count] += score_tab[ best_index ];
1498
50.3M
            }
1499
196k
            mul_tab[mul_count] = 1;
1500
1501
196k
            if (bruteforce_count)
1502
196k
                score_sum[log2_mul_count] = encode_float32_remap_segment(sc, p, mul_count, mul_all[log2_mul_count], 0, 0);
1503
1504
196k
            if (score_sum[log2_mul_count] < score_sum[best_log2_mul_count])
1505
1.32k
                best_log2_mul_count = log2_mul_count;
1506
196k
        }
1507
1508
98.1k
        encode_float32_remap_segment(sc, p, 1<<best_log2_mul_count, mul_all[best_log2_mul_count], 1, 1);
1509
98.1k
    }
1510
32.7k
}
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.7k
{
1516
32.7k
    int x, y, p, i;
1517
32.7k
    const int ring_size = f->context_model ? 3 : 2;
1518
32.7k
    int32_t *sample[4][3];
1519
32.7k
    const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
1520
32.7k
    int bits[4], offset;
1521
32.7k
    int transparency = f->transparency;
1522
1523
32.7k
    ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
1524
1525
32.7k
    sc->run_index = 0;
1526
1527
163k
    for (int p = 0; p < MAX_PLANES; ++p)
1528
130k
        sample[p][2] = sc->sample_buffer32; // dummy to avoid UB pointer arithmetic
1529
1530
32.7k
    memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
1531
32.7k
           (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
1532
1533
1.54M
    for (y = 0; y < h; y++) {
1534
4.93M
        for (i = 0; i < ring_size; i++)
1535
17.1M
            for (p = 0; p < MAX_PLANES; p++)
1536
13.6M
                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
125M
                b -= g;
1548
125M
                r -= g;
1549
125M
                g += (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
1550
125M
                b += offset;
1551
125M
                r += offset;
1552
125M
            }
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
6.03M
        for (p = 0; p < 3 + transparency; p++) {
1560
4.53M
            int ret;
1561
4.53M
            sample[p][0][-1] = sample[p][1][0  ];
1562
4.53M
            sample[p][1][ w] = sample[p][1][w-1];
1563
4.53M
            ret = encode_line32(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
1564
4.53M
                                bits[p], ac, pass1);
1565
4.53M
            if (ret < 0)
1566
1.82k
                return ret;
1567
4.53M
        }
1568
1.51M
    }
1569
30.8k
    return 0;
1570
32.7k
}
1571
1572
1573
static int encode_slice(AVCodecContext *c, void *arg)
1574
111k
{
1575
111k
    FFV1SliceContext *sc = arg;
1576
111k
    FFV1Context *f   = c->priv_data;
1577
111k
    int width        = sc->slice_width;
1578
111k
    int height       = sc->slice_height;
1579
111k
    int x            = sc->slice_x;
1580
111k
    int y            = sc->slice_y;
1581
111k
    const AVFrame *const p = f->cur_enc_frame;
1582
111k
    const int ps     = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1583
111k
    int ret;
1584
111k
    RangeCoder c_bak = sc->c;
1585
111k
    const int chroma_width  = AV_CEIL_RSHIFT(width,  f->chroma_h_shift);
1586
111k
    const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1587
111k
    const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1588
111k
                                p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1589
111k
                                p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1590
111k
                                p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1591
111k
    int ac = f->ac;
1592
1593
111k
    sc->slice_coding_mode = 0;
1594
111k
    if (f->version > 3 && f->colorspace == 1) {
1595
36.2k
        choose_rct_params(f, sc, planes, p->linesize, width, height);
1596
75.7k
    } else {
1597
75.7k
        sc->slice_rct_by_coef = 1;
1598
75.7k
        sc->slice_rct_ry_coef = 1;
1599
75.7k
    }
1600
1601
114k
retry:
1602
114k
    if (f->key_frame)
1603
31.7k
        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
77.7k
      if (f->bits_per_raw_sample != 32) {
1613
45.0k
        if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
1614
20.7k
            const int cx            = x >> f->chroma_h_shift;
1615
20.7k
            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.7k
            load_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
1621
1622
20.7k
            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.7k
            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
24.2k
        } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
1629
18.5k
            load_plane(f, sc, p->data[0] +           ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
1630
18.5k
            load_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
1631
18.5k
        } else if (f->use32bit) {
1632
5.73k
            load_rgb_frame32(f, sc, planes, width, height, p->linesize);
1633
5.73k
        } else
1634
0
            load_rgb_frame  (f, sc, planes, width, height, p->linesize);
1635
1636
45.0k
        encode_histogram_remap(f, sc);
1637
45.0k
      } else {
1638
32.7k
            load_rgb_float32_frame(f, sc, planes, width, height, p->linesize);
1639
32.7k
            encode_float32_remap(f, sc, planes);
1640
32.7k
      }
1641
77.7k
    }
1642
1643
114k
    if (ac == AC_GOLOMB_RICE) {
1644
43.4k
        sc->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&sc->c, f->version > 2) : 0;
1645
43.4k
        init_put_bits(&sc->pb,
1646
43.4k
                      sc->c.bytestream_start + sc->ac_byte_count,
1647
43.4k
                      sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count);
1648
43.4k
    }
1649
1650
114k
    if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
1651
44.6k
        const int cx            = x >> f->chroma_h_shift;
1652
44.6k
        const int cy            = y >> f->chroma_v_shift;
1653
1654
44.6k
        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.6k
        if (f->chroma_planes) {
1657
15.7k
            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.7k
            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.7k
        }
1660
44.6k
        if (f->transparency)
1661
5.28k
            ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 3, 1, ac);
1662
69.8k
    } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
1663
19.4k
        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.4k
        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
50.3k
    } else if (f->bits_per_raw_sample == 32) {
1666
32.7k
        ret = encode_float32_rgb_frame(f, sc, planes, width, height, p->linesize, ac);
1667
32.7k
    } else if (f->use32bit) {
1668
12.6k
        ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize, ac);
1669
12.6k
    } 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
70.9k
        sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
1675
70.9k
    } else {
1676
43.4k
        flush_put_bits(&sc->pb); // FIXME: nicer padding
1677
43.4k
        sc->ac_byte_count += put_bytes_output(&sc->pb);
1678
43.4k
    }
1679
1680
114k
    if (ret < 0) {
1681
2.47k
        av_assert0(sc->slice_coding_mode == 0);
1682
2.47k
        if (f->version < 4) {
1683
0
            av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1684
0
            return ret;
1685
0
        }
1686
2.47k
        av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1687
2.47k
        ac = 1;
1688
2.47k
        sc->slice_coding_mode = 1;
1689
2.47k
        sc->c = c_bak;
1690
2.47k
        goto retry;
1691
2.47k
    }
1692
1693
111k
    return 0;
1694
114k
}
1695
1696
size_t ff_ffv1_encode_buffer_size(AVCodecContext *avctx)
1697
28.3k
{
1698
28.3k
    FFV1Context *f = avctx->priv_data;
1699
1700
28.3k
    int w = avctx->width  + f->num_h_slices;
1701
28.3k
    int h = avctx->height + f->num_v_slices;
1702
28.3k
    size_t maxsize = w*h * (1 + f->transparency);
1703
28.3k
    if (f->chroma_planes)
1704
16.2k
        maxsize += AV_CEIL_RSHIFT(w, f->chroma_h_shift) * AV_CEIL_RSHIFT(h, f->chroma_v_shift) * 2;
1705
28.3k
    maxsize += f->slice_count * 800; //for slice header
1706
28.3k
    if (f->version > 3) {
1707
20.3k
        maxsize *= f->bits_per_raw_sample + 1;
1708
20.3k
        if (f->remap_mode)
1709
20.3k
            maxsize += f->slice_count * 70000 * (1 + 2*f->chroma_planes + f->transparency);
1710
20.3k
    } else {
1711
8.06k
        maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
1712
8.06k
        maxsize *= 8*(2*f->bits_per_raw_sample + 5);
1713
8.06k
    }
1714
28.3k
    maxsize >>= 3;
1715
28.3k
    maxsize += FF_INPUT_BUFFER_MIN_SIZE;
1716
1717
28.3k
    return maxsize;
1718
28.3k
}
1719
1720
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1721
                        const AVFrame *pict, int *got_packet)
1722
33.2k
{
1723
33.2k
    FFV1Context *f      = avctx->priv_data;
1724
33.2k
    RangeCoder *const c = &f->slices[0].c;
1725
33.2k
    uint8_t keystate    = 128;
1726
33.2k
    uint8_t *buf_p;
1727
33.2k
    int i, ret;
1728
33.2k
    int64_t maxsize;
1729
1730
33.2k
    if(!pict) {
1731
4.89k
        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.89k
        return 0;
1774
4.89k
    }
1775
1776
    /* Maximum packet size */
1777
28.3k
    maxsize = ff_ffv1_encode_buffer_size(avctx);
1778
1779
28.3k
    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.3k
    if ((ret = ff_alloc_packet(avctx, pkt, maxsize)) < 0)
1789
0
        return ret;
1790
1791
28.3k
    ff_init_range_encoder(c, pkt->data, pkt->size);
1792
28.3k
    ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1793
1794
28.3k
    f->cur_enc_frame = pict;
1795
1796
28.3k
    if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
1797
6.17k
        put_rac(c, &keystate, 1);
1798
6.17k
        f->key_frame = 1;
1799
6.17k
        f->gob_count++;
1800
6.17k
        write_header(f);
1801
22.1k
    } else {
1802
22.1k
        put_rac(c, &keystate, 0);
1803
22.1k
        f->key_frame = 0;
1804
22.1k
    }
1805
1806
28.3k
    if (f->ac == AC_RANGE_CUSTOM_TAB) {
1807
14.7k
        int i;
1808
3.78M
        for (i = 1; i < 256; i++) {
1809
3.77M
            c->one_state[i]        = f->state_transition[i];
1810
3.77M
            c->zero_state[256 - i] = 256 - c->one_state[i];
1811
3.77M
        }
1812
14.7k
    }
1813
1814
140k
    for (i = 0; i < f->slice_count; i++) {
1815
111k
        FFV1SliceContext *sc = &f->slices[i];
1816
111k
        uint8_t *start  = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1817
111k
        int len         = pkt->size / f->slice_count;
1818
111k
        if (i) {
1819
83.6k
            ff_init_range_encoder(&sc->c, start, len);
1820
83.6k
        } else {
1821
28.3k
            av_assert0(sc->c.bytestream_end >= sc->c.bytestream_start + len);
1822
28.3k
            av_assert0(sc->c.bytestream < sc->c.bytestream_start + len);
1823
28.3k
            sc->c.bytestream_end = sc->c.bytestream_start + len;
1824
28.3k
        }
1825
111k
    }
1826
28.3k
    avctx->execute(avctx, encode_slice, f->slices, NULL,
1827
28.3k
                   f->slice_count, sizeof(*f->slices));
1828
1829
28.3k
    buf_p = pkt->data;
1830
140k
    for (i = 0; i < f->slice_count; i++) {
1831
111k
        FFV1SliceContext *sc = &f->slices[i];
1832
111k
        int bytes = sc->ac_byte_count;
1833
111k
        if (i > 0 || f->version > 2) {
1834
111k
            av_assert0(bytes < pkt->size / f->slice_count);
1835
111k
            memmove(buf_p, sc->c.bytestream_start, bytes);
1836
111k
            av_assert0(bytes < (1 << 24));
1837
111k
            AV_WB24(buf_p + bytes, bytes);
1838
111k
            bytes += 3;
1839
111k
        }
1840
111k
        if (f->ec) {
1841
33.6k
            unsigned v;
1842
33.6k
            buf_p[bytes++] = 0;
1843
33.6k
            v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, buf_p, bytes) ^ (f->crcref ? 0x8CD88196 : 0);
1844
33.6k
            AV_WL32(buf_p + bytes, v);
1845
33.6k
            bytes += 4;
1846
33.6k
        }
1847
111k
        buf_p += bytes;
1848
111k
    }
1849
1850
28.3k
    if (avctx->flags & AV_CODEC_FLAG_PASS1)
1851
0
        avctx->stats_out[0] = '\0';
1852
1853
28.3k
    f->picture_number++;
1854
28.3k
    pkt->size   = buf_p - pkt->data;
1855
28.3k
    pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1856
28.3k
    *got_packet = 1;
1857
1858
28.3k
    return 0;
1859
28.3k
}
1860
1861
static av_cold int encode_close(AVCodecContext *avctx)
1862
4.90k
{
1863
4.90k
    FFV1Context *const s = avctx->priv_data;
1864
1865
30.1k
    for (int j = 0; j < s->max_slice_count; j++) {
1866
25.2k
        FFV1SliceContext *sc = &s->slices[j];
1867
1868
126k
        for(int p = 0; p<4; p++) {
1869
100k
            av_freep(&sc->unit[p]);
1870
100k
            av_freep(&sc->bitmap[p]);
1871
100k
        }
1872
25.2k
    }
1873
1874
4.90k
    av_freep(&avctx->stats_out);
1875
4.90k
    ff_ffv1_close(s);
1876
1877
4.90k
    return 0;
1878
4.90k
}
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
};