FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpack.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 802 1067 75.2%
Functions: 19 24 79.2%
Branches: 496 708 70.1%

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