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_QUANT_TABLE_SIZE 256 | ||
48 | #define MAX_QUANT_TABLE_MASK (MAX_QUANT_TABLE_SIZE - 1) | ||
49 | #define MAX_CONTEXT_INPUTS 5 | ||
50 | |||
51 | #define AC_GOLOMB_RICE 0 | ||
52 | #define AC_RANGE_DEFAULT_TAB 1 | ||
53 | #define AC_RANGE_CUSTOM_TAB 2 | ||
54 | #define AC_RANGE_DEFAULT_TAB_FORCE -2 | ||
55 | |||
56 | typedef struct VlcState { | ||
57 | uint32_t error_sum; | ||
58 | int16_t drift; | ||
59 | int8_t bias; | ||
60 | uint8_t count; | ||
61 | } VlcState; | ||
62 | |||
63 | typedef struct PlaneContext { | ||
64 | int quant_table_index; | ||
65 | int context_count; | ||
66 | uint8_t (*state)[CONTEXT_SIZE]; | ||
67 | VlcState *vlc_state; | ||
68 | } PlaneContext; | ||
69 | |||
70 | #define MAX_SLICES 1024 | ||
71 | |||
72 | typedef struct FFV1SliceContext { | ||
73 | int16_t *sample_buffer; | ||
74 | int32_t *sample_buffer32; | ||
75 | |||
76 | int slice_width; | ||
77 | int slice_height; | ||
78 | int slice_x; | ||
79 | int slice_y; | ||
80 | int sx, sy; | ||
81 | |||
82 | int run_index; | ||
83 | int slice_coding_mode; | ||
84 | int slice_rct_by_coef; | ||
85 | int slice_rct_ry_coef; | ||
86 | |||
87 | // RefStruct reference, array of MAX_PLANES elements | ||
88 | PlaneContext *plane; | ||
89 | PutBitContext pb; | ||
90 | RangeCoder c; | ||
91 | |||
92 | int ac_byte_count; ///< number of bytes used for AC coding | ||
93 | |||
94 | union { | ||
95 | // decoder-only | ||
96 | struct { | ||
97 | int slice_reset_contexts; | ||
98 | int slice_damaged; | ||
99 | }; | ||
100 | |||
101 | // encoder-only | ||
102 | struct { | ||
103 | uint64_t rc_stat[256][2]; | ||
104 | uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; | ||
105 | }; | ||
106 | }; | ||
107 | } FFV1SliceContext; | ||
108 | |||
109 | typedef struct FFV1Context { | ||
110 | AVClass *class; | ||
111 | AVCodecContext *avctx; | ||
112 | uint64_t rc_stat[256][2]; | ||
113 | uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; | ||
114 | int version; | ||
115 | int micro_version; | ||
116 | int width, height; | ||
117 | int chroma_planes; | ||
118 | int chroma_h_shift, chroma_v_shift; | ||
119 | int transparency; | ||
120 | int flags; | ||
121 | int64_t picture_number; | ||
122 | int key_frame; | ||
123 | ProgressFrame picture, last_picture; | ||
124 | uint32_t crcref; | ||
125 | |||
126 | const AVFrame *cur_enc_frame; | ||
127 | int plane_count; | ||
128 | int ac; ///< 1=range coder <-> 0=golomb rice | ||
129 | int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]; | ||
130 | int context_count[MAX_QUANT_TABLES]; | ||
131 | uint8_t state_transition[256]; | ||
132 | uint8_t (*initial_states[MAX_QUANT_TABLES])[32]; | ||
133 | int colorspace; | ||
134 | |||
135 | int use32bit; | ||
136 | |||
137 | int ec; | ||
138 | int intra; | ||
139 | int key_frame_ok; | ||
140 | int context_model; | ||
141 | int qtable; | ||
142 | |||
143 | int bits_per_raw_sample; | ||
144 | int packed_at_lsb; | ||
145 | |||
146 | int gob_count; | ||
147 | int quant_table_count; | ||
148 | |||
149 | int slice_count; | ||
150 | int max_slice_count; | ||
151 | int num_v_slices; | ||
152 | int num_h_slices; | ||
153 | |||
154 | FFV1SliceContext *slices; | ||
155 | /* RefStruct object, per-slice damage flags shared between frame threads. | ||
156 | * | ||
157 | * After a frame thread marks some slice as finished with | ||
158 | * ff_progress_frame_report(), the corresponding array element must not be | ||
159 | * accessed by this thread anymore, as from then on it is owned by the next | ||
160 | * thread. | ||
161 | */ | ||
162 | uint8_t *slice_damaged; | ||
163 | /* Frame damage flag, used to delay announcing progress, since ER is | ||
164 | * applied after all the slices are decoded. | ||
165 | * NOT shared between frame threads. | ||
166 | */ | ||
167 | uint8_t frame_damaged; | ||
168 | } FFV1Context; | ||
169 | |||
170 | int ff_ffv1_common_init(AVCodecContext *avctx); | ||
171 | int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
172 | int ff_ffv1_init_slices_state(FFV1Context *f); | ||
173 | int ff_ffv1_init_slice_contexts(FFV1Context *f); | ||
174 | PlaneContext *ff_ffv1_planes_alloc(void); | ||
175 | int ff_ffv1_allocate_initial_states(FFV1Context *f); | ||
176 | void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc); | ||
177 | int ff_ffv1_close(AVCodecContext *avctx); | ||
178 | int ff_need_new_slices(int width, int num_h_slices, int chroma_shift); | ||
179 | |||
180 | /** | ||
181 | * This is intended for both width and height | ||
182 | */ | ||
183 | int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift); | ||
184 | |||
185 | 737388618 | static av_always_inline int fold(int diff, int bits) | |
186 | { | ||
187 |
2/2✓ Branch 0 taken 411115045 times.
✓ Branch 1 taken 326273573 times.
|
737388618 | if (bits == 8) |
188 | 411115045 | diff = (int8_t)diff; | |
189 | else { | ||
190 | 326273573 | diff = sign_extend(diff, bits); | |
191 | } | ||
192 | |||
193 | 737388618 | return diff; | |
194 | } | ||
195 | |||
196 | 331045718 | static inline void update_vlc_state(VlcState *const state, const int v) | |
197 | { | ||
198 | 331045718 | int drift = state->drift; | |
199 | 331045718 | int count = state->count; | |
200 | 331045718 | state->error_sum += FFABS(v); | |
201 | 331045718 | drift += v; | |
202 | |||
203 |
2/2✓ Branch 0 taken 4550479 times.
✓ Branch 1 taken 326495239 times.
|
331045718 | if (count == 128) { // FIXME: variable |
204 | 4550479 | count >>= 1; | |
205 | 4550479 | drift >>= 1; | |
206 | 4550479 | state->error_sum >>= 1; | |
207 | } | ||
208 | 331045718 | count++; | |
209 | |||
210 |
2/2✓ Branch 0 taken 11349025 times.
✓ Branch 1 taken 319696693 times.
|
331045718 | if (drift <= -count) { |
211 |
2/2✓ Branch 0 taken 11343359 times.
✓ Branch 1 taken 5666 times.
|
11349025 | state->bias = FFMAX(state->bias - 1, -128); |
212 | |||
213 | 11349025 | drift = FFMAX(drift + count, -count + 1); | |
214 |
2/2✓ Branch 0 taken 10404865 times.
✓ Branch 1 taken 309291828 times.
|
319696693 | } else if (drift > 0) { |
215 |
2/2✓ Branch 0 taken 10380666 times.
✓ Branch 1 taken 24199 times.
|
10404865 | state->bias = FFMIN(state->bias + 1, 127); |
216 | |||
217 | 10404865 | drift = FFMIN(drift - count, 0); | |
218 | } | ||
219 | |||
220 | 331045718 | state->drift = drift; | |
221 | 331045718 | state->count = count; | |
222 | 331045718 | } | |
223 | |||
224 | #endif /* AVCODEC_FFV1_H */ | ||
225 |