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/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 | #define MAX_PLANES 4 | ||
45 | #define CONTEXT_SIZE 32 | ||
46 | |||
47 | #define MAX_QUANT_TABLES 8 | ||
48 | #define MAX_QUANT_TABLE_SIZE 256 | ||
49 | #define MAX_QUANT_TABLE_MASK (MAX_QUANT_TABLE_SIZE - 1) | ||
50 | #define MAX_CONTEXT_INPUTS 5 | ||
51 | |||
52 | #define AC_GOLOMB_RICE 0 | ||
53 | #define AC_RANGE_DEFAULT_TAB 1 | ||
54 | #define AC_RANGE_CUSTOM_TAB 2 | ||
55 | #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 | #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 | |||
143 | const AVFrame *cur_enc_frame; | ||
144 | int plane_count; | ||
145 | int ac; ///< 1=range coder <-> 0=golomb rice | ||
146 | int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]; | ||
147 | int context_count[MAX_QUANT_TABLES]; | ||
148 | uint8_t state_transition[256]; | ||
149 | uint8_t (*initial_states[MAX_QUANT_TABLES])[32]; | ||
150 | int colorspace; | ||
151 | int flt; | ||
152 | int remap_mode; | ||
153 | int remap_optimizer; | ||
154 | int maxsize_warned; | ||
155 | |||
156 | int use32bit; | ||
157 | |||
158 | int ec; | ||
159 | int intra; | ||
160 | int key_frame_ok; | ||
161 | int context_model; | ||
162 | int qtable; | ||
163 | |||
164 | int bits_per_raw_sample; | ||
165 | int packed_at_lsb; | ||
166 | |||
167 | int gob_count; | ||
168 | int quant_table_count; | ||
169 | |||
170 | int slice_count; | ||
171 | int max_slice_count; | ||
172 | int num_v_slices; | ||
173 | int num_h_slices; | ||
174 | |||
175 | FFV1SliceContext *slices; | ||
176 | /* RefStruct object, per-slice damage flags shared between frame threads. | ||
177 | * | ||
178 | * After a frame thread marks some slice as finished with | ||
179 | * ff_progress_frame_report(), the corresponding array element must not be | ||
180 | * accessed by this thread anymore, as from then on it is owned by the next | ||
181 | * thread. | ||
182 | */ | ||
183 | uint8_t *slice_damaged; | ||
184 | /* Frame damage flag, used to delay announcing progress, since ER is | ||
185 | * applied after all the slices are decoded. | ||
186 | * NOT shared between frame threads. | ||
187 | */ | ||
188 | uint8_t frame_damaged; | ||
189 | } FFV1Context; | ||
190 | |||
191 | int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s); | ||
192 | int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
193 | int ff_ffv1_init_slices_state(FFV1Context *f); | ||
194 | int ff_ffv1_init_slice_contexts(FFV1Context *f); | ||
195 | PlaneContext *ff_ffv1_planes_alloc(void); | ||
196 | int ff_ffv1_allocate_initial_states(FFV1Context *f); | ||
197 | void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
198 | void ff_ffv1_close(FFV1Context *s); | ||
199 | int ff_need_new_slices(int width, int num_h_slices, int chroma_shift); | ||
200 | int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state); | ||
201 | int ff_ffv1_read_extra_header(FFV1Context *f); | ||
202 | int ff_ffv1_read_quant_tables(RangeCoder *c, | ||
203 | int16_t quant_table[MAX_CONTEXT_INPUTS][256]); | ||
204 | void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int offset[1], int mask[4], int bits_per_raw_sample); | ||
205 | int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed); | ||
206 | |||
207 | /** | ||
208 | * This is intended for both width and height | ||
209 | */ | ||
210 | int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift); | ||
211 | |||
212 | 733670082 | static av_always_inline int fold(int diff, int bits) | |
213 | { | ||
214 |
2/2✓ Branch 0 taken 408253256 times.
✓ Branch 1 taken 325416826 times.
|
733670082 | if (bits == 8) |
215 | 408253256 | diff = (int8_t)diff; | |
216 | else { | ||
217 | 325416826 | diff = sign_extend(diff, bits); | |
218 | } | ||
219 | |||
220 | 733670082 | return diff; | |
221 | } | ||
222 | |||
223 | 327319982 | static inline void update_vlc_state(VlcState *const state, const int v) | |
224 | { | ||
225 | 327319982 | int drift = state->drift; | |
226 | 327319982 | int count = state->count; | |
227 | 327319982 | state->error_sum += FFABS(v); | |
228 | 327319982 | drift += v; | |
229 | |||
230 |
2/2✓ Branch 0 taken 4485770 times.
✓ Branch 1 taken 322834212 times.
|
327319982 | if (count == 128) { // FIXME: variable |
231 | 4485770 | count >>= 1; | |
232 | 4485770 | drift >>= 1; | |
233 | 4485770 | state->error_sum >>= 1; | |
234 | } | ||
235 | 327319982 | count++; | |
236 | |||
237 |
2/2✓ Branch 0 taken 11300080 times.
✓ Branch 1 taken 316019902 times.
|
327319982 | if (drift <= -count) { |
238 |
2/2✓ Branch 0 taken 11294414 times.
✓ Branch 1 taken 5666 times.
|
11300080 | state->bias = FFMAX(state->bias - 1, -128); |
239 | |||
240 | 11300080 | drift = FFMAX(drift + count, -count + 1); | |
241 |
2/2✓ Branch 0 taken 10297046 times.
✓ Branch 1 taken 305722856 times.
|
316019902 | } else if (drift > 0) { |
242 |
2/2✓ Branch 0 taken 10272954 times.
✓ Branch 1 taken 24092 times.
|
10297046 | state->bias = FFMIN(state->bias + 1, 127); |
243 | |||
244 | 10297046 | drift = FFMIN(drift - count, 0); | |
245 | } | ||
246 | |||
247 | 327319982 | state->drift = drift; | |
248 | 327319982 | state->count = count; | |
249 | 327319982 | } | |
250 | |||
251 | |||
252 | 180482384 | static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, | |
253 | int is_signed) | ||
254 | { | ||
255 |
2/2✓ Branch 1 taken 70640466 times.
✓ Branch 2 taken 109841918 times.
|
180482384 | if (get_rac(c, state + 0)) |
256 | 70640466 | return 0; | |
257 | else { | ||
258 | int e; | ||
259 | unsigned a; | ||
260 | 109841918 | e = 0; | |
261 |
4/4✓ Branch 0 taken 666366859 times.
✓ Branch 1 taken 45110687 times.
✓ Branch 3 taken 601635628 times.
✓ Branch 4 taken 109841918 times.
|
711477546 | while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10 |
262 | 601635628 | e++; | |
263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 601635628 times.
|
601635628 | if (e > 31) |
264 | ✗ | return AVERROR_INVALIDDATA; | |
265 | } | ||
266 | |||
267 | 109841918 | a = 1; | |
268 |
2/2✓ Branch 0 taken 601635628 times.
✓ Branch 1 taken 109841918 times.
|
711477546 | for (int i = e - 1; i >= 0; i--) |
269 |
2/2✓ Branch 0 taken 581487119 times.
✓ Branch 1 taken 20148509 times.
|
601635628 | a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31 |
270 | |||
271 |
6/6✓ Branch 0 taken 109812332 times.
✓ Branch 1 taken 29586 times.
✓ Branch 2 taken 98610141 times.
✓ Branch 3 taken 11202191 times.
✓ Branch 5 taken 60180373 times.
✓ Branch 6 taken 49631959 times.
|
109841918 | e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21 |
272 | 109841918 | return (a ^ e) - e; | |
273 | } | ||
274 | } | ||
275 | |||
276 | #endif /* AVCODEC_FFV1_H */ | ||
277 |