FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpack.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 586 1074 54.6%
Branches: 377 714 52.8%

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