Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/ffv1.h
Line
Count
Source
1
/*
2
 * FFV1 codec for libavcodec
3
 *
4
 * Copyright (c) 2003-2012 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
#ifndef AVCODEC_FFV1_H
24
#define AVCODEC_FFV1_H
25
26
/**
27
 * @file
28
 * FF Video Codec 1 (a lossless codec)
29
 */
30
31
#include "libavutil/attributes.h"
32
#include "avcodec.h"
33
#include "get_bits.h"
34
#include "mathops.h"
35
#include "progressframe.h"
36
#include "put_bits.h"
37
#include "rangecoder.h"
38
39
#ifdef __INTEL_COMPILER
40
#undef av_flatten
41
#define av_flatten
42
#endif
43
44
45.5M
#define MAX_PLANES 4
45
908M
#define CONTEXT_SIZE 32
46
47
714
#define MAX_QUANT_TABLES 8
48
2.30G
#define MAX_QUANT_TABLE_SIZE 256
49
2.30G
#define MAX_QUANT_TABLE_MASK (MAX_QUANT_TABLE_SIZE - 1)
50
#define MAX_CONTEXT_INPUTS 5
51
52
649M
#define AC_GOLOMB_RICE          0
53
598
#define AC_RANGE_DEFAULT_TAB    1
54
251k
#define AC_RANGE_CUSTOM_TAB     2
55
3.94k
#define AC_RANGE_DEFAULT_TAB_FORCE -2
56
57
typedef struct VlcState {
58
    uint32_t error_sum;
59
    int16_t drift;
60
    int8_t bias;
61
    uint8_t count;
62
} VlcState;
63
64
typedef struct PlaneContext {
65
    int quant_table_index;
66
    int context_count;
67
    uint8_t (*state)[CONTEXT_SIZE];
68
    VlcState *vlc_state;
69
} PlaneContext;
70
71
47.4k
#define MAX_SLICES 1024
72
73
typedef struct FFV1SliceContext {
74
    int16_t *sample_buffer;
75
    int32_t *sample_buffer32;
76
77
    int slice_width;
78
    int slice_height;
79
    int slice_x;
80
    int slice_y;
81
    int sx, sy;
82
83
    int run_index;
84
    int slice_coding_mode;
85
    int slice_rct_by_coef;
86
    int slice_rct_ry_coef;
87
    int remap;
88
89
    // RefStruct reference, array of MAX_PLANES elements
90
    PlaneContext *plane;
91
    PutBitContext pb;
92
    RangeCoder c;
93
94
    int ac_byte_count;                   ///< number of bytes used for AC coding
95
96
    union {
97
        // decoder-only
98
        struct {
99
            int slice_reset_contexts;
100
            int slice_damaged;
101
        };
102
103
        // encoder-only
104
        struct {
105
            uint64_t rc_stat[256][2];
106
            uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
107
        };
108
    };
109
    int remap_count[4];
110
111
    uint32_t   *bitmap  [4]; //float encode
112
    uint16_t   *fltmap  [4]; //halffloat encode & decode
113
    uint32_t   *fltmap32[4]; //float decode
114
    unsigned int fltmap_size[4];
115
    unsigned int fltmap32_size[4];
116
    struct Unit {
117
        uint32_t val; //this is unneeded if you accept a dereference on each access
118
        uint32_t ndx;
119
    } *unit[4];
120
} FFV1SliceContext;
121
122
typedef struct FFV1Context {
123
    AVClass *class;
124
    AVCodecContext *avctx;
125
    uint64_t rc_stat[256][2];
126
    uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
127
    int version;
128
    int micro_version;
129
    int combined_version;
130
    int width, height;
131
    int chroma_planes;
132
    int chroma_h_shift, chroma_v_shift;
133
    int transparency;
134
    int flags;
135
    int64_t picture_number;
136
    int key_frame;
137
    ProgressFrame picture, last_picture;
138
    void *hwaccel_picture_private, *hwaccel_last_picture_private;
139
    uint32_t crcref;
140
    enum AVPixelFormat pix_fmt;
141
    enum AVPixelFormat configured_pix_fmt;
142
    int configured_width, configured_height;
143
    int configured_ac;
144
145
    const AVFrame *cur_enc_frame;
146
    int plane_count;
147
    int ac;                              ///< 1=range coder <-> 0=golomb rice
148
    int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE];
149
    int context_count[MAX_QUANT_TABLES];
150
    uint8_t state_transition[256];
151
    uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
152
    int colorspace;
153
    int flt;
154
    int remap_mode;
155
    int remap_optimizer;
