Libav
apedec.c
Go to the documentation of this file.
1 /*
2  * Monkey's Audio lossless audio decoder
3  * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4  * based upon libdemac from Dave Chapman.
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/avassert.h"
25 #include "libavutil/opt.h"
26 #include "avcodec.h"
27 #include "dsputil.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 #include "get_bits.h"
31 #include "unary.h"
32 
38 #define MAX_CHANNELS 2
39 #define MAX_BYTESPERSAMPLE 3
40 
41 #define APE_FRAMECODE_MONO_SILENCE 1
42 #define APE_FRAMECODE_STEREO_SILENCE 3
43 #define APE_FRAMECODE_PSEUDO_STEREO 4
44 
45 #define HISTORY_SIZE 512
46 #define PREDICTOR_ORDER 8
47 
48 #define PREDICTOR_SIZE 50
49 
50 #define YDELAYA (18 + PREDICTOR_ORDER*4)
51 #define YDELAYB (18 + PREDICTOR_ORDER*3)
52 #define XDELAYA (18 + PREDICTOR_ORDER*2)
53 #define XDELAYB (18 + PREDICTOR_ORDER)
54 
55 #define YADAPTCOEFFSA 18
56 #define XADAPTCOEFFSA 14
57 #define YADAPTCOEFFSB 10
58 #define XADAPTCOEFFSB 5
59 
70 };
73 #define APE_FILTER_LEVELS 3
74 
76 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
77  { 0, 0, 0 },
78  { 16, 0, 0 },
79  { 64, 0, 0 },
80  { 32, 256, 0 },
81  { 16, 256, 1280 }
82 };
83 
86  { 0, 0, 0 },
87  { 11, 0, 0 },
88  { 11, 0, 0 },
89  { 10, 13, 0 },
90  { 11, 13, 15 }
91 };
92 
93 
95 typedef struct APEFilter {
96  int16_t *coeffs;
97  int16_t *adaptcoeffs;
98  int16_t *historybuffer;
99  int16_t *delay;
100 
101  int avg;
102 } APEFilter;
103 
104 typedef struct APERice {
105  uint32_t k;
106  uint32_t ksum;
107 } APERice;
108 
109 typedef struct APERangecoder {
110  uint32_t low;
111  uint32_t range;
112  uint32_t help;
113  unsigned int buffer;
114 } APERangecoder;
115 
117 typedef struct APEPredictor {
119 
121 
124 
125  int32_t coeffsA[2][4];
126  int32_t coeffsB[2][5];
128 
129  unsigned int sample_pos;
130 } APEPredictor;
131 
133 typedef struct APEContext {
134  AVClass *class;
137  int channels;
138  int samples;
139  int bps;
140 
143  int fset;
144  int flags;
145 
146  uint32_t CRC;
149 
154 
156 
162 
165  int data_size;
166  const uint8_t *ptr;
167 
168  int error;
169 
170  void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
171  void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
172  void (*predictor_decode_mono)(struct APEContext *ctx, int count);
173  void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
174 } APEContext;
175 
176 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
177  int32_t *decoded1, int count);
178 
179 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
180 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
181 static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
182 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
183 static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
184 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
185 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
186 static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
187 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
188 
189 static void predictor_decode_mono_3800(APEContext *ctx, int count);
190 static void predictor_decode_stereo_3800(APEContext *ctx, int count);
191 static void predictor_decode_mono_3930(APEContext *ctx, int count);
192 static void predictor_decode_stereo_3930(APEContext *ctx, int count);
193 static void predictor_decode_mono_3950(APEContext *ctx, int count);
194 static void predictor_decode_stereo_3950(APEContext *ctx, int count);
195 
196 // TODO: dsputilize
197 
199 {
200  APEContext *s = avctx->priv_data;
201  int i;
202 
203  for (i = 0; i < APE_FILTER_LEVELS; i++)
204  av_freep(&s->filterbuf[i]);
205 
207  av_freep(&s->data);
208  s->decoded_size = s->data_size = 0;
209 
210  return 0;
211 }
212 
214 {
215  APEContext *s = avctx->priv_data;
216  int i;
217 
218  if (avctx->extradata_size != 6) {
219  av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
220  return AVERROR(EINVAL);
221  }
222  if (avctx->channels > 2) {
223  av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
224  return AVERROR(EINVAL);
225  }
226  s->bps = avctx->bits_per_coded_sample;
227  switch (s->bps) {
228  case 8:
229  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
230  break;
231  case 16:
233  break;
234  case 24:
236  break;
237  default:
238  avpriv_request_sample(avctx,
239  "%d bits per coded sample", s->bps);
240  return AVERROR_PATCHWELCOME;
241  }
242  s->avctx = avctx;
243  s->channels = avctx->channels;
244  s->fileversion = AV_RL16(avctx->extradata);
245  s->compression_level = AV_RL16(avctx->extradata + 2);
246  s->flags = AV_RL16(avctx->extradata + 4);
247 
248  av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n",
249  s->compression_level, s->flags);
252  av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
253  s->compression_level);
254  return AVERROR_INVALIDDATA;
255  }
256  s->fset = s->compression_level / 1000 - 1;
257  for (i = 0; i < APE_FILTER_LEVELS; i++) {
258  if (!ape_filter_orders[s->fset][i])
259  break;
260  FF_ALLOC_OR_GOTO(avctx, s->filterbuf[i],
261  (ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4,
262  filter_alloc_fail);
263  }
264 
265  if (s->fileversion < 3860) {
268  } else if (s->fileversion < 3900) {
271  } else if (s->fileversion < 3930) {
274  } else if (s->fileversion < 3990) {
277  } else {
280  }
281 
282  if (s->fileversion < 3930) {
285  } else if (s->fileversion < 3950) {
288  } else {
291  }
292 
293  ff_dsputil_init(&s->dsp, avctx);
295 
296  return 0;
297 filter_alloc_fail:
298  ape_decode_close(avctx);
299  return AVERROR(ENOMEM);
300 }
301 
307 #define CODE_BITS 32
308 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
309 #define SHIFT_BITS (CODE_BITS - 9)
310 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
311 #define BOTTOM_VALUE (TOP_VALUE >> 8)
312 
314 static inline void range_start_decoding(APEContext *ctx)
315 {
316  ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
317  ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
318  ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
319 }
320 
322 static inline void range_dec_normalize(APEContext *ctx)
323 {
324  while (ctx->rc.range <= BOTTOM_VALUE) {
325  ctx->rc.buffer <<= 8;
326  if(ctx->ptr < ctx->data_end) {
327  ctx->rc.buffer += *ctx->ptr;
328  ctx->ptr++;
329  } else {
330  ctx->error = 1;
331  }
332  ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
333  ctx->rc.range <<= 8;
334  }
335 }
336 
343 static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
344 {
345  range_dec_normalize(ctx);
346  ctx->rc.help = ctx->rc.range / tot_f;
347  return ctx->rc.low / ctx->rc.help;
348 }
349 
355 static inline int range_decode_culshift(APEContext *ctx, int shift)
356 {
357  range_dec_normalize(ctx);
358  ctx->rc.help = ctx->rc.range >> shift;
359  return ctx->rc.low / ctx->rc.help;
360 }
361 
362 
369 static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
370 {
371  ctx->rc.low -= ctx->rc.help * lt_f;
372  ctx->rc.range = ctx->rc.help * sy_f;
373 }
374 
376 static inline int range_decode_bits(APEContext *ctx, int n)
377 {
378  int sym = range_decode_culshift(ctx, n);
379  range_decode_update(ctx, 1, sym);
380  return sym;
381 }
382 
383 
384 #define MODEL_ELEMENTS 64
385 
389 static const uint16_t counts_3970[22] = {
390  0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
391  62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
392  65450, 65469, 65480, 65487, 65491, 65493,
393 };
394 
398 static const uint16_t counts_diff_3970[21] = {
399  14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
400  1104, 677, 415, 248, 150, 89, 54, 31,
401  19, 11, 7, 4, 2,
402 };
403 
407 static const uint16_t counts_3980[22] = {
408  0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
409  64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
410  65485, 65488, 65490, 65491, 65492, 65493,
411 };
412 
416 static const uint16_t counts_diff_3980[21] = {
417  19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
418  261, 119, 65, 31, 19, 10, 6, 3,
419  3, 2, 1, 1, 1,
420 };
421 
428 static inline int range_get_symbol(APEContext *ctx,
429  const uint16_t counts[],
430  const uint16_t counts_diff[])
431 {
432  int symbol, cf;
433 
434  cf = range_decode_culshift(ctx, 16);
435 
436  if(cf > 65492){
437  symbol= cf - 65535 + 63;
438  range_decode_update(ctx, 1, cf);
439  if(cf > 65535)
440  ctx->error=1;
441  return symbol;
442  }
443  /* figure out the symbol inefficiently; a binary search would be much better */
444  for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
445 
446  range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
447 
448  return symbol;
449 } // group rangecoder
451 
452 static inline void update_rice(APERice *rice, unsigned int x)
453 {
454  int lim = rice->k ? (1 << (rice->k + 4)) : 0;
455  rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
456 
457  if (rice->ksum < lim)
458  rice->k--;
459  else if (rice->ksum >= (1 << (rice->k + 5)))
460  rice->k++;
461 }
462 
463 static inline int get_rice_ook(GetBitContext *gb, int k)
464 {
465  unsigned int x;
466 
467  x = get_unary(gb, 1, get_bits_left(gb));
468 
469  if (k)
470  x = (x << k) | get_bits(gb, k);
471 
472  return x;
473 }
474 
476  APERice *rice)
477 {
478  unsigned int x, overflow;
479 
480  overflow = get_unary(gb, 1, get_bits_left(gb));
481 
482  if (ctx->fileversion > 3880) {
483  while (overflow >= 16) {
484  overflow -= 16;
485  rice->k += 4;
486  }
487  }
488 
489  if (!rice->k)
490  x = overflow;
491  else
492  x = (overflow << rice->k) + get_bits(gb, rice->k);
493 
494  rice->ksum += x - (rice->ksum + 8 >> 4);
495  if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
496  rice->k--;
497  else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
498  rice->k++;
499 
500  /* Convert to signed */
501  if (x & 1)
502  return (x >> 1) + 1;
503  else
504  return -(x >> 1);
505 }
506 
507 static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
508 {
509  unsigned int x, overflow;
510  int tmpk;
511 
513 
514  if (overflow == (MODEL_ELEMENTS - 1)) {
515  tmpk = range_decode_bits(ctx, 5);
516  overflow = 0;
517  } else
518  tmpk = (rice->k < 1) ? 0 : rice->k - 1;
519 
520  if (tmpk <= 16 || ctx->fileversion < 3910)
521  x = range_decode_bits(ctx, tmpk);
522  else if (tmpk <= 32) {
523  x = range_decode_bits(ctx, 16);
524  x |= (range_decode_bits(ctx, tmpk - 16) << 16);
525  } else {
526  av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
527  return AVERROR_INVALIDDATA;
528  }
529  x += overflow << tmpk;
530 
531  update_rice(rice, x);
532 
533  /* Convert to signed */
534  if (x & 1)
535  return (x >> 1) + 1;
536  else
537  return -(x >> 1);
538 }
539 
540 static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
541 {
542  unsigned int x, overflow;
543  int base, pivot;
544 
545  pivot = rice->ksum >> 5;
546  if (pivot == 0)
547  pivot = 1;
548 
550 
551  if (overflow == (MODEL_ELEMENTS - 1)) {
552  overflow = range_decode_bits(ctx, 16) << 16;
553  overflow |= range_decode_bits(ctx, 16);
554  }
555 
556  if (pivot < 0x10000) {
557  base = range_decode_culfreq(ctx, pivot);
558  range_decode_update(ctx, 1, base);
559  } else {
560  int base_hi = pivot, base_lo;
561  int bbits = 0;
562 
563  while (base_hi & ~0xFFFF) {
564  base_hi >>= 1;
565  bbits++;
566  }
567  base_hi = range_decode_culfreq(ctx, base_hi + 1);
568  range_decode_update(ctx, 1, base_hi);
569  base_lo = range_decode_culfreq(ctx, 1 << bbits);
570  range_decode_update(ctx, 1, base_lo);
571 
572  base = (base_hi << bbits) + base_lo;
573  }
574 
575  x = base + overflow * pivot;
576 
577  update_rice(rice, x);
578 
579  /* Convert to signed */
580  if (x & 1)
581  return (x >> 1) + 1;
582  else
583  return -(x >> 1);
584 }
585 
587  int32_t *out, APERice *rice, int blockstodecode)
588 {
589  int i;
590  int ksummax, ksummin;
591 
592  rice->ksum = 0;
593  for (i = 0; i < 5; i++) {
594  out[i] = get_rice_ook(&ctx->gb, 10);
595  rice->ksum += out[i];
596  }
597  rice->k = av_log2(rice->ksum / 10) + 1;
598  for (; i < 64; i++) {
599  out[i] = get_rice_ook(&ctx->gb, rice->k);
600  rice->ksum += out[i];
601  rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1;
602  }
603  ksummax = 1 << rice->k + 7;
604  ksummin = rice->k ? (1 << rice->k + 6) : 0;
605  for (; i < blockstodecode; i++) {
606  out[i] = get_rice_ook(&ctx->gb, rice->k);
607  rice->ksum += out[i] - out[i - 64];
608  while (rice->ksum < ksummin) {
609  rice->k--;
610  ksummin = rice->k ? ksummin >> 1 : 0;
611  ksummax >>= 1;
612  }
613  while (rice->ksum >= ksummax) {
614  rice->k++;
615  if (rice->k > 24)
616  return;
617  ksummax <<= 1;
618  ksummin = ksummin ? ksummin << 1 : 128;
619  }
620  }
621 
622  for (i = 0; i < blockstodecode; i++) {
623  if (out[i] & 1)
624  out[i] = (out[i] >> 1) + 1;
625  else
626  out[i] = -(out[i] >> 1);
627  }
628 }
629 
630 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
631 {
632  decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
633  blockstodecode);
634 }
635 
636 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
637 {
638  decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
639  blockstodecode);
640  decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
641  blockstodecode);
642 }
643 
644 static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
645 {
646  int32_t *decoded0 = ctx->decoded[0];
647 
648  while (blockstodecode--)
649  *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
650 }
651 
652 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
653 {
654  int32_t *decoded0 = ctx->decoded[0];
655  int32_t *decoded1 = ctx->decoded[1];
656  int blocks = blockstodecode;
657 
658  while (blockstodecode--)
659  *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
660  while (blocks--)
661  *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
662 }
663 
664 static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
665 {
666  int32_t *decoded0 = ctx->decoded[0];
667 
668  while (blockstodecode--)
669  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
670 }
671 
672 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
673 {
674  int32_t *decoded0 = ctx->decoded[0];
675  int32_t *decoded1 = ctx->decoded[1];
676  int blocks = blockstodecode;
677 
678  while (blockstodecode--)
679  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
680  range_dec_normalize(ctx);
681  // because of some implementation peculiarities we need to backpedal here
682  ctx->ptr -= 1;
684  while (blocks--)
685  *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
686 }
687 
688 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
689 {
690  int32_t *decoded0 = ctx->decoded[0];
691  int32_t *decoded1 = ctx->decoded[1];
692 
693  while (blockstodecode--) {
694  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
695  *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
696  }
697 }
698 
699 static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
700 {
701  int32_t *decoded0 = ctx->decoded[0];
702 
703  while (blockstodecode--)
704  *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
705 }
706 
707 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
708 {
709  int32_t *decoded0 = ctx->decoded[0];
710  int32_t *decoded1 = ctx->decoded[1];
711 
712  while (blockstodecode--) {
713  *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
714  *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
715  }
716 }
717 
719 {
720  /* Read the CRC */
721  if (ctx->fileversion >= 3900) {
722  if (ctx->data_end - ctx->ptr < 6)
723  return AVERROR_INVALIDDATA;
724  ctx->CRC = bytestream_get_be32(&ctx->ptr);
725  } else {
726  ctx->CRC = get_bits_long(&ctx->gb, 32);
727  }
728 
729  /* Read the frame flags if they exist */
730  ctx->frameflags = 0;
731  if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
732  ctx->CRC &= ~0x80000000;
733 
734  if (ctx->data_end - ctx->ptr < 6)
735  return AVERROR_INVALIDDATA;
736  ctx->frameflags = bytestream_get_be32(&ctx->ptr);
737  }
738 
739  /* Initialize the rice structs */
740  ctx->riceX.k = 10;
741  ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
742  ctx->riceY.k = 10;
743  ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
744 
745  if (ctx->fileversion >= 3900) {
746  /* The first 8 bits of input are ignored. */
747  ctx->ptr++;
748 
750  }
751 
752  return 0;
753 }
754 
756  375,
757 };
758 
759 static const int32_t initial_coeffs_a_3800[3] = {
760  64, 115, 64,
761 };
762 
763 static const int32_t initial_coeffs_b_3800[2] = {
764  740, 0
765 };
766 
767 static const int32_t initial_coeffs_3930[4] = {
768  360, 317, -109, 98
769 };
770 
772 {
773  APEPredictor *p = &ctx->predictor;
774 
775  /* Zero the history buffers */
776  memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
777  p->buf = p->historybuffer;
778 
779  /* Initialize and zero the coefficients */
780  if (ctx->fileversion < 3930) {
782  memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
783  sizeof(initial_coeffs_fast_3320));
784  memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
785  sizeof(initial_coeffs_fast_3320));
786  } else {
787  memcpy(p->coeffsA[0], initial_coeffs_a_3800,
788  sizeof(initial_coeffs_a_3800));
789  memcpy(p->coeffsA[1], initial_coeffs_a_3800,
790  sizeof(initial_coeffs_a_3800));
791  }
792  } else {
793  memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
794  memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
795  }
796  memset(p->coeffsB, 0, sizeof(p->coeffsB));
797  if (ctx->fileversion < 3930) {
798  memcpy(p->coeffsB[0], initial_coeffs_b_3800,
799  sizeof(initial_coeffs_b_3800));
800  memcpy(p->coeffsB[1], initial_coeffs_b_3800,
801  sizeof(initial_coeffs_b_3800));
802  }
803 
804  p->filterA[0] = p->filterA[1] = 0;
805  p->filterB[0] = p->filterB[1] = 0;
806  p->lastA[0] = p->lastA[1] = 0;
807 
808  p->sample_pos = 0;
809 }
810 
812 static inline int APESIGN(int32_t x) {
813  return (x < 0) - (x > 0);
814 }
815 
817  const int decoded, const int filter,
818  const int delayA)
819 {
820  int32_t predictionA;
821 
822  p->buf[delayA] = p->lastA[filter];
823  if (p->sample_pos < 3) {
824  p->lastA[filter] = decoded;
825  p->filterA[filter] = decoded;
826  return decoded;
827  }
828 
829  predictionA = p->buf[delayA] * 2 - p->buf[delayA - 1];
830  p->lastA[filter] = decoded + (predictionA * p->coeffsA[filter][0] >> 9);
831 
832  if ((decoded ^ predictionA) > 0)
833  p->coeffsA[filter][0]++;
834  else
835  p->coeffsA[filter][0]--;
836 
837  p->filterA[filter] += p->lastA[filter];
838 
839  return p->filterA[filter];
840 }
841 
843  const int decoded, const int filter,
844  const int delayA, const int delayB,
845  const int start, const int shift)
846 {
847  int32_t predictionA, predictionB, sign;
848  int32_t d0, d1, d2, d3, d4;
849 
850  p->buf[delayA] = p->lastA[filter];
851  p->buf[delayB] = p->filterB[filter];
852  if (p->sample_pos < start) {
853  predictionA = decoded + p->filterA[filter];
854  p->lastA[filter] = decoded;
855  p->filterB[filter] = decoded;
856  p->filterA[filter] = predictionA;
857  return predictionA;
858  }
859  d2 = p->buf[delayA];
860  d1 = (p->buf[delayA] - p->buf[delayA - 1]) << 1;
861  d0 = p->buf[delayA] + ((p->buf[delayA - 2] - p->buf[delayA - 1]) << 3);
862  d3 = p->buf[delayB] * 2 - p->buf[delayB - 1];
863  d4 = p->buf[delayB];
864 
865  predictionA = d0 * p->coeffsA[filter][0] +
866  d1 * p->coeffsA[filter][1] +
867  d2 * p->coeffsA[filter][2];
868 
869  sign = APESIGN(decoded);
870  p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
871  p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
872  p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
873 
874  predictionB = d3 * p->coeffsB[filter][0] -
875  d4 * p->coeffsB[filter][1];
876  p->lastA[filter] = decoded + (predictionA >> 11);
877  sign = APESIGN(p->lastA[filter]);
878  p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
879  p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
880 
881  p->filterB[filter] = p->lastA[filter] + (predictionB >> shift);
882  p->filterA[filter] = p->filterB[filter] + ((p->filterA[filter] * 31) >> 5);
883 
884  return p->filterA[filter];
885 }
886 
887 static void long_filter_high_3800(int32_t *buffer, int order, int shift,
888  int32_t *coeffs, int32_t *delay, int length)
889 {
890  int i, j;
891  int32_t dotprod, sign;
892 
893  memset(coeffs, 0, order * sizeof(*coeffs));
894  for (i = 0; i < order; i++)
895  delay[i] = buffer[i];
896  for (i = order; i < length; i++) {
897  dotprod = 0;
898  sign = APESIGN(buffer[i]);
899  for (j = 0; j < order; j++) {
900  dotprod += delay[j] * coeffs[j];
901  coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
902  }
903  buffer[i] -= dotprod >> shift;
904  for (j = 0; j < order - 1; j++)
905  delay[j] = delay[j + 1];
906  delay[order - 1] = buffer[i];
907  }
908 }
909 
910 static void long_filter_ehigh_3830(int32_t *buffer, int length)
911 {
912  int i, j;
913  int32_t dotprod, sign;
914  int32_t coeffs[8], delay[8];
915 
916  memset(coeffs, 0, sizeof(coeffs));
917  memset(delay, 0, sizeof(delay));
918  for (i = 0; i < length; i++) {
919  dotprod = 0;
920  sign = APESIGN(buffer[i]);
921  for (j = 7; j >= 0; j--) {
922  dotprod += delay[j] * coeffs[j];
923  coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
924  }
925  for (j = 7; j > 0; j--)
926  delay[j] = delay[j - 1];
927  delay[0] = buffer[i];
928  buffer[i] -= dotprod >> 9;
929  }
930 }
931 
932 static void predictor_decode_stereo_3800(APEContext *ctx, int count)
933 {
934  APEPredictor *p = &ctx->predictor;
935  int32_t *decoded0 = ctx->decoded[0];
936  int32_t *decoded1 = ctx->decoded[1];
937  int32_t coeffs[256], delay[256];
938  int start = 4, shift = 10;
939 
941  start = 16;
942  long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
943  long_filter_high_3800(decoded1, 16, 9, coeffs, delay, count);
944  } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
945  int order = 128, shift2 = 11;
946 
947  if (ctx->fileversion >= 3830) {
948  order <<= 1;
949  shift++;
950  shift2++;
951  long_filter_ehigh_3830(decoded0 + order, count - order);
952  long_filter_ehigh_3830(decoded1 + order, count - order);
953  }
954  start = order;
955  long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
956  long_filter_high_3800(decoded1, order, shift2, coeffs, delay, count);
957  }
958 
959  while (count--) {
960  int X = *decoded0, Y = *decoded1;
962  *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
963  decoded0++;
964  *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
965  decoded1++;
966  } else {
967  *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
968  start, shift);
969  decoded0++;
970  *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
971  start, shift);
972  decoded1++;
973  }
974 
975  /* Combined */
976  p->buf++;
977  p->sample_pos++;
978 
979  /* Have we filled the history buffer? */
980  if (p->buf == p->historybuffer + HISTORY_SIZE) {
981  memmove(p->historybuffer, p->buf,
982  PREDICTOR_SIZE * sizeof(*p->historybuffer));
983  p->buf = p->historybuffer;
984  }
985  }
986 }
987 
988 static void predictor_decode_mono_3800(APEContext *ctx, int count)
989 {
990  APEPredictor *p = &ctx->predictor;
991  int32_t *decoded0 = ctx->decoded[0];
992  int32_t coeffs[256], delay[256];
993  int start = 4, shift = 10;
994 
996  start = 16;
997  long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
998  } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
999  int order = 128, shift2 = 11;
1000 
1001  if (ctx->fileversion >= 3830) {
1002  order <<= 1;
1003  shift++;
1004  shift2++;
1005  long_filter_ehigh_3830(decoded0 + order, count - order);
1006  }
1007  start = order;
1008  long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
1009  }
1010 
1011  while (count--) {
1013  *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
1014  decoded0++;
1015  } else {
1016  *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
1017  start, shift);
1018  decoded0++;
1019  }
1020 
1021  /* Combined */
1022  p->buf++;
1023  p->sample_pos++;
1024 
1025  /* Have we filled the history buffer? */
1026  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1027  memmove(p->historybuffer, p->buf,
1028  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1029  p->buf = p->historybuffer;
1030  }
1031  }
1032 }
1033 
1035  const int decoded, const int filter,
1036  const int delayA)
1037 {
1038  int32_t predictionA, sign;
1039  int32_t d0, d1, d2, d3;
1040 
1041  p->buf[delayA] = p->lastA[filter];
1042  d0 = p->buf[delayA ];
1043  d1 = p->buf[delayA ] - p->buf[delayA - 1];
1044  d2 = p->buf[delayA - 1] - p->buf[delayA - 2];
1045  d3 = p->buf[delayA - 2] - p->buf[delayA - 3];
1046 
1047  predictionA = d0 * p->coeffsA[filter][0] +
1048  d1 * p->coeffsA[filter][1] +
1049  d2 * p->coeffsA[filter][2] +
1050  d3 * p->coeffsA[filter][3];
1051 
1052  p->lastA[filter] = decoded + (predictionA >> 9);
1053  p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
1054 
1055  sign = APESIGN(decoded);
1056  p->coeffsA[filter][0] += ((d0 < 0) * 2 - 1) * sign;
1057  p->coeffsA[filter][1] += ((d1 < 0) * 2 - 1) * sign;
1058  p->coeffsA[filter][2] += ((d2 < 0) * 2 - 1) * sign;
1059  p->coeffsA[filter][3] += ((d3 < 0) * 2 - 1) * sign;
1060 
1061  return p->filterA[filter];
1062 }
1063 
1064 static void predictor_decode_stereo_3930(APEContext *ctx, int count)
1065 {
1066  APEPredictor *p = &ctx->predictor;
1067  int32_t *decoded0 = ctx->decoded[0];
1068  int32_t *decoded1 = ctx->decoded[1];
1069 
1070  ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1071 
1072  while (count--) {
1073  /* Predictor Y */
1074  int Y = *decoded1, X = *decoded0;
1075  *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
1076  decoded0++;
1077  *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
1078  decoded1++;
1079 
1080  /* Combined */
1081  p->buf++;
1082 
1083  /* Have we filled the history buffer? */
1084  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1085  memmove(p->historybuffer, p->buf,
1086  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1087  p->buf = p->historybuffer;
1088  }
1089  }
1090 }
1091 
1092 static void predictor_decode_mono_3930(APEContext *ctx, int count)
1093 {
1094  APEPredictor *p = &ctx->predictor;
1095  int32_t *decoded0 = ctx->decoded[0];
1096 
1097  ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1098 
1099  while (count--) {
1100  *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
1101  decoded0++;
1102 
1103  p->buf++;
1104 
1105  /* Have we filled the history buffer? */
1106  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1107  memmove(p->historybuffer, p->buf,
1108  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1109  p->buf = p->historybuffer;
1110  }
1111  }
1112 }
1113 
1115  const int decoded, const int filter,
1116  const int delayA, const int delayB,
1117  const int adaptA, const int adaptB)
1118 {
1119  int32_t predictionA, predictionB, sign;
1120 
1121  p->buf[delayA] = p->lastA[filter];
1122  p->buf[adaptA] = APESIGN(p->buf[delayA]);
1123  p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
1124  p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1125 
1126  predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
1127  p->buf[delayA - 1] * p->coeffsA[filter][1] +
1128  p->buf[delayA - 2] * p->coeffsA[filter][2] +
1129  p->buf[delayA - 3] * p->coeffsA[filter][3];
1130 
1131  /* Apply a scaled first-order filter compression */
1132  p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
1133  p->buf[adaptB] = APESIGN(p->buf[delayB]);
1134  p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
1135  p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1136  p->filterB[filter] = p->filterA[filter ^ 1];
1137 
1138  predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
1139  p->buf[delayB - 1] * p->coeffsB[filter][1] +
1140  p->buf[delayB - 2] * p->coeffsB[filter][2] +
1141  p->buf[delayB - 3] * p->coeffsB[filter][3] +
1142  p->buf[delayB - 4] * p->coeffsB[filter][4];
1143 
1144  p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1145  p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
1146 
1147  sign = APESIGN(decoded);
1148  p->coeffsA[filter][0] += p->buf[adaptA ] * sign;
1149  p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
1150  p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
1151  p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
1152  p->coeffsB[filter][0] += p->buf[adaptB ] * sign;
1153  p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
1154  p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
1155  p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
1156  p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
1157 
1158  return p->filterA[filter];
1159 }
1160 
1161 static void predictor_decode_stereo_3950(APEContext *ctx, int count)
1162 {
1163  APEPredictor *p = &ctx->predictor;
1164  int32_t *decoded0 = ctx->decoded[0];
1165  int32_t *decoded1 = ctx->decoded[1];
1166 
1167  ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1168 
1169  while (count--) {
1170  /* Predictor Y */
1171  *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1173  decoded0++;
1174  *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1176  decoded1++;
1177 
1178  /* Combined */
1179  p->buf++;
1180 
1181  /* Have we filled the history buffer? */
1182  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1183  memmove(p->historybuffer, p->buf,
1184  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1185  p->buf = p->historybuffer;
1186  }
1187  }
1188 }
1189 
1190 static void predictor_decode_mono_3950(APEContext *ctx, int count)
1191 {
1192  APEPredictor *p = &ctx->predictor;
1193  int32_t *decoded0 = ctx->decoded[0];
1194  int32_t predictionA, currentA, A, sign;
1195 
1196  ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1197 
1198  currentA = p->lastA[0];
1199 
1200  while (count--) {
1201  A = *decoded0;
1202 
1203  p->buf[YDELAYA] = currentA;
1204  p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
1205 
1206  predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
1207  p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
1208  p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
1209  p->buf[YDELAYA - 3] * p->coeffsA[0][3];
1210 
1211  currentA = A + (predictionA >> 10);
1212 
1213  p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
1214  p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
1215 
1216  sign = APESIGN(A);
1217  p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ] * sign;
1218  p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
1219  p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
1220  p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
1221 
1222  p->buf++;
1223 
1224  /* Have we filled the history buffer? */
1225  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1226  memmove(p->historybuffer, p->buf,
1227  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1228  p->buf = p->historybuffer;
1229  }
1230 
1231  p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
1232  *(decoded0++) = p->filterA[0];
1233  }
1234 
1235  p->lastA[0] = currentA;
1236 }
1237 
1238 static void do_init_filter(APEFilter *f, int16_t *buf, int order)
1239 {
1240  f->coeffs = buf;
1241  f->historybuffer = buf + order;
1242  f->delay = f->historybuffer + order * 2;
1243  f->adaptcoeffs = f->historybuffer + order;
1244 
1245  memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
1246  memset(f->coeffs, 0, order * sizeof(*f->coeffs));
1247  f->avg = 0;
1248 }
1249 
1250 static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
1251 {
1252  do_init_filter(&f[0], buf, order);
1253  do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1254 }
1255 
1256 static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
1257  int32_t *data, int count, int order, int fracbits)
1258 {
1259  int res;
1260  int absres;
1261 
1262  while (count--) {
1263  /* round fixedpoint scalar product */
1264  res = ctx->dsp.scalarproduct_and_madd_int16(f->coeffs, f->delay - order,
1265  f->adaptcoeffs - order,
1266  order, APESIGN(*data));
1267  res = (res + (1 << (fracbits - 1))) >> fracbits;
1268  res += *data;
1269  *data++ = res;
1270 
1271  /* Update the output history */
1272  *f->delay++ = av_clip_int16(res);
1273 
1274  if (version < 3980) {
1275  /* Version ??? to < 3.98 files (untested) */
1276  f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1277  f->adaptcoeffs[-4] >>= 1;
1278  f->adaptcoeffs[-8] >>= 1;
1279  } else {
1280  /* Version 3.98 and later files */
1281 
1282  /* Update the adaption coefficients */
1283  absres = FFABS(res);
1284  if (absres)
1285  *f->adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1286  (25 + (absres <= f->avg*3) + (absres <= f->avg*4/3));
1287  else
1288  *f->adaptcoeffs = 0;
1289 
1290  f->avg += (absres - f->avg) / 16;
1291 
1292  f->adaptcoeffs[-1] >>= 1;
1293  f->adaptcoeffs[-2] >>= 1;
1294  f->adaptcoeffs[-8] >>= 1;
1295  }
1296 
1297  f->adaptcoeffs++;
1298 
1299  /* Have we filled the history buffer? */
1300  if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1301  memmove(f->historybuffer, f->delay - (order * 2),
1302  (order * 2) * sizeof(*f->historybuffer));
1303  f->delay = f->historybuffer + order * 2;
1304  f->adaptcoeffs = f->historybuffer + order;
1305  }
1306  }
1307 }
1308 
1309 static void apply_filter(APEContext *ctx, APEFilter *f,
1310  int32_t *data0, int32_t *data1,
1311  int count, int order, int fracbits)
1312 {
1313  do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1314  if (data1)
1315  do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1316 }
1317 
1318 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
1319  int32_t *decoded1, int count)
1320 {
1321  int i;
1322 
1323  for (i = 0; i < APE_FILTER_LEVELS; i++) {
1324  if (!ape_filter_orders[ctx->fset][i])
1325  break;
1326  apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
1327  ape_filter_orders[ctx->fset][i],
1328  ape_filter_fracbits[ctx->fset][i]);
1329  }
1330 }
1331 
1333 {
1334  int i, ret;
1335  if ((ret = init_entropy_decoder(ctx)) < 0)
1336  return ret;
1338 
1339  for (i = 0; i < APE_FILTER_LEVELS; i++) {
1340  if (!ape_filter_orders[ctx->fset][i])
1341  break;
1342  init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
1343  ape_filter_orders[ctx->fset][i]);
1344  }
1345  return 0;
1346 }
1347 
1348 static void ape_unpack_mono(APEContext *ctx, int count)
1349 {
1351  /* We are pure silence, so we're done. */
1352  av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
1353  return;
1354  }
1355 
1356  ctx->entropy_decode_mono(ctx, count);
1357 
1358  /* Now apply the predictor decoding */
1359  ctx->predictor_decode_mono(ctx, count);
1360 
1361  /* Pseudo-stereo - just copy left channel to right channel */
1362  if (ctx->channels == 2) {
1363  memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1]));
1364  }
1365 }
1366 
1367 static void ape_unpack_stereo(APEContext *ctx, int count)
1368 {
1369  int32_t left, right;
1370  int32_t *decoded0 = ctx->decoded[0];
1371  int32_t *decoded1 = ctx->decoded[1];
1372 
1374  /* We are pure silence, so we're done. */
1375  av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
1376  return;
1377  }
1378 
1379  ctx->entropy_decode_stereo(ctx, count);
1380 
1381  /* Now apply the predictor decoding */
1382  ctx->predictor_decode_stereo(ctx, count);
1383 
1384  /* Decorrelate and scale to output depth */
1385  while (count--) {
1386  left = *decoded1 - (*decoded0 / 2);
1387  right = left + *decoded0;
1388 
1389  *(decoded0++) = left;
1390  *(decoded1++) = right;
1391  }
1392 }
1393 
1395  int *got_frame_ptr, AVPacket *avpkt)
1396 {
1397  AVFrame *frame = data;
1398  const uint8_t *buf = avpkt->data;
1399  APEContext *s = avctx->priv_data;
1400  uint8_t *sample8;
1401  int16_t *sample16;
1402  int32_t *sample24;
1403  int i, ch, ret;
1404  int blockstodecode;
1405 
1406  /* this should never be negative, but bad things will happen if it is, so
1407  check it just to make sure. */
1408  av_assert0(s->samples >= 0);
1409 
1410  if(!s->samples){
1411  uint32_t nblocks, offset;
1412  int buf_size;
1413 
1414  if (!avpkt->size) {
1415  *got_frame_ptr = 0;
1416  return 0;
1417  }
1418  if (avpkt->size < 8) {
1419  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1420  return AVERROR_INVALIDDATA;
1421  }
1422  buf_size = avpkt->size & ~3;
1423  if (buf_size != avpkt->size) {
1424  av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
1425  "extra bytes at the end will be skipped.\n");
1426  }
1427  if (s->fileversion < 3950) // previous versions overread two bytes
1428  buf_size += 2;
1429  av_fast_malloc(&s->data, &s->data_size, buf_size);
1430  if (!s->data)
1431  return AVERROR(ENOMEM);
1432  s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
1433  memset(s->data + (buf_size & ~3), 0, buf_size & 3);
1434  s->ptr = s->data;
1435  s->data_end = s->data + buf_size;
1436 
1437  nblocks = bytestream_get_be32(&s->ptr);
1438  offset = bytestream_get_be32(&s->ptr);
1439  if (s->fileversion >= 3900) {
1440  if (offset > 3) {
1441  av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
1442  s->data = NULL;
1443  return AVERROR_INVALIDDATA;
1444  }
1445  if (s->data_end - s->ptr < offset) {
1446  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1447  return AVERROR_INVALIDDATA;
1448  }
1449  s->ptr += offset;
1450  } else {
1451  init_get_bits(&s->gb, s->ptr, (s->data_end - s->ptr) * 8);
1452  if (s->fileversion > 3800)
1453  skip_bits_long(&s->gb, offset * 8);
1454  else
1455  skip_bits_long(&s->gb, offset);
1456  }
1457 
1458  if (!nblocks || nblocks > INT_MAX) {
1459  av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks);
1460  return AVERROR_INVALIDDATA;
1461  }
1462  s->samples = nblocks;
1463 
1464  /* Initialize the frame decoder */
1465  if (init_frame_decoder(s) < 0) {
1466  av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
1467  return AVERROR_INVALIDDATA;
1468  }
1469 
1470  }
1471 
1472  if (!s->data) {
1473  *got_frame_ptr = 0;
1474  return avpkt->size;
1475  }
1476 
1477  blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
1478  // for old files coefficients were not interleaved,
1479  // so we need to decode all of them at once
1480  if (s->fileversion < 3930)
1481  blockstodecode = s->samples;
1482 
1483  /* reallocate decoded sample buffer if needed */
1485  2 * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer));
1486  if (!s->decoded_buffer)
1487  return AVERROR(ENOMEM);
1488  memset(s->decoded_buffer, 0, s->decoded_size);
1489  s->decoded[0] = s->decoded_buffer;
1490  s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1491 
1492  /* get output buffer */
1493  frame->nb_samples = blockstodecode;
1494  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1495  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1496  return ret;
1497  }
1498 
1499  s->error=0;
1500 
1501  if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1502  ape_unpack_mono(s, blockstodecode);
1503  else
1504  ape_unpack_stereo(s, blockstodecode);
1505  emms_c();
1506 
1507  if (s->error) {
1508  s->samples=0;
1509  av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
1510  return AVERROR_INVALIDDATA;
1511  }
1512 
1513  switch (s->bps) {
1514  case 8:
1515  for (ch = 0; ch < s->channels; ch++) {
1516  sample8 = (uint8_t *)frame->data[ch];
1517  for (i = 0; i < blockstodecode; i++)
1518  *sample8++ = (s->decoded[ch][i] + 0x80) & 0xff;
1519  }
1520  break;
1521  case 16:
1522  for (ch = 0; ch < s->channels; ch++) {
1523  sample16 = (int16_t *)frame->data[ch];
1524  for (i = 0; i < blockstodecode; i++)
1525  *sample16++ = s->decoded[ch][i];
1526  }
1527  break;
1528  case 24:
1529  for (ch = 0; ch < s->channels; ch++) {
1530  sample24 = (int32_t *)frame->data[ch];
1531  for (i = 0; i < blockstodecode; i++)
1532  *sample24++ = s->decoded[ch][i] << 8;
1533  }
1534  break;
1535  }
1536 
1537  s->samples -= blockstodecode;
1538 
1539  *got_frame_ptr = 1;
1540 
1541  return (s->samples == 0) ? avpkt->size : 0;
1542 }
1543 
1545 {
1546  APEContext *s = avctx->priv_data;
1547  s->samples= 0;
1548 }
1549 
1550 #define OFFSET(x) offsetof(APEContext, x)
1551 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1552 static const AVOption options[] = {
1553  { "max_samples", "maximum number of samples decoded per call", OFFSET(blocks_per_loop), AV_OPT_TYPE_INT, { .i64 = 4608 }, 1, INT_MAX, PAR, "max_samples" },
1554  { "all", "no maximum. decode all samples for each packet at once", 0, AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX, PAR, "max_samples" },
1555  { NULL},
1556 };
1557 
1558 static const AVClass ape_decoder_class = {
1559  .class_name = "APE decoder",
1560  .item_name = av_default_item_name,
1561  .option = options,
1562  .version = LIBAVUTIL_VERSION_INT,
1563 };
1564 
1566  .name = "ape",
1567  .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
1568  .type = AVMEDIA_TYPE_AUDIO,
1569  .id = AV_CODEC_ID_APE,
1570  .priv_data_size = sizeof(APEContext),
1571  .init = ape_decode_init,
1575  .flush = ape_flush,
1576  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
1580  .priv_class = &ape_decoder_class,
1581 };
static int init_frame_decoder(APEContext *ctx)
Definition: apedec.c:1332
static const int32_t initial_coeffs_3930[4]
Definition: apedec.c:767
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, int32_t *out, APERice *rice, int blockstodecode)
Definition: apedec.c:586
int compression_level
compression levels
Definition: apedec.c:142
AVCodec ff_ape_decoder
Definition: apedec.c:1565
#define MODEL_ELEMENTS
Definition: apedec.c:384
static av_always_inline int filter_3800(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int start, const int shift)
Definition: apedec.c:842
static const int16_t coeffs[28]
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2440
int32_t coeffsB[2][5]
adaption coefficients
Definition: apedec.c:126
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int decoded_size
Definition: apedec.c:151
#define YADAPTCOEFFSB
Definition: apedec.c:57
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static void range_start_decoding(APEContext *ctx)
Start the decoder.
Definition: apedec.c:314
AVOption.
Definition: opt.h:233
#define XDELAYA
Definition: apedec.c:52
static void apply_filter(APEContext *ctx, APEFilter *f, int32_t *data0, int32_t *data1, int count, int order, int fracbits)
Definition: apedec.c:1309
int fileversion
codec version, very important in decoding process
Definition: apedec.c:141
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
Definition: apedec.c:636
int32_t filterA[2]
Definition: apedec.c:122
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:199
static int APESIGN(int32_t x)
Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)
Definition: apedec.c:812
static void update_rice(APERice *rice, unsigned int x)
Definition: apedec.c:452
int size
Definition: avcodec.h:974
static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
Definition: apedec.c:672
static av_cold int ape_decode_init(AVCodecContext *avctx)
Definition: apedec.c:213
unsigned int buffer
buffer for input/output
Definition: apedec.c:113
static int init_entropy_decoder(APEContext *ctx)
Definition: apedec.c:718
#define AV_RL16
Definition: intreadwrite.h:42
static void ape_flush(AVCodecContext *avctx)
Definition: apedec.c:1544
static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
Definition: apedec.c:688
static av_always_inline int predictor_update_3930(APEPredictor *p, const int decoded, const int filter, const int delayA)
Definition: apedec.c:1034
#define AV_CH_LAYOUT_STEREO
#define OFFSET(x)
Definition: apedec.c:1550
#define XADAPTCOEFFSA
Definition: apedec.c:56
Definition: vf_drawbox.c:37
AVCodec.
Definition: avcodec.h:2755
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
Definition: apedec.c:155
static void predictor_decode_mono_3800(APEContext *ctx, int count)
Definition: apedec.c:988
#define FFALIGN(x, a)
Definition: common.h:62
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int ape_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: apedec.c:1394
Filter histories.
Definition: apedec.c:117
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:269
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1787
uint8_t
#define av_cold
Definition: attributes.h:66
int16_t * delay
filtered values
Definition: apedec.c:99
AVOptions.
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
Definition: apedec.c:1238
static const int32_t initial_coeffs_a_3800[3]
Definition: apedec.c:759
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
Definition: apedec.c:652
static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
Definition: apedec.c:699
static void ape_unpack_mono(APEContext *ctx, int count)
Definition: apedec.c:1348
#define emms_c()
Definition: internal.h:46
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
APERangecoder rc
rangecoder used to decode actual values
Definition: apedec.c:157
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define YDELAYB
Definition: apedec.c:51
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS]
Filter fraction bits depending on compression level.
Definition: apedec.c:85
uint8_t * data
Definition: avcodec.h:973
static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, int32_t *decoded1, int count)
Definition: apedec.c:1318
bitstream reader API header.
signed 32 bits, planar
Definition: samplefmt.h:59
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2481
Decoder context.
Definition: apedec.c:133
void(* entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode)
Definition: apedec.c:171
static const uint16_t counts_3970[22]
Fixed probabilities for symbols in Monkey Audio version 3.97.
Definition: apedec.c:389
static void range_dec_normalize(APEContext *ctx)
Perform normalization.
Definition: apedec.c:322
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
Definition: apedec.c:416
int bps
Definition: apedec.c:139
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define YDELAYA
Definition: apedec.c:50
int32_t lastA[2]
Definition: apedec.c:120
static av_cold int ape_decode_close(AVCodecContext *avctx)
Definition: apedec.c:198
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:740
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: dsputil.h:202
static int ape_decode_value_3900(APEContext *ctx, APERice *rice)
Definition: apedec.c:507
#define AVERROR(e)
Definition: error.h:43
int32_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition: apedec.c:127
sample_fmts
Definition: avconv_filter.c:68
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
#define XDELAYB
Definition: apedec.c:53
int32_t * decoded_buffer
Definition: apedec.c:150
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
int avg
Definition: apedec.c:101
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
Definition: apedec.c:355
#define APE_FILTER_LEVELS
Definition: apedec.c:73
int error
Definition: apedec.c:168
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1840
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
Definition: apedec.c:376
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
audio channel layout utility functions
static void predictor_decode_mono_3930(APEContext *ctx, int count)
Definition: apedec.c:1092
uint8_t * data
current frame data
Definition: apedec.c:163
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
Definition: apedec.c:76
#define FFMIN(a, b)
Definition: common.h:57
static int get_rice_ook(GetBitContext *gb, int k)
Definition: apedec.c:463
static void long_filter_high_3800(int32_t *buffer, int order, int shift, int32_t *coeffs, int32_t *delay, int length)
Definition: apedec.c:887
static av_always_inline int filter_fast_3320(APEPredictor *p, const int decoded, const int filter, const int delayA)
Definition: apedec.c:816
AVCodecContext * avctx
Definition: apedec.c:135
static void ape_unpack_stereo(APEContext *ctx, int count)
Definition: apedec.c:1367
const uint8_t * ptr
current position in frame data
Definition: apedec.c:166
int32_t
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate culmulative frequency for next symbol.
Definition: apedec.c:343
#define FFABS(a)
Definition: common.h:52
static char buffer[20]
Definition: seek-test.c:31
static void predictor_decode_stereo_3930(APEContext *ctx, int count)
Definition: apedec.c:1064
uint32_t ksum
Definition: apedec.c:106
uint32_t help
bytes_to_follow resp. intermediate value
Definition: apedec.c:112
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
Definition: apedec.c:707
#define APE_FRAMECODE_PSEUDO_STEREO
Definition: apedec.c:43
uint32_t range
length of interval
Definition: apedec.c:111
Definition: vf_drawbox.c:37
if(ac->has_optimized_func)
int samples
samples left to decode in current frame
Definition: apedec.c:138
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int fset
which filter set to use (calculated from compression level)
Definition: apedec.c:143
static int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, APERice *rice)
Definition: apedec.c:475
NULL
Definition: eval.c:55
APERice riceX
rice code parameters for the second channel
Definition: apedec.c:158
Libavcodec external API header.
version
Definition: ffv1enc.c:1080
static void predictor_decode_stereo_3950(APEContext *ctx, int count)
Definition: apedec.c:1161
static void predictor_decode_stereo_3800(APEContext *ctx, int count)
Definition: apedec.c:932
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
av_default_item_name
Definition: dnxhdenc.c:45
#define APE_FRAMECODE_STEREO_SILENCE
Definition: apedec.c:42
static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
Definition: apedec.c:1250
int frameflags
frame flags
Definition: apedec.c:147
main external API structure.
Definition: avcodec.h:1054
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
static int ape_decode_value_3990(APEContext *ctx, APERice *rice)
Definition: apedec.c:540
uint32_t CRC
frame CRC
Definition: apedec.c:146
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:575
unsigned int sample_pos
Definition: apedec.c:129
int extradata_size
Definition: avcodec.h:1163
static const uint16_t counts_3980[22]
Fixed probabilities for symbols in Monkey Audio version 3.98.
Definition: apedec.c:407
static int range_get_symbol(APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
Decode symbol.
Definition: apedec.c:428
Describe the class of an AVClass context structure.
Definition: log.h:33
uint32_t low
low end of interval
Definition: apedec.c:110
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int flags
global decoder flags
Definition: apedec.c:144
void(* predictor_decode_mono)(struct APEContext *ctx, int count)
Definition: apedec.c:172
APECompressionLevel
Possible compression levels.
Definition: apedec.c:64
#define EXTRA_BITS
Definition: apedec.c:310
int32_t coeffsA[2][4]
adaption coefficients
Definition: apedec.c:125
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
Definition: apedec.c:369
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
Definition: apedec.c:664
uint32_t k
Definition: apedec.c:105
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:304
#define MAX_CHANNELS
Definition: apedec.c:38
static const int32_t initial_coeffs_fast_3320[1]
Definition: apedec.c:755
static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
Definition: apedec.c:1256
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
#define PREDICTOR_SIZE
Total size of all predictor histories.
Definition: apedec.c:48
void(* predictor_decode_stereo)(struct APEContext *ctx, int count)
Definition: apedec.c:173
static const uint16_t counts_diff_3970[21]
Probability ranges for symbols in Monkey Audio version 3.97.
Definition: apedec.c:398
int blocks_per_loop
maximum number of samples to decode for each call
Definition: apedec.c:153
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:763
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:368
uint8_t * data_end
frame data end
Definition: apedec.c:164
common internal api header.
APERice riceY
rice code parameters for the first channel
Definition: apedec.c:159
static const int shift2[6]
Definition: dxa.c:49
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:33
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:109
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1766
APEFilter filters[APE_FILTER_LEVELS][2]
filters used for reconstruction
Definition: apedec.c:160
static av_always_inline int predictor_update_filter(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
Definition: apedec.c:1114
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
DSPContext dsp
Definition: apedec.c:136
int32_t(* scalarproduct_and_madd_int16)(int16_t *v1, const int16_t *v2, const int16_t *v3, int len, int mul)
Calculate scalar product of v1 and v2, and v1[i] += v3[i] * mul.
Definition: dsputil.h:272
int16_t * coeffs
actual coefficients used in filtering
Definition: apedec.c:96
int32_t filterB[2]
Definition: apedec.c:123
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
DSP utils.
#define YADAPTCOEFFSA
Definition: apedec.c:55
#define PAR
Definition: apedec.c:1551
static void init_predictor_decoder(APEContext *ctx)
Definition: apedec.c:771
void * priv_data
Definition: avcodec.h:1090
static const int32_t initial_coeffs_b_3800[2]
Definition: apedec.c:763
APEPredictor predictor
predictor used for final reconstruction
Definition: apedec.c:148
static const AVClass ape_decoder_class
Definition: apedec.c:1558
unsigned 8 bits, planar
Definition: samplefmt.h:57
void(* entropy_decode_mono)(struct APEContext *ctx, int blockstodecode)
Definition: apedec.c:170
int channels
number of audio channels
Definition: avcodec.h:1780
static void long_filter_ehigh_3830(int32_t *buffer, int length)
Definition: apedec.c:910
#define av_log2
Definition: intmath.h:85
static void predictor_decode_mono_3950(APEContext *ctx, int count)
Definition: apedec.c:1190
GetBitContext gb
Definition: apedec.c:161
Filters applied to the decoded data.
Definition: apedec.c:95
#define XADAPTCOEFFSB
Definition: apedec.c:58
signed 16 bits, planar
Definition: samplefmt.h:58
int32_t * decoded[MAX_CHANNELS]
decoded data for each channel
Definition: apedec.c:152
int32_t * buf
Definition: apedec.c:118
#define HISTORY_SIZE
Definition: apedec.c:45
#define av_always_inline
Definition: attributes.h:40
int data_size
frame data allocated size
Definition: apedec.c:165
static const AVOption options[]
Definition: apedec.c:1552
#define AV_CH_LAYOUT_MONO
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
Definition: apedec.c:97
int channels
Definition: apedec.c:137
#define BOTTOM_VALUE
Definition: apedec.c:311
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:151
for(j=16;j >0;--j)
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
Definition: apedec.c:630
DSPContext.
Definition: dsputil.h:124
int16_t * historybuffer
filter memory
Definition: apedec.c:98
static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
Definition: apedec.c:644