FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1.h
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 24 24 100.0%
Functions: 2 2 100.0%
Branches: 12 12 100.0%

Line Branch Exec 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 "avcodec.h"
32 #include "get_bits.h"
33 #include "mathops.h"
34 #include "progressframe.h"
35 #include "put_bits.h"
36 #include "rangecoder.h"
37
38 #ifdef __INTEL_COMPILER
39 #undef av_flatten
40 #define av_flatten
41 #endif
42
43 #define MAX_PLANES 4
44 #define CONTEXT_SIZE 32
45
46 #define MAX_QUANT_TABLES 8
47 #define MAX_CONTEXT_INPUTS 5
48
49 #define AC_GOLOMB_RICE 0
50 #define AC_RANGE_DEFAULT_TAB 1
51 #define AC_RANGE_CUSTOM_TAB 2
52 #define AC_RANGE_DEFAULT_TAB_FORCE -2
53
54 typedef struct VlcState {
55 int16_t drift;
56 uint16_t error_sum;
57 int8_t bias;
58 uint8_t count;
59 } VlcState;
60
61 typedef struct PlaneContext {
62 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
63 int quant_table_index;
64 int context_count;
65 uint8_t (*state)[CONTEXT_SIZE];
66 VlcState *vlc_state;
67 uint8_t interlace_bit_state[2];
68 } PlaneContext;
69
70 #define MAX_SLICES 1024
71
72 typedef struct FFV1Context {
73 AVClass *class;
74 AVCodecContext *avctx;
75 RangeCoder c;
76 GetBitContext gb;
77 PutBitContext pb;
78 uint64_t rc_stat[256][2];
79 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
80 int version;
81 int micro_version;
82 int width, height;
83 int chroma_planes;
84 int chroma_h_shift, chroma_v_shift;
85 int transparency;
86 int flags;
87 int64_t picture_number;
88 int key_frame;
89 ProgressFrame picture, last_picture;
90 struct FFV1Context *fsrc;
91
92 AVFrame *cur;
93 const AVFrame *cur_enc_frame;
94 int plane_count;
95 int ac; ///< 1=range coder <-> 0=golomb rice
96 int ac_byte_count; ///< number of bytes used for AC coding
97 PlaneContext plane[MAX_PLANES];
98 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
99 int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
100 int context_count[MAX_QUANT_TABLES];
101 uint8_t state_transition[256];
102 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
103 int run_index;
104 int colorspace;
105 int16_t *sample_buffer;
106 int32_t *sample_buffer32;
107
108 int use32bit;
109
110 int ec;
111 int intra;
112 int slice_damaged;
113 int key_frame_ok;
114 int context_model;
115
116 int bits_per_raw_sample;
117 int packed_at_lsb;
118
119 int gob_count;
120 int quant_table_count;
121
122 struct FFV1Context *slice_context[MAX_SLICES];
123 int slice_count;
124 int max_slice_count;
125 int num_v_slices;
126 int num_h_slices;
127 int slice_width;
128 int slice_height;
129 int slice_x;
130 int slice_y;
131 int slice_reset_contexts;
132 int slice_coding_mode;
133 int slice_rct_by_coef;
134 int slice_rct_ry_coef;
135 } FFV1Context;
136
137 int ff_ffv1_common_init(AVCodecContext *avctx);
138 int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1Context *fs);
139 int ff_ffv1_init_slices_state(FFV1Context *f);
140 int ff_ffv1_init_slice_contexts(FFV1Context *f);
141 int ff_ffv1_allocate_initial_states(FFV1Context *f);
142 void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1Context *fs);
143 int ff_ffv1_close(AVCodecContext *avctx);
144
145 608057159 static av_always_inline int fold(int diff, int bits)
146 {
147
2/2
✓ Branch 0 taken 342847186 times.
✓ Branch 1 taken 265209973 times.
608057159 if (bits == 8)
148 342847186 diff = (int8_t)diff;
149 else {
150 265209973 diff = sign_extend(diff, bits);
151 }
152
153 608057159 return diff;
154 }
155
156 285677659 static inline void update_vlc_state(VlcState *const state, const int v)
157 {
158 285677659 int drift = state->drift;
159 285677659 int count = state->count;
160 285677659 state->error_sum += FFABS(v);
161 285677659 drift += v;
162
163
2/2
✓ Branch 0 taken 3962951 times.
✓ Branch 1 taken 281714708 times.
285677659 if (count == 128) { // FIXME: variable
164 3962951 count >>= 1;
165 3962951 drift >>= 1;
166 3962951 state->error_sum >>= 1;
167 }
168 285677659 count++;
169
170
2/2
✓ Branch 0 taken 9558159 times.
✓ Branch 1 taken 276119500 times.
285677659 if (drift <= -count) {
171
2/2
✓ Branch 0 taken 9552493 times.
✓ Branch 1 taken 5666 times.
9558159 state->bias = FFMAX(state->bias - 1, -128);
172
173 9558159 drift = FFMAX(drift + count, -count + 1);
174
2/2
✓ Branch 0 taken 8818816 times.
✓ Branch 1 taken 267300684 times.
276119500 } else if (drift > 0) {
175
2/2
✓ Branch 0 taken 8794617 times.
✓ Branch 1 taken 24199 times.
8818816 state->bias = FFMIN(state->bias + 1, 127);
176
177 8818816 drift = FFMIN(drift - count, 0);
178 }
179
180 285677659 state->drift = drift;
181 285677659 state->count = count;
182 285677659 }
183
184 #endif /* AVCODEC_FFV1_H */
185