156
    int maxsize_warned;
157
158
    int use32bit;
159
160
    int ec;
161
    int intra;
162
    int key_frame_ok;
163
    int context_model;
164
    int qtable;
165
166
    int bits_per_raw_sample;
167
    int packed_at_lsb;
168
169
    int gob_count;
170
    int quant_table_count;
171
172
    int slice_count;
173
    int max_slice_count;
174
    int num_v_slices;
175
    int num_h_slices;
176
177
    FFV1SliceContext *slices;
178
    /* RefStruct object, per-slice damage flags shared between frame threads.
179
     *
180
     * After a frame thread marks some slice as finished with
181
     * ff_progress_frame_report(), the corresponding array element must not be
182
     * accessed by this thread anymore, as from then on it is owned by the next
183
     * thread.
184
     */
185
    uint8_t          *slice_damaged;
186
    /* Frame damage flag, used to delay announcing progress, since ER is
187
     * applied after all the slices are decoded.
188
     * NOT shared between frame threads.
189
     */
190
    uint8_t           frame_damaged;
191
} FFV1Context;
192
193
int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s);
194
int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc);
195
int ff_ffv1_init_slices_state(FFV1Context *f);
196
int ff_ffv1_init_slice_contexts(FFV1Context *f);
197
PlaneContext *ff_ffv1_planes_alloc(void);
198
int ff_ffv1_allocate_initial_states(FFV1Context *f);
199
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc);
200
void ff_ffv1_close(FFV1Context *s);
201
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift);
202
int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state);
203
int ff_ffv1_read_extra_header(FFV1Context *f);
204
int ff_ffv1_read_quant_tables(RangeCoder *c,
205
                              int16_t quant_table[MAX_CONTEXT_INPUTS][256]);
206
void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample);
207
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed);
208
209
/**
210
 * This is intended for both width and height
211
 */
212
int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift);
213
214
static av_always_inline int fold(int diff, int bits)
215
623M
{
216
623M
    if (bits == 8)
217
65.7M
        diff = (int8_t)diff;
218
557M
    else {
219
557M
        diff = sign_extend(diff, bits);
220
557M
    }
221
222
623M
    return diff;
223
623M
}
Unexecuted instantiation: ffv1_parser.c:fold
Unexecuted instantiation: ffv1.c:fold
Unexecuted instantiation: ffv1_parse.c:fold
ffv1dec.c:fold
Line
Count
Source
215
43.8M
{
216
43.8M
    if (bits == 8)
217
6.88M
        diff = (int8_t)diff;
218
36.9M
    else {
219
36.9M
        diff = sign_extend(diff, bits);
220
36.9M
    }
221
222
43.8M
    return diff;
223
43.8M
}
ffv1enc.c:fold
Line
Count
Source
215
579M
{
216
579M
    if (bits == 8)
217
58.8M
        diff = (int8_t)diff;
218
520M
    else {
219
520M
        diff = sign_extend(diff, bits);
220
520M
    }
221
222
579M
    return diff;
223
579M
}
224
225
static inline void update_vlc_state(VlcState *const state, const int v)
226
65.0M
{
227
65.0M
    int drift = state->drift;
228
65.0M
    int count = state->count;
229
65.0M
    state->error_sum += FFABS(v);
230
65.0M
    drift            += v;
231
232
65.0M
    if (count == 128) { // FIXME: variable
233
749k
        count            >>= 1;
234
749k
        drift            >>= 1;
235
749k
        state->error_sum >>= 1;
236
749k
    }
237
65.0M
    count++;
238
239
65.0M
    if (drift <= -count) {
240
6.83M
        state->bias = FFMAX(state->bias - 1, -128);
241
242
6.83M
        drift = FFMAX(drift + count, -count + 1);
243
58.2M
    } else if (drift > 0) {
244
6.55M
        state->bias = FFMIN(state->bias + 1, 127);
245
246
6.55M
        drift = FFMIN(drift - count, 0);
247
6.55M
    }
248
249
65.0M
    state->drift = drift;
250
65.0M
    state->count = count;
251
65.0M
}
Unexecuted instantiation: ffv1_parser.c:update_vlc_state
Unexecuted instantiation: ffv1.c:update_vlc_state
Unexecuted instantiation: ffv1_parse.c:update_vlc_state
ffv1dec.c:update_vlc_state
Line
Count
Source
226
43.8M
{
227
43.8M
    int drift = state->drift;
228
43.8M
    int count = state->count;
229
43.8M
    state->error_sum += FFABS(v);
230
43.8M
    drift            += v;
231
232
43.8M
    if (count == 128) { // FIXME: variable
233
494k
        count            >>= 1;
234
494k
        drift            >>= 1;
235
494k
        state->error_sum >>= 1;
236
494k
    }
237
43.8M
    count++;
238
239
43.8M
    if (drift <= -count) {
240
3.17M
        state->bias = FFMAX(state->bias - 1, -128);
241
242
3.17M
        drift = FFMAX(drift + count, -count + 1);
243
40.6M
    } else if (drift > 0) {
244
2.85M
        state->bias = FFMIN(state->bias + 1, 127);
245
246
2.85M
        drift = FFMIN(drift - count, 0);
247
2.85M
    }
248
249
43.8M
    state->drift = drift;
250
43.8M
    state->count = count;
251
43.8M
}
ffv1enc.c:update_vlc_state
Line
Count
Source
226
21.2M
{
227
21.2M
    int drift = state->drift;
228
21.2M
    int count = state->count;
229
21.2M
    state->error_sum += FFABS(v);
230
21.2M
    drift            += v;
231
232
21.2M
    if (count == 128) { // FIXME: variable
233
254k
        count            >>= 1;
234
254k
        drift            >>= 1;
235
254k
        state->error_sum >>= 1;
236
254k
    }
237
21.2M
    count++;
238
239
21.2M
    if (drift <= -count) {
240
3.65M
        state->bias = FFMAX(state->bias - 1, -128);
241
242
3.65M
        drift = FFMAX(drift + count, -count + 1);
243
17.6M
    } else if (drift > 0) {
244
3.70M
        state->bias = FFMIN(state->bias + 1, 127);
245
246
3.70M
        drift = FFMIN(drift - count, 0);
247
3.70M
    }
248
249
21.2M
    state->drift = drift;
250
21.2M
    state->count = count;
251
21.2M
}
252
253
254
static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
255
                                               int is_signed)
