]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
mp4: Avoid typedefs for data structures.
[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 mp4ff *mp4ff;
27         mp4AudioSpecificConfig masc;
28         struct mp4ff_callback cb;
29 };
30
31 static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want)
32 {
33         struct aac_afh_context *c = user_data;
34         size_t have, rv;
35
36         if (want == 0 || c->fpos >= c->mapsize)
37                 return 0;
38         have = c->mapsize - c->fpos;
39         rv = PARA_MIN(have, (size_t)want);
40         PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos);
41         memcpy(dest, c->map + c->fpos, rv);
42         c->fpos += rv;
43         return rv;
44 }
45
46 static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
47 {
48         struct aac_afh_context *c = user_data;
49         c->fpos = pos;
50         return 0;
51 }
52
53 static int32_t aac_afh_get_track(struct mp4ff *mp4ff, mp4AudioSpecificConfig *masc)
54 {
55         int32_t i, rc, num_tracks = mp4ff_total_tracks(mp4ff);
56
57         assert(num_tracks >= 0);
58         for (i = 0; i < num_tracks; i++) {
59                 unsigned char *buf = NULL;
60                 unsigned buf_size = 0;
61
62                 mp4ff_get_decoder_config(mp4ff, i, &buf, &buf_size);
63                 if (buf) {
64                         rc = NeAACDecAudioSpecificConfig(buf, buf_size, masc);
65                         free(buf);
66                         if (rc < 0)
67                                 continue;
68                         return i;
69                 }
70         }
71         return -1; /* no audio track */
72 }
73
74 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
75 {
76         int ret;
77         struct aac_afh_context *c = para_malloc(sizeof(*c));
78
79         c->map = map;
80         c->mapsize = mapsize;
81         c->fpos = 0;
82         c->cb.read = aac_afh_read_cb;
83         c->cb.seek = aac_afh_seek_cb;
84         c->cb.user_data = c;
85
86         ret = -E_MP4FF_OPEN;
87         c->mp4ff = mp4ff_open_read(&c->cb);
88         if (!c->mp4ff)
89                 goto free_ctx;
90         c->track = aac_afh_get_track(c->mp4ff, &c->masc);
91         ret = -E_MP4FF_TRACK;
92         if (c->track < 0)
93                 goto close_mp4ff;
94         *afh_context = c;
95         return 0;
96 close_mp4ff:
97         mp4ff_close(c->mp4ff);
98 free_ctx:
99         free(c);
100         *afh_context = NULL;
101         return ret;
102 }
103
104 static void aac_afh_close(void *afh_context)
105 {
106         struct aac_afh_context *c = afh_context;
107         mp4ff_close(c->mp4ff);
108         free(c);
109 }
110
111 static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context,
112                 const char **buf, uint32_t *len)
113 {
114         struct aac_afh_context *c = afh_context;
115         int32_t ss;
116         size_t offset;
117
118         assert(chunk_num <= INT_MAX);
119         mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num);
120         offset = c->fpos;
121         ss = mp4ff_get_sample_size(c->mp4ff, c->track, chunk_num);
122         if (ss <= 0)
123                 return -E_MP4FF_BAD_SAMPLE;
124         assert(ss + offset <= c->mapsize);
125         *buf = c->map + offset;
126         *len = ss;
127         return 1;
128 }
129
130 static void _aac_afh_get_taginfo(const struct mp4ff *mp4ff, struct taginfo *tags)
131 {
132         mp4ff_meta_get_artist(mp4ff, &tags->artist);
133         mp4ff_meta_get_title(mp4ff, &tags->title);
134         mp4ff_meta_get_date(mp4ff, &tags->year);
135         mp4ff_meta_get_album(mp4ff, &tags->album);
136         mp4ff_meta_get_comment(mp4ff, &tags->comment);
137 }
138
139 /*
140  * Init m4a file and write some tech data to given pointers.
141  */
142 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
143                 struct afh_info *afhi)
144 {
145         int ret;
146         int32_t rv;
147         struct aac_afh_context *c;
148         int64_t tmp;
149         const char *buf;
150         uint32_t n, len;
151
152         ret = aac_afh_open(map, numbytes, (void **)&c);
153         if (ret < 0)
154                 return ret;
155
156         ret = -E_MP4FF_BAD_SAMPLERATE;
157         rv = mp4ff_get_sample_rate(c->mp4ff, c->track);
158         if (rv <= 0)
159                 goto close;
160         afhi->frequency = rv;
161
162         ret = -E_MP4FF_BAD_CHANNEL_COUNT;
163         rv = mp4ff_get_channel_count(c->mp4ff, c->track);
164         if (rv <= 0)
165                 goto close;
166         afhi->channels = rv;
167
168         ret = -E_MP4FF_BAD_SAMPLE_COUNT;
169         rv = mp4ff_num_samples(c->mp4ff, c->track);
170         if (rv <= 0)
171                 goto close;
172         afhi->chunks_total = rv;
173         afhi->max_chunk_size = 0;
174         for (n = 0; n < afhi->chunks_total; n++) {
175                 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
176                         break;
177                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
178         }
179
180         tmp = c->masc.sbr_present_flag == 1? 2048 : 1024;
181         afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency;
182         ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv);
183
184         if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
185                 numbytes -= buf - map;
186         afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
187         _aac_afh_get_taginfo(c->mp4ff, &afhi->tags);
188         ret = 1;
189 close:
190         aac_afh_close(c);
191         return ret;
192 }
193
194 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
195 {
196         int fd = *(int *)user_data;
197         return read(fd, dest, want);
198 }
199
200 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
201 {
202         int fd = *(int *)user_data;
203         return lseek(fd, pos, SEEK_SET);
204 }
205
206 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
207 {
208         int fd = *(int *)user_data;
209         return write(fd, dest, want);
210 }
211
212 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
213 {
214         int fd = *(int *)user_data;
215         off_t offset = lseek(fd, 0, SEEK_CUR);
216         return ftruncate(fd, offset);
217 }
218
219 static void replace_tag(struct mp4ff_tag *tag, const char *new_val, bool *found)
220 {
221         free(tag->value);
222         tag->value = para_strdup(new_val);
223         *found = true;
224 }
225
226 static void add_tag(struct mp4ff_metadata *md, const char *item, const char *value)
227 {
228         md->tags[md->count].item = para_strdup(item);
229         md->tags[md->count].value = para_strdup(value);
230         md->count++;
231 }
232
233 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
234                 struct taginfo *tags, int fd, __a_unused const char *filename)
235 {
236         int ret, i;
237         int32_t rv;
238         struct mp4ff_metadata metadata;
239         struct mp4ff *mp4ff;
240         struct mp4ff_callback cb = {
241                 .read = aac_afh_meta_read_cb,
242                 .seek = aac_afh_meta_seek_cb,
243                 .write = aac_afh_meta_write_cb,
244                 .truncate = aac_afh_meta_truncate_cb,
245                 .user_data = &fd
246         };
247         bool found_artist = false, found_title = false, found_album = false,
248                 found_year = false, found_comment = false;
249
250         ret = write_all(fd, map, mapsize);
251         if (ret < 0)
252                 return ret;
253         lseek(fd, 0, SEEK_SET);
254
255         mp4ff = mp4ff_open_read_metaonly(&cb);
256         if (!mp4ff)
257                 return -E_MP4FF_OPEN;
258
259         ret = -E_MP4FF_META_READ;
260         rv = mp4ff_meta_get_num_items(mp4ff);
261         if (rv < 0)
262                 goto close;
263         metadata.count = rv;
264         PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
265
266         metadata.tags = para_malloc((metadata.count + 5) * sizeof(struct mp4ff_tag));
267         for (i = 0; i < metadata.count; i++) {
268                 struct mp4ff_tag *tag = metadata.tags + i;
269
270                 ret = -E_MP4FF_META_READ;
271                 if (!mp4ff_meta_get_by_index(mp4ff, i, &tag->item, &tag->value))
272                         goto free_tags;
273                 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
274                 if (!strcmp(tag->item, "artist"))
275                         replace_tag(tag, tags->artist, &found_artist);
276                 else if (!strcmp(tag->item, "title"))
277                         replace_tag(tag, tags->title, &found_title);
278                 else if (!strcmp(tag->item, "album"))
279                         replace_tag(tag, tags->album, &found_album);
280                 else if (!strcmp(tag->item, "date"))
281                         replace_tag(tag, tags->year, &found_year);
282                 else if (!strcmp(tag->item, "comment"))
283                         replace_tag(tag, tags->comment, &found_comment);
284         }
285         if (!found_artist)
286                 add_tag(&metadata, "artist", tags->artist);
287         if (!found_title)
288                 add_tag(&metadata, "title", tags->title);
289         if (!found_album)
290                 add_tag(&metadata, "album", tags->album);
291         if (!found_year)
292                 add_tag(&metadata, "date", tags->year);
293         if (!found_comment)
294                 add_tag(&metadata, "comment", tags->comment);
295         ret = -E_MP4FF_META_WRITE;
296         if (!mp4ff_meta_update(&cb, &metadata))
297                 goto free_tags;
298         ret = 1;
299 free_tags:
300         for (; i > 0; i--) {
301                 free(metadata.tags[i - 1].item);
302                 free(metadata.tags[i - 1].value);
303         }
304         free(metadata.tags);
305 close:
306         mp4ff_close(mp4ff);
307         return ret;
308 }
309
310 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
311
312 /**
313  * The audio format handler for the Advanced Audio Codec.
314  *
315  * This is only compiled in if the faad library is installed.
316  */
317 const struct audio_format_handler aac_afh = {
318         .get_file_info = aac_get_file_info,
319         .suffixes = aac_suffixes,
320         .rewrite_tags = aac_afh_rewrite_tags,
321         .open = aac_afh_open,
322         .get_chunk = aac_afh_get_chunk,
323         .close = aac_afh_close,
324 };