Fix para_strerror() and use osl() wrapper for osl library calls.
[paraslash.git] / aac_afh.c
1 /*
2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
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 <osl.h>
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "afh.h"
18 #include "afs.h"
19 #include "server.h"
20 #include "aac.h"
21
22 static int aac_find_stsz(unsigned char *buf, size_t buflen, off_t *skip)
23 {
24 int i;
25
26 for (i = 0; i + 16 < buflen; i++) {
27 unsigned char *p = buf + i;
28 unsigned sample_count, sample_size;
29
30 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
31 continue;
32 PARA_DEBUG_LOG("found stsz@%d\n", i);
33 i += 8;
34 sample_size = aac_read_int32(buf + i);
35 PARA_DEBUG_LOG("sample size: %d\n", sample_size);
36 i += 4;
37 sample_count = aac_read_int32(buf + i);
38 i += 4;
39 PARA_DEBUG_LOG("sample count: %d\n", sample_count);
40 *skip = i;
41 return sample_count;
42 }
43 return -E_STSZ;
44 }
45
46 static int atom_cmp(unsigned char *buf1, char *buf2)
47 {
48 unsigned char *b2 = (unsigned char *)buf2;
49
50 if (buf1[0] != b2[0])
51 return 1;
52 if (buf1[1] != b2[1])
53 return 1;
54 if (buf1[2] != b2[2])
55 return 1;
56 if (buf1[3] != b2[3])
57 return 1;
58 return 0;
59 }
60
61 static int read_atom_header(unsigned char *buf, uint64_t *subsize, unsigned char type[5])
62 {
63 int i;
64 uint64_t size = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
65
66 memcpy(type, buf + 4, 4);
67 type[4] = '\0';
68
69 PARA_DEBUG_LOG("size: %llu, type: %s\n", (long long unsigned)size, type);
70 if (size != 1) {
71 *subsize = size;
72 return 8;
73 }
74 buf += 4;
75 size = 0;
76 for (i = 0; i < 8; i++)
77 size |= ((uint64_t)buf[i]) << ((7 - i) * 8);
78 *subsize = size;
79 return 16;
80 }
81
82 static char *get_tag(unsigned char *p, int size)
83 {
84 char *buf;
85
86 assert(size > 0);
87 buf = para_malloc(size + 1);
88
89 memcpy(buf, p, size);
90 buf[size] = '\0';
91 PARA_DEBUG_LOG("size: %d: %s\n", size, buf);
92 return buf;
93 }
94
95 static char *read_tags(unsigned char *buf, size_t buflen)
96 {
97 unsigned char *p = buf;
98 char *title = NULL, *artist = NULL, *album = NULL, *year = NULL,
99 *comment = NULL, *result;
100
101 while (p + 32 < buf + buflen) {
102 unsigned char *q, type1[5], type2[5];
103 uint64_t size1, size2;
104 int ret, ret2;
105
106 ret = read_atom_header(p, &size1, type1);
107 ret2 = read_atom_header(p + ret, &size2, type2);
108
109 if (size2 <= 16 || atom_cmp(type2, "data")) {
110 p += size1;
111 continue;
112 }
113 size2 -= 16;
114 q = p + ret + ret2 + 8;
115 if (q + size2 > buf + buflen)
116 break;
117 if (!atom_cmp(type1, "©ART"))
118 artist = get_tag(q, size2);
119 else if (!atom_cmp(type1, "©alb"))
120 album = get_tag(q, size2);
121 else if (!atom_cmp(type1, "©nam"))
122 title = get_tag(q, size2);
123 else if (!atom_cmp(type1, "©cmt"))
124 comment = get_tag(q, size2);
125 else if (!atom_cmp(type1, "©day"))
126 year = get_tag(q, size2);
127 p += size1;
128 }
129 result = make_taginfo(title, artist, album, year, comment);
130 free(title);
131 free(artist);
132 free(album);
133 free(year);
134 free(comment);
135 return result;
136 }
137
138 static char *read_meta(unsigned char *buf, size_t buflen)
139 {
140 unsigned char *p = buf;
141
142 while (p + 4 < buf + buflen) {
143
144 if (p[0] != 'i' || p[1] != 'l' || p[2] != 's' || p[3] != 't') {
145 p++;
146 continue;
147 }
148 p += 4;
149 return read_tags(p, buflen - (p - buf));
150 }
151 return make_taginfo(NULL, NULL, NULL, NULL, NULL);
152 }
153
154 static char *aac_get_taginfo(unsigned char *buf, size_t buflen)
155 {
156 int i;
157 uint64_t subsize;
158 unsigned char type[5];
159
160 for (i = 0; i + 24 < buflen; i++) {
161 unsigned char *p = buf + i;
162 if (p[0] != 'm' || p[1] != 'e' || p[2] != 't' || p[3] != 'a')
163 continue;
164 PARA_INFO_LOG("found metadata at offset %d\n", i);
165 i += 8;
166 p = buf + i;
167 i += read_atom_header(p, &subsize, type);
168 p = buf + i;
169 return read_meta(p, buflen - i);
170 }
171 PARA_INFO_LOG("no meta data\n");
172 return make_taginfo(NULL, NULL, NULL, NULL, NULL);
173 }
174
175 static ssize_t aac_compute_chunk_table(struct afh_info *afhi,
176 unsigned char *map, size_t numbytes)
177 {
178 int ret, i;
179 size_t sum = 0;
180 off_t skip;
181
182 ret = aac_find_stsz(map, numbytes, &skip);
183 if (ret < 0)
184 return ret;
185 afhi->chunks_total = ret;
186 PARA_DEBUG_LOG("sz table has %lu entries\n", afhi->chunks_total);
187 afhi->chunk_table = para_malloc((afhi->chunks_total + 1) * sizeof(size_t));
188 for (i = 1; i <= afhi->chunks_total; i++) {
189 if (skip + 4 > numbytes)
190 break;
191 sum += aac_read_int32(map + skip);
192 afhi->chunk_table[i] = sum;
193 skip += 4;
194 // if (i < 10 || i + 10 > afhi->chunks_total)
195 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
196 }
197 return skip;
198 }
199
200 static int aac_set_chunk_tv(struct afh_info *afhi,
201 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
202 {
203 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
204 struct timeval total;
205 long unsigned ms = 1000.0 * afhi->chunks_total * tmp
206 / mp4ASC->samplingFrequency;
207
208 if (!mp4ASC->samplingFrequency)
209 return -E_MP4ASC;
210 ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
211 ms2tv(ms, &total);
212 tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
213 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
214 mp4ASC->samplingFrequency, ms / 1000,
215 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
216 if (ms < 1000)
217 return -E_MP4ASC;
218 *seconds = ms / 1000;
219 return 1;
220 }
221
222 /*
223 * Init m4a file and write some tech data to given pointers.
224 */
225 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
226 struct afh_info *afhi)
227 {
228 int i;
229 size_t skip;
230 ssize_t ret;
231 unsigned long rate = 0, decoder_len;
232 unsigned char channels = 0;
233 mp4AudioSpecificConfig mp4ASC;
234 NeAACDecHandle handle = NULL;
235 unsigned char *umap = (unsigned char *) map;
236 char *taginfo;
237
238 ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
239 if (ret < 0)
240 goto out;
241 taginfo = aac_get_taginfo(umap, numbytes);
242 handle = aac_open();
243 ret = -E_AAC_AFH_INIT;
244 if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
245 goto out;
246 if (!channels)
247 goto out;
248 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
249 ret = -E_MP4ASC;
250 if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
251 goto out;
252 if (!mp4ASC.samplingFrequency)
253 goto out;
254 ret = aac_compute_chunk_table(afhi, umap, numbytes);
255 if (ret < 0)
256 goto out;
257 skip = ret;
258 ret = aac_set_chunk_tv(afhi, &mp4ASC, &afhi->seconds_total);
259 if (ret < 0)
260 goto out;
261 ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
262 if (ret < 0)
263 goto out;
264 afhi->chunk_table[0] = ret;
265 for (i = 1; i<= afhi->chunks_total; i++)
266 afhi->chunk_table[i] += ret;
267 afhi->channels = channels;
268 afhi->frequency = rate;
269 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
270 ret += (channels * afhi->seconds_total * 500); /* avoid rounding error */
271 afhi->bitrate = ret / (channels * afhi->seconds_total * 1000);
272 afhi->info_string = make_message("%s:\n%s",
273 status_item_list[SI_AUDIO_FILE_INFO],
274 taginfo);
275 free(taginfo);
276 tv_scale(20, &afhi->chunk_tv, &afhi->eof_tv);
277 ret = 1;
278 out:
279 if (handle)
280 NeAACDecClose(handle);
281 return ret;
282 }
283
284 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
285 /**
286 * the init function of the aac audio format handler
287 *
288 * \param afh pointer to the struct to initialize
289 */
290 void aac_afh_init(struct audio_format_handler *afh)
291 {
292 afh->get_file_info = aac_get_file_info,
293 afh->suffixes = aac_suffixes;
294 }