256
19.2M
{
257
19.2M
    if (get_rac(c, state + 0))
258
2.82M
        return 0;
259
16.4M
    else {
260
16.4M
        int e;
261
16.4M
        unsigned a;
262
16.4M
        e = 0;
263
18.0M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
264
1.66M
            e++;
265
1.66M
            if (e > 31)
266
2.01k
                return AVERROR_INVALIDDATA;
267
1.66M
        }
268
269
16.4M
        a = 1;
270
18.0M
        for (int i = e - 1; i >= 0; i--)
271
1.59M
            a += a + get_rac(c, state + 22 + FFMIN(i, 9));  // 22..31
272
273
16.4M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
274
16.4M
        return (a ^ e) - e;
275
16.4M
    }
276
19.2M
}
Unexecuted instantiation: ffv1_parser.c:get_symbol_inline
ffv1.c:get_symbol_inline
Line
Count
Source
256
10.0M
{
257
10.0M
    if (get_rac(c, state + 0))
258
1.42M
        return 0;
259
8.66M
    else {
260
8.66M
        int e;
261
8.66M
        unsigned a;
262
8.66M
        e = 0;
263
9.70M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
264
1.04M
            e++;
265
1.04M
            if (e > 31)
266
1.53k
                return AVERROR_INVALIDDATA;
267
1.04M
        }
268
269
8.66M
        a = 1;
270
9.65M
        for (int i = e - 1; i >= 0; i--)
271
996k
            a += a + get_rac(c, state + 22 + FFMIN(i, 9));  // 22..31
272
273
8.66M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
274
8.66M
        return (a ^ e) - e;
275
8.66M
    }
276
10.0M
}
Unexecuted instantiation: ffv1_parse.c:get_symbol_inline
ffv1dec.c:get_symbol_inline
Line
Count
Source
256
9.15M
{
257
9.15M
    if (get_rac(c, state + 0))
258
1.40M
        return 0;
259
7.74M
    else {
260
7.74M
        int e;
261
7.74M
        unsigned a;
262
7.74M
        e = 0;
263
8.36M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
264
615k
            e++;
265
615k
            if (e > 31)
266
474
                return AVERROR_INVALIDDATA;
267
615k
        }
268
269
7.74M
        a = 1;
270
8.34M
        for (int i = e - 1; i >= 0; i--)
271
600k
            a += a + get_rac(c, state + 22 + FFMIN(i, 9));  // 22..31
272
273
7.74M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
274
7.74M
        return (a ^ e) - e;
275
7.74M
    }
276
9.15M
}
Unexecuted instantiation: ffv1enc.c:get_symbol_inline
277
278
#endif /* AVCODEC_FFV1_H */