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. */
18 /* To get the mp4ff_tag_t and mp4ff_metadata_t typedefs. */
23 #include "portable_io.h"
29 struct aac_afh_context {
35 mp4AudioSpecificConfig masc;
39 static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want)
41 struct aac_afh_context *c = user_data;
44 if (want == 0 || c->fpos >= c->mapsize) {
45 PARA_INFO_LOG("failed attempt to read %u bytes @%zu\n", want,
50 have = c->mapsize - c->fpos;
51 rv = PARA_MIN(have, want);
52 PARA_DEBUG_LOG("reading %u bytes @%zu\n", rv, c->fpos);
53 memcpy(dest, c->map + c->fpos, rv);
58 static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
60 struct aac_afh_context *c = user_data;
65 static int32_t aac_afh_get_track(mp4ff_t *mp4ff, mp4AudioSpecificConfig *masc)
67 int32_t i, rc, num_tracks = mp4ff_total_tracks(mp4ff);
69 assert(num_tracks >= 0);
70 for (i = 0; i < num_tracks; i++) {
71 unsigned char *buf = NULL;
72 unsigned buf_size = 0;
74 mp4ff_get_decoder_config(mp4ff, i, &buf, &buf_size);
76 rc = NeAACDecAudioSpecificConfig(buf, buf_size, masc);
83 return -1; /* no audio track */
86 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
89 struct aac_afh_context *c = para_malloc(sizeof(*c));
94 c->cb.read = aac_afh_read_cb;
95 c->cb.seek = aac_afh_seek_cb;
99 c->mp4ff = mp4ff_open_read(&c->cb);
102 c->track = aac_afh_get_track(c->mp4ff, &c->masc);
103 ret = -E_MP4FF_TRACK;
109 mp4ff_close(c->mp4ff);
116 static void aac_afh_close(void *afh_context)
118 struct aac_afh_context *c = afh_context;
119 mp4ff_close(c->mp4ff);
124 * Libmp4ff function to reposition the file to the given sample.
126 * \param f The opaque handle returned by mp4ff_open_read().
127 * \param track The number of the (audio) track.
128 * \param sample Destination.
130 * We need this function to obtain the offset of the sample within the audio
131 * file. Unfortunately, it is not exposed in the mp4ff header.
133 * \return This function always returns 0.
135 int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample);
137 static int aac_afh_get_chunk(long unsigned chunk_num, void *afh_context,
138 const char **buf, size_t *len)
140 struct aac_afh_context *c = afh_context;
144 assert(chunk_num <= INT_MAX);
145 /* this function always returns zero */
146 mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num);
148 ss = mp4ff_read_sample_getsize(c->mp4ff, c->track, chunk_num);
150 return -E_MP4FF_BAD_SAMPLE;
151 assert(ss + offset <= c->mapsize);
152 *buf = c->map + offset;
157 static void _aac_afh_get_taginfo(const mp4ff_t *mp4ff, struct taginfo *tags)
159 mp4ff_meta_get_artist(mp4ff, &tags->artist);
160 mp4ff_meta_get_title(mp4ff, &tags->title);
161 mp4ff_meta_get_date(mp4ff, &tags->year);
162 mp4ff_meta_get_album(mp4ff, &tags->album);
163 mp4ff_meta_get_comment(mp4ff, &tags->comment);
167 * Init m4a file and write some tech data to given pointers.
169 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
170 struct afh_info *afhi)
174 struct aac_afh_context *c;
180 ret = aac_afh_open(map, numbytes, (void **)&c);
184 ret = -E_MP4FF_BAD_SAMPLERATE;
185 rv = mp4ff_get_sample_rate(c->mp4ff, c->track);
188 afhi->frequency = rv;
190 ret = -E_MP4FF_BAD_CHANNEL_COUNT;
191 rv = mp4ff_get_channel_count(c->mp4ff, c->track);
196 ret = -E_MP4FF_BAD_SAMPLE_COUNT;
197 rv = mp4ff_num_samples(c->mp4ff, c->track);
200 afhi->chunks_total = rv;
201 afhi->max_chunk_size = 0;
202 for (n = 0; n < afhi->chunks_total; n++) {
203 if (aac_afh_get_chunk(n, c, &buf, &sz) < 0)
205 afhi->max_chunk_size = PARA_MAX((size_t)afhi->max_chunk_size, sz);
208 tmp = c->masc.sbr_present_flag == 1? 2048 : 1024;
209 afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency;
210 ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv);
212 if (aac_afh_get_chunk(0, c, &buf, &sz) >= 0)
213 numbytes -= buf - map;
214 afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
215 _aac_afh_get_taginfo(c->mp4ff, &afhi->tags);
222 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
224 int fd = *(int *)user_data;
225 return read(fd, dest, want);
228 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
230 int fd = *(int *)user_data;
231 return lseek(fd, pos, SEEK_SET);
234 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
236 int fd = *(int *)user_data;
237 return write(fd, dest, want);
240 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
242 int fd = *(int *)user_data;
243 off_t offset = lseek(fd, 0, SEEK_CUR);
244 return ftruncate(fd, offset);
247 static void replace_tag(mp4ff_tag_t *tag, const char *new_val, bool *found)
250 tag->value = para_strdup(new_val);
254 static void add_tag(mp4ff_metadata_t *md, const char *item, const char *value)
256 md->tags[md->count].item = para_strdup(item);
257 md->tags[md->count].value = para_strdup(value);
261 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
262 struct taginfo *tags, int fd, __a_unused const char *filename)
266 mp4ff_metadata_t metadata;
268 mp4ff_callback_t cb = {
269 .read = aac_afh_meta_read_cb,
270 .seek = aac_afh_meta_seek_cb,
271 .write = aac_afh_meta_write_cb,
272 .truncate = aac_afh_meta_truncate_cb,
275 bool found_artist = false, found_title = false, found_album = false,
276 found_year = false, found_comment = false;
278 ret = write_all(fd, map, mapsize);
281 lseek(fd, 0, SEEK_SET);
283 mp4ff = mp4ff_open_read_metaonly(&cb);
285 return -E_MP4FF_OPEN;
287 ret = -E_MP4FF_META_READ;
288 rv = mp4ff_meta_get_num_items(mp4ff);
292 PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
294 metadata.tags = para_malloc((metadata.count + 5) * sizeof(mp4ff_tag_t));
295 for (i = 0; i < metadata.count; i++) {
296 mp4ff_tag_t *tag = metadata.tags + i;
298 ret = -E_MP4FF_META_READ;
299 if (mp4ff_meta_get_by_index(mp4ff, i,
300 &tag->item, &tag->value) < 0)
302 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
303 if (!strcmp(tag->item, "artist"))
304 replace_tag(tag, tags->artist, &found_artist);
305 else if (!strcmp(tag->item, "title"))
306 replace_tag(tag, tags->title, &found_title);
307 else if (!strcmp(tag->item, "album"))
308 replace_tag(tag, tags->album, &found_album);
309 else if (!strcmp(tag->item, "date"))
310 replace_tag(tag, tags->year, &found_year);
311 else if (!strcmp(tag->item, "comment"))
312 replace_tag(tag, tags->comment, &found_comment);
315 add_tag(&metadata, "artist", tags->artist);
317 add_tag(&metadata, "title", tags->title);
319 add_tag(&metadata, "album", tags->album);
321 add_tag(&metadata, "date", tags->year);
323 add_tag(&metadata, "comment", tags->comment);
324 ret = -E_MP4FF_META_WRITE;
325 if (mp4ff_meta_update(&cb, &metadata) < 0)
330 free(metadata.tags[i - 1].item);
331 free(metadata.tags[i - 1].value);
339 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
341 * the init function of the aac audio format handler
343 * \param afh pointer to the struct to initialize
345 void aac_afh_init(struct audio_format_handler *afh)
347 afh->get_file_info = aac_get_file_info,
348 afh->suffixes = aac_suffixes;
349 afh->rewrite_tags = aac_afh_rewrite_tags;
350 afh->open = aac_afh_open;
351 afh->get_chunk = aac_afh_get_chunk;
352 afh->close = aac_afh_close;