1 /*
2     DeaDBeeF - The Ultimate Music Player
3     Copyright (C) 2009-2013 Alexey Yakovenko <waker@users.sourceforge.net>
4     based on apedec from FFMpeg Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
5     based upon libdemac from Dave Chapman.
6 
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 */
21 
22 /*
23    main changes compared to ffmpeg:
24      demuxer and decoder joined into 1 module
25      no mallocs/reallocs during decoding
26      streaming through fixed ringbuffer (small mem footprint)
27      24bit support merged from rockbox
28 */
29 
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include <stdio.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <stdlib.h>
37 //#include <alloca.h>
38 #include <assert.h>
39 #include <math.h>
40 #include "../../deadbeef.h"
41 
42 #ifdef TARGET_ANDROID
posix_memalign(void ** memptr,size_t alignment,size_t size)43 int posix_memalign (void **memptr, size_t alignment, size_t size) {
44     *memptr = memalign (alignment, size);
45     return *memptr ? 0 : -1;
46 }
47 #endif
48 
49 #define ENABLE_DEBUG 0
50 
51 //#define trace(...) { fprintf(stderr, __VA_ARGS__); }
52 #define trace(fmt,...)
53 
54 #define likely(x)       __builtin_expect((x),1)
55 #define unlikely(x)     __builtin_expect((x),0)
56 
57 static DB_decoder_t plugin;
58 static DB_functions_t *deadbeef;
59 
60 #define PACKET_BUFFER_SIZE 100000
61 
62 #define min(x,y) ((x)<(y)?(x):(y))
63 #define max(x,y) ((x)>(y)?(x):(y))
64 
bytestream_get_buffer(const uint8_t ** b,uint8_t * dst,unsigned int size)65 static inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
66 {
67     memcpy(dst, *b, size);
68     (*b) += size;
69     return size;
70 }
71 
bytestream_put_buffer(uint8_t ** b,const uint8_t * src,unsigned int size)72 static inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
73 {
74     memcpy(*b, src, size);
75     (*b) += size;
76 }
77 
bytestream_get_byte(const uint8_t ** ptr)78 static inline uint8_t bytestream_get_byte (const uint8_t **ptr) {
79     uint8_t v = *(*ptr);
80     (*ptr)++;
81     return v;
82 }
83 
bytestream_get_be32(const uint8_t ** ptr)84 static inline uint32_t bytestream_get_be32 (const uint8_t **ptr) {
85     const uint8_t *tmp = *ptr;
86     uint32_t x = tmp[3] | (tmp[2] << 8) | (tmp[1] << 16) | (tmp[0] << 24);
87     (*ptr) += 4;
88     return x;
89 }
90 
91 
92 #define BLOCKS_PER_LOOP     4608
93 #define MAX_CHANNELS        2
94 #define MAX_BYTESPERSAMPLE  3
95 
96 #define APE_FRAMECODE_MONO_SILENCE    1
97 #define APE_FRAMECODE_STEREO_SILENCE  3
98 #define APE_FRAMECODE_PSEUDO_STEREO   4
99 
100 #define HISTORY_SIZE 512
101 #define PREDICTOR_ORDER 8
102 /** Total size of all predictor histories */
103 #define PREDICTOR_SIZE 50
104 
105 #define YDELAYA (18 + PREDICTOR_ORDER*4)
106 #define YDELAYB (18 + PREDICTOR_ORDER*3)
107 #define XDELAYA (18 + PREDICTOR_ORDER*2)
108 #define XDELAYB (18 + PREDICTOR_ORDER)
109 
110 #define YADAPTCOEFFSA 18
111 #define XADAPTCOEFFSA 14
112 #define YADAPTCOEFFSB 10
113 #define XADAPTCOEFFSB 5
114 
115 /**
116  * Possible compression levels
117  * @{
118  */
119 enum APECompressionLevel {
120     COMPRESSION_LEVEL_FAST       = 1000,
121     COMPRESSION_LEVEL_NORMAL     = 2000,
122     COMPRESSION_LEVEL_HIGH       = 3000,
123     COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
124     COMPRESSION_LEVEL_INSANE     = 5000
125 };
126 /** @} */
127 
128 #define APE_FILTER_LEVELS 3
129 
130 /** Filter orders depending on compression level */
131 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
132     {  0,   0,    0 },
133     { 16,   0,    0 },
134     { 64,   0,    0 },
135     { 32, 256,    0 },
136     { 16, 256, 1280 }
137 };
138 
139 /** Filter fraction bits depending on compression level */
140 static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
141     {  0,  0,  0 },
142     { 11,  0,  0 },
143     { 11,  0,  0 },
144     { 10, 13,  0 },
145     { 11, 13, 15 }
146 };
147 
148 
149 /** Filters applied to the decoded data */
150 typedef struct APEFilter {
151     int16_t *coeffs;        ///< actual coefficients used in filtering
152     int16_t *adaptcoeffs;   ///< adaptive filter coefficients used for correcting of actual filter coefficients
153     int16_t *historybuffer; ///< filter memory
154     int16_t *delay;         ///< filtered values
155 
156     int avg;
157 } APEFilter;
158 
159 typedef struct APERice {
160     uint32_t k;
161     uint32_t ksum;
162 } APERice;
163 
164 typedef struct APERangecoder {
165     uint32_t low;           ///< low end of interval
166     uint32_t range;         ///< length of interval
167     uint32_t help;          ///< bytes_to_follow resp. intermediate value
168     unsigned int buffer;    ///< buffer for input/output
169 } APERangecoder;
170 
171 /** Filter histories */
172 typedef struct APEPredictor {
173     int32_t *buf;
174 
175     int32_t lastA[2];
176 
177     int32_t filterA[2];
178     int32_t filterB[2];
179 
180     int32_t coeffsA[2][4];  ///< adaption coefficients
181     int32_t coeffsB[2][5];  ///< adaption coefficients
182     int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
183 } APEPredictor;
184 
185 /* The earliest and latest file formats supported by this library */
186 #define APE_MIN_VERSION 3950
187 #define APE_MAX_VERSION 3990
188 
189 #define MAC_FORMAT_FLAG_8_BIT                 1 // is 8-bit [OBSOLETE]
190 #define MAC_FORMAT_FLAG_CRC                   2 // uses the new CRC32 error detection [OBSOLETE]
191 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL        4 // uint32 nPeakLevel after the header [OBSOLETE]
192 #define MAC_FORMAT_FLAG_24_BIT                8 // is 24-bit [OBSOLETE]
193 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS    16 // has the number of seek elements after the peak level
194 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER    32 // create the wave header on decompression (not stored)
195 
196 #define MAC_SUBFRAME_SIZE 4608
197 
198 #define APE_EXTRADATA_SIZE 6
199 
200 typedef struct {
201     int64_t pos;
202     int nblocks;
203     int size;
204     int skip;
205 } APEFrame;
206 
207 /** Decoder context */
208 typedef struct APEContext {
209     /* Derived fields */
210     uint32_t junklength;
211     uint32_t firstframe;
212     uint32_t totalsamples;
213     int currentframe;
214     APEFrame *frames;
215 
216     /* Info from Descriptor Block */
217     char magic[4];
218     int16_t fileversion;
219     int16_t padding1;
220     uint32_t descriptorlength;
221     uint32_t headerlength;
222     uint32_t seektablelength;
223     uint32_t wavheaderlength;
224     uint32_t audiodatalength;
225     uint32_t audiodatalength_high;
226     uint32_t wavtaillength;
227     uint8_t md5[16];
228 
229     /* Info from Header Block */
230     uint16_t compressiontype;
231     uint16_t formatflags;
232     uint32_t blocksperframe;
233     uint32_t finalframeblocks;
234     uint32_t totalframes;
235     uint16_t bps;
236     uint16_t channels;
237     uint32_t samplerate;
238     int samples;                             ///< samples left to decode in current frame
239 
240     /* Seektable */
241     uint32_t *seektable;
242 
243     int fset;                                ///< which filter set to use (calculated from compression level)
244     int flags;                               ///< global decoder flags
245 
246     uint32_t CRC;                            ///< frame CRC
247     int frameflags;                          ///< frame flags
248     int currentframeblocks;                  ///< samples (per channel) in current frame
249     int blocksdecoded;                       ///< count of decoded samples in current frame
250     APEPredictor predictor;                  ///< predictor used for final reconstruction
251 
252     int32_t decoded0[BLOCKS_PER_LOOP];       ///< decoded data for the first channel
253     int32_t decoded1[BLOCKS_PER_LOOP];       ///< decoded data for the second channel
254 
255     int16_t* filterbuf[APE_FILTER_LEVELS];   ///< filter memory
256 
257     APERangecoder rc;                        ///< rangecoder used to decode actual values
258     APERice riceX;                           ///< rice code parameters for the second channel
259     APERice riceY;                           ///< rice code parameters for the first channel
260     APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
261 
262     uint8_t *data_end;                       ///< frame data end
263     const uint8_t *ptr;                      ///< current position in frame data
264     const uint8_t *last_ptr;
265 
266     uint8_t *packet_data; // must be PACKET_BUFFER_SIZE
267     int packet_remaining; // number of bytes in packet_data
268     int packet_sizeleft; // number of bytes left unread for current ape frame
269     int samplestoskip;
270     int currentsample; // current sample from beginning of file
271 
272     uint8_t buffer[BLOCKS_PER_LOOP * 2 * 2 * 2];
273     int remaining;
274 
275     int error;
276     int skip_header;
277     int filterbuf_size[APE_FILTER_LEVELS];
278 } APEContext;
279 
280 typedef struct {
281     DB_fileinfo_t info;
282     int startsample;
283     int endsample;
284     APEContext ape_ctx;
285     DB_FILE *fp;
286 } ape_info_t;
287 
288 
289 inline static int
read_uint16(DB_FILE * fp,uint16_t * x)290 read_uint16(DB_FILE *fp, uint16_t* x)
291 {
292     unsigned char tmp[2];
293     int n;
294 
295     n = deadbeef->fread(tmp, 1, 2, fp);
296 
297     if (n != 2)
298         return -1;
299 
300     *x = tmp[0] | (tmp[1] << 8);
301 
302     return 0;
303 }
304 
305 
306 inline static int
read_int16(DB_FILE * fp,int16_t * x)307 read_int16(DB_FILE *fp, int16_t* x)
308 {
309     return read_uint16(fp, (uint16_t*)x);
310 }
311 
312 inline static int
read_uint32(DB_FILE * fp,uint32_t * x)313 read_uint32(DB_FILE *fp, uint32_t* x)
314 {
315     unsigned char tmp[4];
316     int n;
317 
318     n = deadbeef->fread(tmp, 1, 4, fp);
319 
320     if (n != 4)
321         return -1;
322 
323     *x = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);
324 
325     return 0;
326 }
327 
ape_dumpinfo(APEContext * ape_ctx)328 static void ape_dumpinfo(APEContext * ape_ctx)
329 {
330 #if ENABLE_DEBUG
331     int i;
332 
333     fprintf (stderr, "Descriptor Block:\n\n");
334     fprintf (stderr, "magic                = \"%c%c%c%c\"\n", ape_ctx->magic[0], ape_ctx->magic[1], ape_ctx->magic[2], ape_ctx->magic[3]);
335     fprintf (stderr, "fileversion          = %d\n", ape_ctx->fileversion);
336     fprintf (stderr, "descriptorlength     = %d\n", ape_ctx->descriptorlength);
337     fprintf (stderr, "headerlength         = %d\n", ape_ctx->headerlength);
338     fprintf (stderr, "seektablelength      = %d\n", ape_ctx->seektablelength);
339     fprintf (stderr, "wavheaderlength      = %d\n", ape_ctx->wavheaderlength);
340     fprintf (stderr, "audiodatalength      = %d\n", ape_ctx->audiodatalength);
341     fprintf (stderr, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
342     fprintf (stderr, "wavtaillength        = %d\n", ape_ctx->wavtaillength);
343     fprintf (stderr, "md5                  = ");
344     for (i = 0; i < 16; i++)
345          fprintf (stderr, "%02x", ape_ctx->md5[i]);
346     fprintf (stderr, "\n");
347 
348     fprintf (stderr, "\nHeader Block:\n\n");
349 
350     fprintf (stderr, "compressiontype      = %d\n", ape_ctx->compressiontype);
351     fprintf (stderr, "formatflags          = %d\n", ape_ctx->formatflags);
352     fprintf (stderr, "blocksperframe       = %d\n", ape_ctx->blocksperframe);
353     fprintf (stderr, "finalframeblocks     = %d\n", ape_ctx->finalframeblocks);
354     fprintf (stderr, "totalframes          = %d\n", ape_ctx->totalframes);
355     fprintf (stderr, "bps                  = %d\n", ape_ctx->bps);
356     fprintf (stderr, "channels             = %d\n", ape_ctx->channels);
357     fprintf (stderr, "samplerate           = %d\n", ape_ctx->samplerate);
358 
359     fprintf (stderr, "\nSeektable\n\n");
360     if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
361         fprintf (stderr, "No seektable\n");
362     } else {
363         for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
364             if (i < ape_ctx->totalframes - 1) {
365                 fprintf (stderr, "%8d   %d (%d bytes)\n", i, ape_ctx->seektable[i], ape_ctx->seektable[i + 1] - ape_ctx->seektable[i]);
366             } else {
367                 fprintf (stderr, "%8d   %d\n", i, ape_ctx->seektable[i]);
368             }
369         }
370     }
371 
372     fprintf (stderr, "\nFrames\n\n");
373     for (i = 0; i < ape_ctx->totalframes; i++)
374         fprintf (stderr, "%8d   %8lld %8d (%d samples)\n", i, ape_ctx->frames[i].pos, ape_ctx->frames[i].size, ape_ctx->frames[i].nblocks);
375 
376     fprintf (stderr, "\nCalculated information:\n\n");
377     fprintf (stderr, "junklength           = %d\n", ape_ctx->junklength);
378     fprintf (stderr, "firstframe           = %d\n", ape_ctx->firstframe);
379     fprintf (stderr, "totalsamples         = %d\n", ape_ctx->totalsamples);
380 #endif
381 }
382 
383 static int
ape_read_header(DB_FILE * fp,APEContext * ape)384 ape_read_header(DB_FILE *fp, APEContext *ape)
385 {
386     int i;
387     int total_blocks;
388 
389     /* TODO: Skip any leading junk such as id3v2 tags */
390     ape->junklength = 0;
391 
392     if (deadbeef->fread (ape->magic, 1, 4, fp) != 4) {
393         return -1;
394     }
395     if (memcmp (ape->magic, "MAC ", 4))
396         return -1;
397 
398     if (read_uint16 (fp, &ape->fileversion) < 0) {
399         return -1;
400     }
401 
402     if (ape->fileversion < APE_MIN_VERSION) {
403         fprintf (stderr, "ape: Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
404         return -1;
405     }
406 
407     if (ape->fileversion >= 3980) {
408         if (read_uint16 (fp, &ape->padding1) < 0) {
409             return -1;
410         }
411         if (read_uint32 (fp, &ape->descriptorlength) < 0) {
412             return -1;
413         }
414         if (read_uint32 (fp, &ape->headerlength) < 0) {
415             return -1;
416         }
417         if (read_uint32 (fp, &ape->seektablelength) < 0) {
418             return -1;
419         }
420         if (read_uint32 (fp, &ape->wavheaderlength) < 0) {
421             return -1;
422         }
423         if (read_uint32 (fp, &ape->audiodatalength) < 0) {
424             return -1;
425         }
426         if (read_uint32 (fp, &ape->audiodatalength_high) < 0) {
427             return -1;
428         }
429         if (read_uint32 (fp, &ape->wavtaillength) < 0) {
430             return -1;
431         }
432         if (deadbeef->fread (ape->md5, 1, 16, fp) != 16) {
433             return -1;
434         }
435 
436         /* Skip any unknown bytes at the end of the descriptor.
437            This is for future compatibility */
438         if (ape->descriptorlength > 52) {
439             if (deadbeef->fseek (fp, ape->descriptorlength - 52, SEEK_CUR)) {
440                 return -1;
441             }
442         }
443 
444         /* Read header data */
445         if (read_uint16 (fp, &ape->compressiontype) < 0) {
446             return -1;
447         }
448         if (read_uint16 (fp, &ape->formatflags) < 0) {
449             return -1;
450         }
451         if (read_uint32 (fp, &ape->blocksperframe) < 0) {
452             return -1;
453         }
454         if (read_uint32 (fp, &ape->finalframeblocks) < 0) {
455             return -1;
456         }
457         if (read_uint32 (fp, & ape->totalframes) < 0) {
458             return -1;
459         }
460         if (read_uint16 (fp, &ape->bps) < 0) {
461             return -1;
462         }
463         if (read_uint16 (fp, &ape->channels) < 0) {
464             return -1;
465         }
466         if (read_uint32 (fp, &ape->samplerate) < 0) {
467             return -1;
468         }
469     } else {
470         ape->descriptorlength = 0;
471         ape->headerlength = 32;
472 
473         if (read_uint16 (fp, &ape->compressiontype) < 0) {
474             return -1;
475         }
476         if (read_uint16 (fp, &ape->formatflags) < 0) {
477             return -1;
478         }
479         if (read_uint16 (fp, &ape->channels) < 0) {
480             return -1;
481         }
482         if (read_uint32 (fp, &ape->samplerate) < 0) {
483             return -1;
484         }
485         if (read_uint32 (fp, &ape->wavheaderlength) < 0) {
486             return -1;
487         }
488         if (read_uint32 (fp, &ape->wavtaillength) < 0) {
489             return -1;
490         }
491         if (read_uint32 (fp, &ape->totalframes) < 0) {
492             return -1;
493         }
494         if (read_uint32 (fp, &ape->finalframeblocks) < 0) {
495             return -1;
496         }
497 
498         if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
499             if (deadbeef->fseek(fp, 4, SEEK_CUR)) { /* Skip the peak level */
500                 return -1;
501             }
502             ape->headerlength += 4;
503         }
504 
505         if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
506             if (read_uint32 (fp, &ape->seektablelength) < 0) {
507                 return -1;
508             };
509             ape->headerlength += 4;
510             ape->seektablelength *= sizeof(int32_t);
511         } else
512             ape->seektablelength = ape->totalframes * sizeof(int32_t);
513 
514         if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
515             ape->bps = 8;
516         else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
517             ape->bps = 24;
518         else
519             ape->bps = 16;
520 
521         if (ape->fileversion >= 3950)
522             ape->blocksperframe = 73728 * 4;
523         else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800  && ape->compressiontype >= 4000))
524             ape->blocksperframe = 73728;
525         else
526             ape->blocksperframe = 9216;
527 
528         /* Skip any stored wav header */
529         if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER)) {
530             if (deadbeef->fseek (fp, ape->wavheaderlength, SEEK_CUR)) {
531                 return -1;
532             }
533         }
534     }
535 
536     if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
537         fprintf (stderr, "ape: Too many frames: %d\n", ape->totalframes);
538         return -1;
539     }
540     ape->frames       = malloc(ape->totalframes * sizeof(APEFrame));
541     if(!ape->frames)
542         return -1;
543     ape->firstframe   = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
544     ape->currentframe = 0;
545 
546 
547     ape->totalsamples = ape->finalframeblocks;
548     if (ape->totalframes > 1)
549         ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
550 
551     if (ape->seektablelength > 0) {
552         ape->seektable = malloc(ape->seektablelength);
553         for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++) {
554             if (read_uint32 (fp, &ape->seektable[i]) < 0) {
555                 return -1;
556             }
557         }
558     }
559 
560     ape->frames[0].pos     = ape->firstframe;
561     ape->frames[0].nblocks = ape->blocksperframe;
562     ape->frames[0].skip    = 0;
563     for (i = 1; i < ape->totalframes; i++) {
564         ape->frames[i].pos      = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe;
565         ape->frames[i].nblocks  = ape->blocksperframe;
566         ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
567         ape->frames[i].skip     = (ape->frames[i].pos - ape->frames[0].pos) & 3;
568     }
569     ape->frames[ape->totalframes - 1].size    = ape->finalframeblocks * 4;
570     ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
571 
572     for (i = 0; i < ape->totalframes; i++) {
573         if(ape->frames[i].skip){
574             ape->frames[i].pos  -= ape->frames[i].skip;
575             ape->frames[i].size += ape->frames[i].skip;
576         }
577         ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
578     }
579 
580 
581     ape_dumpinfo(ape);
582 
583 #if ENABLE_DEBUG
584     fprintf (stderr, "ape: Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
585 #endif
586 
587     total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
588 
589     return 0;
590 }
591 
592 #   define AV_WB32(p, d) do {                   \
593         ((uint8_t*)(p))[3] = (d);               \
594         ((uint8_t*)(p))[2] = (d)>>8;            \
595         ((uint8_t*)(p))[1] = (d)>>16;           \
596         ((uint8_t*)(p))[0] = (d)>>24;           \
597     } while(0)
598 
599 #define AV_WL32(p, v) AV_WB32(p, bswap_32(v))
600 
bswap_32(uint32_t x)601 static inline const uint32_t bswap_32(uint32_t x)
602 {
603     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
604     x= (x>>16) | (x<<16);
605     return x;
606 }
607 
ape_read_packet(DB_FILE * fp,APEContext * ape_ctx)608 static int ape_read_packet(DB_FILE *fp, APEContext *ape_ctx)
609 {
610     int ret;
611     int nblocks;
612     APEContext *ape = ape_ctx;
613     uint32_t extra_size = 8;
614 
615     if (ape->currentframe > ape->totalframes)
616         return -1;
617     trace ("seeking to packet %d (%lld + %d)\n", ape->currentframe, ape->frames[ape->currentframe].pos, ape_ctx->skip_header);
618     if (deadbeef->fseek (fp, ape->frames[ape->currentframe].pos + ape_ctx->skip_header, SEEK_SET) != 0) {
619         return -1;
620     }
621 
622     /* Calculate how many blocks there are in this frame */
623     if (ape->currentframe == (ape->totalframes - 1))
624         nblocks = ape->finalframeblocks;
625     else
626         nblocks = ape->blocksperframe;
627 
628     AV_WL32(ape->packet_data    , nblocks);
629     AV_WL32(ape->packet_data + 4, ape->frames[ape->currentframe].skip);
630 //    packet_sizeleft -= 8;
631 
632 // update bitrate
633     int bitrate = -1;
634     if (nblocks != 0 && ape->frames[ape->currentframe].size != 0) {
635         float sec = (float)nblocks / ape->samplerate;
636         bitrate = ape->frames[ape->currentframe].size / sec * 8;
637     }
638     if (bitrate > 0) {
639         deadbeef->streamer_set_bitrate (bitrate/1000);
640     }
641 
642     int sz = PACKET_BUFFER_SIZE-8;
643     sz = min (sz, ape->frames[ape->currentframe].size);
644 //    fprintf (stderr, "readsize: %d, packetsize: %d\n", sz, ape->frames[ape->currentframe].size);
645     ret = deadbeef->fread (ape->packet_data + extra_size, 1, sz, fp);
646     ape->packet_sizeleft = ape->frames[ape->currentframe].size - sz + 8;
647     ape->packet_remaining = sz+8;
648 
649     ape->currentframe++;
650 
651     return 0;
652 }
653 
654 static void
ape_free_ctx(APEContext * ape_ctx)655 ape_free_ctx (APEContext *ape_ctx) {
656     int i;
657     if (ape_ctx->packet_data) {
658         free (ape_ctx->packet_data);
659         ape_ctx->packet_data = NULL;
660     }
661     if (ape_ctx->frames) {
662         free (ape_ctx->frames);
663         ape_ctx->frames = NULL;
664     }
665     if (ape_ctx->seektable) {
666         free (ape_ctx->seektable);
667         ape_ctx->seektable = NULL;
668     }
669     for (i = 0; i < APE_FILTER_LEVELS; i++) {
670         if (ape_ctx->filterbuf[i]) {
671             free (ape_ctx->filterbuf[i]);
672             ape_ctx->filterbuf[i] = NULL;
673         }
674     }
675     memset (ape_ctx, 0, sizeof (APEContext));
676 }
677 
678 static void
ffap_free(DB_fileinfo_t * _info)679 ffap_free (DB_fileinfo_t *_info)
680 {
681     ape_info_t *info = (ape_info_t *)_info;
682     ape_free_ctx (&info->ape_ctx);
683     if (info->fp) {
684         deadbeef->fclose (info->fp);
685     }
686     free (info);
687 }
688 
689 static DB_fileinfo_t *
ffap_open(uint32_t hints)690 ffap_open (uint32_t hints) {
691     DB_fileinfo_t *_info = malloc (sizeof (ape_info_t));
692     memset (_info, 0, sizeof (ape_info_t));
693     return _info;
694 }
695 
696 static int
ffap_init(DB_fileinfo_t * _info,DB_playItem_t * it)697 ffap_init (DB_fileinfo_t *_info, DB_playItem_t *it)
698 {
699     ape_info_t *info = (ape_info_t*)_info;
700 
701     deadbeef->pl_lock ();
702     info->fp = deadbeef->fopen (deadbeef->pl_find_meta (it, ":URI"));
703     deadbeef->pl_unlock ();
704     if (!info->fp) {
705         return -1;
706     }
707     memset (&info->ape_ctx, 0, sizeof (info->ape_ctx));
708     int skip = deadbeef->junk_get_leading_size (info->fp);
709     if (skip > 0) {
710         if (deadbeef->fseek (info->fp, skip, SEEK_SET)) {
711             return -1;
712         }
713         info->ape_ctx.skip_header = skip;
714     }
715     if (ape_read_header (info->fp, &info->ape_ctx)) {
716         return -1;
717     }
718     int i;
719 
720     if (info->ape_ctx.channels > 2) {
721         fprintf (stderr, "ape: Only mono and stereo is supported\n");
722         return -1;
723     }
724 
725 #if ENABLE_DEBUG
726     fprintf (stderr, "ape: Compression Level: %d - Flags: %d\n", info->ape_ctx.compressiontype, info->ape_ctx.formatflags);
727 #endif
728     if (info->ape_ctx.compressiontype % 1000 || info->ape_ctx.compressiontype > COMPRESSION_LEVEL_INSANE) {
729         fprintf (stderr, "ape: Incorrect compression level %d\n", info->ape_ctx.compressiontype);
730         return -1;
731     }
732     info->ape_ctx.fset = info->ape_ctx.compressiontype / 1000 - 1;
733     for (i = 0; i < APE_FILTER_LEVELS; i++) {
734         if (!ape_filter_orders[info->ape_ctx.fset][i])
735             break;
736         info->ape_ctx.filterbuf_size[i] = (ape_filter_orders[info->ape_ctx.fset][i] * 3 + HISTORY_SIZE) * 4;
737         int err = posix_memalign ((void **)&info->ape_ctx.filterbuf[i], 16, info->ape_ctx.filterbuf_size[i]);
738         if (err) {
739             trace ("ffap: out of memory (posix_memalign)\n");
740             return -1;
741         }
742     }
743 
744     _info->plugin = &plugin;
745     _info->fmt.bps = info->ape_ctx.bps;
746     _info->fmt.samplerate = info->ape_ctx.samplerate;
747     _info->fmt.channels = info->ape_ctx.channels;
748     _info->fmt.channelmask = _info->fmt.channels == 1 ? DDB_SPEAKER_FRONT_LEFT : (DDB_SPEAKER_FRONT_LEFT | DDB_SPEAKER_FRONT_RIGHT);
749     _info->readpos = 0;
750 
751     info->ape_ctx.packet_data = malloc (PACKET_BUFFER_SIZE);
752     if (!info->ape_ctx.packet_data) {
753         fprintf (stderr, "ape: failed to allocate memory for packet data\n");
754         return -1;
755     }
756 
757     if (it->endsample > 0) {
758         info->startsample = it->startsample;
759         info->endsample = it->endsample;
760         plugin.seek_sample (_info, 0);
761         //trace ("start: %d/%f, end: %d/%f\n", startsample, timestart, endsample, timeend);
762     }
763     else {
764         info->startsample = 0;
765         info->endsample = info->ape_ctx.totalsamples-1;
766     }
767 
768     return 0;
769 }
770 
771 /**
772  * @defgroup rangecoder APE range decoder
773  * @{
774  */
775 
776 #define CODE_BITS    32
777 #define TOP_VALUE    ((unsigned int)1 << (CODE_BITS-1))
778 #define SHIFT_BITS   (CODE_BITS - 9)
779 #define EXTRA_BITS   ((CODE_BITS-2) % 8 + 1)
780 #define BOTTOM_VALUE (TOP_VALUE >> 8)
781 
782 /** Start the decoder */
range_start_decoding(APEContext * ctx)783 static inline void range_start_decoding(APEContext * ctx)
784 {
785     ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
786     ctx->rc.low    = ctx->rc.buffer >> (8 - EXTRA_BITS);
787     ctx->rc.range  = (uint32_t) 1 << EXTRA_BITS;
788 }
789 
790 /** Perform normalization */
range_dec_normalize(APEContext * ctx)791 static inline void range_dec_normalize(APEContext * ctx)
792 {
793     while (ctx->rc.range <= BOTTOM_VALUE) {
794         ctx->rc.buffer <<= 8;
795         if(ctx->ptr < ctx->data_end)
796             ctx->rc.buffer += *ctx->ptr;
797         ctx->ptr++;
798         ctx->rc.low    = (ctx->rc.low << 8)    | ((ctx->rc.buffer >> 1) & 0xFF);
799         ctx->rc.range  <<= 8;
800     }
801 }
802 
803 /**
804  * Calculate culmulative frequency for next symbol. Does NO update!
805  * @param ctx decoder context
806  * @param tot_f is the total frequency or (code_value)1<<shift
807  * @return the culmulative frequency
808  */
range_decode_culfreq(APEContext * ctx,int tot_f)809 static inline int range_decode_culfreq(APEContext * ctx, int tot_f)
810 {
811     range_dec_normalize(ctx);
812     ctx->rc.help = ctx->rc.range / tot_f;
813     return ctx->rc.low / ctx->rc.help;
814 }
815 
816 /**
817  * Decode value with given size in bits
818  * @param ctx decoder context
819  * @param shift number of bits to decode
820  */
range_decode_culshift(APEContext * ctx,int shift)821 static inline int range_decode_culshift(APEContext * ctx, int shift)
822 {
823     range_dec_normalize(ctx);
824     ctx->rc.help = ctx->rc.range >> shift;
825     return ctx->rc.low / ctx->rc.help;
826 }
827 
828 
829 /**
830  * Update decoding state
831  * @param ctx decoder context
832  * @param sy_f the interval length (frequency of the symbol)
833  * @param lt_f the lower end (frequency sum of < symbols)
834  */
range_decode_update(APEContext * ctx,int sy_f,int lt_f)835 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f)
836 {
837     ctx->rc.low  -= ctx->rc.help * lt_f;
838     ctx->rc.range = ctx->rc.help * sy_f;
839 }
840 
841 /** Decode n bits (n <= 16) without modelling */
range_decode_bits(APEContext * ctx,int n)842 static inline int range_decode_bits(APEContext * ctx, int n)
843 {
844     int sym = range_decode_culshift(ctx, n);
845     range_decode_update(ctx, 1, sym);
846     return sym;
847 }
848 
849 
850 #define MODEL_ELEMENTS 64
851 
852 /**
853  * Fixed probabilities for symbols in Monkey Audio version 3.97
854  */
855 static const uint16_t counts_3970[22] = {
856         0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
857     62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
858     65450, 65469, 65480, 65487, 65491, 65493,
859 };
860 
861 /**
862  * Probability ranges for symbols in Monkey Audio version 3.97
863  */
864 static const uint16_t counts_diff_3970[21] = {
865     14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
866     1104, 677, 415, 248, 150, 89, 54, 31,
867     19, 11, 7, 4, 2,
868 };
869 
870 /**
871  * Fixed probabilities for symbols in Monkey Audio version 3.98
872  */
873 static const uint16_t counts_3980[22] = {
874         0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
875     64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
876     65485, 65488, 65490, 65491, 65492, 65493,
877 };
878 
879 /**
880  * Probability ranges for symbols in Monkey Audio version 3.98
881  */
882 static const uint16_t counts_diff_3980[21] = {
883     19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
884     261, 119, 65, 31, 19, 10, 6, 3,
885     3, 2, 1, 1, 1,
886 };
887 
888 /**
889  * Decode symbol
890  * @param ctx decoder context
891  * @param counts probability range start position
892  * @param counts_diff probability range widths
893  */
range_get_symbol(APEContext * ctx,const uint16_t counts[],const uint16_t counts_diff[])894 static inline int range_get_symbol(APEContext * ctx,
895                                    const uint16_t counts[],
896                                    const uint16_t counts_diff[])
897 {
898     int symbol, cf;
899 
900     cf = range_decode_culshift(ctx, 16);
901 
902     if(cf > 65492){
903         symbol= cf - 65535 + 63;
904         range_decode_update(ctx, 1, cf);
905         if(unlikely (cf > 65535)) {
906             ctx->error=1;
907         }
908         return symbol;
909     }
910     /* figure out the symbol inefficiently; a binary search would be much better */
911     for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
912 
913     range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
914 
915     return symbol;
916 }
917 /** @} */ // group rangecoder
918 
update_rice(APERice * rice,int x)919 static inline void update_rice(APERice *rice, int x)
920 {
921     int lim = rice->k ? (1 << (rice->k + 4)) : 0;
922     rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
923 
924     if (rice->ksum < lim)
925         rice->k--;
926     else if (rice->ksum >= (1 << (rice->k + 5)))
927         rice->k++;
928 }
929 
ape_decode_value(APEContext * ctx,APERice * rice)930 static inline int ape_decode_value(APEContext * ctx, APERice *rice)
931 {
932     int x, overflow;
933 
934     if (ctx->fileversion < 3990) {
935         int tmpk;
936 
937         overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
938 
939         if (overflow == (MODEL_ELEMENTS - 1)) {
940             tmpk = range_decode_bits(ctx, 5);
941             overflow = 0;
942         } else
943             tmpk = (rice->k < 1) ? 0 : rice->k - 1;
944 
945         if (tmpk <= 16)
946             x = range_decode_bits(ctx, tmpk);
947         else {
948             x = range_decode_bits(ctx, 16);
949             x |= (range_decode_bits(ctx, tmpk - 16) << 16);
950         }
951         x += overflow << tmpk;
952     } else {
953         int base, pivot;
954 
955         pivot = rice->ksum >> 5;
956         if (pivot == 0)
957             pivot = 1;
958 
959         overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
960 
961         if (overflow == (MODEL_ELEMENTS - 1)) {
962             overflow  = range_decode_bits(ctx, 16) << 16;
963             overflow |= range_decode_bits(ctx, 16);
964         }
965 
966         if (pivot >= 0x10000) {
967             /* Codepath for 24-bit streams */
968             int nbits, lo_bits, base_hi, base_lo;
969 
970             /* Count the number of bits in pivot */
971             nbits = 17; /* We know there must be at least 17 bits */
972             while ((pivot >> nbits) > 0) { nbits++; }
973 
974             /* base_lo is the low (nbits-16) bits of base
975                base_hi is the high 16 bits of base
976                */
977             lo_bits = (nbits - 16);
978 
979             // {{{ unrolled base_hi = range_decode_culfreq(ctx, (pivot >> lo_bits) + 1)
980             range_dec_normalize(ctx);
981             ctx->rc.help = ctx->rc.range / ((pivot >> lo_bits) + 1);
982             if (unlikely (ctx->rc.help == 0)) {
983                 trace ("rc.help=0\n");
984                 ctx->error = 1;
985                 return 0;
986             }
987             base_hi = ctx->rc.low / ctx->rc.help;
988             // }}}
989             range_decode_update(ctx, 1, base_hi);
990 
991             base_lo = range_decode_culshift(ctx, lo_bits);
992             range_decode_update(ctx, 1, base_lo);
993 
994             base = (base_hi << lo_bits) + base_lo;
995         }
996         else {
997             // {{{ unrolled base = range_decode_culfreq(ctx, pivot)
998             range_dec_normalize(ctx);
999             ctx->rc.help = ctx->rc.range / pivot;
1000             if (unlikely (ctx->rc.help == 0)) {
1001                 ctx->error = 1;
1002                 return 0;
1003             }
1004             base = ctx->rc.low / ctx->rc.help;
1005             // }}}
1006             range_decode_update(ctx, 1, base);
1007         }
1008 
1009         x = base + overflow * pivot;
1010     }
1011 
1012     update_rice(rice, x);
1013 
1014     /* Convert to signed */
1015     if (x & 1)
1016         return (x >> 1) + 1;
1017     else
1018         return -(x >> 1);
1019 }
1020 
entropy_decode(APEContext * ctx,int blockstodecode,int stereo)1021 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo)
1022 {
1023     int32_t *decoded0 = ctx->decoded0;
1024     int32_t *decoded1 = ctx->decoded1;
1025 
1026     ctx->blocksdecoded = blockstodecode;
1027 
1028     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1029         /* We are pure silence, just memset the output buffer. */
1030         memset(decoded0, 0, blockstodecode * sizeof(int32_t));
1031         memset(decoded1, 0, blockstodecode * sizeof(int32_t));
1032     } else {
1033         while (likely (blockstodecode--)) {
1034             *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
1035             if (stereo)
1036                 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
1037         }
1038     }
1039 
1040     if (ctx->blocksdecoded == ctx->currentframeblocks)
1041         range_dec_normalize(ctx);   /* normalize to use up all bytes */
1042 }
1043 
init_entropy_decoder(APEContext * ctx)1044 static void init_entropy_decoder(APEContext * ctx)
1045 {
1046     /* Read the CRC */
1047     ctx->CRC = bytestream_get_be32(&ctx->ptr);
1048 
1049     /* Read the frame flags if they exist */
1050     ctx->frameflags = 0;
1051     if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
1052         ctx->CRC &= ~0x80000000;
1053 
1054         ctx->frameflags = bytestream_get_be32(&ctx->ptr);
1055     }
1056 
1057     /* Keep a count of the blocks decoded in this frame */
1058     ctx->blocksdecoded = 0;
1059 
1060     /* Initialize the rice structs */
1061     ctx->riceX.k = 10;
1062     ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
1063     ctx->riceY.k = 10;
1064     ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
1065 
1066     /* The first 8 bits of input are ignored. */
1067     ctx->ptr++;
1068 
1069     range_start_decoding(ctx);
1070 }
1071 
1072 static const int32_t initial_coeffs[4] = {
1073     360, 317, -109, 98
1074 };
1075 
init_predictor_decoder(APEContext * ctx)1076 static void init_predictor_decoder(APEContext * ctx)
1077 {
1078     APEPredictor *p = &ctx->predictor;
1079 
1080     /* Zero the history buffers */
1081     memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
1082     p->buf = p->historybuffer;
1083 
1084     /* Initialize and zero the coefficients */
1085     memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
1086     memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
1087     memset(p->coeffsB, 0, sizeof(p->coeffsB));
1088 
1089     p->filterA[0] = p->filterA[1] = 0;
1090     p->filterB[0] = p->filterB[1] = 0;
1091     p->lastA[0]   = p->lastA[1]   = 0;
1092 }
1093 
1094 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
APESIGN(int32_t x)1095 static inline int APESIGN(int32_t x) {
1096     return (x < 0) - (x > 0);
1097 }
1098 
predictor_update_filter(APEPredictor * p,const int decoded,const int filter,const int delayA,const int delayB,const int adaptA,const int adaptB)1099 static int predictor_update_filter(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
1100 {
1101     int32_t predictionA, predictionB;
1102 
1103     p->buf[delayA]     = p->lastA[filter];
1104     p->buf[adaptA]     = APESIGN(p->buf[delayA]);
1105     p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
1106     p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1107 
1108     predictionA = p->buf[delayA    ] * p->coeffsA[filter][0] +
1109                   p->buf[delayA - 1] * p->coeffsA[filter][1] +
1110                   p->buf[delayA - 2] * p->coeffsA[filter][2] +
1111                   p->buf[delayA - 3] * p->coeffsA[filter][3];
1112 
1113     /*  Apply a scaled first-order filter compression */
1114     p->buf[delayB]     = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
1115     p->buf[adaptB]     = APESIGN(p->buf[delayB]);
1116     p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
1117     p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1118     p->filterB[filter] = p->filterA[filter ^ 1];
1119 
1120     predictionB = p->buf[delayB    ] * p->coeffsB[filter][0] +
1121                   p->buf[delayB - 1] * p->coeffsB[filter][1] +
1122                   p->buf[delayB - 2] * p->coeffsB[filter][2] +
1123                   p->buf[delayB - 3] * p->coeffsB[filter][3] +
1124                   p->buf[delayB - 4] * p->coeffsB[filter][4];
1125 
1126     p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1127     p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
1128 
1129     if (!decoded) // no need updating filter coefficients
1130         return p->filterA[filter];
1131 
1132     if (decoded > 0) {
1133         p->coeffsA[filter][0] -= p->buf[adaptA    ];
1134         p->coeffsA[filter][1] -= p->buf[adaptA - 1];
1135         p->coeffsA[filter][2] -= p->buf[adaptA - 2];
1136         p->coeffsA[filter][3] -= p->buf[adaptA - 3];
1137 
1138         p->coeffsB[filter][0] -= p->buf[adaptB    ];
1139         p->coeffsB[filter][1] -= p->buf[adaptB - 1];
1140         p->coeffsB[filter][2] -= p->buf[adaptB - 2];
1141         p->coeffsB[filter][3] -= p->buf[adaptB - 3];
1142         p->coeffsB[filter][4] -= p->buf[adaptB - 4];
1143     } else {
1144         p->coeffsA[filter][0] += p->buf[adaptA    ];
1145         p->coeffsA[filter][1] += p->buf[adaptA - 1];
1146         p->coeffsA[filter][2] += p->buf[adaptA - 2];
1147         p->coeffsA[filter][3] += p->buf[adaptA - 3];
1148 
1149         p->coeffsB[filter][0] += p->buf[adaptB    ];
1150         p->coeffsB[filter][1] += p->buf[adaptB - 1];
1151         p->coeffsB[filter][2] += p->buf[adaptB - 2];
1152         p->coeffsB[filter][3] += p->buf[adaptB - 3];
1153         p->coeffsB[filter][4] += p->buf[adaptB - 4];
1154     }
1155     return p->filterA[filter];
1156 }
1157 
predictor_decode_stereo(APEContext * ctx,int count)1158 static void predictor_decode_stereo(APEContext * ctx, int count)
1159 {
1160     int32_t predictionA, predictionB;
1161     APEPredictor *p = &ctx->predictor;
1162     int32_t *decoded0 = ctx->decoded0;
1163     int32_t *decoded1 = ctx->decoded1;
1164 
1165     while (count--) {
1166         /* Predictor Y */
1167         predictionA = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB);
1168         predictionB = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB);
1169         *(decoded0++) = predictionA;
1170         *(decoded1++) = predictionB;
1171 
1172         /* Combined */
1173         p->buf++;
1174 
1175         /* Have we filled the history buffer? */
1176         if (p->buf == p->historybuffer + HISTORY_SIZE) {
1177             memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
1178             p->buf = p->historybuffer;
1179         }
1180     }
1181 }
1182 
predictor_decode_mono(APEContext * ctx,int count)1183 static void predictor_decode_mono(APEContext * ctx, int count)
1184 {
1185     APEPredictor *p = &ctx->predictor;
1186     int32_t *decoded0 = ctx->decoded0;
1187     int32_t predictionA, currentA, A;
1188 
1189     currentA = p->lastA[0];
1190 
1191     while (count--) {
1192         A = *decoded0;
1193 
1194         p->buf[YDELAYA] = currentA;
1195         p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
1196 
1197         predictionA = p->buf[YDELAYA    ] * p->coeffsA[0][0] +
1198                       p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
1199                       p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
1200                       p->buf[YDELAYA - 3] * p->coeffsA[0][3];
1201 
1202         currentA = A + (predictionA >> 10);
1203 
1204         p->buf[YADAPTCOEFFSA]     = APESIGN(p->buf[YDELAYA    ]);
1205         p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
1206 
1207         if (A > 0) {
1208             p->coeffsA[0][0] -= p->buf[YADAPTCOEFFSA    ];
1209             p->coeffsA[0][1] -= p->buf[YADAPTCOEFFSA - 1];
1210             p->coeffsA[0][2] -= p->buf[YADAPTCOEFFSA - 2];
1211             p->coeffsA[0][3] -= p->buf[YADAPTCOEFFSA - 3];
1212         } else if (A < 0) {
1213             p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA    ];
1214             p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1];
1215             p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2];
1216             p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3];
1217         }
1218 
1219         p->buf++;
1220 
1221         /* Have we filled the history buffer? */
1222         if (p->buf == p->historybuffer + HISTORY_SIZE) {
1223             memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
1224             p->buf = p->historybuffer;
1225         }
1226 
1227         p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
1228         *(decoded0++) = p->filterA[0];
1229     }
1230 
1231     p->lastA[0] = currentA;
1232 }
1233 
do_init_filter(APEFilter * f,int16_t * buf,int order)1234 static void do_init_filter(APEFilter *f, int16_t * buf, int order)
1235 {
1236     f->coeffs = buf;
1237     f->historybuffer = buf + order;
1238     f->delay       = f->historybuffer + order * 2;
1239     f->adaptcoeffs = f->historybuffer + order;
1240 
1241     memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
1242     memset(f->coeffs, 0, order * sizeof(int16_t));
1243     f->avg = 0;
1244 }
1245 
init_filter(APEContext * ctx,APEFilter * f,int16_t * buf,int order)1246 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order)
1247 {
1248     do_init_filter(&f[0], buf, order);
1249     do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1250 }
1251 
1252 #ifdef HAVE_SSE2
1253 
1254 #if ARCH_X86_64
1255 #    define REG_a "rax"
1256 #    define REG_b "rbx"
1257 #    define REG_c "rcx"
1258 #    define REG_d "rdx"
1259 #    define REG_D "rdi"
1260 #    define REG_S "rsi"
1261 #    define PTR_SIZE "8"
1262 #    define REG_SP "rsp"
1263 #    define REG_BP "rbp"
1264 #    define REGBP   rbp
1265 #    define REGa    rax
1266 #    define REGb    rbx
1267 #    define REGc    rcx
1268 #    define REGd    rdx
1269 #    define REGSP   rsp
1270 typedef int64_t x86_reg;
1271 #elif ARCH_X86_32
1272 #    define REG_a "eax"
1273 #    define REG_b "ebx"
1274 #    define REG_c "ecx"
1275 #    define REG_d "edx"
1276 #    define REG_D "edi"
1277 #    define REG_S "esi"
1278 #    define PTR_SIZE "4"
1279 #    define REG_SP "esp"
1280 #    define REG_BP "ebp"
1281 #    define REGBP   ebp
1282 #    define REGa    eax
1283 #    define REGb    ebx
1284 #    define REGc    ecx
1285 #    define REGd    edx
1286 #    define REGSP   esp
1287 typedef int32_t x86_reg;
1288 #else
1289 #warning unknown arch, SIMD optimizations will be disabled
1290 typedef int x86_reg;
1291 #endif
1292 
1293 typedef struct { uint64_t a, b; } xmm_reg;
1294 #define DECLARE_ALIGNED(n,t,v)      t v __attribute__ ((aligned (n)))
1295 #define DECLARE_ALIGNED_16(t, v) DECLARE_ALIGNED(16, t, v)
1296 #endif
1297 
scalarproduct_and_madd_int16_c(int16_t * v1,const int16_t * v2,const int16_t * v3,int order,int mul)1298 static int32_t scalarproduct_and_madd_int16_c(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul)
1299 {
1300     int res = 0;
1301     while (order--) {
1302         res   += *v1 * *v2++;
1303         *v1++ += mul * *v3++;
1304     }
1305     return res;
1306 }
1307 
1308 static int32_t
1309 (*scalarproduct_and_madd_int16)(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul);
1310 
clip_int16(int a)1311 static inline int16_t clip_int16(int a)
1312 {
1313     if ((a+32768) & ~65535) return (a>>31) ^ 32767;
1314         else                    return a;
1315 }
1316 
bswap_buf(uint32_t * dst,const uint32_t * src,int w)1317 static void bswap_buf(uint32_t *dst, const uint32_t *src, int w){
1318     int i;
1319 
1320     for(i=0; i+8<=w; i+=8){
1321         dst[i+0]= bswap_32(src[i+0]);
1322         dst[i+1]= bswap_32(src[i+1]);
1323         dst[i+2]= bswap_32(src[i+2]);
1324         dst[i+3]= bswap_32(src[i+3]);
1325         dst[i+4]= bswap_32(src[i+4]);
1326         dst[i+5]= bswap_32(src[i+5]);
1327         dst[i+6]= bswap_32(src[i+6]);
1328         dst[i+7]= bswap_32(src[i+7]);
1329     }
1330     for(;i<w; i++){
1331         dst[i+0]= bswap_32(src[i+0]);
1332     }
1333 }
1334 
1335 
do_apply_filter(APEContext * ctx,int version,APEFilter * f,int32_t * data,int count,int order,int fracbits)1336 static inline void do_apply_filter(APEContext * ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
1337 {
1338     int res;
1339     int absres;
1340 
1341     while (count--) {
1342         res = scalarproduct_and_madd_int16(f->coeffs, f->delay - order, f->adaptcoeffs - order, order, APESIGN(*data));
1343         res = (res + (1 << (fracbits - 1))) >> fracbits;
1344         res += *data;
1345 
1346         *data++ = res;
1347 
1348         /* Update the output history */
1349         *f->delay++ = clip_int16(res);
1350 
1351         if (version < 3980) {
1352             /* Version ??? to < 3.98 files (untested) */
1353             f->adaptcoeffs[0]  = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1354             f->adaptcoeffs[-4] >>= 1;
1355             f->adaptcoeffs[-8] >>= 1;
1356         } else {
1357             /* Version 3.98 and later files */
1358 
1359             /* Update the adaption coefficients */
1360             absres = (res < 0 ? -res : res);
1361 
1362             if (absres > (f->avg * 3))
1363                 *f->adaptcoeffs = ((res >> 25) & 64) - 32;
1364             else if (absres > (f->avg * 4) / 3)
1365                 *f->adaptcoeffs = ((res >> 26) & 32) - 16;
1366             else if (absres > 0)
1367                 *f->adaptcoeffs = ((res >> 27) & 16) - 8;
1368             else
1369                 *f->adaptcoeffs = 0;
1370 
1371             f->avg += (absres - f->avg) / 16;
1372 
1373             f->adaptcoeffs[-1] >>= 1;
1374             f->adaptcoeffs[-2] >>= 1;
1375             f->adaptcoeffs[-8] >>= 1;
1376         }
1377 
1378         f->adaptcoeffs++;
1379 
1380         /* Have we filled the history buffer? */
1381         if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1382             memmove(f->historybuffer, f->delay - (order * 2),
1383                     (order * 2) * sizeof(int16_t));
1384             f->delay = f->historybuffer + order * 2;
1385             f->adaptcoeffs = f->historybuffer + order;
1386         }
1387     }
1388 }
1389 
apply_filter(APEContext * ctx,APEFilter * f,int32_t * data0,int32_t * data1,int count,int order,int fracbits)1390 static void apply_filter(APEContext * ctx, APEFilter *f,
1391                          int32_t * data0, int32_t * data1,
1392                          int count, int order, int fracbits)
1393 {
1394     do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1395     if (data1)
1396         do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1397 }
1398 
ape_apply_filters(APEContext * ctx,int32_t * decoded0,int32_t * decoded1,int count)1399 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0,
1400                               int32_t * decoded1, int count)
1401 {
1402     int i;
1403 
1404     for (i = 0; i < APE_FILTER_LEVELS; i++) {
1405         if (!ape_filter_orders[ctx->fset][i])
1406             break;
1407         apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
1408     }
1409 }
1410 
init_frame_decoder(APEContext * ctx)1411 static void init_frame_decoder(APEContext * ctx)
1412 {
1413     int i;
1414     init_entropy_decoder(ctx);
1415     init_predictor_decoder(ctx);
1416 
1417     for (i = 0; i < APE_FILTER_LEVELS; i++) {
1418         if (!ape_filter_orders[ctx->fset][i])
1419             break;
1420         init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
1421     }
1422 }
1423 
ape_unpack_mono(APEContext * ctx,int count)1424 static void ape_unpack_mono(APEContext * ctx, int count)
1425 {
1426     int32_t left;
1427     int32_t *decoded0 = ctx->decoded0;
1428     int32_t *decoded1 = ctx->decoded1;
1429 
1430     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1431         entropy_decode(ctx, count, 0);
1432         /* We are pure silence, so we're done. */
1433         //fprintf (stderr, "pure silence mono\n");
1434         return;
1435     }
1436 
1437     entropy_decode(ctx, count, 0);
1438     ape_apply_filters(ctx, decoded0, NULL, count);
1439 
1440     /* Now apply the predictor decoding */
1441     predictor_decode_mono(ctx, count);
1442 
1443     /* Pseudo-stereo - just copy left channel to right channel */
1444     if (ctx->channels == 2) {
1445         while (count--) {
1446             left = *decoded0;
1447             *(decoded1++) = *(decoded0++) = left;
1448         }
1449     }
1450 }
1451 
ape_unpack_stereo(APEContext * ctx,int count)1452 static void ape_unpack_stereo(APEContext * ctx, int count)
1453 {
1454     int32_t left, right;
1455     int32_t *decoded0 = ctx->decoded0;
1456     int32_t *decoded1 = ctx->decoded1;
1457 
1458     if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1459         /* We are pure silence, so we're done. */
1460         //fprintf (stderr, "pure silence stereo\n");
1461         return;
1462     }
1463 
1464     entropy_decode(ctx, count, 1);
1465     ape_apply_filters(ctx, decoded0, decoded1, count);
1466 
1467     /* Now apply the predictor decoding */
1468     predictor_decode_stereo(ctx, count);
1469 
1470     /* Decorrelate and scale to output depth */
1471     while (count--) {
1472         left = *decoded1 - (*decoded0 / 2);
1473         right = left + *decoded0;
1474 
1475         *(decoded0++) = left;
1476         *(decoded1++) = right;
1477     }
1478 }
1479 
1480 static int
ape_decode_frame(DB_fileinfo_t * _info,void * data,int * data_size)1481 ape_decode_frame(DB_fileinfo_t *_info, void *data, int *data_size)
1482 {
1483     ape_info_t *info = (ape_info_t*)_info;
1484     APEContext *s = &info->ape_ctx;
1485     char *samples = data;
1486     int nblocks;
1487     int i, n;
1488     int blockstodecode;
1489     int bytes_used;
1490     int samplesize = _info->fmt.bps/8 * s->channels;;
1491 
1492     /* should not happen but who knows */
1493     if (BLOCKS_PER_LOOP * samplesize > *data_size) {
1494         fprintf (stderr, "ape: Packet size is too big! (max is %d while you have %d)\n", *data_size, BLOCKS_PER_LOOP * samplesize);
1495         return -1;
1496     }
1497 
1498     if (s->packet_remaining < PACKET_BUFFER_SIZE) {
1499         if (s->samples == 0) {
1500             if (s->currentframe == s->totalframes) {
1501                 return -1;
1502             }
1503             assert (!s->samples);
1504             trace ("start reading packet %d\n", s->currentframe);
1505             assert (s->samples == 0); // all samples from prev packet must have been read
1506             // start new packet
1507             if (ape_read_packet (info->fp, s) < 0) {
1508                 fprintf (stderr, "ape: error reading packet\n");
1509                 return -1;
1510             }
1511             bswap_buf((uint32_t*)(s->packet_data), (const uint32_t*)(s->packet_data), s->packet_remaining >> 2);
1512 
1513             s->ptr = s->last_ptr = s->packet_data;
1514 
1515             nblocks = s->samples = bytestream_get_be32(&s->ptr);
1516 
1517             trace ("s->samples=%d (1)\n", s->samples);
1518             n = bytestream_get_be32(&s->ptr);
1519             if(n < 0 || n > 3){
1520                 trace ("ape: Incorrect offset passed\n");
1521                 return -1;
1522             }
1523             s->ptr += n;
1524 
1525             s->currentframeblocks = nblocks;
1526 
1527             //buf += 4;
1528             if (s->samples <= 0) {
1529                 *data_size = 0;
1530                 bytes_used = s->packet_remaining;
1531                 goto error;
1532             }
1533 
1534             memset(s->decoded0,  0, sizeof(s->decoded0));
1535             memset(s->decoded1,  0, sizeof(s->decoded1));
1536 
1537             /* Initialize the frame decoder */
1538             trace ("init_frame_decoder\n");
1539             init_frame_decoder(s);
1540         }
1541         else {
1542             int sz = PACKET_BUFFER_SIZE - s->packet_remaining;
1543             sz = min (sz, s->packet_sizeleft);
1544             sz = sz&~3;
1545             uint8_t *p = s->packet_data + s->packet_remaining;
1546             int r = deadbeef->fread (p, 1, sz, info->fp);
1547             bswap_buf((uint32_t*)p, (const uint32_t*)p, r >> 2);
1548             s->packet_sizeleft -= r;
1549             s->packet_remaining += r;
1550             //fprintf (stderr, "read more %d bytes for current packet, sizeleft=%d, packet_remaining=%d\n", r, packet_sizeleft, packet_remaining);
1551         }
1552     }
1553     s->data_end = s->packet_data + s->packet_remaining;
1554 
1555     if (s->packet_remaining == 0 && !s->samples) {
1556         *data_size = 0;
1557         return 0;
1558     }
1559     if (!s->packet_remaining) {
1560         fprintf (stderr, "ape: packetbuf is empty!!\n");
1561         *data_size = 0;
1562         bytes_used = s->packet_remaining;
1563         goto error;
1564     }
1565 
1566     nblocks = s->samples;
1567     blockstodecode = min(BLOCKS_PER_LOOP, nblocks);
1568 
1569     s->error=0;
1570 
1571     if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1572         ape_unpack_mono(s, blockstodecode);
1573     else
1574         ape_unpack_stereo(s, blockstodecode);
1575 
1576     if(s->error || s->ptr >= s->data_end){
1577         s->samples=0;
1578         if (s->error) {
1579             fprintf (stderr, "ape: Error decoding frame, error=%d\n", s->error);
1580         }
1581         else {
1582             fprintf (stderr, "ape: Error decoding frame, ptr >= data_end\n");
1583         }
1584         return -1;
1585     }
1586 
1587     int skip = min (s->samplestoskip, blockstodecode);
1588     i = skip;
1589 
1590     if (_info->fmt.bps == 32) {
1591         for (; i < blockstodecode; i++) {
1592             *((int32_t*)samples) = s->decoded0[i];
1593             samples += 4;
1594             if(s->channels > 1) {
1595                 *((int32_t*)samples) = s->decoded1[i];
1596                 samples += 4;
1597             }
1598         }
1599     }
1600     else if (_info->fmt.bps == 24) {
1601         for (; i < blockstodecode; i++) {
1602             int32_t sample = s->decoded0[i];
1603 
1604             samples[0] = sample&0xff;
1605             samples[1] = (sample&0xff00)>>8;
1606             samples[2] = (sample&0xff0000)>>16;
1607             samples += 3;
1608             if(s->channels > 1) {
1609                 sample = s->decoded1[i];
1610                 samples[0] = sample&0xff;
1611                 samples[1] = (sample&0xff00)>>8;
1612                 samples[2] = (sample&0xff0000)>>16;
1613                 samples += 3;
1614             }
1615         }
1616     }
1617     else if (_info->fmt.bps == 16) {
1618         for (; i < blockstodecode; i++) {
1619             *((int16_t*)samples) = (int16_t)s->decoded0[i];
1620             samples += 2;
1621             if(s->channels > 1) {
1622                 *((int16_t*)samples) = (int16_t)s->decoded1[i];
1623                 samples += 2;
1624             }
1625         }
1626     }
1627     else if (_info->fmt.bps == 8) {
1628         for (; i < blockstodecode; i++) {
1629             *samples = (int16_t)s->decoded0[i];
1630             samples++;
1631             if(s->channels > 1) {
1632                 *samples = (int16_t)s->decoded1[i];
1633                 samples++;
1634             }
1635         }
1636     }
1637 
1638     s->samplestoskip -= skip;
1639     s->samples -= blockstodecode;
1640 
1641     *data_size = (blockstodecode - skip) * samplesize;
1642 //    ape_ctx.currentsample += blockstodecode - skip;
1643     bytes_used = s->samples ? s->ptr - s->last_ptr : s->packet_remaining;
1644 
1645     // shift everything
1646 error:
1647     if (bytes_used < s->packet_remaining) {
1648         memmove (s->packet_data, s->packet_data+bytes_used, s->packet_remaining-bytes_used);
1649     }
1650     s->packet_remaining -= bytes_used;
1651     s->ptr -= bytes_used;
1652     s->last_ptr = s->ptr;
1653 
1654     return bytes_used;
1655 }
1656 
1657 static DB_playItem_t *
ffap_insert(ddb_playlist_t * plt,DB_playItem_t * after,const char * fname)1658 ffap_insert (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname) {
1659     APEContext ape_ctx;
1660     memset (&ape_ctx, 0, sizeof (ape_ctx));
1661     DB_FILE *fp = deadbeef->fopen (fname);
1662     if (!fp) {
1663         return NULL;
1664     }
1665 
1666     int64_t fsize = deadbeef->fgetlength (fp);
1667 
1668     int skip = deadbeef->junk_get_leading_size (fp);
1669     if (skip > 0) {
1670         if (deadbeef->fseek (fp, skip, SEEK_SET)) {
1671             goto error;
1672         }
1673     }
1674     if (ape_read_header (fp, &ape_ctx) < 0) {
1675         fprintf (stderr, "ape: failed to read ape header\n");
1676         goto error;
1677     }
1678     if (ape_ctx.fileversion < APE_MIN_VERSION) {
1679         fprintf(stderr, "ape: unsupported file version - %.2f\n", ape_ctx.fileversion/1000.0);
1680         goto error;
1681     }
1682 
1683     float duration = ape_ctx.totalsamples / (float)ape_ctx.samplerate;
1684     DB_playItem_t *it = NULL;
1685     it = deadbeef->pl_item_alloc_init (fname, plugin.plugin.id);
1686     deadbeef->pl_add_meta (it, ":FILETYPE", "APE");
1687     deadbeef->plt_set_item_duration (plt, it, duration);
1688 
1689     /*int v2err = */deadbeef->junk_id3v2_read (it, fp);
1690     int v1err = deadbeef->junk_id3v1_read (it, fp);
1691     if (v1err >= 0) {
1692         if (deadbeef->fseek (fp, -128, SEEK_END)) {
1693             goto error;
1694         }
1695     }
1696     else {
1697         if (deadbeef->fseek (fp, 0, SEEK_END)) {
1698             goto error;
1699         }
1700     }
1701     /*int apeerr = */deadbeef->junk_apev2_read (it, fp);
1702 
1703     deadbeef->fclose (fp);
1704     fp = NULL;
1705 
1706     // embedded cue
1707     deadbeef->pl_lock ();
1708     const char *cuesheet = deadbeef->pl_find_meta (it, "cuesheet");
1709     DB_playItem_t *cue = NULL;
1710     if (cuesheet) {
1711         cue = deadbeef->plt_insert_cue_from_buffer (plt, after, it, cuesheet, strlen (cuesheet), ape_ctx.totalsamples, ape_ctx.samplerate);
1712         if (cue) {
1713             deadbeef->pl_item_unref (it);
1714             deadbeef->pl_item_unref (cue);
1715             deadbeef->pl_unlock ();
1716             ape_free_ctx (&ape_ctx);
1717             return cue;
1718         }
1719     }
1720     deadbeef->pl_unlock ();
1721 
1722     char s[100];
1723     snprintf (s, sizeof (s), "%lld", fsize);
1724     deadbeef->pl_add_meta (it, ":FILE_SIZE", s);
1725     snprintf (s, sizeof (s), "%d", ape_ctx.bps);
1726     deadbeef->pl_add_meta (it, ":BPS", s);
1727     snprintf (s, sizeof (s), "%d", ape_ctx.channels);
1728     deadbeef->pl_add_meta (it, ":CHANNELS", s);
1729     snprintf (s, sizeof (s), "%d", ape_ctx.samplerate);
1730     deadbeef->pl_add_meta (it, ":SAMPLERATE", s);
1731     int br = (int)roundf(fsize / duration * 8 / 1000);
1732     snprintf (s, sizeof (s), "%d", br);
1733     deadbeef->pl_add_meta (it, ":BITRATE", s);
1734 
1735     cue  = deadbeef->plt_insert_cue (plt, after, it, ape_ctx.totalsamples, ape_ctx.samplerate);
1736     if (cue) {
1737         deadbeef->pl_item_unref (it);
1738         deadbeef->pl_item_unref (cue);
1739         ape_free_ctx (&ape_ctx);
1740         return cue;
1741     }
1742 
1743     deadbeef->pl_add_meta (it, "title", NULL);
1744 
1745     after = deadbeef->plt_insert_item (plt, after, it);
1746     deadbeef->pl_item_unref (it);
1747 
1748     ape_free_ctx (&ape_ctx);
1749     return after;
1750 
1751 error:
1752     if (fp) {
1753         deadbeef->fclose (fp);
1754     }
1755     if (ape_ctx.packet_data) {
1756         ape_free_ctx (&ape_ctx);
1757     }
1758     return NULL;
1759 
1760 }
1761 
1762 static int
ffap_read(DB_fileinfo_t * _info,char * buffer,int size)1763 ffap_read (DB_fileinfo_t *_info, char *buffer, int size) {
1764     ape_info_t *info = (ape_info_t*)_info;
1765 
1766     int samplesize = _info->fmt.bps / 8 * info->ape_ctx.channels;
1767 
1768     if (info->ape_ctx.currentsample + size / samplesize > info->endsample) {
1769         size = (info->endsample - info->ape_ctx.currentsample + 1) * samplesize;
1770         trace ("size truncated to %d bytes (%d samples), cursample=%d, info->endsample=%d, totalsamples=%d\n", size, size / samplesize, info->ape_ctx.currentsample, info->endsample, info->ape_ctx.totalsamples);
1771         if (size <= 0) {
1772             return 0;
1773         }
1774     }
1775     int inits = size;
1776     while (size > 0) {
1777         if (info->ape_ctx.remaining > 0) {
1778             int sz = min (size, info->ape_ctx.remaining);
1779             memcpy (buffer, info->ape_ctx.buffer, sz);
1780             buffer += sz;
1781             size -= sz;
1782             if (info->ape_ctx.remaining > sz) {
1783                 memmove (info->ape_ctx.buffer, info->ape_ctx.buffer + sz, info->ape_ctx.remaining-sz);
1784             }
1785             info->ape_ctx.remaining -= sz;
1786             continue;
1787         }
1788         int s = BLOCKS_PER_LOOP * 2 * 2 * 2;
1789         assert (info->ape_ctx.remaining <= s/2);
1790         s -= info->ape_ctx.remaining;
1791         uint8_t *buf = info->ape_ctx.buffer + info->ape_ctx.remaining;
1792         int n = ape_decode_frame (_info, buf, &s);
1793         if (n == -1) {
1794             break;
1795         }
1796         info->ape_ctx.remaining += s;
1797 
1798         int sz = min (size, info->ape_ctx.remaining);
1799         memcpy (buffer, info->ape_ctx.buffer, sz);
1800         buffer += sz;
1801         size -= sz;
1802         if (info->ape_ctx.remaining > sz) {
1803             memmove (info->ape_ctx.buffer, info->ape_ctx.buffer + sz, info->ape_ctx.remaining-sz);
1804         }
1805         info->ape_ctx.remaining -= sz;
1806     }
1807     info->ape_ctx.currentsample += (inits - size) / samplesize;
1808     _info->readpos = (info->ape_ctx.currentsample-info->startsample) / (float)_info->fmt.samplerate;
1809     return inits - size;
1810 }
1811 
1812 static int
ffap_seek_sample(DB_fileinfo_t * _info,int sample)1813 ffap_seek_sample (DB_fileinfo_t *_info, int sample) {
1814     ape_info_t *info = (ape_info_t*)_info;
1815     sample += info->startsample;
1816     trace ("seeking to %d/%d\n", sample, info->ape_ctx.totalsamples);
1817     uint32_t newsample = sample;
1818     if (newsample > info->ape_ctx.totalsamples) {
1819         trace ("eof\n");
1820         return -1;
1821     }
1822     int nframe = newsample / info->ape_ctx.blocksperframe;
1823     if (nframe >= info->ape_ctx.totalframes) {
1824         trace ("eof2\n");
1825         return -1;
1826     }
1827     info->ape_ctx.currentframe = nframe;
1828     info->ape_ctx.samplestoskip = newsample - nframe * info->ape_ctx.blocksperframe;
1829     trace ("seek to sample %d at blockstart\n", nframe * info->ape_ctx.blocksperframe);
1830     trace ("samples to skip: %d\n", info->ape_ctx.samplestoskip);
1831 
1832     // reset decoder
1833     info->ape_ctx.CRC = 0;
1834     info->ape_ctx.frameflags = 0;
1835     info->ape_ctx.currentframeblocks = 0;
1836     info->ape_ctx.blocksdecoded = 0;
1837     memset (&info->ape_ctx.predictor, 0, sizeof (info->ape_ctx.predictor));
1838     memset (info->ape_ctx.decoded0, 0, sizeof (info->ape_ctx.decoded0));
1839     memset (info->ape_ctx.decoded1, 0, sizeof (info->ape_ctx.decoded1));
1840     for (int i = 0; i < APE_FILTER_LEVELS; i++) {
1841         memset (info->ape_ctx.filterbuf[i], 0, info->ape_ctx.filterbuf_size[i]);
1842     }
1843     memset (&info->ape_ctx.rc, 0, sizeof (info->ape_ctx.rc));
1844     memset (&info->ape_ctx.riceX, 0, sizeof (info->ape_ctx.riceX));
1845     memset (&info->ape_ctx.riceY, 0, sizeof (info->ape_ctx.riceY));
1846     memset (info->ape_ctx.filters, 0, sizeof (info->ape_ctx.filters));
1847     memset (info->ape_ctx.packet_data, 0, PACKET_BUFFER_SIZE);
1848     info->ape_ctx.packet_sizeleft = 0;
1849     info->ape_ctx.data_end = NULL;
1850     info->ape_ctx.ptr = NULL;
1851     info->ape_ctx.last_ptr = NULL;
1852     info->ape_ctx.error = 0;
1853     memset (info->ape_ctx.buffer, 0, sizeof (info->ape_ctx.buffer));
1854 
1855     info->ape_ctx.remaining = 0;
1856     info->ape_ctx.packet_remaining = 0;
1857     info->ape_ctx.samples = 0;
1858     info->ape_ctx.currentsample = newsample;
1859     _info->readpos = (float)(newsample-info->startsample)/info->ape_ctx.samplerate;
1860     return 0;
1861 }
1862 
1863 static int
ffap_seek(DB_fileinfo_t * _info,float seconds)1864 ffap_seek (DB_fileinfo_t *_info, float seconds) {
1865     return ffap_seek_sample (_info, seconds * _info->fmt.samplerate);
1866 }
1867 
1868 
ffap_read_metadata(DB_playItem_t * it)1869 static int ffap_read_metadata (DB_playItem_t *it) {
1870     deadbeef->pl_lock ();
1871     DB_FILE *fp = deadbeef->fopen (deadbeef->pl_find_meta (it, ":URI"));
1872     deadbeef->pl_unlock ();
1873     if (!fp) {
1874         return -1;
1875     }
1876     deadbeef->pl_delete_all_meta (it);
1877     /*int apeerr = */deadbeef->junk_apev2_read (it, fp);
1878     /*int v2err = */deadbeef->junk_id3v2_read (it, fp);
1879     /*int v1err = */deadbeef->junk_id3v1_read (it, fp);
1880     deadbeef->pl_add_meta (it, "title", NULL);
1881     deadbeef->fclose (fp);
1882     return 0;
1883 }
1884 
ffap_write_metadata(DB_playItem_t * it)1885 static int ffap_write_metadata (DB_playItem_t *it) {
1886     // get options
1887     int strip_id3v2 = deadbeef->conf_get_int ("ape.strip_id3v2", 0);
1888     int strip_id3v1 = 0;//deadbeef->conf_get_int ("ape.strip_id3v1", 0);
1889     int strip_apev2 = deadbeef->conf_get_int ("ape.strip_apev2", 0);
1890     int write_id3v2 = deadbeef->conf_get_int ("ape.write_id3v2", 0);
1891     int write_id3v1 = 0;//deadbeef->conf_get_int ("ape.write_id3v1", 0);
1892     int write_apev2 = deadbeef->conf_get_int ("ape.write_apev2", 1);
1893 
1894     uint32_t junk_flags = 0;
1895     if (strip_id3v2) {
1896         junk_flags |= JUNK_STRIP_ID3V2;
1897     }
1898     if (strip_id3v1) {
1899         junk_flags |= JUNK_STRIP_ID3V1;
1900     }
1901     if (strip_apev2) {
1902         junk_flags |= JUNK_STRIP_APEV2;
1903     }
1904     if (write_id3v2) {
1905         junk_flags |= JUNK_WRITE_ID3V2;
1906     }
1907     if (write_id3v1) {
1908         junk_flags |= JUNK_WRITE_ID3V1;
1909     }
1910     if (write_apev2) {
1911         junk_flags |= JUNK_WRITE_APEV2;
1912     }
1913 
1914     return deadbeef->junk_rewrite_tags (it, junk_flags, 4, NULL);
1915 }
1916 
1917 static const char *exts[] = { "ape", NULL };
1918 
1919 // define plugin interface
1920 static DB_decoder_t plugin = {
1921     .plugin.api_vmajor = 1,
1922     .plugin.api_vminor = 0,
1923     .plugin.version_major = 1,
1924     .plugin.version_minor = 0,
1925     .plugin.type = DB_PLUGIN_DECODER,
1926     .plugin.id = "ffap",
1927     .plugin.name = "Monkey's Audio (APE) decoder",
1928     .plugin.descr = "APE player based on code from libavc and rockbox",
1929     .plugin.copyright =
1930         "Copyright (C) 2009-2013 Alexey Yakovenko <waker@users.sourceforge.net>\n"
1931         "\n"
1932         "based on apedec from FFMpeg Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>\n"
1933         "based upon libdemac from Dave Chapman.\n"
1934         "\n"
1935         "This program is free software; you can redistribute it and/or\n"
1936         "modify it under the terms of the GNU General Public License\n"
1937         "as published by the Free Software Foundation; either version 2\n"
1938         "of the License, or (at your option) any later version.\n"
1939         "\n"
1940         "This program is distributed in the hope that it will be useful,\n"
1941         "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1942         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
1943         "GNU General Public License for more details.\n"
1944         "\n"
1945         "You should have received a copy of the GNU General Public License\n"
1946         "along with this program; if not, write to the Free Software\n"
1947         "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n"
1948     ,
1949     .plugin.website = "http://deadbeef.sf.net",
1950     .open = ffap_open,
1951     .init = ffap_init,
1952     .free = ffap_free,
1953     .read = ffap_read,
1954     .seek = ffap_seek,
1955     .seek_sample = ffap_seek_sample,
1956     .insert = ffap_insert,
1957     .read_metadata = ffap_read_metadata,
1958     .write_metadata = ffap_write_metadata,
1959     .exts = exts,
1960 };
1961 
1962 #if HAVE_SSE2 && !ARCH_UNKNOWN
1963 
1964 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul);
1965 
1966 #define FF_MM_MMX      0x0001 ///< standard MMX
1967 #define FF_MM_3DNOW    0x0004 ///< AMD 3DNOW
1968 #define FF_MM_MMX2     0x0002 ///< SSE integer functions or AMD MMX ext
1969 #define FF_MM_SSE      0x0008 ///< SSE functions
1970 #define FF_MM_SSE2     0x0010 ///< PIV SSE2 functions
1971 #define FF_MM_3DNOWEXT 0x0020 ///< AMD 3DNowExt
1972 #define FF_MM_SSE3     0x0040 ///< Prescott SSE3 functions
1973 #define FF_MM_SSSE3    0x0080 ///< Conroe SSSE3 functions
1974 #define FF_MM_SSE4     0x0100 ///< Penryn SSE4.1 functions
1975 #define FF_MM_SSE42    0x0200 ///< Nehalem SSE4.2 functions
1976 #define FF_MM_IWMMXT   0x0100 ///< XScale IWMMXT
1977 #define FF_MM_ALTIVEC  0x0001 ///< standard AltiVec
1978 
1979 #ifdef __APPLE__
1980 #define mm_support() FF_MM_SSE2
1981 #else
1982 /* ebx saving is necessary for PIC. gcc seems unable to see it alone */
1983 #define cpuid(index,eax,ebx,ecx,edx)\
1984     __asm__ volatile\
1985         ("mov %%"REG_b", %%"REG_S"\n\t"\
1986          "cpuid\n\t"\
1987          "xchg %%"REG_b", %%"REG_S\
1988          : "=a" (eax), "=S" (ebx),\
1989            "=c" (ecx), "=d" (edx)\
1990          : "0" (index));
1991 
1992 /* Function to test if multimedia instructions are supported...  */
mm_support(void)1993 int mm_support(void)
1994 {
1995     int rval = 0;
1996     int eax, ebx, ecx, edx;
1997     int max_std_level, max_ext_level, std_caps=0, ext_caps=0;
1998 
1999 #if ARCH_X86_32
2000     x86_reg a, c;
2001     __asm__ volatile (
2002         /* See if CPUID instruction is supported ... */
2003         /* ... Get copies of EFLAGS into eax and ecx */
2004         "pushfl\n\t"
2005         "pop %0\n\t"
2006         "mov %0, %1\n\t"
2007 
2008         /* ... Toggle the ID bit in one copy and store */
2009         /*     to the EFLAGS reg */
2010         "xor $0x200000, %0\n\t"
2011         "push %0\n\t"
2012         "popfl\n\t"
2013 
2014         /* ... Get the (hopefully modified) EFLAGS */
2015         "pushfl\n\t"
2016         "pop %0\n\t"
2017         : "=a" (a), "=c" (c)
2018         :
2019         : "cc"
2020         );
2021 
2022     if (a == c) {
2023         trace ("ffap: cpuid is not supported\n");
2024         return 0; /* CPUID not supported */
2025     }
2026 #endif
2027 
2028     cpuid(0, max_std_level, ebx, ecx, edx);
2029 
2030     if(max_std_level >= 1){
2031         cpuid(1, eax, ebx, ecx, std_caps);
2032         if (std_caps & (1<<23))
2033             rval |= FF_MM_MMX;
2034         if (std_caps & (1<<25))
2035             rval |= FF_MM_MMX2
2036 #ifdef HAVE_SSE2
2037                   | FF_MM_SSE;
2038         if (std_caps & (1<<26))
2039             rval |= FF_MM_SSE2;
2040         if (ecx & 1)
2041             rval |= FF_MM_SSE3;
2042         if (ecx & 0x00000200 )
2043             rval |= FF_MM_SSSE3;
2044         if (ecx & 0x00080000 )
2045             rval |= FF_MM_SSE4;
2046         if (ecx & 0x00100000 )
2047             rval |= FF_MM_SSE42;
2048 #endif
2049                   ;
2050     }
2051 
2052     cpuid(0x80000000, max_ext_level, ebx, ecx, edx);
2053 
2054     if(max_ext_level >= 0x80000001){
2055         cpuid(0x80000001, eax, ebx, ecx, ext_caps);
2056         if (ext_caps & (1<<31))
2057             rval |= FF_MM_3DNOW;
2058         if (ext_caps & (1<<30))
2059             rval |= FF_MM_3DNOWEXT;
2060         if (ext_caps & (1<<23))
2061             rval |= FF_MM_MMX;
2062         if (ext_caps & (1<<22))
2063             rval |= FF_MM_MMX2;
2064     }
2065 
2066     return rval;
2067 }
2068 #endif
2069 #endif
2070 
2071 #if ARCH_ARM
2072 int32_t EXTERN_ASMff_scalarproduct_int16_neon(int16_t *v1, int16_t *v2, int len,
2073                                     int shift);
2074 int32_t EXTERN_ASMff_scalarproduct_and_madd_int16_neon(int16_t *v1, const int16_t *v2, const int16_t *v3, int order, int mul);
2075 
2076 #endif
2077 
2078 DB_plugin_t *
ffap_load(DB_functions_t * api)2079 ffap_load (DB_functions_t *api) {
2080     // detect sse2
2081 #if ARCH_ARM
2082         scalarproduct_and_madd_int16 = EXTERN_ASMff_scalarproduct_and_madd_int16_neon;
2083 #elif HAVE_SSE2 && !ARCH_UNKNOWN
2084     trace ("ffap: was compiled with sse2 support\n");
2085     int mm_flags = mm_support ();
2086     if (mm_flags & FF_MM_SSE2) {
2087         trace ("ffap: sse2 support detected\n");
2088         scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
2089     }
2090     else {
2091         trace ("ffap: sse2 is not supported by CPU\n");
2092         scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_c;
2093     }
2094 #else
2095 //    trace ("ffap: sse2 support was not compiled in\n");
2096     scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_c;
2097 #endif
2098     deadbeef = api;
2099     return DB_PLUGIN (&plugin);
2100 }
2101