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