FFmpeg
Loading...
Searching...
No Matches
wavpack.c
Go to the documentation of this file.
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
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
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
69typedef enum {
70 MODULATION_PCM, // pulse code modulation
71 MODULATION_DSD // pulse density modulation (aka DSD)
73
105
106typedef struct WavpackContext {
108
111
114
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
127
128#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
129typedef 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
139static av_always_inline unsigned get_tail(GetBitContext *gb, unsigned k)
140{
141 int p, e, res;
142
143 if (k < 1)
144 return 0;
145 p = av_log2(k);
146 e = (1LL << (p + 1)) - k - 1;
147 res = get_bits_long(gb, p);
148 if (res >= e)
149 res = res * 2U - e + get_bits1(gb);
150 return res;
151}
152
154{
155 int i, br[2], sl[2];
156
157 for (i = 0; i <= ctx->stereo_in; i++) {
158 if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
159 return AVERROR_INVALIDDATA;
160 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
161 br[i] = ctx->ch[i].bitrate_acc >> 16;
162 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
163 }
164 if (ctx->stereo_in && ctx->hybrid_bitrate) {
165 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
166 if (balance > br[0]) {
167 br[1] = br[0] * 2;
168 br[0] = 0;
169 } else if (-balance > br[0]) {
170 br[0] *= 2;
171 br[1] = 0;
172 } else {
173 br[1] = br[0] + balance;
174 br[0] = br[0] - balance;
175 }
176 }
177 for (i = 0; i <= ctx->stereo_in; i++) {
178 if (ctx->hybrid_bitrate) {
179 if (sl[i] - br[i] > -0x100)
180 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
181 else
182 ctx->ch[i].error_limit = 0;
183 } else {
184 ctx->ch[i].error_limit = wp_exp2(br[i]);
185 }
186 }
187
188 return 0;
189}
190
192 int channel, int *last)
193{
194 int t, t2;
195 int sign, base, add, ret;
196 WvChannel *c = &ctx->ch[channel];
197
198 *last = 0;
199
200 if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
201 !ctx->zero && !ctx->one) {
202 if (ctx->zeroes) {
203 ctx->zeroes--;
204 if (ctx->zeroes) {
205 c->slow_level -= LEVEL_DECAY(c->slow_level);
206 return 0;
207 }
208 } else {
209 t = get_unary_0_33(gb);
210 if (t >= 2) {
211 if (t >= 32 || get_bits_left(gb) < t - 1)
212 goto error;
213 t = get_bits_long(gb, t - 1) | (1 << (t - 1));
214 } else {
215 if (get_bits_left(gb) < 0)
216 goto error;
217 }
218 ctx->zeroes = t;
219 if (ctx->zeroes) {
220 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
221 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
222 c->slow_level -= LEVEL_DECAY(c->slow_level);
223 return 0;
224 }
225 }
226 }
227
228 if (ctx->zero) {
229 t = 0;
230 ctx->zero = 0;
231 } else {
232 t = get_unary_0_33(gb);
233 if (get_bits_left(gb) < 0)
234 goto error;
235 if (t == 16) {
236 t2 = get_unary_0_33(gb);
237 if (t2 < 2) {
238 if (get_bits_left(gb) < 0)
239 goto error;
240 t += t2;
241 } else {
242 if (t2 >= 32 || get_bits_left(gb) < t2 - 1)
243 goto error;
244 t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
245 }
246 }
247
248 if (ctx->one) {
249 ctx->one = t & 1;
250 t = (t >> 1) + 1;
251 } else {
252 ctx->one = t & 1;
253 t >>= 1;
254 }
255 ctx->zero = !ctx->one;
256 }
257
258 if (ctx->hybrid && !channel) {
259 if (update_error_limit(ctx) < 0)
260 goto error;
261 }
262
263 if (!t) {
264 base = 0;
265 add = GET_MED(0) - 1;
266 DEC_MED(0);
267 } else if (t == 1) {
268 base = GET_MED(0);
269 add = GET_MED(1) - 1;
270 INC_MED(0);
271 DEC_MED(1);
272 } else if (t == 2) {
273 base = GET_MED(0) + GET_MED(1);
274 add = GET_MED(2) - 1;
275 INC_MED(0);
276 INC_MED(1);
277 DEC_MED(2);
278 } else {
279 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2U);
280 add = GET_MED(2) - 1;
281 INC_MED(0);
282 INC_MED(1);
283 INC_MED(2);
284 }
285 if (!c->error_limit) {
286 ret = base + get_tail(gb, add);
287 if (get_bits_left(gb) <= 0)
288 goto error;
289 } else {
290 int mid = (base * 2U + add + 1) >> 1;
291 while (add > c->error_limit) {
292 if (get_bits_left(gb) <= 0)
293 goto error;
294 if (get_bits1(gb)) {
295 add -= (mid - (unsigned)base);
296 base = mid;
297 } else
298 add = mid - (unsigned)base - 1;
299 mid = (base * 2U + add + 1) >> 1;
300 }
301 ret = mid;
302 }
303 sign = get_bits1(gb);
304 if (ctx->hybrid_bitrate)
305 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
306 return sign ? ~ret : ret;
307
308error:
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
317static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
318 unsigned S)
319{
320 unsigned bit;
321
322 if (s->extra_bits) {
323 S *= 1 << s->extra_bits;
324
325 if (s->got_extra_bits &&
326 get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
327 S |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
328 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
329 }
330 }
331
332 bit = (S & s->and) | s->or;
333 bit = ((S + bit) << s->shift) - bit;
334
335 if (s->hybrid)
336 bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
337
338 return bit << s->post_shift;
339}
340
341static 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 int exp = s->float_max_exp;
350
351 if (s->got_extra_bits) {
352 const int max_bits = 1 + 23 + 8 + 1;
353 const int left_bits = get_bits_left(&s->gb_extra_bits);
354
355 if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
356 return 0.0;
357 }
358
359 if (S) {
360 S *= 1U << s->float_shift;
361 sign = S < 0;
362 if (sign)
363 S = -(unsigned)S;
364 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 } else if (exp) {
371 int shift = 23 - av_log2(S);
372 exp = s->float_max_exp;
373 if (exp <= shift)
374 shift = --exp;
375 exp -= shift;
376
377 if (shift) {
378 S <<= shift;
379 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
380 (s->got_extra_bits &&
381 (s->float_flag & WV_FLT_SHIFT_SAME) &&
382 get_bits1(&s->gb_extra_bits))) {
383 S |= (1 << shift) - 1;
384 } else if (s->got_extra_bits &&
385 (s->float_flag & WV_FLT_SHIFT_SENT)) {
386 S |= get_bits(&s->gb_extra_bits, shift);
387 }
388 }
389 } else {
390 exp = s->float_max_exp;
391 }
392 S &= 0x7fffff;
393 } else {
394 sign = 0;
395 exp = 0;
396 if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
397 if (get_bits1(&s->gb_extra_bits)) {
398 S = get_bits(&s->gb_extra_bits, 23);
399 if (s->float_max_exp >= 25)
400 exp = get_bits(&s->gb_extra_bits, 8);
401 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 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
410
411 value.u = (sign << 31) | (exp << 23) | S;
412 return value.f;
413}
414
415static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
416 uint32_t crc_extra_bits)
417{
418 if (crc != s->CRC) {
419 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
420 return AVERROR_INVALIDDATA;
421 }
422 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 return 0;
428}
429
430static void init_ptable(int *table, int rate_i, int rate_s)
431{
432 int value = 0x808000, rate = rate_i << 8;
433
434 for (int c = (rate + 128) >> 8; c--;)
435 value += (DOWN - value) >> DECAY;
436
437 for (int i = 0; i < PTABLE_BINS/2; i++) {
438 table[i] = value;
439 table[PTABLE_BINS-1-i] = 0x100ffff - value;
440
441 if (value > 0x010000) {
442 rate += (rate * rate_s + 128) >> 8;
443
444 for (int c = (rate + 64) >> 7; c--;)
445 value += (DOWN - value) >> DECAY;
446 }
447 }
448}
449
450typedef struct {
452 unsigned int byte;
453} DSDfilters;
454
455static 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
461static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right,
462 ptrdiff_t stride)
463{
464 uint32_t checksum = 0xFFFFFFFF;
465 uint8_t *dst_l = dst_left, *dst_r = dst_right;
466 int total_samples = s->samples, stereo = dst_r ? 1 : 0;
467 DSDfilters filters[2], *sp = filters;
468 int rate_i, rate_s;
469 uint32_t low, high, value;
470
471 if (bytestream2_get_bytes_left(&s->gbyte) < (stereo ? 20 : 13))
472 return AVERROR_INVALIDDATA;
473
474 rate_i = bytestream2_get_byte(&s->gbyte);
475 rate_s = bytestream2_get_byte(&s->gbyte);
476
477 if (rate_s != RATE_S)
478 return AVERROR_INVALIDDATA;
479
480 init_ptable(s->ptable, rate_i, rate_s);
481
482 for (int channel = 0; channel < stereo + 1; channel++) {
483 DSDfilters *sp = filters + channel;
484
485 sp->fltr1 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
486 sp->fltr2 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
487 sp->fltr3 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
488 sp->fltr4 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
489 sp->fltr5 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
490 sp->fltr6 = 0;
491 sp->factor = bytestream2_get_byte(&s->gbyte) & 0xff;
492 sp->factor |= (bytestream2_get_byte(&s->gbyte) << 8) & 0xff00;
493 sp->factor = (int32_t)((uint32_t)sp->factor << 16) >> 16;
494 }
495
496 value = bytestream2_get_be32(&s->gbyte);
497 high = 0xffffffff;
498 low = 0x0;
499
500 while (total_samples--) {
501 int bitcount = 8;
502
503 sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
504
505 if (stereo)
506 sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
507
508 while (bitcount--) {
509 int32_t *pp = s->ptable + ((sp[0].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
510 uint32_t split = low + ((high - low) >> 8) * (*pp >> 16);
511
512 if (value <= split) {
513 high = split;
514 *pp += (UP - *pp) >> DECAY;
515 sp[0].fltr0 = -1;
516 } else {
517 low = split + 1;
518 *pp += (DOWN - *pp) >> DECAY;
519 sp[0].fltr0 = 0;
520 }
521
522 if (DSD_BYTE_READY(high, low) && !bytestream2_get_bytes_left(&s->gbyte))
523 return AVERROR_INVALIDDATA;
524 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
525 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
526 high = (high << 8) | 0xff;
527 low <<= 8;
528 }
529
530 sp[0].value += sp[0].fltr6 * 8;
531 sp[0].byte = (sp[0].byte << 1) | (sp[0].fltr0 & 1);
532 sp[0].factor += (((sp[0].value ^ sp[0].fltr0) >> 31) | 1) &
533 ((sp[0].value ^ (sp[0].value - (sp[0].fltr6 * 16))) >> 31);
534 sp[0].fltr1 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr1) >> 6;
535 sp[0].fltr2 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr2) >> 4;
536 sp[0].fltr3 += (sp[0].fltr2 - sp[0].fltr3) >> 4;
537 sp[0].fltr4 += (sp[0].fltr3 - sp[0].fltr4) >> 4;
538 sp[0].value = (sp[0].fltr4 - sp[0].fltr5) >> 4;
539 sp[0].fltr5 += sp[0].value;
540 sp[0].fltr6 += (sp[0].value - sp[0].fltr6) >> 3;
541 sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
542
543 if (!stereo)
544 continue;
545
546 pp = s->ptable + ((sp[1].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
547 split = low + ((high - low) >> 8) * (*pp >> 16);
548
549 if (value <= split) {
550 high = split;
551 *pp += (UP - *pp) >> DECAY;
552 sp[1].fltr0 = -1;
553 } else {
554 low = split + 1;
555 *pp += (DOWN - *pp) >> DECAY;
556 sp[1].fltr0 = 0;
557 }
558
559 if (DSD_BYTE_READY(high, low) && !bytestream2_get_bytes_left(&s->gbyte))
560 return AVERROR_INVALIDDATA;
561 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
562 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
563 high = (high << 8) | 0xff;
564 low <<= 8;
565 }
566
567 sp[1].value += sp[1].fltr6 * 8;
568 sp[1].byte = (sp[1].byte << 1) | (sp[1].fltr0 & 1);
569 sp[1].factor += (((sp[1].value ^ sp[1].fltr0) >> 31) | 1) &
570 ((sp[1].value ^ (sp[1].value - (sp[1].fltr6 * 16))) >> 31);
571 sp[1].fltr1 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr1) >> 6;
572 sp[1].fltr2 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr2) >> 4;
573 sp[1].fltr3 += (sp[1].fltr2 - sp[1].fltr3) >> 4;
574 sp[1].fltr4 += (sp[1].fltr3 - sp[1].fltr4) >> 4;
575 sp[1].value = (sp[1].fltr4 - sp[1].fltr5) >> 4;
576 sp[1].fltr5 += sp[1].value;
577 sp[1].fltr6 += (sp[1].value - sp[1].fltr6) >> 3;
578 sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
579 }
580
581 checksum += (checksum << 1) + (*dst_l = sp[0].byte & 0xff);
582 sp[0].factor -= (sp[0].factor + 512) >> 10;
583 dst_l += stride;
584
585 if (stereo) {
586 checksum += (checksum << 1) + (*dst_r = filters[1].byte & 0xff);
587 filters[1].factor -= (filters[1].factor + 512) >> 10;
588 dst_r += stride;
589 }
590 }
591
592 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 return 0;
603}
604
605static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right,
606 ptrdiff_t stride)
607{
608 uint8_t *dst_l = dst_left, *dst_r = dst_right;
609 uint8_t history_bits, max_probability;
610 int total_summed_probabilities = 0;
611 int total_samples = s->samples;
612 uint8_t *vlb = s->value_lookup_buffer;
613 int history_bins, p0, p1, chan;
614 uint32_t checksum = 0xFFFFFFFF;
615 uint32_t low, high, value;
616
617 if (!bytestream2_get_bytes_left(&s->gbyte))
618 return AVERROR_INVALIDDATA;
619
620 history_bits = bytestream2_get_byte(&s->gbyte);
621
622 if (!bytestream2_get_bytes_left(&s->gbyte) || history_bits > MAX_HISTORY_BITS)
623 return AVERROR_INVALIDDATA;
624
625 history_bins = 1 << history_bits;
626 max_probability = bytestream2_get_byte(&s->gbyte);
627
628 if (max_probability < 0xff) {
629 uint8_t *outptr = (uint8_t *)s->probabilities;
630 uint8_t *outend = outptr + sizeof(*s->probabilities) * history_bins;
631
632 while (outptr < outend && bytestream2_get_bytes_left(&s->gbyte)) {
633 int code = bytestream2_get_byte(&s->gbyte);
634
635 if (code > max_probability) {
636 int zcount = code - max_probability;
637
638 while (outptr < outend && zcount--)
639 *outptr++ = 0;
640 } else if (code) {
641 *outptr++ = code;
642 }
643 else {
644 break;
645 }
646 }
647
648 if (outptr < outend ||
649 (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 for (p0 = 0; p0 < history_bins; p0++) {
659 int32_t sum_values = 0;
660
661 for (int i = 0; i < 256; i++)
662 s->summed_probabilities[p0][i] = sum_values += s->probabilities[p0][i];
663
664 if (sum_values) {
665 total_summed_probabilities += sum_values;
666
667 if (total_summed_probabilities > history_bins * MAX_BIN_BYTES)
668 return AVERROR_INVALIDDATA;
669
670 s->value_lookup[p0] = vlb;
671
672 for (int i = 0; i < 256; i++) {
673 int c = s->probabilities[p0][i];
674
675 while (c--)
676 *vlb++ = i;
677 }
678 }
679 }
680
681 if (bytestream2_get_bytes_left(&s->gbyte) < 4)
682 return AVERROR_INVALIDDATA;
683
684 chan = p0 = p1 = 0;
685 low = 0; high = 0xffffffff;
686 value = bytestream2_get_be32(&s->gbyte);
687
688 if (dst_r)
689 total_samples *= 2;
690
691 while (total_samples--) {
692 unsigned int mult, index, code;
693
694 if (!s->summed_probabilities[p0][255])
695 return AVERROR_INVALIDDATA;
696
697 mult = (high - low) / s->summed_probabilities[p0][255];
698
699 if (!mult) {
700 if (bytestream2_get_bytes_left(&s->gbyte) >= 4)
701 value = bytestream2_get_be32(&s->gbyte);
702
703 low = 0;
704 high = 0xffffffff;
705 mult = high / s->summed_probabilities[p0][255];
706
707 if (!mult)
708 return AVERROR_INVALIDDATA;
709 }
710
711 index = (value - low) / mult;
712
713 if (index >= s->summed_probabilities[p0][255])
714 return AVERROR_INVALIDDATA;
715
716 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 if ((code = s->value_lookup[p0][index]))
723 low += s->summed_probabilities[p0][code-1] * mult;
724
725 if (chan) {
726 *dst_r = code;
727 dst_r += stride;
728 }
729 else {
730 *dst_l = code;
731 dst_l += stride;
732 }
733
734 chan ^= 1;
735 }
736
737 high = low + s->probabilities[p0][code] * mult - 1;
738 checksum += (checksum << 1) + code;
739
740 if (!dst_r) {
741 p0 = code & (history_bins-1);
742 } else {
743 p0 = p1;
744 p1 = code & (history_bins-1);
745 }
746
747 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
748 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
749 high = (high << 8) | 0xff;
750 low <<= 8;
751 }
752 }
753
754 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 return 0;
765}
766
767static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right,
768 ptrdiff_t stride)
769{
770 uint8_t *dst_l = dst_left, *dst_r = dst_right;
771 int total_samples = s->samples;
772 uint32_t checksum = 0xFFFFFFFF;
773
774 if (bytestream2_get_bytes_left(&s->gbyte) != total_samples * (dst_r ? 2 : 1))
775 return AVERROR_INVALIDDATA;
776
777 while (total_samples--) {
778 checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&s->gbyte));
779 dst_l += stride;
780
781 if (dst_r) {
782 checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&s->gbyte));
783 dst_r += stride;
784 }
785 }
786
787 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 return 0;
798}
799
801 void *dst_l, void *dst_r, const int type)
802{
803 int i, j, count = 0;
804 int last, t;
805 int A, B, L, L2, R, R2;
806 int pos = 0;
807 uint32_t crc = 0xFFFFFFFF;
808 uint32_t crc_extra_bits = 0xFFFFFFFF;
809 int16_t *dst16_l = dst_l;
810 int16_t *dst16_r = dst_r;
811 int32_t *dst32_l = dst_l;
812 int32_t *dst32_r = dst_r;
813 float *dstfl_l = dst_l;
814 float *dstfl_r = dst_r;
815
816 s->one = s->zero = s->zeroes = 0;
817 do {
818 L = wv_get_value(s, gb, 0, &last);
819 if (last)
820 break;
821 R = wv_get_value(s, gb, 1, &last);
822 if (last)
823 break;
824 for (i = 0; i < s->terms; i++) {
825 Decorr *decorr = &s->decorr[i];
826
827 t = decorr->value;
828 if (t > 0) {
829 if (t > 8) {
830 if (t & 1) {
831 A = 2U * decorr->samplesA[0] - decorr->samplesA[1];
832 B = 2U * decorr->samplesB[0] - decorr->samplesB[1];
833 } else {
834 A = (int)(3U * decorr->samplesA[0] - decorr->samplesA[1]) >> 1;
835 B = (int)(3U * decorr->samplesB[0] - decorr->samplesB[1]) >> 1;
836 }
837 decorr->samplesA[1] = decorr->samplesA[0];
838 decorr->samplesB[1] = decorr->samplesB[0];
839 j = 0;
840 } else {
841 A = decorr->samplesA[pos];
842 B = decorr->samplesB[pos];
843 j = (pos + t) & 7;
844 }
845 if (type != AV_SAMPLE_FMT_S16P) {
846 L2 = L + ((decorr->weightA * (int64_t)A + 512) >> 10);
847 R2 = R + ((decorr->weightB * (int64_t)B + 512) >> 10);
848 } else {
849 L2 = L + (unsigned)((int)(decorr->weightA * (unsigned)A + 512) >> 10);
850 R2 = R + (unsigned)((int)(decorr->weightB * (unsigned)B + 512) >> 10);
851 }
852 if (A && L)
853 decorr->weightA -= ((((L ^ A) >> 30) & 2) - 1) * decorr->delta;
854 if (B && R)
855 decorr->weightB -= ((((R ^ B) >> 30) & 2) - 1) * decorr->delta;
856 decorr->samplesA[j] = L = L2;
857 decorr->samplesB[j] = R = R2;
858 } else if (t == -1) {
860 L2 = L + ((decorr->weightA * (int64_t)decorr->samplesA[0] + 512) >> 10);
861 else
862 L2 = L + (unsigned)((int)(decorr->weightA * (unsigned)decorr->samplesA[0] + 512) >> 10);
863 UPDATE_WEIGHT_CLIP(decorr->weightA, decorr->delta, decorr->samplesA[0], L);
864 L = L2;
866 R2 = R + ((decorr->weightB * (int64_t)L2 + 512) >> 10);
867 else
868 R2 = R + (unsigned)((int)(decorr->weightB * (unsigned)L2 + 512) >> 10);
869 UPDATE_WEIGHT_CLIP(decorr->weightB, decorr->delta, L2, R);
870 R = R2;
871 decorr->samplesA[0] = R;
872 } else {
874 R2 = R + ((decorr->weightB * (int64_t)decorr->samplesB[0] + 512) >> 10);
875 else
876 R2 = R + (unsigned)((int)(decorr->weightB * (unsigned)decorr->samplesB[0] + 512) >> 10);
877 UPDATE_WEIGHT_CLIP(decorr->weightB, decorr->delta, decorr->samplesB[0], R);
878 R = R2;
879
880 if (t == -3) {
881 R2 = decorr->samplesA[0];
882 decorr->samplesA[0] = R;
883 }
884
886 L2 = L + ((decorr->weightA * (int64_t)R2 + 512) >> 10);
887 else
888 L2 = L + (unsigned)((int)(decorr->weightA * (unsigned)R2 + 512) >> 10);
889 UPDATE_WEIGHT_CLIP(decorr->weightA, decorr->delta, R2, L);
890 L = L2;
891 decorr->samplesB[0] = L;
892 }
893 }
894
895 if (type == AV_SAMPLE_FMT_S16P) {
896 if (FFABS((int64_t)L) + FFABS((int64_t)R) > (1<<19)) {
897 av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
898 return AVERROR_INVALIDDATA;
899 }
900 }
901
902 pos = (pos + 1) & 7;
903 if (s->joint)
904 L += (unsigned)(R -= (unsigned)(L >> 1));
905 crc = (crc * 3 + L) * 3 + R;
906
907 if (type == AV_SAMPLE_FMT_FLTP) {
908 *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
909 *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
910 } else if (type == AV_SAMPLE_FMT_S32P) {
911 *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
912 *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
913 } else {
914 *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
915 *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
916 }
917 count++;
918 } while (!last && count < s->samples);
919
920 if (last && count < s->samples) {
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 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
927 wv_check_crc(s, crc, crc_extra_bits))
928 return AVERROR_INVALIDDATA;
929
930 return 0;
931}
932
934 void *dst, const int type)
935{
936 int i, j, count = 0;
937 int last, t;
938 int A, S, T;
939 int pos = 0;
940 uint32_t crc = 0xFFFFFFFF;
941 uint32_t crc_extra_bits = 0xFFFFFFFF;
942 int16_t *dst16 = dst;
943 int32_t *dst32 = dst;
944 float *dstfl = dst;
945
946 s->one = s->zero = s->zeroes = 0;
947 do {
948 T = wv_get_value(s, gb, 0, &last);
949 S = 0;
950 if (last)
951 break;
952 for (i = 0; i < s->terms; i++) {
953 Decorr *decorr = &s->decorr[i];
954
955 t = decorr->value;
956 if (t > 8) {
957 if (t & 1)
958 A = 2U * decorr->samplesA[0] - decorr->samplesA[1];
959 else
960 A = (int)(3U * decorr->samplesA[0] - decorr->samplesA[1]) >> 1;
961 decorr->samplesA[1] = decorr->samplesA[0];
962 j = 0;
963 } else {
964 A = decorr->samplesA[pos];
965 j = (pos + t) & 7;
966 }
968 S = T + ((decorr->weightA * (int64_t)A + 512) >> 10);
969 else
970 S = T + (unsigned)((int)(decorr->weightA * (unsigned)A + 512) >> 10);
971 if (A && T)
972 decorr->weightA -= ((((T ^ A) >> 30) & 2) - 1) * decorr->delta;
973 decorr->samplesA[j] = T = S;
974 }
975 pos = (pos + 1) & 7;
976 crc = crc * 3 + S;
977
978 if (type == AV_SAMPLE_FMT_FLTP) {
979 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
980 } else if (type == AV_SAMPLE_FMT_S32P) {
981 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
982 } else {
983 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
984 }
985 count++;
986 } while (!last && count < s->samples);
987
988 if (last && count < s->samples) {
990 memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
991 }
992
993 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 return 0;
1000}
1001
1003{
1004 WavpackFrameContext **fdec = av_realloc_array(c->fdec, c->fdec_num + 1, sizeof(*c->fdec));
1005
1006 if (!fdec)
1007 return -1;
1008 c->fdec = fdec;
1009
1010 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
1011 if (!c->fdec[c->fdec_num])
1012 return -1;
1013 c->fdec_num++;
1014 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
1015
1016 return 0;
1017}
1018
1019#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1020static void wv_dsd_swr_free(AVRefStructOpaque opaque, void *obj)
1021{
1022 WvDSDSwr *h = obj;
1023
1024 swr_free(&h->swr);
1025}
1026#endif
1027
1028static int wv_dsd_reset(AVCodecContext *avctx, int channels)
1029{
1030 WavpackContext *s = avctx->priv_data;
1031
1032 s->dsd_channels = 0;
1033#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1034 av_refstruct_unref(&s->dsd_swr);
1035#endif
1036 av_refstruct_unref(&s->curr_progress);
1037 av_refstruct_unref(&s->prev_progress);
1038
1039 if (!channels)
1040 return 0;
1041
1042#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1043 {
1044 s->dsd_swr = av_refstruct_alloc_ext(sizeof(*s->dsd_swr), 0, NULL,
1045 wv_dsd_swr_free);
1046 if (!s->dsd_swr)
1047 return AVERROR(ENOMEM);
1048
1049 int ret = ff_dsd_to_pcm_init(avctx, &s->dsd_swr->swr);
1050 if (ret < 0) {
1051 av_refstruct_unref(&s->dsd_swr);
1052 return ret;
1053 }
1054 s->dsd_channels = channels;
1055 }
1056 return 0;
1057#else
1058 return AVERROR_BUG;
1059#endif
1060}
1061
1062#if HAVE_THREADS
1063static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1064{
1065 WavpackContext *fsrc = src->priv_data;
1066 WavpackContext *fdst = dst->priv_data;
1067
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
1077static 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
1083static void progress_pool_reset_cb(AVRefStructOpaque opaque, void *obj)
1084{
1085 ThreadProgress *progress = obj;
1086 ff_thread_progress_reset(progress);
1087}
1088
1089static av_cold void progress_pool_free_entry_cb(AVRefStructOpaque opaque, void *obj)
1090{
1091 ThreadProgress *progress = obj;
1093}
1094#endif
1095
1097{
1098 WavpackContext *s = avctx->priv_data;
1099
1100 s->avctx = avctx;
1101
1102 s->fdec_num = 0;
1103
1104 s->dsd_raw = 1;
1105#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1106 s->dsd_raw = avctx->request_sample_fmt == AV_SAMPLE_FMT_DSD;
1107#endif
1108
1109#if HAVE_THREADS
1110 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),
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 return 0;
1122}
1123
1125{
1126 WavpackContext *s = avctx->priv_data;
1127
1128 for (int i = 0; i < s->fdec_num; i++)
1129 av_freep(&s->fdec[i]);
1130 av_freep(&s->fdec);
1131 s->fdec_num = 0;
1132
1133 av_refstruct_pool_uninit(&s->progress_pool);
1134 wv_dsd_reset(avctx, 0);
1135#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1136 av_freep(&s->dsd_scratch);
1137#endif
1138
1139 return 0;
1140}
1141
1142static int wavpack_decode_block(AVCodecContext *avctx, AVFrame *frame, int block_no,
1143 const uint8_t *buf, int buf_size, int *new_progress)
1144{
1145 WavpackContext *wc = avctx->priv_data;
1147 GetByteContext gb;
1148 enum AVSampleFormat sample_fmt;
1149 void *samples_l = NULL, *samples_r = NULL;
1150 ptrdiff_t stride = 0;
1151 int ret;
1152 int got_terms = 0, got_weights = 0, got_samples = 0,
1153 got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1154 int got_dsd = 0;
1155 int i, j, id, size, ssize, weights, t;
1156 int bpp, chan = 0, orig_bpp, sample_rate = 0, rate_x = 1, dsd_mode = 0;
1157 int multiblock;
1158 uint64_t chmask = 0;
1159
1160 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 s = wc->fdec[block_no];
1166
1167 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
1168 memset(s->ch, 0, sizeof(s->ch));
1169 s->extra_bits = 0;
1170 s->and = s->or = s->shift = 0;
1171 s->got_extra_bits = 0;
1172
1173 bytestream2_init(&gb, buf, buf_size);
1174
1175 s->samples = bytestream2_get_le32(&gb);
1176 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 s->frame_flags = bytestream2_get_le32(&gb);
1182
1183 if (s->frame_flags & WV_DSD_DATA)
1184 sample_fmt = wc->dsd_raw ? AV_SAMPLE_FMT_DSD : AV_SAMPLE_FMT_FLTP;
1185 else if (s->frame_flags & WV_FLOAT_DATA)
1186 sample_fmt = AV_SAMPLE_FMT_FLTP;
1187 else if ((s->frame_flags & 0x03) <= 1)
1188 sample_fmt = AV_SAMPLE_FMT_S16P;
1189 else
1190 sample_fmt = AV_SAMPLE_FMT_S32P;
1191
1192 if (wc->ch_offset && avctx->sample_fmt != sample_fmt)
1193 return AVERROR_INVALIDDATA;
1194
1195 bpp = av_get_bytes_per_sample(sample_fmt);
1196 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
1197 multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
1198
1199 s->stereo = !(s->frame_flags & WV_MONO);
1200 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
1201 s->joint = s->frame_flags & WV_JOINT_STEREO;
1202 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
1203 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
1204 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
1205 if (s->post_shift < 0 || s->post_shift > 31) {
1206 return AVERROR_INVALIDDATA;
1207 }
1208 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
1209 s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
1210 s->CRC = bytestream2_get_le32(&gb);
1211
1212 // parse metadata blocks
1213 while (bytestream2_get_bytes_left(&gb)) {
1214 id = bytestream2_get_byte(&gb);
1215 size = bytestream2_get_byte(&gb);
1216 if (id & WP_IDF_LONG)
1217 size |= (bytestream2_get_le16u(&gb)) << 8;
1218 size <<= 1; // size is specified in words
1219 ssize = size;
1220 if (id & WP_IDF_ODD)
1221 size--;
1222 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 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 switch (id & WP_IDF_MASK) {
1233 case WP_ID_DECTERMS:
1234 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 s->terms = size;
1241 for (i = 0; i < s->terms; i++) {
1242 uint8_t val = bytestream2_get_byte(&gb);
1243 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
1244 s->decorr[s->terms - i - 1].delta = val >> 5;
1245 }
1246 got_terms = 1;
1247 break;
1248 case WP_ID_DECWEIGHTS:
1249 if (!got_terms) {
1250 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1251 continue;
1252 }
1253 weights = size >> s->stereo_in;
1254 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 for (i = 0; i < weights; i++) {
1260 t = (int8_t)bytestream2_get_byte(&gb);
1261 s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
1262 if (s->decorr[s->terms - i - 1].weightA > 0)
1263 s->decorr[s->terms - i - 1].weightA +=
1264 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
1265 if (s->stereo_in) {
1266 t = (int8_t)bytestream2_get_byte(&gb);
1267 s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
1268 if (s->decorr[s->terms - i - 1].weightB > 0)
1269 s->decorr[s->terms - i - 1].weightB +=
1270 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
1271 }
1272 }
1273 got_weights = 1;
1274 break;
1275 case WP_ID_DECSAMPLES:
1276 if (!got_terms) {
1277 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1278 continue;
1279 }
1280 t = 0;
1281 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
1282 Decorr *decorr = &s->decorr[i];
1283
1284 if (decorr->value > 8) {
1285 decorr->samplesA[0] =
1286 wp_exp2(bytestream2_get_le16(&gb));
1287 decorr->samplesA[1] =
1288 wp_exp2(bytestream2_get_le16(&gb));
1289
1290 if (s->stereo_in) {
1291 decorr->samplesB[0] =
1292 wp_exp2(bytestream2_get_le16(&gb));
1293 decorr->samplesB[1] =
1294 wp_exp2(bytestream2_get_le16(&gb));
1295 t += 4;
1296 }
1297 t += 4;
1298 } 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 for (j = 0; j < decorr->value; j++) {
1306 decorr->samplesA[j] =
1307 wp_exp2(bytestream2_get_le16(&gb));
1308 if (s->stereo_in) {
1309 decorr->samplesB[j] =
1310 wp_exp2(bytestream2_get_le16(&gb));
1311 }
1312 }
1313 t += decorr->value * 2 * (s->stereo_in + 1);
1314 }
1315 }
1316 got_samples = 1;
1317 break;
1318 case WP_ID_ENTROPY:
1319 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 for (j = 0; j <= s->stereo_in; j++)
1327 for (i = 0; i < 3; i++) {
1328 s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
1329 }
1330 got_entropy = 1;
1331 break;
1332 case WP_ID_HYBRID:
1333 if (s->hybrid_bitrate) {
1334 for (i = 0; i <= s->stereo_in; i++) {
1335 s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
1336 size -= 2;
1337 }
1338 }
1339 for (i = 0; i < (s->stereo_in + 1); i++) {
1340 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
1341 size -= 2;
1342 }
1343 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 for (i = 0; i < (s->stereo_in + 1); i++)
1350 s->ch[i].bitrate_delta = 0;
1351 }
1352 got_hybrid = 1;
1353 break;
1354 case WP_ID_INT32INFO: {
1355 uint8_t val[4];
1356 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 bytestream2_get_buffer(&gb, val, 4);
1364 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 s->extra_bits = val[0];
1370 }
1371 if (val[1])
1372 s->shift = val[1];
1373 if (val[2]) {
1374 s->and = s->or = 1;
1375 s->shift = val[2];
1376 }
1377 if (val[3]) {
1378 s->and = 1;
1379 s->shift = val[3];
1380 }
1381 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 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 break;
1396 }
1397 case WP_ID_FLOATINFO:
1398 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 s->float_flag = bytestream2_get_byte(&gb);
1405 s->float_shift = bytestream2_get_byte(&gb);
1406 s->float_max_exp = bytestream2_get_byte(&gb);
1407 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 got_float = 1;
1414 bytestream2_skip(&gb, 1);
1415 break;
1416 case WP_ID_DATA:
1417 if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
1418 return ret;
1419 bytestream2_skip(&gb, size);
1420 got_pcm = 1;
1421 break;
1422 case WP_ID_DSD_DATA:
1423 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 rate_x = bytestream2_get_byte(&gb);
1430 if (rate_x > 30)
1431 return AVERROR_INVALIDDATA;
1432 rate_x = 1 << rate_x;
1433 dsd_mode = bytestream2_get_byte(&gb);
1434 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 bytestream2_init(&s->gbyte, gb.buffer, size-2);
1440 bytestream2_skip(&gb, size-2);
1441 got_dsd = 1;
1442 break;
1443 case WP_ID_EXTRABITS:
1444 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 if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
1451 return ret;
1452 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
1453 bytestream2_skip(&gb, size);
1454 s->got_extra_bits = 1;
1455 break;
1456 case WP_ID_CHANINFO:
1457 if (size <= 1) {
1458 av_log(avctx, AV_LOG_ERROR,
1459 "Insufficient channel information\n");
1460 return AVERROR_INVALIDDATA;
1461 }
1462 chan = bytestream2_get_byte(&gb);
1463 switch (size - 2) {
1464 case 0:
1465 chmask = bytestream2_get_byte(&gb);
1466 break;
1467 case 1:
1468 chmask = bytestream2_get_le16(&gb);
1469 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 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 default:
1502 bytestream2_skip(&gb, size);
1503 }
1504 if (id & WP_IDF_ODD)
1505 bytestream2_skip(&gb, 1);
1506 }
1507
1508 if (got_pcm) {
1509 if (!got_terms) {
1510 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1511 return AVERROR_INVALIDDATA;
1512 }
1513 if (!got_weights) {
1514 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1515 return AVERROR_INVALIDDATA;
1516 }
1517 if (!got_samples) {
1518 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1519 return AVERROR_INVALIDDATA;
1520 }
1521 if (!got_entropy) {
1522 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1523 return AVERROR_INVALIDDATA;
1524 }
1525 if (s->hybrid && !got_hybrid) {
1526 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1527 return AVERROR_INVALIDDATA;
1528 }
1529 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 if (s->got_extra_bits && sample_fmt != AV_SAMPLE_FMT_FLTP) {
1534 const int size = get_bits_left(&s->gb_extra_bits);
1535 const int wanted = s->samples * s->extra_bits << s->stereo_in;
1536 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 if (!got_pcm && !got_dsd) {
1544 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1545 return AVERROR_INVALIDDATA;
1546 }
1547
1548 if ((got_pcm && wc->modulation != MODULATION_PCM) ||
1549 (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 if (!wc->ch_offset) {
1555 AVChannelLayout new_ch_layout = { 0 };
1556 int new_samplerate;
1557 int sr = (s->frame_flags >> 23) & 0xf;
1558 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 new_samplerate = wv_rates[sr];
1566
1567 if (new_samplerate * (uint64_t)rate_x > INT_MAX)
1568 return AVERROR_INVALIDDATA;
1569 new_samplerate *= rate_x;
1570
1571 if (multiblock) {
1572 if (chmask) {
1573 av_channel_layout_from_mask(&new_ch_layout, chmask);
1574 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 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 int reset_dsd = !wc->dsd_raw &&
1589 ((wc->dsd_swr && !got_dsd) ||
1590 got_dsd && (new_ch_layout.nb_channels != wc->dsd_channels ||
1591 av_channel_layout_compare(&new_ch_layout, &avctx->ch_layout) ||
1592 new_samplerate != avctx->sample_rate));
1593#endif
1594 av_channel_layout_copy(&avctx->ch_layout, &new_ch_layout);
1595 avctx->sample_rate = new_samplerate;
1596 avctx->sample_fmt = sample_fmt;
1597 avctx->bits_per_raw_sample = orig_bpp;
1598
1599#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1600 if (reset_dsd) {
1601 ret = wv_dsd_reset(avctx, got_dsd ? new_ch_layout.nb_channels : 0);
1602 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 frame->nb_samples = s->samples;
1611 ret = ff_thread_get_buffer(avctx, frame, 0);
1612 if (ret < 0)
1613 return ret;
1614
1616 if (wc->progress_pool) {
1617 if (WV_DSD_SWR(wc)) {
1620 if (!wc->prev_progress)
1621 return AVERROR(ENOMEM);
1623 *new_progress = 1;
1624 }
1625 av_assert1(!!WV_DSD_SWR(wc) == !!wc->curr_progress);
1627 }
1628 }
1629
1630 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 if (got_dsd) {
1636 // DSD output is interleaved
1637 stride = avctx->ch_layout.nb_channels;
1638#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
1639 if (wc->dsd_swr) {
1640 av_fast_malloc(&wc->dsd_scratch, &wc->dsd_scratch_size,
1641 (size_t)s->samples * stride);
1642 if (!wc->dsd_scratch)
1643 return AVERROR(ENOMEM);
1644 samples_l = wc->dsd_scratch + wc->ch_offset;
1645 } else
1646#endif
1647 samples_l = frame->data[0] + wc->ch_offset;
1648 if (s->stereo)
1649 samples_r = (uint8_t *)samples_l + 1;
1650 } else {
1651 samples_l = frame->extended_data[wc->ch_offset];
1652 if (s->stereo)
1653 samples_r = frame->extended_data[wc->ch_offset + 1];
1654 }
1655
1656 wc->ch_offset += 1 + s->stereo;
1657
1658 if (s->stereo_in) {
1659 if (got_dsd) {
1660 if (dsd_mode == 3) {
1661 ret = wv_unpack_dsd_high(s, samples_l, samples_r, stride);
1662 } else if (dsd_mode == 1) {
1663 ret = wv_unpack_dsd_fast(s, samples_l, samples_r, stride);
1664 } else {
1665 ret = wv_unpack_dsd_copy(s, samples_l, samples_r, stride);
1666 }
1667 } else {
1668 ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1669 }
1670 if (ret < 0)
1671 return ret;
1672 } else {
1673 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 ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1683 }
1684 if (ret < 0)
1685 return ret;
1686
1687 if (s->stereo) {
1688 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 memcpy(samples_r, samples_l, bpp * s->samples);
1694 }
1695 }
1696
1697 return 0;
1698}
1699
1701{
1702 wv_dsd_reset(avctx, 0);
1703}
1704
1706 int *got_frame_ptr, AVPacket *avpkt)
1707{
1708 WavpackContext *s = avctx->priv_data;
1709 const uint8_t *buf = avpkt->data;
1710 int buf_size = avpkt->size;
1711 int frame_size, ret, frame_flags;
1712 int block = 0, new_progress = 0;
1713
1714 av_assert1(!s->curr_progress || WV_DSD_SWR(s));
1715
1716 if (avpkt->size <= WV_HEADER_SIZE)
1717 return AVERROR_INVALIDDATA;
1718
1719 s->ch_offset = 0;
1720
1721 /* determine number of samples */
1722 s->samples = AV_RL32(buf + 20);
1723 frame_flags = AV_RL32(buf + 24);
1724 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 s->modulation = (frame_flags & WV_DSD_DATA) ? MODULATION_DSD : MODULATION_PCM;
1731
1732 while (buf_size > WV_HEADER_SIZE) {
1733 frame_size = AV_RL32(buf + 4) - 12;
1734 buf += 20;
1735 buf_size -= 20;
1736 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 ret = wavpack_decode_block(avctx, frame, block, buf,
1744 frame_size, &new_progress);
1745 if (ret < 0)
1746 goto error;
1747 block++;
1748 buf += frame_size;
1749 buf_size -= frame_size;
1750 }
1751
1752 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 if (s->dsd_swr) {
1760 if (s->prev_progress)
1761 ff_thread_progress_await(s->prev_progress, INT_MAX);
1762 ret = swr_convert(s->dsd_swr->swr, frame->extended_data, s->samples,
1763 (const uint8_t *const []){ s->dsd_scratch },
1764 s->samples);
1765 if (s->curr_progress)
1766 ff_thread_progress_report(s->curr_progress, INT_MAX);
1767 if (ret != s->samples)
1768 return ret < 0 ? ret : AVERROR_BUG;
1769 }
1770#endif
1771
1772 *got_frame_ptr = 1;
1773
1774 return avpkt->size;
1775
1776error:
1777 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 return ret;
1784}
1785
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),
1795 .flush = wavpack_decode_flush,
1796 UPDATE_THREAD_CONTEXT(update_thread_context),
1797 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1799 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1800};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
static char * split(char *message, char delim)
#define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed)
const FFCodec ff_wavpack_decoder
Definition wavpack.c:1786
static AVFormatContext * ctx
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define T(x)
Definition vpx_arith.h:29
#define A(x)
Definition vpx_arith.h:28
#define L(x)
Definition vpx_arith.h:36
int32_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1595
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define UP
Definition cdgraphics.c:166
#define DOWN
Definition cdgraphics.c:167
Public libavutil channel layout APIs header.
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static int16_t block[64]
Definition dct.c:125
enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
Allows to synchronize objects whose lifetime is the whole decoding process among all frame threads.
Definition decode.c:1990
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition defs.h:48
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
static AVFrame * frame
int high
Definition dovi_rpuenc.c:39
int ff_dsd_to_pcm_init(struct AVCodecContext *avctx, struct SwrContext **swrp)
(Re)create a libswresample context converting AV_SAMPLE_FMT_DSD to avctx->sample_fmt at the same samp...
enum AVCodecID id
Definition dts2pts.c:607
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
double value
Definition eval.c:102
int8_t exp
Definition eval.c:76
#define S(s, c, i)
static const uint8_t frame_size[4]
Definition g723_1.h:222
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_WAVPACK
Definition codec_id.h:478
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:555
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:217
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:109
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_DSD
DSD (Direct Stream Digital) bitstream, interleaved.
Definition samplefmt.h:77
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition swresample.c:137
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
Definition swresample.c:743
int index
Definition gxfenc.c:90
static const int weights[]
Definition hevc_pel.c:32
#define B
Definition huffyuv.h:42
#define R
Definition huffyuv.h:44
cl_device_type type
#define av_log2
Definition intmath.h:84
#define AV_RL32(p)
static int shift(int a, int b)
Definition bonk.c:261
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
static int16_t mult(Float11 *f1, Float11 *f2)
Definition g726.c:60
Multithreading API for decoders.
@ FF_THREAD_IS_FIRST_THREAD
Definition thread.h:62
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
#define FFSWAP(type, a, b)
Definition macros.h:52
Memory handling functions.
static const uint16_t table[]
Definition prosumer.c:203
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:160
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
Definition refstruct.c:297
#define AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR
If this flag is set and both init_cb and free_entry_cb callbacks are provided, then free_cb will be c...
Definition refstruct.h:213
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition refstruct.h:292
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:94
static AVRefStructPool * av_refstruct_pool_alloc_ext(size_t size, unsigned flags, void *opaque, int(*init_cb)(AVRefStructOpaque opaque, void *obj), void(*reset_cb)(AVRefStructOpaque opaque, void *obj), void(*free_entry_cb)(AVRefStructOpaque opaque, void *obj), void(*free_cb)(AVRefStructOpaque opaque))
A wrapper around av_refstruct_pool_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:258
#define R2
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int active_thread_type
Which multithreading methods are in use by the codec.
Definition avcodec.h:1603
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1576
enum AVSampleFormat request_sample_fmt
desired sample format
Definition avcodec.h:1097
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1417
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.
Definition refstruct.c:183
int32_t value
Definition wavpack.c:451
int32_t factor
Definition wavpack.c:451
int32_t fltr3
Definition wavpack.c:451
int32_t fltr1
Definition wavpack.c:451
int32_t fltr0
Definition wavpack.c:451
unsigned int byte
Definition wavpack.c:452
int32_t fltr2
Definition wavpack.c:451
int32_t fltr6
Definition wavpack.c:451
int32_t fltr5
Definition wavpack.c:451
int32_t fltr4
Definition wavpack.c:451
int samplesB[MAX_TERM]
Definition wavpack.h:95
int value
Definition wavpack.h:91
int samplesA[MAX_TERM]
Definition wavpack.h:94
int delta
Definition wavpack.h:90
int weightA
Definition wavpack.h:92
int weightB
Definition wavpack.h:93
const uint8_t * buffer
Definition bytestream.h:34
The libswresample context.
ThreadProgress is an API to easily notify other threads about progress of any kind as long as it can ...
AVCodecContext * avctx
Definition wavpack.c:107
ThreadProgress * prev_progress
RefStruct references.
Definition wavpack.c:123
WavpackFrameContext ** fdec
Definition wavpack.c:109
AVRefStructPool * progress_pool
RefStruct reference.
Definition wavpack.c:124
Modulation modulation
Definition wavpack.c:115
int dsd_raw
output the raw DSD bitstream instead of PCM
Definition wavpack.c:116
ThreadProgress * curr_progress
Definition wavpack.c:123
uint16_t summed_probabilities[MAX_HISTORY_BINS][256]
Definition wavpack.c:101
GetBitContext gb
Definition wavpack.c:80
AVCodecContext * avctx
Definition wavpack.c:75
uint8_t value_lookup_buffer[MAX_HISTORY_BINS *MAX_BIN_BYTES]
Definition wavpack.c:100
Decorr decorr[MAX_TERMS]
Definition wavpack.c:86
int ptable[PTABLE_BINS]
Definition wavpack.c:99
GetBitContext gb_extra_bits
Definition wavpack.c:83
uint8_t probabilities[MAX_HISTORY_BINS][256]
Definition wavpack.c:102
GetByteContext gbyte
Definition wavpack.c:98
WvChannel ch[2]
Definition wavpack.c:96
uint32_t frame_flags
Definition wavpack.c:76
uint32_t crc_extra_bits
Definition wavpack.c:82
uint8_t * value_lookup[MAX_HISTORY_BINS]
Definition wavpack.c:103
#define stride
libswresample public header
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
#define src
Definition vp8dsp.c:248
av_cold void ff_thread_progress_destroy(ThreadProgress *pro)
Destroy a ThreadProgress.
av_cold int ff_thread_progress_init(ThreadProgress *pro, int init_mode)
Initialize a ThreadProgress.
void ff_thread_progress_report(ThreadProgress *pro, int n)
This function is a no-op in no-op mode; otherwise it notifies other threads that a certain level of p...
void ff_thread_progress_await(const ThreadProgress *pro_c, int n)
This function is a no-op in no-op mode; otherwise it waits until other threads have reached a certain...
static void ff_thread_progress_reset(ThreadProgress *pro)
Reset the ThreadProgress.progress counter; must only be called if the ThreadProgress is not in use in...
int size
static int get_unary_0_33(GetBitContext *gb)
Get unary code terminated by a 0 with a maximum length of 33.
Definition unary.h:59
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58
uint8_t base
Definition vp3data.h:128
static double c[64]
static int wv_dsd_reset(AVCodecContext *avctx, int channels)
Definition wavpack.c:1028
static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
Definition wavpack.c:341
static av_cold int wavpack_decode_end(AVCodecContext *avctx)
Definition wavpack.c:1124
Modulation
Definition wavpack.c:69
@ MODULATION_PCM
Definition wavpack.c:70
@ MODULATION_DSD
Definition wavpack.c:71
static int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
Definition wavpack.c:933
static av_always_inline unsigned get_tail(GetBitContext *gb, unsigned k)
Definition wavpack.c:139
#define MAX_HISTORY_BITS
Definition wavpack.c:65
#define PRECISION_USE
Definition wavpack.c:61
static int update_error_limit(WavpackFrameContext *ctx)
Definition wavpack.c:153
#define RATE_S
Definition wavpack.c:63
static int wavpack_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition wavpack.c:1705
static int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb, void *dst_l, void *dst_r, const int type)
Definition wavpack.c:800
static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb, int channel, int *last)
Definition wavpack.c:191
static int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc, unsigned S)
Definition wavpack.c:317
static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right, ptrdiff_t stride)
Definition wavpack.c:605
#define DSD_BYTE_READY(low, high)
Definition wavpack.c:49
#define MAX_BIN_BYTES
Definition wavpack.c:67
static av_cold int wv_alloc_frame_context(WavpackContext *c)
Definition wavpack.c:1002
#define LEVEL_DECAY(a)
Definition wavpack.c:137
#define PRECISION
Definition wavpack.c:59
#define DECAY
Definition wavpack.c:57
static void wv_dsd_silence(uint8_t *dst, int samples, ptrdiff_t stride)
Definition wavpack.c:455
#define MAX_HISTORY_BINS
Definition wavpack.c:66
static void init_ptable(int *table, int rate_i, int rate_s)
Definition wavpack.c:430
static int wavpack_decode_block(AVCodecContext *avctx, AVFrame *frame, int block_no, const uint8_t *buf, int buf_size, int *new_progress)
Definition wavpack.c:1142
static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right, ptrdiff_t stride)
Definition wavpack.c:767
static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right, ptrdiff_t stride)
Definition wavpack.c:461
static av_cold void wavpack_decode_flush(AVCodecContext *avctx)
Definition wavpack.c:1700
#define VALUE_ONE
Definition wavpack.c:60
#define PTABLE_MASK
Definition wavpack.c:53
#define PTABLE_BINS
Definition wavpack.c:52
static av_cold int wavpack_decode_init(AVCodecContext *avctx)
Definition wavpack.c:1096
#define WV_DSD_SWR(wc)
Definition wavpack.c:134
static int wv_check_crc(WavpackFrameContext *s, uint32_t crc, uint32_t crc_extra_bits)
Definition wavpack.c:415
#define WV_FLT_SHIFT_SENT
Definition wavpack.h:56
static av_always_inline int wp_exp2(int16_t val)
Definition wavpack.h:134
#define MAX_TERMS
Definition wavpack.h:30
#define WV_FLT_ZERO_SENT
Definition wavpack.h:57
#define WV_FLT_SHIFT_SAME
Definition wavpack.h:55
#define WV_HYBRID_BITRATE
Definition wavpack.h:45
#define WV_HYBRID_MODE
Definition wavpack.h:43
#define WV_DSD_DATA
Definition wavpack.h:41
#define WV_MONO
Definition wavpack.h:35
@ WP_ID_DECTERMS
Definition wavpack.h:73
@ WP_ID_DECSAMPLES
Definition wavpack.h:75
@ WP_ID_EXTRABITS
Definition wavpack.h:83
@ WP_ID_INT32INFO
Definition wavpack.h:80
@ WP_ID_HYBRID
Definition wavpack.h:77
@ WP_ID_SAMPLE_RATE
Definition wavpack.h:86
@ WP_ID_ENTROPY
Definition wavpack.h:76
@ WP_ID_DATA
Definition wavpack.h:81
@ WP_ID_CHANINFO
Definition wavpack.h:84
@ WP_ID_DECWEIGHTS
Definition wavpack.h:74
@ WP_ID_FLOATINFO
Definition wavpack.h:79
@ WP_ID_DSD_DATA
Definition wavpack.h:85
#define INC_MED(n)
Definition wavpack.h:109
#define WV_FLT_ZERO_SIGN
Definition wavpack.h:58
#define WV_FLT_SHIFT_ONES
Definition wavpack.h:54
@ WP_IDF_ODD
Definition wavpack.h:66
@ WP_IDF_LONG
Definition wavpack.h:67
@ WP_IDF_MASK
Definition wavpack.h:64
static const int wv_rates[16]
Definition wavpack.h:125
#define WV_MAX_CHANNELS
Definition wavpack.h:60
#define WV_FLOAT_DATA
Definition wavpack.h:38
#define DEC_MED(n)
Definition wavpack.h:108
#define WV_MAX_SAMPLES
Definition wavpack.h:61
#define WV_HEADER_SIZE
Definition wavpack.h:33
#define GET_MED(n)
Definition wavpack.h:107
#define WV_JOINT_STEREO
Definition wavpack.h:36
static av_always_inline int wp_log2(uint32_t val)
Definition wavpack.h:151
#define WV_SINGLE_BLOCK
Definition wavpack.h:52
#define WV_FALSE_STEREO
Definition wavpack.h:40
#define UPDATE_WEIGHT_CLIP(weight, delta, samples, in)
Definition wavpack.h:112