| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 73 |
|
| 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 |
|
| 90 |
|
| 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 |
|
| 97 |
static const uint8_t s_header_template[0x2a] = |
| 98 |
{ |
| 99 |
0x66, 0x4C, 0x61, 0x43, |
| 100 |
0x80, |
| 101 |
|
| 102 |
0x00, 0x00, 0x22, |
| 103 |
0x00, 0x00, |
| 104 |
0x00, 0x00, |
| 105 |
0x00, 0x00, 0x00, |
| 106 |
0x00, 0x00, 0x00, |
| 107 |
0x0A, 0xC4, 0x42, 0xF0, 0x00, 0x00, 0x00, 0x00, |
| 108 |
|
| 109 |
|
| 110 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 111 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 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 |
|
| 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 |
|
| 130 |
|
| 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)]; |
| 137 |
uint32_t buf_frames = ARRAY_LENGTH(buffer) / channels(decoder); |
| 138 |
|
| 139 |
|
| 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 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
uint32_t flac_decoder_finish(flac_decoder* decoder) |
| 163 |
{ |
| 164 |
|
| 165 |
drflac *flac = (drflac*)decoder->decoder; |
| 166 |
uint64_t position = decoder->compressed_offset; |
| 167 |
|
| 168 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 197 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 238 |
if (metadata->type != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO) |
| 239 |
return; |
| 240 |
|
| 241 |
|
| 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 |
|
| 249 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 292 |
|
| 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 |
|
| 320 |
|
| 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 |
} |