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