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