2 * Copyright (C) 2006-2007 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"
33 /** size of the input buffer, must be big enough to hold header */
34 #define AAC_INBUF_SIZE 65536
37 static unsigned char *inbuf
;
38 static size_t inbuf_len
;
40 static void aac_close_audio_file(void)
50 static int aac_find_stsz(unsigned char *buf
, unsigned buflen
, size_t *skip
)
54 for (i
= 0; i
+ 16 < buflen
; i
++) {
55 unsigned char *p
= buf
+ i
;
56 unsigned sample_count
, sample_size
;
58 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 's' || p
[3] != 'z')
60 PARA_INFO_LOG("found stsz@%d\n", i
);
62 sample_size
= aac_read_int32(buf
+ i
);
63 PARA_INFO_LOG("sample size: %d\n", sample_size
);
65 sample_count
= aac_read_int32(buf
+ i
);
67 PARA_INFO_LOG("sample count: %d\n", sample_count
);
71 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen
);
75 static int read_chunk_table(struct audio_format_info
*afi
, size_t skip
)
81 ret
= aac_find_stsz(inbuf
, inbuf_len
, &skip
);
84 ret
= read(fileno(infile
), inbuf
, AAC_INBUF_SIZE
);
87 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
89 afi
->chunks_total
= ret
;
90 PARA_INFO_LOG("sz table has %lu entries\n", afi
->chunks_total
);
91 afi
->chunk_table
= para_malloc((afi
->chunks_total
+ 1) * sizeof(size_t));
92 for (i
= 1; i
<= afi
->chunks_total
; i
++) {
93 if (skip
+ 4 > inbuf_len
) {
94 skip
= inbuf_len
- skip
;
95 memmove(inbuf
, inbuf
+ inbuf_len
- skip
, skip
);
96 ret
= read(fileno(infile
), inbuf
+ skip
, AAC_INBUF_SIZE
- skip
);
99 inbuf_len
= ret
+ skip
;
101 PARA_INFO_LOG("next buffer: %zu bytes\n", inbuf_len
);
103 sum
+= aac_read_int32(inbuf
+ skip
);
104 afi
->chunk_table
[i
] = sum
;
106 if (i
< 10 || i
+ 10 > afi
->chunks_total
)
107 PARA_DEBUG_LOG("offset #%d: %zu\n", i
, afi
->chunk_table
[i
]);
112 static long unsigned aac_set_chunk_tv(struct audio_format_info
*afi
,
113 mp4AudioSpecificConfig
*mp4ASC
)
115 float tmp
= mp4ASC
->sbr_present_flag
== 1? 2047 : 1023,
116 ms
= 1000.0 * afi
->chunks_total
* tmp
/ mp4ASC
->samplingFrequency
;
117 struct timeval total
;
120 tv_divide(afi
->chunks_total
, &total
, &afi
->chunk_tv
);
121 PARA_INFO_LOG("%luHz, %fs (%lu x %lums)\n",
122 mp4ASC
->samplingFrequency
, ms
/ 1000,
123 afi
->chunks_total
, tv2ms(&afi
->chunk_tv
));
128 * Init m4a file and write some tech data to given pointers.
130 static int aac_get_file_info(FILE *file
, struct audio_format_info
*afi
)
132 int i
, ret
, decoder_len
;
134 unsigned long rate
= 0;
135 unsigned char channels
= 0;
136 mp4AudioSpecificConfig mp4ASC
;
137 NeAACDecHandle handle
;
139 inbuf
= para_malloc(AAC_INBUF_SIZE
);
142 ret
= read(fileno(infile
), inbuf
, AAC_INBUF_SIZE
);
146 ret
= aac_find_esds(inbuf
, inbuf_len
, &skip
);
151 ret
= NeAACDecInit(handle
, inbuf
+ skip
,
152 decoder_len
, &rate
, &channels
);
154 return -E_AACDEC_INIT
;
156 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate
, channels
);
157 ret
= NeAACDecAudioSpecificConfig(inbuf
+ skip
, inbuf_len
- skip
,
161 ret
= read_chunk_table(afi
, skip
);
164 afi
->seconds_total
= aac_set_chunk_tv(afi
, &mp4ASC
);
166 ret
= aac_find_entry_point(inbuf
, inbuf_len
, &skip
);
169 ret
= read(fileno(infile
), inbuf
, AAC_INBUF_SIZE
);
172 PARA_INFO_LOG("next buffer: %d bytes\n", ret
);
174 afi
->chunk_table
[0] = ret
;
175 for (i
= 1; i
<= afi
->chunks_total
; i
++)
176 afi
->chunk_table
[i
] += ret
;
177 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lums\n"
178 "audio_file_info2:\n"
179 "audio_file_info3:\n",
181 tv2ms(&afi
->chunk_tv
));
182 tv_scale(20, &afi
->chunk_tv
, &afi
->eof_tv
);
186 static const char* aac_suffixes
[] = {"m4a", "mp4", NULL
};
187 /** the init function of the aac audio format handler */
188 void aac_afh_init(struct audio_format_handler
*p
)
190 p
->get_file_info
= aac_get_file_info
,
191 p
->close_audio_file
= aac_close_audio_file
;
192 p
->suffixes
= aac_suffixes
;