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