FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpack.c
Date: 2026-09-17 13:51:07
Exec Total Coverage
Lines: 820 1095 74.9%
Functions: 19 25 76.0%
Branches: 511 734 69.6%

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