]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_afh.c
aac: Move declaration of mp4ff_set_sample_position() to mp4.h.
[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         mp4ff_t *mp4ff;
27         mp4AudioSpecificConfig masc;
28         mp4ff_callback_t 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(mp4ff_t *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         /* this function always returns zero */
120         mp4ff_set_sample_position(c->mp4ff, c->track, chunk_num);
121         offset = c->fpos;
122         ss = mp4ff_read_sample_getsize(c->mp4ff, c->track, chunk_num);
123         if (ss <= 0)
124                 return -E_MP4FF_BAD_SAMPLE;
125         assert(ss + offset <= c->mapsize);
126         *buf = c->map + offset;
127         *len = ss;
128         return 1;
129 }
130
131 static void _aac_afh_get_taginfo(const mp4ff_t *mp4ff, struct taginfo *tags)
132 {
133         mp4ff_meta_get_artist(mp4ff, &tags->artist);
134         mp4ff_meta_get_title(mp4ff, &tags->title);
135         mp4ff_meta_get_date(mp4ff, &tags->year);
136         mp4ff_meta_get_album(mp4ff, &tags->album);
137         mp4ff_meta_get_comment(mp4ff, &tags->comment);
138 }
139
140 /*
141  * Init m4a file and write some tech data to given pointers.
142  */
143 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
144                 struct afh_info *afhi)
145 {
146         int ret;
147         int32_t rv;
148         struct aac_afh_context *c;
149         int64_t tmp;
150         const char *buf;
151         uint32_t n, len;
152
153         ret = aac_afh_open(map, numbytes, (void **)&c);
154         if (ret < 0)
155                 return ret;
156
157         ret = -E_MP4FF_BAD_SAMPLERATE;
158         rv = mp4ff_get_sample_rate(c->mp4ff, c->track);
159         if (rv <= 0)
160                 goto close;
161         afhi->frequency = rv;
162
163         ret = -E_MP4FF_BAD_CHANNEL_COUNT;
164         rv = mp4ff_get_channel_count(c->mp4ff, c->track);
165         if (rv <= 0)
166                 goto close;
167         afhi->channels = rv;
168
169         ret = -E_MP4FF_BAD_SAMPLE_COUNT;
170         rv = mp4ff_num_samples(c->mp4ff, c->track);
171         if (rv <= 0)
172                 goto close;
173         afhi->chunks_total = rv;
174         afhi->max_chunk_size = 0;
175         for (n = 0; n < afhi->chunks_total; n++) {
176                 if (aac_afh_get_chunk(n, c, &buf, &len) < 0)
177                         break;
178                 afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len);
179         }
180
181         tmp = c->masc.sbr_present_flag == 1? 2048 : 1024;
182         afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency;
183         ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv);
184
185         if (aac_afh_get_chunk(0, c, &buf, &len) >= 0)
186                 numbytes -= buf - map;
187         afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000;
188         _aac_afh_get_taginfo(c->mp4ff, &afhi->tags);
189         ret = 1;
190 close:
191         aac_afh_close(c);
192         return ret;
193 }
194
195 static uint32_t aac_afh_meta_read_cb(void *user_data, void *dest, uint32_t want)
196 {
197         int fd = *(int *)user_data;
198         return read(fd, dest, want);
199 }
200
201 static uint32_t aac_afh_meta_seek_cb(void *user_data, uint64_t pos)
202 {
203         int fd = *(int *)user_data;
204         return lseek(fd, pos, SEEK_SET);
205 }
206
207 static uint32_t aac_afh_meta_write_cb(void *user_data, void *dest, uint32_t want)
208 {
209         int fd = *(int *)user_data;
210         return write(fd, dest, want);
211 }
212
213 static uint32_t aac_afh_meta_truncate_cb(void *user_data)
214 {
215         int fd = *(int *)user_data;
216         off_t offset = lseek(fd, 0, SEEK_CUR);
217         return ftruncate(fd, offset);
218 }
219
220 static void replace_tag(mp4ff_tag_t *tag, const char *new_val, bool *found)
221 {
222         free(tag->value);
223         tag->value = para_strdup(new_val);
224         *found = true;
225 }
226
227 static void add_tag(mp4ff_metadata_t *md, const char *item, const char *value)
228 {
229         md->tags[md->count].item = para_strdup(item);
230         md->tags[md->count].value = para_strdup(value);
231         md->count++;
232 }
233
234 static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
235                 struct taginfo *tags, int fd, __a_unused const char *filename)
236 {
237         int ret, i;
238         int32_t rv;
239         mp4ff_metadata_t metadata;
240         mp4ff_t *mp4ff;
241         mp4ff_callback_t cb = {
242                 .read = aac_afh_meta_read_cb,
243                 .seek = aac_afh_meta_seek_cb,
244                 .write = aac_afh_meta_write_cb,
245                 .truncate = aac_afh_meta_truncate_cb,
246                 .user_data = &fd
247         };
248         bool found_artist = false, found_title = false, found_album = false,
249                 found_year = false, found_comment = false;
250
251         ret = write_all(fd, map, mapsize);
252         if (ret < 0)
253                 return ret;
254         lseek(fd, 0, SEEK_SET);
255
256         mp4ff = mp4ff_open_read_metaonly(&cb);
257         if (!mp4ff)
258                 return -E_MP4FF_OPEN;
259
260         ret = -E_MP4FF_META_READ;
261         rv = mp4ff_meta_get_num_items(mp4ff);
262         if (rv < 0)
263                 goto close;
264         metadata.count = rv;
265         PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
266
267         metadata.tags = para_malloc((metadata.count + 5) * sizeof(mp4ff_tag_t));
268         for (i = 0; i < metadata.count; i++) {
269                 mp4ff_tag_t *tag = metadata.tags + i;
270
271                 ret = -E_MP4FF_META_READ;
272                 if (!mp4ff_meta_get_by_index(mp4ff, i, &tag->item, &tag->value))
273                         goto free_tags;
274                 PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value);
275                 if (!strcmp(tag->item, "artist"))
276                         replace_tag(tag, tags->artist, &found_artist);
277                 else if (!strcmp(tag->item, "title"))
278                         replace_tag(tag, tags->title, &found_title);
279                 else if (!strcmp(tag->item, "album"))
280                         replace_tag(tag, tags->album, &found_album);
281                 else if (!strcmp(tag->item, "date"))
282                         replace_tag(tag, tags->year, &found_year);
283                 else if (!strcmp(tag->item, "comment"))
284                         replace_tag(tag, tags->comment, &found_comment);
285         }
286         if (!found_artist)
287                 add_tag(&metadata, "artist", tags->artist);
288         if (!found_title)
289                 add_tag(&metadata, "title", tags->title);
290         if (!found_album)
291                 add_tag(&metadata, "album", tags->album);
292         if (!found_year)
293                 add_tag(&metadata, "date", tags->year);
294         if (!found_comment)
295                 add_tag(&metadata, "comment", tags->comment);
296         ret = -E_MP4FF_META_WRITE;
297         if (!mp4ff_meta_update(&cb, &metadata))
298                 goto free_tags;
299         ret = 1;
300 free_tags:
301         for (; i > 0; i--) {
302                 free(metadata.tags[i - 1].item);
303                 free(metadata.tags[i - 1].value);
304         }
305         free(metadata.tags);
306 close:
307         mp4ff_close(mp4ff);
308         return ret;
309 }
310
311 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
312
313 /**
314  * The audio format handler for the Advanced Audio Codec.
315  *
316  * This is only compiled in if the faad library is installed.
317  */
318 const struct audio_format_handler aac_afh = {
319         .get_file_info = aac_get_file_info,
320         .suffixes = aac_suffixes,
321         .rewrite_tags = aac_afh_rewrite_tags,
322         .open = aac_afh_open,
323         .get_chunk = aac_afh_get_chunk,
324         .close = aac_afh_close,
325 };