This commit is contained in:
parent
109921d57b
commit
2ace9b3ecb
|
@ -28,7 +28,8 @@ libalac_la_SOURCES = \
|
|||
dp_enc.c \
|
||||
matrix_dec.c \
|
||||
matrix_enc.c \
|
||||
libalac.cpp
|
||||
libalac_encoder.cpp \
|
||||
libalac_decoder.cpp
|
||||
|
||||
pkgconfigdir = $(libdir)/pkgconfig
|
||||
pkgconfig_DATA = alac.pc
|
||||
|
|
|
@ -19,6 +19,23 @@ int alac_encoder_get_magic_cookie(alac_encoder* encoder, unsigned char * output)
|
|||
int alac_encoder_write(alac_encoder* encoder, const unsigned char * input, int input_size, unsigned char * output);
|
||||
void alac_encoder_delete(alac_encoder* encoder);
|
||||
|
||||
typedef struct {
|
||||
void *ptr;
|
||||
int sample_rate;
|
||||
int channels;
|
||||
int bit_depth;
|
||||
int frames_per_packet;
|
||||
int input_max_packet_size;
|
||||
int output_packet_size;
|
||||
|
||||
} alac_decoder;
|
||||
|
||||
typedef struct {
|
||||
int input_bytes_used;
|
||||
int output_bytes;
|
||||
|
||||
} alac_decoder_read_result;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
70
codec/libalac_decoder.cpp
Normal file
70
codec/libalac_decoder.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include <vector>
|
||||
#include <cstdio>
|
||||
#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, reinterpret_cast<uint8_t *>(decoder), 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>(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;
|
||||
}
|
|
@ -150,6 +150,9 @@ int alac_encoder_write(alac_encoder* encoder, const unsigned char * input, int i
|
|||
}
|
||||
|
||||
void alac_encoder_delete(alac_encoder* encoder){
|
||||
if(encoder->ptr == nullptr){
|
||||
return;
|
||||
}
|
||||
delete reinterpret_cast<ALACEncoder*>(encoder->ptr);
|
||||
encoder->ptr = nullptr;
|
||||
}
|
Loading…
Reference in a new issue