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 uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
46 struct aac_afh_context *c = user_data;
51 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
54 struct aac_afh_context *c = para_malloc(sizeof(*c));
59 c->cb.read = aac_afh_read_cb;
60 c->cb.seek = aac_afh_seek_cb;
63 ret = mp4_open_read(&c->cb, &c->mp4);
74 static void aac_afh_close(void *afh_context)
76 struct aac_afh_context *c = afh_context;
81 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
82 const char **buf, uint32_t *len)
84 struct aac_afh_context *c = afh_context;
89 ret = mp4_set_sample_position(c->mp4, chunk_num);
93 ss = mp4_get_sample_size(c->mp4, chunk_num);
95 return -E_MP4_BAD_SAMPLE;
96 assert(ss + offset <= c->mapsize);
97 *buf = c->map + offset;
102 static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
104 tags->artist = mp4_meta_get_artist(mp4);
105 tags->title = mp4_meta_get_title(mp4);
106 tags->year = mp4_meta_get_date(mp4);
107 tags->album = mp4_meta_get_album(mp4);
108 tags->comment = mp4_meta_get_comment(mp4);
112 * Init m4a file and write some tech data to given pointers.
114 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
115 struct afh_info *afhi)
119 struct aac_afh_context *c;
120 uint64_t milliseconds;
124 ret = aac_afh_open(map, numbytes, (void **)&c);
128 ret = -E_MP4_BAD_SAMPLERATE;
129 rv = mp4_get_sample_rate(c->mp4);
132 afhi->frequency = rv;
134 ret = -E_MP4_BAD_CHANNEL_COUNT;
135 rv = mp4_get_channel_count(c->mp4);
140 ret = -E_MP4_BAD_SAMPLE_COUNT;
141 rv = mp4_num_samples(c->mp4);
144 afhi->chunks_total = rv;
145 afhi->max_chunk_size = 0;
146 for (n = 0; n < afhi->chunks_total; n++) {
147 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
149 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
151 milliseconds = mp4_get_duration(c->mp4);
152 afhi->seconds_total = milliseconds / 1000;
153 ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
154 if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
155 numbytes -= buf - map;
156 afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
157 _aac_afh_get_taginfo(c->mp4, &afhi->tags);
164 static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
166 int fd = *(int *)user_data;
167 return read(fd, dest, want);
170 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
172 int fd = *(int *)user_data;
173 return lseek(fd, pos, SEEK_SET);
176 static ssize_t aac_afh_meta_write_cb(void *user_data, void *dest, size_t count)
178 int fd = *(int *)user_data;
179 return write(fd, dest, count);
182 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
184 int fd = *(int *)user_data;
185 off_t offset = lseek(fd, 0, SEEK_CUR);
186 return ftruncate(fd, offset);
189 static void replace_or_add_tag(const char *item, const char *value,
190 struct mp4_metadata *meta)
195 for (n = 0; n < meta->count; n++) {
197 if (strcasecmp(t->item, item))
200 t->value = para_strdup(value);
203 /* item not found, add new tag */
204 meta->tags = para_realloc(meta->tags, (meta->count + 1)
205 * sizeof(struct mp4_tag));
206 t = meta->tags + meta->count;
207 t->item = para_strdup(item);
208 t->value = para_strdup(value);
212 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
213 struct taginfo *tags, int fd, __a_unused const char *filename)
216 struct mp4_metadata *metadata;
218 struct mp4_callback cb = {
219 .read = aac_afh_meta_read_cb,
220 .seek = aac_afh_meta_seek_cb,
221 .write = aac_afh_meta_write_cb,
222 .truncate = aac_afh_meta_truncate_cb,
226 ret = write_all(fd, map, mapsize);
229 lseek(fd, 0, SEEK_SET);
231 ret = mp4_open_meta(&cb, &mp4);
234 metadata = mp4_get_meta(mp4);
235 PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
236 replace_or_add_tag("artist", tags->artist, metadata);
237 replace_or_add_tag("title", tags->title, metadata);
238 replace_or_add_tag("album", tags->album, metadata);
239 replace_or_add_tag("date", tags->year, metadata);
240 replace_or_add_tag("comment", tags->comment, metadata);
241 ret = mp4_meta_update(mp4);
246 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
249 * The audio format handler for the Advanced Audio Codec.
251 * This is only compiled in if the faad library is installed.
253 const struct audio_format_handler aac_afh = {
254 .get_file_info = aac_get_file_info,
255 .suffixes = aac_suffixes,
256 .rewrite_tags = aac_afh_rewrite_tags,
257 .open = aac_afh_open,
258 .get_chunk = aac_afh_get_chunk,
259 .close = aac_afh_close,