55efeefa1d7dc1b067e23da06294a7fb222e9304
[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 #include "fd.h"
32
33 /* must be big enough to hold header */
34 #define DEFAULT_INBUF_SIZE 65536
35
36 static FILE *infile;
37 static int inbuf_size;
38 static unsigned char *inbuf;
39 static size_t inbuf_len;
40 struct audio_format_handler *af;
41 static size_t num_chunks;
42 static size_t entry;
43
44 static size_t *chunk_table;
45 NeAACDecHandle handle;
46
47 static void aac_close_audio_file(void)
48 {
49 if (!infile)
50 return;
51 fclose(infile);
52 infile = NULL;
53 }
54
55 static int aac_find_stsz(unsigned char *buf, unsigned buflen, size_t *skip)
56 {
57 int i;
58
59 for (i = 0; i + 16 < buflen; i++) {
60 unsigned char *p = buf + i;
61 unsigned sample_count, sample_size;
62
63 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
64 continue;
65 PARA_INFO_LOG("found stsz@%d\n", i);
66 i += 8;
67 sample_size = aac_read_int32(buf + i);
68 PARA_INFO_LOG("sample size: %d\n", sample_size);
69 i += 4;
70 sample_count = aac_read_int32(buf + i);
71 i += 4;
72 PARA_INFO_LOG("sample count: %d\n", sample_count);
73 *skip = i;
74 return sample_count;
75 }
76 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen);
77 return -E_STSZ;
78 }
79
80 static int read_chunk_table(size_t skip)
81 {
82 int ret, i;
83 size_t sum = 0;
84
85 for (;;) {
86 ret = aac_find_stsz(inbuf, inbuf_len, &skip);
87 if (ret >= 0)
88 break;
89 ret = read(fileno(infile), inbuf, inbuf_size);
90 if (ret <= 0)
91 return -E_AAC_READ;
92 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
93 }
94 num_chunks = ret;
95 PARA_INFO_LOG("sz table has %zu entries\n", num_chunks);
96 free(chunk_table);
97 chunk_table = para_malloc(num_chunks * sizeof(size_t));
98 for (i = 0; i < num_chunks; i++) {
99 if (skip + 4 > inbuf_len) {
100 skip = inbuf_len - skip;
101 memmove(inbuf, inbuf + inbuf_len - skip, skip);
102 ret = read(fileno(infile), inbuf + skip, inbuf_size - skip);
103 if (ret <= 0)
104 return -E_AAC_READ;
105 inbuf_len = ret + skip;
106 skip = 0;
107 PARA_INFO_LOG("next buffer: %zu bytes\n", inbuf_len);
108 }
109 sum += aac_read_int32(inbuf + skip);
110 chunk_table[i] = sum;
111 skip += 4;
112 if (i < 10 || i > num_chunks - 10)
113 PARA_DEBUG_LOG("offset #%d: %zu\n", i, chunk_table[i]);
114 }
115 return 1;
116
117 }
118
119 /*
120 * Init m4a file and write some tech data to given pointers.
121 */
122 static int aac_get_file_info(FILE *file, char *info_str, long unsigned *frames,
123 int *seconds)
124 {
125 int ret, decoder_len;
126 size_t skip;
127 unsigned long rate = 0;
128 unsigned char channels = 0;
129 mp4AudioSpecificConfig mp4ASC;
130
131 free(inbuf);
132 inbuf_size = DEFAULT_INBUF_SIZE;
133 inbuf = para_malloc(inbuf_size);
134 infile = file;
135
136 PARA_INFO_LOG("file: %p\n", file);
137 ret = read(fileno(infile), inbuf, inbuf_size);
138 PARA_INFO_LOG("read %d bytes\n", ret);
139 if (ret <= 0)
140 return -E_AAC_READ;
141 PARA_INFO_LOG("checking aac %d bytes\n", ret);
142 inbuf_len = ret;
143 ret = aac_find_esds(inbuf, inbuf_len, &skip);
144 if (ret < 0)
145 return ret;
146 decoder_len = ret;
147 handle = aac_open();
148 ret = NeAACDecInit(handle, inbuf + skip,
149 decoder_len, &rate, &channels);
150 if (ret < 0)
151 return -E_AACDEC_INIT;
152 skip += ret;
153 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
154 ret = NeAACDecAudioSpecificConfig(inbuf + skip, inbuf_len - skip, &mp4ASC);
155 if (ret >= 0) {
156 PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
157 mp4ASC.samplingFrequency);
158 } else
159 PARA_WARNING_LOG("no mp4ASC %s\n", "");
160
161 ret = read_chunk_table(skip);
162 if (ret < 0)
163 return ret;
164 *frames = num_chunks;
165 for (;;) {
166 ret = aac_find_entry_point(inbuf, inbuf_len, &skip);
167 if (ret >= 0)
168 break;
169 ret = read(fileno(infile), inbuf, inbuf_size);
170 if (ret <= 0)
171 return -E_AAC_READ;
172 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
173 }
174 entry = ret;
175 PARA_INFO_LOG("offset table has %zu entries, entry: %zu\n", num_chunks,
176 entry);
177 #if 1
178 sprintf(info_str, "audio_file_info1:%zu x %lums\n"
179 "audio_file_info2:\n"
180 "audio_file_info3:\n",
181 num_chunks,
182 tv2ms(&af->chunk_tv));
183 #endif
184 #if 1
185 {
186 struct timeval total_tv;
187 tv_scale(num_chunks, &af->chunk_tv, &total_tv);
188 *seconds = tv2ms(&total_tv) / 1000;
189 PARA_INFO_LOG("%d seconds, %zu chunks\n", *seconds, num_chunks);
190 }
191 #endif
192 return 1;
193 }
194
195 /*
196 * Simple stream reposition routine
197 */
198 static int aac_reposition_stream(long unsigned request)
199 {
200 return 1;
201 // return -E_AAC_REPOS;
202 }
203
204 static char *aac_read_chunk(long unsigned current_chunk, ssize_t *len)
205 {
206 int ret;
207 size_t pos;
208
209 *len = 0;
210 if (current_chunk >= num_chunks)
211 return NULL;
212 if (!current_chunk) {
213 *len = chunk_table[0];
214 pos = entry;
215 } else {
216 *len = chunk_table[current_chunk] - chunk_table[current_chunk - 1];
217 pos = entry + chunk_table[current_chunk - 1];
218 }
219 if (inbuf_size < *len) {
220 inbuf = para_realloc(inbuf, *len);
221 inbuf_size = *len;
222 }
223 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
224 // pos, *len);
225 ret = fseek(infile, pos, SEEK_SET);
226 if (ret < 0)
227 return NULL;
228 ret = para_fread(inbuf, *len, 1, infile);
229 if (ret != *len)
230 return NULL;
231 // PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
232 // (long unsigned) inbuf[4]);
233 return (char *)inbuf;
234 }
235
236 static const char* aac_suffixes[] = {"m4a", NULL};
237 /** the init function of the aac audio format handler */
238 void aac_afh_init(void *p)
239 {
240 af = p;
241 af->reposition_stream = aac_reposition_stream;
242 af->get_file_info = aac_get_file_info,
243 af->read_chunk = aac_read_chunk;
244 af->close_audio_file = aac_close_audio_file;
245 af->get_header_info = NULL;
246 af->chunk_tv.tv_sec = 0;
247 af->chunk_tv.tv_usec = 23120;
248 tv_scale(3, &af->chunk_tv, &af->eof_tv);
249 af->suffixes = aac_suffixes;
250 }