FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_vp8.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 0 123 0.0%
Functions: 0 12 0.0%
Branches: 0 86 0.0%

Line Branch Exec 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 {
45 av_assert0(decoder);
46 av_assert0(gbc);
47
48 decoder->gbc = gbc;
49 decoder->value = 0;
50 decoder->range = 255;
51
52 decoder->count = 0;
53
54 return 0;
55 }
56
57 static bool cbs_vp8_bool_decoder_fill_value(CBSVP8BoolDecoder *decoder)
58 {
59 int bits = 8 - decoder->count;
60
61 av_assert0(decoder->count <= 8);
62 if (decoder->count == 8) {
63 return true;
64 }
65
66 if (get_bits_left(decoder->gbc) >= bits) {
67 decoder->value |= get_bits(decoder->gbc, bits);
68 decoder->count += bits;
69 }
70
71 return (decoder->count == 8);
72 }
73
74 static int cbs_vp8_bool_decoder_read_bool(CBSVP8BoolDecoder *decoder,
75 const uint8_t prob, uint8_t *output)
76 {
77 uint8_t split = 1 + (((decoder->range - 1) * prob) >> 8);
78
79 if (!cbs_vp8_bool_decoder_fill_value(decoder)) {
80 return AVERROR_INVALIDDATA;
81 }
82
83 av_assert0(decoder->count == 8);
84 if (decoder->value >= split) {
85 *output = 1;
86 decoder->range -= split;
87 decoder->value -= split;
88 } else {
89 *output = 0;
90 decoder->range = split;
91 }
92
93 while (decoder->range < 128) {
94 decoder->value <<= 1;
95 decoder->range <<= 1;
96 --decoder->count;
97 }
98
99 return 0;
100 }
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 {
107 int ret = 0;
108
109 av_assert0(num_bits <= 32);
110
111 *output = 0;
112 for (; num_bits > 0; --num_bits) {
113 uint8_t bit_output = 0;
114 if ((ret = cbs_vp8_bool_decoder_read_bool(decoder, prob,
115 &bit_output)) != 0) {
116 return ret;
117 }
118
119 *output = (*output << 1) | bit_output;
120 }
121
122 return 0;
123 }
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 {
130 int ret = 0;
131 GetBitContext *gbc = bool_decoder->gbc;
132 uint32_t value;
133
134 CBS_TRACE_READ_START();
135
136 av_assert0(width >= 0 && width <= 8);
137
138 ret = cbs_vp8_bool_decoder_read_literal(bool_decoder, prob, width, &value);
139 if (ret != 0) {
140 return ret;
141 }
142
143 if (trace_enable) {
144 CBS_TRACE_READ_END();
145 }
146
147 *write_to = value;
148 return 0;
149 }
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 {
155 int ret = 0;
156 GetBitContext *gbc = bool_decoder->gbc;
157 int32_t value;
158 uint8_t sign = 0;
159
160 CBS_TRACE_READ_START();
161
162 av_assert0(width >= 0 && width <= 8);
163
164 ret = cbs_vp8_bool_decoder_read_literal(bool_decoder, prob, width, &value);
165 if (ret != 0) {
166 return ret;
167 }
168
169 ret = cbs_vp8_bool_decoder_read_bool(bool_decoder, prob, &sign);
170 if (ret != 0) {
171 return ret;
172 }
173
174 if (sign) {
175 value = -value;
176 }
177
178 CBS_TRACE_READ_END();
179
180 *write_to = value;
181 return 0;
182 }
183
184 static int cbs_vp8_read_unsigned_le(CodedBitstreamContext *ctx, GetBitContext *gbc,
185 int width, const char *name,
186 const int *subscripts, uint32_t *write_to)
187 {
188 int32_t value;
189
190 CBS_TRACE_READ_START();
191
192 av_assert0(width > 0 && width <= 24);
193
194 if (get_bits_left(gbc) < width) {
195 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value: bitstream ended.\n");
196 return AVERROR_INVALIDDATA;
197 }
198
199 value = get_bits_le(gbc, width);
200
201 CBS_TRACE_READ_END();
202
203 *write_to = value;
204 return 0;
205 }
206
207 #define HEADER(name) \
208 do { \
209 ff_cbs_trace_header(ctx, name); \
210 } while (0)
211
212 #define CHECK(call) \
213 do { \
214 int err = (call); \
215 if (err < 0) \
216 return err; \
217 } while (0)
218
219 #define FUNC_NAME(rw, codec, name) cbs_##codec##_##rw##_##name
220 #define FUNC_VP8(rw, name) FUNC_NAME(rw, vp8, name)
221 #define FUNC(name) FUNC_VP8(READWRITE, name)
222
223 #define SUBSCRIPTS(subs, ...) \
224 (subs > 0 ? ((int[subs + 1]){subs, __VA_ARGS__}) : NULL)
225
226 #define f(width, name) xf(width, name, 0)
227
228 // bool [de|en]coder methods.
229 #define bc_f(width, name) bc_unsigned_subs(width, DEFAULT_PROB, true, name, 0)
230 #define bc_s(width, name) bc_signed_subs(width, DEFAULT_PROB, name, 0)
231 #define bc_fs(width, name, subs, ...) \
232 bc_unsigned_subs(width, DEFAULT_PROB, true, name, subs, __VA_ARGS__)
233 #define bc_ss(width, name, subs, ...) \
234 bc_signed_subs(width, DEFAULT_PROB, name, subs, __VA_ARGS__)
235
236 // bool [de|en]coder methods for boolean value and disable tracing.
237 #define bc_b(name) bc_unsigned_subs(1, DEFAULT_PROB, false, name, 0)
238 #define bc_b_prob(prob, name) bc_unsigned_subs(1, prob, false, name, 0)
239
240 #define READ
241 #define READWRITE read
242 #define RWContext GetBitContext
243 #define CBSVP8BoolCodingRW CBSVP8BoolDecoder
244
245 #define xf(width, name, subs, ...) \
246 do { \
247 uint32_t value; \
248 CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, \
249 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
250 current->name = value; \
251 } while (0)
252
253 #define fixed(width, name, value) \
254 do { \
255 uint32_t fixed_value; \
256 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, 0, &fixed_value, \
257 value, value)); \
258 } while (0)
259
260 #define bc_unsigned_subs(width, prob, enable_trace, name, subs, ...) \
261 do { \
262 uint32_t value; \
263 CHECK(cbs_vp8_bool_decoder_read_unsigned( \
264 ctx, bool_coding_rw, width, prob, #name, \
265 SUBSCRIPTS(subs, __VA_ARGS__), &value, enable_trace)); \
266 current->name = value; \
267 } while (0)
268
269 #define bc_signed_subs(width, prob, name, subs, ...) \
270 do { \
271 int32_t value; \
272 CHECK(cbs_vp8_bool_decoder_read_signed( \
273 ctx, bool_coding_rw, width, prob, #name, \
274 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
275 current->name = value; \
276 } while (0)
277
278 #include "cbs_vp8_syntax_template.c"
279
280 static int cbs_vp8_split_fragment(CodedBitstreamContext *ctx,
281 CodedBitstreamFragment *frag, int header)
282 {
283 int err;
284
285 if (frag->data_size == 0)
286 return AVERROR_INVALIDDATA;
287
288 err = ff_cbs_append_unit_data(frag, 0, frag->data, frag->data_size,
289 frag->data_ref);
290 if (err < 0)
291 return err;
292
293 return 0;
294 }
295
296 static int cbs_vp8_read_unit(CodedBitstreamContext *ctx,
297 CodedBitstreamUnit *unit)
298 {
299 VP8RawFrame *frame;
300 GetBitContext gbc;
301 CBSVP8BoolDecoder bool_decoder;
302 int err, pos;
303
304 err = ff_cbs_alloc_unit_content(ctx, unit);
305 if (err < 0)
306 return err;
307 frame = unit->content;
308
309 // Create GetBitContext for uncompressed header.
310 err = init_get_bits8_le(&gbc, unit->data, 8 * unit->data_size);
311 if (err < 0)
312 return err;
313
314 err = cbs_vp8_read_uncompressed_header(ctx, &gbc, frame);
315 if (err < 0)
316 return err;
317
318 pos = get_bits_count(&gbc);
319 av_assert0(pos % 8 == 0);
320
321 // Create boolean decoder for compressed header.
322 err = cbs_vp8_bool_decoder_init(&bool_decoder, &gbc);
323 if (err < 0)
324 return err;
325
326 err = cbs_vp8_read_compressed_header(ctx, &bool_decoder, frame);
327 if (err < 0)
328 return err;
329
330 pos = get_bits_count(&gbc);
331 pos /= 8;
332 av_assert0(pos <= unit->data_size);
333
334 frame->data_ref = av_buffer_ref(unit->data_ref);
335 if (!frame->data_ref)
336 return AVERROR(ENOMEM);
337
338 frame->data = unit->data + pos;
339 frame->data_size = unit->data_size - pos;
340
341 return 0;
342 }
343
344 static int cbs_vp8_write_unit(CodedBitstreamContext *ctx,
345 CodedBitstreamUnit *unit, PutBitContext *pbc)
346 {
347 return AVERROR_PATCHWELCOME;
348 }
349
350 static int cbs_vp8_assemble_fragment(CodedBitstreamContext *ctx,
351 CodedBitstreamFragment *frag)
352 {
353 return AVERROR_PATCHWELCOME;
354 }
355
356 static void cbs_vp8_flush(CodedBitstreamContext *ctx)
357 {
358 // Do nothing.
359 }
360
361 static const CodedBitstreamUnitTypeDescriptor cbs_vp8_unit_types[] = {
362 CBS_UNIT_TYPE_INTERNAL_REF(0, VP8RawFrame, data),
363 CBS_UNIT_TYPE_END_OF_LIST,
364 };
365
366 const CodedBitstreamType ff_cbs_type_vp8 = {
367 .codec_id = AV_CODEC_ID_VP8,
368
369 .priv_data_size = 0,
370
371 .unit_types = cbs_vp8_unit_types,
372
373 .split_fragment = &cbs_vp8_split_fragment,
374 .read_unit = &cbs_vp8_read_unit,
375 .write_unit = &cbs_vp8_write_unit,
376
377 .flush = &cbs_vp8_flush,
378
379 .assemble_fragment = &cbs_vp8_assemble_fragment,
380 };
381