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;
64 c->mp4 = mp4_open_read(&c->cb);
75 static void aac_afh_close(void *afh_context)
77 struct aac_afh_context *c = afh_context;
82 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
83 const char **buf, uint32_t *len)
85 struct aac_afh_context *c = afh_context;
90 ret = mp4_set_sample_position(c->mp4, chunk_num);
94 ss = mp4_get_sample_size(c->mp4, chunk_num);
96 return -E_MP4_BAD_SAMPLE;
97 assert(ss + offset <= c->mapsize);
98 *buf = c->map + offset;
103 static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
105 tags->artist = mp4_meta_get_artist(mp4);
106 tags->title = mp4_meta_get_title(mp4);
107 tags->year = mp4_meta_get_date(mp4);
108 tags->album = mp4_meta_get_album(mp4);
109 tags->comment = mp4_meta_get_comment(mp4);
113 * Init m4a file and write some tech data to given pointers.
115 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
116 struct afh_info *afhi)
120 struct aac_afh_context *c;
121 uint64_t milliseconds;
125 ret = aac_afh_open(map, numbytes, (void **)&c);
129 ret = -E_MP4_BAD_SAMPLERATE;
130 rv = mp4_get_sample_rate(c->mp4);
133 afhi->frequency = rv;
135 ret = -E_MP4_BAD_CHANNEL_COUNT;
136 rv = mp4_get_channel_count(c->mp4);
141 ret = -E_MP4_BAD_SAMPLE_COUNT;
142 rv = mp4_num_samples(c->mp4);
145 afhi->chunks_total = rv;
146 afhi->max_chunk_size = 0;
147 for (n = 0; n < afhi->chunks_total; n++) {
148 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
150 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
152 milliseconds = mp4_get_duration(c->mp4);
153 afhi->seconds_total = milliseconds / 1000;
154 ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
155 if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
156 numbytes -= buf - map;
157 afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
158 _aac_afh_get_taginfo(c->mp4, &afhi->tags);
165 static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
167 int fd = *(int *)user_data;
168 return read(fd, dest, want);
171 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
173 int fd = *(int *)user_data;
174 return lseek(fd, pos, SEEK_SET);
177 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
179 int fd = *(int *)user_data;
180 return write(fd, dest, want);
183 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
185 int fd = *(int *)user_data;
186 off_t offset = lseek(fd, 0, SEEK_CUR);
187 return ftruncate(fd, offset);
190 static void replace_or_add_tag(const char *item, const char *value,
191 struct mp4_metadata *meta)
196 for (n = 0; n < meta->count; n++) {
198 if (strcasecmp(t->item, item))
201 t->value = para_strdup(value);
204 /* item not found, add new tag */
205 meta->tags = para_realloc(meta->tags, (meta->count + 1)
206 * sizeof(struct mp4_tag));
207 t = meta->tags + meta->count;
208 t->item = para_strdup(item);
209 t->value = para_strdup(value);
213 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
214 struct taginfo *tags, int fd, __a_unused const char *filename)
217 struct mp4_metadata *metadata;
219 struct mp4_callback cb = {
220 .read = aac_afh_meta_read_cb,
221 .seek = aac_afh_meta_seek_cb,
222 .write = aac_afh_meta_write_cb,
223 .truncate = aac_afh_meta_truncate_cb,
227 ret = write_all(fd, map, mapsize);
230 lseek(fd, 0, SEEK_SET);
232 mp4 = mp4_open_meta(&cb);
235 metadata = mp4_get_meta(mp4);
236 PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
237 replace_or_add_tag("artist", tags->artist, metadata);
238 replace_or_add_tag("title", tags->title, metadata);
239 replace_or_add_tag("album", tags->album, metadata);
240 replace_or_add_tag("date", tags->year, metadata);
241 replace_or_add_tag("comment", tags->comment, metadata);
242 ret = -E_MP4_META_WRITE;
243 if (!mp4_meta_update(mp4))
251 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
254 * The audio format handler for the Advanced Audio Codec.
256 * This is only compiled in if the faad library is installed.
258 const struct audio_format_handler aac_afh = {
259 .get_file_info = aac_get_file_info,
260 .suffixes = aac_suffixes,
261 .rewrite_tags = aac_afh_rewrite_tags,
262 .open = aac_afh_open,
263 .get_chunk = aac_afh_get_chunk,
264 .close = aac_afh_close,