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