Debian Codegraph @

src/libchdr_flac.c

raw symbols SCIP
1 /* license:BSD-3-Clause
2 * copyright-holders:Aaron Giles
3 ***************************************************************************
4
5 flac.c
6
7 FLAC compression wrappers
8
9 ***************************************************************************/
10
11 #include <string.h>
12
13 #include "../include/libchdr/flac.h"
14 #include "../include/libchdr/macros.h"
15 #define DR_FLAC_IMPLEMENTATION
16 #define DR_FLAC_NO_STDIO
17 #include "../include/dr_libs/dr_flac.h"
18
19 /***************************************************************************
20 * FLAC DECODER
21 ***************************************************************************
22 */
23
24 static size_t flac_decoder_read_callback(void *userdata, void *buffer, size_t bytes);
25 static drflac_bool32 flac_decoder_seek_callback(void *userdata, int offset, drflac_seek_origin origin);
26 static drflac_bool32 flac_decoder_tell_callback(void *userdata, drflac_int64 *cursor);
27 static void flac_decoder_metadata_callback(void *userdata, drflac_metadata *metadata);
28 static void flac_decoder_write_callback(void *userdata, void *buffer, size_t bytes);
29
30
31 /* getters (valid after reset) */
32 static uint32_t sample_rate(flac_decoder *decoder) { return decoder->sample_rate; }
33 static uint8_t channels(flac_decoder *decoder) { return decoder->channels; }
34 static uint8_t bits_per_sample(flac_decoder *decoder) { return decoder->bits_per_sample; }
35
36 /*-------------------------------------------------
37 * flac_decoder - constructor
38 *-------------------------------------------------
39 */
40
41 int flac_decoder_init(flac_decoder *decoder)
42 {
43 decoder->decoder = NULL;
44 decoder->sample_rate = 0;
45 decoder->channels = 0;
46 decoder->bits_per_sample = 0;
47 decoder->compressed_offset = 0;
48 decoder->compressed_start = NULL;
49 decoder->compressed_length = 0;
50 decoder->compressed2_start = NULL;
51 decoder->compressed2_length = 0;
52 decoder->uncompressed_offset = 0;
53 decoder->uncompressed_length = 0;
54 decoder->uncompressed_swap = 0;
55 return 0;
56 }
57
58 /*-------------------------------------------------
59 * flac_decoder - destructor
60 *-------------------------------------------------
61 */
62
63 void flac_decoder_free(flac_decoder* decoder)
64 {
65 if ((decoder != NULL) && (decoder->decoder != NULL)) {
66 drflac_close((drflac*)decoder->decoder);
67 decoder->decoder = NULL;
68 }
69 }
70
71 /*-------------------------------------------------
72 * reset - reset state with the original
73 * parameters
74 *-------------------------------------------------
75 */
76
77 static int flac_decoder_internal_reset(flac_decoder* decoder)
78 {
79 decoder->compressed_offset = 0;
80 flac_decoder_free(decoder);
81 decoder->decoder = drflac_open_with_metadata(
82 flac_decoder_read_callback, flac_decoder_seek_callback,
83 flac_decoder_tell_callback, flac_decoder_metadata_callback,
84 decoder, NULL);
85 return (decoder->decoder != NULL);
86 }
87
88 /*-------------------------------------------------
89 * reset - reset state with new memory parameters
90 * and a custom-generated header
91 *-------------------------------------------------
92 */
93
94 int flac_decoder_reset(flac_decoder* decoder, uint32_t sample_rate, uint8_t num_channels, uint32_t block_size, const void *buffer, uint32_t length)
95 {
96 /* modify the template header with our parameters */
97 static const uint8_t s_header_template[0x2a] =
98 {
99 0x66, 0x4C, 0x61, 0x43, /* +00: 'fLaC' stream header */
100 0x80, /* +04: metadata block type 0 (STREAMINFO), */
101 /* flagged as last block */
102 0x00, 0x00, 0x22, /* +05: metadata block length = 0x22 */
103 0x00, 0x00, /* +08: minimum block size */
104 0x00, 0x00, /* +0A: maximum block size */
105 0x00, 0x00, 0x00, /* +0C: minimum frame size (0 == unknown) */
106 0x00, 0x00, 0x00, /* +0F: maximum frame size (0 == unknown) */
107 0x0A, 0xC4, 0x42, 0xF0, 0x00, 0x00, 0x00, 0x00, /* +12: sample rate (0x0ac44 == 44100), */
108 /* numchannels (2), sample bits (16), */
109 /* samples in stream (0 == unknown) */
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* +1A: MD5 signature (0 == none) */
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* +2A: start of stream data */
112 };
113 memcpy(decoder->custom_header, s_header_template, sizeof(s_header_template));
114 decoder->custom_header[0x08] = decoder->custom_header[0x0a] = (block_size*num_channels) >> 8;
115 decoder->custom_header[0x09] = decoder->custom_header[0x0b] = (block_size*num_channels) & 0xff;
116 decoder->custom_header[0x12] = sample_rate >> 12;
117 decoder->custom_header[0x13] = sample_rate >> 4;
118 decoder->custom_header[0x14] = (sample_rate << 4) | ((num_channels - 1) << 1);
119
120 /* configure the header ahead of the provided buffer */
121 decoder->compressed_start = (const uint8_t *)(decoder->custom_header);
122 decoder->compressed_length = sizeof(decoder->custom_header);
123 decoder->compressed2_start = (const uint8_t *)(buffer);
124 decoder->compressed2_length = length;
125 return flac_decoder_internal_reset(decoder);
126 }
127
128 /*-------------------------------------------------
129 * decode_interleaved - decode to an interleaved
130 * sound stream
131 *-------------------------------------------------
132 */
133
134 int flac_decoder_decode_interleaved(flac_decoder* decoder, int16_t *samples, uint32_t num_frames, int swap_endian)
135 {
136 int16_t buffer[2352 / sizeof(int16_t)]; /* 2352 is the number of bytes per CD audio sector */
137 uint32_t buf_frames = ARRAY_LENGTH(buffer) / channels(decoder);
138
139 /* configure the uncompressed buffer */
140 memset(decoder->uncompressed_start, 0, sizeof(decoder->uncompressed_start));
141 decoder->uncompressed_start[0] = samples;
142 decoder->uncompressed_offset = 0;
143 decoder->uncompressed_length = num_frames;
144 decoder->uncompressed_swap = swap_endian;
145
146 /* loop until we get everything we want */
147 while (decoder->uncompressed_offset < decoder->uncompressed_length) {
148 uint32_t frames_to_do = MIN(num_frames, buf_frames);
149 if (!drflac_read_pcm_frames_s16((drflac*)decoder->decoder, frames_to_do, buffer))
150 return 0;
151 flac_decoder_write_callback(decoder, buffer, frames_to_do*sizeof(*buffer)*channels(decoder));
152 num_frames -= frames_to_do;
153 }
154 return 1;
155 }
156
157 /*-------------------------------------------------
158 * finish - finish up the decode
159 *-------------------------------------------------
160 */
161
162 uint32_t flac_decoder_finish(flac_decoder* decoder)
163 {
164 /* get the final decoding position and move forward */
165 drflac *flac = (drflac*)decoder->decoder;
166 uint64_t position = decoder->compressed_offset;
167
168 /* ugh... there's no function to obtain bytes used in drflac :-/ */
169 position -= DRFLAC_CACHE_L2_LINES_REMAINING(&flac->bs) * sizeof(drflac_cache_t);
170 position -= DRFLAC_CACHE_L1_BITS_REMAINING(&flac->bs) / 8;
171 position -= flac->bs.unalignedByteCount;
172
173 /* adjust position if we provided the header */
174 if (position == 0)
175 return 0;
176 if (decoder->compressed_start == (const uint8_t *)(decoder->custom_header))
177 position -= decoder->compressed_length;
178
179 flac_decoder_free(decoder);
180 return position;
181 }
182
183 /*-------------------------------------------------
184 * detect_native_endian - detect system endianness
185 *-------------------------------------------------
186 */
187
188 int flac_decoder_detect_native_endian(void)
189 {
190 uint16_t native_endian = 0;
191 *(uint8_t *)(&native_endian) = 1;
192 return (native_endian & 1);
193 }
194
195 /*-------------------------------------------------
196 * read_callback - handle reads from the input
197 * stream
198 *-------------------------------------------------
199 */
200
201 static size_t flac_decoder_read_callback(void *userdata, void *buffer, size_t bytes)
202 {
203 flac_decoder *decoder = (flac_decoder*)userdata;
204 uint8_t *dst = (uint8_t*)buffer;
205
206 /* copy from primary buffer first */
207 uint32_t outputpos = 0;
208 if (outputpos < bytes && decoder->compressed_offset < decoder->compressed_length)
209 {
210 uint32_t bytes_to_copy = MIN(bytes - outputpos, decoder->compressed_length - decoder->compressed_offset);
211 memcpy(&dst[outputpos], decoder->compressed_start + decoder->compressed_offset, bytes_to_copy);
212 outputpos += bytes_to_copy;
213 decoder->compressed_offset += bytes_to_copy;
214 }
215
216 /* once we're out of that, copy from the secondary buffer */
217 if (outputpos < bytes && decoder->compressed_offset < decoder->compressed_length + decoder->compressed2_length)
218 {
219 uint32_t bytes_to_copy = MIN(bytes - outputpos, decoder->compressed2_length - (decoder->compressed_offset - decoder->compressed_length));
220 memcpy(&dst[outputpos], decoder->compressed2_start + decoder->compressed_offset - decoder->compressed_length, bytes_to_copy);
221 outputpos += bytes_to_copy;
222 decoder->compressed_offset += bytes_to_copy;
223 }
224
225 return outputpos;
226 }
227
228 /*-------------------------------------------------
229 * metadata_callback - handle STREAMINFO metadata
230 *-------------------------------------------------
231 */
232
233 static void flac_decoder_metadata_callback(void *userdata, drflac_metadata *metadata)
234 {
235 flac_decoder *decoder = (flac_decoder*)userdata;
236
237 /* ignore all but STREAMINFO metadata */
238 if (metadata->type != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO)
239 return;
240
241 /* parse out the data we care about */
242 decoder->sample_rate = metadata->data.streaminfo.sampleRate;
243 decoder->bits_per_sample = metadata->data.streaminfo.bitsPerSample;
244 decoder->channels = metadata->data.streaminfo.channels;
245 }
246
247 /*-------------------------------------------------
248 * write_callback - handle writes to the output
249 * stream
250 *-------------------------------------------------
251 */
252
253 static void flac_decoder_write_callback(void *userdata, void *buffer, size_t bytes)
254 {
255 int sampnum, chan;
256 int shift, blocksize;
257 flac_decoder * decoder = (flac_decoder *)userdata;
258 int16_t *sampbuf = (int16_t *)buffer;
259 int sampch = channels(decoder);
260 uint32_t offset = decoder->uncompressed_offset;
261 uint16_t usample;
262
263 /* interleaved case */
264 shift = decoder->uncompressed_swap ? 8 : 0;
265 blocksize = bytes / (sampch * sizeof(sampbuf[0]));
266 if (decoder->uncompressed_start[1] == NULL)
267 {
268 int16_t *dest = decoder->uncompressed_start[0] + offset * sampch;
269 for (sampnum = 0; sampnum < blocksize && offset < decoder->uncompressed_length; sampnum++, offset++)
270 for (chan = 0; chan < sampch; chan++) {
271 usample = (uint16_t)*sampbuf++;
272 *dest++ = (int16_t)((usample << shift) | (usample >> shift));
273 }
274 }
275
276 /* non-interleaved case */
277 else
278 {
279 for (sampnum = 0; sampnum < blocksize && offset < decoder->uncompressed_length; sampnum++, offset++)
280 for (chan = 0; chan < sampch; chan++) {
281 usample = (uint16_t)*sampbuf++;
282 if (decoder->uncompressed_start[chan] != NULL)
283 decoder->uncompressed_start[chan][offset] = (int16_t) ((usample << shift) | (usample >> shift));
284 }
285 }
286 decoder->uncompressed_offset = offset;
287 }
288
289
290 /*-------------------------------------------------
291 * seek_callback - handle seeks on the output
292 * stream
293 *-------------------------------------------------
294 */
295
296 static drflac_bool32 flac_decoder_seek_callback(void *userdata, int offset, drflac_seek_origin origin)
297 {
298 flac_decoder * decoder = (flac_decoder *)userdata;
299 uint32_t length = decoder->compressed_length + decoder->compressed2_length;
300
301 if (origin == DRFLAC_SEEK_SET) {
302 uint32_t pos = offset;
303 if (pos <= length) {
304 decoder->compressed_offset = pos;
305 return DRFLAC_TRUE;
306 }
307 } else if (origin == DRFLAC_SEEK_CUR) {
308 uint32_t pos = decoder->compressed_offset + offset;
309 if (pos <= length) {
310 decoder->compressed_offset = pos;
311 return DRFLAC_TRUE;
312 }
313 }
314 return DRFLAC_FALSE;
315 }
316
317
318 /*-------------------------------------------------
319 * tell_callback - handle seeks on the output
320 * stream
321 *-------------------------------------------------
322 */
323
324 static drflac_bool32 flac_decoder_tell_callback(void *userdata, drflac_int64 *cursor)
325 {
326 flac_decoder * decoder = (flac_decoder *)userdata;
327 *cursor = decoder->compressed_offset;
328 return 1;
329 }