Coverage Report

Created: 2025-11-16 07:20

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
47.0M
#define MAX_PLANES 4
45
924M
#define CONTEXT_SIZE 32
46
47
600
#define MAX_QUANT_TABLES 8
48
2.33G
#define MAX_QUANT_TABLE_SIZE 256
49
2.32G
#define MAX_QUANT_TABLE_MASK (MAX_QUANT_TABLE_SIZE - 1)
50
#define MAX_CONTEXT_INPUTS 5
51
52
647M
#define AC_GOLOMB_RICE          0
53
733
#define AC_RANGE_DEFAULT_TAB    1
54
233k
#define AC_RANGE_CUSTOM_TAB     2
55
4.01k
#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
102k
#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
144
    const AVFrame *cur_enc_frame;
145
    int plane_count;
146
    int ac;                              ///< 1=range coder <-> 0=golomb rice
147
    int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE];
148
    int context_count[MAX_QUANT_TABLES];
149
    uint8_t state_transition[256];
150
    uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
151
    int colorspace;
152
    int flt;
153
    int remap_mode;
154
    int remap_optimizer;
155
    int maxsize_warned;
156
157
    int use32bit;
158
159
    int ec;
160
    int intra;
161
    int key_frame_ok;
162
    int context_model;
163
    int qtable;
164
165
    int bits_per_raw_sample;
166
    int packed_at_lsb;
167
168
    int gob_count;
169
    int quant_table_count;
170
171
    int slice_count;
172
    int max_slice_count;
173
    int num_v_slices;
174
    int num_h_slices;
175
176
    FFV1SliceContext *slices;
177
    /* RefStruct object, per-slice damage flags shared between frame threads.
178
     *
179
     * After a frame thread marks some slice as finished with
180
     * ff_progress_frame_report(), the corresponding array element must not be
181
     * accessed by this thread anymore, as from then on it is owned by the next
182
     * thread.
183
     */
184
    uint8_t          *slice_damaged;
185
    /* Frame damage flag, used to delay announcing progress, since ER is
186
     * applied after all the slices are decoded.
187
     * NOT shared between frame threads.
188
     */
189
    uint8_t           frame_damaged;
190
} FFV1Context;
191
192
int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s);
193
int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc);
194
int ff_ffv1_init_slices_state(FFV1Context *f);
195
int ff_ffv1_init_slice_contexts(FFV1Context *f);
196
PlaneContext *ff_ffv1_planes_alloc(void);
197
int ff_ffv1_allocate_initial_states(FFV1Context *f);
198
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc);
199
void ff_ffv1_close(FFV1Context *s);
200
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift);
201
int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state);
202
int ff_ffv1_read_extra_header(FFV1Context *f);
203
int ff_ffv1_read_quant_tables(RangeCoder *c,
204
                              int16_t quant_table[MAX_CONTEXT_INPUTS][256]);
205
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);
206
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed);
207
208
/**
209
 * This is intended for both width and height
210
 */
