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