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);
111 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
112 const char **buf, uint32_t *len)
114 struct aac_afh_context *c = afh_context;
118 assert(chunk_num <= INT_MAX);
119 /* this function always returns zero */
120 mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num);
122 ss = mp4ff_read_sample_getsize(c->mp4ff, c->track, chunk_num);
124 return -E_MP4FF_BAD_SAMPLE;
125 assert(ss + offset <= c->mapsize);
126 *buf = c->map + offset;
131 static void _aac_afh_get_taginfo(const mp4ff_t *mp4ff, struct taginfo *tags)
133 mp4ff_meta_get_artist(mp4ff, &tags->artist);
134 mp4ff_meta_get_title(mp4ff, &tags->title);
135 mp4ff_meta_get_date(mp4ff, &tags->year);
136 mp4ff_meta_get_album(mp4ff, &tags->album);
137 mp4ff_meta_get_comment(mp4ff, &tags->comment);
141 * Init m4a file and write some tech data to given pointers.
143 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
144 struct afh_info *afhi)
148 struct aac_afh_context *c;
153 ret = aac_afh_open(map, numbytes, (void **)&c);
157 ret = -E_MP4FF_BAD_SAMPLERATE;
158 rv = mp4ff_get_sample_rate(c->mp4ff, c->track);
161 afhi->frequency = rv;
163 ret = -E_MP4FF_BAD_CHANNEL_COUNT;
164 rv = mp4ff_get_channel_count(c->mp4ff, c->track);
169 ret = -E_MP4FF_BAD_SAMPLE_COUNT;
170 rv = mp4ff_num_samples(c->mp4ff, c->track);
173 afhi->chunks_total = rv;
174 afhi->max_chunk_size = 0;
175 for (n = 0; n < afhi->chunks_total; n++) {
176 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
178 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
181 tmp = c->masc.sbr_present_flag == 1? 2048 : 1024;
182 afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency;
183 ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv);
185 if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
186 numbytes -= buf - map;
187 afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
188 _aac_afh_get_taginfo(c->mp4ff, &afhi->tags);
195 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
197 int fd = *(int *)user_data;
198 return read(fd, dest, want);
201 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
203 int fd = *(int *)user_data;
204 return lseek(fd, pos, SEEK_SET);
207 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
209 int fd = *(int *)user_data;
210 return write(fd, dest, want);
213 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
215 int fd = *(int *)user_data;
216 off_t offset = lseek(fd, 0, SEEK_CUR);
217 return ftruncate(fd, offset);
220 static void replace_tag(mp4ff_tag_t *tag, const char *new_val, bool *found)
223 tag->value = para_strdup(new_val);
227 static void add_tag(mp4ff_metadata_t *md, const char *item, const char *value)
229 md->tags[md->count].item = para_strdup(item);
230 md->tags[md->count].value = para_strdup(value);
234 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
235 struct taginfo *tags, int fd, __a_unused const char *filename)
239 mp4ff_metadata_t metadata;
241 mp4ff_callback_t cb = {
242 .read = aac_afh_meta_read_cb,
243 .seek = aac_afh_meta_seek_cb,
244 .write = aac_afh_meta_write_cb,
245 .truncate = aac_afh_meta_truncate_cb,
248 bool found_artist = false, found_title = false, found_album = false,
249 found_year = false, found_comment = false;
251 ret = write_all(fd, map, mapsize);
254 lseek(fd, 0, SEEK_SET);
256 mp4ff = mp4ff_open_read_metaonly(&cb);
258 return -E_MP4FF_OPEN;
260 ret = -E_MP4FF_META_READ;
261 rv = mp4ff_meta_get_num_items(mp4ff);
265 PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
267 metadata.tags = para_malloc((metadata.count + 5) * sizeof(mp4ff_tag_t));
268 for (i = 0; i < metadata.count; i++) {
269 mp4ff_tag_t *tag = metadata.tags + i;
271 ret = -E_MP4FF_META_READ;
272 if (!mp4ff_meta_get_by_index(mp4ff, i, &tag->item, &tag->value))
274 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
275 if (!strcmp(tag->item, "artist"))
276 replace_tag(tag, tags->artist, &found_artist);
277 else if (!strcmp(tag->item, "title"))
278 replace_tag(tag, tags->title, &found_title);
279 else if (!strcmp(tag->item, "album"))
280 replace_tag(tag, tags->album, &found_album);
281 else if (!strcmp(tag->item, "date"))
282 replace_tag(tag, tags->year, &found_year);
283 else if (!strcmp(tag->item, "comment"))
284 replace_tag(tag, tags->comment, &found_comment);
287 add_tag(&metadata, "artist", tags->artist);
289 add_tag(&metadata, "title", tags->title);
291 add_tag(&metadata, "album", tags->album);
293 add_tag(&metadata, "date", tags->year);
295 add_tag(&metadata, "comment", tags->comment);
296 ret = -E_MP4FF_META_WRITE;
297 if (!mp4ff_meta_update(&cb, &metadata))
302 free(metadata.tags[i - 1].item);
303 free(metadata.tags[i - 1].value);
311 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
314 * The audio format handler for the Advanced Audio Codec.
316 * This is only compiled in if the faad library is installed.
318 const struct audio_format_handler aac_afh = {
319 .get_file_info = aac_get_file_info,
320 .suffixes = aac_suffixes,
321 .rewrite_tags = aac_afh_rewrite_tags,
322 .open = aac_afh_open,
323 .get_chunk = aac_afh_get_chunk,
324 .close = aac_afh_close,