FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpack.c
Date: 2024-03-29 11:55:30
Exec Total Coverage
Lines: 585 1063 55.0%
Functions: 13 21 61.9%
Branches: 372 704 52.8%

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