]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
mp4: Provide return value for mp4_set_sample_position().
[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 ssize_t aac_afh_read_cb(void *user_data, void *dest, size_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, 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_get_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         int ret;
107
108         ret = mp4_set_sample_position(c->mp4, c->track, chunk_num);
109         if (ret < 0)
110                 return ret;
111         offset = c->fpos;
112         ss = mp4_get_sample_size(c->mp4, c->track, chunk_num);
113         if (ss <= 0)
114                 return -E_MP4_BAD_SAMPLE;
115         assert(ss + offset <= c->mapsize);
116         *buf = c->map + offset;
117         *len = ss;
118         return 1;
119 }
120
121 static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
122 {
123         tags->artist = mp4_meta_get_artist(mp4);
124         tags->title = mp4_meta_get_title(mp4);
125         tags->year = mp4_meta_get_date(mp4);
126         tags->album = mp4_meta_get_album(mp4);
127         tags->comment = mp4_meta_get_comment(mp4);
128 }
129
130 /*
131  * Init m4a file and write some tech data to given pointers.
132  */
133 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
134                 struct afh_info *afhi)
135 {
136         int ret;
137         int32_t rv;
138         struct aac_afh_context *c;
139         uint64_t milliseconds;
140         const char *buf;
141         uint32_t n, len;
142
143         ret = aac_afh_open(map, numbytes, (void **)&c);
144         if (ret < 0)
145                 return ret;
146
147         ret = -E_MP4_BAD_SAMPLERATE;
148         rv = mp4_get_sample_rate(c->mp4, c->track);
149         if (rv <= 0)
150                 goto close;
151         afhi->frequency = rv;
152
153         ret = -E_MP4_BAD_CHANNEL_COUNT;
154         rv = mp4_get_channel_count(c->mp4, c->track);
155         if (rv <= 0)
156                 goto close;
157         afhi->channels = rv;
158
159         ret = -E_MP4_BAD_SAMPLE_COUNT;
160         rv = mp4_num_samples(c->mp4, c->track);
161         if (rv <= 0)
162                 goto close;
163         afhi->chunks_total = rv;
164         afhi->max_chunk_size = 0;
165         for (n = 0; n < afhi->chunks_total; n++) {
166                 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
167                         break;
168                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
169         }
170         milliseconds = mp4_get_duration(c->mp4, c->track);
171         afhi->seconds_total = milliseconds / 1000;
172         ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
173         if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
174                 numbytes -= buf - map;
175         afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
176         _aac_afh_get_taginfo(c->mp4, &afhi->tags);
177         ret = 1;
178 close:
179         aac_afh_close(c);
180         return ret;
181 }
182
183 static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
184 {
185         int fd = *(int *)user_data;
186         return read(fd, dest, want);
187 }
188
189 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
190 {
191         int fd = *(int *)user_data;
192         return lseek(fd, pos, SEEK_SET);
193 }
194
195 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
196 {
197         int fd = *(int *)user_data;
198         return write(fd, dest, want);
199 }
200
201 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
202 {
203         int fd = *(int *)user_data;
204         off_t offset = lseek(fd, 0, SEEK_CUR);
205         return ftruncate(fd, offset);
206 }
207
208 static void replace_or_add_tag(const char *item, const char *value,
209                 struct mp4_metadata *meta)
210 {
211         uint32_t n;
212         struct mp4_tag *t;
213
214         for (n = 0; n < meta->count; n++) {
215                 t = meta->tags + n;
216                 if (strcasecmp(t->item, item))
217                         continue;
218                 free(t->value);
219                 t->value = para_strdup(value);
220                 return;
221         }
222         /* item not found, add new tag */
223         meta->tags = para_realloc(meta->tags, (meta->count + 1)
224                 * sizeof(struct mp4_tag));
225         t = meta->tags + meta->count;
226         t->item = para_strdup(item);
227         t->value = para_strdup(value);
228         meta->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;
235         struct mp4_metadata *metadata;
236         struct mp4 *mp4;
237         struct mp4_callback cb = {
238                 .read = aac_afh_meta_read_cb,
239                 .seek = aac_afh_meta_seek_cb,
240                 .write = aac_afh_meta_write_cb,
241                 .truncate = aac_afh_meta_truncate_cb,
242                 .user_data = &fd
243         };
244
245         ret = write_all(fd, map, mapsize);
246         if (ret < 0)
247                 return ret;
248         lseek(fd, 0, SEEK_SET);
249
250         mp4 = mp4_open_meta(&cb);
251         if (!mp4)
252                 return -E_MP4_OPEN;
253         metadata = mp4_get_meta(mp4);
254         PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
255         replace_or_add_tag("artist", tags->artist, metadata);
256         replace_or_add_tag("title", tags->title, metadata);
257         replace_or_add_tag("album", tags->album, metadata);
258         replace_or_add_tag("date", tags->year, metadata);
259         replace_or_add_tag("comment", tags->comment, metadata);
260         ret = -E_MP4_META_WRITE;
261         if (!mp4_meta_update(mp4))
262                 goto close;
263         ret = 1;
264 close:
265         mp4_close(mp4);
266         return ret;
267 }
268
269 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
270
271 /**
272  * The audio format handler for the Advanced Audio Codec.
273  *
274  * This is only compiled in if the faad library is installed.
275  */
276 const struct audio_format_handler aac_afh = {
277         .get_file_info = aac_get_file_info,
278         .suffixes = aac_suffixes,
279         .rewrite_tags = aac_afh_rewrite_tags,
280         .open = aac_afh_open,
281         .get_chunk = aac_afh_get_chunk,
282         .close = aac_afh_close,
283 };