diff --git a/components/codecs/inc/alac/alac_wrapper.h b/components/codecs/inc/alac/alac_wrapper.h index 59cf4722..e9e753d5 100644 --- a/components/codecs/inc/alac/alac_wrapper.h +++ b/components/codecs/inc/alac/alac_wrapper.h @@ -19,7 +19,7 @@ extern "C" { struct alac_codec_s *alac_create_decoder(int magic_cookie_size, unsigned char *magic_cookie, unsigned char *sample_size, unsigned *sample_rate, - unsigned char *channels); + unsigned char *channels, unsigned int *block_size); void alac_delete_decoder(struct alac_codec_s *codec); bool alac_to_pcm(struct alac_codec_s *codec, unsigned char* input, unsigned char *output, char channels, unsigned *out_frames); diff --git a/components/codecs/lib/libalac.a b/components/codecs/lib/libalac.a index 18d936a0..803eec5e 100644 Binary files a/components/codecs/lib/libalac.a and b/components/codecs/lib/libalac.a differ diff --git a/components/raop/rtp.c b/components/raop/rtp.c index 599fb224..e12cf95f 100644 --- a/components/raop/rtp.c +++ b/components/raop/rtp.c @@ -168,7 +168,7 @@ static void rtp_thread_func(void *arg); /*---------------------------------------------------------------------------*/ static struct alac_codec_s* alac_init(int fmtp[32]) { struct alac_codec_s *alac; - unsigned sample_rate; + unsigned sample_rate, block_size; unsigned char sample_size, channels; struct { uint32_t frameLength; @@ -196,7 +196,7 @@ static struct alac_codec_s* alac_init(int fmtp[32]) { config.avgBitRate = htonl(fmtp[10]); config.sampleRate = htonl(fmtp[11]); - alac = alac_create_decoder(sizeof(config), (unsigned char*) &config, &sample_size, &sample_rate, &channels); + alac = alac_create_decoder(sizeof(config), (unsigned char*) &config, &sample_size, &sample_rate, &channels, &block_size); if (!alac) { LOG_ERROR("cannot create alac codec", NULL); return NULL; diff --git a/components/squeezelite/alac.c b/components/squeezelite/alac.c index 0fc0f8b1..71bab64f 100644 --- a/components/squeezelite/alac.c +++ b/components/squeezelite/alac.c @@ -119,8 +119,16 @@ static int read_mp4_header(void) { // extract audio config from within alac if (!strcmp(type, "alac") && bytes > len) { u8_t *ptr = streambuf->readp + 36; - l->decoder = alac_create_decoder(len - 36, ptr, &l->sample_size, &l->sample_rate, &l->channels); - l->play = l->trak; + unsigned int block_size; + l->decoder = alac_create_decoder(len - 36, ptr, &l->sample_size, &l->sample_rate, &l->channels, &block_size); + l->play = l->trak; + l->writebuf = malloc(block_size + 256); + if (!l->writebuf) { + LOG_ERROR("cannot allocate write buffer for %u bytes", block_size); + return -1; + } else { + LOG_INFO("write buffer of %u bytes", block_size); + } } // extract the total number of samples from stts @@ -510,12 +518,11 @@ static decode_state alac_decode(void) { static void alac_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) { if (l->decoder) alac_delete_decoder(l->decoder); - else l->writebuf = malloc(BLOCK_SIZE * 2); - + if (l->writebuf) free(l->writebuf); if (l->chunkinfo) free(l->chunkinfo); if (l->block_size) free(l->block_size); if (l->stsc) free(l->stsc); - l->decoder = l->chunkinfo = l->stsc = l->block_size = NULL; + l->writebuf = l->decoder = l->chunkinfo = l->stsc = l->block_size = NULL; l->skip = 0; l->samples = l->sttssamples = 0; l->empty = false; @@ -524,11 +531,11 @@ static void alac_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) { static void alac_close(void) { if (l->decoder) alac_delete_decoder(l->decoder); + if (l->writebuf) free(l->writebuf); if (l->chunkinfo) free(l->chunkinfo); if (l->block_size) free(l->block_size); if (l->stsc) free(l->stsc); - l->decoder = l->chunkinfo = l->stsc = l->block_size = NULL; - free(l->writebuf); + l->writebuf = l->decoder = l->chunkinfo = l->stsc = l->block_size = NULL; } struct codec *register_alac(void) { @@ -547,7 +554,7 @@ struct codec *register_alac(void) { return NULL; } - l->decoder = l->chunkinfo = l->stsc = l->block_size = NULL; + l->writebuf = l->decoder = l->chunkinfo = l->stsc = l->block_size = NULL; LOG_INFO("using alac to decode alc"); return &ret;