]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
mp4: Simplify and doxify meta tag accessors.
[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, num_tracks = mp4_total_tracks(mp4);
55
56         assert(num_tracks >= 0);
57         for (i = 0; i < num_tracks; i++)
58                 if (mp4_is_audio_track(mp4, i))
59                         return i;
60         return -E_MP4_TRACK; /* no audio track */
61 }
62
63 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
64 {
65         int ret;
66         struct aac_afh_context *c = para_malloc(sizeof(*c));
67
68         c->map = map;
69         c->mapsize = mapsize;
70         c->fpos = 0;
71         c->cb.read = aac_afh_read_cb;
72         c->cb.seek = aac_afh_seek_cb;
73         c->cb.user_data = c;
74
75         ret = -E_MP4_OPEN;
76         c->mp4 = mp4_open_read(&c->cb);
77         if (!c->mp4)
78                 goto free_ctx;
79         ret = aac_afh_get_track(c->mp4);
80         if (ret < 0)
81                 goto close_mp4;
82         c->track = ret;
83         *afh_context = c;
84         return 0;
85 close_mp4:
86         mp4_close(c->mp4);
87 free_ctx:
88         free(c);
89         *afh_context = NULL;
90         return ret;
91 }
92
93 static void aac_afh_close(void *afh_context)
94 {
95         struct aac_afh_context *c = afh_context;
96         mp4_close(c->mp4);
97         free(c);
98 }
99
100 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
101                 const char **buf, uint32_t *len)
102 {
103         struct aac_afh_context *c = afh_context;
104         int32_t ss;
105         size_t offset;
106
107         assert(chunk_num <= INT_MAX);
108         mp4_set_sample_position(c->mp4, c->track, chunk_num);
109         offset = c->fpos;
110         ss = mp4_get_sample_size(c->mp4, c->track, chunk_num);
111         if (ss <= 0)
112                 return -E_MP4_BAD_SAMPLE;
113         assert(ss + offset <= c->mapsize);
114         *buf = c->map + offset;
115         *len = ss;
116         return 1;
117 }
118
119 static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
120 {
121         tags->artist = mp4_meta_get_artist(mp4);
122         tags->title = mp4_meta_get_title(mp4);
123         tags->year = mp4_meta_get_date(mp4);
124         tags->album = mp4_meta_get_album(mp4);
125         tags->comment = mp4_meta_get_comment(mp4);
126 }
127
128 /*
129  * Init m4a file and write some tech data to given pointers.
130  */
131 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
132                 struct afh_info *afhi)
133 {
134         int ret;
135         int32_t rv;
136         struct aac_afh_context *c;
137         uint64_t milliseconds;
138         const char *buf;
139         uint32_t n, len;
140
141         ret = aac_afh_open(map, numbytes, (void **)&c);
142         if (ret < 0)
143                 return ret;
144
145         ret = -E_MP4_BAD_SAMPLERATE;
146         rv = mp4_get_sample_rate(c->mp4, c->track);
147         if (rv <= 0)
148                 goto close;
149         afhi->frequency = rv;
150
151         ret = -E_MP4_BAD_CHANNEL_COUNT;
152         rv = mp4_get_channel_count(c->mp4, c->track);
153         if (rv <= 0)
154                 goto close;
155         afhi->channels = rv;
156
157         ret = -E_MP4_BAD_SAMPLE_COUNT;
158         rv = mp4_num_samples(c->mp4, c->track);
159         if (rv <= 0)
160                 goto close;
161         afhi->chunks_total = rv;
162         afhi->max_chunk_size = 0;
163         for (n = 0; n < afhi->chunks_total; n++) {
164                 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
165                         break;
166                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
167         }
168         milliseconds = mp4_get_duration(c->mp4, c->track);
169         afhi->seconds_total = milliseconds / 1000;
170         ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
171         if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
172                 numbytes -= buf - map;
173         afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
174         _aac_afh_get_taginfo(c->mp4, &afhi->tags);
175         ret = 1;
176 close:
177         aac_afh_close(c);
178         return ret;
179 }
180
181 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
182 {
183         int fd = *(int *)user_data;
184         return read(fd, dest, want);
185 }
186
187 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
188 {
189         int fd = *(int *)user_data;
190         return lseek(fd, pos, SEEK_SET);
191 }
192
193 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
194 {
195         int fd = *(int *)user_data;
196         return write(fd, dest, want);
197 }
198
199 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
200 {
201         int fd = *(int *)user_data;
202         off_t offset = lseek(fd, 0, SEEK_CUR);
203         return ftruncate(fd, offset);
204 }
205
206 static void replace_tag(struct mp4_tag *tag, const char *new_val, bool *found)
207 {
208         free(tag->value);
209         tag->value = para_strdup(new_val);
210         *found = true;
211 }
212
213 static void add_tag(struct mp4_metadata *md, const char *item, const char *value)
214 {
215         md->tags[md->count].item = para_strdup(item);
216         md->tags[md->count].value = para_strdup(value);
217         md->count++;
218 }
219
220 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
221                 struct taginfo *tags, int fd, __a_unused const char *filename)
222 {
223         int ret, i;
224         int32_t rv;
225         struct mp4_metadata metadata;
226         struct mp4 *mp4;
227         struct mp4_callback cb = {
228                 .read = aac_afh_meta_read_cb,
229                 .seek = aac_afh_meta_seek_cb,
230                 .write = aac_afh_meta_write_cb,
231                 .truncate = aac_afh_meta_truncate_cb,
232                 .user_data = &fd
233         };
234         bool found_artist = false, found_title = false, found_album = false,
235                 found_year = false, found_comment = false;
236
237         ret = write_all(fd, map, mapsize);
238         if (ret < 0)
239                 return ret;
240         lseek(fd, 0, SEEK_SET);
241
242         mp4 = mp4_open_meta(&cb);
243         if (!mp4)
244                 return -E_MP4_OPEN;
245
246         ret = -E_MP4_META_READ;
247         rv = mp4_meta_get_num_items(mp4);
248         if (rv < 0)
249                 goto close;
250         metadata.count = rv;
251         PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
252
253         metadata.tags = para_malloc((metadata.count + 5) * sizeof(struct mp4_tag));
254         for (i = 0; i < metadata.count; i++) {
255                 struct mp4_tag *tag = metadata.tags + i;
256
257                 ret = -E_MP4_META_READ;
258                 if (!mp4_meta_get_by_index(mp4, i, &tag->item, &tag->value))
259                         goto free_tags;
260                 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
261                 if (!strcmp(tag->item, "artist"))
262                         replace_tag(tag, tags->artist, &found_artist);
263                 else if (!strcmp(tag->item, "title"))
264                         replace_tag(tag, tags->title, &found_title);
265                 else if (!strcmp(tag->item, "album"))
266                         replace_tag(tag, tags->album, &found_album);
267                 else if (!strcmp(tag->item, "date"))
268                         replace_tag(tag, tags->year, &found_year);
269                 else if (!strcmp(tag->item, "comment"))
270                         replace_tag(tag, tags->comment, &found_comment);
271         }
272         if (!found_artist)
273                 add_tag(&metadata, "artist", tags->artist);
274         if (!found_title)
275                 add_tag(&metadata, "title", tags->title);
276         if (!found_album)
277                 add_tag(&metadata, "album", tags->album);
278         if (!found_year)
279                 add_tag(&metadata, "date", tags->year);
280         if (!found_comment)
281                 add_tag(&metadata, "comment", tags->comment);
282         ret = -E_MP4_META_WRITE;
283         if (!mp4_meta_update(&cb, &metadata))
284                 goto free_tags;
285         ret = 1;
286 free_tags:
287         for (; i > 0; i--) {
288                 free(metadata.tags[i - 1].item);
289                 free(metadata.tags[i - 1].value);
290         }
291         free(metadata.tags);
292 close:
293         mp4_close(mp4);
294         return ret;
295 }
296
297 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
298
299 /**
300  * The audio format handler for the Advanced Audio Codec.
301  *
302  * This is only compiled in if the faad library is installed.
303  */
304 const struct audio_format_handler aac_afh = {
305         .get_file_info = aac_get_file_info,
306         .suffixes = aac_suffixes,
307         .rewrite_tags = aac_afh_rewrite_tags,
308         .open = aac_afh_open,
309         .get_chunk = aac_afh_get_chunk,
310         .close = aac_afh_close,
311 };