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 read_stsz(unsigned skip
)
50 long unsigned sum
= 0;
53 ret
= aac_find_stsz(inbuf
, inbuf_len
, &skip
);
56 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
59 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
62 PARA_INFO_LOG("sz table has %d entries\n", num_chunks
);
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
);
72 inbuf_len
= ret
+ skip
;
74 PARA_INFO_LOG("next buffer: %d bytes\n", inbuf_len
);
76 sum
+= aac_read_int32(inbuf
+ skip
);
79 if (i
< 10 || i
> num_chunks
- 10)
80 PARA_DEBUG_LOG("offset #%d: %d\n", i
, chunk_table
[i
]);
88 * Init m4a file and write some tech data to given pointers.
90 static int aac_get_file_info(FILE *file
, char *info_str
, long unsigned *frames
,
93 int ret
, skip
, decoder_len
;
94 unsigned long rate
= 0;
95 unsigned char channels
= 0;
96 mp4AudioSpecificConfig mp4ASC
;
99 inbuf_size
= DEFAULT_INBUF_SIZE
;
100 inbuf
= para_malloc(inbuf_size
);
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
);
108 PARA_INFO_LOG("checking aac %d bytes\n", ret
);
110 ret
= aac_find_esds(inbuf
, inbuf_len
, &skip
);
115 ret
= NeAACDecInit(handle
, inbuf
+ skip
,
116 decoder_len
, &rate
, &channels
);
118 return -E_AACDEC_INIT
;
120 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate
, channels
);
121 ret
= NeAACDecAudioSpecificConfig(inbuf
+ skip
, inbuf_len
- skip
, &mp4ASC
);
123 PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
124 mp4ASC
.samplingFrequency
);
126 PARA_WARNING_LOG("no mp4ASC %s\n", "");
128 ret
= read_stsz(skip
);
131 *frames
= num_chunks
;
133 ret
= aac_find_entry(inbuf
, inbuf_len
, &skip
);
136 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
139 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
142 PARA_INFO_LOG("offset table has %d entries\, entry: %zd\n", num_chunks
,
145 sprintf(info_str
, "audio_file_info1:%d x %lums\n"
146 "audio_file_info2:\n"
147 "audio_file_info3:\n",
149 tv2ms(&af
->chunk_tv
));
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
);
163 * Simple stream reposition routine
165 static int aac_reposition_stream(long unsigned request
)
170 static __must_check
int para_fread(void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
172 size_t res
= fread(ptr
, size
, nmemb
, stream
);
180 char *aac_read_chunk(long unsigned current_chunk
, ssize_t
*len
)
186 if (current_chunk
>= num_chunks
)
188 if (!current_chunk
) {
191 } else if (current_chunk
== 1) {
192 *len
= chunk_table
[0];
195 *len
= chunk_table
[current_chunk
- 1] - chunk_table
[current_chunk
- 2];
196 pos
= entry
+ chunk_table
[current_chunk
- 2];
198 if (inbuf_size
< *len
) {
199 inbuf
= para_realloc(inbuf
, *len
);
202 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
204 ret
= fseek(infile
, pos
, SEEK_SET
);
207 ret
= para_fread(inbuf
, *len
, 1, infile
);
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
;
215 void aac_afh_init(void *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
);