]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
mp4: Replace mp4ff prefix by mp4.
[paraslash.git] / aac_afh.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2 /*
3  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
4  * Ahead Software AG
5  */
6
7 /** \file aac_afh.c para_server's aac audio format handler. */
8
9 #include <regex.h>
10 #include <neaacdec.h>
11
12 #include "para.h"
13 #include "mp4.h"
14 #include "error.h"
15 #include "portable_io.h"
16 #include "afh.h"
17 #include "string.h"
18 #include "fd.h"
19
20
21 struct aac_afh_context {
22         const void *map;
23         size_t mapsize;
24         size_t fpos;
25         int32_t track;
26         struct mp4 *mp4;
27         struct mp4_callback cb;
28 };
29
30 static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want)
31 {
32         struct aac_afh_context *c = user_data;
33         size_t have, rv;
34
35         if (want == 0 || c->fpos >= c->mapsize)
36                 return 0;
37         have = c->mapsize - c->fpos;
38         rv = PARA_MIN(have, (size_t)want);
39         PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos);
40         memcpy(dest, c->map + c->fpos, rv);
41         c->fpos += rv;
42         return rv;
43 }
44
45 static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
46 {
47         struct aac_afh_context *c = user_data;
48         c->fpos = pos;
49         return 0;
50 }
51
52 static int32_t aac_afh_get_track(struct mp4 *mp4)
53 {
54         int32_t i, rc, num_tracks = mp4_total_tracks(mp4);
55
56         assert(num_tracks >= 0);
57         for (i = 0; i < num_tracks; i++) {
58                 unsigned char *buf = NULL;
59                 unsigned buf_size = 0;
60
61                 mp4_get_decoder_config(mp4, i, &buf, &buf_size);
62                 if (buf) {
63                         mp4AudioSpecificConfig masc;
64                         rc = NeAACDecAudioSpecificConfig(buf, buf_size, &masc);
65                         free(buf);
66                         if (rc < 0)
67                                 continue;
68                         return i;
69                 }
70         }
71         return -1; /* no audio track */
72 }
73
74 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
75 {
76         int ret;
77         struct aac_afh_context *c = para_malloc(sizeof(*c));
78
79         c->map = map;
80         c->mapsize = mapsize;
81         c->fpos = 0;
82         c->cb.read = aac_afh_read_cb;
83         c->cb.seek = aac_afh_seek_cb;
84         c->cb.user_data = c;
85
86         ret = -E_MP4_OPEN;
87         c->mp4 = mp4_open_read(&c->cb);
88         if (!c->mp4)
89                 goto free_ctx;
90         c->track = aac_afh_get_track(c->mp4);
91         ret = -E_MP4_TRACK;
92         if (c->track < 0)
93                 goto close_mp4;
94         *afh_context = c;
95         return 0;
96 close_mp4:
97         mp4_close(c->mp4);
98 free_ctx:
99         free(c);
100         *afh_context = NULL;
101         return ret;
102 }
103
104 static void aac_afh_close(void *afh_context)
105 {
106         struct aac_afh_context *c = afh_context;
107         mp4_close(c->mp4);
108         free(c);
109 }
110
111 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
112                 const char **buf, uint32_t *len)
113 {
114         struct aac_afh_context *c = afh_context;
115         int32_t ss;
116         size_t offset;
117
118         assert(chunk_num <= INT_MAX);
119         mp4_set_sample_position(c->mp4, c->track, chunk_num);
120         offset = c->fpos;
121         ss = mp4_get_sample_size(c->mp4, c->track, chunk_num);
122         if (ss <= 0)
123                 return -E_MP4_BAD_SAMPLE;
124         assert(ss + offset <= c->mapsize);
125         *buf = c->map + offset;
126         *len = ss;
127         return 1;
128 }
129
130 static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
131 {
132         mp4_meta_get_artist(mp4, &tags->artist);
133         mp4_meta_get_title(mp4, &tags->title);
134         mp4_meta_get_date(mp4, &tags->year);
135         mp4_meta_get_album(mp4, &tags->album);
136         mp4_meta_get_comment(mp4, &tags->comment);
137 }
138
139 /*
140  * Init m4a file and write some tech data to given pointers.
141  */
142 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
143                 struct afh_info *afhi)
144 {
145         int ret;
146         int32_t rv;
147         struct aac_afh_context *c;
148         uint64_t milliseconds;
149         const char *buf;
150         uint32_t n, len;
151
152         ret = aac_afh_open(map, numbytes, (void **)&c);
153         if (ret < 0)
154                 return ret;
155
156         ret = -E_MP4_BAD_SAMPLERATE;
157         rv = mp4_get_sample_rate(c->mp4, c->track);
158         if (rv <= 0)
159                 goto close;
160         afhi->frequency = rv;
161
162         ret = -E_MP4_BAD_CHANNEL_COUNT;
163         rv = mp4_get_channel_count(c->mp4, c->track);
164         if (rv <= 0)
165                 goto close;
166         afhi->channels = rv;
167
168         ret = -E_MP4_BAD_SAMPLE_COUNT;
169         rv = mp4_num_samples(c->mp4, c->track);
170         if (rv <= 0)
171                 goto close;
172         afhi->chunks_total = rv;
173         afhi->max_chunk_size = 0;
174         for (n = 0; n < afhi->chunks_total; n++) {
175                 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
176                         break;
177                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
178         }
179         milliseconds = mp4_get_duration(c->mp4, c->track);
180         afhi->seconds_total = milliseconds / 1000;
181         ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
182         if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
183                 numbytes -= buf - map;
184         afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
185         _aac_afh_get_taginfo(c->mp4, &afhi->tags);
186         ret = 1;
187 close:
188         aac_afh_close(c);
189         return ret;
190 }
191
192 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
193 {
194         int fd = *(int *)user_data;
195         return read(fd, dest, want);
196 }
197
198 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
199 {
200         int fd = *(int *)user_data;
201         return lseek(fd, pos, SEEK_SET);
202 }
203
204 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
205 {
206         int fd = *(int *)user_data;
207         return write(fd, dest, want);
208 }
209
210 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
211 {
212         int fd = *(int *)user_data;
213         off_t offset = lseek(fd, 0, SEEK_CUR);
214         return ftruncate(fd, offset);
215 }
216
217 static void replace_tag(struct mp4_tag *tag, const char *new_val, bool *found)
218 {
219         free(tag->value);
220         tag->value = para_strdup(new_val);
221         *found = true;
222 }
223
224 static void add_tag(struct mp4_metadata *md, const char *item, const char *value)
225 {
226         md->tags[md->count].item = para_strdup(item);
227         md->tags[md->count].value = para_strdup(value);
228         md->count++;
229 }
230
231 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
232                 struct taginfo *tags, int fd, __a_unused const char *filename)
233 {
234         int ret, i;
235         int32_t rv;
236         struct mp4_metadata metadata;
237         struct mp4 *mp4;
238         struct mp4_callback cb = {
239                 .read = aac_afh_meta_read_cb,
240                 .seek = aac_afh_meta_seek_cb,
241                 .write = aac_afh_meta_write_cb,
242                 .truncate = aac_afh_meta_truncate_cb,
243                 .user_data = &fd
244         };
245         bool found_artist = false, found_title = false, found_album = false,
246                 found_year = false, found_comment = false;
247
248         ret = write_all(fd, map, mapsize);
249         if (ret < 0)
250                 return ret;
251         lseek(fd, 0, SEEK_SET);
252
253         mp4 = mp4_open_meta(&cb);
254         if (!mp4)
255                 return -E_MP4_OPEN;
256
257         ret = -E_MP4_META_READ;
258         rv = mp4_meta_get_num_items(mp4);
259         if (rv < 0)
260                 goto close;
261         metadata.count = rv;
262         PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
263
264         metadata.tags = para_malloc((metadata.count + 5) * sizeof(struct mp4_tag));
265         for (i = 0; i < metadata.count; i++) {
266                 struct mp4_tag *tag = metadata.tags + i;
267
268                 ret = -E_MP4_META_READ;
269                 if (!mp4_meta_get_by_index(mp4, i, &tag->item, &tag->value))
270                         goto free_tags;
271                 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
272                 if (!strcmp(tag->item, "artist"))
273                         replace_tag(tag, tags->artist, &found_artist);
274                 else if (!strcmp(tag->item, "title"))
275                         replace_tag(tag, tags->title, &found_title);
276                 else if (!strcmp(tag->item, "album"))
277                         replace_tag(tag, tags->album, &found_album);
278                 else if (!strcmp(tag->item, "date"))
279                         replace_tag(tag, tags->year, &found_year);
280                 else if (!strcmp(tag->item, "comment"))
281                         replace_tag(tag, tags->comment, &found_comment);
282         }
283         if (!found_artist)
284                 add_tag(&metadata, "artist", tags->artist);
285         if (!found_title)
286                 add_tag(&metadata, "title", tags->title);
287         if (!found_album)
288                 add_tag(&metadata, "album", tags->album);
289         if (!found_year)
290                 add_tag(&metadata, "date", tags->year);
291         if (!found_comment)
292                 add_tag(&metadata, "comment", tags->comment);
293         ret = -E_MP4_META_WRITE;
294         if (!mp4_meta_update(&cb, &metadata))
295                 goto free_tags;
296         ret = 1;
297 free_tags:
298         for (; i > 0; i--) {
299                 free(metadata.tags[i - 1].item);
300                 free(metadata.tags[i - 1].value);
301         }
302         free(metadata.tags);
303 close:
304         mp4_close(mp4);
305         return ret;
306 }
307
308 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
309
310 /**
311  * The audio format handler for the Advanced Audio Codec.
312  *
313  * This is only compiled in if the faad library is installed.
314  */
315 const struct audio_format_handler aac_afh = {
316         .get_file_info = aac_get_file_info,
317         .suffixes = aac_suffixes,
318         .rewrite_tags = aac_afh_rewrite_tags,
319         .open = aac_afh_open,
320         .get_chunk = aac_afh_get_chunk,
321         .close = aac_afh_close,
322 };