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