Merge branch 't/audiod_exit_cleanup'
[paraslash.git] / aac_afh.c
1 /*
2 * Copyright (C) 2006-2014 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
15 #include "para.h"
16 #include "error.h"
17 #include "afh.h"
18 #include "string.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(const unsigned char *buf1, const char *buf2)
46 {
47 const 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 void read_tags(unsigned char *buf, size_t buflen, struct afh_info *afhi)
95 {
96 unsigned char *p = buf;
97
98 while (p + 32 < buf + buflen) {
99 unsigned char *q, type1[5], type2[5];
100 uint64_t size1, size2;
101 int ret, ret2;
102
103 ret = read_atom_header(p, &size1, type1);
104 ret2 = read_atom_header(p + ret, &size2, type2);
105
106 if (size2 <= 16 || atom_cmp(type2, "data")) {
107 p += size1;
108 continue;
109 }
110 size2 -= 16;
111 q = p + ret + ret2 + 8;
112 if (q + size2 > buf + buflen)
113 break;
114 if (!atom_cmp(type1, "\xa9" "ART"))
115 afhi->tags.artist = get_tag(q, size2);
116 else if (!atom_cmp(type1, "\xa9" "alb"))
117 afhi->tags.album = get_tag(q, size2);
118 else if (!atom_cmp(type1, "\xa9" "nam"))
119 afhi->tags.title = get_tag(q, size2);
120 else if (!atom_cmp(type1, "\xa9" "cmt"))
121 afhi->tags.comment = get_tag(q, size2);
122 else if (!atom_cmp(type1, "\xa9" "day"))
123 afhi->tags.year = get_tag(q, size2);
124 p += size1;
125 }
126 }
127
128 static void read_meta(unsigned char *buf, size_t buflen, struct afh_info *afhi)
129 {
130 unsigned char *p = buf;
131
132 while (p + 4 < buf + buflen) {
133
134 if (p[0] != 'i' || p[1] != 'l' || p[2] != 's' || p[3] != 't') {
135 p++;
136 continue;
137 }
138 p += 4;
139 return read_tags(p, buflen - (p - buf), afhi);
140 }
141 }
142
143 static void aac_get_taginfo(unsigned char *buf, size_t buflen,
144 struct afh_info *afhi)
145 {
146 int i;
147 uint64_t subsize;
148 unsigned char type[5];
149
150 for (i = 0; i + 24 < buflen; i++) {
151 unsigned char *p = buf + i;
152 if (p[0] != 'm' || p[1] != 'e' || p[2] != 't' || p[3] != 'a')
153 continue;
154 PARA_INFO_LOG("found metadata at offset %d\n", i);
155 i += 8;
156 p = buf + i;
157 i += read_atom_header(p, &subsize, type);
158 p = buf + i;
159 return read_meta(p, buflen - i, afhi);
160 }
161 PARA_INFO_LOG("no meta data\n");
162 }
163
164 static ssize_t aac_compute_chunk_table(struct afh_info *afhi,
165 unsigned char *map, size_t numbytes)
166 {
167 int ret, i;
168 size_t sum = 0;
169 off_t skip;
170
171 ret = aac_find_stsz(map, numbytes, &skip);
172 if (ret < 0)
173 return ret;
174 afhi->chunks_total = ret;
175 PARA_DEBUG_LOG("sz table has %lu entries\n", afhi->chunks_total);
176 afhi->chunk_table = para_malloc((afhi->chunks_total + 1) * sizeof(size_t));
177 for (i = 1; i <= afhi->chunks_total; i++) {
178 if (skip + 4 > numbytes)
179 break;
180 sum += aac_read_int32(map + skip);
181 afhi->chunk_table[i] = sum;
182 skip += 4;
183 // if (i < 10 || i + 10 > afhi->chunks_total)
184 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
185 }
186 return skip;
187 }
188
189 static int aac_set_chunk_tv(struct afh_info *afhi,
190 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
191 {
192 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
193 struct timeval total;
194 long unsigned ms;
195
196 if (!mp4ASC->samplingFrequency)
197 return -E_MP4ASC;
198 ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
199 ms2tv(ms, &total);
200 tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
201 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
202 mp4ASC->samplingFrequency, ms / 1000,
203 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
204 if (ms < 1000)
205 return -E_MP4ASC;
206 *seconds = ms / 1000;
207 return 1;
208 }
209
210 /*
211 * Init m4a file and write some tech data to given pointers.
212 */
213 static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd,
214 struct afh_info *afhi)
215 {
216 int i;
217 size_t skip;
218 ssize_t ret;
219 unsigned long rate = 0, decoder_len;
220 unsigned char channels = 0;
221 mp4AudioSpecificConfig mp4ASC;
222 NeAACDecHandle handle = NULL;
223 unsigned char *umap = (unsigned char *) map;
224
225 ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
226 if (ret < 0)
227 goto out;
228 aac_get_taginfo(umap, numbytes, afhi);
229 handle = aac_open();
230 ret = -E_AAC_AFH_INIT;
231 if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
232 goto out;
233 if (!channels)
234 goto out;
235 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
236 ret = -E_MP4ASC;
237 if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
238 goto out;
239 if (!mp4ASC.samplingFrequency)
240 goto out;
241 ret = aac_compute_chunk_table(afhi, umap, numbytes);
242 if (ret < 0)
243 goto out;
244 skip = ret;
245 ret = aac_set_chunk_tv(afhi, &mp4ASC, &afhi->seconds_total);
246 if (ret < 0)
247 goto out;
248 ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
249 if (ret < 0)
250 goto out;
251 afhi->chunk_table[0] = ret;
252 for (i = 1; i<= afhi->chunks_total; i++)
253 afhi->chunk_table[i] += ret;
254 afhi->channels = channels;
255 afhi->frequency = rate;
256 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
257 ret += (channels * afhi->seconds_total * 500); /* avoid rounding error */
258 afhi->bitrate = ret / (channels * afhi->seconds_total * 1000);
259 ret = 1;
260 out:
261 if (handle)
262 NeAACDecClose(handle);
263 return ret;
264 }
265
266 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
267 /**
268 * the init function of the aac audio format handler
269 *
270 * \param afh pointer to the struct to initialize
271 */
272 void aac_afh_init(struct audio_format_handler *afh)
273 {
274 afh->get_file_info = aac_get_file_info,
275 afh->suffixes = aac_suffixes;
276 }