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