FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpack.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 582 1070 54.4%
Branches: 373 708 52.7%

Line Branch Exec Source
1 /*
2 * WavPack lossless audio decoder
3 * Copyright (c) 2006,2011 Konstantin Shishkov
4 * Copyright (c) 2020 David Bryant
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 #include "libavutil/buffer.h"
24 #include "libavutil/channel_layout.h"
25
26 #define BITSTREAM_READER_LE
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "get_bits.h"
31 #include "thread.h"
32 #include "threadframe.h"
33 #include "unary.h"
34 #include "wavpack.h"
35 #include "dsd.h"
36
37 /**
38 * @file
39 * WavPack lossless audio decoder
40 */
41
42 #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) & 0xff000000))
43
44 #define PTABLE_BITS 8
45 #define PTABLE_BINS (1<<PTABLE_BITS)
46 #define PTABLE_MASK (PTABLE_BINS-1)
47
48 #define UP 0x010000fe
49 #define DOWN 0x00010000
50 #define DECAY 8
51
52 #define PRECISION 20
53 #define VALUE_ONE (1 << PRECISION)
54 #define PRECISION_USE 12
55
56 #define RATE_S 20
57
58 #define MAX_HISTORY_BITS 5
59 #define MAX_HISTORY_BINS (1 << MAX_HISTORY_BITS)
60 #define MAX_BIN_BYTES 1280 // for value_lookup, per bin (2k - 512 - 256)
61
62 typedef enum {
63 MODULATION_PCM, // pulse code modulation
64 MODULATION_DSD // pulse density modulation (aka DSD)
65 } Modulation;
66
67 typedef struct WavpackFrameContext {
68 AVCodecContext *avctx;
69 int frame_flags;
70 int stereo, stereo_in;
71 int joint;
72 uint32_t CRC;
73 GetBitContext gb;
74 int got_extra_bits;
75 uint32_t crc_extra_bits;
76 GetBitContext gb_extra_bits;
77 int samples;
78 int terms;
79 Decorr decorr[MAX_TERMS];
80 int zero, one, zeroes;
81 int extra_bits;
82 int and, or, shift;
83 int post_shift;
84 int hybrid, hybrid_bitrate;
85 int hybrid_maxclip, hybrid_minclip;
86 int float_flag;
87 int float_shift;
88 int float_max_exp;
89 WvChannel ch[2];
90
91 GetByteContext gbyte;
92 int ptable [PTABLE_BINS];
93 uint8_t value_lookup_buffer[MAX_HISTORY_BINS*MAX_BIN_BYTES];
94 uint16_t summed_probabilities[MAX_HISTORY_BINS][256];
95 uint8_t probabilities[MAX_HISTORY_BINS][256];
96 uint8_t *value_lookup[MAX_HISTORY_BINS];
97 } WavpackFrameContext;
98
99 #define WV_MAX_FRAME_DECODERS 14
100
101 typedef struct WavpackContext {
102 AVCodecContext *avctx;
103
104 WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
105 int fdec_num;
106
107 int block;
108 int samples;
109 int ch_offset;
110
111 AVFrame *frame;
112 ThreadFrame curr_frame, prev_frame;
113 Modulation modulation;
114
115 AVBufferRef *dsd_ref;
116 DSDContext *dsdctx;
117 int dsd_channels;
118 } WavpackContext;
119
120 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
121
122 28282042 static av_always_inline unsigned get_tail(GetBitContext *gb, int k)
123 {
124 int p, e, res;
125
126
2/2
✓ Branch 0 taken 3083218 times.
✓ Branch 1 taken 25198824 times.
28282042 if (k < 1)
127 3083218 return 0;
128 25198824 p = av_log2(k);
129 25198824 e = (1 << (p + 1)) - k - 1;
130 25198824 res = get_bitsz(gb, p);
131
2/2
✓ Branch 0 taken 15873859 times.
✓ Branch 1 taken 9324965 times.
25198824 if (res >= e)
132 15873859 res = (res << 1) - e + get_bits1(gb);
133 25198824 return res;
134 }
135
136 34605992 static int update_error_limit(WavpackFrameContext *ctx)
137 {
138 int i, br[2], sl[2];
139
140
2/2
✓ Branch 0 taken 59875526 times.
✓ Branch 1 taken 34605992 times.
94481518 for (i = 0; i <= ctx->stereo_in; i++) {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59875526 times.
59875526 if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
142 return AVERROR_INVALIDDATA;
143 59875526 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
144 59875526 br[i] = ctx->ch[i].bitrate_acc >> 16;
145 59875526 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
146 }
147
3/4
✓ Branch 0 taken 25269534 times.
✓ Branch 1 taken 9336458 times.
✓ Branch 2 taken 25269534 times.
✗ Branch 3 not taken.
34605992 if (ctx->stereo_in && ctx->hybrid_bitrate) {
148 25269534 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
149
2/2
✓ Branch 0 taken 7996128 times.
✓ Branch 1 taken 17273406 times.
25269534 if (balance > br[0]) {
150 7996128 br[1] = br[0] * 2;
151 7996128 br[0] = 0;
152
2/2
✓ Branch 0 taken 2254389 times.
✓ Branch 1 taken 15019017 times.
17273406 } else if (-balance > br[0]) {
153 2254389 br[0] *= 2;
154 2254389 br[1] = 0;
155 } else {
156 15019017 br[1] = br[0] + balance;
157 15019017 br[0] = br[0] - balance;
158 }
159 }
160
2/2
✓ Branch 0 taken 59875526 times.
✓ Branch 1 taken 34605992 times.
94481518 for (i = 0; i <= ctx->stereo_in; i++) {
161
1/2
✓ Branch 0 taken 59875526 times.
✗ Branch 1 not taken.
59875526 if (ctx->hybrid_bitrate) {
162
2/2
✓ Branch 0 taken 59872999 times.
✓ Branch 1 taken 2527 times.
59875526 if (sl[i] - br[i] > -0x100)
163 59872999 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
164 else
165 2527 ctx->ch[i].error_limit = 0;
166 } else {
167 ctx->ch[i].error_limit = wp_exp2(br[i]);
168 }
169 }
170
171 34605992 return 0;
172 }
173
174 350742057 static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
175 int channel, int *last)
176 {
177 int t, t2;
178 int sign, base, add, ret;
179 350742057 WvChannel *c = &ctx->ch[channel];
180
181 350742057 *last = 0;
182
183
4/4
✓ Branch 0 taken 264715557 times.
✓ Branch 1 taken 86026500 times.
✓ Branch 2 taken 264014532 times.
✓ Branch 3 taken 701025 times.
350742057 if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
184
4/4
✓ Branch 0 taken 263853453 times.
✓ Branch 1 taken 161079 times.
✓ Branch 2 taken 263810815 times.
✓ Branch 3 taken 42638 times.
264014532 !ctx->zero && !ctx->one) {
185
2/2
✓ Branch 0 taken 263606534 times.
✓ Branch 1 taken 204281 times.
263810815 if (ctx->zeroes) {
186 263606534 ctx->zeroes--;
187
2/2
✓ Branch 0 taken 263501545 times.
✓ Branch 1 taken 104989 times.
263606534 if (ctx->zeroes) {
188 263501545 c->slow_level -= LEVEL_DECAY(c->slow_level);
189 263501545 return 0;
190 }
191 } else {
192 204281 t = get_unary_0_33(gb);
193
2/2
✓ Branch 0 taken 70185 times.
✓ Branch 1 taken 134096 times.
204281 if (t >= 2) {
194
2/4
✓ Branch 0 taken 70185 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 70185 times.
70185 if (t >= 32 || get_bits_left(gb) < t - 1)
195 goto error;
196 70185 t = get_bits_long(gb, t - 1) | (1 << (t - 1));
197 } else {
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 134096 times.
134096 if (get_bits_left(gb) < 0)
199 goto error;
200 }
201 204281 ctx->zeroes = t;
202
2/2
✓ Branch 0 taken 111012 times.
✓ Branch 1 taken 93269 times.
204281 if (ctx->zeroes) {
203 111012 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
204 111012 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
205 111012 c->slow_level -= LEVEL_DECAY(c->slow_level);
206 111012 return 0;
207 }
208 }
209 }
210
211
2/2
✓ Branch 0 taken 36018736 times.
✓ Branch 1 taken 51110764 times.
87129500 if (ctx->zero) {
212 36018736 t = 0;
213 36018736 ctx->zero = 0;
214 } else {
215 51110764 t = get_unary_0_33(gb);
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51110764 times.
51110764 if (get_bits_left(gb) < 0)
217 goto error;
218
2/2
✓ Branch 0 taken 20139 times.
✓ Branch 1 taken 51090625 times.
51110764 if (t == 16) {
219 20139 t2 = get_unary_0_33(gb);
220
2/2
✓ Branch 0 taken 4854 times.
✓ Branch 1 taken 15285 times.
20139 if (t2 < 2) {
221
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4854 times.
4854 if (get_bits_left(gb) < 0)
222 goto error;
223 4854 t += t2;
224 } else {
225
2/4
✓ Branch 0 taken 15285 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 15285 times.
15285 if (t2 >= 32 || get_bits_left(gb) < t2 - 1)
226 goto error;
227 15285 t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
228 }
229 }
230
231
2/2
✓ Branch 0 taken 15090662 times.
✓ Branch 1 taken 36020102 times.
51110764 if (ctx->one) {
232 15090662 ctx->one = t & 1;
233 15090662 t = (t >> 1) + 1;
234 } else {
235 36020102 ctx->one = t & 1;
236 36020102 t >>= 1;
237 }
238 51110764 ctx->zero = !ctx->one;
239 }
240
241
4/4
✓ Branch 0 taken 59874831 times.
✓ Branch 1 taken 27254669 times.
✓ Branch 2 taken 34605992 times.
✓ Branch 3 taken 25268839 times.
87129500 if (ctx->hybrid && !channel) {
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34605992 times.
34605992 if (update_error_limit(ctx) < 0)
243 goto error;
244 }
245
246
2/2
✓ Branch 0 taken 62106315 times.
✓ Branch 1 taken 25023185 times.
87129500 if (!t) {
247 62106315 base = 0;
248 62106315 add = GET_MED(0) - 1;
249 62106315 DEC_MED(0);
250
2/2
✓ Branch 0 taken 17844678 times.
✓ Branch 1 taken 7178507 times.
25023185 } else if (t == 1) {
251 17844678 base = GET_MED(0);
252 17844678 add = GET_MED(1) - 1;
253 17844678 INC_MED(0);
254 17844678 DEC_MED(1);
255
2/2
✓ Branch 0 taken 5062511 times.
✓ Branch 1 taken 2115996 times.
7178507 } else if (t == 2) {
256 5062511 base = GET_MED(0) + GET_MED(1);
257 5062511 add = GET_MED(2) - 1;
258 5062511 INC_MED(0);
259 5062511 INC_MED(1);
260 5062511 DEC_MED(2);
261 } else {
262 2115996 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2U);
263 2115996 add = GET_MED(2) - 1;
264 2115996 INC_MED(0);
265 2115996 INC_MED(1);
266 2115996 INC_MED(2);
267 }
268
2/2
✓ Branch 0 taken 28282042 times.
✓ Branch 1 taken 58847458 times.
87129500 if (!c->error_limit) {
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28282042 times.
28282042 if (add >= 0x2000000U) {
270 av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
271 goto error;
272 }
273 28282042 ret = base + get_tail(gb, add);
274
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28282042 times.
28282042 if (get_bits_left(gb) <= 0)
275 goto error;
276 } else {
277 58847458 int mid = (base * 2U + add + 1) >> 1;
278
2/2
✓ Branch 0 taken 25419273 times.
✓ Branch 1 taken 58847458 times.
84266731 while (add > c->error_limit) {
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25419273 times.
25419273 if (get_bits_left(gb) <= 0)
280 goto error;
281
2/2
✓ Branch 1 taken 10905078 times.
✓ Branch 2 taken 14514195 times.
25419273 if (get_bits1(gb)) {
282 10905078 add -= (mid - (unsigned)base);
283 10905078 base = mid;
284 } else
285 14514195 add = mid - (unsigned)base - 1;
286 25419273 mid = (base * 2U + add + 1) >> 1;
287 }
288 58847458 ret = mid;
289 }
290 87129500 sign = get_bits1(gb);
291
2/2
✓ Branch 0 taken 59874831 times.
✓ Branch 1 taken 27254669 times.
87129500 if (ctx->hybrid_bitrate)
292 59874831 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
293
2/2
✓ Branch 0 taken 40808659 times.
✓ Branch 1 taken 46320841 times.
87129500 return sign ? ~ret : ret;
294
295 error:
296 ret = get_bits_left(gb);
297 if (ret <= 0) {
298 av_log(ctx->avctx, AV_LOG_ERROR, "Too few bits (%d) left\n", ret);
299 }
300 *last = 1;
301 return 0;
302 }
303
304 347567571 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
305 unsigned S)
306 {
307 unsigned bit;
308
309
2/2
✓ Branch 0 taken 352800 times.
✓ Branch 1 taken 347214771 times.
347567571 if (s->extra_bits) {
310 352800 S *= 1 << s->extra_bits;
311
312
1/2
✓ Branch 0 taken 352800 times.
✗ Branch 1 not taken.
352800 if (s->got_extra_bits &&
313
1/2
✓ Branch 1 taken 352800 times.
✗ Branch 2 not taken.
352800 get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
314 352800 S |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
315 352800 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
316 }
317 }
318
319 347567571 bit = (S & s->and) | s->or;
320 347567571 bit = ((S + bit) << s->shift) - bit;
321
322
2/2
✓ Branch 0 taken 320210656 times.
✓ Branch 1 taken 27356915 times.
347567571 if (s->hybrid)
323 320210656 bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
324
325 347567571 return bit << s->post_shift;
326 }
327
328 3174484 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
329 {
330 union {
331 float f;
332 uint32_t u;
333 } value;
334
335 unsigned int sign;
336 3174484 int exp = s->float_max_exp;
337
338
2/2
✓ Branch 0 taken 828900 times.
✓ Branch 1 taken 2345584 times.
3174484 if (s->got_extra_bits) {
339 828900 const int max_bits = 1 + 23 + 8 + 1;
340 828900 const int left_bits = get_bits_left(&s->gb_extra_bits);
341
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 828900 times.
828900 if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
343 return 0.0;
344 }
345
346
2/2
✓ Branch 0 taken 3174479 times.
✓ Branch 1 taken 5 times.
3174484 if (S) {
347 3174479 S *= 1U << s->float_shift;
348 3174479 sign = S < 0;
349
2/2
✓ Branch 0 taken 1588502 times.
✓ Branch 1 taken 1585977 times.
3174479 if (sign)
350 1588502 S = -(unsigned)S;
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3174479 times.
3174479 if (S >= 0x1000000U) {
352 if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
353 S = get_bits(&s->gb_extra_bits, 23);
354 else
355 S = 0;
356 exp = 255;
357
1/2
✓ Branch 0 taken 3174479 times.
✗ Branch 1 not taken.
3174479 } else if (exp) {
358 3174479 int shift = 23 - av_log2(S);
359 3174479 exp = s->float_max_exp;
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3174479 times.
3174479 if (exp <= shift)
361 shift = --exp;
362 3174479 exp -= shift;
363
364
2/2
✓ Branch 0 taken 3130357 times.
✓ Branch 1 taken 44122 times.
3174479 if (shift) {
365 3130357 S <<= shift;
366
1/2
✓ Branch 0 taken 3130357 times.
✗ Branch 1 not taken.
3130357 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
367
2/2
✓ Branch 0 taken 817921 times.
✓ Branch 1 taken 2312436 times.
3130357 (s->got_extra_bits &&
368
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 817921 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
817921 (s->float_flag & WV_FLT_SHIFT_SAME) &&
369 get_bits1(&s->gb_extra_bits))) {
370 S |= (1 << shift) - 1;
371
2/2
✓ Branch 0 taken 817921 times.
✓ Branch 1 taken 2312436 times.
3130357 } else if (s->got_extra_bits &&
372
1/2
✓ Branch 0 taken 817921 times.
✗ Branch 1 not taken.
817921 (s->float_flag & WV_FLT_SHIFT_SENT)) {
373 817921 S |= get_bits(&s->gb_extra_bits, shift);
374 }
375 }
376 } else {
377 exp = s->float_max_exp;
378 }
379 3174479 S &= 0x7fffff;
380 } else {
381 5 sign = 0;
382 5 exp = 0;
383
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
5 if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
384
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (get_bits1(&s->gb_extra_bits)) {
385 2 S = get_bits(&s->gb_extra_bits, 23);
386
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->float_max_exp >= 25)
387 2 exp = get_bits(&s->gb_extra_bits, 8);
388 2 sign = get_bits1(&s->gb_extra_bits);
389 } else {
390 if (s->float_flag & WV_FLT_ZERO_SIGN)
391 sign = get_bits1(&s->gb_extra_bits);
392 }
393 }
394 }
395
396 3174484 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
397
398 3174484 value.u = (sign << 31) | (exp << 23) | S;
399 3174484 return value.f;
400 }
401
402 static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
403 uint32_t crc_extra_bits)
404 {
405 if (crc != s->CRC) {
406 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
407 return AVERROR_INVALIDDATA;
408 }
409 if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
410 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
411 return AVERROR_INVALIDDATA;
412 }
413
414 return 0;
415 }
416
417 static void init_ptable(int *table, int rate_i, int rate_s)
418 {
419 int value = 0x808000, rate = rate_i << 8;
420
421 for (int c = (rate + 128) >> 8; c--;)
422 value += (DOWN - value) >> DECAY;
423
424 for (int i = 0; i < PTABLE_BINS/2; i++) {
425 table[i] = value;
426 table[PTABLE_BINS-1-i] = 0x100ffff - value;
427
428 if (value > 0x010000) {
429 rate += (rate * rate_s + 128) >> 8;
430
431 for (int c = (rate + 64) >> 7; c--;)
432 value += (DOWN - value) >> DECAY;
433 }
434 }
435 }
436
437 typedef struct {
438 int32_t value, fltr0, fltr1, fltr2, fltr3, fltr4, fltr5, fltr6, factor;
439 unsigned int byte;
440 } DSDfilters;
441
442 static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
443 {
444 uint32_t checksum = 0xFFFFFFFF;
445 uint8_t *dst_l = dst_left, *dst_r = dst_right;
446 int total_samples = s->samples, stereo = dst_r ? 1 : 0;
447 DSDfilters filters[2], *sp = filters;
448 int rate_i, rate_s;
449 uint32_t low, high, value;
450
451 if (bytestream2_get_bytes_left(&s->gbyte) < (stereo ? 20 : 13))
452 return AVERROR_INVALIDDATA;
453
454 rate_i = bytestream2_get_byte(&s->gbyte);
455 rate_s = bytestream2_get_byte(&s->gbyte);
456
457 if (rate_s != RATE_S)
458 return AVERROR_INVALIDDATA;
459
460 init_ptable(s->ptable, rate_i, rate_s);
461
462 for (int channel = 0; channel < stereo + 1; channel++) {
463 DSDfilters *sp = filters + channel;
464
465 sp->fltr1 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
466 sp->fltr2 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
467 sp->fltr3 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
468 sp->fltr4 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
469 sp->fltr5 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
470 sp->fltr6 = 0;
471 sp->factor = bytestream2_get_byte(&s->gbyte) & 0xff;
472 sp->factor |= (bytestream2_get_byte(&s->gbyte) << 8) & 0xff00;
473 sp->factor = (int32_t)((uint32_t)sp->factor << 16) >> 16;
474 }
475
476 value = bytestream2_get_be32(&s->gbyte);
477 high = 0xffffffff;
478 low = 0x0;
479
480 while (total_samples--) {
481 int bitcount = 8;
482
483 sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
484
485 if (stereo)
486 sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
487
488 while (bitcount--) {
489 int32_t *pp = s->ptable + ((sp[0].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
490 uint32_t split = low + ((high - low) >> 8) * (*pp >> 16);
491
492 if (value <= split) {
493 high = split;
494 *pp += (UP - *pp) >> DECAY;
495 sp[0].fltr0 = -1;
496 } else {
497 low = split + 1;
498 *pp += (DOWN - *pp) >> DECAY;
499 sp[0].fltr0 = 0;
500 }
501
502 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
503 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
504 high = (high << 8) | 0xff;
505 low <<= 8;
506 }
507
508 sp[0].value += sp[0].fltr6 * 8;
509 sp[0].byte = (sp[0].byte << 1) | (sp[0].fltr0 & 1);
510 sp[0].factor += (((sp[0].value ^ sp[0].fltr0) >> 31) | 1) &
511 ((sp[0].value ^ (sp[0].value - (sp[0].fltr6 * 16))) >> 31);
512 sp[0].fltr1 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr1) >> 6;
513 sp[0].fltr2 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr2) >> 4;
514 sp[0].fltr3 += (sp[0].fltr2 - sp[0].fltr3) >> 4;
515 sp[0].fltr4 += (sp[0].fltr3 - sp[0].fltr4) >> 4;
516 sp[0].value = (sp[0].fltr4 - sp[0].fltr5) >> 4;
517 sp[0].fltr5 += sp[0].value;
518 sp[0].fltr6 += (sp[0].value - sp[0].fltr6) >> 3;
519 sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
520
521 if (!stereo)
522 continue;
523
524 pp = s->ptable + ((sp[1].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
525 split = low + ((high - low) >> 8) * (*pp >> 16);
526
527 if (value <= split) {
528 high = split;
529 *pp += (UP - *pp) >> DECAY;
530 sp[1].fltr0 = -1;
531 } else {
532 low = split + 1;
533 *pp += (DOWN - *pp) >> DECAY;
534 sp[1].fltr0 = 0;
535 }
536
537 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
538 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
539 high = (high << 8) | 0xff;
540 low <<= 8;
541 }
542
543 sp[1].value += sp[1].fltr6 * 8;
544 sp[1].byte = (sp[1].byte << 1) | (sp[1].fltr0 & 1);
545 sp[1].factor += (((sp[1].value ^ sp[1].fltr0) >> 31) | 1) &
546 ((sp[1].value ^ (sp[1].value - (sp[1].fltr6 * 16))) >> 31);
547 sp[1].fltr1 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr1) >> 6;
548 sp[1].fltr2 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr2) >> 4;
549 sp[1].fltr3 += (sp[1].fltr2 - sp[1].fltr3) >> 4;
550 sp[1].fltr4 += (sp[1].fltr3 - sp[1].fltr4) >> 4;
551 sp[1].value = (sp[1].fltr4 - sp[1].fltr5) >> 4;
552 sp[1].fltr5 += sp[1].value;
553 sp[1].fltr6 += (sp[1].value - sp[1].fltr6) >> 3;
554 sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
555 }
556
557 checksum += (checksum << 1) + (*dst_l = sp[0].byte & 0xff);
558 sp[0].factor -= (sp[0].factor + 512) >> 10;
559 dst_l += 4;
560
561 if (stereo) {
562 checksum += (checksum << 1) + (*dst_r = filters[1].byte & 0xff);
563 filters[1].factor -= (filters[1].factor + 512) >> 10;
564 dst_r += 4;
565 }
566 }
567
568 if (wv_check_crc(s, checksum, 0)) {
569 if (s->avctx->err_recognition & AV_EF_CRCCHECK)
570 return AVERROR_INVALIDDATA;
571
572 memset(dst_left, 0x69, s->samples * 4);
573
574 if (dst_r)
575 memset(dst_right, 0x69, s->samples * 4);
576 }
577
578 return 0;
579 }
580
581 static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
582 {
583 uint8_t *dst_l = dst_left, *dst_r = dst_right;
584 uint8_t history_bits, max_probability;
585 int total_summed_probabilities = 0;
586 int total_samples = s->samples;
587 uint8_t *vlb = s->value_lookup_buffer;
588 int history_bins, p0, p1, chan;
589 uint32_t checksum = 0xFFFFFFFF;
590 uint32_t low, high, value;
591
592 if (!bytestream2_get_bytes_left(&s->gbyte))
593 return AVERROR_INVALIDDATA;
594
595 history_bits = bytestream2_get_byte(&s->gbyte);
596
597 if (!bytestream2_get_bytes_left(&s->gbyte) || history_bits > MAX_HISTORY_BITS)
598 return AVERROR_INVALIDDATA;
599
600 history_bins = 1 << history_bits;
601 max_probability = bytestream2_get_byte(&s->gbyte);
602
603 if (max_probability < 0xff) {
604 uint8_t *outptr = (uint8_t *)s->probabilities;
605 uint8_t *outend = outptr + sizeof(*s->probabilities) * history_bins;
606
607 while (outptr < outend && bytestream2_get_bytes_left(&s->gbyte)) {
608 int code = bytestream2_get_byte(&s->gbyte);
609
610 if (code > max_probability) {
611 int zcount = code - max_probability;
612
613 while (outptr < outend && zcount--)
614 *outptr++ = 0;
615 } else if (code) {
616 *outptr++ = code;
617 }
618 else {
619 break;
620 }
621 }
622
623 if (outptr < outend ||
624 (bytestream2_get_bytes_left(&s->gbyte) && bytestream2_get_byte(&s->gbyte)))
625 return AVERROR_INVALIDDATA;
626 } else if (bytestream2_get_bytes_left(&s->gbyte) > (int)sizeof(*s->probabilities) * history_bins) {
627 bytestream2_get_buffer(&s->gbyte, (uint8_t *)s->probabilities,
628 sizeof(*s->probabilities) * history_bins);
629 } else {
630 return AVERROR_INVALIDDATA;
631 }
632
633 for (p0 = 0; p0 < history_bins; p0++) {
634 int32_t sum_values = 0;
635
636 for (int i = 0; i < 256; i++)
637 s->summed_probabilities[p0][i] = sum_values += s->probabilities[p0][i];
638
639 if (sum_values) {
640 total_summed_probabilities += sum_values;
641
642 if (total_summed_probabilities > history_bins * MAX_BIN_BYTES)
643 return AVERROR_INVALIDDATA;
644
645 s->value_lookup[p0] = vlb;
646
647 for (int i = 0; i < 256; i++) {
648 int c = s->probabilities[p0][i];
649
650 while (c--)
651 *vlb++ = i;
652 }
653 }
654 }
655
656 if (bytestream2_get_bytes_left(&s->gbyte) < 4)
657 return AVERROR_INVALIDDATA;
658
659 chan = p0 = p1 = 0;
660 low = 0; high = 0xffffffff;
661 value = bytestream2_get_be32(&s->gbyte);
662
663 if (dst_r)
664 total_samples *= 2;
665
666 while (total_samples--) {
667 unsigned int mult, index, code;
668
669 if (!s->summed_probabilities[p0][255])
670 return AVERROR_INVALIDDATA;
671
672 mult = (high - low) / s->summed_probabilities[p0][255];
673
674 if (!mult) {
675 if (bytestream2_get_bytes_left(&s->gbyte) >= 4)
676 value = bytestream2_get_be32(&s->gbyte);
677
678 low = 0;
679 high = 0xffffffff;
680 mult = high / s->summed_probabilities[p0][255];
681
682 if (!mult)
683 return AVERROR_INVALIDDATA;
684 }
685
686 index = (value - low) / mult;
687
688 if (index >= s->summed_probabilities[p0][255])
689 return AVERROR_INVALIDDATA;
690
691 if (!dst_r) {
692 if ((*dst_l = code = s->value_lookup[p0][index]))
693 low += s->summed_probabilities[p0][code-1] * mult;
694
695 dst_l += 4;
696 } else {
697 if ((code = s->value_lookup[p0][index]))
698 low += s->summed_probabilities[p0][code-1] * mult;
699
700 if (chan) {
701 *dst_r = code;
702 dst_r += 4;
703 }
704 else {
705 *dst_l = code;
706 dst_l += 4;
707 }
708
709 chan ^= 1;
710 }
711
712 high = low + s->probabilities[p0][code] * mult - 1;
713 checksum += (checksum << 1) + code;
714
715 if (!dst_r) {
716 p0 = code & (history_bins-1);
717 } else {
718 p0 = p1;
719 p1 = code & (history_bins-1);
720 }
721
722 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
723 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
724 high = (high << 8) | 0xff;
725 low <<= 8;
726 }
727 }
728
729 if (wv_check_crc(s, checksum, 0)) {
730 if (s->avctx->err_recognition & AV_EF_CRCCHECK)
731 return AVERROR_INVALIDDATA;
732
733 memset(dst_left, 0x69, s->samples * 4);
734
735 if (dst_r)
736 memset(dst_right, 0x69, s->samples * 4);
737 }
738
739 return 0;
740 }
741
742 static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
743 {
744 uint8_t *dst_l = dst_left, *dst_r = dst_right;
745 int total_samples = s->samples;
746 uint32_t checksum = 0xFFFFFFFF;
747
748 if (bytestream2_get_bytes_left(&s->gbyte) != total_samples * (dst_r ? 2 : 1))
749 return AVERROR_INVALIDDATA;
750
751 while (total_samples--) {
752 checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&s->gbyte));
753 dst_l += 4;
754
755 if (dst_r) {
756 checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&s->gbyte));
757 dst_r += 4;
758 }
759 }
760
761 if (wv_check_crc(s, checksum, 0)) {
762 if (s->avctx->err_recognition & AV_EF_CRCCHECK)
763 return AVERROR_INVALIDDATA;
764
765 memset(dst_left, 0x69, s->samples * 4);
766
767 if (dst_r)
768 memset(dst_right, 0x69, s->samples * 4);
769 }
770
771 return 0;
772 }
773
774 7591 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
775 void *dst_l, void *dst_r, const int type)
776 {
777 7591 int i, j, count = 0;
778 int last, t;
779 int A, B, L, L2, R, R2;
780 7591 int pos = 0;
781 7591 uint32_t crc = 0xFFFFFFFF;
782 7591 uint32_t crc_extra_bits = 0xFFFFFFFF;
783 7591 int16_t *dst16_l = dst_l;
784 7591 int16_t *dst16_r = dst_r;
785 7591 int32_t *dst32_l = dst_l;
786 7591 int32_t *dst32_r = dst_r;
787 7591 float *dstfl_l = dst_l;
788 7591 float *dstfl_r = dst_r;
789
790 7591 s->one = s->zero = s->zeroes = 0;
791 do {
792 167565348 L = wv_get_value(s, gb, 0, &last);
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167565348 times.
167565348 if (last)
794 break;
795 167565348 R = wv_get_value(s, gb, 1, &last);
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167565348 times.
167565348 if (last)
797 break;
798
2/2
✓ Branch 0 taken 874059500 times.
✓ Branch 1 taken 167565348 times.
1041624848 for (i = 0; i < s->terms; i++) {
799 874059500 t = s->decorr[i].value;
800
2/2
✓ Branch 0 taken 857319428 times.
✓ Branch 1 taken 16740072 times.
874059500 if (t > 0) {
801
2/2
✓ Branch 0 taken 486097630 times.
✓ Branch 1 taken 371221798 times.
857319428 if (t > 8) {
802
2/2
✓ Branch 0 taken 157268390 times.
✓ Branch 1 taken 328829240 times.
486097630 if (t & 1) {
803 157268390 A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
804 157268390 B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
805 } else {
806 328829240 A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
807 328829240 B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
808 }
809 486097630 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
810 486097630 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
811 486097630 j = 0;
812 } else {
813 371221798 A = s->decorr[i].samplesA[pos];
814 371221798 B = s->decorr[i].samplesB[pos];
815 371221798 j = (pos + t) & 7;
816 }
817
2/2
✓ Branch 0 taken 21296280 times.
✓ Branch 1 taken 836023148 times.
857319428 if (type != AV_SAMPLE_FMT_S16P) {
818 21296280 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
819 21296280 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
820 } else {
821 836023148 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
822 836023148 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
823 }
824
4/4
✓ Branch 0 taken 194847390 times.
✓ Branch 1 taken 662472038 times.
✓ Branch 2 taken 189144006 times.
✓ Branch 3 taken 5703384 times.
857319428 if (A && L)
825 189144006 s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
826
4/4
✓ Branch 0 taken 194201846 times.
✓ Branch 1 taken 663117582 times.
✓ Branch 2 taken 187155846 times.
✓ Branch 3 taken 7046000 times.
857319428 if (B && R)
827 187155846 s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
828 857319428 s->decorr[i].samplesA[j] = L = L2;
829 857319428 s->decorr[i].samplesB[j] = R = R2;
830
2/2
✓ Branch 0 taken 3724236 times.
✓ Branch 1 taken 13015836 times.
16740072 } else if (t == -1) {
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3724236 times.
3724236 if (type != AV_SAMPLE_FMT_S16P)
832 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
833 else
834 3724236 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
835
9/10
✓ Branch 0 taken 3558418 times.
✓ Branch 1 taken 165818 times.
✓ Branch 2 taken 3424694 times.
✓ Branch 3 taken 133724 times.
✓ Branch 4 taken 1710424 times.
✓ Branch 5 taken 1714270 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1710424 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1714267 times.
3724236 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
836 3724236 L = L2;
837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3724236 times.
3724236 if (type != AV_SAMPLE_FMT_S16P)
838 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
839 else
840 3724236 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
841
10/10
✓ Branch 0 taken 3577466 times.
✓ Branch 1 taken 146770 times.
✓ Branch 2 taken 3380796 times.
✓ Branch 3 taken 196670 times.
✓ Branch 4 taken 1681154 times.
✓ Branch 5 taken 1699642 times.
✓ Branch 6 taken 403 times.
✓ Branch 7 taken 1680751 times.
✓ Branch 8 taken 18293 times.
✓ Branch 9 taken 1681349 times.
3724236 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
842 3724236 R = R2;
843 3724236 s->decorr[i].samplesA[0] = R;
844 } else {
845
2/2
✓ Branch 0 taken 815850 times.
✓ Branch 1 taken 12199986 times.
13015836 if (type != AV_SAMPLE_FMT_S16P)
846 815850 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
847 else
848 12199986 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
849
10/10
✓ Branch 0 taken 12113972 times.
✓ Branch 1 taken 901864 times.
✓ Branch 2 taken 11801283 times.
✓ Branch 3 taken 312689 times.
✓ Branch 4 taken 5912966 times.
✓ Branch 5 taken 5888317 times.
✓ Branch 6 taken 7735 times.
✓ Branch 7 taken 5905231 times.
✓ Branch 8 taken 427 times.
✓ Branch 9 taken 5887890 times.
13015836 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
850 13015836 R = R2;
851
852
2/2
✓ Branch 0 taken 10657190 times.
✓ Branch 1 taken 2358646 times.
13015836 if (t == -3) {
853 10657190 R2 = s->decorr[i].samplesA[0];
854 10657190 s->decorr[i].samplesA[0] = R;
855 }
856
857
2/2
✓ Branch 0 taken 815850 times.
✓ Branch 1 taken 12199986 times.
13015836 if (type != AV_SAMPLE_FMT_S16P)
858 815850 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
859 else
860 12199986 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
861
10/10
✓ Branch 0 taken 12073484 times.
✓ Branch 1 taken 942352 times.
✓ Branch 2 taken 11689188 times.
✓ Branch 3 taken 384296 times.
✓ Branch 4 taken 5828619 times.
✓ Branch 5 taken 5860569 times.
✓ Branch 6 taken 437 times.
✓ Branch 7 taken 5828182 times.
✓ Branch 8 taken 1285 times.
✓ Branch 9 taken 5859284 times.
13015836 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
862 13015836 L = L2;
863 13015836 s->decorr[i].samplesB[0] = L;
864 }
865 }
866
867
2/2
✓ Branch 0 taken 163142922 times.
✓ Branch 1 taken 4422426 times.
167565348 if (type == AV_SAMPLE_FMT_S16P) {
868
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 163142921 times.
163142922 if (FFABS((int64_t)L) + FFABS((int64_t)R) > (1<<19)) {
869 1 av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
870 1 return AVERROR_INVALIDDATA;
871 }
872 }
873
874 167565347 pos = (pos + 1) & 7;
875
2/2
✓ Branch 0 taken 161293901 times.
✓ Branch 1 taken 6271446 times.
167565347 if (s->joint)
876 161293901 L += (unsigned)(R -= (unsigned)(L >> 1));
877 167565347 crc = (crc * 3 + L) * 3 + R;
878
879
2/2
✓ Branch 0 taken 1371242 times.
✓ Branch 1 taken 166194105 times.
167565347 if (type == AV_SAMPLE_FMT_FLTP) {
880 1371242 *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
881 1371242 *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
882
2/2
✓ Branch 0 taken 3051184 times.
✓ Branch 1 taken 163142921 times.
166194105 } else if (type == AV_SAMPLE_FMT_S32P) {
883 3051184 *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
884 3051184 *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
885 } else {
886 163142921 *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
887 163142921 *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
888 }
889 167565347 count++;
890
3/4
✓ Branch 0 taken 167565347 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 167557757 times.
✓ Branch 3 taken 7590 times.
167565347 } while (!last && count < s->samples);
891
892
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7590 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7590 if (last && count < s->samples) {
893 int size = av_get_bytes_per_sample(type);
894 memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
895 memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
896 }
897
898
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7590 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7590 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
899 wv_check_crc(s, crc, crc_extra_bits))
900 return AVERROR_INVALIDDATA;
901
902 7590 return 0;
903 }
904
905 689 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
906 void *dst, const int type)
907 {
908 689 int i, j, count = 0;
909 int last, t;
910 int A, S, T;
911 689 int pos = 0;
912 689 uint32_t crc = 0xFFFFFFFF;
913 689 uint32_t crc_extra_bits = 0xFFFFFFFF;
914 689 int16_t *dst16 = dst;
915 689 int32_t *dst32 = dst;
916 689 float *dstfl = dst;
917
918 689 s->one = s->zero = s->zeroes = 0;
919 do {
920 15611361 T = wv_get_value(s, gb, 0, &last);
921 15611361 S = 0;
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15611361 times.
15611361 if (last)
923 break;
924
2/2
✓ Branch 0 taken 104876917 times.
✓ Branch 1 taken 15611361 times.
120488278 for (i = 0; i < s->terms; i++) {
925 104876917 t = s->decorr[i].value;
926
2/2
✓ Branch 0 taken 44372416 times.
✓ Branch 1 taken 60504501 times.
104876917 if (t > 8) {
927
2/2
✓ Branch 0 taken 11939195 times.
✓ Branch 1 taken 32433221 times.
44372416 if (t & 1)
928 11939195 A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
929 else
930 32433221 A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
931 44372416 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
932 44372416 j = 0;
933 } else {
934 60504501 A = s->decorr[i].samplesA[pos];
935 60504501 j = (pos + t) & 7;
936 }
937
2/2
✓ Branch 0 taken 29647840 times.
✓ Branch 1 taken 75229077 times.
104876917 if (type != AV_SAMPLE_FMT_S16P)
938 29647840 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
939 else
940 75229077 S = T + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
941
4/4
✓ Branch 0 taken 96585985 times.
✓ Branch 1 taken 8290932 times.
✓ Branch 2 taken 92156442 times.
✓ Branch 3 taken 4429543 times.
104876917 if (A && T)
942 92156442 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
943 104876917 s->decorr[i].samplesA[j] = T = S;
944 }
945 15611361 pos = (pos + 1) & 7;
946 15611361 crc = crc * 3 + S;
947
948
2/2
✓ Branch 0 taken 432000 times.
✓ Branch 1 taken 15179361 times.
15611361 if (type == AV_SAMPLE_FMT_FLTP) {
949 432000 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
950
2/2
✓ Branch 0 taken 4691168 times.
✓ Branch 1 taken 10488193 times.
15179361 } else if (type == AV_SAMPLE_FMT_S32P) {
951 4691168 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
952 } else {
953 10488193 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
954 }
955 15611361 count++;
956
3/4
✓ Branch 0 taken 15611361 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15610672 times.
✓ Branch 3 taken 689 times.
15611361 } while (!last && count < s->samples);
957
958
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
689 if (last && count < s->samples) {
959 int size = av_get_bytes_per_sample(type);
960 memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
961 }
962
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
689 if (s->avctx->err_recognition & AV_EF_CRCCHECK) {
964 int ret = wv_check_crc(s, crc, crc_extra_bits);
965 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
966 return ret;
967 }
968
969 689 return 0;
970 }
971
972 119 static av_cold int wv_alloc_frame_context(WavpackContext *c)
973 {
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
119 if (c->fdec_num == WV_MAX_FRAME_DECODERS)
975 return -1;
976
977 119 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
119 if (!c->fdec[c->fdec_num])
979 return -1;
980 119 c->fdec_num++;
981 119 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
982
983 119 return 0;
984 }
985
986 7548 static int wv_dsd_reset(WavpackContext *s, int channels)
987 {
988 int i;
989
990 7548 s->dsdctx = NULL;
991 7548 s->dsd_channels = 0;
992 7548 av_buffer_unref(&s->dsd_ref);
993
994
1/2
✓ Branch 0 taken 7548 times.
✗ Branch 1 not taken.
7548 if (!channels)
995 7548 return 0;
996
997 if (channels > INT_MAX / sizeof(*s->dsdctx))
998 return AVERROR(EINVAL);
999
1000 s->dsd_ref = av_buffer_allocz(channels * sizeof(*s->dsdctx));
1001 if (!s->dsd_ref)
1002 return AVERROR(ENOMEM);
1003 s->dsdctx = (DSDContext*)s->dsd_ref->data;
1004 s->dsd_channels = channels;
1005
1006 for (i = 0; i < channels; i++)
1007 memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
1008
1009 return 0;
1010 }
1011
1012 #if HAVE_THREADS
1013 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1014 {
1015 WavpackContext *fsrc = src->priv_data;
1016 WavpackContext *fdst = dst->priv_data;
1017 int ret;
1018
1019 if (dst == src)
1020 return 0;
1021
1022 ff_thread_release_ext_buffer(dst, &fdst->curr_frame);
1023 if (fsrc->curr_frame.f->data[0]) {
1024 if ((ret = ff_thread_ref_frame(&fdst->curr_frame, &fsrc->curr_frame)) < 0)
1025 return ret;
1026 }
1027
1028 fdst->dsdctx = NULL;
1029 fdst->dsd_channels = 0;
1030 ret = av_buffer_replace(&fdst->dsd_ref, fsrc->dsd_ref);
1031 if (ret < 0)
1032 return ret;
1033 if (fsrc->dsd_ref) {
1034 fdst->dsdctx = (DSDContext*)fdst->dsd_ref->data;
1035 fdst->dsd_channels = fsrc->dsd_channels;
1036 }
1037
1038 return 0;
1039 }
1040 #endif
1041
1042 66 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
1043 {
1044 66 WavpackContext *s = avctx->priv_data;
1045
1046 66 s->avctx = avctx;
1047
1048 66 s->fdec_num = 0;
1049
1050 66 s->curr_frame.f = av_frame_alloc();
1051 66 s->prev_frame.f = av_frame_alloc();
1052
1053
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
66 if (!s->curr_frame.f || !s->prev_frame.f)
1054 return AVERROR(ENOMEM);
1055
1056 66 ff_init_dsd_data();
1057
1058 66 return 0;
1059 }
1060
1061 66 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
1062 {
1063 66 WavpackContext *s = avctx->priv_data;
1064
1065
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 66 times.
185 for (int i = 0; i < s->fdec_num; i++)
1066 119 av_freep(&s->fdec[i]);
1067 66 s->fdec_num = 0;
1068
1069 66 ff_thread_release_ext_buffer(avctx, &s->curr_frame);
1070 66 av_frame_free(&s->curr_frame.f);
1071
1072 66 ff_thread_release_ext_buffer(avctx, &s->prev_frame);
1073 66 av_frame_free(&s->prev_frame.f);
1074
1075 66 av_buffer_unref(&s->dsd_ref);
1076
1077 66 return 0;
1078 }
1079
1080 8280 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
1081 const uint8_t *buf, int buf_size)
1082 {
1083 8280 WavpackContext *wc = avctx->priv_data;
1084 WavpackFrameContext *s;
1085 GetByteContext gb;
1086 enum AVSampleFormat sample_fmt;
1087 8280 void *samples_l = NULL, *samples_r = NULL;
1088 int ret;
1089 8280 int got_terms = 0, got_weights = 0, got_samples = 0,
1090 8280 got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1091 8280 int got_dsd = 0;
1092 int i, j, id, size, ssize, weights, t;
1093 8280 int bpp, chan = 0, orig_bpp, sample_rate = 0, rate_x = 1, dsd_mode = 0;
1094 int multiblock;
1095 8280 uint64_t chmask = 0;
1096
1097
3/4
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 8161 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 119 times.
8280 if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
1098 av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
1099 return AVERROR_INVALIDDATA;
1100 }
1101
1102 8280 s = wc->fdec[block_no];
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!s) {
1104 av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
1105 block_no);
1106 return AVERROR_INVALIDDATA;
1107 }
1108
1109 8280 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
1110 8280 memset(s->ch, 0, sizeof(s->ch));
1111 8280 s->extra_bits = 0;
1112 8280 s->and = s->or = s->shift = 0;
1113 8280 s->got_extra_bits = 0;
1114
1115 8280 bytestream2_init(&gb, buf, buf_size);
1116
1117 8280 s->samples = bytestream2_get_le32(&gb);
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (s->samples != wc->samples) {
1119 av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
1120 "a sequence: %d and %d\n", wc->samples, s->samples);
1121 return AVERROR_INVALIDDATA;
1122 }
1123 8280 s->frame_flags = bytestream2_get_le32(&gb);
1124
1125
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 8213 times.
8280 if (s->frame_flags & (WV_FLOAT_DATA | WV_DSD_DATA))
1126 67 sample_fmt = AV_SAMPLE_FMT_FLTP;
1127
2/2
✓ Branch 0 taken 7887 times.
✓ Branch 1 taken 326 times.
8213 else if ((s->frame_flags & 0x03) <= 1)
1128 7887 sample_fmt = AV_SAMPLE_FMT_S16P;
1129 else
1130 326 sample_fmt = AV_SAMPLE_FMT_S32P;
1131
1132
3/4
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 7548 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 732 times.
8280 if (wc->ch_offset && avctx->sample_fmt != sample_fmt)
1133 return AVERROR_INVALIDDATA;
1134
1135 8280 bpp = av_get_bytes_per_sample(sample_fmt);
1136 8280 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
1137 8280 multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
1138
1139 8280 s->stereo = !(s->frame_flags & WV_MONO);
1140
2/2
✓ Branch 0 taken 8238 times.
✓ Branch 1 taken 42 times.
8280 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
1141 8280 s->joint = s->frame_flags & WV_JOINT_STEREO;
1142 8280 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
1143 8280 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
1144 8280 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
1145
2/4
✓ Branch 0 taken 8280 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8280 times.
8280 if (s->post_shift < 0 || s->post_shift > 31) {
1146 return AVERROR_INVALIDDATA;
1147 }
1148 8280 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
1149 8280 s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
1150 8280 s->CRC = bytestream2_get_le32(&gb);
1151
1152 // parse metadata blocks
1153
2/2
✓ Branch 1 taken 49542 times.
✓ Branch 2 taken 8280 times.
57822 while (bytestream2_get_bytes_left(&gb)) {
1154 49542 id = bytestream2_get_byte(&gb);
1155 49542 size = bytestream2_get_byte(&gb);
1156
2/2
✓ Branch 0 taken 8306 times.
✓ Branch 1 taken 41236 times.
49542 if (id & WP_IDF_LONG)
1157 8306 size |= (bytestream2_get_le16u(&gb)) << 8;
1158 49542 size <<= 1; // size is specified in words
1159 49542 ssize = size;
1160
2/2
✓ Branch 0 taken 8740 times.
✓ Branch 1 taken 40802 times.
49542 if (id & WP_IDF_ODD)
1161 8740 size--;
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49542 times.
49542 if (size < 0) {
1163 av_log(avctx, AV_LOG_ERROR,
1164 "Got incorrect block %02X with size %i\n", id, size);
1165 break;
1166 }
1167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49542 times.
49542 if (bytestream2_get_bytes_left(&gb) < ssize) {
1168 av_log(avctx, AV_LOG_ERROR,
1169 "Block size %i is out of bounds\n", size);
1170 break;
1171 }
1172
11/13
✓ Branch 0 taken 8280 times.
✓ Branch 1 taken 8280 times.
✓ Branch 2 taken 8280 times.
✓ Branch 3 taken 8280 times.
✓ Branch 4 taken 7459 times.
✓ Branch 5 taken 168 times.
✓ Branch 6 taken 67 times.
✓ Branch 7 taken 8280 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 26 times.
✓ Branch 10 taken 297 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 125 times.
49542 switch (id & WP_IDF_MASK) {
1173 8280 case WP_ID_DECTERMS:
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (size > MAX_TERMS) {
1175 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
1176 s->terms = 0;
1177 bytestream2_skip(&gb, ssize);
1178 continue;
1179 }
1180 8280 s->terms = size;
1181
2/2
✓ Branch 0 taken 44344 times.
✓ Branch 1 taken 8280 times.
52624 for (i = 0; i < s->terms; i++) {
1182 44344 uint8_t val = bytestream2_get_byte(&gb);
1183 44344 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
1184 44344 s->decorr[s->terms - i - 1].delta = val >> 5;
1185 }
1186 8280 got_terms = 1;
1187 8280 break;
1188 8280 case WP_ID_DECWEIGHTS:
1189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!got_terms) {
1190 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1191 continue;
1192 }
1193 8280 weights = size >> s->stereo_in;
1194
2/4
✓ Branch 0 taken 8280 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8280 times.
8280 if (weights > MAX_TERMS || weights > s->terms) {
1195 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
1196 bytestream2_skip(&gb, ssize);
1197 continue;
1198 }
1199
2/2
✓ Branch 0 taken 14571 times.
✓ Branch 1 taken 8280 times.
22851 for (i = 0; i < weights; i++) {
1200 14571 t = (int8_t)bytestream2_get_byte(&gb);
1201 14571 s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
1202
2/2
✓ Branch 0 taken 9857 times.
✓ Branch 1 taken 4714 times.
14571 if (s->decorr[s->terms - i - 1].weightA > 0)
1203 9857 s->decorr[s->terms - i - 1].weightA +=
1204 9857 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
1205
2/2
✓ Branch 0 taken 9956 times.
✓ Branch 1 taken 4615 times.
14571 if (s->stereo_in) {
1206 9956 t = (int8_t)bytestream2_get_byte(&gb);
1207 9956 s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
1208
2/2
✓ Branch 0 taken 6850 times.
✓ Branch 1 taken 3106 times.
9956 if (s->decorr[s->terms - i - 1].weightB > 0)
1209 6850 s->decorr[s->terms - i - 1].weightB +=
1210 6850 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
1211 }
1212 }
1213 8280 got_weights = 1;
1214 8280 break;
1215 8280 case WP_ID_DECSAMPLES:
1216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!got_terms) {
1217 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1218 continue;
1219 }
1220 8280 t = 0;
1221
4/4
✓ Branch 0 taken 16502 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 8274 times.
✓ Branch 3 taken 8228 times.
16554 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
1222
2/2
✓ Branch 0 taken 8057 times.
✓ Branch 1 taken 217 times.
8274 if (s->decorr[i].value > 8) {
1223 8057 s->decorr[i].samplesA[0] =
1224 8057 wp_exp2(bytestream2_get_le16(&gb));
1225 8057 s->decorr[i].samplesA[1] =
1226 8057 wp_exp2(bytestream2_get_le16(&gb));
1227
1228
2/2
✓ Branch 0 taken 7453 times.
✓ Branch 1 taken 604 times.
8057 if (s->stereo_in) {
1229 7453 s->decorr[i].samplesB[0] =
1230 7453 wp_exp2(bytestream2_get_le16(&gb));
1231 7453 s->decorr[i].samplesB[1] =
1232 7453 wp_exp2(bytestream2_get_le16(&gb));
1233 7453 t += 4;
1234 }
1235 8057 t += 4;
1236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
217 } else if (s->decorr[i].value < 0) {
1237 s->decorr[i].samplesA[0] =
1238 wp_exp2(bytestream2_get_le16(&gb));
1239 s->decorr[i].samplesB[0] =
1240 wp_exp2(bytestream2_get_le16(&gb));
1241 t += 4;
1242 } else {
1243
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 217 times.
592 for (j = 0; j < s->decorr[i].value; j++) {
1244 375 s->decorr[i].samplesA[j] =
1245 375 wp_exp2(bytestream2_get_le16(&gb));
1246
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 154 times.
375 if (s->stereo_in) {
1247 221 s->decorr[i].samplesB[j] =
1248 221 wp_exp2(bytestream2_get_le16(&gb));
1249 }
1250 }
1251 217 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
1252 }
1253 }
1254 8280 got_samples = 1;
1255 8280 break;
1256 8280 case WP_ID_ENTROPY:
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (size != 6 * (s->stereo_in + 1)) {
1258 av_log(avctx, AV_LOG_ERROR,
1259 "Entropy vars size should be %i, got %i.\n",
1260 6 * (s->stereo_in + 1), size);
1261 bytestream2_skip(&gb, ssize);
1262 continue;
1263 }
1264
2/2
✓ Branch 0 taken 15871 times.
✓ Branch 1 taken 8280 times.
24151 for (j = 0; j <= s->stereo_in; j++)
1265
2/2
✓ Branch 0 taken 47613 times.
✓ Branch 1 taken 15871 times.
63484 for (i = 0; i < 3; i++) {
1266 47613 s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
1267 }
1268 8280 got_entropy = 1;
1269 8280 break;
1270 7459 case WP_ID_HYBRID:
1271
1/2
✓ Branch 0 taken 7459 times.
✗ Branch 1 not taken.
7459 if (s->hybrid_bitrate) {
1272
2/2
✓ Branch 0 taken 14526 times.
✓ Branch 1 taken 7459 times.
21985 for (i = 0; i <= s->stereo_in; i++) {
1273 14526 s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
1274 14526 size -= 2;
1275 }
1276 }
1277
2/2
✓ Branch 0 taken 14526 times.
✓ Branch 1 taken 7459 times.
21985 for (i = 0; i < (s->stereo_in + 1); i++) {
1278 14526 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
1279 14526 size -= 2;
1280 }
1281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7459 times.
7459 if (size > 0) {
1282 for (i = 0; i < (s->stereo_in + 1); i++) {
1283 s->ch[i].bitrate_delta =
1284 wp_exp2((int16_t)bytestream2_get_le16(&gb));
1285 }
1286 } else {
1287
2/2
✓ Branch 0 taken 14526 times.
✓ Branch 1 taken 7459 times.
21985 for (i = 0; i < (s->stereo_in + 1); i++)
1288 14526 s->ch[i].bitrate_delta = 0;
1289 }
1290 7459 got_hybrid = 1;
1291 7459 break;
1292 168 case WP_ID_INT32INFO: {
1293 uint8_t val[4];
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (size != 4) {
1295 av_log(avctx, AV_LOG_ERROR,
1296 "Invalid INT32INFO, size = %i\n",
1297 size);
1298 bytestream2_skip(&gb, ssize - 4);
1299 continue;
1300 }
1301 168 bytestream2_get_buffer(&gb, val, 4);
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (val[0] > 30) {
1303 av_log(avctx, AV_LOG_ERROR,
1304 "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
1305 continue;
1306 } else {
1307 168 s->extra_bits = val[0];
1308 }
1309
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 8 times.
168 if (val[1])
1310 160 s->shift = val[1];
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (val[2]) {
1312 s->and = s->or = 1;
1313 s->shift = val[2];
1314 }
1315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (val[3]) {
1316 s->and = 1;
1317 s->shift = val[3];
1318 }
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (s->shift > 31) {
1320 av_log(avctx, AV_LOG_ERROR,
1321 "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
1322 s->and = s->or = s->shift = 0;
1323 continue;
1324 }
1325 /* original WavPack decoder forces 32-bit lossy sound to be treated
1326 * as 24-bit one in order to have proper clipping */
1327
5/8
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 147 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 147 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 147 times.
168 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
1328 s->post_shift += 8;
1329 s->shift -= 8;
1330 s->hybrid_maxclip >>= 8;
1331 s->hybrid_minclip >>= 8;
1332 }
1333 168 break;
1334 }
1335 67 case WP_ID_FLOATINFO:
1336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (size != 4) {
1337 av_log(avctx, AV_LOG_ERROR,
1338 "Invalid FLOATINFO, size = %i\n", size);
1339 bytestream2_skip(&gb, ssize);
1340 continue;
1341 }
1342 67 s->float_flag = bytestream2_get_byte(&gb);
1343 67 s->float_shift = bytestream2_get_byte(&gb);
1344 67 s->float_max_exp = bytestream2_get_byte(&gb);
1345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (s->float_shift > 31) {
1346 av_log(avctx, AV_LOG_ERROR,
1347 "Invalid FLOATINFO, shift = %d (> 31)\n", s->float_shift);
1348 s->float_shift = 0;
1349 continue;
1350 }
1351 67 got_float = 1;
1352 67 bytestream2_skip(&gb, 1);
1353 67 break;
1354 8280 case WP_ID_DATA:
1355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8280 times.
8280 if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
1356 return ret;
1357 8280 bytestream2_skip(&gb, size);
1358 8280 got_pcm = 1;
1359 8280 break;
1360 case WP_ID_DSD_DATA:
1361 if (size < 2) {
1362 av_log(avctx, AV_LOG_ERROR, "Invalid DSD_DATA, size = %i\n",
1363 size);
1364 bytestream2_skip(&gb, ssize);
1365 continue;
1366 }
1367 rate_x = bytestream2_get_byte(&gb);
1368 if (rate_x > 30)
1369 return AVERROR_INVALIDDATA;
1370 rate_x = 1 << rate_x;
1371 dsd_mode = bytestream2_get_byte(&gb);
1372 if (dsd_mode && dsd_mode != 1 && dsd_mode != 3) {
1373 av_log(avctx, AV_LOG_ERROR, "Invalid DSD encoding mode: %d\n",
1374 dsd_mode);
1375 return AVERROR_INVALIDDATA;
1376 }
1377 bytestream2_init(&s->gbyte, gb.buffer, size-2);
1378 bytestream2_skip(&gb, size-2);
1379 got_dsd = 1;
1380 break;
1381 26 case WP_ID_EXTRABITS:
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (size <= 4) {
1383 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
1384 size);
1385 bytestream2_skip(&gb, size);
1386 continue;
1387 }
1388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
1389 return ret;
1390 26 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
1391 26 bytestream2_skip(&gb, size);
1392 26 s->got_extra_bits = 1;
1393 26 break;
1394 297 case WP_ID_CHANINFO:
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (size <= 1) {
1396 av_log(avctx, AV_LOG_ERROR,
1397 "Insufficient channel information\n");
1398 return AVERROR_INVALIDDATA;
1399 }
1400 297 chan = bytestream2_get_byte(&gb);
1401
2/7
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 274 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
297 switch (size - 2) {
1402 23 case 0:
1403 23 chmask = bytestream2_get_byte(&gb);
1404 23 break;
1405 274 case 1:
1406 274 chmask = bytestream2_get_le16(&gb);
1407 274 break;
1408 case 2:
1409 chmask = bytestream2_get_le24(&gb);
1410 break;
1411 case 3:
1412 chmask = bytestream2_get_le32(&gb);
1413 break;
1414 case 4:
1415 size = bytestream2_get_byte(&gb);
1416 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1417 chan += 1;
1418 if (avctx->ch_layout.nb_channels != chan)
1419 av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1420 " instead of %i.\n", chan, avctx->ch_layout.nb_channels);
1421 chmask = bytestream2_get_le24(&gb);
1422 break;
1423 case 5:
1424 size = bytestream2_get_byte(&gb);
1425 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1426 chan += 1;
1427 if (avctx->ch_layout.nb_channels != chan)
1428 av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1429 " instead of %i.\n", chan, avctx->ch_layout.nb_channels);
1430 chmask = bytestream2_get_le32(&gb);
1431 break;
1432 default:
1433 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
1434 size);
1435 }
1436 297 break;
1437 case WP_ID_SAMPLE_RATE:
1438 if (size != 3) {
1439 av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
1440 return AVERROR_INVALIDDATA;
1441 }
1442 sample_rate = bytestream2_get_le24(&gb);
1443 break;
1444 125 default:
1445 125 bytestream2_skip(&gb, size);
1446 }
1447
2/2
✓ Branch 0 taken 8740 times.
✓ Branch 1 taken 40802 times.
49542 if (id & WP_IDF_ODD)
1448 8740 bytestream2_skip(&gb, 1);
1449 }
1450
1451
1/2
✓ Branch 0 taken 8280 times.
✗ Branch 1 not taken.
8280 if (got_pcm) {
1452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!got_terms) {
1453 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1454 return AVERROR_INVALIDDATA;
1455 }
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!got_weights) {
1457 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1458 return AVERROR_INVALIDDATA;
1459 }
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!got_samples) {
1461 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1462 return AVERROR_INVALIDDATA;
1463 }
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (!got_entropy) {
1465 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1466 return AVERROR_INVALIDDATA;
1467 }
1468
3/4
✓ Branch 0 taken 7459 times.
✓ Branch 1 taken 821 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7459 times.
8280 if (s->hybrid && !got_hybrid) {
1469 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1470 return AVERROR_INVALIDDATA;
1471 }
1472
3/4
✓ Branch 0 taken 8213 times.
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8213 times.
8280 if (!got_float && sample_fmt == AV_SAMPLE_FMT_FLTP) {
1473 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1474 return AVERROR_INVALIDDATA;
1475 }
1476
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8254 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 18 times.
8280 if (s->got_extra_bits && sample_fmt != AV_SAMPLE_FMT_FLTP) {
1477 8 const int size = get_bits_left(&s->gb_extra_bits);
1478 8 const int wanted = s->samples * s->extra_bits << s->stereo_in;
1479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (size < wanted) {
1480 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1481 s->got_extra_bits = 0;
1482 }
1483 }
1484 }
1485
1486
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8280 if (!got_pcm && !got_dsd) {
1487 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1488 return AVERROR_INVALIDDATA;
1489 }
1490
1491
3/6
✓ Branch 0 taken 8280 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8280 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8280 times.
8280 if ((got_pcm && wc->modulation != MODULATION_PCM) ||
1492 (got_dsd && wc->modulation != MODULATION_DSD)) {
1493 av_log(avctx, AV_LOG_ERROR, "Invalid PCM/DSD mix encountered\n");
1494 return AVERROR_INVALIDDATA;
1495 }
1496
1497
2/2
✓ Branch 0 taken 7548 times.
✓ Branch 1 taken 732 times.
8280 if (!wc->ch_offset) {
1498 7548 AVChannelLayout new_ch_layout = { 0 };
1499 int new_samplerate;
1500 7548 int sr = (s->frame_flags >> 23) & 0xf;
1501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
7548 if (sr == 0xf) {
1502 if (!sample_rate) {
1503 av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
1504 return AVERROR_INVALIDDATA;
1505 }
1506 new_samplerate = sample_rate;
1507 } else
1508 7548 new_samplerate = wv_rates[sr];
1509
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
7548 if (new_samplerate * (uint64_t)rate_x > INT_MAX)
1511 return AVERROR_INVALIDDATA;
1512 7548 new_samplerate *= rate_x;
1513
1514
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 7251 times.
7548 if (multiblock) {
1515
1/2
✓ Branch 0 taken 297 times.
✗ Branch 1 not taken.
297 if (chmask) {
1516 297 av_channel_layout_from_mask(&new_ch_layout, chmask);
1517
2/4
✓ Branch 0 taken 297 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 297 times.
297 if (chan && new_ch_layout.nb_channels != chan) {
1518 av_log(avctx, AV_LOG_ERROR, "Channel mask does not match the channel count\n");
1519 return AVERROR_INVALIDDATA;
1520 }
1521 } else {
1522 ret = av_channel_layout_copy(&new_ch_layout, &avctx->ch_layout);
1523 if (ret < 0) {
1524 av_log(avctx, AV_LOG_ERROR, "Error copying channel layout\n");
1525 return ret;
1526 }
1527 }
1528 } else {
1529 7251 av_channel_layout_default(&new_ch_layout, s->stereo + 1);
1530 }
1531
1532 /* clear DSD state if stream properties change */
1533
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7548 if (new_ch_layout.nb_channels != wc->dsd_channels ||
1534 av_channel_layout_compare(&new_ch_layout, &avctx->ch_layout) ||
1535 new_samplerate != avctx->sample_rate ||
1536 !!got_dsd != !!wc->dsdctx) {
1537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
7548 ret = wv_dsd_reset(wc, got_dsd ? new_ch_layout.nb_channels : 0);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
7548 if (ret < 0) {
1539 av_log(avctx, AV_LOG_ERROR, "Error reinitializing the DSD context\n");
1540 return ret;
1541 }
1542 7548 ff_thread_release_ext_buffer(avctx, &wc->curr_frame);
1543 }
1544 7548 av_channel_layout_copy(&avctx->ch_layout, &new_ch_layout);
1545 7548 avctx->sample_rate = new_samplerate;
1546 7548 avctx->sample_fmt = sample_fmt;
1547 7548 avctx->bits_per_raw_sample = orig_bpp;
1548
1549 7548 ff_thread_release_ext_buffer(avctx, &wc->prev_frame);
1550 7548 FFSWAP(ThreadFrame, wc->curr_frame, wc->prev_frame);
1551
1552 /* get output buffer */
1553 7548 wc->curr_frame.f->nb_samples = s->samples;
1554 7548 ret = ff_thread_get_ext_buffer(avctx, &wc->curr_frame,
1555 AV_GET_BUFFER_FLAG_REF);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
7548 if (ret < 0)
1557 return ret;
1558
1559 7548 wc->frame = wc->curr_frame.f;
1560 7548 ff_thread_finish_setup(avctx);
1561 }
1562
1563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8280 times.
8280 if (wc->ch_offset + s->stereo >= avctx->ch_layout.nb_channels) {
1564 av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
1565 return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
1566 }
1567
1568 8280 samples_l = wc->frame->extended_data[wc->ch_offset];
1569
2/2
✓ Branch 0 taken 7633 times.
✓ Branch 1 taken 647 times.
8280 if (s->stereo)
1570 7633 samples_r = wc->frame->extended_data[wc->ch_offset + 1];
1571
1572 8280 wc->ch_offset += 1 + s->stereo;
1573
1574
2/2
✓ Branch 0 taken 7591 times.
✓ Branch 1 taken 689 times.
8280 if (s->stereo_in) {
1575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7591 times.
7591 if (got_dsd) {
1576 if (dsd_mode == 3) {
1577 ret = wv_unpack_dsd_high(s, samples_l, samples_r);
1578 } else if (dsd_mode == 1) {
1579 ret = wv_unpack_dsd_fast(s, samples_l, samples_r);
1580 } else {
1581 ret = wv_unpack_dsd_copy(s, samples_l, samples_r);
1582 }
1583 } else {
1584 7591 ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1585 }
1586
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7590 times.
7591 if (ret < 0)
1587 1 return ret;
1588 } else {
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
689 if (got_dsd) {
1590 if (dsd_mode == 3) {
1591 ret = wv_unpack_dsd_high(s, samples_l, NULL);
1592 } else if (dsd_mode == 1) {
1593 ret = wv_unpack_dsd_fast(s, samples_l, NULL);
1594 } else {
1595 ret = wv_unpack_dsd_copy(s, samples_l, NULL);
1596 }
1597 } else {
1598 689 ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1599 }
1600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
689 if (ret < 0)
1601 return ret;
1602
1603
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 647 times.
689 if (s->stereo)
1604 42 memcpy(samples_r, samples_l, bpp * s->samples);
1605 }
1606
1607 8279 return 0;
1608 }
1609
1610 static void wavpack_decode_flush(AVCodecContext *avctx)
1611 {
1612 WavpackContext *s = avctx->priv_data;
1613
1614 wv_dsd_reset(s, 0);
1615 }
1616
1617 static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
1618 {
1619 WavpackContext *s = avctx->priv_data;
1620 AVFrame *frame = frmptr;
1621
1622 ff_dsd2pcm_translate (&s->dsdctx [jobnr], s->samples, 0,
1623 (uint8_t *)frame->extended_data[jobnr], 4,
1624 (float *)frame->extended_data[jobnr], 1);
1625
1626 return 0;
1627 }
1628
1629 7548 static int wavpack_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
1630 int *got_frame_ptr, AVPacket *avpkt)
1631 {
1632 7548 WavpackContext *s = avctx->priv_data;
1633 7548 const uint8_t *buf = avpkt->data;
1634 7548 int buf_size = avpkt->size;
1635 int frame_size, ret, frame_flags;
1636
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7548 times.
7548 if (avpkt->size <= WV_HEADER_SIZE)
1638 return AVERROR_INVALIDDATA;
1639
1640 7548 s->frame = NULL;
1641 7548 s->block = 0;
1642 7548 s->ch_offset = 0;
1643
1644 /* determine number of samples */
1645 7548 s->samples = AV_RL32(buf + 20);
1646 7548 frame_flags = AV_RL32(buf + 24);
1647
2/4
✓ Branch 0 taken 7548 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7548 times.
7548 if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1648 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1649 s->samples);
1650 return AVERROR_INVALIDDATA;
1651 }
1652
1653 7548 s->modulation = (frame_flags & WV_DSD_DATA) ? MODULATION_DSD : MODULATION_PCM;
1654
1655
2/2
✓ Branch 0 taken 8280 times.
✓ Branch 1 taken 7547 times.
15827 while (buf_size > WV_HEADER_SIZE) {
1656 8280 frame_size = AV_RL32(buf + 4) - 12;
1657 8280 buf += 20;
1658 8280 buf_size -= 20;
1659
2/4
✓ Branch 0 taken 8280 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8280 times.
8280 if (frame_size <= 0 || frame_size > buf_size) {
1660 av_log(avctx, AV_LOG_ERROR,
1661 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1662 s->block, frame_size, buf_size);
1663 ret = AVERROR_INVALIDDATA;
1664 goto error;
1665 }
1666
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8279 times.
8280 if ((ret = wavpack_decode_block(avctx, s->block, buf, frame_size)) < 0)
1667 1 goto error;
1668 8279 s->block++;
1669 8279 buf += frame_size;
1670 8279 buf_size -= frame_size;
1671 }
1672
1673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7547 times.
7547 if (s->ch_offset != avctx->ch_layout.nb_channels) {
1674 av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
1675 ret = AVERROR_INVALIDDATA;
1676 goto error;
1677 }
1678
1679 7547 ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1680 7547 ff_thread_release_ext_buffer(avctx, &s->prev_frame);
1681
1682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7547 times.
7547 if (s->modulation == MODULATION_DSD)
1683 avctx->execute2(avctx, dsd_channel, s->frame, NULL, avctx->ch_layout.nb_channels);
1684
1685 7547 ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1686
1687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7547 times.
7547 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
1688 return ret;
1689
1690 7547 *got_frame_ptr = 1;
1691
1692 7547 return avpkt->size;
1693
1694 1 error:
1695
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->frame) {
1696 1 ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1697 1 ff_thread_release_ext_buffer(avctx, &s->prev_frame);
1698 1 ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1699 }
1700
1701 1 return ret;
1702 }
1703
1704 const FFCodec ff_wavpack_decoder = {
1705 .p.name = "wavpack",
1706 .p.long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1707 .p.type = AVMEDIA_TYPE_AUDIO,
1708 .p.id = AV_CODEC_ID_WAVPACK,
1709 .priv_data_size = sizeof(WavpackContext),
1710 .init = wavpack_decode_init,
1711 .close = wavpack_decode_end,
1712 FF_CODEC_DECODE_CB(wavpack_decode_frame),
1713 .flush = wavpack_decode_flush,
1714 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1715 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1716 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_CHANNEL_CONF,
1717 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
1718 FF_CODEC_CAP_ALLOCATE_PROGRESS,
1719 };
1720