summaryrefslogtreecommitdiff
path: root/audio_codec/libape/Ape_decoder.h (plain)
blob: c5dfa109e77e3a85a79e6ec2d8a52b0fad770fb4
1
2/**********************
3***********************/
4#ifndef APE_DECODER_H
5#define APE_DECODER_H
6
7#ifdef ENABLE_CPU2_DECODER
8#include "../TLSF/tlsf.h"
9#endif
10
11
12
13
14#define BLOCKS_PER_LOOP 1024
15#define MAX_CHANNELS 2
16#define MAX_BYTESPERSAMPLE 3
17
18#define APE_FRAMECODE_MONO_SILENCE 1
19#define APE_FRAMECODE_STEREO_SILENCE 3
20#define APE_FRAMECODE_PSEUDO_STEREO 4
21
22#define HISTORY_SIZE 512
23#define PREDICTOR_ORDER 8
24/** Total size of all predictor histories */
25#define PREDICTOR_SIZE 50
26
27#define YDELAYA (18 + PREDICTOR_ORDER*4)
28#define YDELAYB (18 + PREDICTOR_ORDER*3)
29#define XDELAYA (18 + PREDICTOR_ORDER*2)
30#define XDELAYB (18 + PREDICTOR_ORDER)
31
32#define YADAPTCOEFFSA 18
33#define XADAPTCOEFFSA 14
34#define YADAPTCOEFFSB 10
35#define XADAPTCOEFFSB 5
36#define APE_FILTER_LEVELS 3
37#define bswap_32(x) \
38 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
39 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
40
41/**
42 * Possible compression levels
43 * @{
44 */
45enum APECompressionLevel {
46 COMPRESSION_LEVEL_FAST = 1000,
47 COMPRESSION_LEVEL_NORMAL = 2000,
48 COMPRESSION_LEVEL_HIGH = 3000,
49 COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
50 COMPRESSION_LEVEL_INSANE = 5000
51};
52/** Filters applied to the decoded data */
53typedef struct APEFilter {
54 int16_t *coeffs; ///< actual coefficients used in filtering
55 int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients
56 int16_t *historybuffer; ///< filter memory
57 int16_t *delay; ///< filtered values
58
59 int avg;
60} APEFilter;
61
62typedef struct APERice {
63 uint32_t k;
64 uint32_t ksum;
65} APERice;
66
67typedef struct APERangecoder {
68 uint32_t low; ///< low end of interval
69 uint32_t range; ///< length of interval
70 uint32_t help; ///< bytes_to_follow resp. intermediate value
71 unsigned int buffer; ///< buffer for input/output
72} APERangecoder;
73
74/** Filter histories */
75typedef struct APEPredictor {
76 int32_t *buf;
77
78 int32_t lastA[2];
79
80 int32_t filterA[2];
81 int32_t filterB[2];
82
83 int32_t coeffsA[2][4]; ///< adaption coefficients
84 int32_t coeffsB[2][5]; ///< adaption coefficients
85 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
86} APEPredictor;
87
88typedef struct {
89 unsigned current_decoding_frame;
90 unsigned channels;
91 unsigned bits_per_sample;
92 unsigned sample_rate; /* in Hz */
93 unsigned blocksize; /* in samples (per channel) */
94 void *ape_header_context;/*the ape stream info read from the header,should be same as the struct in demuxer*/
95} APE_Codec_Public_t;
96
97typedef struct {
98 void *APE_Decoder;
99 // DSPContext dsp;
100 int channels;
101 int samples; ///< samples left to decode in current frame
102
103 int fileversion; ///< codec version, very important in decoding process
104 int compression_level; ///< compression levels
105 int fset; ///< which filter set to use (calculated from compression level)
106 int flags; ///< global decoder flags
107
108 uint32_t CRC; ///< frame CRC
109 int frameflags; ///< frame flags
110 int currentframeblocks; ///< samples (per channel) in current frame
111 int blocksdecoded; ///< count of decoded samples in current frame
112 APEPredictor predictor; ///< predictor used for final reconstruction
113
114 int32_t decoded0[BLOCKS_PER_LOOP]; ///< decoded data for the first channel
115 int32_t decoded1[BLOCKS_PER_LOOP]; ///< decoded data for the second channel
116
117 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
118
119 APERangecoder rc; ///< rangecoder used to decode actual values
120 APERice riceX; ///< rice code parameters for the second channel
121 APERice riceY; ///< rice code parameters for the first channel
122 APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
123
124 unsigned char *data; ///< current frame data
125 unsigned char *data_end; ///< frame data end
126 const unsigned char *ptr; ///< current position in frame data
127 const unsigned char *last_ptr; ///< position where last 4608-sample block ended
128
129 int error;
130} APE_COdec_Private_t;
131
132typedef struct {
133 APE_Codec_Public_t *public_data;
134 APE_COdec_Private_t *private_data;
135
136} APE_Decoder_t;
137typedef struct {
138 unsigned pos;
139 int nblocks;
140 int size;
141 int skip;
142 unsigned pts;
143} APEFrame;
144
145typedef struct {
146 /* Derived fields */
147 unsigned junklength;
148 unsigned firstframe;
149 unsigned totalsamples;
150 int currentframe;
151 APEFrame *frames;
152 int sync_flag;
153
154 /* Info from Descriptor Block */
155 char magic[4];
156 int fileversion;
157 int padding1;
158 unsigned descriptorlength;
159 unsigned headerlength;
160 unsigned seektablelength;
161 unsigned wavheaderlength;
162 unsigned audiodatalength;
163 unsigned audiodatalength_high;
164 unsigned wavtaillength;
165 char md5[16];
166
167 /* Info from Header Block */
168 unsigned compressiontype;
169 unsigned formatflags;
170 unsigned blocksperframe;
171 unsigned finalframeblocks;
172 unsigned totalframes;
173 unsigned bps;
174 unsigned channels;
175 unsigned samplerate;
176
177 /* Seektable */
178 unsigned *seektable;
179} APEContext;
180
181typedef enum {
182 APE_DECODE_INIT_FINISH = 0,
183 APE_DECODE_INIT_ERROR,
184 APE_DECODE_ONE_FRAME_FINISH,
185 APE_DECODE_REFILL_FRAME_FINISH,
186 APE_DECODE_CONTINUE,
187 APE_DECODE_END_OF_STREAM,
188 APE_DECODE_ERROR_ABORT,
189 APE_DECODE_MEM_MALLOC_ERROR
190} APE_Decode_status_t;
191
192
193APE_Decoder_t* ape_decoder_new(void* ape_head_context);
194APE_Decode_status_t ape_decode_init(APE_Decoder_t *avctx);
195APE_Decode_status_t ape_decode_frame(APE_Decoder_t * avctx, \
196 void *data, int *data_size, \
197 const unsigned char *buf, int buf_size);
198void ape_decoder_delete(APE_Decoder_t *decoder);
199
200typedef struct _APEIOBuf {
201 int bytesLeft;
202 int thislop_decoded_size;
203 unsigned char *readPtr;
204 unsigned lastframesize;
205 short outBuf[MAX_CHANNELS][BLOCKS_PER_LOOP];
206} APEIOBuf;
207
208typedef struct {
209 unsigned bps;
210 unsigned channels;
211 unsigned samplerate;
212 unsigned compressiontype;
213 unsigned formatflags;
214 int fileversion;
215} ape_extra_data;
216#endif
217