]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
42cba27a3670ddac4bde28f9d683c9c31a86e924
[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         struct mp4 *mp4;
26         struct mp4_callback cb;
27 };
28
29 static ssize_t aac_afh_read_cb(void *user_data, void *dest, size_t want)
30 {
31         struct aac_afh_context *c = user_data;
32         size_t have, rv;
33
34         if (want == 0 || c->fpos >= c->mapsize)
35                 return 0;
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);
40         c->fpos += rv;
41         return rv;
42 }
43
44 static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
45 {
46         struct aac_afh_context *c = user_data;
47         c->fpos = pos;
48         return 0;
49 }
50
51 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
52 {
53         int ret;
54         struct aac_afh_context *c = para_malloc(sizeof(*c));
55
56         c->map = map;
57         c->mapsize = mapsize;
58         c->fpos = 0;
59         c->cb.read = aac_afh_read_cb;
60         c->cb.seek = aac_afh_seek_cb;
61         c->cb.user_data = c;
62
63         ret = -E_MP4_OPEN;
64         c->mp4 = mp4_open_read(&c->cb);
65         if (!c->mp4)
66                 goto free_ctx;
67         *afh_context = c;
68         return 0;
69 free_ctx:
70         free(c);
71         *afh_context = NULL;
72         return ret;
73 }
74
75 static void aac_afh_close(void *afh_context)
76 {
77         struct aac_afh_context *c = afh_context;
78         mp4_close(c->mp4);
79         free(c);
80 }
81
82 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
83                 const char **buf, uint32_t *len)
84 {
85         struct aac_afh_context *c = afh_context;
86         int32_t ss;
87         size_t offset;
88         int ret;
89
90         ret = mp4_set_sample_position(c->mp4, chunk_num);
91         if (ret < 0)
92                 return ret;
93         offset = c->fpos;
94         ss = mp4_get_sample_size(c->mp4, chunk_num);
95         if (ss <= 0)
96                 return -E_MP4_BAD_SAMPLE;
97         assert(ss + offset <= c->mapsize);
98         *buf = c->map + offset;
99         *len = ss;
100         return 1;
101 }
102
103 static void _aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
104 {
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);
110 }
111
112 /*
113  * Init m4a file and write some tech data to given pointers.
114  */
115 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
116                 struct afh_info *afhi)
117 {
118         int ret;
119         int32_t rv;
120         struct aac_afh_context *c;
121         uint64_t milliseconds;
122         const char *buf;
123         uint32_t n, len;
124
125         ret = aac_afh_open(map, numbytes, (void **)&c);
126         if (ret < 0)
127                 return ret;
128
129         ret = -E_MP4_BAD_SAMPLERATE;
130         rv = mp4_get_sample_rate(c->mp4);
131         if (rv <= 0)
132                 goto close;
133         afhi->frequency = rv;
134
135         ret = -E_MP4_BAD_CHANNEL_COUNT;
136         rv = mp4_get_channel_count(c->mp4);
137         if (rv <= 0)
138                 goto close;
139         afhi->channels = rv;
140
141         ret = -E_MP4_BAD_SAMPLE_COUNT;
142         rv = mp4_num_samples(c->mp4);
143         if (rv <= 0)
144                 goto close;
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)
149                         break;
150                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
151         }
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);
159         ret = 1;
160 close:
161         aac_afh_close(c);
162         return ret;
163 }
164
165 static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
166 {
167         int fd = *(int *)user_data;
168         return read(fd, dest, want);
169 }
170
171 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
172 {
173         int fd = *(int *)user_data;
174         return lseek(fd, pos, SEEK_SET);
175 }
176
177 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
178 {
179         int fd = *(int *)user_data;
180         return write(fd, dest, want);
181 }
182
183 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
184 {
185         int fd = *(int *)user_data;
186         off_t offset = lseek(fd, 0, SEEK_CUR);
187         return ftruncate(fd, offset);
188 }
189
190 static void replace_or_add_tag(const char *item, const char *value,
191                 struct mp4_metadata *meta)
192 {
193         uint32_t n;
194         struct mp4_tag *t;
195
196         for (n = 0; n < meta->count; n++) {
197                 t = meta->tags + n;
198                 if (strcasecmp(t->item, item))
199                         continue;
200                 free(t->value);
201                 t->value = para_strdup(value);
202                 return;
203         }
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);
210         meta->count++;
211 }
212
213 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
214                 struct taginfo *tags, int fd, __a_unused const char *filename)
215 {
216         int ret;
217         struct mp4_metadata *metadata;
218         struct mp4 *mp4;
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,
224                 .user_data = &fd
225         };
226
227         ret = write_all(fd, map, mapsize);
228         if (ret < 0)
229                 return ret;
230         lseek(fd, 0, SEEK_SET);
231
232         mp4 = mp4_open_meta(&cb);
233         if (!mp4)
234                 return -E_MP4_OPEN;
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))
244                 goto close;
245         ret = 1;
246 close:
247         mp4_close(mp4);
248         return ret;
249 }
250
251 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
252
253 /**
254  * The audio format handler for the Advanced Audio Codec.
255  *
256  * This is only compiled in if the faad library is installed.
257  */
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,
265 };