FFmpeg
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 << 1) - 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  while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
502  value = (value << 8) | bytestream2_get_byte(&s->gbyte);
503  high = (high << 8) | 0xff;
504  low <<= 8;
505  }
506 
507  sp[0].value += sp[0].fltr6 * 8;
508  sp[0].byte = (sp[0].byte << 1) | (sp[0].fltr0 & 1);
509  sp[0].factor += (((sp[0].value ^ sp[0].fltr0) >> 31) | 1) &
510  ((sp[0].value ^ (sp[0].value - (sp[0].fltr6 * 16))) >> 31);
511  sp[0].fltr1 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr1) >> 6;
512  sp[0].fltr2 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr2) >> 4;
513  sp[0].fltr3 += (sp[0].fltr2 - sp[0].fltr3) >> 4;
514  sp[0].fltr4 += (sp[0].fltr3 - sp[0].fltr4) >> 4;
515  sp[0].value = (sp[0].fltr4 - sp[0].fltr5) >> 4;
516  sp[0].fltr5 += sp[0].value;
517  sp[0].fltr6 += (sp[0].value - sp[0].fltr6) >> 3;
518  sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
519 
520  if (!stereo)
521  continue;
522 
523  pp = s->ptable + ((sp[1].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
524  split = low + ((high - low) >> 8) * (*pp >> 16);
525 
526  if (value <= split) {
527  high = split;
528  *pp += (UP - *pp) >> DECAY;
529  sp[1].fltr0 = -1;
530  } else {
531  low = split + 1;
532  *pp += (DOWN - *pp) >> DECAY;
533  sp[1].fltr0 = 0;
534  }
535 
536  while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
537  value = (value << 8) | bytestream2_get_byte(&s->gbyte);
538  high = (high << 8) | 0xff;
539  low <<= 8;
540  }
541 
542  sp[1].value += sp[1].fltr6 * 8;
543  sp[1].byte = (sp[1].byte << 1) | (sp[1].fltr0 & 1);
544  sp[1].factor += (((sp[1].value ^ sp[1].fltr0) >> 31) | 1) &
545  ((sp[1].value ^ (sp[1].value - (sp[1].fltr6 * 16))) >> 31);
546  sp[1].fltr1 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr1) >> 6;
547  sp[1].fltr2 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr2) >> 4;
548  sp[1].fltr3 += (sp[1].fltr2 - sp[1].fltr3) >> 4;
549  sp[1].fltr4 += (sp[1].fltr3 - sp[1].fltr4) >> 4;
550  sp[1].value = (sp[1].fltr4 - sp[1].fltr5) >> 4;
551  sp[1].fltr5 += sp[1].value;
552  sp[1].fltr6 += (sp[1].value - sp[1].fltr6) >> 3;
553  sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
554  }
555 
556  checksum += (checksum << 1) + (*dst_l = sp[0].byte & 0xff);
557  sp[0].factor -= (sp[0].factor + 512) >> 10;
558  dst_l += 4;
559 
560  if (stereo) {
561  checksum += (checksum << 1) + (*dst_r = filters[1].byte & 0xff);
562  filters[1].factor -= (filters[1].factor + 512) >> 10;
563  dst_r += 4;
564  }
565  }
566 
567  if (wv_check_crc(s, checksum, 0)) {
569  return AVERROR_INVALIDDATA;
570 
571  memset(dst_left, 0x69, s->samples * 4);
572 
573  if (dst_r)
574  memset(dst_right, 0x69, s->samples * 4);
575  }
576 
577  return 0;
578 }
579 
580 static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
581 {
582  uint8_t *dst_l = dst_left, *dst_r = dst_right;
583  uint8_t history_bits, max_probability;
584  int total_summed_probabilities = 0;
585  int total_samples = s->samples;
586  uint8_t *vlb = s->value_lookup_buffer;
587  int history_bins, p0, p1, chan;
588  uint32_t checksum = 0xFFFFFFFF;
589  uint32_t low, high, value;
590 
592  return AVERROR_INVALIDDATA;
593 
594  history_bits = bytestream2_get_byte(&s->gbyte);
595 
596  if (!bytestream2_get_bytes_left(&s->gbyte) || history_bits > MAX_HISTORY_BITS)
597  return AVERROR_INVALIDDATA;
598 
599  history_bins = 1 << history_bits;
600  max_probability = bytestream2_get_byte(&s->gbyte);
601 
602  if (max_probability < 0xff) {
603  uint8_t *outptr = (uint8_t *)s->probabilities;
604  uint8_t *outend = outptr + sizeof(*s->probabilities) * history_bins;
605 
606  while (outptr < outend && bytestream2_get_bytes_left(&s->gbyte)) {
607  int code = bytestream2_get_byte(&s->gbyte);
608 
609  if (code > max_probability) {
610  int zcount = code - max_probability;
611 
612  while (outptr < outend && zcount--)
613  *outptr++ = 0;
614  } else if (code) {
615  *outptr++ = code;
616  }
617  else {
618  break;
619  }
620  }
621 
622  if (outptr < outend ||
623  (bytestream2_get_bytes_left(&s->gbyte) && bytestream2_get_byte(&s->gbyte)))
624  return AVERROR_INVALIDDATA;
625  } else if (bytestream2_get_bytes_left(&s->gbyte) > (int)sizeof(*s->probabilities) * history_bins) {
627  sizeof(*s->probabilities) * history_bins);
628  } else {
629  return AVERROR_INVALIDDATA;
630  }
631 
632  for (p0 = 0; p0 < history_bins; p0++) {
633  int32_t sum_values = 0;
634 
635  for (int i = 0; i < 256; i++)
636  s->summed_probabilities[p0][i] = sum_values += s->probabilities[p0][i];
637 
638  if (sum_values) {
639  total_summed_probabilities += sum_values;
640 
641  if (total_summed_probabilities > history_bins * MAX_BIN_BYTES)
642  return AVERROR_INVALIDDATA;
643 
644  s->value_lookup[p0] = vlb;
645 
646  for (int i = 0; i < 256; i++) {
647  int c = s->probabilities[p0][i];
648 
649  while (c--)
650  *vlb++ = i;
651  }
652  }
653  }
654 
655  if (bytestream2_get_bytes_left(&s->gbyte) < 4)
656  return AVERROR_INVALIDDATA;
657 
658  chan = p0 = p1 = 0;
659  low = 0; high = 0xffffffff;
660  value = bytestream2_get_be32(&s->gbyte);
661 
662  if (dst_r)
663  total_samples *= 2;
664 
665  while (total_samples--) {
666  unsigned int mult, index, code;
667 
668  if (!s->summed_probabilities[p0][255])
669  return AVERROR_INVALIDDATA;
670 
671  mult = (high - low) / s->summed_probabilities[p0][255];
672 
673  if (!mult) {
674  if (bytestream2_get_bytes_left(&s->gbyte) >= 4)
675  value = bytestream2_get_be32(&s->gbyte);
676 
677  low = 0;
678  high = 0xffffffff;
679  mult = high / s->summed_probabilities[p0][255];
680 
681  if (!mult)
682  return AVERROR_INVALIDDATA;
683  }
684 
685  index = (value - low) / mult;
686 
687  if (index >= s->summed_probabilities[p0][255])
688  return AVERROR_INVALIDDATA;
689 
690  if (!dst_r) {
691  if ((*dst_l = code = s->value_lookup[p0][index]))
692  low += s->summed_probabilities[p0][code-1] * mult;
693 
694  dst_l += 4;
695  } else {
696  if ((code = s->value_lookup[p0][index]))
697  low += s->summed_probabilities[p0][code-1] * mult;
698 
699  if (chan) {
700  *dst_r = code;
701  dst_r += 4;
702  }
703  else {
704  *dst_l = code;
705  dst_l += 4;
706  }
707 
708  chan ^= 1;
709  }
710 
711  high = low + s->probabilities[p0][code] * mult - 1;
712  checksum += (checksum << 1) + code;
713 
714  if (!dst_r) {
715  p0 = code & (history_bins-1);
716  } else {
717  p0 = p1;
718  p1 = code & (history_bins-1);
719  }
720 
721  while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
722  value = (value << 8) | bytestream2_get_byte(&s->gbyte);
723  high = (high << 8) | 0xff;
724  low <<= 8;
725  }
726  }
727 
728  if (wv_check_crc(s, checksum, 0)) {
730  return AVERROR_INVALIDDATA;
731 
732  memset(dst_left, 0x69, s->samples * 4);
733 
734  if (dst_r)
735  memset(dst_right, 0x69, s->samples * 4);
736  }
737 
738  return 0;
739 }
740 
741 static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
742 {
743  uint8_t *dst_l = dst_left, *dst_r = dst_right;
744  int total_samples = s->samples;
745  uint32_t checksum = 0xFFFFFFFF;
746 
747  if (bytestream2_get_bytes_left(&s->gbyte) != total_samples * (dst_r ? 2 : 1))
748  return AVERROR_INVALIDDATA;
749 
750  while (total_samples--) {
751  checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&s->gbyte));
752  dst_l += 4;
753 
754  if (dst_r) {
755  checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&s->gbyte));
756  dst_r += 4;
757  }
758  }
759 
760  if (wv_check_crc(s, checksum, 0)) {
762  return AVERROR_INVALIDDATA;
763 
764  memset(dst_left, 0x69, s->samples * 4);
765 
766  if (dst_r)
767  memset(dst_right, 0x69, s->samples * 4);
768  }
769 
770  return 0;
771 }
772 
774  void *dst_l, void *dst_r, const int type)
775 {
776  int i, j, count = 0;
777  int last, t;
778  int A, B, L, L2, R, R2;
779  int pos = 0;
780  uint32_t crc = 0xFFFFFFFF;
781  uint32_t crc_extra_bits = 0xFFFFFFFF;
782  int16_t *dst16_l = dst_l;
783  int16_t *dst16_r = dst_r;
784  int32_t *dst32_l = dst_l;
785  int32_t *dst32_r = dst_r;
786  float *dstfl_l = dst_l;
787  float *dstfl_r = dst_r;
788 
789  s->one = s->zero = s->zeroes = 0;
790  do {
791  L = wv_get_value(s, gb, 0, &last);
792  if (last)
793  break;
794  R = wv_get_value(s, gb, 1, &last);
795  if (last)
796  break;
797  for (i = 0; i < s->terms; i++) {
798  t = s->decorr[i].value;
799  if (t > 0) {
800  if (t > 8) {
801  if (t & 1) {
802  A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
803  B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
804  } else {
805  A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
806  B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
807  }
808  s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
809  s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
810  j = 0;
811  } else {
812  A = s->decorr[i].samplesA[pos];
813  B = s->decorr[i].samplesB[pos];
814  j = (pos + t) & 7;
815  }
816  if (type != AV_SAMPLE_FMT_S16P) {
817  L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
818  R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
819  } else {
820  L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
821  R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
822  }
823  if (A && L)
824  s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
825  if (B && R)
826  s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
827  s->decorr[i].samplesA[j] = L = L2;
828  s->decorr[i].samplesB[j] = R = R2;
829  } else if (t == -1) {
830  if (type != AV_SAMPLE_FMT_S16P)
831  L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
832  else
833  L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
834  UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
835  L = L2;
836  if (type != AV_SAMPLE_FMT_S16P)
837  R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
838  else
839  R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
840  UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
841  R = R2;
842  s->decorr[i].samplesA[0] = R;
843  } else {
844  if (type != AV_SAMPLE_FMT_S16P)
845  R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
846  else
847  R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
848  UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
849  R = R2;
850 
851  if (t == -3) {
852  R2 = s->decorr[i].samplesA[0];
853  s->decorr[i].samplesA[0] = R;
854  }
855 
856  if (type != AV_SAMPLE_FMT_S16P)
857  L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
858  else
859  L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
860  UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
861  L = L2;
862  s->decorr[i].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) {
892  int size = av_get_bytes_per_sample(type);
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  t = s->decorr[i].value;
925  if (t > 8) {
926  if (t & 1)
927  A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
928  else
929  A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
930  s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
931  j = 0;
932  } else {
933  A = s->decorr[i].samplesA[pos];
934  j = (pos + t) & 7;
935  }
936  if (type != AV_SAMPLE_FMT_S16P)
937  S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
938  else
939  S = T + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
940  if (A && T)
941  s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
942  s->decorr[i].samplesA[j] = T = S;
943  }
944  pos = (pos + 1) & 7;
945  crc = crc * 3 + S;
946 
947  if (type == AV_SAMPLE_FMT_FLTP) {
948  *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
949  } else if (type == AV_SAMPLE_FMT_S32P) {
950  *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
951  } else {
952  *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
953  }
954  count++;
955  } while (!last && count < s->samples);
956 
957  if (last && count < s->samples) {
958  int size = av_get_bytes_per_sample(type);
959  memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
960  }
961 
963  int ret = wv_check_crc(s, crc, crc_extra_bits);
964  if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
965  return ret;
966  }
967 
968  return 0;
969 }
970 
972 {
974  return -1;
975 
976  c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
977  if (!c->fdec[c->fdec_num])
978  return -1;
979  c->fdec_num++;
980  c->fdec[c->fdec_num - 1]->avctx = c->avctx;
981 
982  return 0;
983 }
984 
986 {
987  int i;
988 
989  s->dsdctx = NULL;
990  s->dsd_channels = 0;
992 
993  if (!channels)
994  return 0;
995 
996  if (channels > INT_MAX / sizeof(*s->dsdctx))
997  return AVERROR(EINVAL);
998 
999  s->dsd_ref = av_buffer_allocz(channels * sizeof(*s->dsdctx));
1000  if (!s->dsd_ref)
1001  return AVERROR(ENOMEM);
1002  s->dsdctx = (DSDContext*)s->dsd_ref->data;
1003  s->dsd_channels = channels;
1004 
1005  for (i = 0; i < channels; i++)
1006  memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
1007 
1008  return 0;
1009 }
1010 
1011 #if HAVE_THREADS
1012 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1013 {
1014  WavpackContext *fsrc = src->priv_data;
1015  WavpackContext *fdst = dst->priv_data;
1016  int ret;
1017 
1018  if (dst == src)
1019  return 0;
1020 
1021  ff_thread_release_buffer(dst, &fdst->curr_frame);
1022  if (fsrc->curr_frame.f->data[0]) {
1023  if ((ret = ff_thread_ref_frame(&fdst->curr_frame, &fsrc->curr_frame)) < 0)
1024  return ret;
1025  }
1026 
1027  av_buffer_unref(&fdst->dsd_ref);
1028  fdst->dsdctx = NULL;
1029  fdst->dsd_channels = 0;
1030  if (fsrc->dsd_ref) {
1031  fdst->dsd_ref = av_buffer_ref(fsrc->dsd_ref);
1032  if (!fdst->dsd_ref)
1033  return AVERROR(ENOMEM);
1034  fdst->dsdctx = (DSDContext*)fdst->dsd_ref->data;
1035  fdst->dsd_channels = fsrc->dsd_channels;
1036  }
1037 
1038  return 0;
1039 }
1040 #endif
1041 
1043 {
1044  WavpackContext *s = avctx->priv_data;
1045 
1046  s->avctx = avctx;
1047 
1048  s->fdec_num = 0;
1049 
1050  s->curr_frame.f = av_frame_alloc();
1051  s->prev_frame.f = av_frame_alloc();
1052 
1053  if (!s->curr_frame.f || !s->prev_frame.f)
1054  return AVERROR(ENOMEM);
1055 
1056  ff_init_dsd_data();
1057 
1058  return 0;
1059 }
1060 
1062 {
1063  WavpackContext *s = avctx->priv_data;
1064 
1065  for (int i = 0; i < s->fdec_num; i++)
1066  av_freep(&s->fdec[i]);
1067  s->fdec_num = 0;
1068 
1070  av_frame_free(&s->curr_frame.f);
1071 
1073  av_frame_free(&s->prev_frame.f);
1074 
1075  av_buffer_unref(&s->dsd_ref);
1076 
1077  return 0;
1078 }
1079 
1080 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
1081  const uint8_t *buf, int buf_size)
1082 {
1083  WavpackContext *wc = avctx->priv_data;
1086  enum AVSampleFormat sample_fmt;
1087  void *samples_l = NULL, *samples_r = NULL;
1088  int ret;
1089  int got_terms = 0, got_weights = 0, got_samples = 0,
1090  got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1091  int got_dsd = 0;
1092  int i, j, id, size, ssize, weights, t;
1093  int bpp, chan = 0, orig_bpp, sample_rate = 0, rate_x = 1, dsd_mode = 0;
1094  int multiblock;
1095  uint64_t chmask = 0;
1096 
1097  if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
1098  av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
1099  return AVERROR_INVALIDDATA;
1100  }
1101 
1102  s = wc->fdec[block_no];
1103  if (!s) {
1104  av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
1105  block_no);
1106  return AVERROR_INVALIDDATA;
1107  }
1108 
1109  memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
1110  memset(s->ch, 0, sizeof(s->ch));
1111  s->extra_bits = 0;
1112  s->and = s->or = s->shift = 0;
1113  s->got_extra_bits = 0;
1114 
1115  bytestream2_init(&gb, buf, buf_size);
1116 
1117  s->samples = bytestream2_get_le32(&gb);
1118  if (s->samples != wc->samples) {
1119  av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
1120  "a sequence: %d and %d\n", wc->samples, s->samples);
1121  return AVERROR_INVALIDDATA;
1122  }
1123  s->frame_flags = bytestream2_get_le32(&gb);
1124 
1125  if (s->frame_flags & (WV_FLOAT_DATA | WV_DSD_DATA))
1126  sample_fmt = AV_SAMPLE_FMT_FLTP;
1127  else if ((s->frame_flags & 0x03) <= 1)
1128  sample_fmt = AV_SAMPLE_FMT_S16P;
1129  else
1130  sample_fmt = AV_SAMPLE_FMT_S32P;
1131 
1132  bpp = av_get_bytes_per_sample(sample_fmt);
1133  orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
1134  multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
1135 
1136  s->stereo = !(s->frame_flags & WV_MONO);
1137  s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
1138  s->joint = s->frame_flags & WV_JOINT_STEREO;
1139  s->hybrid = s->frame_flags & WV_HYBRID_MODE;
1141  s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
1142  if (s->post_shift < 0 || s->post_shift > 31) {
1143  return AVERROR_INVALIDDATA;
1144  }
1145  s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
1146  s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
1147  s->CRC = bytestream2_get_le32(&gb);
1148 
1149  // parse metadata blocks
1150  while (bytestream2_get_bytes_left(&gb)) {
1151  id = bytestream2_get_byte(&gb);
1152  size = bytestream2_get_byte(&gb);
1153  if (id & WP_IDF_LONG)
1154  size |= (bytestream2_get_le16u(&gb)) << 8;
1155  size <<= 1; // size is specified in words
1156  ssize = size;
1157  if (id & WP_IDF_ODD)
1158  size--;
1159  if (size < 0) {
1160  av_log(avctx, AV_LOG_ERROR,
1161  "Got incorrect block %02X with size %i\n", id, size);
1162  break;
1163  }
1164  if (bytestream2_get_bytes_left(&gb) < ssize) {
1165  av_log(avctx, AV_LOG_ERROR,
1166  "Block size %i is out of bounds\n", size);
1167  break;
1168  }
1169  switch (id & WP_IDF_MASK) {
1170  case WP_ID_DECTERMS:
1171  if (size > MAX_TERMS) {
1172  av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
1173  s->terms = 0;
1174  bytestream2_skip(&gb, ssize);
1175  continue;
1176  }
1177  s->terms = size;
1178  for (i = 0; i < s->terms; i++) {
1179  uint8_t val = bytestream2_get_byte(&gb);
1180  s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
1181  s->decorr[s->terms - i - 1].delta = val >> 5;
1182  }
1183  got_terms = 1;
1184  break;
1185  case WP_ID_DECWEIGHTS:
1186  if (!got_terms) {
1187  av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1188  continue;
1189  }
1190  weights = size >> s->stereo_in;
1191  if (weights > MAX_TERMS || weights > s->terms) {
1192  av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
1193  bytestream2_skip(&gb, ssize);
1194  continue;
1195  }
1196  for (i = 0; i < weights; i++) {
1197  t = (int8_t)bytestream2_get_byte(&gb);
1198  s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
1199  if (s->decorr[s->terms - i - 1].weightA > 0)
1200  s->decorr[s->terms - i - 1].weightA +=
1201  (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
1202  if (s->stereo_in) {
1203  t = (int8_t)bytestream2_get_byte(&gb);
1204  s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
1205  if (s->decorr[s->terms - i - 1].weightB > 0)
1206  s->decorr[s->terms - i - 1].weightB +=
1207  (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
1208  }
1209  }
1210  got_weights = 1;
1211  break;
1212  case WP_ID_DECSAMPLES:
1213  if (!got_terms) {
1214  av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1215  continue;
1216  }
1217  t = 0;
1218  for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
1219  if (s->decorr[i].value > 8) {
1220  s->decorr[i].samplesA[0] =
1221  wp_exp2(bytestream2_get_le16(&gb));
1222  s->decorr[i].samplesA[1] =
1223  wp_exp2(bytestream2_get_le16(&gb));
1224 
1225  if (s->stereo_in) {
1226  s->decorr[i].samplesB[0] =
1227  wp_exp2(bytestream2_get_le16(&gb));
1228  s->decorr[i].samplesB[1] =
1229  wp_exp2(bytestream2_get_le16(&gb));
1230  t += 4;
1231  }
1232  t += 4;
1233  } else if (s->decorr[i].value < 0) {
1234  s->decorr[i].samplesA[0] =
1235  wp_exp2(bytestream2_get_le16(&gb));
1236  s->decorr[i].samplesB[0] =
1237  wp_exp2(bytestream2_get_le16(&gb));
1238  t += 4;
1239  } else {
1240  for (j = 0; j < s->decorr[i].value; j++) {
1241  s->decorr[i].samplesA[j] =
1242  wp_exp2(bytestream2_get_le16(&gb));
1243  if (s->stereo_in) {
1244  s->decorr[i].samplesB[j] =
1245  wp_exp2(bytestream2_get_le16(&gb));
1246  }
1247  }
1248  t += s->decorr[i].value * 2 * (s->stereo_in + 1);
1249  }
1250  }
1251  got_samples = 1;
1252  break;
1253  case WP_ID_ENTROPY:
1254  if (size != 6 * (s->stereo_in + 1)) {
1255  av_log(avctx, AV_LOG_ERROR,
1256  "Entropy vars size should be %i, got %i.\n",
1257  6 * (s->stereo_in + 1), size);
1258  bytestream2_skip(&gb, ssize);
1259  continue;
1260  }
1261  for (j = 0; j <= s->stereo_in; j++)
1262  for (i = 0; i < 3; i++) {
1263  s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
1264  }
1265  got_entropy = 1;
1266  break;
1267  case WP_ID_HYBRID:
1268  if (s->hybrid_bitrate) {
1269  for (i = 0; i <= s->stereo_in; i++) {
1270  s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
1271  size -= 2;
1272  }
1273  }
1274  for (i = 0; i < (s->stereo_in + 1); i++) {
1275  s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
1276  size -= 2;
1277  }
1278  if (size > 0) {
1279  for (i = 0; i < (s->stereo_in + 1); i++) {
1280  s->ch[i].bitrate_delta =
1281  wp_exp2((int16_t)bytestream2_get_le16(&gb));
1282  }
1283  } else {
1284  for (i = 0; i < (s->stereo_in + 1); i++)
1285  s->ch[i].bitrate_delta = 0;
1286  }
1287  got_hybrid = 1;
1288  break;
1289  case WP_ID_INT32INFO: {
1290  uint8_t val[4];
1291  if (size != 4) {
1292  av_log(avctx, AV_LOG_ERROR,
1293  "Invalid INT32INFO, size = %i\n",
1294  size);
1295  bytestream2_skip(&gb, ssize - 4);
1296  continue;
1297  }
1298  bytestream2_get_buffer(&gb, val, 4);
1299  if (val[0] > 30) {
1300  av_log(avctx, AV_LOG_ERROR,
1301  "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
1302  continue;
1303  } else if (val[0]) {
1304  s->extra_bits = val[0];
1305  } else if (val[1]) {
1306  s->shift = val[1];
1307  } else if (val[2]) {
1308  s->and = s->or = 1;
1309  s->shift = val[2];
1310  } else if (val[3]) {
1311  s->and = 1;
1312  s->shift = val[3];
1313  }
1314  if (s->shift > 31) {
1315  av_log(avctx, AV_LOG_ERROR,
1316  "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
1317  s->and = s->or = s->shift = 0;
1318  continue;
1319  }
1320  /* original WavPack decoder forces 32-bit lossy sound to be treated
1321  * as 24-bit one in order to have proper clipping */
1322  if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
1323  s->post_shift += 8;
1324  s->shift -= 8;
1325  s->hybrid_maxclip >>= 8;
1326  s->hybrid_minclip >>= 8;
1327  }
1328  break;
1329  }
1330  case WP_ID_FLOATINFO:
1331  if (size != 4) {
1332  av_log(avctx, AV_LOG_ERROR,
1333  "Invalid FLOATINFO, size = %i\n", size);
1334  bytestream2_skip(&gb, ssize);
1335  continue;
1336  }
1337  s->float_flag = bytestream2_get_byte(&gb);
1338  s->float_shift = bytestream2_get_byte(&gb);
1339  s->float_max_exp = bytestream2_get_byte(&gb);
1340  if (s->float_shift > 31) {
1341  av_log(avctx, AV_LOG_ERROR,
1342  "Invalid FLOATINFO, shift = %d (> 31)\n", s->float_shift);
1343  s->float_shift = 0;
1344  continue;
1345  }
1346  got_float = 1;
1347  bytestream2_skip(&gb, 1);
1348  break;
1349  case WP_ID_DATA:
1350  if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
1351  return ret;
1352  bytestream2_skip(&gb, size);
1353  got_pcm = 1;
1354  break;
1355  case WP_ID_DSD_DATA:
1356  if (size < 2) {
1357  av_log(avctx, AV_LOG_ERROR, "Invalid DSD_DATA, size = %i\n",
1358  size);
1359  bytestream2_skip(&gb, ssize);
1360  continue;
1361  }
1362  rate_x = bytestream2_get_byte(&gb);
1363  if (rate_x > 30)
1364  return AVERROR_INVALIDDATA;
1365  rate_x = 1 << rate_x;
1366  dsd_mode = bytestream2_get_byte(&gb);
1367  if (dsd_mode && dsd_mode != 1 && dsd_mode != 3) {
1368  av_log(avctx, AV_LOG_ERROR, "Invalid DSD encoding mode: %d\n",
1369  dsd_mode);
1370  return AVERROR_INVALIDDATA;
1371  }
1372  bytestream2_init(&s->gbyte, gb.buffer, size-2);
1373  bytestream2_skip(&gb, size-2);
1374  got_dsd = 1;
1375  break;
1376  case WP_ID_EXTRABITS:
1377  if (size <= 4) {
1378  av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
1379  size);
1380  bytestream2_skip(&gb, size);
1381  continue;
1382  }
1383  if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
1384  return ret;
1386  bytestream2_skip(&gb, size);
1387  s->got_extra_bits = 1;
1388  break;
1389  case WP_ID_CHANINFO:
1390  if (size <= 1) {
1391  av_log(avctx, AV_LOG_ERROR,
1392  "Insufficient channel information\n");
1393  return AVERROR_INVALIDDATA;
1394  }
1395  chan = bytestream2_get_byte(&gb);
1396  switch (size - 2) {
1397  case 0:
1398  chmask = bytestream2_get_byte(&gb);
1399  break;
1400  case 1:
1401  chmask = bytestream2_get_le16(&gb);
1402  break;
1403  case 2:
1404  chmask = bytestream2_get_le24(&gb);
1405  break;
1406  case 3:
1407  chmask = bytestream2_get_le32(&gb);
1408  break;
1409  case 4:
1410  size = bytestream2_get_byte(&gb);
1411  chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1412  chan += 1;
1413  if (avctx->channels != chan)
1414  av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1415  " instead of %i.\n", chan, avctx->channels);
1416  chmask = bytestream2_get_le24(&gb);
1417  break;
1418  case 5:
1419  size = bytestream2_get_byte(&gb);
1420  chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1421  chan += 1;
1422  if (avctx->channels != chan)
1423  av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1424  " instead of %i.\n", chan, avctx->channels);
1425  chmask = bytestream2_get_le32(&gb);
1426  break;
1427  default:
1428  av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
1429  size);
1430  chan = avctx->channels;
1431  chmask = avctx->channel_layout;
1432  }
1433  break;
1434  case WP_ID_SAMPLE_RATE:
1435  if (size != 3) {
1436  av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
1437  return AVERROR_INVALIDDATA;
1438  }
1439  sample_rate = bytestream2_get_le24(&gb);
1440  break;
1441  default:
1442  bytestream2_skip(&gb, size);
1443  }
1444  if (id & WP_IDF_ODD)
1445  bytestream2_skip(&gb, 1);
1446  }
1447 
1448  if (got_pcm) {
1449  if (!got_terms) {
1450  av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1451  return AVERROR_INVALIDDATA;
1452  }
1453  if (!got_weights) {
1454  av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1455  return AVERROR_INVALIDDATA;
1456  }
1457  if (!got_samples) {
1458  av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1459  return AVERROR_INVALIDDATA;
1460  }
1461  if (!got_entropy) {
1462  av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1463  return AVERROR_INVALIDDATA;
1464  }
1465  if (s->hybrid && !got_hybrid) {
1466  av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1467  return AVERROR_INVALIDDATA;
1468  }
1469  if (!got_float && sample_fmt == AV_SAMPLE_FMT_FLTP) {
1470  av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1471  return AVERROR_INVALIDDATA;
1472  }
1473  if (s->got_extra_bits && sample_fmt != AV_SAMPLE_FMT_FLTP) {
1474  const int size = get_bits_left(&s->gb_extra_bits);
1475  const int wanted = s->samples * s->extra_bits << s->stereo_in;
1476  if (size < wanted) {
1477  av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1478  s->got_extra_bits = 0;
1479  }
1480  }
1481  }
1482 
1483  if (!got_pcm && !got_dsd) {
1484  av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1485  return AVERROR_INVALIDDATA;
1486  }
1487 
1488  if ((got_pcm && wc->modulation != MODULATION_PCM) ||
1489  (got_dsd && wc->modulation != MODULATION_DSD)) {
1490  av_log(avctx, AV_LOG_ERROR, "Invalid PCM/DSD mix encountered\n");
1491  return AVERROR_INVALIDDATA;
1492  }
1493 
1494  if (!wc->ch_offset) {
1495  int new_channels = avctx->channels;
1496  uint64_t new_chmask = avctx->channel_layout;
1497  int new_samplerate;
1498  int sr = (s->frame_flags >> 23) & 0xf;
1499  if (sr == 0xf) {
1500  if (!sample_rate) {
1501  av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
1502  return AVERROR_INVALIDDATA;
1503  }
1504  new_samplerate = sample_rate;
1505  } else
1506  new_samplerate = wv_rates[sr];
1507 
1508  if (new_samplerate * (uint64_t)rate_x > INT_MAX)
1509  return AVERROR_INVALIDDATA;
1510  new_samplerate *= rate_x;
1511 
1512  if (multiblock) {
1513  if (chan)
1514  new_channels = chan;
1515  if (chmask)
1516  new_chmask = chmask;
1517  } else {
1518  new_channels = s->stereo ? 2 : 1;
1519  new_chmask = s->stereo ? AV_CH_LAYOUT_STEREO :
1521  }
1522 
1523  if (new_chmask &&
1524  av_get_channel_layout_nb_channels(new_chmask) != new_channels) {
1525  av_log(avctx, AV_LOG_ERROR, "Channel mask does not match the channel count\n");
1526  return AVERROR_INVALIDDATA;
1527  }
1528 
1529  /* clear DSD state if stream properties change */
1530  if (new_channels != wc->dsd_channels ||
1531  new_chmask != avctx->channel_layout ||
1532  new_samplerate != avctx->sample_rate ||
1533  !!got_dsd != !!wc->dsdctx) {
1534  ret = wv_dsd_reset(wc, got_dsd ? new_channels : 0);
1535  if (ret < 0) {
1536  av_log(avctx, AV_LOG_ERROR, "Error reinitializing the DSD context\n");
1537  return ret;
1538  }
1539  ff_thread_release_buffer(avctx, &wc->curr_frame);
1540  }
1541  avctx->channels = new_channels;
1542  avctx->channel_layout = new_chmask;
1543  avctx->sample_rate = new_samplerate;
1544  avctx->sample_fmt = sample_fmt;
1545  avctx->bits_per_raw_sample = orig_bpp;
1546 
1547  ff_thread_release_buffer(avctx, &wc->prev_frame);
1549 
1550  /* get output buffer */
1551  wc->curr_frame.f->nb_samples = s->samples;
1552  if ((ret = ff_thread_get_buffer(avctx, &wc->curr_frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1553  return ret;
1554 
1555  wc->frame = wc->curr_frame.f;
1556  ff_thread_finish_setup(avctx);
1557  }
1558 
1559  if (wc->ch_offset + s->stereo >= avctx->channels) {
1560  av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
1561  return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
1562  }
1563 
1564  samples_l = wc->frame->extended_data[wc->ch_offset];
1565  if (s->stereo)
1566  samples_r = wc->frame->extended_data[wc->ch_offset + 1];
1567 
1568  wc->ch_offset += 1 + s->stereo;
1569 
1570  if (s->stereo_in) {
1571  if (got_dsd) {
1572  if (dsd_mode == 3) {
1573  ret = wv_unpack_dsd_high(s, samples_l, samples_r);
1574  } else if (dsd_mode == 1) {
1575  ret = wv_unpack_dsd_fast(s, samples_l, samples_r);
1576  } else {
1577  ret = wv_unpack_dsd_copy(s, samples_l, samples_r);
1578  }
1579  } else {
1580  ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1581  }
1582  if (ret < 0)
1583  return ret;
1584  } else {
1585  if (got_dsd) {
1586  if (dsd_mode == 3) {
1587  ret = wv_unpack_dsd_high(s, samples_l, NULL);
1588  } else if (dsd_mode == 1) {
1589  ret = wv_unpack_dsd_fast(s, samples_l, NULL);
1590  } else {
1591  ret = wv_unpack_dsd_copy(s, samples_l, NULL);
1592  }
1593  } else {
1594  ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1595  }
1596  if (ret < 0)
1597  return ret;
1598 
1599  if (s->stereo)
1600  memcpy(samples_r, samples_l, bpp * s->samples);
1601  }
1602 
1603  return 0;
1604 }
1605 
1607 {
1608  WavpackContext *s = avctx->priv_data;
1609 
1610  wv_dsd_reset(s, 0);
1611 }
1612 
1613 static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
1614 {
1615  WavpackContext *s = avctx->priv_data;
1616  AVFrame *frame = frmptr;
1617 
1618  ff_dsd2pcm_translate (&s->dsdctx [jobnr], s->samples, 0,
1619  (uint8_t *)frame->extended_data[jobnr], 4,
1620  (float *)frame->extended_data[jobnr], 1);
1621 
1622  return 0;
1623 }
1624 
1626  int *got_frame_ptr, AVPacket *avpkt)
1627 {
1628  WavpackContext *s = avctx->priv_data;
1629  const uint8_t *buf = avpkt->data;
1630  int buf_size = avpkt->size;
1631  int frame_size, ret, frame_flags;
1632 
1633  if (avpkt->size <= WV_HEADER_SIZE)
1634  return AVERROR_INVALIDDATA;
1635 
1636  s->frame = NULL;
1637  s->block = 0;
1638  s->ch_offset = 0;
1639 
1640  /* determine number of samples */
1641  s->samples = AV_RL32(buf + 20);
1642  frame_flags = AV_RL32(buf + 24);
1643  if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1644  av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1645  s->samples);
1646  return AVERROR_INVALIDDATA;
1647  }
1648 
1649  s->modulation = (frame_flags & WV_DSD_DATA) ? MODULATION_DSD : MODULATION_PCM;
1650 
1651  while (buf_size > WV_HEADER_SIZE) {
1652  frame_size = AV_RL32(buf + 4) - 12;
1653  buf += 20;
1654  buf_size -= 20;
1655  if (frame_size <= 0 || frame_size > buf_size) {
1656  av_log(avctx, AV_LOG_ERROR,
1657  "Block %d has invalid size (size %d vs. %d bytes left)\n",
1658  s->block, frame_size, buf_size);
1659  ret = AVERROR_INVALIDDATA;
1660  goto error;
1661  }
1662  if ((ret = wavpack_decode_block(avctx, s->block, buf, frame_size)) < 0)
1663  goto error;
1664  s->block++;
1665  buf += frame_size;
1666  buf_size -= frame_size;
1667  }
1668 
1669  if (s->ch_offset != avctx->channels) {
1670  av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
1671  ret = AVERROR_INVALIDDATA;
1672  goto error;
1673  }
1674 
1675  ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1677 
1678  if (s->modulation == MODULATION_DSD)
1679  avctx->execute2(avctx, dsd_channel, s->frame, NULL, avctx->channels);
1680 
1681  ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1682 
1683  if ((ret = av_frame_ref(data, s->frame)) < 0)
1684  return ret;
1685 
1686  *got_frame_ptr = 1;
1687 
1688  return avpkt->size;
1689 
1690 error:
1691  if (s->frame) {
1692  ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1694  ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1695  }
1696 
1697  return ret;
1698 }
1699 
1701  .name = "wavpack",
1702  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1703  .type = AVMEDIA_TYPE_AUDIO,
1704  .id = AV_CODEC_ID_WAVPACK,
1705  .priv_data_size = sizeof(WavpackContext),
1707  .close = wavpack_decode_end,
1711  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1713  .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS,
1714 };
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:126
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
unsigned bitrate_delta
Definition: wavpack.h:99
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
static void wavpack_decode_flush(AVCodecContext *avctx)
Definition: wavpack.c:1606
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:985
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:1080
int av_log2(unsigned v)
Definition: intmath.c:26
#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
GLint GLenum type
Definition: opengl_enc.c:104
#define MAX_TERMS
Definition: wavpack.h:27
static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
Definition: wavpack.c:1613
int weightB
Definition: wavpack.h:89
GetByteContext gbyte
Definition: wavpack.c:90
#define WV_HYBRID_BITRATE
Definition: wavpack.h:42
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
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
av_cold void ff_init_dsd_data(void)
Definition: dsd.c:46
ThreadFrame curr_frame
Definition: wavpack.c:111
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before ff_thread_await_progress() has been called on them.reget_buffer() and buffer age optimizations no longer work.*The contents of buffers must not be written to after ff_thread_report_progress() has been called on them.This includes draw_edges().Porting codecs to frame threading
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:1700
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
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
Multithreading support functions.
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
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
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:1840
#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
ptrdiff_t size
Definition: opengl_enc.c:100
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:741
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 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:186
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:410
AVCodecContext * avctx
Definition: wavpack.c:67
uint8_t * value_lookup[MAX_HISTORY_BINS]
Definition: wavpack.c:95
#define WV_SINGLE_BLOCK
Definition: wavpack.h:49
const char * name
Name of the codec implementation.
Definition: codec.h:197
GLsizei count
Definition: opengl_enc.c:108
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:225
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames.The frames must then be freed with ff_thread_release_buffer().Otherwise decode directly into the user-supplied frames.Call ff_thread_report_progress() after some part of the current picture has decoded.A good place to put this is where draw_horiz_band() is called-add this if it isn't called anywhere
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:773
static const int wv_rates[16]
Definition: wavpack.h:121
#define WV_FALSE_STEREO
Definition: wavpack.h:37
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call ff_thread_finish_setup() afterwards.If some code can't be moved
static int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
Definition: wavpack.c:904
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
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
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_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
if(ret)
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:1061
#define GET_MED(n)
Definition: wavpack.h:103
sample_rate
int frame_size
Definition: mxfenc.c:2139
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
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:87
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
main external API structure.
Definition: avcodec.h:526
int32_t fltr1
Definition: wavpack.c:437
F H1 F F H1 F F F F H1<-F-------F-------F v v v H2 H3 H2^^^F-------F-------F-> H1<-F-------F-------F|||||||||F H1 F|||||||||F H1 Funavailable fullpel samples(outside the picture for example) shall be equalto the closest available fullpel sampleSmaller pel interpolation:--------------------------if diag_mc is set then points which lie on a line between 2 vertically, horizontally or diagonally adjacent halfpel points shall be interpolatedlinearly with rounding to nearest and halfway values rounded up.points which lie on 2 diagonals at the same time should only use the onediagonal not containing the fullpel point F--> O q O<--h1-> O q O<--F v\/v\/v O O O O O O O|/|\|q q q q q|/|\|O O O O O O O^/\^/\^h2--> O q O<--h3-> O q O<--h2 v\/v\/v O O O O O O O|\|/|q q q q q|\|/|O O O O O O O^/\^/\^F--> O q O<--h1-> O q O<--Fthe remaining points shall be bilinearly interpolated from theup to 4 surrounding halfpel and fullpel points, again rounding should be tonearest and halfway values rounded upcompliant Snow decoders MUST support 1-1/8 pel luma and 1/2-1/16 pel chromainterpolation at leastOverlapped block motion compensation:-------------------------------------FIXMELL band prediction:===================Each sample in the LL0 subband is predicted by the median of the left, top andleft+top-topleft samples, samples outside the subband shall be considered tobe 0.To reverse this prediction in the decoder apply the following.for(y=0;y< height;y++){for(x=0;x< width;x++){sample[y][x]+=median(sample[y-1][x], sample[y][x-1], sample[y-1][x]+sample[y][x-1]-sample[y-1][x-1]);}}sample[-1][*]=sample[*][-1]=0;width, height here are the width and height of the LL0 subband not of the finalvideoDequantization:===============FIXMEWavelet Transform:==================Snow supports 2 wavelet transforms, the symmetric biorthogonal 5/3 integertransform and an integer approximation of the symmetric biorthogonal 9/7daubechies wavelet.2D IDWT(inverse discrete wavelet transform)--------------------------------------------The 2D IDWT applies a 2D filter recursively, each time combining the4 lowest frequency subbands into a single subband until only 1 subbandremains.The 2D filter is done by first applying a 1D filter in the vertical directionand then applying it in the horizontal one.------------------------------------------------------------|LL0|HL0|||||||||||||---+---|HL1||L0|H0|HL1||LL1|HL1|||||LH0|HH0|||||||||||||-------+-------|-> L1 H1 LH1 HH1 LH1 HH1 LH1 HH1 L2
Definition: snow.txt:554
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
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have update_thread_context() run it in the next thread.Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities.There will be very little speed gain at this point but it should work.If there are inter-frame dependencies
#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:84
int32_t fltr0
Definition: wavpack.c:437
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void init_ptable(int *table, int rate_i, int rate_s)
Definition: wavpack.c:416
int index
Definition: gxfenc.c:89
#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:1625
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
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
#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:580
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
#define bit(string, value)
Definition: cbs_mpeg2.c:58
#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:94
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:667
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:971
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:1042
#define FFSWAP(type, a, b)
Definition: common.h:99
#define WV_MAX_FRAME_DECODERS
Definition: wavpack.c:98
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:347
#define AV_CH_LAYOUT_MONO
enum AVCodecID id
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
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