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 {
26 struct mp4_callback cb;
29 static ssize_t aac_afh_read_cb(void *user_data, void *dest, size_t want)
31 struct aac_afh_context *c = user_data;
34 if (want == 0 || c->fpos >= c->mapsize)
36 have = c->mapsize - c->fpos;
37 rv = PARA_MIN(have, want);
38 PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos);
39 memcpy(dest, c->map + c->fpos, rv);
44 static off_t aac_afh_seek_cb(void *user_data, off_t offset, int whence)
46 struct aac_afh_context *c = user_data;
48 if (whence == SEEK_SET)
50 else if (whence == SEEK_CUR)
52 else if (whence == SEEK_END)
53 c->fpos = c->mapsize + offset;
59 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
62 struct aac_afh_context *c = para_malloc(sizeof(*c));
67 c->cb.read = aac_afh_read_cb;
68 c->cb.seek = aac_afh_seek_cb;
71 ret = mp4_open_read(&c->cb, &c->mp4);
82 static void aac_afh_close(void *afh_context)
84 struct aac_afh_context *c = afh_context;
89 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
90 const char **buf, uint32_t *len)
92 struct aac_afh_context *c = afh_context;
97 ret = mp4_set_sample_position(c->mp4, chunk_num);
101 ss = mp4_get_sample_size(c->mp4, chunk_num);
103 return -E_MP4_BAD_SAMPLE;
104 assert(ss + offset <= c->mapsize);
105 *buf = c->map + offset;
110 static void aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
112 tags->artist = mp4_get_tag_value(mp4, "artist");
113 tags->title = mp4_get_tag_value(mp4, "title");
114 tags->year = mp4_get_tag_value(mp4, "date");
115 tags->album = mp4_get_tag_value(mp4, "album");
116 tags->comment = mp4_get_tag_value(mp4, "comment");
120 * Init m4a file and write some tech data to given pointers.
122 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
123 struct afh_info *afhi)
127 struct aac_afh_context *c;
128 uint64_t milliseconds;
132 ret = aac_afh_open(map, numbytes, (void **)&c);
136 ret = -E_MP4_BAD_SAMPLERATE;
137 rv = mp4_get_sample_rate(c->mp4);
140 afhi->frequency = rv;
142 ret = -E_MP4_BAD_CHANNEL_COUNT;
143 rv = mp4_get_channel_count(c->mp4);
148 ret = -E_MP4_BAD_SAMPLE_COUNT;
149 rv = mp4_num_samples(c->mp4);
152 afhi->chunks_total = rv;
153 afhi->max_chunk_size = 0;
154 for (n = 0; n < afhi->chunks_total; n++) {
155 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
157 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
159 milliseconds = mp4_get_duration(c->mp4);
160 afhi->seconds_total = milliseconds / 1000;
161 ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
162 if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
163 numbytes -= buf - map;
164 afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
165 aac_afh_get_taginfo(c->mp4, &afhi->tags);
172 static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
174 int fd = *(int *)user_data;
175 return read(fd, dest, want);
178 static off_t aac_afh_meta_seek_cb(void *user_data, off_t offset, int whence)
180 int fd = *(int *)user_data;
181 off_t ret = lseek(fd, offset, whence);
183 assert(ret != (off_t)-1);
187 static ssize_t aac_afh_meta_write_cb(void *user_data, void *dest, size_t count)
189 int fd = *(int *)user_data;
190 return write(fd, dest, count);
193 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
195 int fd = *(int *)user_data;
196 off_t offset = lseek(fd, 0, SEEK_CUR);
197 return ftruncate(fd, offset);
200 static void replace_or_add_tag(const char *item, const char *value,
201 struct mp4_metadata *meta)
206 for (n = 0; n < meta->count; n++) {
208 if (strcasecmp(t->item, item))
211 t->value = para_strdup(value);
214 /* item not found, add new tag */
215 meta->tags = para_realloc(meta->tags, (meta->count + 1)
216 * sizeof(struct mp4_tag));
217 t = meta->tags + meta->count;
218 t->item = para_strdup(item);
219 t->value = para_strdup(value);
223 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
224 struct taginfo *tags, int fd, __a_unused const char *filename)
227 struct mp4_metadata *metadata;
229 struct mp4_callback cb = {
230 .read = aac_afh_meta_read_cb,
231 .seek = aac_afh_meta_seek_cb,
232 .write = aac_afh_meta_write_cb,
233 .truncate = aac_afh_meta_truncate_cb,
237 ret = write_all(fd, map, mapsize);
240 lseek(fd, 0, SEEK_SET);
242 ret = mp4_open_meta(&cb, &mp4);
245 metadata = mp4_get_meta(mp4);
246 PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
247 replace_or_add_tag("artist", tags->artist, metadata);
248 replace_or_add_tag("title", tags->title, metadata);
249 replace_or_add_tag("album", tags->album, metadata);
250 replace_or_add_tag("date", tags->year, metadata);
251 replace_or_add_tag("comment", tags->comment, metadata);
252 ret = mp4_meta_update(mp4);
257 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
260 * The audio format handler for the Advanced Audio Codec.
262 * This is only compiled in if the faad library is installed.
264 const struct audio_format_handler aac_afh = {
265 .get_file_info = aac_get_file_info,
266 .suffixes = aac_suffixes,
267 .rewrite_tags = aac_afh_rewrite_tags,
268 .open = aac_afh_open,
269 .get_chunk = aac_afh_get_chunk,
270 .close = aac_afh_close,