Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2016 Alexandra Hájková | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #ifdef BITSTREAM_TEMPLATE_LE | ||
22 | # define BS_SUFFIX_LOWER _le | ||
23 | # define BS_SUFFIX_UPPER LE | ||
24 | #else | ||
25 | # define BS_SUFFIX_LOWER _be | ||
26 | # define BS_SUFFIX_UPPER BE | ||
27 | #endif | ||
28 | |||
29 | #define BS_JOIN(x, y, z) x ## y ## z | ||
30 | #define BS_JOIN3(x, y, z) BS_JOIN(x, y, z) | ||
31 | #define BS_FUNC(x) BS_JOIN3(bits_, x, BS_SUFFIX_LOWER) | ||
32 | |||
33 | #define BSCTX BS_JOIN3(Bitstream, Context, BS_SUFFIX_UPPER) | ||
34 | |||
35 | typedef struct BSCTX { | ||
36 | uint64_t bits; // stores bits read from the buffer | ||
37 | const uint8_t *buffer, *buffer_end; | ||
38 | const uint8_t *ptr; // pointer to the position inside a buffer | ||
39 | unsigned bits_valid; // number of bits left in bits field | ||
40 | unsigned size_in_bits; | ||
41 | } BSCTX; | ||
42 | |||
43 | /** | ||
44 | * @return | ||
45 | * - 0 on successful refill | ||
46 | * - a negative number when bitstream end is hit | ||
47 | * | ||
48 | * Always succeeds when UNCHECKED_BITSTREAM_READER is enabled. | ||
49 | */ | ||
50 | 22024 | static inline int BS_FUNC(priv_refill_64)(BSCTX *bc) | |
51 | { | ||
52 | #if !UNCHECKED_BITSTREAM_READER | ||
53 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20169 times.
|
20175 | if (bc->ptr >= bc->buffer_end) |
54 | 6 | return -1; | |
55 | #endif | ||
56 | |||
57 | #ifdef BITSTREAM_TEMPLATE_LE | ||
58 | 19779 | bc->bits = AV_RL64(bc->ptr); | |
59 | #else | ||
60 | 2239 | bc->bits = AV_RB64(bc->ptr); | |
61 | #endif | ||
62 | 22018 | bc->ptr += 8; | |
63 | 22018 | bc->bits_valid = 64; | |
64 | |||
65 | 22018 | return 0; | |
66 | } | ||
67 | |||
68 | /** | ||
69 | * @return | ||
70 | * - 0 on successful refill | ||
71 | * - a negative number when bitstream end is hit | ||
72 | * | ||
73 | * Always succeeds when UNCHECKED_BITSTREAM_READER is enabled. | ||
74 | */ | ||
75 | 5550929 | static inline int BS_FUNC(priv_refill_32)(BSCTX *bc) | |
76 | { | ||
77 | #if !UNCHECKED_BITSTREAM_READER | ||
78 |
2/2✓ Branch 0 taken 248 times.
✓ Branch 1 taken 409767 times.
|
410015 | if (bc->ptr >= bc->buffer_end) |
79 | 248 | return -1; | |
80 | #endif | ||
81 | |||
82 | #ifdef BITSTREAM_TEMPLATE_LE | ||
83 | 217951 | bc->bits |= (uint64_t)AV_RL32(bc->ptr) << bc->bits_valid; | |
84 | #else | ||
85 | 5332730 | bc->bits |= (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_valid); | |
86 | #endif | ||
87 | 5550681 | bc->ptr += 4; | |
88 | 5550681 | bc->bits_valid += 32; | |
89 | |||
90 | 5550681 | return 0; | |
91 | } | ||
92 | |||
93 | /** | ||
94 | * Initialize BitstreamContext. | ||
95 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
96 | * larger than the actual read bits because some optimized bitstream | ||
97 | * readers read 32 or 64 bits at once and could read over the end | ||
98 | * @param bit_size the size of the buffer in bits | ||
99 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
100 | */ | ||
101 | 2355 | static inline int BS_FUNC(init)(BSCTX *bc, const uint8_t *buffer, | |
102 | unsigned int bit_size) | ||
103 | { | ||
104 | unsigned int buffer_size; | ||
105 | |||
106 |
2/4✓ Branch 0 taken 1639 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1639 times.
|
2355 | if (bit_size > INT_MAX - 7 || !buffer) { |
107 | ✗ | bc->buffer = NULL; | |
108 | ✗ | bc->ptr = NULL; | |
109 | ✗ | bc->bits_valid = 0; | |
110 | ✗ | return AVERROR_INVALIDDATA; | |
111 | } | ||
112 | |||
113 | 2355 | buffer_size = (bit_size + 7) >> 3; | |
114 | |||
115 | 2355 | bc->buffer = buffer; | |
116 | 2355 | bc->buffer_end = buffer + buffer_size; | |
117 | 2355 | bc->ptr = bc->buffer; | |
118 | 2355 | bc->size_in_bits = bit_size; | |
119 | 2355 | bc->bits_valid = 0; | |
120 | 2355 | bc->bits = 0; | |
121 | |||
122 | 2355 | BS_FUNC(priv_refill_64)(bc); | |
123 | |||
124 | 2355 | return 0; | |
125 | } | ||
126 | |||
127 | /** | ||
128 | * Initialize BitstreamContext. | ||
129 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
130 | * larger than the actual read bits because some optimized bitstream | ||
131 | * readers read 32 or 64 bits at once and could read over the end | ||
132 | * @param byte_size the size of the buffer in bytes | ||
133 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow | ||
134 | */ | ||
135 | 923 | static inline int BS_FUNC(init8)(BSCTX *bc, const uint8_t *buffer, | |
136 | unsigned int byte_size) | ||
137 | { | ||
138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 923 times.
|
923 | if (byte_size > INT_MAX / 8) |
139 | ✗ | return AVERROR_INVALIDDATA; | |
140 | 923 | return BS_FUNC(init)(bc, buffer, byte_size * 8); | |
141 | } | ||
142 | |||
143 | /** | ||
144 | * Return number of bits already read. | ||
145 | */ | ||
146 | 498 | static inline int BS_FUNC(tell)(const BSCTX *bc) | |
147 | { | ||
148 | 498 | return (bc->ptr - bc->buffer) * 8 - bc->bits_valid; | |
149 | } | ||
150 | |||
151 | /** | ||
152 | * Return buffer size in bits. | ||
153 | */ | ||
154 | static inline int BS_FUNC(size)(const BSCTX *bc) | ||
155 | { | ||
156 | return bc->size_in_bits; | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * Return the number of the bits left in a buffer. | ||
161 | */ | ||
162 | 37883202 | static inline int BS_FUNC(left)(const BSCTX *bc) | |
163 | { | ||
164 | 37883202 | return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_valid; | |
165 | } | ||
166 | |||
167 | 47460138 | static inline uint64_t BS_FUNC(priv_val_show)(BSCTX *bc, unsigned int n) | |
168 | { | ||
169 |
2/4✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 290 times.
|
290 | av_assert2(n > 0 && n <= 64); |
170 | |||
171 | #ifdef BITSTREAM_TEMPLATE_LE | ||
172 | 1561133 | return bc->bits & (UINT64_MAX >> (64 - n)); | |
173 | #else | ||
174 | 45899005 | return bc->bits >> (64 - n); | |
175 | #endif | ||
176 | } | ||
177 | |||
178 | 47460173 | static inline void BS_FUNC(priv_skip_remaining)(BSCTX *bc, unsigned int n) | |
179 | { | ||
180 | #ifdef BITSTREAM_TEMPLATE_LE | ||
181 | 1561214 | bc->bits >>= n; | |
182 | #else | ||
183 | 45898959 | bc->bits <<= n; | |
184 | #endif | ||
185 | 47460173 | bc->bits_valid -= n; | |
186 | 47460173 | } | |
187 | |||
188 | 1561170 | static inline uint64_t BS_FUNC(priv_val_get)(BSCTX *bc, unsigned int n) | |
189 | { | ||
190 | uint64_t ret; | ||
191 | |||
192 |
2/4✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 182 times.
|
182 | av_assert2(n > 0 && n < 64); |
193 | |||
194 | 1561170 | ret = BS_FUNC(priv_val_show)(bc, n); | |
195 | 1561170 | BS_FUNC(priv_skip_remaining)(bc, n); | |
196 | |||
197 | 1561170 | return ret; | |
198 | } | ||
199 | |||
200 | /** | ||
201 | * Return one bit from the buffer. | ||
202 | */ | ||
203 | 686451 | static inline unsigned int BS_FUNC(read_bit)(BSCTX *bc) | |
204 | { | ||
205 |
3/4✓ Branch 0 taken 19463 times.
✓ Branch 1 taken 666988 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19463 times.
|
686451 | if (!bc->bits_valid && BS_FUNC(priv_refill_64)(bc) < 0) |
206 | ✗ | return 0; | |
207 | |||
208 | 686451 | return BS_FUNC(priv_val_get)(bc, 1); | |
209 | } | ||
210 | |||
211 | /** | ||
212 | * Return n bits from the buffer, n has to be in the 1-32 range. | ||
213 | * May be faster than bits_read() when n is not a compile-time constant and is | ||
214 | * known to be non-zero; | ||
215 | */ | ||
216 | 874623 | static inline uint32_t BS_FUNC(read_nz)(BSCTX *bc, unsigned int n) | |
217 | { | ||
218 |
2/4✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 109 times.
|
109 | av_assert2(n > 0 && n <= 32); |
219 | |||
220 |
2/2✓ Branch 0 taken 217940 times.
✓ Branch 1 taken 656683 times.
|
874623 | if (n > bc->bits_valid) { |
221 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 217940 times.
|
217940 | if (BS_FUNC(priv_refill_32)(bc) < 0) |
222 | ✗ | bc->bits_valid = n; | |
223 | } | ||
224 | |||
225 | 874623 | return BS_FUNC(priv_val_get)(bc, n); | |
226 | } | ||
227 | |||
228 | /** | ||
229 | * Return n bits from the buffer, n has to be in the 0-32 range. | ||
230 | */ | ||
231 | 860012 | static inline uint32_t BS_FUNC(read)(BSCTX *bc, unsigned int n) | |
232 | { | ||
233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | av_assert2(n <= 32); |
234 | |||
235 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 860010 times.
|
860012 | if (!n) |
236 | 2 | return 0; | |
237 | |||
238 | 860010 | return BS_FUNC(read_nz)(bc, n); | |
239 | } | ||
240 | |||
241 | /** | ||
242 | * Return n bits from the buffer, n has to be in the 0-63 range. | ||
243 | */ | ||
244 | 67 | static inline uint64_t BS_FUNC(read_63)(BSCTX *bc, unsigned int n) | |
245 | { | ||
246 | 67 | uint64_t ret = 0; | |
247 | 67 | unsigned left = 0; | |
248 | |||
249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | av_assert2(n <= 63); |
250 | |||
251 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 61 times.
|
67 | if (!n) |
252 | 6 | return 0; | |
253 | |||
254 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 26 times.
|
61 | if (n > bc->bits_valid) { |
255 | 35 | left = bc->bits_valid; | |
256 | 35 | n -= left; | |
257 | |||
258 |
1/2✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
|
35 | if (left) |
259 | 35 | ret = BS_FUNC(priv_val_get)(bc, left); | |
260 | |||
261 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
|
35 | if (BS_FUNC(priv_refill_64)(bc) < 0) |
262 | ✗ | bc->bits_valid = n; | |
263 | |||
264 | } | ||
265 | |||
266 | #ifdef BITSTREAM_TEMPLATE_LE | ||
267 | 42 | ret = BS_FUNC(priv_val_get)(bc, n) << left | ret; | |
268 | #else | ||
269 | 19 | ret = BS_FUNC(priv_val_get)(bc, n) | ret << n; | |
270 | #endif | ||
271 | |||
272 | 61 | return ret; | |
273 | } | ||
274 | |||
275 | /** | ||
276 | * Return n bits from the buffer, n has to be in the 0-64 range. | ||
277 | */ | ||
278 | 47 | static inline uint64_t BS_FUNC(read_64)(BSCTX *bc, unsigned int n) | |
279 | { | ||
280 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | av_assert2(n <= 64); |
281 | |||
282 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
|
47 | if (n == 64) { |
283 | 1 | uint64_t ret = BS_FUNC(read_63)(bc, 63); | |
284 | #ifdef BITSTREAM_TEMPLATE_LE | ||
285 | 1 | return ret | ((uint64_t)BS_FUNC(read_bit)(bc) << 63); | |
286 | #else | ||
287 | ✗ | return (ret << 1) | (uint64_t)BS_FUNC(read_bit)(bc); | |
288 | #endif | ||
289 | } | ||
290 | 46 | return BS_FUNC(read_63)(bc, n); | |
291 | } | ||
292 | |||
293 | /** | ||
294 | * Return n bits from the buffer as a signed integer, n has to be in the 1-32 | ||
295 | * range. May be faster than bits_read_signed() when n is not a compile-time | ||
296 | * constant and is known to be non-zero; | ||
297 | */ | ||
298 | 10421 | static inline int32_t BS_FUNC(read_signed_nz)(BSCTX *bc, unsigned int n) | |
299 | { | ||
300 |
2/4✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
|
61 | av_assert2(n > 0 && n <= 32); |
301 | 10421 | return sign_extend(BS_FUNC(read_nz)(bc, n), n); | |
302 | } | ||
303 | |||
304 | /** | ||
305 | * Return n bits from the buffer as a signed integer. | ||
306 | * n has to be in the 0-32 range. | ||
307 | */ | ||
308 | 38 | static inline int32_t BS_FUNC(read_signed)(BSCTX *bc, unsigned int n) | |
309 | { | ||
310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | av_assert2(n <= 32); |
311 | |||
312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (!n) |
313 | ✗ | return 0; | |
314 | |||
315 | 38 | return BS_FUNC(read_signed_nz)(bc, n); | |
316 | } | ||
317 | |||
318 | /** | ||
319 | * Return n bits from the buffer but do not change the buffer state. | ||
320 | * n has to be in the 1-32 range. May | ||
321 | */ | ||
322 | 38164148 | static inline uint32_t BS_FUNC(peek_nz)(BSCTX *bc, unsigned int n) | |
323 | { | ||
324 |
2/4✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
|
108 | av_assert2(n > 0 && n <= 32); |
325 | |||
326 |
2/2✓ Branch 0 taken 3984163 times.
✓ Branch 1 taken 34179985 times.
|
38164148 | if (n > bc->bits_valid) |
327 | 3984163 | BS_FUNC(priv_refill_32)(bc); | |
328 | |||
329 | 38164148 | return BS_FUNC(priv_val_show)(bc, n); | |
330 | } | ||
331 | |||
332 | /** | ||
333 | * Return n bits from the buffer but do not change the buffer state. | ||
334 | * n has to be in the 0-32 range. | ||
335 | */ | ||
336 | 38164092 | static inline uint32_t BS_FUNC(peek)(BSCTX *bc, unsigned int n) | |
337 | { | ||
338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | av_assert2(n <= 32); |
339 | |||
340 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 38164086 times.
|
38164092 | if (!n) |
341 | 6 | return 0; | |
342 | |||
343 | 38164086 | return BS_FUNC(peek_nz)(bc, n); | |
344 | } | ||
345 | |||
346 | /** | ||
347 | * Return n bits from the buffer as a signed integer, do not change the buffer | ||
348 | * state. n has to be in the 1-32 range. May be faster than bits_peek_signed() | ||
349 | * when n is not a compile-time constant and is known to be non-zero; | ||
350 | */ | ||
351 | 43 | static inline int BS_FUNC(peek_signed_nz)(BSCTX *bc, unsigned int n) | |
352 | { | ||
353 |
2/4✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
|
43 | av_assert2(n > 0 && n <= 32); |
354 | 43 | return sign_extend(BS_FUNC(peek_nz)(bc, n), n); | |
355 | } | ||
356 | |||
357 | /** | ||
358 | * Return n bits from the buffer as a signed integer, | ||
359 | * do not change the buffer state. | ||
360 | * n has to be in the 0-32 range. | ||
361 | */ | ||
362 | 20 | static inline int BS_FUNC(peek_signed)(BSCTX *bc, unsigned int n) | |
363 | { | ||
364 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | av_assert2(n <= 32); |
365 | |||
366 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!n) |
367 | ✗ | return 0; | |
368 | |||
369 | 20 | return BS_FUNC(peek_signed_nz)(bc, n); | |
370 | } | ||
371 | |||
372 | /** | ||
373 | * Skip n bits in the buffer. | ||
374 | */ | ||
375 | 273 | static inline void BS_FUNC(skip)(BSCTX *bc, unsigned int n) | |
376 | { | ||
377 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 171 times.
|
273 | if (n < bc->bits_valid) |
378 | 102 | BS_FUNC(priv_skip_remaining)(bc, n); | |
379 | else { | ||
380 | 171 | n -= bc->bits_valid; | |
381 | 171 | bc->bits = 0; | |
382 | 171 | bc->bits_valid = 0; | |
383 | |||
384 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
|
171 | if (n >= 64) { |
385 | ✗ | unsigned int skip = n / 8; | |
386 | |||
387 | ✗ | n -= skip * 8; | |
388 | ✗ | bc->ptr += skip; | |
389 | } | ||
390 | 171 | BS_FUNC(priv_refill_64)(bc); | |
391 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 130 times.
|
171 | if (n) |
392 | 41 | BS_FUNC(priv_skip_remaining)(bc, n); | |
393 | } | ||
394 | 273 | } | |
395 | |||
396 | /** | ||
397 | * Seek to the given bit position. | ||
398 | */ | ||
399 | static inline void BS_FUNC(seek)(BSCTX *bc, unsigned pos) | ||
400 | { | ||
401 | bc->ptr = bc->buffer; | ||
402 | bc->bits = 0; | ||
403 | bc->bits_valid = 0; | ||
404 | |||
405 | BS_FUNC(skip)(bc, pos); | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * Skip bits to a byte boundary. | ||
410 | */ | ||
411 | 81 | static inline const uint8_t *BS_FUNC(align)(BSCTX *bc) | |
412 | { | ||
413 | 81 | unsigned int n = -BS_FUNC(tell)(bc) & 7; | |
414 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 8 times.
|
81 | if (n) |
415 | 73 | BS_FUNC(skip)(bc, n); | |
416 | 81 | return bc->buffer + (BS_FUNC(tell)(bc) >> 3); | |
417 | } | ||
418 | |||
419 | /** | ||
420 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). | ||
421 | * If MSB not set it is negative. | ||
422 | * @param n length in bits | ||
423 | */ | ||
424 | static inline int BS_FUNC(read_xbits)(BSCTX *bc, unsigned int n) | ||
425 | { | ||
426 | int32_t cache = BS_FUNC(peek)(bc, 32); | ||
427 | int sign = ~cache >> 31; | ||
428 | BS_FUNC(priv_skip_remaining)(bc, n); | ||
429 | |||
430 | return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign; | ||
431 | } | ||
432 | |||
433 | /** | ||
434 | * Return decoded truncated unary code for the values 0, 1, 2. | ||
435 | */ | ||
436 | static inline int BS_FUNC(decode012)(BSCTX *bc) | ||
437 | { | ||
438 | if (!BS_FUNC(read_bit)(bc)) | ||
439 | return 0; | ||
440 | else | ||
441 | return BS_FUNC(read_bit)(bc) + 1; | ||
442 | } | ||
443 | |||
444 | /** | ||
445 | * Return decoded truncated unary code for the values 2, 1, 0. | ||
446 | */ | ||
447 | static inline int BS_FUNC(decode210)(BSCTX *bc) | ||
448 | { | ||
449 | if (BS_FUNC(read_bit)(bc)) | ||
450 | return 0; | ||
451 | else | ||
452 | return 2 - BS_FUNC(read_bit)(bc); | ||
453 | } | ||
454 | |||
455 | /* Read sign bit and flip the sign of the provided value accordingly. */ | ||
456 | 18 | static inline int BS_FUNC(apply_sign)(BSCTX *bc, int val) | |
457 | { | ||
458 | 18 | int sign = BS_FUNC(read_signed)(bc, 1); | |
459 | 18 | return (val ^ sign) - sign; | |
460 | } | ||
461 | |||
462 | static inline int BS_FUNC(skip_1stop_8data)(BSCTX *s) | ||
463 | { | ||
464 | if (BS_FUNC(left)(s) <= 0) | ||
465 | return AVERROR_INVALIDDATA; | ||
466 | |||
467 | while (BS_FUNC(read_bit)(s)) { | ||
468 | BS_FUNC(skip)(s, 8); | ||
469 | if (BS_FUNC(left)(s) <= 0) | ||
470 | return AVERROR_INVALIDDATA; | ||
471 | } | ||
472 | |||
473 | return 0; | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * Return the LUT element for the given bitstream configuration. | ||
478 | */ | ||
479 | 282167 | static inline int BS_FUNC(priv_set_idx)(BSCTX *bc, int code, int *n, | |
480 | int *nb_bits, const VLCElem *table) | ||
481 | { | ||
482 | unsigned idx; | ||
483 | |||
484 | 282167 | *nb_bits = -*n; | |
485 | 282167 | idx = BS_FUNC(peek)(bc, *nb_bits) + code; | |
486 | 282167 | *n = table[idx].len; | |
487 | |||
488 | 282167 | return table[idx].sym; | |
489 | } | ||
490 | |||
491 | /** | ||
492 | * Parse a vlc code. | ||
493 | * @param bits is the number of bits which will be read at once, must be | ||
494 | * identical to nb_bits in vlc_init() | ||
495 | * @param max_depth is the number of times bits bits must be read to completely | ||
496 | * read the longest vlc code | ||
497 | * = (max_vlc_length + bits - 1) / bits | ||
498 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
499 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
500 | * is undefined. | ||
501 | */ | ||
502 | 29760256 | static inline int BS_FUNC(read_vlc)(BSCTX *bc, const VLCElem *table, | |
503 | int bits, int max_depth) | ||
504 | { | ||
505 | int nb_bits; | ||
506 | 29760256 | unsigned idx = BS_FUNC(peek)(bc, bits); | |
507 | 29760256 | int code = table[idx].sym; | |
508 | 29760256 | int n = table[idx].len; | |
509 | |||
510 |
3/4✓ Branch 0 taken 29760256 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 201729 times.
✓ Branch 3 taken 29558527 times.
|
29760256 | if (max_depth > 1 && n < 0) { |
511 | 201729 | BS_FUNC(priv_skip_remaining)(bc, bits); | |
512 | 201729 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
513 |
3/4✓ Branch 0 taken 201729 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 201672 times.
|
201729 | if (max_depth > 2 && n < 0) { |
514 | 57 | BS_FUNC(priv_skip_remaining)(bc, nb_bits); | |
515 | 57 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
516 | } | ||
517 | } | ||
518 | 29760256 | BS_FUNC(priv_skip_remaining)(bc, n); | |
519 | |||
520 | 29760256 | return code; | |
521 | } | ||
522 | |||
523 | /** | ||
524 | * Parse a vlc / vlc_multi code. | ||
525 | * @param bits is the number of bits which will be read at once, must be | ||
526 | * identical to nb_bits in vlc_init() | ||
527 | * @param max_depth is the number of times bits bits must be read to completely | ||
528 | * read the longest vlc code | ||
529 | * = (max_vlc_length + bits - 1) / bits | ||
530 | * @param dst the parsed symbol(s) will be stored here. Up to 8 bytes are written | ||
531 | * @returns number of symbols parsed | ||
532 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
533 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
534 | * is undefined. | ||
535 | */ | ||
536 | 8121617 | static inline int BS_FUNC(read_vlc_multi)(BSCTX *bc, uint8_t dst[8], | |
537 | const VLC_MULTI_ELEM *const Jtable, | ||
538 | const VLCElem *const table, | ||
539 | const int bits, const int max_depth, | ||
540 | const int symbols_size) | ||
541 | { | ||
542 | 8121617 | unsigned idx = BS_FUNC(peek)(bc, bits); | |
543 | 8121617 | int ret, nb_bits, code, n = Jtable[idx].len; | |
544 |
2/2✓ Branch 0 taken 8041236 times.
✓ Branch 1 taken 80381 times.
|
8121617 | if (Jtable[idx].num) { |
545 | 8041236 | AV_COPY64U(dst, Jtable[idx].val8); | |
546 | 8041236 | ret = Jtable[idx].num; | |
547 | } else { | ||
548 | 80381 | code = table[idx].sym; | |
549 | 80381 | n = table[idx].len; | |
550 |
2/4✓ Branch 0 taken 80381 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80381 times.
✗ Branch 3 not taken.
|
80381 | if (max_depth > 1 && n < 0) { |
551 | 80381 | BS_FUNC(priv_skip_remaining)(bc, bits); | |
552 | 80381 | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
553 |
2/4✓ Branch 0 taken 80381 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80381 times.
|
80381 | if (max_depth > 2 && n < 0) { |
554 | ✗ | BS_FUNC(priv_skip_remaining)(bc, nb_bits); | |
555 | ✗ | code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); | |
556 | } | ||
557 | } | ||
558 |
1/2✓ Branch 0 taken 80381 times.
✗ Branch 1 not taken.
|
80381 | if (symbols_size == 1) |
559 | 80381 | *dst = code; | |
560 | else | ||
561 | ✗ | AV_WN16(dst, code); | |
562 | 80381 | ret = n > 0; | |
563 | } | ||
564 | 8121617 | BS_FUNC(priv_skip_remaining)(bc, n); | |
565 | |||
566 | 8121617 | return ret; | |
567 | } | ||
568 | |||
569 | #undef BSCTX | ||
570 | #undef BS_FUNC | ||
571 | #undef BS_JOIN3 | ||
572 | #undef BS_JOIN | ||
573 | #undef BS_SUFFIX_UPPER | ||
574 | #undef BS_SUFFIX_LOWER | ||
575 |