FFmpeg  4.3.6
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 "libavutil/buffer.h"
25 
26 #define BITSTREAM_READER_LE
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "get_bits.h"
30 #include "internal.h"
31 #include "thread.h"
32 #include "unary.h"
33 #include "wavpack.h"
34 #include "dsd.h"
35 
36 /**
37  * @file
38  * WavPack lossless audio decoder
39  */
40 
41 #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) & 0xff000000))
42 
43 #define PTABLE_BITS 8
44 #define PTABLE_BINS (1<<PTABLE_BITS)
45 #define PTABLE_MASK (PTABLE_BINS-1)
46 
47 #define UP 0x010000fe
48 #define DOWN 0x00010000
49 #define DECAY 8
50 
51 #define PRECISION 20
52 #define VALUE_ONE (1 << PRECISION)
53 #define PRECISION_USE 12
54 
55 #define RATE_S 20
56 
57 #define MAX_HISTORY_BITS 5
58 #define MAX_HISTORY_BINS (1 << MAX_HISTORY_BITS)
59 #define MAX_BIN_BYTES 1280 // for value_lookup, per bin (2k - 512 - 256)
60 
61 typedef enum {
62  MODULATION_PCM, // pulse code modulation
63  MODULATION_DSD // pulse density modulation (aka DSD)
64 } Modulation;
65 
66 typedef struct WavpackFrameContext {
70  int joint;
71  uint32_t CRC;
74  uint32_t crc_extra_bits;
76  int samples;
77  int terms;
79  int zero, one, zeroes;
81  int and, or, shift;
89 
97 
98 #define WV_MAX_FRAME_DECODERS 14
99 
100 typedef struct WavpackContext {
102 
104  int fdec_num;
105 
106  int block;
107  int samples;
109 
111  ThreadFrame curr_frame, prev_frame;
113 
118 
119 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
120 
121 static av_always_inline unsigned get_tail(GetBitContext *gb, int k)
122 {
123  int p, e, res;
124 
125  if (k < 1)
126  return 0;
127  p = av_log2(k);
128  e = (1 << (p + 1)) - k - 1;
129  res = get_bitsz(gb, p);
130  if (res >= e)
131  res = res * 2U - e + get_bits1(gb);
132  return res;
133 }
134 
136 {
137  int i, br[2], sl[2];
138 
139  for (i = 0; i <= ctx->stereo_in; i++) {
140  if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
141  return AVERROR_INVALIDDATA;
142  ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
143  br[i] = ctx->ch[i].bitrate_acc >> 16;
144  sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
145  }
146  if (ctx->stereo_in && ctx->hybrid_bitrate) {
147  int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
148  if (balance > br[0]) {
149  br[1] = br[0] * 2;
150  br[0] = 0;
151  } else if (-balance > br[0]) {
152  br[0] *= 2;
153  br[1] = 0;
154  } else {
155  br[1] = br[0] + balance;
156  br[0] = br[0] - balance;
157  }
158  }
159  for (i = 0; i <= ctx->stereo_in; i++) {
160  if (ctx->hybrid_bitrate) {
161  if (sl[i] - br[i] > -0x100)
162  ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
163  else
164  ctx->ch[i].error_limit = 0;
165  } else {
166  ctx->ch[i].error_limit = wp_exp2(br[i]);
167  }
168  }
169 
170  return 0;
171 }
172 
174  int channel, int *last)
175 {
176  int t, t2;
177  int sign, base, add, ret;
178  WvChannel *c = &ctx->ch[channel];
179 
180  *last = 0;
181 
182  if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
183  !ctx->zero && !ctx->one) {
184  if (ctx->zeroes) {
185  ctx->zeroes--;
186  if (ctx->zeroes) {
188  return 0;
189  }
190  } else {
191  t = get_unary_0_33(gb);
192  if (t >= 2) {
193  if (t >= 32 || get_bits_left(gb) < t - 1)
194  goto error;
195  t = get_bits_long(gb, t - 1) | (1 << (t - 1));
196  } else {
197  if (get_bits_left(gb) < 0)
198  goto error;
199  }
200  ctx->zeroes = t;
201  if (ctx->zeroes) {
202  memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
203  memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
205  return 0;
206  }
207  }
208  }
209 
210  if (ctx->zero) {
211  t = 0;
212  ctx->zero = 0;
213  } else {
214  t = get_unary_0_33(gb);
215  if (get_bits_left(gb) < 0)
216  goto error;
217  if (t == 16) {
218  t2 = get_unary_0_33(gb);
219  if (t2 < 2) {
220  if (get_bits_left(gb) < 0)
221  goto error;
222  t += t2;
223  } else {
224  if (t2 >= 32 || get_bits_left(gb) < t2 - 1)
225  goto error;
226  t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
227  }
228  }
229 
230  if (ctx->one) {
231  ctx->one = t & 1;
232  t = (t >> 1) + 1;
233  } else {
234  ctx->one = t & 1;
235  t >>= 1;
236  }
237  ctx->zero = !ctx->one;
238  }
239 
240  if (ctx->hybrid && !channel) {
241  if (update_error_limit(ctx) < 0)
242  goto error;
243  }
244 
245  if (!t) {
246  base = 0;
247  add = GET_MED(0) - 1;
248  DEC_MED(0);
249  } else if (t == 1) {
250  base = GET_MED(0);
251  add = GET_MED(1) - 1;
252  INC_MED(0);
253  DEC_MED(1);
254  } else if (t == 2) {
255  base = GET_MED(0) + GET_MED(1);
256  add = GET_MED(2) - 1;
257  INC_MED(0);
258  INC_MED(1);
259  DEC_MED(2);
260  } else {
261  base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2U);
262  add = GET_MED(2) - 1;
263  INC_MED(0);
264  INC_MED(1);
265  INC_MED(2);
266  }
267  if (!c->error_limit) {
268  if (add >= 0x2000000U) {
269  av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
270  goto error;
271  }
272  ret = base + get_tail(gb, add);
273  if (get_bits_left(gb) <= 0)
274  goto error;
275  } else {
276  int mid = (base * 2U + add + 1) >> 1;
277  while (add > c->error_limit) {
278  if (get_bits_left(gb) <= 0)
279  goto error;
280  if (get_bits1(gb)) {
281  add -= (mid - (unsigned)base);
282  base = mid;
283  } else
284  add = mid - (unsigned)base - 1;
285  mid = (base * 2U + add + 1) >> 1;
286  }
287  ret = mid;
288  }
289  sign = get_bits1(gb);
290  if (ctx->hybrid_bitrate)
291  c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
292  return sign ? ~ret : ret;
293 
294 error:
295  ret = get_bits_left(gb);
296  if (ret <= 0) {
297  av_log(ctx->avctx, AV_LOG_ERROR, "Too few bits (%d) left\n", ret);
298  }
299  *last = 1;
300  return 0;
301 }
302 
303 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
304  unsigned S)
305 {
306  unsigned bit;
307 
308  if (s->extra_bits) {
309  S *= 1 << s->extra_bits;
310 
311  if (s->got_extra_bits &&
313  S |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
314  *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
315  }
316  }
317 
318  bit = (S & s->and) | s->or;
319  bit = ((S + bit) << s->shift) - bit;
320 
321  if (s->hybrid)
322  bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
323 
324  return bit << s->post_shift;
325 }
326 
327 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
328 {
329  union {
330  float f;
331  uint32_t u;
332  } value;
333 
334  unsigned int sign;
335  int exp = s->float_max_exp;
336 
337  if (s->got_extra_bits) {
338  const int max_bits = 1 + 23 + 8 + 1;
339  const int left_bits = get_bits_left(&s->gb_extra_bits);
340 
341  if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
342  return 0.0;
343  }
344 
345  if (S) {
346  S *= 1U << s->float_shift;
347  sign = S < 0;
348  if (sign)
349  S = -(unsigned)S;
350  if (S >= 0x1000000U) {
351  if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
352  S = get_bits(&s->gb_extra_bits, 23);
353  else
354  S = 0;
355  exp = 255;
356  } else if (exp) {
357  int shift = 23 - av_log2(S);
358  exp = s->float_max_exp;
359  if (exp <= shift)
360  shift = --exp;
361  exp -= shift;
362 
363  if (shift) {
364  S <<= shift;
365  if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
366  (s->got_extra_bits &&
367  (s->float_flag & WV_FLT_SHIFT_SAME) &&
368  get_bits1(&s->gb_extra_bits))) {
369  S |= (1 << shift) - 1;
370  } else if (s->got_extra_bits &&
371  (s->float_flag & WV_FLT_SHIFT_SENT)) {
372  S |= get_bits(&s->gb_extra_bits, shift);
373  }
374  }
375  } else {
376  exp = s->float_max_exp;
377  }
378  S &= 0x7fffff;
379  } else {
380  sign = 0;
381  exp = 0;
382  if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
383  if (get_bits1(&s->gb_extra_bits)) {
384  S = get_bits(&s->gb_extra_bits, 23);
385  if (s->float_max_exp >= 25)
386  exp = get_bits(&s->gb_extra_bits, 8);
387  sign = get_bits1(&s->gb_extra_bits);
388  } else {
389  if (s->float_flag & WV_FLT_ZERO_SIGN)
390  sign = get_bits1(&s->gb_extra_bits);
391  }
392  }
393  }
394 
395  *crc = *crc * 27 + S * 9 + exp * 3 + sign;
396 
397  value.u = (sign << 31) | (exp << 23) | S;
398  return value.f;
399 }
400 
401 static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
402  uint32_t crc_extra_bits)
403 {
404  if (crc != s->CRC) {
405  av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
406  return AVERROR_INVALIDDATA;
407  }
408  if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
409  av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
410  return AVERROR_INVALIDDATA;
411  }
412 
413  return 0;
414 }
415 
416 static void init_ptable(int *table, int rate_i, int rate_s)
417 {
418  int value = 0x808000, rate = rate_i << 8;
419 
420  for (int c = (rate + 128) >> 8; c--;)
421  value += (DOWN - value) >> DECAY;
422 
423  for (int i = 0; i < PTABLE_BINS/2; i++) {
424  table[i] = value;
425  table[PTABLE_BINS-1-i] = 0x100ffff - value;
426 
427  if (value > 0x010000) {
428  rate += (rate * rate_s + 128) >> 8;
429 
430  for (int c = (rate + 64) >> 7; c--;)
431  value += (DOWN - value) >> DECAY;
432  }
433  }
434 }
435 
436 typedef struct {
437  int32_t value, fltr0, fltr1, fltr2, fltr3, fltr4, fltr5, fltr6, factor;
438  unsigned int byte;
439 } DSDfilters;
440 
441 static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
442 {
443  uint32_t checksum = 0xFFFFFFFF;
444  uint8_t *dst_l = dst_left, *dst_r = dst_right;
445  int total_samples = s->samples, stereo = dst_r ? 1 : 0;
446  DSDfilters filters[2], *sp = filters;
447  int rate_i, rate_s;
448  uint32_t low, high, value;
449 
450  if (bytestream2_get_bytes_left(&s->gbyte) < (stereo ? 20 : 13))
451  return AVERROR_INVALIDDATA;
452 
453  rate_i = bytestream2_get_byte(&s->gbyte);
454  rate_s = bytestream2_get_byte(&s->gbyte);
455 
456  if (rate_s != RATE_S)
457  return AVERROR_INVALIDDATA;
458 
459  init_ptable(s->ptable, rate_i, rate_s);
460 
461  for (int channel = 0; channel < stereo + 1; channel++) {
462  DSDfilters *sp = filters + channel;
463 
464  sp->fltr1 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
465  sp->fltr2 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
466  sp->fltr3 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
467  sp->fltr4 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
468  sp->fltr5 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
469  sp->fltr6 = 0;
470  sp->factor = bytestream2_get_byte(&s->gbyte) & 0xff;
471  sp->factor |= (bytestream2_get_byte(&s->gbyte) << 8) & 0xff00;
472  sp->factor = (int32_t)((uint32_t)sp->factor << 16) >> 16;
473  }
474 
475  value = bytestream2_get_be32(&s->gbyte);
476  high = 0xffffffff;
477  low = 0x0;
478 
479  while (total_samples--) {
480  int bitcount = 8;
481 
482  sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
483 
484  if (stereo)
485  sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
486 
487  while (bitcount--) {
488  int32_t *pp = s->ptable + ((sp[0].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
489  uint32_t split = low + ((high - low) >> 8) * (*pp >> 16);
490 
491  if (value <= split) {
492  high = split;
493  *pp += (UP - *pp) >> DECAY;
494  sp[0].fltr0 = -1;
495  } else {
496  low = split + 1;
497  *pp += (DOWN - *pp) >> DECAY;
498  sp[0].fltr0 = 0;
499  }
500 
501  if (DSD_BYTE_READY(high, low) && !bytestream2_get_bytes_left(&s->gbyte))
502  return AVERROR_INVALIDDATA;
503  while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
504  value = (value << 8) | bytestream2_get_byte(&s->gbyte);
505  high = (high << 8) | 0xff;
506  low <<= 8;
507  }
508 
509  sp[0].value += sp[0].fltr6 * 8;
510  sp[0].byte = (sp[0].byte << 1) | (sp[0].fltr0 & 1);
511  sp[0].factor += (((sp[0].value ^ sp[0].fltr0) >> 31) | 1) &
512  ((sp[0].value ^ (sp[0].value - (sp[0].fltr6 * 16))) >> 31);
513  sp[0].fltr1 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr1) >> 6;
514  sp[0].fltr2 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr2) >> 4;
515  sp[0].fltr3 += (sp[0].fltr2 - sp[0].fltr3) >> 4;
516  sp[0].fltr4 += (sp[0].fltr3 - sp[0].fltr4) >> 4;
517  sp[0].value = (sp[0].fltr4 - sp[0].fltr5) >> 4;
518  sp[0].fltr5 += sp[0].value;
519  sp[0].fltr6 += (sp[0].value - sp[0].fltr6) >> 3;
520  sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
521 
522  if (!stereo)
523  continue;
524 
525  pp = s->ptable + ((sp[1].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
526  split = low + ((high - low) >> 8) * (*pp >> 16);
527 
528  if (value <= split) {
529  high = split;
530  *pp += (UP - *pp) >> DECAY;
531  sp[1].fltr0 = -1;
532  } else {
533  low = split + 1;
534  *pp += (DOWN - *pp) >> DECAY;
535  sp[1].fltr0 = 0;
536  }
537 
538  if (DSD_BYTE_READY(high, low) && !bytestream2_get_bytes_left(&s->gbyte))
539  return AVERROR_INVALIDDATA;
540  while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
541  value = (value << 8) | bytestream2_get_byte(&s->gbyte);
542  high = (high << 8) | 0xff;
543  low <<= 8;
544  }
545 
546  sp[1].value += sp[1].fltr6 * 8;
547  sp[1].byte = (sp[1].byte << 1) | (sp[1].fltr0 & 1);
548  sp[1].factor += (((sp[1].value ^ sp[1].fltr0) >> 31) | 1) &
549  ((sp[1].value ^ (sp[1].value - (sp[1].fltr6 * 16))) >> 31);
550  sp[1].fltr1 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr1) >> 6;
551  sp[1].fltr2 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr2) >> 4;
552  sp[1].fltr3 += (sp[1].fltr2 - sp[1].fltr3) >> 4;
553  sp[1].fltr4 += (sp[1].fltr3 - sp[1].fltr4) >> 4;
554  sp[1].value = (sp[1].fltr4 - sp[1].fltr5) >> 4;
555  sp[1].fltr5 += sp[1].value;
556  sp[1].fltr6 += (sp[1].value - sp[1].fltr6) >> 3;
557  sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
558  }
559 
560  checksum += (checksum << 1) + (*dst_l = sp[0].byte & 0xff);
561  sp[0].factor -= (sp[0].factor + 512) >> 10;
562  dst_l += 4;
563 
564  if (stereo) {
565  checksum += (checksum << 1) + (*dst_r = filters[1].byte & 0xff);
566  filters[1].factor -= (filters[1].factor + 512) >> 10;
567  dst_r += 4;
568  }
569  }
570 
571  if (wv_check_crc(s, checksum, 0)) {
573  return AVERROR_INVALIDDATA;
574 
575  memset(dst_left, 0x69, s->samples * 4);
576 
577  if (dst_r)
578  memset(dst_right, 0x69, s->samples * 4);
579  }
580 
581  return 0;
582 }
583 
584 static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
585 {
586  uint8_t *dst_l = dst_left, *dst_r = dst_right;
587  uint8_t history_bits, max_probability;
588  int total_summed_probabilities = 0;
589  int total_samples = s->samples;
590  uint8_t *vlb = s->value_lookup_buffer;
591  int history_bins, p0, p1, chan;
592  uint32_t checksum = 0xFFFFFFFF;
593  uint32_t low, high, value;
594 
596  return AVERROR_INVALIDDATA;
597 
598  history_bits = bytestream2_get_byte(&s->gbyte);
599 
600  if (!bytestream2_get_bytes_left(&s->gbyte) || history_bits > MAX_HISTORY_BITS)
601  return AVERROR_INVALIDDATA;
602 
603  history_bins = 1 << history_bits;
604  max_probability = bytestream2_get_byte(&s->gbyte);
605 
606  if (max_probability < 0xff) {
607  uint8_t *outptr = (uint8_t *)s->probabilities;
608  uint8_t *outend = outptr + sizeof(*s->probabilities) * history_bins;
609 
610  while (outptr < outend && bytestream2_get_bytes_left(&s->gbyte)) {
611  int code = bytestream2_get_byte(&s->gbyte);
612 
613  if (code > max_probability) {
614  int zcount = code - max_probability;
615 
616  while (outptr < outend && zcount--)
617  *outptr++ = 0;
618  } else if (code) {
619  *outptr++ = code;
620  }
621  else {
622  break;
623  }
624  }
625 
626  if (outptr < outend ||
627  (bytestream2_get_bytes_left(&s->gbyte) && bytestream2_get_byte(&s->gbyte)))
628  return AVERROR_INVALIDDATA;
629  } else if (bytestream2_get_bytes_left(&s->gbyte) > (int)sizeof(*s->probabilities) * history_bins) {
631  sizeof(*s->probabilities) * history_bins);
632  } else {
633  return AVERROR_INVALIDDATA;
634  }
635 
636  for (p0 = 0; p0 < history_bins; p0++) {
637  int32_t sum_values = 0;
638 
639  for (int i = 0; i < 256; i++)
640  s->summed_probabilities[p0][i] = sum_values += s->probabilities[p0][i];
641 
642  if (sum_values) {
643  total_summed_probabilities += sum_values;
644 
645  if (total_summed_probabilities > history_bins * MAX_BIN_BYTES)
646  return AVERROR_INVALIDDATA;
647 
648  s->value_lookup[p0] = vlb;
649 
650  for (int i = 0; i < 256; i++) {
651  int c = s->probabilities[p0][i];
652 
653  while (c--)
654  *vlb++ = i;
655  }
656  }
657  }
658 
659  if (bytestream2_get_bytes_left(&s->gbyte) < 4)
660  return AVERROR_INVALIDDATA;
661 
662  chan = p0 = p1 = 0;
663  low = 0; high = 0xffffffff;
664  value = bytestream2_get_be32(&s->gbyte);
665 
666  if (dst_r)
667  total_samples *= 2;
668 
669  while (total_samples--) {
670  unsigned int mult, index, code;
671 
672  if (!s->summed_probabilities[p0][255])
673  return AVERROR_INVALIDDATA;
674 
675  mult = (high - low) / s->summed_probabilities[p0][255];
676 
677  if (!mult) {
678  if (bytestream2_get_bytes_left(&s->gbyte) >= 4)
679  value = bytestream2_get_be32(&s->gbyte);
680 
681  low = 0;
682  high = 0xffffffff;
683  mult = high / s->summed_probabilities[p0][255];
684 
685  if (!mult)
686  return AVERROR_INVALIDDATA;
687  }
688 
689  index = (value - low) / mult;
690 
691  if (index >= s->summed_probabilities[p0][255])
692  return AVERROR_INVALIDDATA;
693 
694  if (!dst_r) {
695  if ((*dst_l = code = s->value_lookup[p0][index]))
696  low += s->summed_probabilities[p0][code-1] * mult;
697 
698  dst_l += 4;
699  } else {
700  if ((code = s->value_lookup[p0][index]))
701  low += s->summed_probabilities[p0][code-1] * mult;
702 
703  if (chan) {
704  *dst_r = code;
705  dst_r += 4;
706  }
707  else {
708  *dst_l = code;
709  dst_l += 4;
710  }
711 
712  chan ^= 1;
713  }
714 
715  high = low + s->probabilities[p0][code] * mult - 1;
716  checksum += (checksum << 1) + code;
717 
718  if (!dst_r) {
719  p0 = code & (history_bins-1);
720  } else {
721  p0 = p1;
722  p1 = code & (history_bins-1);
723  }
724 
725  while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
726  value = (value << 8) | bytestream2_get_byte(&s->gbyte);
727  high = (high << 8) | 0xff;
728  low <<= 8;
729  }
730  }
731 
732  if (wv_check_crc(s, checksum, 0)) {
734  return AVERROR_INVALIDDATA;
735 
736  memset(dst_left, 0x69, s->samples * 4);
737 
738  if (dst_r)
739  memset(dst_right, 0x69, s->samples * 4);
740  }
741 
742  return 0;
743 }
744 
745 static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
746 {
747  uint8_t *dst_l = dst_left, *dst_r = dst_right;
748  int total_samples = s->samples;
749  uint32_t checksum = 0xFFFFFFFF;
750 
751  if (bytestream2_get_bytes_left(&s->gbyte) != total_samples * (dst_r ? 2 : 1))
752  return AVERROR_INVALIDDATA;
753 
754  while (total_samples--) {
755  checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&s->gbyte));
756  dst_l += 4;
757 
758  if (dst_r) {
759  checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&s->gbyte));
760  dst_r += 4;
761  }
762  }
763 
764  if (wv_check_crc(s, checksum, 0)) {
766  return AVERROR_INVALIDDATA;
767 
768  memset(dst_left, 0x69, s->samples * 4);
769 
770  if (dst_r)
771  memset(dst_right, 0x69, s->samples * 4);
772  }
773 
774  return 0;
775 }
776 
778  void *dst_l, void *dst_r, const int type)
779 {
780  int i, j, count = 0;
781  int last, t;
782  int A, B, L, L2, R, R2;
783  int pos = 0;
784  uint32_t crc = 0xFFFFFFFF;
785  uint32_t crc_extra_bits = 0xFFFFFFFF;
786  int16_t *dst16_l = dst_l;
787  int16_t *dst16_r = dst_r;
788  int32_t *dst32_l = dst_l;
789  int32_t *dst32_r = dst_r;
790  float *dstfl_l = dst_l;
791  float *dstfl_r = dst_r;
792 
793  s->one = s->zero = s->zeroes = 0;
794  do {
795  L = wv_get_value(s, gb, 0, &last);
796  if (last)
797  break;
798  R = wv_get_value(s, gb, 1, &last);
799  if (last)
800  break;
801  for (i = 0; i < s->terms; i++) {
802  t = s->decorr[i].value;
803  if (t > 0) {
804  if (t > 8) {
805  if (t & 1) {
806  A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
807  B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
808  } else {
809  A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
810  B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
811  }
812  s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
813  s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
814  j = 0;
815  } else {
816  A = s->decorr[i].samplesA[pos];
817  B = s->decorr[i].samplesB[pos];
818  j = (pos + t) & 7;
819  }
820  if (type != AV_SAMPLE_FMT_S16P) {
821  L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
822  R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
823  } else {
824  L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
825  R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
826  }
827  if (A && L)
828  s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
829  if (B && R)
830  s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
831  s->decorr[i].samplesA[j] = L = L2;
832  s->decorr[i].samplesB[j] = R = R2;
833  } else if (t == -1) {
834  if (type != AV_SAMPLE_FMT_S16P)
835  L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
836  else
837  L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
838  UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
839  L = L2;
840  if (type != AV_SAMPLE_FMT_S16P)
841  R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
842  else
843  R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
844  UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
845  R = R2;
846  s->decorr[i].samplesA[0] = R;
847  } else {
848  if (type != AV_SAMPLE_FMT_S16P)
849  R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
850  else
851  R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
852  UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
853  R = R2;
854 
855  if (t == -3) {
856  R2 = s->decorr[i].samplesA[0];
857  s->decorr[i].samplesA[0] = R;
858  }
859 
860  if (type != AV_SAMPLE_FMT_S16P)
861  L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
862  else
863  L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
864  UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
865  L = L2;
866  s->decorr[i].samplesB[0] = L;
867  }
868  }
869 
870  if (type == AV_SAMPLE_FMT_S16P) {
871  if (FFABS((int64_t)L) + FFABS((int64_t)R) > (1<<19)) {
872  av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
873  return AVERROR_INVALIDDATA;
874  }
875  }
876 
877  pos = (pos + 1) & 7;
878  if (s->joint)
879  L += (unsigned)(R -= (unsigned)(L >> 1));
880  crc = (crc * 3 + L) * 3 + R;
881 
882  if (type == AV_SAMPLE_FMT_FLTP) {
883  *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
884  *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
885  } else if (type == AV_SAMPLE_FMT_S32P) {
886  *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
887  *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
888  } else {
889  *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
890  *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
891  }
892  count++;
893  } while (!last && count < s->samples);
894 
895  if (last && count < s->samples) {
896  int size = av_get_bytes_per_sample(type);
897  memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
898  memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
899  }
900 
901  if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
902  wv_check_crc(s, crc, crc_extra_bits))
903  return AVERROR_INVALIDDATA;
904 
905  return 0;
906 }
907 
909  void *dst, const int type)
910 {
911  int i, j, count = 0;
912  int last, t;
913  int A, S, T;
914  int pos = 0;
915  uint32_t crc = 0xFFFFFFFF;
916  uint32_t crc_extra_bits = 0xFFFFFFFF;
917  int16_t *dst16 = dst;
918  int32_t *dst32 = dst;
919  float *dstfl = dst;
920 
921  s->one = s->zero = s->zeroes = 0;
922  do {
923  T = wv_get_value(s, gb, 0, &last);
924  S = 0;
925  if (last)
926  break;
927  for (i = 0; i < s->terms; i++) {
928  t = s->decorr[i].value;
929  if (t > 8) {
930  if (t & 1)
931  A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
932  else
933  A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
934  s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
935  j = 0;
936  } else {
937  A = s->decorr[i].samplesA[pos];
938  j = (pos + t) & 7;
939  }
940  if (type != AV_SAMPLE_FMT_S16P)
941  S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
942  else
943  S = T + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
944  if (A && T)
945  s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
946  s->decorr[i].samplesA[j] = T = S;
947  }
948  pos = (pos + 1) & 7;
949  crc = crc * 3 + S;
950 
951  if (type == AV_SAMPLE_FMT_FLTP) {
952  *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
953  } else if (type == AV_SAMPLE_FMT_S32P) {
954  *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
955  } else {
956  *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
957  }
958  count++;
959  } while (!last && count < s->samples);
960 
961  if (last && count < s->samples) {
962  int size = av_get_bytes_per_sample(type);
963  memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
964  }
965 
967  int ret = wv_check_crc(s, crc, crc_extra_bits);
968  if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
969  return ret;
970  }
971 
972  return 0;
973 }
974 
976 {
978  return -1;
979 
980  c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
981  if (!c->fdec[c->fdec_num])
982  return -1;
983  c->fdec_num++;
984  c->fdec[c->fdec_num - 1]->avctx = c->avctx;
985 
986  return 0;
987 }
988 
990 {
991  int i;
992 
993  s->dsdctx = NULL;
994  s->dsd_channels = 0;
996 
997  if (!channels)
998  return 0;
999 
1000  if (channels > INT_MAX / sizeof(*s->dsdctx))
1001  return AVERROR(EINVAL);
1002 
1003  s->dsd_ref = av_buffer_allocz(channels * sizeof(*s->dsdctx));
1004  if (!s->dsd_ref)
1005  return AVERROR(ENOMEM);
1006  s->dsdctx = (DSDContext*)s->dsd_ref->data;
1007  s->dsd_channels = channels;
1008 
1009  for (i = 0; i < channels; i++)
1010  memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
1011 
1012  return 0;
1013 }
1014 
1015 #if HAVE_THREADS
1016 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1017 {
1018  WavpackContext *fsrc = src->priv_data;
1019  WavpackContext *fdst = dst->priv_data;
1020  int ret;
1021 
1022  if (dst == src)
1023  return 0;
1024 
1025  ff_thread_release_buffer(dst, &fdst->curr_frame);
1026  if (fsrc->curr_frame.f->data[0]) {
1027  if ((ret = ff_thread_ref_frame(&fdst->curr_frame, &fsrc->curr_frame)) < 0)
1028  return ret;
1029  }
1030 
1031  av_buffer_unref(&fdst->dsd_ref);
1032  fdst->dsdctx = NULL;
1033  fdst->dsd_channels = 0;
1034  if (fsrc->dsd_ref) {
1035  fdst->dsd_ref = av_buffer_ref(fsrc->dsd_ref);
1036  if (!fdst->dsd_ref)
1037  return AVERROR(ENOMEM);
1038  fdst->dsdctx = (DSDContext*)fdst->dsd_ref->data;
1039  fdst->dsd_channels = fsrc->dsd_channels;
1040  }
1041 
1042  return 0;
1043 }
1044 #endif
1045 
1047 {
1048  WavpackContext *s = avctx->priv_data;
1049 
1050  s->avctx = avctx;
1051 
1052  s->fdec_num = 0;
1053 
1054  s->curr_frame.f = av_frame_alloc();
1055  s->prev_frame.f = av_frame_alloc();
1056 
1057  if (!s->curr_frame.f || !s->prev_frame.f)
1058  return AVERROR(ENOMEM);
1059 
1060  ff_init_dsd_data();
1061 
1062  return 0;
1063 }
1064 
1066 {
1067  WavpackContext *s = avctx->priv_data;
1068 
1069  for (int i = 0; i < s->fdec_num; i++)
1070  av_freep(&s->fdec[i]);
1071  s->fdec_num = 0;
1072 
1074  av_frame_free(&s->curr_frame.f);
1075 
1077  av_frame_free(&s->prev_frame.f);
1078 
1079  av_buffer_unref(&s->dsd_ref);
1080 
1081  return 0;
1082 }
1083 
1084 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
1085  const uint8_t *buf, int buf_size)
1086 {
1087  WavpackContext *wc = avctx->priv_data;
1090  enum AVSampleFormat sample_fmt;
1091  void *samples_l = NULL, *samples_r = NULL;
1092  int ret;
1093  int got_terms = 0, got_weights = 0, got_samples = 0,
1094  got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1095  int got_dsd = 0;
1096  int i, j, id, size, ssize, weights, t;
1097  int bpp, chan = 0, orig_bpp, sample_rate = 0, rate_x = 1, dsd_mode = 0;
1098  int multiblock;
1099  uint64_t chmask = 0;
1100 
1101  if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
1102  av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
1103  return AVERROR_INVALIDDATA;
1104  }
1105 
1106  s = wc->fdec[block_no];
1107  if (!s) {
1108  av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
1109  block_no);
1110  return AVERROR_INVALIDDATA;
1111  }
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;
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  if (s->decorr[i].value > 8) {
1227  s->decorr[i].samplesA[0] =
1228  wp_exp2(bytestream2_get_le16(&gb));
1229  s->decorr[i].samplesA[1] =
1230  wp_exp2(bytestream2_get_le16(&gb));
1231 
1232  if (s->stereo_in) {
1233  s->decorr[i].samplesB[0] =
1234  wp_exp2(bytestream2_get_le16(&gb));
1235  s->decorr[i].samplesB[1] =
1236  wp_exp2(bytestream2_get_le16(&gb));
1237  t += 4;
1238  }
1239  t += 4;
1240  } else if (s->decorr[i].value < 0) {
1241  s->decorr[i].samplesA[0] =
1242  wp_exp2(bytestream2_get_le16(&gb));
1243  s->decorr[i].samplesB[0] =
1244  wp_exp2(bytestream2_get_le16(&gb));
1245  t += 4;
1246  } else {
1247  for (j = 0; j < s->decorr[i].value; j++) {
1248  s->decorr[i].samplesA[j] =
1249  wp_exp2(bytestream2_get_le16(&gb));
1250  if (s->stereo_in) {
1251  s->decorr[i].samplesB[j] =
1252  wp_exp2(bytestream2_get_le16(&gb));
1253  }
1254  }
1255  t += s->decorr[i].value * 2 * (s->stereo_in + 1);
1256  }
1257  }
1258  got_samples = 1;
1259  break;
1260  case WP_ID_ENTROPY:
1261  if (size != 6 * (s->stereo_in + 1)) {
1262  av_log(avctx, AV_LOG_ERROR,
1263  "Entropy vars size should be %i, got %i.\n",
1264  6 * (s->stereo_in + 1), size);
1265  bytestream2_skip(&gb, ssize);
1266  continue;
1267  }
1268  for (j = 0; j <= s->stereo_in; j++)
1269  for (i = 0; i < 3; i++) {
1270  s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
1271  }
1272  got_entropy = 1;
1273  break;
1274  case WP_ID_HYBRID:
1275  if (s->hybrid_bitrate) {
1276  for (i = 0; i <= s->stereo_in; i++) {
1277  s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
1278  size -= 2;
1279  }
1280  }
1281  for (i = 0; i < (s->stereo_in + 1); i++) {
1282  s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
1283  size -= 2;
1284  }
1285  if (size > 0) {
1286  for (i = 0; i < (s->stereo_in + 1); i++) {
1287  s->ch[i].bitrate_delta =
1288  wp_exp2((int16_t)bytestream2_get_le16(&gb));
1289  }
1290  } else {
1291  for (i = 0; i < (s->stereo_in + 1); i++)
1292  s->ch[i].bitrate_delta = 0;
1293  }
1294  got_hybrid = 1;
1295  break;
1296  case WP_ID_INT32INFO: {
1297  uint8_t val[4];
1298  if (size != 4) {
1299  av_log(avctx, AV_LOG_ERROR,
1300  "Invalid INT32INFO, size = %i\n",
1301  size);
1302  bytestream2_skip(&gb, ssize - 4);
1303  continue;
1304  }
1305  bytestream2_get_buffer(&gb, val, 4);
1306  if (val[0] > 30) {
1307  av_log(avctx, AV_LOG_ERROR,
1308  "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
1309  continue;
1310  } else if (val[0]) {
1311  s->extra_bits = val[0];
1312  } else if (val[1]) {
1313  s->shift = val[1];
1314  } else if (val[2]) {
1315  s->and = s->or = 1;
1316  s->shift = val[2];
1317  } else if (val[3]) {
1318  s->and = 1;
1319  s->shift = val[3];
1320  }
1321  if (s->shift > 31) {
1322  av_log(avctx, AV_LOG_ERROR,
1323  "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
1324  s->and = s->or = s->shift = 0;
1325  continue;
1326  }
1327  /* original WavPack decoder forces 32-bit lossy sound to be treated
1328  * as 24-bit one in order to have proper clipping */
1329  if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
1330  s->post_shift += 8;
1331  s->shift -= 8;
1332  s->hybrid_maxclip >>= 8;
1333  s->hybrid_minclip >>= 8;
1334  }
1335  break;
1336  }
1337  case WP_ID_FLOATINFO:
1338  if (size != 4) {
1339  av_log(avctx, AV_LOG_ERROR,
1340  "Invalid FLOATINFO, size = %i\n", size);
1341  bytestream2_skip(&gb, ssize);
1342  continue;
1343  }
1344  s->float_flag = bytestream2_get_byte(&gb);
1345  s->float_shift = bytestream2_get_byte(&gb);
1346  s->float_max_exp = bytestream2_get_byte(&gb);
1347  if (s->float_shift > 31) {
1348  av_log(avctx, AV_LOG_ERROR,
1349  "Invalid FLOATINFO, shift = %d (> 31)\n", s->float_shift);
1350  s->float_shift = 0;
1351  continue;
1352  }
1353  got_float = 1;
1354  bytestream2_skip(&gb, 1);
1355  break;
1356  case WP_ID_DATA:
1357  if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
1358  return ret;
1359  bytestream2_skip(&gb, size);
1360  got_pcm = 1;
1361  break;
1362  case WP_ID_DSD_DATA:
1363  if (size < 2) {
1364  av_log(avctx, AV_LOG_ERROR, "Invalid DSD_DATA, size = %i\n",
1365  size);
1366  bytestream2_skip(&gb, ssize);
1367  continue;
1368  }
1369  rate_x = bytestream2_get_byte(&gb);
1370  if (rate_x > 30)
1371  return AVERROR_INVALIDDATA;
1372  rate_x = 1 << rate_x;
1373  dsd_mode = bytestream2_get_byte(&gb);
1374  if (dsd_mode && dsd_mode != 1 && dsd_mode != 3) {
1375  av_log(avctx, AV_LOG_ERROR, "Invalid DSD encoding mode: %d\n",
1376  dsd_mode);
1377  return AVERROR_INVALIDDATA;
1378  }
1379  bytestream2_init(&s->gbyte, gb.buffer, size-2);
1380  bytestream2_skip(&gb, size-2);
1381  got_dsd = 1;
1382  break;
1383  case WP_ID_EXTRABITS:
1384  if (size <= 4) {
1385  av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
1386  size);
1387  bytestream2_skip(&gb, size);
1388  continue;
1389  }
1390  if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
1391  return ret;
1393  bytestream2_skip(&gb, size);
1394  s->got_extra_bits = 1;
1395  break;
1396  case WP_ID_CHANINFO:
1397  if (size <= 1) {
1398  av_log(avctx, AV_LOG_ERROR,
1399  "Insufficient channel information\n");
1400  return AVERROR_INVALIDDATA;
1401  }
1402  chan = bytestream2_get_byte(&gb);
1403  switch (size - 2) {
1404  case 0:
1405  chmask = bytestream2_get_byte(&gb);
1406  break;
1407  case 1:
1408  chmask = bytestream2_get_le16(&gb);
1409  break;
1410  case 2:
1411  chmask = bytestream2_get_le24(&gb);
1412  break;
1413  case 3:
1414  chmask = bytestream2_get_le32(&gb);
1415  break;
1416  case 4:
1417  size = bytestream2_get_byte(&gb);
1418  chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1419  chan += 1;
1420  if (avctx->channels != chan)
1421  av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1422  " instead of %i.\n", chan, avctx->channels);
1423  chmask = bytestream2_get_le24(&gb);
1424  break;
1425  case 5:
1426  size = bytestream2_get_byte(&gb);
1427  chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1428  chan += 1;
1429  if (avctx->channels != chan)
1430  av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1431  " instead of %i.\n", chan, avctx->channels);
1432  chmask = bytestream2_get_le32(&gb);
1433  break;
1434  default:
1435  av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
1436  size);
1437  chan = avctx->channels;
1438  chmask = avctx->channel_layout;
1439  }
1440  break;
1441  case WP_ID_SAMPLE_RATE:
1442  if (size != 3) {
1443  av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
1444  return AVERROR_INVALIDDATA;
1445  }
1446  sample_rate = bytestream2_get_le24(&gb);
1447  break;
1448  default:
1449  bytestream2_skip(&gb, size);
1450  }
1451  if (id & WP_IDF_ODD)
1452  bytestream2_skip(&gb, 1);
1453  }
1454 
1455  if (got_pcm) {
1456  if (!got_terms) {
1457  av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1458  return AVERROR_INVALIDDATA;
1459  }
1460  if (!got_weights) {
1461  av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1462  return AVERROR_INVALIDDATA;
1463  }
1464  if (!got_samples) {
1465  av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1466  return AVERROR_INVALIDDATA;
1467  }
1468  if (!got_entropy) {
1469  av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1470  return AVERROR_INVALIDDATA;
1471  }
1472  if (s->hybrid && !got_hybrid) {
1473  av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1474  return AVERROR_INVALIDDATA;
1475  }
1476  if (!got_float && sample_fmt == AV_SAMPLE_FMT_FLTP) {
1477  av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1478  return AVERROR_INVALIDDATA;
1479  }
1480  if (s->got_extra_bits && sample_fmt != AV_SAMPLE_FMT_FLTP) {
1481  const int size = get_bits_left(&s->gb_extra_bits);
1482  const int wanted = s->samples * s->extra_bits << s->stereo_in;
1483  if (size < wanted) {
1484  av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1485  s->got_extra_bits = 0;
1486  }
1487  }
1488  }
1489 
1490  if (!got_pcm && !got_dsd) {
1491  av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1492  return AVERROR_INVALIDDATA;
1493  }
1494 
1495  if ((got_pcm && wc->modulation != MODULATION_PCM) ||
1496  (got_dsd && wc->modulation != MODULATION_DSD)) {
1497  av_log(avctx, AV_LOG_ERROR, "Invalid PCM/DSD mix encountered\n");
1498  return AVERROR_INVALIDDATA;
1499  }
1500 
1501  if (!wc->ch_offset) {
1502  int new_channels = avctx->channels;
1503  uint64_t new_chmask = avctx->channel_layout;
1504  int new_samplerate;
1505  int sr = (s->frame_flags >> 23) & 0xf;
1506  if (sr == 0xf) {
1507  if (!sample_rate) {
1508  av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
1509  return AVERROR_INVALIDDATA;
1510  }
1511  new_samplerate = sample_rate;
1512  } else
1513  new_samplerate = wv_rates[sr];
1514 
1515  if (new_samplerate * (uint64_t)rate_x > INT_MAX)
1516  return AVERROR_INVALIDDATA;
1517  new_samplerate *= rate_x;
1518 
1519  if (multiblock) {
1520  if (chan)
1521  new_channels = chan;
1522  if (chmask)
1523  new_chmask = chmask;
1524  } else {
1525  new_channels = s->stereo ? 2 : 1;
1526  new_chmask = s->stereo ? AV_CH_LAYOUT_STEREO :
1528  }
1529 
1530  if (new_chmask &&
1531  av_get_channel_layout_nb_channels(new_chmask) != new_channels) {
1532  av_log(avctx, AV_LOG_ERROR, "Channel mask does not match the channel count\n");
1533  return AVERROR_INVALIDDATA;
1534  }
1535 
1536  /* clear DSD state if stream properties change */
1537  if (new_channels != wc->dsd_channels ||
1538  new_chmask != avctx->channel_layout ||
1539  new_samplerate != avctx->sample_rate ||
1540  !!got_dsd != !!wc->dsdctx) {
1541  ret = wv_dsd_reset(wc, got_dsd ? new_channels : 0);
1542  if (ret < 0) {
1543  av_log(avctx, AV_LOG_ERROR, "Error reinitializing the DSD context\n");
1544  return ret;
1545  }
1546  ff_thread_release_buffer(avctx, &wc->curr_frame);
1547  }
1548  avctx->channels = new_channels;
1549  avctx->channel_layout = new_chmask;
1550  avctx->sample_rate = new_samplerate;
1551  avctx->sample_fmt = sample_fmt;
1552  avctx->bits_per_raw_sample = orig_bpp;
1553 
1554  ff_thread_release_buffer(avctx, &wc->prev_frame);
1556 
1557  /* get output buffer */
1558  wc->curr_frame.f->nb_samples = s->samples;
1559  if ((ret = ff_thread_get_buffer(avctx, &wc->curr_frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1560  return ret;
1561 
1562  wc->frame = wc->curr_frame.f;
1563  ff_thread_finish_setup(avctx);
1564  }
1565 
1566  if (wc->ch_offset + s->stereo >= avctx->channels) {
1567  av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
1568  return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
1569  }
1570 
1571  samples_l = wc->frame->extended_data[wc->ch_offset];
1572  if (s->stereo)
1573  samples_r = wc->frame->extended_data[wc->ch_offset + 1];
1574 
1575  wc->ch_offset += 1 + s->stereo;
1576 
1577  if (s->stereo_in) {
1578  if (got_dsd) {
1579  if (dsd_mode == 3) {
1580  ret = wv_unpack_dsd_high(s, samples_l, samples_r);
1581  } else if (dsd_mode == 1) {
1582  ret = wv_unpack_dsd_fast(s, samples_l, samples_r);
1583  } else {
1584  ret = wv_unpack_dsd_copy(s, samples_l, samples_r);
1585  }
1586  } else {
1587  ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1588  }
1589  if (ret < 0)
1590  return ret;
1591  } else {
1592  if (got_dsd) {
1593  if (dsd_mode == 3) {
1594  ret = wv_unpack_dsd_high(s, samples_l, NULL);
1595  } else if (dsd_mode == 1) {
1596  ret = wv_unpack_dsd_fast(s, samples_l, NULL);
1597  } else {
1598  ret = wv_unpack_dsd_copy(s, samples_l, NULL);
1599  }
1600  } else {
1601  ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1602  }
1603  if (ret < 0)
1604  return ret;
1605 
1606  if (s->stereo)
1607  memcpy(samples_r, samples_l, bpp * s->samples);
1608  }
1609 
1610  return 0;
1611 }
1612 
1614 {
1615  WavpackContext *s = avctx->priv_data;
1616 
1617  wv_dsd_reset(s, 0);
1618 }
1619 
1620 static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
1621 {
1622  WavpackContext *s = avctx->priv_data;
1623  AVFrame *frame = frmptr;
1624 
1625  ff_dsd2pcm_translate (&s->dsdctx [jobnr], s->samples, 0,
1626  (uint8_t *)frame->extended_data[jobnr], 4,
1627  (float *)frame->extended_data[jobnr], 1);
1628 
1629  return 0;
1630 }
1631 
1633  int *got_frame_ptr, AVPacket *avpkt)
1634 {
1635  WavpackContext *s = avctx->priv_data;
1636  const uint8_t *buf = avpkt->data;
1637  int buf_size = avpkt->size;
1638  int frame_size, ret, frame_flags;
1639 
1640  if (avpkt->size <= WV_HEADER_SIZE)
1641  return AVERROR_INVALIDDATA;
1642 
1643  s->frame = NULL;
1644  s->block = 0;
1645  s->ch_offset = 0;
1646 
1647  /* determine number of samples */
1648  s->samples = AV_RL32(buf + 20);
1649  frame_flags = AV_RL32(buf + 24);
1650  if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1651  av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1652  s->samples);
1653  return AVERROR_INVALIDDATA;
1654  }
1655 
1656  s->modulation = (frame_flags & WV_DSD_DATA) ? MODULATION_DSD : MODULATION_PCM;
1657 
1658  while (buf_size > WV_HEADER_SIZE) {
1659  frame_size = AV_RL32(buf + 4) - 12;
1660  buf += 20;
1661  buf_size -= 20;
1662  if (frame_size <= 0 || frame_size > buf_size) {
1663  av_log(avctx, AV_LOG_ERROR,
1664  "Block %d has invalid size (size %d vs. %d bytes left)\n",
1665  s->block, frame_size, buf_size);
1666  ret = AVERROR_INVALIDDATA;
1667  goto error;
1668  }
1669  if ((ret = wavpack_decode_block(avctx, s->block, buf, frame_size)) < 0)
1670  goto error;
1671  s->block++;
1672  buf += frame_size;
1673  buf_size -= frame_size;
1674  }
1675 
1676  if (s->ch_offset != avctx->channels) {
1677  av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
1678  ret = AVERROR_INVALIDDATA;
1679  goto error;
1680  }
1681 
1682  ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1684 
1685  if (s->modulation == MODULATION_DSD)
1686  avctx->execute2(avctx, dsd_channel, s->frame, NULL, avctx->channels);
1687 
1688  ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1689 
1690  if ((ret = av_frame_ref(data, s->frame)) < 0)
1691  return ret;
1692 
1693  *got_frame_ptr = 1;
1694 
1695  return avpkt->size;
1696 
1697 error:
1698  if (s->frame) {
1699  ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1701  ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1702  }
1703 
1704  return ret;
1705 }
1706 
1708  .name = "wavpack",
1709  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1710  .type = AVMEDIA_TYPE_AUDIO,
1711  .id = AV_CODEC_ID_WAVPACK,
1712  .priv_data_size = sizeof(WavpackContext),
1714  .close = wavpack_decode_end,
1717  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1718  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1721 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
int delta
Definition: wavpack.h:86
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
#define WV_HYBRID_MODE
Definition: wavpack.h:40
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int median[3]
Definition: wavpack.h:97
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
unsigned bitrate_delta
Definition: wavpack.h:99
static void flush(AVCodecContext *avctx)
static void wavpack_decode_flush(AVCodecContext *avctx)
Definition: wavpack.c:1613
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVFrame * f
Definition: thread.h:35
static int wv_dsd_reset(WavpackContext *s, int channels)
Definition: wavpack.c:989
int slow_level
Definition: wavpack.h:98
Definition: wvdec.c:32
#define MAX_HISTORY_BINS
Definition: wavpack.c:58
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int32_t fltr5
Definition: wavpack.c:437
int dsd_channels
Definition: wavpack.c:116
int samplesA[MAX_TERM]
Definition: wavpack.h:90
int size
Definition: packet.h:356
int32_t factor
Definition: wavpack.c:437
#define DECAY
Definition: wavpack.c:49
static int wavpack_decode_block(AVCodecContext *avctx, int block_no, const uint8_t *buf, int buf_size)
Definition: wavpack.c:1084
#define R2
Definition: simple_idct.c:173
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
#define WV_FLT_SHIFT_ONES
Definition: wavpack.h:51
#define WV_FLOAT_DATA
Definition: wavpack.h:35
#define MAX_TERMS
Definition: wavpack.h:27
static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
Definition: wavpack.c:1620
int weightB
Definition: wavpack.h:89
GetByteContext gbyte
Definition: wavpack.c:90
#define WV_HYBRID_BITRATE
Definition: wavpack.h:42
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1757
#define AV_CH_LAYOUT_STEREO
static void error(const char *err)
uint32_t CRC
Definition: wavpack.c:71
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
av_cold void ff_init_dsd_data(void)
Definition: dsd.c:46
ThreadFrame curr_frame
Definition: wavpack.c:111
AVCodec.
Definition: codec.h:190
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
Decorr decorr[MAX_TERMS]
Definition: wavpack.c:78
int32_t fltr2
Definition: wavpack.c:437
uint8_t base
Definition: vp3data.h:202
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Modulation
Definition: wavpack.c:61
#define PRECISION
Definition: wavpack.c:51
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
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
uint8_t
#define av_cold
Definition: attributes.h:88
static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
Definition: wavpack.c:441
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
AVCodec ff_wavpack_decoder
Definition: wavpack.c:1707
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:55
#define f(width, name)
Definition: cbs_vp9.c:255
Multithreading support functions.
int value
Definition: wavpack.h:87
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:444
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
static AVFrame * frame
const char data[16]
Definition: mxf.c:91
uint8_t * data
Definition: packet.h:355
const uint8_t * buffer
Definition: bytestream.h:34
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1891
#define sp
Definition: regdef.h:63
bitstream reader API header.
#define WV_HEADER_SIZE
Definition: wavpack.h:30
#define WV_FLT_ZERO_SIGN
Definition: wavpack.h:55
#define MAX_BIN_BYTES
Definition: wavpack.c:59
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...
static av_always_inline int wp_log2(uint32_t val)
Definition: wavpack.h:182
channels
Definition: aptx.h:33
#define A(x)
Definition: vp56_arith.h:28
DSDContext * dsdctx
Definition: wavpack.c:115
#define av_log(a,...)
#define MAX_HISTORY_BITS
Definition: wavpack.c:57
static const uint16_t table[]
Definition: prosumer.c:206
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: internal.h:75
WavpackFrameContext * fdec[WV_MAX_FRAME_DECODERS]
Definition: wavpack.c:103
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
uint8_t probabilities[MAX_HISTORY_BINS][256]
Definition: wavpack.c:94
#define U(x)
Definition: vp56_arith.h:37
#define src
Definition: vp8dsp.c:254
static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
Definition: wavpack.c:745
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int update_error_limit(WavpackFrameContext *ctx)
Definition: wavpack.c:135
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define R
Definition: huffyuvdsp.h:34
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
#define S(s, c, i)
#define AVERROR(e)
Definition: error.h:43
#define DSD_BYTE_READY(low, high)
Definition: wavpack.c:41
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define B
Definition: huffyuvdsp.h:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
const uint8_t * code
Definition: spdifenc.c:413
static av_always_inline int wp_exp2(int16_t val)
Definition: wavpack.h:165
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
unsigned int pos
Definition: spdifenc.c:412
AVCodecContext * avctx
Definition: wavpack.c:67
uint8_t * value_lookup[MAX_HISTORY_BINS]
Definition: wavpack.c:95
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
#define WV_SINGLE_BLOCK
Definition: wavpack.h:49
const char * name
Name of the codec implementation.
Definition: codec.h:197
int ptable[PTABLE_BINS]
Definition: wavpack.c:91
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:106
int8_t exp
Definition: eval.c:72
#define WV_DSD_DATA
Definition: wavpack.h:38
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:227
int weightA
Definition: wavpack.h:88
int32_t fltr4
Definition: wavpack.c:437
#define T(x)
Definition: vp56_arith.h:29
audio channel layout utility functions
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:55
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1655
signed 32 bits, planar
Definition: samplefmt.h:68
static int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb, void *dst_l, void *dst_r, const int type)
Definition: wavpack.c:777
static const int wv_rates[16]
Definition: wavpack.h:121
#define WV_FALSE_STEREO
Definition: wavpack.h:37
static int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
Definition: wavpack.c:908
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
#define WV_FLT_SHIFT_SAME
Definition: wavpack.h:52
Per-channel buffer.
Definition: dsd.h:42
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
#define PTABLE_BINS
Definition: wavpack.c:44
#define UP
Definition: wavpack.c:47
#define AV_RL32
Definition: intreadwrite.h:146
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1666
#define WV_FLT_SHIFT_SENT
Definition: wavpack.h:53
static volatile int checksum
Definition: adler32.c:30
#define L(x)
Definition: vp56_arith.h:36
#define RATE_S
Definition: wavpack.c:55
int error_limit
Definition: wavpack.h:98
#define av_log2
Definition: intmath.h:83
int32_t value
Definition: wavpack.c:437
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:110
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:1845
#define VALUE_ONE
Definition: wavpack.c:52
static av_cold int wavpack_decode_end(AVCodecContext *avctx)
Definition: wavpack.c:1065
#define GET_MED(n)
Definition: wavpack.h:103
sample_rate
int frame_size
Definition: mxfenc.c:2137
uint32_t crc_extra_bits
Definition: wavpack.c:74
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
unsigned int byte
Definition: wavpack.c:438
int sample_rate
samples per second
Definition: avcodec.h:1186
unsigned bitrate_acc
Definition: wavpack.h:99
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:526
int32_t fltr1
Definition: wavpack.c:437
uint8_t * data
The data buffer.
Definition: buffer.h:89
static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
Definition: wavpack.c:327
int samplesB[MAX_TERM]
Definition: wavpack.h:91
static int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc, unsigned S)
Definition: wavpack.c:303
int32_t fltr6
Definition: wavpack.c:437
#define LEVEL_DECAY(a)
Definition: wavpack.c:119
Modulation modulation
Definition: wavpack.c:112
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
int32_t fltr0
Definition: wavpack.c:437
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
double value
Definition: eval.c:98
static void init_ptable(int *table, int rate_i, int rate_s)
Definition: wavpack.c:416
int index
Definition: gxfenc.c:89
cl_device_type type
#define WV_JOINT_STEREO
Definition: wavpack.h:33
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
Definition: avcodec.h:1663
refcounted data buffer API
static const int factor[16]
Definition: vf_pp7.c:75
static int wavpack_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: wavpack.c:1632
static int wv_check_crc(WavpackFrameContext *s, uint32_t crc, uint32_t crc_extra_bits)
Definition: wavpack.c:401
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb, int channel, int *last)
Definition: wavpack.c:173
WvChannel ch[2]
Definition: wavpack.c:88
#define UPDATE_WEIGHT_CLIP(weight, delta, samples, in)
Definition: wavpack.h:108
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
static av_always_inline unsigned get_tail(GetBitContext *gb, int k)
Definition: wavpack.c:121
A reference to a data buffer.
Definition: buffer.h:81
static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
Definition: wavpack.c:584
int
ThreadFrame prev_frame
Definition: wavpack.c:111
uint8_t buf[FIFOSIZE]
Definition: dsd.h:43
common internal api header.
#define DOWN
Definition: wavpack.c:48
if(ret< 0)
Definition: vf_mcdeint.c:279
#define bit(string, value)
Definition: cbs_mpeg2.c:58
static double c[64]
#define WV_FLT_ZERO_SENT
Definition: wavpack.h:54
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
int32_t fltr3
Definition: wavpack.c:437
#define INC_MED(n)
Definition: wavpack.h:105
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
GetBitContext gb_extra_bits
Definition: wavpack.c:75
void * priv_data
Definition: avcodec.h:553
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:668
int channels
number of audio channels
Definition: avcodec.h:1187
static const struct PPFilter filters[]
Definition: postprocess.c:134
AVFrame * frame
Definition: wavpack.c:110
AVCodecContext * avctx
Definition: wavpack.c:101
#define WV_MAX_SAMPLES
Definition: wavpack.h:57
#define DEC_MED(n)
Definition: wavpack.h:104
uint16_t summed_probabilities[MAX_HISTORY_BINS][256]
Definition: wavpack.c:93
#define PTABLE_MASK
Definition: wavpack.c:45
Definition: wavpack.h:85
GetBitContext gb
Definition: wavpack.c:72
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:67
#define av_always_inline
Definition: attributes.h:45
static av_cold int wv_alloc_frame_context(WavpackContext *c)
Definition: wavpack.c:975
uint8_t value_lookup_buffer[MAX_HISTORY_BINS *MAX_BIN_BYTES]
Definition: wavpack.c:92
static av_cold int wavpack_decode_init(AVCodecContext *avctx)
Definition: wavpack.c:1046
#define FFSWAP(type, a, b)
Definition: common.h:99
#define WV_MAX_FRAME_DECODERS
Definition: wavpack.c:98
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:347
#define AV_CH_LAYOUT_MONO
enum AVCodecID id
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:509
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:366
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
for(j=16;j >0;--j)
#define t2
Definition: regdef.h:30
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:415
AVBufferRef * dsd_ref
Definition: wavpack.c:114
#define PRECISION_USE
Definition: wavpack.c:53