1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
7 /** \file aac_afh.c para_server's aac audio format handler. */
15 #include "portable_io.h"
21 struct aac_afh_context {
27 mp4AudioSpecificConfig masc;
31 static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want)
33 struct aac_afh_context *c = user_data;
36 if (want == 0 || c->fpos >= c->mapsize)
38 have = c->mapsize - c->fpos;
39 rv = PARA_MIN(have, (size_t)want);
40 PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos);
41 memcpy(dest, c->map + c->fpos, rv);
46 static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
48 struct aac_afh_context *c = user_data;
53 static int32_t aac_afh_get_track(mp4ff_t *mp4ff, mp4AudioSpecificConfig *masc)
55 int32_t i, rc, num_tracks = mp4ff_total_tracks(mp4ff);
57 assert(num_tracks >= 0);
58 for (i = 0; i < num_tracks; i++) {
59 unsigned char *buf = NULL;
60 unsigned buf_size = 0;
62 mp4ff_get_decoder_config(mp4ff, i, &buf, &buf_size);
64 rc = NeAACDecAudioSpecificConfig(buf, buf_size, masc);
71 return -1; /* no audio track */
74 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
77 struct aac_afh_context *c = para_malloc(sizeof(*c));
82 c->cb.read = aac_afh_read_cb;
83 c->cb.seek = aac_afh_seek_cb;
87 c->mp4ff = mp4ff_open_read(&c->cb);
90 c->track = aac_afh_get_track(c->mp4ff, &c->masc);
97 mp4ff_close(c->mp4ff);
104 static void aac_afh_close(void *afh_context)
106 struct aac_afh_context *c = afh_context;
107 mp4ff_close(c->mp4ff);
112 * Libmp4ff function to reposition the file to the given sample.
114 * \param f The opaque handle returned by mp4ff_open_read().
115 * \param track The number of the (audio) track.
116 * \param sample Destination.
118 * We need this function to obtain the offset of the sample within the audio
119 * file. Unfortunately, it is not exposed in the mp4ff header.
121 * \return This function always returns 0.
123 int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample);
125 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
126 const char **buf, uint32_t *len)
128 struct aac_afh_context *c = afh_context;
132 assert(chunk_num <= INT_MAX);
133 /* this function always returns zero */
134 mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num);
136 ss = mp4ff_read_sample_getsize(c->mp4ff, c->track, chunk_num);
138 return -E_MP4FF_BAD_SAMPLE;
139 assert(ss + offset <= c->mapsize);
140 *buf = c->map + offset;
145 static void _aac_afh_get_taginfo(const mp4ff_t *mp4ff, struct taginfo *tags)
147 mp4ff_meta_get_artist(mp4ff, &tags->artist);
148 mp4ff_meta_get_title(mp4ff, &tags->title);
149 mp4ff_meta_get_date(mp4ff, &tags->year);
150 mp4ff_meta_get_album(mp4ff, &tags->album);
151 mp4ff_meta_get_comment(mp4ff, &tags->comment);
155 * Init m4a file and write some tech data to given pointers.
157 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
158 struct afh_info *afhi)
162 struct aac_afh_context *c;
167 ret = aac_afh_open(map, numbytes, (void **)&c);
171 ret = -E_MP4FF_BAD_SAMPLERATE;
172 rv = mp4ff_get_sample_rate(c->mp4ff, c->track);
175 afhi->frequency = rv;
177 ret = -E_MP4FF_BAD_CHANNEL_COUNT;
178 rv = mp4ff_get_channel_count(c->mp4ff, c->track);
183 ret = -E_MP4FF_BAD_SAMPLE_COUNT;
184 rv = mp4ff_num_samples(c->mp4ff, c->track);
187 afhi->chunks_total = rv;
188 afhi->max_chunk_size = 0;
189 for (n = 0; n < afhi->chunks_total; n++) {
190 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
192 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
195 tmp = c->masc.sbr_present_flag == 1? 2048 : 1024;
196 afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency;
197 ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv);
199 if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
200 numbytes -= buf - map;
201 afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
202 _aac_afh_get_taginfo(c->mp4ff, &afhi->tags);
209 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
211 int fd = *(int *)user_data;
212 return read(fd, dest, want);
215 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
217 int fd = *(int *)user_data;
218 return lseek(fd, pos, SEEK_SET);
221 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
223 int fd = *(int *)user_data;
224 return write(fd, dest, want);
227 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
229 int fd = *(int *)user_data;
230 off_t offset = lseek(fd, 0, SEEK_CUR);
231 return ftruncate(fd, offset);
234 static void replace_tag(mp4ff_tag_t *tag, const char *new_val, bool *found)
237 tag->value = para_strdup(new_val);
241 static void add_tag(mp4ff_metadata_t *md, const char *item, const char *value)
243 md->tags[md->count].item = para_strdup(item);
244 md->tags[md->count].value = para_strdup(value);
248 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
249 struct taginfo *tags, int fd, __a_unused const char *filename)
253 mp4ff_metadata_t metadata;
255 mp4ff_callback_t cb = {
256 .read = aac_afh_meta_read_cb,
257 .seek = aac_afh_meta_seek_cb,
258 .write = aac_afh_meta_write_cb,
259 .truncate = aac_afh_meta_truncate_cb,
262 bool found_artist = false, found_title = false, found_album = false,
263 found_year = false, found_comment = false;
265 ret = write_all(fd, map, mapsize);
268 lseek(fd, 0, SEEK_SET);
270 mp4ff = mp4ff_open_read_metaonly(&cb);
272 return -E_MP4FF_OPEN;
274 ret = -E_MP4FF_META_READ;
275 rv = mp4ff_meta_get_num_items(mp4ff);
279 PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
281 metadata.tags = para_malloc((metadata.count + 5) * sizeof(mp4ff_tag_t));
282 for (i = 0; i < metadata.count; i++) {
283 mp4ff_tag_t *tag = metadata.tags + i;
285 ret = -E_MP4FF_META_READ;
286 if (!mp4ff_meta_get_by_index(mp4ff, i, &tag->item, &tag->value))
288 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
289 if (!strcmp(tag->item, "artist"))
290 replace_tag(tag, tags->artist, &found_artist);
291 else if (!strcmp(tag->item, "title"))
292 replace_tag(tag, tags->title, &found_title);
293 else if (!strcmp(tag->item, "album"))
294 replace_tag(tag, tags->album, &found_album);
295 else if (!strcmp(tag->item, "date"))
296 replace_tag(tag, tags->year, &found_year);
297 else if (!strcmp(tag->item, "comment"))
298 replace_tag(tag, tags->comment, &found_comment);
301 add_tag(&metadata, "artist", tags->artist);
303 add_tag(&metadata, "title", tags->title);
305 add_tag(&metadata, "album", tags->album);
307 add_tag(&metadata, "date", tags->year);
309 add_tag(&metadata, "comment", tags->comment);
310 ret = -E_MP4FF_META_WRITE;
311 if (!mp4ff_meta_update(&cb, &metadata))
316 free(metadata.tags[i - 1].item);
317 free(metadata.tags[i - 1].value);
325 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
328 * The audio format handler for the Advanced Audio Codec.
330 * This is only compiled in if the faad library is installed.
332 const struct audio_format_handler aac_afh = {
333 .get_file_info = aac_get_file_info,
334 .suffixes = aac_suffixes,
335 .rewrite_tags = aac_afh_rewrite_tags,
336 .open = aac_afh_open,
337 .get_chunk = aac_afh_get_chunk,
338 .close = aac_afh_close,