2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
11 /** \file aac_afh.c para_server's aac audio format handler */
18 static int aac_find_stsz(unsigned char *buf, size_t buflen, off_t *skip)
22 for (i = 0; i + 16 < buflen; i++) {
23 unsigned char *p = buf + i;
24 unsigned sample_count, sample_size;
26 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
28 PARA_INFO_LOG("found stsz@%d\n", i);
30 sample_size = aac_read_int32(buf + i);
31 PARA_INFO_LOG("sample size: %d\n", sample_size);
33 sample_count = aac_read_int32(buf + i);
35 PARA_INFO_LOG("sample count: %d\n", sample_count);
42 static ssize_t aac_compute_chunk_table(struct audio_format_info *afi,
43 unsigned char *map, size_t numbytes)
49 ret = aac_find_stsz(map, numbytes, &skip);
52 afi->chunks_total = ret;
53 PARA_INFO_LOG("sz table has %lu entries\n", afi->chunks_total);
54 afi->chunk_table = para_malloc((afi->chunks_total + 1) * sizeof(size_t));
55 for (i = 1; i <= afi->chunks_total; i++) {
56 if (skip + 4 > numbytes)
58 sum += aac_read_int32(map + skip);
59 afi->chunk_table[i] = sum;
61 // if (i < 10 || i + 10 > afi->chunks_total)
62 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afi->chunk_table[i]);
67 static int aac_set_chunk_tv(struct audio_format_info *afi,
68 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
70 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
72 long unsigned ms = 1000.0 * afi->chunks_total * tmp
73 / mp4ASC->samplingFrequency;
75 if (!mp4ASC->samplingFrequency)
77 ms = 1000.0 * afi->chunks_total * tmp / mp4ASC->samplingFrequency;
79 tv_divide(afi->chunks_total, &total, &afi->chunk_tv);
80 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
81 mp4ASC->samplingFrequency, ms / 1000,
82 afi->chunks_total, tv2ms(&afi->chunk_tv));
90 * Init m4a file and write some tech data to given pointers.
92 static int aac_get_file_info(char *map, size_t numbytes,
93 struct audio_format_info *afi)
98 unsigned long rate = 0, decoder_len;
99 unsigned char channels = 0;
100 mp4AudioSpecificConfig mp4ASC;
101 NeAACDecHandle handle;
102 unsigned char *umap = (unsigned char *) map;
104 ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
108 ret = -E_AAC_AFH_INIT;
109 if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
113 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
115 if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
117 if (!mp4ASC.samplingFrequency)
119 ret = aac_compute_chunk_table(afi, umap, numbytes);
123 ret = aac_set_chunk_tv(afi, &mp4ASC, &afi->seconds_total);
126 ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
129 afi->chunk_table[0] = ret;
130 for (i = 1; i<= afi->chunks_total; i++)
131 afi->chunk_table[i] += ret;
132 afi->channels = channels;
133 afi->frequency = rate;
134 ret = (afi->chunk_table[afi->chunks_total] - afi->chunk_table[0]) * 8; /* bits */
135 ret += (channels * afi->seconds_total * 500); /* avoid rounding error */
136 afi->bitrate = ret / (channels * afi->seconds_total * 1000);
137 sprintf(afi->info_string, "audio_file_info1:%lu x %lums, "
138 "%uHz, %d channel%s, %ukb/s\n"
139 "audio_file_info2:\n"
140 "audio_file_info3:\n",
141 afi->chunks_total, tv2ms(&afi->chunk_tv),
142 afi->frequency, channels, channels == 1? "" : "s", afi->bitrate
144 tv_scale(20, &afi->chunk_tv, &afi->eof_tv);
150 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
152 * the init function of the aac audio format handler
154 * \param afh pointer to the struct to initialize
156 void aac_afh_init(struct audio_format_handler *afh)
158 afh->get_file_info = aac_get_file_info,
159 afh->suffixes = aac_suffixes;