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"
34 /* must be big enough to hold header */
35 #define DEFAULT_INBUF_SIZE 65536
38 static int inbuf_size
;
39 static unsigned char *inbuf
;
40 static size_t inbuf_len
;
41 struct audio_format_handler
*af
;
42 static size_t num_chunks
;
45 static size_t *chunk_table
;
46 NeAACDecHandle handle
;
48 static void aac_close_audio_file(void)
56 static int aac_find_stsz(unsigned char *buf
, unsigned buflen
, size_t *skip
)
60 for (i
= 0; i
+ 16 < buflen
; i
++) {
61 unsigned char *p
= buf
+ i
;
62 unsigned sample_count
, sample_size
;
64 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 's' || p
[3] != 'z')
66 PARA_INFO_LOG("found stsz@%d\n", i
);
68 sample_size
= aac_read_int32(buf
+ i
);
69 PARA_INFO_LOG("sample size: %d\n", sample_size
);
71 sample_count
= aac_read_int32(buf
+ i
);
73 PARA_INFO_LOG("sample count: %d\n", sample_count
);
77 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen
);
81 static int read_chunk_table(size_t skip
)
87 ret
= aac_find_stsz(inbuf
, inbuf_len
, &skip
);
90 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
93 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
96 PARA_INFO_LOG("sz table has %zu entries\n", num_chunks
);
98 chunk_table
= para_malloc(num_chunks
* sizeof(size_t));
99 for (i
= 0; i
< num_chunks
; i
++) {
100 if (skip
+ 4 > inbuf_len
) {
101 skip
= inbuf_len
- skip
;
102 memmove(inbuf
, inbuf
+ inbuf_len
- skip
, skip
);
103 ret
= read(fileno(infile
), inbuf
+ skip
, inbuf_size
- skip
);
106 inbuf_len
= ret
+ skip
;
108 PARA_INFO_LOG("next buffer: %zu bytes\n", inbuf_len
);
110 sum
+= aac_read_int32(inbuf
+ skip
);
111 chunk_table
[i
] = sum
;
113 if (i
< 10 || i
+ 10 > num_chunks
)
114 PARA_DEBUG_LOG("offset #%d: %zu\n", i
, chunk_table
[i
]);
120 long unsigned aac_set_chunk_tv(mp4AudioSpecificConfig
*mp4ASC
)
122 float tmp
= mp4ASC
->sbr_present_flag
== 1? 2047 : 1023,
123 ms
= 1000.0 * num_chunks
* tmp
/ mp4ASC
->samplingFrequency
;
124 struct timeval total
;
127 tv_divide(num_chunks
, &total
, &af
->chunk_tv
);
128 PARA_INFO_LOG("%luHz, %fs (%zd x %lums)\n",
129 mp4ASC
->samplingFrequency
, ms
/ 1000,
130 num_chunks
, tv2ms(&af
->chunk_tv
));
135 * Init m4a file and write some tech data to given pointers.
137 static int aac_get_file_info(FILE *file
, char *info_str
, long unsigned *frames
,
140 int ret
, decoder_len
;
142 unsigned long rate
= 0;
143 unsigned char channels
= 0;
144 mp4AudioSpecificConfig mp4ASC
;
147 inbuf_size
= DEFAULT_INBUF_SIZE
;
148 inbuf
= para_malloc(inbuf_size
);
151 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
155 ret
= aac_find_esds(inbuf
, inbuf_len
, &skip
);
160 ret
= NeAACDecInit(handle
, inbuf
+ skip
,
161 decoder_len
, &rate
, &channels
);
163 return -E_AACDEC_INIT
;
165 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate
, channels
);
166 ret
= NeAACDecAudioSpecificConfig(inbuf
+ skip
, inbuf_len
- skip
,
170 ret
= read_chunk_table(skip
);
173 *frames
= num_chunks
;
174 *seconds
= aac_set_chunk_tv(&mp4ASC
);
176 ret
= aac_find_entry_point(inbuf
, inbuf_len
, &skip
);
179 ret
= read(fileno(infile
), inbuf
, inbuf_size
);
182 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
185 sprintf(info_str
, "audio_file_info1:%zu x %lums\n"
186 "audio_file_info2:\n"
187 "audio_file_info3:\n",
189 tv2ms(&af
->chunk_tv
));
190 tv_scale(20, &af
->chunk_tv
, &af
->eof_tv
);
195 * nothing to do as we'll seek to the correct offset in aac read_chunk() anyway
197 static int aac_reposition_stream(__a_unused
long unsigned request
)
202 static char *aac_read_chunk(long unsigned current_chunk
, ssize_t
*len
)
208 if (current_chunk
>= num_chunks
)
210 if (!current_chunk
) {
211 *len
= chunk_table
[0];
214 *len
= chunk_table
[current_chunk
] - chunk_table
[current_chunk
- 1];
215 pos
= entry
+ chunk_table
[current_chunk
- 1];
217 if (inbuf_size
< *len
) {
218 inbuf
= para_realloc(inbuf
, *len
);
221 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
223 ret
= fseek(infile
, pos
, SEEK_SET
);
226 ret
= para_fread(inbuf
, *len
, 1, infile
);
229 // PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
230 // (long unsigned) inbuf[4]);
231 return (char *)inbuf
;
234 static const char* aac_suffixes
[] = {"m4a", NULL
};
235 /** the init function of the aac audio format handler */
236 void aac_afh_init(struct audio_format_handler
*p
)
239 af
->reposition_stream
= aac_reposition_stream
;
240 af
->get_file_info
= aac_get_file_info
,
241 af
->read_chunk
= aac_read_chunk
;
242 af
->close_audio_file
= aac_close_audio_file
;
243 af
->get_header_info
= NULL
;
244 af
->suffixes
= aac_suffixes
;