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
20
24
25 #include <stdbool.h>
26
27 #define DEFAULT_PROB 0x80
28
29 // The probability table is defined in 'vp8data.c'.
31
32 // Implements VP8 boolean decoder using GetBitContext to read the bitstream.
35
38
39 uint8_t
count;
// Store the number of bits in the `value` buffer.
40
42
44 {
47
51
53
54 return 0;
55 }
56
58 {
60
63 return true;
64 }
65
69 }
70
72 }
73
76 {
78
81 }
82
88 } else {
91 }
92
97 }
98
99 return 0;
100 }
101
104 uint32_t num_bits,
106 {
108
110
112 for (; num_bits > 0; --num_bits) {
113 uint8_t bit_output = 0;
115 &bit_output)) != 0) {
117 }
118
120 }
121
122 return 0;
123 }
124
127 uint8_t
prob,
const char *
name,
const int *subscripts, uint32_t *write_to,
128 bool trace_enable)
129 {
133
135
137
141 }
142
143 if (trace_enable) {
145 }
146
148 return 0;
149 }
150
153 uint8_t
prob,
const char *
name,
const int *subscripts,
int32_t *write_to)
154 {
158 uint8_t sign = 0;
159
161
163
167 }
168
172 }
173
174 if (sign) {
176 }
177
179
181 return 0;
182 }
183
186 const char *
name,
const int *subscripts,
187 uint32_t *write_to, uint32_t range_min,
188 uint32_t range_max)
189 {
191
193
195
199 }
200
202
204
205 if (value < range_min || value > range_max) {
207 "%s out of range: "
208 "%" PRIu32 ", but must be in [%" PRIu32 ",%" PRIu32 "].\n",
211 }
212
214 return 0;
215 }
216
217 #define HEADER(name) \
218 do { \
219 ff_cbs_trace_header(ctx, name); \
220 } while (0)
221
222 #define CHECK(call) \
223 do { \
224 int err = (call); \
225 if (err < 0) \
226 return err; \
227 } 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 #define f(width, name) xf(width, name, 0, )
237
238 // bool [de|en]coder methods.
239 #define bc_f(width, name) bc_unsigned_subs(width, DEFAULT_PROB, true, name, 0, )
240 #define bc_s(width, name) bc_signed_subs(width, DEFAULT_PROB, name, 0, )
241 #define bc_fs(width, name, subs, ...) \
242 bc_unsigned_subs(width, DEFAULT_PROB, true, name, subs, __VA_ARGS__)
243 #define bc_ss(width, name, subs, ...) \
244 bc_signed_subs(width, DEFAULT_PROB, name, subs, __VA_ARGS__)
245
246 // bool [de|en]coder methods for boolean value and disable tracing.
247 #define bc_b(name) bc_unsigned_subs(1, DEFAULT_PROB, false, name, 0, )
248 #define bc_b_prob(prob, name) bc_unsigned_subs(1, prob, false, name, 0, )
249
251 #define READWRITE read
252 #define RWContext GetBitContext
253 #define CBSVP8BoolCodingRW CBSVP8BoolDecoder
254
255 #define xf(width, name, subs, ...) \
256 do { \
257 uint32_t value; \
258 CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, \
259 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
260 0, MAX_UINT_BITS(width))); \
261 current->name = value; \
262 } while (0)
263
264 #define fixed(width, name, value) \
265 do { \
266 uint32_t fixed_value; \
267 CHECK(cbs_vp8_read_unsigned_le(ctx, rw, width, #name, 0, &fixed_value, \
268 value, value)); \
269 } while (0)
270
271 #define bc_unsigned_subs(width, prob, enable_trace, name, subs, ...) \
272 do { \
273 uint32_t value; \
274 CHECK(cbs_vp8_bool_decoder_read_unsigned( \
275 ctx, bool_coding_rw, width, prob, #name, \
276 SUBSCRIPTS(subs, __VA_ARGS__), &value, enable_trace)); \
277 current->name = value; \
278 } while (0)
279
280 #define bc_signed_subs(width, prob, name, subs, ...) \
281 do { \
282 int32_t value; \
283 CHECK(cbs_vp8_bool_decoder_read_signed( \
284 ctx, bool_coding_rw, width, prob, #name, \
285 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
286 current->name = value; \
287 } while (0)
288
290
293 {
294 int err;
295
298
301 if (err < 0)
302 return err;
303
304 return 0;
305 }
306
309 {
314
316 if (err < 0)
317 return err;
319
320 // Create GetBitContext for uncompressed header.
322 if (err < 0)
323 return err;
324
325 err = cbs_vp8_read_uncompressed_header(
ctx, &gbc,
frame);
326 if (err < 0)
327 return err;
328
331
332 // Create boolean decoder for compressed header.
334 if (err < 0)
335 return err;
336
337 err = cbs_vp8_read_compressed_header(
ctx, &bool_decoder,
frame);
338 if (err < 0)
339 return err;
340
342 // Position may not be byte-aligned after compressed header; Round up byte
343 // count for accurate data positioning.
346
348 if (!
frame->data_ref)
350
353
354 return 0;
355 }
356
359 {
361 }
362
365 {
367 }
368
372 };
373
376
377 .priv_data_size = 0,
378
380
384
386 };