2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
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. */
14 #include <mp4v2/mp4v2.h>
23 static int aac_find_stsz(unsigned char *buf, size_t buflen, off_t *skip)
27 for (i = 0; i + 16 < buflen; i++) {
28 unsigned char *p = buf + i;
29 unsigned sample_count, sample_size;
31 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
33 PARA_DEBUG_LOG("found stsz@%d\n", i);
35 sample_size = aac_read_int32(buf + i);
36 PARA_DEBUG_LOG("sample size: %d\n", sample_size);
38 sample_count = aac_read_int32(buf + i);
40 PARA_DEBUG_LOG("sample count: %d\n", sample_count);
47 static int atom_cmp(const unsigned char *buf1, const char *buf2)
49 const unsigned char *b2 = (unsigned char *)buf2;
62 static int read_atom_header(unsigned char *buf, uint64_t *subsize, unsigned char type[5])
65 uint64_t size = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
67 memcpy(type, buf + 4, 4);
70 PARA_DEBUG_LOG("size: %llu, type: %s\n", (long long unsigned)size, type);
77 for (i = 0; i < 8; i++)
78 size |= ((uint64_t)buf[i]) << ((7 - i) * 8);
83 static char *get_tag(unsigned char *p, int size)
88 buf = para_malloc(size + 1);
92 PARA_DEBUG_LOG("size: %d: %s\n", size, buf);
96 static void read_tags(unsigned char *buf, size_t buflen, struct afh_info *afhi)
98 unsigned char *p = buf;
100 while (p + 32 < buf + buflen) {
101 unsigned char *q, type1[5], type2[5];
102 uint64_t size1, size2;
105 ret = read_atom_header(p, &size1, type1);
106 ret2 = read_atom_header(p + ret, &size2, type2);
108 if (size2 <= 16 || atom_cmp(type2, "data")) {
113 q = p + ret + ret2 + 8;
114 if (q + size2 > buf + buflen)
116 if (!atom_cmp(type1, "\xa9" "ART"))
117 afhi->tags.artist = get_tag(q, size2);
118 else if (!atom_cmp(type1, "\xa9" "alb"))
119 afhi->tags.album = get_tag(q, size2);
120 else if (!atom_cmp(type1, "\xa9" "nam"))
121 afhi->tags.title = get_tag(q, size2);
122 else if (!atom_cmp(type1, "\xa9" "cmt"))
123 afhi->tags.comment = get_tag(q, size2);
124 else if (!atom_cmp(type1, "\xa9" "day"))
125 afhi->tags.year = get_tag(q, size2);
130 static void read_meta(unsigned char *buf, size_t buflen, struct afh_info *afhi)
132 unsigned char *p = buf;
134 while (p + 4 < buf + buflen) {
136 if (p[0] != 'i' || p[1] != 'l' || p[2] != 's' || p[3] != 't') {
141 return read_tags(p, buflen - (p - buf), afhi);
145 static void aac_get_taginfo(unsigned char *buf, size_t buflen,
146 struct afh_info *afhi)
150 unsigned char type[5];
152 for (i = 0; i + 24 < buflen; i++) {
153 unsigned char *p = buf + i;
154 if (p[0] != 'm' || p[1] != 'e' || p[2] != 't' || p[3] != 'a')
156 PARA_INFO_LOG("found metadata at offset %d\n", i);
159 i += read_atom_header(p, &subsize, type);
161 return read_meta(p, buflen - i, afhi);
163 PARA_INFO_LOG("no meta data\n");
166 static ssize_t aac_compute_chunk_table(struct afh_info *afhi,
167 unsigned char *map, size_t numbytes)
173 ret = aac_find_stsz(map, numbytes, &skip);
176 afhi->chunks_total = ret;
177 PARA_DEBUG_LOG("sz table has %lu entries\n", afhi->chunks_total);
178 afhi->chunk_table = para_malloc((afhi->chunks_total + 1) * sizeof(size_t));
179 for (i = 1; i <= afhi->chunks_total; i++) {
180 if (skip + 4 > numbytes)
182 sum += aac_read_int32(map + skip);
183 afhi->chunk_table[i] = sum;
185 // if (i < 10 || i + 10 > afhi->chunks_total)
186 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
191 static int aac_set_chunk_tv(struct afh_info *afhi,
192 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
194 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
195 struct timeval total;
198 if (!mp4ASC->samplingFrequency)
200 ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
202 tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
203 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
204 mp4ASC->samplingFrequency, ms / 1000,
205 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
208 *seconds = ms / 1000;
213 * Init m4a file and write some tech data to given pointers.
215 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
216 struct afh_info *afhi)
221 unsigned long rate = 0, decoder_len;
222 unsigned char channels = 0;
223 mp4AudioSpecificConfig mp4ASC;
224 NeAACDecHandle handle = NULL;
225 unsigned char *umap = (unsigned char *) map;
227 ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
230 aac_get_taginfo(umap, numbytes, afhi);
232 ret = -E_AAC_AFH_INIT;
233 if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
237 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
239 if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
241 if (!mp4ASC.samplingFrequency)
243 ret = aac_compute_chunk_table(afhi, umap, numbytes);
247 ret = aac_set_chunk_tv(afhi, &mp4ASC, &afhi->seconds_total);
250 ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
253 afhi->chunk_table[0] = ret;
254 for (i = 1; i<= afhi->chunks_total; i++)
255 afhi->chunk_table[i] += ret;
256 afhi->channels = channels;
257 afhi->frequency = rate;
258 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
259 ret += (channels * afhi->seconds_total * 500); /* avoid rounding error */
260 afhi->bitrate = ret / (channels * afhi->seconds_total * 1000);
264 NeAACDecClose(handle);
268 static int aac_rewrite_tags(const char *map, size_t mapsize,
269 struct taginfo *tags, int fd, const char *filename)
272 const MP4Tags *mdata;
273 int ret = write_all(fd, map, mapsize);
277 lseek(fd, 0, SEEK_SET);
278 h = MP4Modify(filename, 0);
280 PARA_ERROR_LOG("MP4Modify() failed, fd = %d\n", fd);
283 mdata = MP4TagsAlloc();
285 if (!MP4TagsFetch(mdata, h)) {
286 PARA_ERROR_LOG("MP4Tags_Fetch() failed\n");
291 if (!MP4TagsSetAlbum(mdata, tags->album)) {
292 PARA_ERROR_LOG("Could not set album\n");
296 if (!MP4TagsSetArtist(mdata, tags->artist)) {
297 PARA_ERROR_LOG("Could not set album\n");
301 if (!MP4TagsSetComments(mdata, tags->comment)) {
302 PARA_ERROR_LOG("Could not set comment\n");
306 if (!MP4TagsSetName(mdata, tags->title)) {
307 PARA_ERROR_LOG("Could not set title\n");
311 if (!MP4TagsSetReleaseDate(mdata, tags->year)) {
312 PARA_ERROR_LOG("Could not set release date\n");
317 if (!MP4TagsStore(mdata, h)) {
318 PARA_ERROR_LOG("Could not store tags\n");
330 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
332 * the init function of the aac audio format handler
334 * \param afh pointer to the struct to initialize
336 void aac_afh_init(struct audio_format_handler *afh)
338 afh->get_file_info = aac_get_file_info,
339 afh->suffixes = aac_suffixes;
340 afh->rewrite_tags = aac_rewrite_tags;