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 | uint16_t fltmap[4][65536]; | ||
110 | } FFV1SliceContext; | ||
111 | |||
112 | typedef struct FFV1Context { | ||
113 | AVClass *class; | ||
114 | AVCodecContext *avctx; | ||
115 | uint64_t rc_stat[256][2]; | ||
116 | uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; | ||
117 | int version; | ||
118 | int micro_version; | ||
119 | int combined_version; | ||
120 | int width, height; | ||
121 | int chroma_planes; | ||
122 | int chroma_h_shift, chroma_v_shift; | ||
123 | int transparency; | ||
124 | int flags; | ||
125 | int64_t picture_number; | ||
126 | int key_frame; | ||
127 | ProgressFrame picture, last_picture; | ||
128 | uint32_t crcref; | ||
129 | enum AVPixelFormat pix_fmt; | ||
130 | |||
131 | const AVFrame *cur_enc_frame; | ||
132 | int plane_count; | ||
133 | int ac; ///< 1=range coder <-> 0=golomb rice | ||
134 | int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]; | ||
135 | int context_count[MAX_QUANT_TABLES]; | ||
136 | uint8_t state_transition[256]; | ||
137 | uint8_t (*initial_states[MAX_QUANT_TABLES])[32]; | ||
138 | int colorspace; | ||
139 | int flt; | ||
140 | |||
141 | |||
142 | int use32bit; | ||
143 | |||
144 | int ec; | ||
145 | int intra; | ||
146 | int key_frame_ok; | ||
147 | int context_model; | ||
148 | int qtable; | ||
149 | |||
150 | int bits_per_raw_sample; | ||
151 | int packed_at_lsb; | ||
152 | |||
153 | int gob_count; | ||
154 | int quant_table_count; | ||
155 | |||
156 | int slice_count; | ||
157 | int max_slice_count; | ||
158 | int num_v_slices; | ||
159 | int num_h_slices; | ||
160 | |||
161 | FFV1SliceContext *slices; | ||
162 | /* RefStruct object, per-slice damage flags shared between frame threads. | ||
163 | * | ||
164 | * After a frame thread marks some slice as finished with | ||
165 | * ff_progress_frame_report(), the corresponding array element must not be | ||
166 | * accessed by this thread anymore, as from then on it is owned by the next | ||
167 | * thread. | ||
168 | */ | ||
169 | uint8_t *slice_damaged; | ||
170 | /* Frame damage flag, used to delay announcing progress, since ER is | ||
171 | * applied after all the slices are decoded. | ||
172 | * NOT shared between frame threads. | ||
173 | */ | ||
174 | uint8_t frame_damaged; | ||
175 | } FFV1Context; | ||
176 | |||
177 | int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s); | ||
178 | int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
179 | int ff_ffv1_init_slices_state(FFV1Context *f); | ||
180 | int ff_ffv1_init_slice_contexts(FFV1Context *f); | ||
181 | PlaneContext *ff_ffv1_planes_alloc(void); | ||
182 | int ff_ffv1_allocate_initial_states(FFV1Context *f); | ||
183 | void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
184 | void ff_ffv1_close(FFV1Context *s); | ||
185 | int ff_need_new_slices(int width, int num_h_slices, int chroma_shift); | ||
186 | int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state); | ||
187 | int ff_ffv1_read_extra_header(FFV1Context *f); | ||
188 | int ff_ffv1_read_quant_tables(RangeCoder *c, | ||
189 | int16_t quant_table[MAX_CONTEXT_INPUTS][256]); | ||
190 | int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed); | ||
191 | |||
192 | /** | ||
193 | * This is intended for both width and height | ||
194 | */ | ||
195 | int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift); | ||
196 | |||
197 | 737388618 | static av_always_inline int fold(int diff, int bits) | |
198 | { | ||
199 |
2/2✓ Branch 0 taken 411115045 times.
✓ Branch 1 taken 326273573 times.
|
737388618 | if (bits == 8) |
200 | 411115045 | diff = (int8_t)diff; | |
201 | else { | ||
202 | 326273573 | diff = sign_extend(diff, bits); | |
203 | } | ||
204 | |||
205 | 737388618 | return diff; | |
206 | } | ||
207 | |||
208 | 331045718 | static inline void update_vlc_state(VlcState *const state, const int v) | |
209 | { | ||
210 | 331045718 | int drift = state->drift; | |
211 | 331045718 | int count = state->count; | |
212 | 331045718 | state->error_sum += FFABS(v); | |
213 | 331045718 | drift += v; | |
214 | |||
215 |
2/2✓ Branch 0 taken 4550479 times.
✓ Branch 1 taken 326495239 times.
|
331045718 | if (count == 128) { // FIXME: variable |
216 | 4550479 | count >>= 1; | |
217 | 4550479 | drift >>= 1; | |
218 | 4550479 | state->error_sum >>= 1; | |
219 | } | ||
220 | 331045718 | count++; | |
221 | |||
222 |
2/2✓ Branch 0 taken 11349025 times.
✓ Branch 1 taken 319696693 times.
|
331045718 | if (drift <= -count) { |
223 |
2/2✓ Branch 0 taken 11343359 times.
✓ Branch 1 taken 5666 times.
|
11349025 | state->bias = FFMAX(state->bias - 1, -128); |
224 | |||
225 | 11349025 | drift = FFMAX(drift + count, -count + 1); | |
226 |
2/2✓ Branch 0 taken 10404865 times.
✓ Branch 1 taken 309291828 times.
|
319696693 | } else if (drift > 0) { |
227 |
2/2✓ Branch 0 taken 10380666 times.
✓ Branch 1 taken 24199 times.
|
10404865 | state->bias = FFMIN(state->bias + 1, 127); |
228 | |||
229 | 10404865 | drift = FFMIN(drift - count, 0); | |
230 | } | ||
231 | |||
232 | 331045718 | state->drift = drift; | |
233 | 331045718 | state->count = count; | |
234 | 331045718 | } | |
235 | |||
236 | |||
237 | 187679296 | static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, | |
238 | int is_signed) | ||
239 | { | ||
240 |
2/2✓ Branch 1 taken 74868466 times.
✓ Branch 2 taken 112810830 times.
|
187679296 | if (get_rac(c, state + 0)) |
241 | 74868466 | return 0; | |
242 | else { | ||
243 | int e; | ||
244 | unsigned a; | ||
245 | 112810830 | e = 0; | |
246 |
4/4✓ Branch 0 taken 680844844 times.
✓ Branch 1 taken 45940109 times.
✓ Branch 3 taken 613974123 times.
✓ Branch 4 taken 112810830 times.
|
726784953 | while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10 |
247 | 613974123 | e++; | |
248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 613974123 times.
|
613974123 | if (e > 31) |
249 | ✗ | return AVERROR_INVALIDDATA; | |
250 | } | ||
251 | |||
252 | 112810830 | a = 1; | |
253 |
2/2✓ Branch 0 taken 613974123 times.
✓ Branch 1 taken 112810830 times.
|
726784953 | for (int i = e - 1; i >= 0; i--) |
254 |
2/2✓ Branch 0 taken 593460226 times.
✓ Branch 1 taken 20513897 times.
|
613974123 | a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31 |
255 | |||
256 |
6/6✓ Branch 0 taken 112781314 times.
✓ Branch 1 taken 29516 times.
✓ Branch 2 taken 101378922 times.
✓ Branch 3 taken 11402392 times.
✓ Branch 5 taken 61789051 times.
✓ Branch 6 taken 50992263 times.
|
112810830 | e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21 |
257 | 112810830 | return (a ^ e) - e; | |
258 | } | ||
259 | } | ||
260 | |||
261 | #endif /* AVCODEC_FFV1_H */ | ||
262 |