浏览代码

better alac management

Philippe G 4 年之前
父节点
当前提交
174942f509
共有 4 个文件被更改,包括 18 次插入11 次删除
  1. 1 1
      components/codecs/inc/alac/alac_wrapper.h
  2. 二进制
      components/codecs/lib/libalac.a
  3. 2 2
      components/raop/rtp.c
  4. 15 8
      components/squeezelite/alac.c

+ 1 - 1
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);

二进制
components/codecs/lib/libalac.a


+ 2 - 2
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;

+ 15 - 8
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;