211
int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift);
212
213
static av_always_inline int fold(int diff, int bits)
214
633M
{
215
633M
    if (bits == 8)
216
69.9M
        diff = (int8_t)diff;
217
563M
    else {
218
563M
        diff = sign_extend(diff, bits);
219
563M
    }
220
221
633M
    return diff;
222
633M
}
Unexecuted instantiation: ffv1_parser.c:fold
Unexecuted instantiation: ffv1.c:fold
Unexecuted instantiation: ffv1_parse.c:fold
ffv1dec.c:fold
Line
Count
Source
214
33.8M
{
215
33.8M
    if (bits == 8)
216
10.5M
        diff = (int8_t)diff;
217
23.3M
    else {
218
23.3M
        diff = sign_extend(diff, bits);
219
23.3M
    }
220
221
33.8M
    return diff;
222
33.8M
}
ffv1enc.c:fold
Line
Count
Source
214
599M
{
215
599M
    if (bits == 8)
216
59.4M
        diff = (int8_t)diff;
217
539M
    else {
218
539M
        diff = sign_extend(diff, bits);
219
539M
    }
220
221
599M
    return diff;
222
599M
}
223
224
static inline void update_vlc_state(VlcState *const state, const int v)
225
54.0M
{
226
54.0M
    int drift = state->drift;
227
54.0M
    int count = state->count;
228
54.0M
    state->error_sum += FFABS(v);
229
54.0M
    drift            += v;
230
231
54.0M
    if (count == 128) { // FIXME: variable
232
633k
        count            >>= 1;
233
633k
        drift            >>= 1;
234
633k
        state->error_sum >>= 1;
235
633k
    }
236
54.0M
    count++;
237
238
54.0M
    if (drift <= -count) {
239
5.05M
        state->bias = FFMAX(state->bias - 1, -128);
240
241
5.05M
        drift = FFMAX(drift + count, -count + 1);
242
49.0M
    } else if (drift > 0) {
243
4.92M
        state->bias = FFMIN(state->bias + 1, 127);
244
245
4.92M
        drift = FFMIN(drift - count, 0);
246
4.92M
    }
247
248
54.0M
    state->drift = drift;
249
54.0M
    state->count = count;
250
54.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
225
33.8M
{
226
33.8M
    int drift = state->drift;
227
33.8M
    int count = state->count;
228
33.8M
    state->error_sum += FFABS(v);
229
33.8M
    drift            += v;
230
231
33.8M
    if (count == 128) { // FIXME: variable
232
399k
        count            >>= 1;
233
399k
        drift            >>= 1;
234
399k
        state->error_sum >>= 1;
235
399k
    }
236
33.8M
    count++;
237
238
33.8M
    if (drift <= -count) {
239
1.73M
        state->bias = FFMAX(state->bias - 1, -128);
240
241
1.73M
        drift = FFMAX(drift + count, -count + 1);
242
32.0M
    } else if (drift > 0) {
243
1.52M
        state->bias = FFMIN(state->bias + 1, 127);
244
245
1.52M
        drift = FFMIN(drift - count, 0);
246
1.52M
    }
247
248
33.8M
    state->drift = drift;
249
33.8M
    state->count = count;
250
33.8M
}
ffv1enc.c:update_vlc_state
Line
Count
Source
225
20.2M
{
226
20.2M
    int drift = state->drift;
227
20.2M
    int count = state->count;
228
20.2M
    state->error_sum += FFABS(v);
229
20.2M
    drift            += v;
230
231
20.2M
    if (count == 128) { // FIXME: variable
232
234k
        count            >>= 1;
233
234k
        drift            >>= 1;
234
234k
        state->error_sum >>= 1;
235
234k
    }
236
20.2M
    count++;
237
238
20.2M
    if (drift <= -count) {
239
3.31M
        state->bias = FFMAX(state->bias - 1, -128);
240
241
3.31M
        drift = FFMAX(drift + count, -count + 1);
242
16.9M
    } else if (drift > 0) {
243
3.40M
        state->bias = FFMIN(state->bias + 1, 127);
244
245
3.40M
        drift = FFMIN(drift - count, 0);
246
3.40M
    }
247
248
20.2M
    state->drift = drift;
249
20.2M
    state->count = count;
250
20.2M
}
251
252
253
static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
254
                                               int is_signed)
255
11.2M
{
256
11.2M
    if (get_rac(c, state + 0))
257
2.14M
        return 0;
258
9.15M
    else {
259
9.15M
        int e;
260
9.15M
        unsigned a;
261
9.15M
        e = 0;
262
10.6M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
263
1.51M
            e++;
264
1.51M
            if (e > 31)
265
2.19k
                return AVERROR_INVALIDDATA;
266
1.51M
        }
267
268
9.15M
        a = 1;
269
10.6M
        for (int i = e - 1; i >= 0; i--)
270
1.44M
            a += a + get_rac(c, state + 22 + FFMIN(i, 9));  // 22..31
271
272
9.15M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
273
9.15M
        return (a ^ e) - e;
274
9.15M
    }
275
11.2M
}
Unexecuted instantiation: ffv1_parser.c:get_symbol_inline
ffv1.c:get_symbol_inline
Line
Count
Source
255
5.01M
{
256
5.01M
    if (get_rac(c, state + 0))
257
1.17M
        return 0;
258
3.83M
    else {
259
3.83M
        int e;
260
3.83M
        unsigned a;
261
3.83M
        e = 0;
262
4.78M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
263
948k
            e++;
264
948k
            if (e > 31)
265
1.77k
                return AVERROR_INVALIDDATA;
266
948k
        }
267
268
3.83M
        a = 1;
269
4.72M
        for (int i = e - 1; i >= 0; i--)
270
891k
            a += a + get_rac(c, state + 22 + FFMIN(i, 9));  // 22..31
271
272
3.83M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
273
3.83M
        return (a ^ e) - e;
274
3.83M
    }
275
5.01M
}
Unexecuted instantiation: ffv1_parse.c:get_symbol_inline
ffv1dec.c:get_symbol_inline
Line
Count
Source
255
6.28M
{
256
6.28M
    if (get_rac(c, state + 0))
257
964k
        return 0;
258
5.32M
    else {
259
5.32M
        int e;
260
5.32M
        unsigned a;
261
5.32M
        e = 0;
262
5.89M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
263
571k
            e++;
264
571k
            if (e > 31)
265
415
                return AVERROR_INVALIDDATA;
266
571k
        }
267
268
5.31M
        a = 1;
269
5.87M
        for (int i = e - 1; i >= 0; i--)
270
558k
            a += a + get_rac(c, state + 22 + FFMIN(i, 9));  // 22..31
271
272
5.31M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
273
5.31M
        return (a ^ e) - e;
274
5.32M
    }
275
6.28M
}
Unexecuted instantiation: ffv1enc.c:get_symbol_inline
276
277
#endif /* AVCODEC_FFV1_H */