]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
mp4: Simplify sample_range_size().
[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
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 ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_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_or_add_tag(const char *item, const char *value,
207                 struct mp4_metadata *meta)
208 {
209         uint32_t n;
210         struct mp4_tag *t;
211
212         for (n = 0; n < meta->count; n++) {
213                 t = meta->tags + n;
214                 if (strcasecmp(t->item, item))
215                         continue;
216                 free(t->value);
217                 t->value = para_strdup(value);
218                 return;
219         }
220         /* item not found, add new tag */
221         meta->tags = para_realloc(meta->tags, (meta->count + 1)
222                 * sizeof(struct mp4_tag));
223         t = meta->tags + meta->count;
224         t->item = para_strdup(item);
225         t->value = para_strdup(value);
226         meta->count++;
227 }
228
229 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
230                 struct taginfo *tags, int fd, __a_unused const char *filename)
231 {
232         int ret;
233         struct mp4_metadata *metadata;
234         struct mp4 *mp4;
235         struct mp4_callback cb = {
236                 .read = aac_afh_meta_read_cb,
237                 .seek = aac_afh_meta_seek_cb,
238                 .write = aac_afh_meta_write_cb,
239                 .truncate = aac_afh_meta_truncate_cb,
240                 .user_data = &fd
241         };
242
243         ret = write_all(fd, map, mapsize);
244         if (ret < 0)
245                 return ret;
246         lseek(fd, 0, SEEK_SET);
247
248         mp4 = mp4_open_meta(&cb);
249         if (!mp4)
250                 return -E_MP4_OPEN;
251         metadata = mp4_get_meta(mp4);
252         PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
253         replace_or_add_tag("artist", tags->artist, metadata);
254         replace_or_add_tag("title", tags->title, metadata);
255         replace_or_add_tag("album", tags->album, metadata);
256         replace_or_add_tag("date", tags->year, metadata);
257         replace_or_add_tag("comment", tags->comment, metadata);
258         ret = -E_MP4_META_WRITE;
259         if (!mp4_meta_update(mp4))
260                 goto close;
261         ret = 1;
262 close:
263         mp4_close(mp4);
264         return ret;
265 }
266
267 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
268
269 /**
270  * The audio format handler for the Advanced Audio Codec.
271  *
272  * This is only compiled in if the faad library is installed.
273  */
274 const struct audio_format_handler aac_afh = {
275         .get_file_info = aac_get_file_info,
276         .suffixes = aac_suffixes,
277         .rewrite_tags = aac_afh_rewrite_tags,
278         .open = aac_afh_open,
279         .get_chunk = aac_afh_get_chunk,
280         .close = aac_afh_close,
281 };