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