aac_afh.c: Remove duplicate check for samplingFrequency.
[paraslash.git] / aac_afh.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 /*
7  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
8  * Ahead Software AG
9  */
10
11 /** \file aac_afh.c para_server's aac audio format handler. */
12
13 #include <regex.h>
14 #include <mp4v2/mp4v2.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "portable_io.h"
19 #include "afh.h"
20 #include "string.h"
21 #include "aac.h"
22 #include "fd.h"
23
24 static int aac_find_stsz(char *buf, size_t buflen, off_t *skip)
25 {
26         int i;
27
28         for (i = 0; i + 16 < buflen; i++) {
29                 char *p = buf + i;
30                 unsigned sample_count, sample_size;
31
32                 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
33                         continue;
34                 PARA_DEBUG_LOG("found stsz@%d\n", i);
35                 i += 8;
36                 sample_size = read_u32_be(buf + i);
37                 PARA_DEBUG_LOG("sample size: %u\n", sample_size);
38                 i += 4;
39                 sample_count = read_u32_be(buf + i);
40                 i += 4;
41                 PARA_DEBUG_LOG("sample count: %u\n", sample_count);
42                 *skip = i;
43                 return sample_count;
44         }
45         return -E_STSZ;
46 }
47
48 static int atom_cmp(const char *buf1, const char *buf2)
49 {
50         return memcmp(buf1, buf2, 4)? 1 : 0;
51 }
52
53 static int read_atom_header(char *buf, uint64_t *subsize, char type[5])
54 {
55         uint64_t size = read_u32_be(buf);
56
57         memcpy(type, buf + 4, 4);
58         type[4] = '\0';
59
60         PARA_DEBUG_LOG("size: %llu, type: %s\n", (long long unsigned)size, type);
61         if (size != 1) {
62                 *subsize = size;
63                 return 8;
64         }
65         buf += 4;
66         size = 0;
67         size = read_u64_be(buf);
68         *subsize = size;
69         return 16;
70 }
71
72 static char *get_tag(char *p, int size)
73 {
74         char *buf;
75
76         assert(size > 0);
77         buf = para_malloc(size + 1);
78
79         memcpy(buf, p, size);
80         buf[size] = '\0';
81         PARA_DEBUG_LOG("size: %d: %s\n", size, buf);
82         return buf;
83 }
84
85 static void read_tags(char *buf, size_t buflen, struct afh_info *afhi)
86 {
87         char *p = buf;
88
89         while (p + 32 < buf + buflen) {
90                 char *q, type1[5], type2[5];
91                 uint64_t size1, size2;
92                 int ret, ret2;
93
94                 ret = read_atom_header(p, &size1, type1);
95                 ret2 = read_atom_header(p + ret, &size2, type2);
96
97                 if (size2 <= 16 || atom_cmp(type2, "data")) {
98                         p += size1;
99                         continue;
100                 }
101                 size2 -= 16;
102                 q = p + ret + ret2 + 8;
103                 if (q + size2 > buf + buflen)
104                         break;
105                 if (!atom_cmp(type1, "\xa9" "ART"))
106                         afhi->tags.artist = get_tag(q, size2);
107                 else if (!atom_cmp(type1, "\xa9" "alb"))
108                         afhi->tags.album = get_tag(q, size2);
109                 else if (!atom_cmp(type1, "\xa9" "nam"))
110                         afhi->tags.title = get_tag(q, size2);
111                 else if (!atom_cmp(type1, "\xa9" "cmt"))
112                         afhi->tags.comment = get_tag(q, size2);
113                 else if (!atom_cmp(type1, "\xa9" "day"))
114                         afhi->tags.year = get_tag(q, size2);
115                 p += size1;
116         }
117 }
118
119 static void read_meta(char *buf, size_t buflen, struct afh_info *afhi)
120 {
121         char *p = buf;
122
123         while (p + 4 < buf + buflen) {
124
125                 if (p[0] != 'i' || p[1] != 'l' || p[2] != 's' || p[3] != 't') {
126                         p++;
127                         continue;
128                 }
129                 p += 4;
130                 return read_tags(p, buflen - (p - buf), afhi);
131         }
132 }
133
134 static void aac_get_taginfo(char *buf, size_t buflen, struct afh_info *afhi)
135 {
136         int i;
137         uint64_t subsize;
138         char type[5];
139
140         for (i = 0; i + 24 < buflen; i++) {
141                 char *p = buf + i;
142                 if (p[0] != 'm' || p[1] != 'e' || p[2] != 't' || p[3] != 'a')
143                         continue;
144                 PARA_INFO_LOG("found metadata at offset %d\n", i);
145                 i += 8;
146                 p = buf + i;
147                 i += read_atom_header(p, &subsize, type);
148                 p = buf + i;
149                 return read_meta(p, buflen - i, afhi);
150         }
151         PARA_INFO_LOG("no meta data\n");
152 }
153
154 static ssize_t aac_compute_chunk_table(struct afh_info *afhi,
155                 char *map, size_t numbytes)
156 {
157         int ret, i;
158         size_t sum = 0;
159         off_t skip;
160
161         ret = aac_find_stsz(map, numbytes, &skip);
162         if (ret < 0)
163                 return ret;
164         afhi->chunks_total = ret;
165         PARA_DEBUG_LOG("sz table has %" PRIu32 " entries\n", afhi->chunks_total);
166         afhi->chunk_table = para_malloc((afhi->chunks_total + 1) * sizeof(size_t));
167         for (i = 1; i <= afhi->chunks_total; i++) {
168                 if (skip + 4 > numbytes)
169                         break;
170                 sum += read_u32_be(map + skip);
171                 afhi->chunk_table[i] = sum;
172                 skip += 4;
173 //              if (i < 10 || i + 10 > afhi->chunks_total)
174 //                      PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
175         }
176         return skip;
177 }
178
179 static int aac_set_chunk_tv(struct afh_info *afhi,
180                 mp4AudioSpecificConfig *mp4ASC, uint32_t *seconds)
181 {
182         float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
183         struct timeval total;
184         long unsigned ms;
185
186         ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
187         ms2tv(ms, &total);
188         tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
189         PARA_INFO_LOG("%luHz, %lus (%" PRIu32 " x %lums)\n",
190                 mp4ASC->samplingFrequency, ms / 1000,
191                 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
192         if (ms < 1000)
193                 return -E_MP4ASC;
194         *seconds = ms / 1000;
195         return 1;
196 }
197
198 /*
199  * Init m4a file and write some tech data to given pointers.
200  */
201 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
202                 struct afh_info *afhi)
203 {
204         int i;
205         size_t skip;
206         ssize_t ret;
207         unsigned long rate = 0, decoder_len;
208         unsigned char channels = 0;
209         mp4AudioSpecificConfig mp4ASC;
210         NeAACDecHandle handle = NULL;
211
212         ret = aac_find_esds(map, numbytes, &skip, &decoder_len);
213         if (ret < 0)
214                 goto out;
215         aac_get_taginfo(map, numbytes, afhi);
216         handle = aac_open();
217         ret = -E_AAC_AFH_INIT;
218         if (NeAACDecInit(handle, (unsigned char *)map + skip, decoder_len,
219                         &rate, &channels))
220                 goto out;
221         if (!channels)
222                 goto out;
223         PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
224         ret = -E_MP4ASC;
225         if (NeAACDecAudioSpecificConfig((unsigned char *)map + skip,
226                         numbytes - skip, &mp4ASC))
227                 goto out;
228         if (!mp4ASC.samplingFrequency)
229                 goto out;
230         ret = aac_compute_chunk_table(afhi, map, numbytes);
231         if (ret < 0)
232                 goto out;
233         skip = ret;
234         ret = aac_set_chunk_tv(afhi, &mp4ASC, &afhi->seconds_total);
235         if (ret < 0)
236                 goto out;
237         ret = aac_find_entry_point(map + skip, numbytes - skip, &skip);
238         if (ret < 0)
239                 goto out;
240         afhi->chunk_table[0] = ret;
241         for (i = 1; i<= afhi->chunks_total; i++)
242                 afhi->chunk_table[i] += ret;
243         afhi->channels = channels;
244         afhi->frequency = rate;
245         ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
246         ret += (channels * afhi->seconds_total * 500); /* avoid rounding error */
247         afhi->bitrate = ret / (channels * afhi->seconds_total * 1000);
248         ret = 1;
249 out:
250         if (handle)
251                 NeAACDecClose(handle);
252         return ret;
253 }
254
255 static int aac_rewrite_tags(const char *map, size_t mapsize,
256                 struct taginfo *tags, int fd, const char *filename)
257 {
258         MP4FileHandle h;
259         const MP4Tags *mdata;
260         int ret = write_all(fd, map, mapsize);
261
262         if (ret < 0)
263                 return ret;
264         lseek(fd, 0, SEEK_SET);
265         h = MP4Modify(filename, 0);
266         if (!h) {
267                 PARA_ERROR_LOG("MP4Modify() failed, fd = %d\n", fd);
268                 return -E_MP4V2;
269         }
270         mdata = MP4TagsAlloc();
271         assert(mdata);
272         if (!MP4TagsFetch(mdata, h)) {
273                 PARA_ERROR_LOG("MP4Tags_Fetch() failed\n");
274                 ret = -E_MP4V2;
275                 goto close;
276         }
277
278         if (!MP4TagsSetAlbum(mdata, tags->album)) {
279                 PARA_ERROR_LOG("Could not set album\n");
280                 ret = -E_MP4V2;
281                 goto tags_free;
282         }
283         if (!MP4TagsSetArtist(mdata, tags->artist)) {
284                 PARA_ERROR_LOG("Could not set album\n");
285                 ret = -E_MP4V2;
286                 goto tags_free;
287         }
288         if (!MP4TagsSetComments(mdata, tags->comment)) {
289                 PARA_ERROR_LOG("Could not set comment\n");
290                 ret = -E_MP4V2;
291                 goto tags_free;
292         }
293         if (!MP4TagsSetName(mdata, tags->title)) {
294                 PARA_ERROR_LOG("Could not set title\n");
295                 ret = -E_MP4V2;
296                 goto tags_free;
297         }
298         if (!MP4TagsSetReleaseDate(mdata, tags->year)) {
299                 PARA_ERROR_LOG("Could not set release date\n");
300                 ret = -E_MP4V2;
301                 goto tags_free;
302         }
303
304         if (!MP4TagsStore(mdata, h)) {
305                 PARA_ERROR_LOG("Could not store tags\n");
306                 ret = -E_MP4V2;
307                 goto tags_free;
308         }
309         ret = 1;
310 tags_free:
311         MP4TagsFree(mdata);
312 close:
313         MP4Close(h, 0);
314         return ret;
315 }
316
317 static const char * const aac_suffixes[] = {"m4a", "mp4", NULL};
318 /**
319  * the init function of the aac audio format handler
320  *
321  * \param afh pointer to the struct to initialize
322  */
323 void aac_afh_init(struct audio_format_handler *afh)
324 {
325         afh->get_file_info = aac_get_file_info,
326         afh->suffixes = aac_suffixes;
327         afh->rewrite_tags = aac_rewrite_tags;
328 }