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.
18 /** \file aac_afh.c para_server's aac audio format handler */
21 #include "server.cmdline.h"
28 /* must be big enough to hold header */
29 #define DEFAULT_INBUF_SIZE 65536
32 static int inbuf_size
;
33 static unsigned char *inbuf
;
34 static unsigned inbuf_len
;
35 struct audio_format
*af
;
37 static unsigned num_chunks
, entry
;
39 static size_t *chunk_table
;
40 NeAACDecHandle handle
;
43 static void aac_close_audio_file(void)
47 static int aac_find_stsz(unsigned char *buf
, unsigned buflen
, unsigned *skip
)
51 for (i
= 0; i
+ 16 < buflen
; i
++) {
52 unsigned char *p
= buf
+ i
;
53 unsigned sample_count
, sample_size
;
55 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 's' || p
[3] != 'z')
57 PARA_INFO_LOG("found stsz@%d\n", i
);
59 sample_size
= aac_read_int32(buf
+ i
);
60 PARA_INFO_LOG("sample size: %d\n", sample_size
);
62 sample_count
= aac_read_int32(buf
+ i
);
64 PARA_INFO_LOG("sample count: %d\n", sample_count
);
68 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen
);
73 static int read_stsz(unsigned skip
)
76 long unsigned sum
= 0;
79 ret
= aac_find_stsz(inbuf
, inbuf_len
, &skip
);
82 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
85 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
88 PARA_INFO_LOG("sz table has %d entries\n", num_chunks
);
90 chunk_table
= para_malloc(num_chunks
* sizeof(size_t));
91 for (i
= 0; i
< num_chunks
; i
++) {
92 if (skip
+ 4 > inbuf_len
) {
93 skip
= inbuf_len
- skip
;
94 memmove(inbuf
, inbuf
+ inbuf_len
- skip
, skip
);
95 ret
= read(fileno(infile
), inbuf
+ skip
, inbuf_size
- skip
);
98 inbuf_len
= ret
+ skip
;
100 PARA_INFO_LOG("next buffer: %d bytes\n", inbuf_len
);
102 sum
+= aac_read_int32(inbuf
+ skip
);
103 chunk_table
[i
] = sum
;
105 if (i
< 10 || i
> num_chunks
- 10)
106 PARA_DEBUG_LOG("offset #%d: %d\n", i
, chunk_table
[i
]);
114 * Init m4a file and write some tech data to given pointers.
116 static int aac_get_file_info(FILE *file
, char *info_str
, long unsigned *frames
,
119 int ret
, skip
, decoder_len
;
120 unsigned long rate
= 0;
121 unsigned char channels
= 0;
122 mp4AudioSpecificConfig mp4ASC
;
125 inbuf_size
= DEFAULT_INBUF_SIZE
;
126 inbuf
= para_malloc(inbuf_size
);
129 PARA_INFO_LOG("file: %p\n", file
);
130 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
131 PARA_INFO_LOG("read %d bytes\n", ret
);
134 PARA_INFO_LOG("checking aac %d bytes\n", ret
);
136 ret
= aac_find_esds(inbuf
, inbuf_len
, &skip
);
141 ret
= NeAACDecInit(handle
, inbuf
+ skip
,
142 decoder_len
, &rate
, &channels
);
144 return -E_AACDEC_INIT
;
146 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate
, channels
);
147 ret
= NeAACDecAudioSpecificConfig(inbuf
+ skip
, inbuf_len
- skip
, &mp4ASC
);
149 PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
150 mp4ASC
.samplingFrequency
);
152 PARA_WARNING_LOG("no mp4ASC %s\n", "");
154 ret
= read_stsz(skip
);
157 *frames
= num_chunks
;
159 ret
= aac_find_entry(inbuf
, inbuf_len
, &skip
);
162 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
165 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
168 PARA_INFO_LOG("offset table has %d entries\, entry: %zd\n", num_chunks
,
171 sprintf(info_str
, "audio_file_info1:%d x %lums\n"
172 "audio_file_info2:\n"
173 "audio_file_info3:\n",
175 tv2ms(&af
->chunk_tv
));
179 struct timeval total_tv
;
180 tv_scale(num_chunks
, &af
->chunk_tv
, &total_tv
);
181 *seconds
= tv2ms(&total_tv
) / 1000;
182 PARA_INFO_LOG("%d seconds, %d chunks\n", *seconds
, num_chunks
);
189 * Simple stream reposition routine
191 static int aac_reposition_stream(long unsigned request
)
196 static __must_check
int para_fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
198 size_t res
= fread(ptr
, size
, nmemb
, stream
);
206 char *aac_read_chunk(long unsigned current_chunk
, ssize_t
*len
)
212 if (current_chunk
>= num_chunks
)
214 if (!current_chunk
) {
217 } else if (current_chunk
== 1) {
218 *len
= chunk_table
[0];
221 *len
= chunk_table
[current_chunk
- 1] - chunk_table
[current_chunk
- 2];
222 pos
= entry
+ chunk_table
[current_chunk
- 2];
224 if (inbuf_size
< *len
) {
225 inbuf
= para_realloc(inbuf
, *len
);
228 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
230 ret
= fseek(infile
, pos
, SEEK_SET
);
233 ret
= para_fread(inbuf
, *len
, 1, infile
);
236 // PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
237 // (long unsigned) inbuf[4]);
238 return (char *)inbuf
;
241 void aac_afh_init(void *p
)
244 af
->reposition_stream
= aac_reposition_stream
;
245 af
->get_file_info
= aac_get_file_info
,
246 af
->read_chunk
= aac_read_chunk
;
247 af
->close_audio_file
= aac_close_audio_file
;
248 af
->get_header_info
= NULL
;
249 af
->chunk_tv
.tv_sec
= 0;
250 af
->chunk_tv
.tv_usec
= 23120;
251 tv_scale(3, &af
->chunk_tv
, &af
->eof_tv
);