DataHoarder
70b76cc688
All checks were successful
continuous-integration/drone/push Build is passing
68 lines
2 KiB
C++
68 lines
2 KiB
C++
#include "libalac.h"
|
|
#include "ALACDecoder.h"
|
|
#include "ALACBitUtilities.h"
|
|
|
|
enum
|
|
{
|
|
kTestFormatFlag_16BitSourceData = 1,
|
|
kTestFormatFlag_20BitSourceData = 2,
|
|
kTestFormatFlag_24BitSourceData = 3,
|
|
kTestFormatFlag_32BitSourceData = 4
|
|
};
|
|
|
|
alac_decoder alac_decoder_new(const unsigned char* magicCookie, int magicCookieSize) {
|
|
alac_decoder decoder;
|
|
decoder.ptr = nullptr;
|
|
|
|
|
|
|
|
auto d = new ALACDecoder();
|
|
if(d->Init((void *) magicCookie, magicCookieSize) != ALAC_noErr){
|
|
delete d;
|
|
return decoder;
|
|
}
|
|
|
|
decoder.ptr = d;
|
|
decoder.sample_rate = d->mConfig.sampleRate;
|
|
decoder.channels = d->mConfig.numChannels;
|
|
decoder.bit_depth = d->mConfig.bitDepth;
|
|
decoder.frames_per_packet = d->mConfig.frameLength;
|
|
|
|
decoder.input_max_packet_size = decoder.channels * (decoder.bit_depth >> 3) * decoder.frames_per_packet + kALACMaxEscapeHeaderBytes;
|
|
decoder.output_packet_size = decoder.channels * (decoder.bit_depth >> 3) * decoder.frames_per_packet;
|
|
|
|
return decoder;
|
|
}
|
|
|
|
alac_decoder_read_result alac_decoder_read(alac_decoder* decoder, const unsigned char * input, int input_size, unsigned char * output){
|
|
if(decoder->ptr == nullptr){
|
|
return {-1, -1};
|
|
}
|
|
|
|
auto d = reinterpret_cast<ALACDecoder*>(decoder->ptr);
|
|
|
|
if(input_size > decoder->input_max_packet_size){
|
|
return {-1, -1};
|
|
}
|
|
|
|
|
|
|
|
BitBuffer inputBuffer;
|
|
BitBufferInit(&inputBuffer, const_cast<uint8_t*>(input), input_size);
|
|
|
|
uint32_t ioNumSamples = input_size;
|
|
if(d->Decode(&inputBuffer, output, decoder->frames_per_packet, decoder->channels, &ioNumSamples) != ALAC_noErr){
|
|
return {-1, -1};
|
|
}
|
|
|
|
|
|
return {static_cast<int>(BitBufferGetPosition(&inputBuffer) / 8), static_cast<int>(decoder->channels * ioNumSamples * (decoder->bit_depth >> 3))};
|
|
}
|
|
|
|
void alac_decoder_delete(alac_decoder* decoder){
|
|
if(decoder->ptr == nullptr){
|
|
return;
|
|
}
|
|
delete reinterpret_cast<ALACDecoder*>(decoder->ptr);
|
|
decoder->ptr = nullptr;
|
|
} |