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