]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
mp4: Remove E_MP4_BAD_CHANNEL_COUNT.
[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 off_t aac_afh_seek_cb(void *user_data, off_t offset, int whence)
45 {
46         struct aac_afh_context *c = user_data;
47
48         if (whence == SEEK_SET)
49                 c->fpos = offset;
50         else if (whence == SEEK_CUR)
51                 c->fpos += offset;
52         else if (whence == SEEK_END)
53                 c->fpos = c->mapsize + offset;
54         else
55                 assert(false);
56         return c->fpos;
57 }
58
59 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
60 {
61         int ret;
62         struct aac_afh_context *c = para_malloc(sizeof(*c));
63
64         c->map = map;
65         c->mapsize = mapsize;
66         c->fpos = 0;
67         c->cb.read = aac_afh_read_cb;
68         c->cb.seek = aac_afh_seek_cb;
69         c->cb.user_data = c;
70
71         ret = mp4_open_read(&c->cb, &c->mp4);
72         if (ret < 0)
73                 goto free_ctx;
74         *afh_context = c;
75         return 0;
76 free_ctx:
77         free(c);
78         *afh_context = NULL;
79         return ret;
80 }
81
82 static void aac_afh_close(void *afh_context)
83 {
84         struct aac_afh_context *c = afh_context;
85         mp4_close(c->mp4);
86         free(c);
87 }
88
89 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
90                 const char **buf, uint32_t *len)
91 {
92         struct aac_afh_context *c = afh_context;
93         uint32_t ss;
94         size_t offset;
95         int ret;
96
97         ret = mp4_set_sample_position(c->mp4, chunk_num);
98         if (ret < 0)
99                 return ret;
100         offset = c->fpos;
101         ret = mp4_get_sample_size(c->mp4, chunk_num, &ss);
102         if (ret < 0)
103                 return ret;
104         assert(ss + offset <= c->mapsize);
105         *buf = c->map + offset;
106         *len = ss;
107         return 1;
108 }
109
110 static void aac_afh_get_taginfo(const struct mp4 *mp4, struct taginfo *tags)
111 {
112         tags->artist = mp4_get_tag_value(mp4, "artist");
113         tags->title = mp4_get_tag_value(mp4, "title");
114         tags->year = mp4_get_tag_value(mp4, "date");
115         tags->album = mp4_get_tag_value(mp4, "album");
116         tags->comment = mp4_get_tag_value(mp4, "comment");
117 }
118
119 /*
120  * Init m4a file and write some tech data to given pointers.
121  */
122 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
123                 struct afh_info *afhi)
124 {
125         int ret;
126         int32_t rv;
127         struct aac_afh_context *c;
128         uint64_t milliseconds;
129         const char *buf;
130         uint32_t n, len;
131
132         ret = aac_afh_open(map, numbytes, (void **)&c);
133         if (ret < 0)
134                 return ret;
135
136         ret = -E_MP4_BAD_SAMPLERATE;
137         rv = mp4_get_sample_rate(c->mp4);
138         if (rv <= 0)
139                 goto close;
140         afhi->frequency = rv;
141
142         afhi->channels = mp4_get_channel_count(c->mp4);
143         assert(afhi->channels > 0);
144
145         ret = -E_MP4_BAD_SAMPLE_COUNT;
146         rv = mp4_num_samples(c->mp4);
147         if (rv <= 0)
148                 goto close;
149         afhi->chunks_total = rv;
150         afhi->max_chunk_size = 0;
151         for (n = 0; n < afhi->chunks_total; n++) {
152                 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
153                         break;
154                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
155         }
156         milliseconds = mp4_get_duration(c->mp4);
157         afhi->seconds_total = milliseconds / 1000;
158         ms2tv(milliseconds / afhi->chunks_total, &afhi->chunk_tv);
159         if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
160                 numbytes -= buf - map;
161         afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
162         aac_afh_get_taginfo(c->mp4, &afhi->tags);
163         ret = 1;
164 close:
165         aac_afh_close(c);
166         return ret;
167 }
168
169 static ssize_t aac_afh_meta_read_cb(void *user_data, void *dest, size_t want)
170 {
171         int fd = *(int *)user_data;
172         return read(fd, dest, want);
173 }
174
175 static off_t aac_afh_meta_seek_cb(void *user_data, off_t offset, int whence)
176 {
177         int fd = *(int *)user_data;
178         off_t ret = lseek(fd, offset, whence);
179
180         assert(ret != (off_t)-1);
181         return ret;
182 }
183
184 static ssize_t aac_afh_meta_write_cb(void *user_data, void *dest, size_t count)
185 {
186         int fd = *(int *)user_data;
187         return write(fd, dest, count);
188 }
189
190 static int aac_afh_meta_truncate_cb(void *user_data)
191 {
192         int fd = *(int *)user_data;
193         off_t offset = lseek(fd, 0, SEEK_CUR);
194         return ftruncate(fd, offset);
195 }
196
197 static void replace_or_add_tag(const char *item, const char *value,
198                 struct mp4_metadata *meta)
199 {
200         uint32_t n;
201         struct mp4_tag *t;
202
203         for (n = 0; n < meta->count; n++) {
204                 t = meta->tags + n;
205                 if (strcasecmp(t->item, item))
206                         continue;
207                 free(t->value);
208                 t->value = para_strdup(value);
209                 return;
210         }
211         /* item not found, add new tag */
212         meta->tags = para_realloc(meta->tags, (meta->count + 1)
213                 * sizeof(struct mp4_tag));
214         t = meta->tags + meta->count;
215         t->item = para_strdup(item);
216         t->value = para_strdup(value);
217         meta->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;
224         struct mp4_metadata *metadata;
225         struct mp4 *mp4;
226         struct mp4_callback cb = {
227                 .read = aac_afh_meta_read_cb,
228                 .seek = aac_afh_meta_seek_cb,
229                 .write = aac_afh_meta_write_cb,
230                 .truncate = aac_afh_meta_truncate_cb,
231                 .user_data = &fd
232         };
233
234         ret = write_all(fd, map, mapsize);
235         if (ret < 0)
236                 return ret;
237         lseek(fd, 0, SEEK_SET);
238
239         ret = mp4_open_meta(&cb, &mp4);
240         if (ret < 0)
241                 return ret;
242         metadata = mp4_get_meta(mp4);
243         PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
244         replace_or_add_tag("artist", tags->artist, metadata);
245         replace_or_add_tag("title", tags->title, metadata);
246         replace_or_add_tag("album", tags->album, metadata);
247         replace_or_add_tag("date", tags->year, metadata);
248         replace_or_add_tag("comment", tags->comment, metadata);
249         ret = mp4_meta_update(mp4);
250         mp4_close(mp4);
251         return ret;
252 }
253
254 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
255
256 /**
257  * The audio format handler for the Advanced Audio Codec.
258  *
259  * This is only compiled in if the faad library is installed.
260  */
261 const struct audio_format_handler aac_afh = {
262         .get_file_info = aac_get_file_info,
263         .suffixes = aac_suffixes,
264         .rewrite_tags = aac_afh_rewrite_tags,
265         .open = aac_afh_open,
266         .get_chunk = aac_afh_get_chunk,
267         .close = aac_afh_close,
268 };