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