2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
23 /** \file aac_afh.c para_server's aac audio format handler */
25 #include "server.cmdline.h"
32 /* must be big enough to hold header */
33 #define DEFAULT_INBUF_SIZE 65536
36 static int inbuf_size;
37 static unsigned char *inbuf;
38 static size_t inbuf_len;
39 struct audio_format *af;
40 static size_t num_chunks;
43 static size_t *chunk_table;
44 NeAACDecHandle handle;
47 static void aac_close_audio_file(void)
51 static int aac_find_stsz(unsigned char *buf, unsigned buflen, size_t *skip)
55 for (i = 0; i + 16 < buflen; i++) {
56 unsigned char *p = buf + i;
57 unsigned sample_count, sample_size;
59 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
61 PARA_INFO_LOG("found stsz@%d\n", i);
63 sample_size = aac_read_int32(buf + i);
64 PARA_INFO_LOG("sample size: %d\n", sample_size);
66 sample_count = aac_read_int32(buf + i);
68 PARA_INFO_LOG("sample count: %d\n", sample_count);
72 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen);
76 static int read_chunk_table(size_t skip)
82 ret = aac_find_stsz(inbuf, inbuf_len, &skip);
85 ret = read(fileno(infile), inbuf, inbuf_size);
88 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
91 PARA_INFO_LOG("sz table has %zu entries\n", num_chunks);
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);
101 inbuf_len = ret + skip;
103 PARA_INFO_LOG("next buffer: %zu bytes\n", inbuf_len);
105 sum += aac_read_int32(inbuf + skip);
106 chunk_table[i] = sum;
108 if (i < 10 || i > num_chunks - 10)
109 PARA_DEBUG_LOG("offset #%d: %zu\n", i, chunk_table[i]);
116 * Init m4a file and write some tech data to given pointers.
118 static int aac_get_file_info(FILE *file, char *info_str, long unsigned *frames,
121 int ret, decoder_len;
123 unsigned long rate = 0;
124 unsigned char channels = 0;
125 mp4AudioSpecificConfig mp4ASC;
128 inbuf_size = DEFAULT_INBUF_SIZE;
129 inbuf = para_malloc(inbuf_size);
132 PARA_INFO_LOG("file: %p\n", file);
133 ret = read(fileno(infile), inbuf, inbuf_size);
134 PARA_INFO_LOG("read %d bytes\n", ret);
137 PARA_INFO_LOG("checking aac %d bytes\n", ret);
139 ret = aac_find_esds(inbuf, inbuf_len, &skip);
144 ret = NeAACDecInit(handle, inbuf + skip,
145 decoder_len, &rate, &channels);
147 return -E_AACDEC_INIT;
149 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
150 ret = NeAACDecAudioSpecificConfig(inbuf + skip, inbuf_len - skip, &mp4ASC);
152 PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
153 mp4ASC.samplingFrequency);
155 PARA_WARNING_LOG("no mp4ASC %s\n", "");
157 ret = read_chunk_table(skip);
160 *frames = num_chunks;
162 ret = aac_find_entry_point(inbuf, inbuf_len, &skip);
165 ret = read(fileno(infile), inbuf, inbuf_size);
168 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
171 PARA_INFO_LOG("offset table has %zu entries, entry: %zu\n", num_chunks,
174 sprintf(info_str, "audio_file_info1:%zu x %lums\n"
175 "audio_file_info2:\n"
176 "audio_file_info3:\n",
178 tv2ms(&af->chunk_tv));
182 struct timeval total_tv;
183 tv_scale(num_chunks, &af->chunk_tv, &total_tv);
184 *seconds = tv2ms(&total_tv) / 1000;
185 PARA_INFO_LOG("%d seconds, %zu chunks\n", *seconds, num_chunks);
192 * Simple stream reposition routine
194 static int aac_reposition_stream(long unsigned request)
197 // return -E_AAC_REPOS;
200 static __must_check int para_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
202 size_t res = fread(ptr, size, nmemb, stream);
210 static char *aac_read_chunk(long unsigned current_chunk, ssize_t *len)
216 if (current_chunk >= num_chunks)
218 if (!current_chunk) {
219 *len = chunk_table[0];
222 *len = chunk_table[current_chunk] - chunk_table[current_chunk - 1];
223 pos = entry + chunk_table[current_chunk - 1];
225 if (inbuf_size < *len) {
226 inbuf = para_realloc(inbuf, *len);
229 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
231 ret = fseek(infile, pos, SEEK_SET);
234 ret = para_fread(inbuf, *len, 1, infile);
237 // PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
238 // (long unsigned) inbuf[4]);
239 return (char *)inbuf;
242 void aac_afh_init(void *p)
245 af->reposition_stream = aac_reposition_stream;
246 af->get_file_info = aac_get_file_info,
247 af->read_chunk = aac_read_chunk;
248 af->close_audio_file = aac_close_audio_file;
249 af->get_header_info = NULL;
250 af->chunk_tv.tv_sec = 0;
251 af->chunk_tv.tv_usec = 23120;
252 tv_scale(3, &af->chunk_tv, &af->eof_tv);