93d3bec8157702b9f769178d4d9c4ed5e81ecd2e
[paraslash.git] / aac_afh.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18 /*
19 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
20 * Ahead Software AG
21 */
22
23 /** \file aac_afh.c para_server's aac audio format handler */
24
25 #include "server.cmdline.h"
26 #include "server.h"
27 #include "afs.h"
28 #include "error.h"
29 #include "string.h"
30 #include "aac.h"
31
32 /* must be big enough to hold header */
33 #define DEFAULT_INBUF_SIZE 65536
34
35 static FILE *infile;
36 static int inbuf_size;
37 static unsigned char *inbuf;
38 static unsigned inbuf_len;
39 struct audio_format *af;
40
41 static unsigned num_chunks, entry;
42
43 static size_t *chunk_table;
44 NeAACDecHandle handle;
45
46
47 static void aac_close_audio_file(void)
48 {
49 }
50
51 static int aac_find_stsz(unsigned char *buf, unsigned buflen, unsigned *skip)
52 {
53 int i;
54
55 for (i = 0; i + 16 < buflen; i++) {
56 unsigned char *p = buf + i;
57 unsigned sample_count, sample_size;
58
59 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
60 continue;
61 PARA_INFO_LOG("found stsz@%d\n", i);
62 i += 8;
63 sample_size = aac_read_int32(buf + i);
64 PARA_INFO_LOG("sample size: %d\n", sample_size);
65 i += 4;
66 sample_count = aac_read_int32(buf + i);
67 i += 4;
68 PARA_INFO_LOG("sample count: %d\n", sample_count);
69 *skip = i;
70 return sample_count;
71 }
72 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen);
73 return -E_STSZ;
74 }
75
76 static int read_chunk_table(unsigned skip)
77 {
78 int ret, i;
79 long unsigned sum = 0;
80
81 for (;;) {
82 ret = aac_find_stsz(inbuf, inbuf_len, &skip);
83 if (ret >= 0)
84 break;
85 ret = read(fileno(infile), inbuf, inbuf_size);
86 if (ret <= 0)
87 return -E_AAC_READ;
88 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
89 }
90 num_chunks = ret;
91 PARA_INFO_LOG("sz table has %d entries\n", num_chunks);
92 free(chunk_table);
93 chunk_table = para_malloc(num_chunks * sizeof(size_t));
94 for (i = 0; i < num_chunks; i++) {
95 if (skip + 4 > inbuf_len) {
96 skip = inbuf_len - skip;
97 memmove(inbuf, inbuf + inbuf_len - skip, skip);
98 ret = read(fileno(infile), inbuf + skip, inbuf_size - skip);
99 if (ret <= 0)
100 return -E_AAC_READ;
101 inbuf_len = ret + skip;
102 skip = 0;
103 PARA_INFO_LOG("next buffer: %d bytes\n", inbuf_len);
104 }
105 sum += aac_read_int32(inbuf + skip);
106 chunk_table[i] = sum;
107 skip += 4;
108 if (i < 10 || i > num_chunks - 10)
109 PARA_DEBUG_LOG("offset #%d: %d\n", i, chunk_table[i]);
110 }
111 return 1;
112
113 }
114
115 /*
116 * Init m4a file and write some tech data to given pointers.
117 */
118 static int aac_get_file_info(FILE *file, char *info_str, long unsigned *frames,
119 int *seconds)
120 {
121 int ret, skip, decoder_len;
122 unsigned long rate = 0;
123 unsigned char channels = 0;
124 mp4AudioSpecificConfig mp4ASC;
125
126 free(inbuf);
127 inbuf_size = DEFAULT_INBUF_SIZE;
128 inbuf = para_malloc(inbuf_size);
129 infile = file;
130
131 PARA_INFO_LOG("file: %p\n", file);
132 ret = read(fileno(infile), inbuf, inbuf_size);
133 PARA_INFO_LOG("read %d bytes\n", ret);
134 if (ret <= 0)
135 return -E_AAC_READ;
136 PARA_INFO_LOG("checking aac %d bytes\n", ret);
137 inbuf_len = ret;
138 ret = aac_find_esds(inbuf, inbuf_len, &skip);
139 if (ret < 0)
140 return ret;
141 decoder_len = ret;
142 handle = aac_open();
143 ret = NeAACDecInit(handle, inbuf + skip,
144 decoder_len, &rate, &channels);
145 if (ret < 0)
146 return -E_AACDEC_INIT;
147 skip += ret;
148 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
149 ret = NeAACDecAudioSpecificConfig(inbuf + skip, inbuf_len - skip, &mp4ASC);
150 if (ret >= 0) {
151 PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
152 mp4ASC.samplingFrequency);
153 } else
154 PARA_WARNING_LOG("no mp4ASC %s\n", "");
155
156 ret = read_chunk_table(skip);
157 if (ret < 0)
158 return ret;
159 *frames = num_chunks;
160 for (;;) {
161 ret = aac_find_entry_point(inbuf, inbuf_len, &skip);
162 if (ret >= 0)
163 break;
164 ret = read(fileno(infile), inbuf, inbuf_size);
165 if (ret <= 0)
166 return -E_AAC_READ;
167 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
168 }
169 entry = ret;
170 PARA_INFO_LOG("offset table has %d entries\, entry: %zd\n", num_chunks,
171 entry);
172 #if 1
173 sprintf(info_str, "audio_file_info1:%d x %lums\n"
174 "audio_file_info2:\n"
175 "audio_file_info3:\n",
176 num_chunks,
177 tv2ms(&af->chunk_tv));
178 #endif
179 #if 1
180 {
181 struct timeval total_tv;
182 tv_scale(num_chunks, &af->chunk_tv, &total_tv);
183 *seconds = tv2ms(&total_tv) / 1000;
184 PARA_INFO_LOG("%d seconds, %d chunks\n", *seconds, num_chunks);
185 }
186 #endif
187 return 1;
188 }
189
190 /*
191 * Simple stream reposition routine
192 */
193 static int aac_reposition_stream(long unsigned request)
194 {
195 return 1;
196 // return -E_AAC_REPOS;
197 }
198
199 static __must_check int para_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
200 {
201 size_t res = fread(ptr, size, nmemb, stream);
202 if (res == nmemb)
203 return size * nmemb;
204 if (feof(stream))
205 return 0;
206 return -E_FREAD;
207 }
208
209 static char *aac_read_chunk(long unsigned current_chunk, ssize_t *len)
210 {
211 int ret;
212 size_t pos;
213
214 *len = 0;
215 if (current_chunk >= num_chunks)
216 return NULL;
217 if (!current_chunk) {
218 *len = entry;
219 pos = 0;
220 } else if (current_chunk == 1) {
221 *len = chunk_table[0];
222 pos = entry;
223 } else {
224 *len = chunk_table[current_chunk - 1] - chunk_table[current_chunk - 2];
225 pos = entry + chunk_table[current_chunk - 2];
226 }
227 if (inbuf_size < *len) {
228 inbuf = para_realloc(inbuf, *len);
229 inbuf_size = *len;
230 }
231 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
232 // pos, *len);
233 ret = fseek(infile, pos, SEEK_SET);
234 if (ret < 0)
235 return NULL;
236 ret = para_fread(inbuf, *len, 1, infile);
237 if (ret != *len)
238 return NULL;
239 // PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
240 // (long unsigned) inbuf[4]);
241 return (char *)inbuf;
242 }
243
244 void aac_afh_init(void *p)
245 {
246 af = p;
247 af->reposition_stream = aac_reposition_stream;
248 af->get_file_info = aac_get_file_info,
249 af->read_chunk = aac_read_chunk;
250 af->close_audio_file = aac_close_audio_file;
251 af->get_header_info = NULL;
252 af->chunk_tv.tv_sec = 0;
253 af->chunk_tv.tv_usec = 23120;
254 tv_scale(3, &af->chunk_tv, &af->eof_tv);
255 }