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