Added magic cookie, fixed Encode
This commit is contained in:
parent
abddc2592b
commit
cef0bc38e6
|
@ -1,4 +1,5 @@
|
|||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include "libalac.h"
|
||||
#include "ALACEncoder.h"
|
||||
#include "ALACBitUtilities.h"
|
||||
|
@ -19,6 +20,7 @@ alac_encoder alac_encoder_new(int sampleRate, int channels, int bitDepth, int fa
|
|||
encoder.bit_depth = bitDepth;
|
||||
encoder.input_packet_size = 0;
|
||||
encoder.output_max_packet_size = 0;
|
||||
encoder.magic_cookie_size = 0;
|
||||
|
||||
AudioFormatDescription d;
|
||||
d.mFormatID = kALACFormatAppleLossless;
|
||||
|
@ -66,10 +68,26 @@ alac_encoder alac_encoder_new(int sampleRate, int channels, int bitDepth, int fa
|
|||
encoder.ptr = e;
|
||||
encoder.input_packet_size = channels * (bitDepth >> 3) * d.mFramesPerPacket;
|
||||
encoder.output_max_packet_size = encoder.input_packet_size + kALACMaxEscapeHeaderBytes;
|
||||
encoder.magic_cookie_size = e->GetMagicCookieSize(channels);
|
||||
|
||||
return encoder;
|
||||
}
|
||||
|
||||
int alac_encoder_get_magic_cookie(alac_encoder* encoder, unsigned char * output) {
|
||||
if (encoder->ptr == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
auto e = reinterpret_cast<ALACEncoder*>(encoder->ptr);
|
||||
|
||||
|
||||
uint32_t ioSize = encoder->magic_cookie_size;
|
||||
e->GetMagicCookie(output, &ioSize);
|
||||
|
||||
return ioSize;
|
||||
}
|
||||
|
||||
int alac_encoder_write(alac_encoder* encoder, const unsigned char * input, int input_size, unsigned char * output){
|
||||
if(encoder->ptr == nullptr){
|
||||
return -1;
|
||||
|
@ -90,10 +108,11 @@ int alac_encoder_write(alac_encoder* encoder, const unsigned char * input, int i
|
|||
AudioFormatDescription inputFormat;
|
||||
inputFormat.mFormatID = kALACFormatLinearPCM;
|
||||
inputFormat.mSampleRate = static_cast<alac_float64_t>(encoder->sample_rate);
|
||||
inputFormat.mFramesPerPacket = outputFormat.mFramesPerPacket;
|
||||
inputFormat.mFramesPerPacket = 1;
|
||||
inputFormat.mChannelsPerFrame = encoder->channels;
|
||||
inputFormat.mBitsPerChannel = encoder->bit_depth;
|
||||
inputFormat.mBytesPerPacket = inputFormat.mBytesPerFrame = inputFormat.mFramesPerPacket * (inputFormat.mBitsPerChannel >> 3) * inputFormat.mChannelsPerFrame;
|
||||
inputFormat.mBytesPerFrame = (inputFormat.mBitsPerChannel >> 3) * inputFormat.mChannelsPerFrame;
|
||||
inputFormat.mBytesPerPacket = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame;
|
||||
inputFormat.mFormatFlags = kALACFormatFlagIsSignedInteger | kALACFormatFlagIsPacked; //little endian
|
||||
inputFormat.mReserved = 0;
|
||||
|
||||
|
@ -114,20 +133,20 @@ int alac_encoder_write(alac_encoder* encoder, const unsigned char * input, int i
|
|||
break;
|
||||
}
|
||||
|
||||
int32_t ioNumBytes = 0;
|
||||
|
||||
if(input_size > inputFormat.mBytesPerPacket){
|
||||
if(input_size > encoder->input_packet_size){
|
||||
return -1;
|
||||
}
|
||||
|
||||
ioNumBytes = input_size;
|
||||
int32_t ioNumBytes = input_size;
|
||||
if(e->Encode(inputFormat, outputFormat, const_cast<unsigned char *>(input), output, &ioNumBytes) != ALAC_noErr){
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return ioNumBytes;
|
||||
|
||||
}
|
||||
|
||||
void alac_encoder_delete(alac_encoder* encoder){
|
||||
delete reinterpret_cast<ALACEncoder*>(encoder->ptr);
|
||||
encoder->ptr = nullptr;
|
||||
|
|
|
@ -10,6 +10,7 @@ typedef struct {
|
|||
int bit_depth;
|
||||
int input_packet_size;
|
||||
int output_max_packet_size;
|
||||
int magic_cookie_size;
|
||||
} alac_encoder;
|
||||
|
||||
alac_encoder alac_encoder_new(int sampleRate, int channels, int bitDepth, int fastMode);
|
||||
|
|
Loading…
Reference in a new issue