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>
19 #include "portable_io.h"
25 struct aac_afh_context {
31 mp4AudioSpecificConfig masc;
35 static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want)
37 struct aac_afh_context *c = user_data;
40 if (want == 0 || c->fpos >= c->mapsize) {
41 PARA_INFO_LOG("failed attempt to read %u bytes @%zu\n", want,
46 have = c->mapsize - c->fpos;
47 rv = PARA_MIN(have, want);
48 PARA_DEBUG_LOG("reading %u bytes @%zu\n", rv, c->fpos);
49 memcpy(dest, c->map + c->fpos, rv);
54 static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
56 struct aac_afh_context *c = user_data;
61 static int32_t aac_afh_get_track(mp4ff_t *mp4ff, mp4AudioSpecificConfig *masc)
63 int32_t i, rc, num_tracks = mp4ff_total_tracks(mp4ff);
65 assert(num_tracks >= 0);
66 for (i = 0; i < num_tracks; i++) {
67 unsigned char *buf = NULL;
68 unsigned buf_size = 0;
70 mp4ff_get_decoder_config(mp4ff, i, &buf, &buf_size);
72 rc = NeAACDecAudioSpecificConfig(buf, buf_size, masc);
79 return -1; /* no audio track */
82 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
85 struct aac_afh_context *c = para_malloc(sizeof(*c));
90 c->cb.read = aac_afh_read_cb;
91 c->cb.seek = aac_afh_seek_cb;
95 c->mp4ff = mp4ff_open_read(&c->cb);
98 c->track = aac_afh_get_track(c->mp4ff, &c->masc);
105 mp4ff_close(c->mp4ff);
112 static void aac_afh_close(void *afh_context)
114 struct aac_afh_context *c = afh_context;
115 mp4ff_close(c->mp4ff);
120 * Libmp4ff function to reposition the file to the given sample.
122 * \param f The opaque handle returned by mp4ff_open_read().
123 * \param track The number of the (audio) track.
124 * \param sample Destination.
126 * We need this function to obtain the offset of the sample within the audio
127 * file. Unfortunately, it is not exposed in the mp4ff header.
129 * \return This function always returns 0.
131 int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample);
133 static int aac_afh_get_chunk(long unsigned chunk_num, void *afh_context,
134 const char **buf, size_t *len)
136 struct aac_afh_context *c = afh_context;
140 assert(chunk_num <= INT_MAX);
141 /* this function always returns zero */
142 mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num);
144 ss = mp4ff_read_sample_getsize(c->mp4ff, c->track, chunk_num);
146 return -E_MP4FF_BAD_SAMPLE;
147 assert(ss + offset <= c->mapsize);
148 *buf = c->map + offset;
152 static int aac_find_stsz(char *buf, size_t buflen, size_t *skip)
156 for (i = 0; i + 16 < buflen; i++) {
158 unsigned sample_count, sample_size;
160 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
162 PARA_DEBUG_LOG("found stsz@%d\n", i);
164 sample_size = read_u32_be(buf + i);
165 PARA_DEBUG_LOG("sample size: %u\n", sample_size);
167 sample_count = read_u32_be(buf + i);
169 PARA_DEBUG_LOG("sample count: %u\n", sample_count);
176 static int atom_cmp(const char *buf1, const char *buf2)
178 return memcmp(buf1, buf2, 4)? 1 : 0;
181 static int read_atom_header(char *buf, uint64_t *subsize, char type[5])
183 uint64_t size = read_u32_be(buf);
185 memcpy(type, buf + 4, 4);
188 PARA_DEBUG_LOG("size: %llu, type: %s\n", (long long unsigned)size, type);
195 size = read_u64_be(buf);
200 static char *get_tag(char *p, int size)
205 buf = para_malloc(size + 1);
207 memcpy(buf, p, size);
209 PARA_DEBUG_LOG("size: %d: %s\n", size, buf);
213 static void read_tags(char *buf, size_t buflen, struct afh_info *afhi)
217 while (p + 32 < buf + buflen) {
218 char *q, type1[5], type2[5];
219 uint64_t size1, size2;
222 ret = read_atom_header(p, &size1, type1);
223 ret2 = read_atom_header(p + ret, &size2, type2);
225 if (size2 <= 16 || atom_cmp(type2, "data")) {
230 q = p + ret + ret2 + 8;
231 if (q + size2 > buf + buflen)
233 if (!atom_cmp(type1, "\xa9" "ART"))
234 afhi->tags.artist = get_tag(q, size2);
235 else if (!atom_cmp(type1, "\xa9" "alb"))
236 afhi->tags.album = get_tag(q, size2);
237 else if (!atom_cmp(type1, "\xa9" "nam"))
238 afhi->tags.title = get_tag(q, size2);
239 else if (!atom_cmp(type1, "\xa9" "cmt"))
240 afhi->tags.comment = get_tag(q, size2);
241 else if (!atom_cmp(type1, "\xa9" "day"))
242 afhi->tags.year = get_tag(q, size2);
247 static void read_meta(char *buf, size_t buflen, struct afh_info *afhi)
251 while (p + 4 < buf + buflen) {
253 if (p[0] != 'i' || p[1] != 'l' || p[2] != 's' || p[3] != 't') {
258 return read_tags(p, buflen - (p - buf), afhi);
262 static void aac_get_taginfo(char *buf, size_t buflen, struct afh_info *afhi)
268 for (i = 0; i + 24 < buflen; i++) {
270 if (p[0] != 'm' || p[1] != 'e' || p[2] != 't' || p[3] != 'a')
272 PARA_INFO_LOG("found metadata at offset %d\n", i);
275 i += read_atom_header(p, &subsize, type);
277 return read_meta(p, buflen - i, afhi);
279 PARA_INFO_LOG("no meta data\n");
282 static ssize_t aac_compute_chunk_info(struct afh_info *afhi,
283 char *map, size_t numbytes, mp4AudioSpecificConfig *mp4ASC)
287 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
288 struct timeval total;
291 afhi->chunk_table = NULL;
292 ret = aac_find_stsz(map, numbytes, &skip);
295 afhi->chunks_total = ret;
296 afhi->max_chunk_size = 0;
297 PARA_DEBUG_LOG("sz table has %" PRIu32 " entries\n", afhi->chunks_total);
298 for (i = 1; i <= afhi->chunks_total; i++) {
300 if (skip + 4 > numbytes)
302 val = read_u32_be(map + skip);
303 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, val);
307 ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
309 tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
310 PARA_INFO_LOG("%luHz, %lus (%" PRIu32 " x %lums)\n",
311 mp4ASC->samplingFrequency, ms / 1000,
312 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
315 afhi->seconds_total = ms / 1000;
316 ret = aac_find_entry_point(map, numbytes, &skip);
319 ret = (numbytes - ret) * 8;
320 ret += (afhi->channels * afhi->seconds_total * 500); /* avoid rounding error */
321 afhi->bitrate = ret / (afhi->channels * afhi->seconds_total * 1000);
326 * Init m4a file and write some tech data to given pointers.
328 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
329 struct afh_info *afhi)
333 unsigned long rate = 0, decoder_len;
334 unsigned char channels = 0;
335 mp4AudioSpecificConfig mp4ASC;
336 NeAACDecHandle handle = NULL;
338 ret = aac_find_esds(map, numbytes, &skip, &decoder_len);
341 aac_get_taginfo(map, numbytes, afhi);
343 ret = -E_AAC_AFH_INIT;
344 if (NeAACDecInit(handle, (unsigned char *)map + skip, decoder_len,
349 afhi->channels = channels;
350 afhi->frequency = rate;
351 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
353 if (NeAACDecAudioSpecificConfig((unsigned char *)map + skip,
354 numbytes - skip, &mp4ASC))
356 if (!mp4ASC.samplingFrequency)
358 ret = aac_compute_chunk_info(afhi, map, numbytes, &mp4ASC);
364 NeAACDecClose(handle);
368 static int aac_rewrite_tags(const char *map, size_t mapsize,
369 struct taginfo *tags, int fd, const char *filename)
372 const MP4Tags *mdata;
373 int ret = write_all(fd, map, mapsize);
377 lseek(fd, 0, SEEK_SET);
378 h = MP4Modify(filename, 0);
380 PARA_ERROR_LOG("MP4Modify() failed, fd = %d\n", fd);
383 mdata = MP4TagsAlloc();
385 if (!MP4TagsFetch(mdata, h)) {
386 PARA_ERROR_LOG("MP4Tags_Fetch() failed\n");
391 if (!MP4TagsSetAlbum(mdata, tags->album)) {
392 PARA_ERROR_LOG("Could not set album\n");
396 if (!MP4TagsSetArtist(mdata, tags->artist)) {
397 PARA_ERROR_LOG("Could not set album\n");
401 if (!MP4TagsSetComments(mdata, tags->comment)) {
402 PARA_ERROR_LOG("Could not set comment\n");
406 if (!MP4TagsSetName(mdata, tags->title)) {
407 PARA_ERROR_LOG("Could not set title\n");
411 if (!MP4TagsSetReleaseDate(mdata, tags->year)) {
412 PARA_ERROR_LOG("Could not set release date\n");
417 if (!MP4TagsStore(mdata, h)) {
418 PARA_ERROR_LOG("Could not store tags\n");
430 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
432 * the init function of the aac audio format handler
434 * \param afh pointer to the struct to initialize
436 void aac_afh_init(struct audio_format_handler *afh)
438 afh->get_file_info = aac_get_file_info,
439 afh->suffixes = aac_suffixes;
440 afh->rewrite_tags = aac_rewrite_tags;
441 afh->open = aac_afh_open;
442 afh->get_chunk = aac_afh_get_chunk;
443 afh->close = aac_afh_close;