FFmpeg coverage


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