1 /* license:BSD-3-Clause
2 * copyright-holders:Aaron Giles
3 ***************************************************************************
7 FLAC compression wrappers
9 ***************************************************************************/
14 #include <libchdr/flac.h>
15 #define DR_FLAC_IMPLEMENTATION
16 #include <dr_libs/dr_flac.h>
18 /***************************************************************************
20 ***************************************************************************
23 static size_t flac_decoder_read_callback(void *userdata, void *buffer, size_t bytes);
24 static drflac_bool32 flac_decoder_seek_callback(void *userdata, int offset, drflac_seek_origin origin);
25 static void flac_decoder_metadata_callback(void *userdata, drflac_metadata *metadata);
26 static void flac_decoder_write_callback(void *userdata, void *buffer, size_t len);
29 /* getters (valid after reset) */
30 static uint32_t sample_rate(flac_decoder *decoder) { return decoder->sample_rate; }
31 static uint8_t channels(flac_decoder *decoder) { return decoder->channels; }
32 static uint8_t bits_per_sample(flac_decoder *decoder) { return decoder->bits_per_sample; }
34 /*-------------------------------------------------
35 * flac_decoder - constructor
36 *-------------------------------------------------
39 int flac_decoder_init(flac_decoder *decoder)
41 decoder->decoder = NULL;
42 decoder->sample_rate = 0;
43 decoder->channels = 0;
44 decoder->bits_per_sample = 0;
45 decoder->compressed_offset = 0;
46 decoder->compressed_start = NULL;
47 decoder->compressed_length = 0;
48 decoder->compressed2_start = NULL;
49 decoder->compressed2_length = 0;
50 decoder->uncompressed_offset = 0;
51 decoder->uncompressed_length = 0;
52 decoder->uncompressed_swap = 0;
56 /*-------------------------------------------------
57 * flac_decoder - destructor
58 *-------------------------------------------------
61 void flac_decoder_free(flac_decoder* decoder)
63 if ((decoder != NULL) && (decoder->decoder != NULL))
64 drflac_close(decoder->decoder);
65 decoder->decoder = NULL;
68 /*-------------------------------------------------
69 * reset - reset state with the original
71 *-------------------------------------------------
74 static int flac_decoder_internal_reset(flac_decoder* decoder)
76 decoder->compressed_offset = 0;
77 flac_decoder_free(decoder);
78 decoder->decoder = drflac_open_with_metadata(
79 flac_decoder_read_callback, flac_decoder_seek_callback,
80 flac_decoder_metadata_callback, decoder, NULL);
81 return (decoder->decoder != NULL);
84 /*-------------------------------------------------
85 * reset - reset state with new memory parameters
86 * and a custom-generated header
87 *-------------------------------------------------
90 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)
92 /* modify the template header with our parameters */
93 static const uint8_t s_header_template[0x2a] =
95 0x66, 0x4C, 0x61, 0x43, /* +00: 'fLaC' stream header */
96 0x80, /* +04: metadata block type 0 (STREAMINFO), */
97 /* flagged as last block */
98 0x00, 0x00, 0x22, /* +05: metadata block length = 0x22 */
99 0x00, 0x00, /* +08: minimum block size */
100 0x00, 0x00, /* +0A: maximum block size */
101 0x00, 0x00, 0x00, /* +0C: minimum frame size (0 == unknown) */
102 0x00, 0x00, 0x00, /* +0F: maximum frame size (0 == unknown) */
103 0x0A, 0xC4, 0x42, 0xF0, 0x00, 0x00, 0x00, 0x00, /* +12: sample rate (0x0ac44 == 44100), */
104 /* numchannels (2), sample bits (16), */
105 /* samples in stream (0 == unknown) */
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* +1A: MD5 signature (0 == none) */
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* +2A: start of stream data */
109 memcpy(decoder->custom_header, s_header_template, sizeof(s_header_template));
110 decoder->custom_header[0x08] = decoder->custom_header[0x0a] = (block_size*num_channels) >> 8;
111 decoder->custom_header[0x09] = decoder->custom_header[0x0b] = (block_size*num_channels) & 0xff;
112 decoder->custom_header[0x12] = sample_rate >> 12;
113 decoder->custom_header[0x13] = sample_rate >> 4;
114 decoder->custom_header[0x14] = (sample_rate << 4) | ((num_channels - 1) << 1);
116 /* configure the header ahead of the provided buffer */
117 decoder->compressed_start = (const uint8_t *)(decoder->custom_header);
118 decoder->compressed_length = sizeof(decoder->custom_header);
119 decoder->compressed2_start = (const uint8_t *)(buffer);
120 decoder->compressed2_length = length;
121 return flac_decoder_internal_reset(decoder);
124 /*-------------------------------------------------
125 * decode_interleaved - decode to an interleaved
127 *-------------------------------------------------
130 int flac_decoder_decode_interleaved(flac_decoder* decoder, int16_t *samples, uint32_t num_samples, int swap_endian)
132 /* configure the uncompressed buffer */
133 memset(decoder->uncompressed_start, 0, sizeof(decoder->uncompressed_start));
134 decoder->uncompressed_start[0] = samples;
135 decoder->uncompressed_offset = 0;
136 decoder->uncompressed_length = num_samples;
137 decoder->uncompressed_swap = swap_endian;
139 #define BUFFER 2352 /* bytes per CD audio sector */
140 int16_t buffer[BUFFER];
141 uint32_t buf_samples = BUFFER / channels(decoder);
142 /* loop until we get everything we want */
143 while (decoder->uncompressed_offset < decoder->uncompressed_length) {
144 uint32_t frames = (num_samples < buf_samples ? num_samples : buf_samples);
145 if (!drflac_read_pcm_frames_s16(decoder->decoder, frames, buffer))
147 flac_decoder_write_callback(decoder, buffer, frames*sizeof(*buffer)*channels(decoder));
148 num_samples -= frames;
153 /*-------------------------------------------------
154 * finish - finish up the decode
155 *-------------------------------------------------
158 uint32_t flac_decoder_finish(flac_decoder* decoder)
160 /* get the final decoding position and move forward */
161 drflac *flac = decoder->decoder;
162 uint64_t position = decoder->compressed_offset;
164 /* ugh... there's no function to obtain bytes used in drflac :-/ */
165 position -= DRFLAC_CACHE_L2_LINES_REMAINING(&flac->bs) * sizeof(drflac_cache_t);
166 position -= DRFLAC_CACHE_L1_BITS_REMAINING(&flac->bs) / 8;
167 position -= flac->bs.unalignedByteCount;
169 /* adjust position if we provided the header */
172 if (decoder->compressed_start == (const uint8_t *)(decoder->custom_header))
173 position -= decoder->compressed_length;
175 flac_decoder_free(decoder);
179 /*-------------------------------------------------
180 * read_callback - handle reads from the input
182 *-------------------------------------------------
185 #define MIN(x, y) ((x) < (y) ? (x) : (y))
187 static size_t flac_decoder_read_callback(void *userdata, void *buffer, size_t bytes)
189 flac_decoder* decoder = (flac_decoder*)userdata;
190 uint8_t *dst = buffer;
192 /* copy from primary buffer first */
193 uint32_t outputpos = 0;
194 if (outputpos < bytes && decoder->compressed_offset < decoder->compressed_length)
196 uint32_t bytes_to_copy = MIN(bytes - outputpos, decoder->compressed_length - decoder->compressed_offset);
197 memcpy(&dst[outputpos], decoder->compressed_start + decoder->compressed_offset, bytes_to_copy);
198 outputpos += bytes_to_copy;
199 decoder->compressed_offset += bytes_to_copy;
202 /* once we're out of that, copy from the secondary buffer */
203 if (outputpos < bytes && decoder->compressed_offset < decoder->compressed_length + decoder->compressed2_length)
205 uint32_t bytes_to_copy = MIN(bytes - outputpos, decoder->compressed2_length - (decoder->compressed_offset - decoder->compressed_length));
206 memcpy(&dst[outputpos], decoder->compressed2_start + decoder->compressed_offset - decoder->compressed_length, bytes_to_copy);
207 outputpos += bytes_to_copy;
208 decoder->compressed_offset += bytes_to_copy;
214 /*-------------------------------------------------
215 * metadata_callback - handle STREAMINFO metadata
216 *-------------------------------------------------
219 static void flac_decoder_metadata_callback(void *userdata, drflac_metadata *metadata)
221 flac_decoder *decoder = userdata;
223 /* ignore all but STREAMINFO metadata */
224 if (metadata->type != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO)
227 /* parse out the data we care about */
228 decoder->sample_rate = metadata->data.streaminfo.sampleRate;
229 decoder->bits_per_sample = metadata->data.streaminfo.bitsPerSample;
230 decoder->channels = metadata->data.streaminfo.channels;
233 /*-------------------------------------------------
234 * write_callback - handle writes to the output
236 *-------------------------------------------------
239 static void flac_decoder_write_callback(void *userdata, void *buffer, size_t bytes)
242 int shift, blocksize;
243 flac_decoder * decoder = (flac_decoder *)userdata;
244 int16_t *sampbuf = (int16_t *)buffer;
245 int sampch = channels(decoder);
246 uint32_t offset = decoder->uncompressed_offset;
249 /* interleaved case */
250 shift = decoder->uncompressed_swap ? 8 : 0;
251 blocksize = bytes / (sampch * sizeof(sampbuf[0]));
252 if (decoder->uncompressed_start[1] == NULL)
254 int16_t *dest = decoder->uncompressed_start[0] + offset * sampch;
255 for (sampnum = 0; sampnum < blocksize && offset < decoder->uncompressed_length; sampnum++, offset++)
256 for (chan = 0; chan < sampch; chan++) {
257 usample = (uint16_t)*sampbuf++;
258 *dest++ = (int16_t)((usample << shift) | (usample >> shift));
262 /* non-interleaved case */
265 for (sampnum = 0; sampnum < blocksize && offset < decoder->uncompressed_length; sampnum++, offset++)
266 for (chan = 0; chan < sampch; chan++) {
267 usample = (uint16_t)*sampbuf++;
268 if (decoder->uncompressed_start[chan] != NULL)
269 decoder->uncompressed_start[chan][offset] = (int16_t) ((usample << shift) | (usample >> shift));
272 decoder->uncompressed_offset = offset;
276 /*-------------------------------------------------
277 * seek_callback - handle seeks on the output
279 *-------------------------------------------------
282 static drflac_bool32 flac_decoder_seek_callback(void *userdata, int offset, drflac_seek_origin origin)
284 flac_decoder * decoder = (flac_decoder *)userdata;
285 uint32_t length = decoder->compressed_length + decoder->compressed2_length;
287 if (origin == drflac_seek_origin_start) {
288 uint32_t pos = offset;
290 decoder->compressed_offset = pos;
293 } else if (origin == drflac_seek_origin_current) {
294 uint32_t pos = decoder->compressed_offset + offset;
296 decoder->compressed_offset = pos;