aac_afh.c: Kill global variable inbuf_size
[paraslash.git] / aac_afh.c
1 /*
2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18 /*
19 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
20 * Ahead Software AG
21 */
22
23 /** \file aac_afh.c para_server's aac audio format handler */
24
25 #include "server.cmdline.h"
26 #include "server.h"
27 #include "vss.h"
28 #include "error.h"
29 #include "string.h"
30 #include "aac.h"
31 #include "fd.h"
32
33 /** size of the input buffer, must be big enough to hold header */
34 #define AAC_INBUF_SIZE 65536
35
36 static struct audio_format_handler *af;
37 static FILE *infile;
38 static unsigned char *inbuf;
39 static size_t inbuf_len, num_chunks;
40
41 static void aac_close_audio_file(void)
42 {
43 if (!infile)
44 return;
45 fclose(infile);
46 infile = NULL;
47 free(inbuf);
48 inbuf = NULL;
49 }
50
51 static int aac_find_stsz(unsigned char *buf, unsigned buflen, size_t *skip)
52 {
53 int i;
54
55 for (i = 0; i + 16 < buflen; i++) {
56 unsigned char *p = buf + i;
57 unsigned sample_count, sample_size;
58
59 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
60 continue;
61 PARA_INFO_LOG("found stsz@%d\n", i);
62 i += 8;
63 sample_size = aac_read_int32(buf + i);
64 PARA_INFO_LOG("sample size: %d\n", sample_size);
65 i += 4;
66 sample_count = aac_read_int32(buf + i);
67 i += 4;
68 PARA_INFO_LOG("sample count: %d\n", sample_count);
69 *skip = i;
70 return sample_count;
71 }
72 PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen);
73 return -E_STSZ;
74 }
75
76 static int read_chunk_table(struct audio_format_info *afi, size_t skip)
77 {
78 int ret, i;
79 size_t sum = 0;
80
81 for (;;) {
82 ret = aac_find_stsz(inbuf, inbuf_len, &skip);
83 if (ret >= 0)
84 break;
85 ret = read(fileno(infile), inbuf, AAC_INBUF_SIZE);
86 if (ret <= 0)
87 return -E_AAC_READ;
88 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
89 }
90 num_chunks = ret;
91 PARA_INFO_LOG("sz table has %zu entries\n", num_chunks);
92 afi->chunk_table = para_malloc((num_chunks + 1) * sizeof(size_t));
93 for (i = 1; i <= num_chunks; i++) {
94 if (skip + 4 > inbuf_len) {
95 skip = inbuf_len - skip;
96 memmove(inbuf, inbuf + inbuf_len - skip, skip);
97 ret = read(fileno(infile), inbuf + skip, AAC_INBUF_SIZE - skip);
98 if (ret <= 0)
99 return -E_AAC_READ;
100 inbuf_len = ret + skip;
101 skip = 0;
102 PARA_INFO_LOG("next buffer: %zu bytes\n", inbuf_len);
103 }
104 sum += aac_read_int32(inbuf + skip);
105 afi->chunk_table[i] = sum;
106 skip += 4;
107 if (i < 10 || i + 10 > num_chunks)
108 PARA_DEBUG_LOG("offset #%d: %zu\n", i, afi->chunk_table[i]);
109 }
110 return 1;
111 }
112
113 static long unsigned aac_set_chunk_tv(struct audio_format_info *afi,
114 mp4AudioSpecificConfig *mp4ASC)
115 {
116 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023,
117 ms = 1000.0 * num_chunks * tmp / mp4ASC->samplingFrequency;
118 struct timeval total;
119
120 ms2tv(ms, &total);
121 tv_divide(num_chunks, &total, &afi->chunk_tv);
122 PARA_INFO_LOG("%luHz, %fs (%zd x %lums)\n",
123 mp4ASC->samplingFrequency, ms / 1000,
124 num_chunks, tv2ms(&afi->chunk_tv));
125 return ms / 1000;
126 }
127
128 /*
129 * Init m4a file and write some tech data to given pointers.
130 */
131 static int aac_get_file_info(FILE *file, struct audio_format_info *afi)
132 {
133 int i, ret, decoder_len;
134 size_t skip;
135 unsigned long rate = 0;
136 unsigned char channels = 0;
137 mp4AudioSpecificConfig mp4ASC;
138 NeAACDecHandle handle;
139
140 inbuf = para_malloc(AAC_INBUF_SIZE);
141 infile = file;
142
143 ret = read(fileno(infile), inbuf, AAC_INBUF_SIZE);
144 if (ret <= 0)
145 return -E_AAC_READ;
146 inbuf_len = ret;
147 ret = aac_find_esds(inbuf, inbuf_len, &skip);
148 if (ret < 0)
149 return ret;
150 decoder_len = ret;
151 handle = aac_open();
152 ret = NeAACDecInit(handle, inbuf + skip,
153 decoder_len, &rate, &channels);
154 if (ret < 0)
155 return -E_AACDEC_INIT;
156 skip += ret;
157 PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
158 ret = NeAACDecAudioSpecificConfig(inbuf + skip, inbuf_len - skip,
159 &mp4ASC);
160 if (ret < 0)
161 return -E_MP4ASC;
162 ret = read_chunk_table(afi, skip);
163 if (ret < 0)
164 return ret;
165 afi->chunks_total = num_chunks;
166 afi->seconds_total = aac_set_chunk_tv(afi, &mp4ASC);
167 for (;;) {
168 ret = aac_find_entry_point(inbuf, inbuf_len, &skip);
169 if (ret >= 0)
170 break;
171 ret = read(fileno(infile), inbuf, AAC_INBUF_SIZE);
172 if (ret <= 0)
173 return -E_AAC_READ;
174 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
175 }
176 afi->chunk_table[0] = ret;
177 for (i = 1; i<= num_chunks; i++)
178 afi->chunk_table[i] += ret;
179 sprintf(afi->info_string, "audio_file_info1:%zu x %lums\n"
180 "audio_file_info2:\n"
181 "audio_file_info3:\n",
182 num_chunks,
183 tv2ms(&afi->chunk_tv));
184 tv_scale(20, &afi->chunk_tv, &afi->eof_tv);
185 return 1;
186 }
187
188 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
189 /** the init function of the aac audio format handler */
190 void aac_afh_init(struct audio_format_handler *p)
191 {
192 af = p;
193 af->get_file_info = aac_get_file_info,
194 af->close_audio_file = aac_close_audio_file;
195 af->suffixes = aac_suffixes;
196 }