/src/ffmpeg/libavcodec/cbs_vp8.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of FFmpeg. |
3 | | * |
4 | | * FFmpeg is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * FFmpeg is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with FFmpeg; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | */ |
18 | | |
19 | | #include "libavutil/avassert.h" |
20 | | |
21 | | #include "cbs.h" |
22 | | #include "cbs_internal.h" |
23 | | #include "cbs_vp8.h" |
24 | | |
25 | | #include <stdbool.h> |
26 | | |
27 | | #define DEFAULT_PROB 0x80 |
28 | | |
29 | | // The probability table is defined in 'vp8data.c'. |
30 | | extern const uint8_t ff_vp8_token_update_probs[4][8][3][11]; |
31 | | |
32 | | // Implements VP8 boolean decoder using GetBitContext to read the bitstream. |
33 | | typedef struct CBSVP8BoolDecoder { |
34 | | GetBitContext *gbc; |
35 | | |
36 | | uint8_t value; |
37 | | uint8_t range; |
38 | | |
39 | | uint8_t count; // Store the number of bits in the `value` buffer. |
40 | | |
41 | | } CBSVP8BoolDecoder; |
42 | | |
43 | | static int cbs_vp8_bool_decoder_init(CBSVP8BoolDecoder *decoder, GetBitContext *gbc) |
44 | 92.5k | { |
45 | 92.5k | av_assert0(decoder); |
46 | 92.5k | av_assert0(gbc); |
47 | | |
48 | 92.5k | decoder->gbc = gbc; |
49 | 92.5k | decoder->value = 0; |
50 | 92.5k | decoder->range = 255; |
51 | | |
52 | 92.5k | decoder->count = 0; |
53 | | |
54 | 92.5k | return 0; |
55 | 92.5k | } |
56 | | |
57 | | static bool cbs_vp8_bool_decoder_fill_value(CBSVP8BoolDecoder *decoder) |
58 | 78.6M | { |
59 | 78.6M | int bits = 8 - decoder->count; |
60 | | |
61 | 78.6M | av_assert0(decoder->count <= 8); |
62 | 78.6M | if (decoder->count == 8) { |
63 | 60.7M | return true; |
64 | 60.7M | } |
65 | | |
66 | 17.8M | if (get_bits_left(decoder->gbc) >= bits) { |
67 | 17.7M | decoder->value |= get_bits(decoder->gbc, bits); |
68 | 17.7M | decoder->count += bits; |
69 | 17.7M | } |
70 | | |
71 | 17.8M | return (decoder->count == 8); |
72 | 78.6M | } |
73 | | |
74 | | static int cbs_vp8_bool_decoder_read_bool(CBSVP8BoolDecoder *decoder, |
75 | | const uint8_t prob, uint8_t *output) |
76 | 78.6M | { |
77 | 78.6M | uint8_t split = 1 + (((decoder->range - 1) * prob) >> 8); |
78 | | |
79 | 78.6M | if (!cbs_vp8_bool_decoder_fill_value(decoder)) { |
80 | 59.5k | return AVERROR_INVALIDDATA; |
81 | 59.5k | } |
82 | | |
83 | 78.5M | av_assert0(decoder->count == 8); |
84 | 78.5M | if (decoder->value >= split) { |
85 | 8.08M | *output = 1; |
86 | 8.08M | decoder->range -= split; |
87 | 8.08M | decoder->value -= split; |
88 | 70.4M | } else { |
89 | 70.4M | *output = 0; |
90 | 70.4M | decoder->range = split; |
91 | 70.4M | } |
92 | | |
93 | 98.0M | while (decoder->range < 128) { |
94 | 19.4M | decoder->value <<= 1; |
95 | 19.4M | decoder->range <<= 1; |
96 | 19.4M | --decoder->count; |
97 | 19.4M | } |
98 | | |
99 | 78.5M | return 0; |
100 | 78.5M | } |
101 | | |
102 | | static int cbs_vp8_bool_decoder_read_literal(CBSVP8BoolDecoder *decoder, |
103 | | const uint8_t prob, |
104 | | uint32_t num_bits, |
105 | | uint32_t *output) |
106 | 67.1M | { |
107 | 67.1M | int ret = 0; |
108 | | |
109 | 67.1M | av_assert0(num_bits <= 32); |
110 | | |
111 | 67.1M | *output = 0; |
112 | 145M | for (; num_bits > 0; --num_bits) { |
113 | 78.3M | uint8_t bit_output = 0; |
114 | 78.3M | if ((ret = cbs_vp8_bool_decoder_read_bool(decoder, prob, |
115 | 78.3M | &bit_output)) != 0) { |
116 | 58.3k | return ret; |
117 | 58.3k | } |
118 | | |
119 | 78.2M | *output = (*output << 1) | bit_output; |
120 | 78.2M | } |
121 | | |
122 | 67.0M | return 0; |
123 | 67.1M | } |
124 | | |
125 | | static int cbs_vp8_bool_decoder_read_unsigned( |
126 | | CodedBitstreamContext *ctx, CBSVP8BoolDecoder *bool_decoder, int width, |
127 | | uint8_t prob, const char *name, const int *subscripts, uint32_t *write_to, |
128 | | bool trace_enable) |
129 | 66.8M | { |
130 | 66.8M | int ret = 0; |
131 | 66.8M | GetBitContext *gbc = bool_decoder->gbc; |
132 | 66.8M | uint32_t value; |
133 | | |
134 | 66.8M | CBS_TRACE_READ_START(); |
135 | | |
136 | 66.8M | av_assert0(width >= 0 && width <= 8); |
137 | | |
138 | 66.8M | ret = cbs_vp8_bool_decoder_read_literal(bool_decoder, prob, width, &value); |
139 | 66.8M | if (ret != 0) { |
140 | 51.2k | return ret; |
141 | 51.2k | } |
142 | | |
143 | 66.8M | if (trace_enable) { |
144 | 2.56M | CBS_TRACE_READ_END(); |
145 | 2.56M | } |
146 | | |
147 | 66.8M | *write_to = value; |
148 | 66.8M | return 0; |
149 | 66.8M | } |
150 | | |
151 | | static int cbs_vp8_bool_decoder_read_signed( |
152 | | CodedBitstreamContext *ctx, CBSVP8BoolDecoder *bool_decoder, int width, |
153 | | uint8_t prob, const char *name, const int *subscripts, int32_t *write_to) |
154 | 287k | { |
155 | 287k | int ret = 0; |
156 | 287k | GetBitContext *gbc = bool_decoder->gbc; |
157 | 287k | int32_t value; |
158 | 287k | uint8_t sign = 0; |
159 | | |
160 | 287k | CBS_TRACE_READ_START(); |
161 | | |
162 | 287k | av_assert0(width >= 0 && width <= 8); |
163 | | |
164 | 287k | ret = cbs_vp8_bool_decoder_read_literal(bool_decoder, prob, width, &value); |
165 | 287k | if (ret != 0) { |
166 | 7.09k | return ret; |
167 | 7.09k | } |
168 | | |
169 | 280k | ret = cbs_vp8_bool_decoder_read_bool(bool_decoder, prob, &sign); |
170 | 280k | if (ret != 0) { |
171 | 1.28k | return ret; |
172 | 1.28k | } |
173 | | |
174 | 279k | if (sign) { |
175 | 138k | value = -value; |
176 | 138k | } |
177 | | |
178 | 279k | CBS_TRACE_READ_END(); |
179 | | |
180 | 279k | *write_to = value; |
181 | 279k | return 0; |
182 | 279k | } |
183 | | |
184 | | static int cbs_vp8_read_unsigned_le(CodedBitstreamContext *ctx, |
185 | | GetBitContext *gbc, int width, |
186 | | const char *name, const int *subscripts, |
187 | | uint32_t *write_to, uint32_t range_min, |
188 | | uint32_t range_max) |
189 | 1.21M | { |
190 | 1.21M | int32_t value; |
191 | | |
192 | 1.21M | CBS_TRACE_READ_START(); |
193 | | |
194 | 1.21M | av_assert0(width > 0 && width <= 24); |
195 | | |
196 | 1.21M | if (get_bits_left(gbc) < width) { |
197 | 20.2k | av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value: bitstream ended.\n"); |
198 | 20.2k | return AVERROR_INVALIDDATA; |
199 | 20.2k | } |
200 | | |
201 | 1.19M | value = get_bits_le(gbc, width); |
202 | | |
203 | 1.19M | CBS_TRACE_READ_END(); |
204 | | |
205 | 1.19M | if (value < range_min || value > range_max) { |
206 | 150k | av_log(ctx->log_ctx, AV_LOG_ERROR, |
207 | 150k | "%s out of range: " |
208 | 150k | "%" PRIu32 ", but must be in [%" PRIu32 ",%" PRIu32 "].\n", |
209 | 150k | name, value, range_min, range_max); |
210 | 150k | return AVERROR_INVALIDDATA; |
211 | 150k | } |
212 | | |
213 | 1.04M | *write_to = value; |
214 | 1.04M | return 0; |
215 | 1.19M | } |
216 | | |
217 | | #define HEADER(name) \ |
218 | 263k | do { \ |
219 | 263k | ff_cbs_trace_header(ctx, name); \ |
220 | 263k | } while (0) |
221 | | |
222 | | #define CHECK(call) \ |
223 | 69.0M | do { \ |
224 | 136M | int err = (call); \ |
225 | 69.0M | if (err < 0) \ |
226 | 69.0M | return err; \ |
227 | 69.0M | } while (0) |
228 | | |
229 | | #define FUNC_NAME(rw, codec, name) cbs_##codec##_##rw##_##name |
230 | | #define FUNC_VP8(rw, name) FUNC_NAME(rw, vp8, name) |
231 | | #define FUNC(name) FUNC_VP8(READWRITE, name) |
232 | | |
233 | | #define SUBSCRIPTS(subs, ...) \ |
234 | | (subs > 0 ? ((int[subs + 1]){subs, __VA_ARGS__}) : NULL) |
235 | | |
236 | 1.05M | #define f(width, name) xf(width, name, 0, ) |
237 | | |
238 | | // bool [de|en]coder methods. |
239 | 1.39M | #define bc_f(width, name) bc_unsigned_subs(width, DEFAULT_PROB, true, name, 0, ) |
240 | 153k | #define bc_s(width, name) bc_signed_subs(width, DEFAULT_PROB, name, 0, ) |
241 | | #define bc_fs(width, name, subs, ...) \ |
242 | 1.20M | bc_unsigned_subs(width, DEFAULT_PROB, true, name, subs, __VA_ARGS__) |
243 | | #define bc_ss(width, name, subs, ...) \ |
244 | 134k | bc_signed_subs(width, DEFAULT_PROB, name, subs, __VA_ARGS__) |
245 | | |
246 | | // bool [de|en]coder methods for boolean value and disable tracing. |
247 | 2.33M | #define bc_b(name) bc_unsigned_subs(1, DEFAULT_PROB, false, name, 0, ) |
248 | 61.9M | #define bc_b_prob(prob, name) bc_unsigned_subs(1, prob, false, name, 0, ) |
249 | | |
250 | | #define READ |
251 | | #define READWRITE read |
252 | | #define RWContext GetBitContext |
253 | | #define CBSVP8BoolCodingRW CBSVP8BoolDecoder |
254 | | |
255 | | #define xf(width, name, subs, ...) \ |
256 | 1.05M | do { \ |
257 | 1.05M | uint32_t value; \ |
258 | 1.05M | CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, \ |
259 | 1.05M | SUBSCRIPTS(subs, __VA_ARGS__), &value, \ |
260 | 1.05M | 0, MAX_UINT_BITS(width))); \ |
261 | 1.05M | current->name = value; \ |
262 | 1.03M | } while (0) |
263 | | |
264 | | #define fixed(width, name, value) \ |
265 | 155k | do { \ |
266 | 155k | uint32_t fixed_value; \ |
267 | 155k | CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, 0, &fixed_value, \ |
268 | 155k | value, value)); \ |
269 | 155k | } while (0) |
270 | | |
271 | | #define bc_unsigned_subs(width, prob, enable_trace, name, subs, ...) \ |
272 | 66.8M | do { \ |
273 | 66.8M | uint32_t value; \ |
274 | 66.8M | CHECK(cbs_vp8_bool_decoder_read_unsigned( \ |
275 | 66.8M | ctx, bool_coding_rw, width, prob, #name, \ |
276 | 66.8M | SUBSCRIPTS(subs, __VA_ARGS__), &value, enable_trace)); \ |
277 | 66.8M | current->name = value; \ |
278 | 66.8M | } while (0) |
279 | | |
280 | | #define bc_signed_subs(width, prob, name, subs, ...) \ |
281 | 287k | do { \ |
282 | 287k | int32_t value; \ |
283 | 287k | CHECK(cbs_vp8_bool_decoder_read_signed( \ |
284 | 287k | ctx, bool_coding_rw, width, prob, #name, \ |
285 | 287k | SUBSCRIPTS(subs, __VA_ARGS__), &value)); \ |
286 | 287k | current->name = value; \ |
287 | 279k | } while (0) |
288 | | |
289 | | #include "cbs_vp8_syntax_template.c" |
290 | | |
291 | | static int cbs_vp8_split_fragment(CodedBitstreamContext *ctx, |
292 | | CodedBitstreamFragment *frag, int header) |
293 | 264k | { |
294 | 264k | int err; |
295 | | |
296 | 264k | if (frag->data_size == 0) |
297 | 1.13k | return AVERROR_INVALIDDATA; |
298 | | |
299 | 263k | err = ff_cbs_append_unit_data(frag, 0, frag->data, frag->data_size, |
300 | 263k | frag->data_ref); |
301 | 263k | if (err < 0) |
302 | 0 | return err; |
303 | | |
304 | 263k | return 0; |
305 | 263k | } |
306 | | |
307 | | static int cbs_vp8_read_unit(CodedBitstreamContext *ctx, |
308 | | CodedBitstreamUnit *unit) |
309 | 263k | { |
310 | 263k | VP8RawFrame *frame; |
311 | 263k | GetBitContext gbc; |
312 | 263k | CBSVP8BoolDecoder bool_decoder; |
313 | 263k | int err, pos; |
314 | | |
315 | 263k | err = ff_cbs_alloc_unit_content(ctx, unit); |
316 | 263k | if (err < 0) |
317 | 0 | return err; |
318 | 263k | frame = unit->content; |
319 | | |
320 | | // Create GetBitContext for uncompressed header. |
321 | 263k | err = init_get_bits8_le(&gbc, unit->data, unit->data_size); |
322 | 263k | if (err < 0) |
323 | 0 | return err; |
324 | | |
325 | 263k | err = cbs_vp8_read_uncompressed_header(ctx, &gbc, frame); |
326 | 263k | if (err < 0) |
327 | 171k | return err; |
328 | | |
329 | 92.5k | pos = get_bits_count(&gbc); |
330 | 92.5k | av_assert0(pos % 8 == 0); |
331 | | |
332 | | // Create boolean decoder for compressed header. |
333 | 92.5k | err = cbs_vp8_bool_decoder_init(&bool_decoder, &gbc); |
334 | 92.5k | if (err < 0) |
335 | 0 | return err; |
336 | | |
337 | 92.5k | err = cbs_vp8_read_compressed_header(ctx, &bool_decoder, frame); |
338 | 92.5k | if (err < 0) |
339 | 59.5k | return err; |
340 | | |
341 | 32.9k | pos = get_bits_count(&gbc); |
342 | | // Position may not be byte-aligned after compressed header; Round up byte |
343 | | // count for accurate data positioning. |
344 | 32.9k | pos = (pos + 7) / 8; |
345 | 32.9k | av_assert0(pos <= unit->data_size); |
346 | | |
347 | 32.9k | frame->data_ref = av_buffer_ref(unit->data_ref); |
348 | 32.9k | if (!frame->data_ref) |
349 | 0 | return AVERROR(ENOMEM); |
350 | | |
351 | 32.9k | frame->data = unit->data + pos; |
352 | 32.9k | frame->data_size = unit->data_size - pos; |
353 | | |
354 | 32.9k | return 0; |
355 | 32.9k | } |
356 | | |
357 | | static int cbs_vp8_write_unit(CodedBitstreamContext *ctx, |
358 | | CodedBitstreamUnit *unit, PutBitContext *pbc) |
359 | 0 | { |
360 | 0 | return AVERROR_PATCHWELCOME; |
361 | 0 | } |
362 | | |
363 | | static int cbs_vp8_assemble_fragment(CodedBitstreamContext *ctx, |
364 | | CodedBitstreamFragment *frag) |
365 | 0 | { |
366 | 0 | return AVERROR_PATCHWELCOME; |
367 | 0 | } |
368 | | |
369 | | static CodedBitstreamUnitTypeDescriptor cbs_vp8_unit_types[] = { |
370 | | CBS_UNIT_TYPE_INTERNAL_REF(0, VP8RawFrame, data), |
371 | | CBS_UNIT_TYPE_END_OF_LIST, |
372 | | }; |
373 | | |
374 | | const CodedBitstreamType ff_cbs_type_vp8 = { |
375 | | .codec_id = AV_CODEC_ID_VP8, |
376 | | |
377 | | .priv_data_size = 0, |
378 | | |
379 | | .unit_types = cbs_vp8_unit_types, |
380 | | |
381 | | .split_fragment = &cbs_vp8_split_fragment, |
382 | | .read_unit = &cbs_vp8_read_unit, |
383 | | .write_unit = &cbs_vp8_write_unit, |
384 | | |
385 | | .assemble_fragment = &cbs_vp8_assemble_fragment, |
386 | | }; |