aac: move aac_find_stsz from aac_common.c to to aac_afh.c
[paraslash.git] / aac_afh.c
1 /*
2  * Copyright (C) 2006 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 /** \file aac_afh.c para_server's aac audio format handler */
19
20
21 #include "server.cmdline.h"
22 #include "server.h"
23 #include "afs.h"
24 #include "error.h"
25 #include "string.h"
26 #include "aac.h"
27
28 /* must be big enough to hold header */
29 #define DEFAULT_INBUF_SIZE 65536
30
31 static FILE *infile;
32 static int inbuf_size;
33 static unsigned char *inbuf;
34 static unsigned inbuf_len;
35 struct audio_format *af;
36
37 static unsigned num_chunks, entry;
38
39 static size_t *chunk_table;
40 NeAACDecHandle handle;
41
42
43 static void aac_close_audio_file(void)
44 {
45 }
46
47 static int aac_find_stsz(unsigned char *buf, unsigned buflen, unsigned *skip)
48 {
49         int i;
50
51         for (i = 0; i + 16 < buflen; i++) {
52                 unsigned char *p = buf + i;
53                 unsigned sample_count, sample_size;
54
55                 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
56                         continue;
57                 PARA_INFO_LOG("found stsz@%d\n", i);
58                 i += 8;
59                 sample_size = aac_read_int32(buf + i);
60                 PARA_INFO_LOG("sample size: %d\n", sample_size);
61                 i += 4;
62                 sample_count = aac_read_int32(buf + i);
63                 i += 4;
64                 PARA_INFO_LOG("sample count: %d\n", sample_count);
65                 *skip = i;
66                 return sample_count;
67         }
68         PARA_WARNING_LOG("stsz not found, buflen: %d\n", buflen);
69         return -E_STCO;
70 }
71
72
73 static int read_stsz(unsigned skip)
74 {
75         int ret, i;
76         long unsigned sum = 0;
77
78         for (;;) {
79                 ret = aac_find_stsz(inbuf, inbuf_len, &skip);
80                 if (ret >= 0)
81                         break;
82                 ret = read(fileno(infile), inbuf, inbuf_size);
83                 if (ret <= 0)
84                         return -E_AAC_READ;
85                 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
86         }
87         num_chunks = ret;
88         PARA_INFO_LOG("sz table has %d entries\n", num_chunks);
89         free(chunk_table);
90         chunk_table = para_malloc(num_chunks * sizeof(size_t));
91         for (i = 0; i < num_chunks; i++) {
92                 if (skip + 4 > inbuf_len) {
93                         skip = inbuf_len - skip;
94                         memmove(inbuf, inbuf + inbuf_len - skip, skip);
95                         ret = read(fileno(infile), inbuf + skip, 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: %d bytes\n", inbuf_len);
101                 }
102                 sum += aac_read_int32(inbuf + skip);
103                 chunk_table[i] = sum;
104                 skip += 4;
105                 if (i < 10 || i > num_chunks - 10)
106                         PARA_DEBUG_LOG("offset #%d: %d\n", i, chunk_table[i]);
107         }
108         return 1;
109
110 }
111
112
113 /*
114  * Init m4a file and write some tech data to given pointers.
115  */
116 static int aac_get_file_info(FILE *file, char *info_str, long unsigned *frames,
117         int *seconds)
118 {
119         int ret, skip, decoder_len;
120         unsigned long rate = 0;
121         unsigned char channels = 0;
122         mp4AudioSpecificConfig mp4ASC;
123
124         free(inbuf);
125         inbuf_size = DEFAULT_INBUF_SIZE;
126         inbuf = para_malloc(inbuf_size);
127         infile = file;
128
129         PARA_INFO_LOG("file: %p\n", file);
130         ret = read(fileno(infile), inbuf, inbuf_size);
131         PARA_INFO_LOG("read %d bytes\n", ret);
132         if (ret <= 0)
133                 return -E_AAC_READ;
134         PARA_INFO_LOG("checking aac %d bytes\n", ret);
135         inbuf_len = ret;
136         ret = aac_find_esds(inbuf, inbuf_len, &skip);
137         if (ret < 0)
138                 return ret;
139         decoder_len = ret;
140         handle = aac_open();
141         ret = NeAACDecInit(handle, inbuf + skip,
142                 decoder_len, &rate, &channels);
143         if (ret < 0)
144                 return -E_AACDEC_INIT;
145         skip += ret;
146         PARA_INFO_LOG("rate: %lu, channels: %d\n", rate, channels);
147         ret = NeAACDecAudioSpecificConfig(inbuf + skip, inbuf_len - skip, &mp4ASC);
148         if (ret >= 0) {
149                 PARA_DEBUG_LOG("mp4ASC.samplingFrequency: %lu\n",
150                         mp4ASC.samplingFrequency);
151         } else
152                 PARA_WARNING_LOG("no mp4ASC %s\n", "");
153
154         ret = read_stsz(skip);
155         if (ret < 0)
156                 return ret;
157         *frames = num_chunks;
158         for (;;) {
159                 ret = aac_find_entry(inbuf, inbuf_len, &skip);
160                 if (ret >= 0)
161                         break;
162                 ret = read(fileno(infile), inbuf, inbuf_size);
163                 if (ret <= 0)
164                         return -E_AAC_READ;
165                 PARA_INFO_LOG("next buffer: %d bytes\n", ret);
166         }
167         entry = ret;
168         PARA_INFO_LOG("offset table has %d entries\, entry: %zd\n", num_chunks,
169                 entry);
170 #if 1
171         sprintf(info_str, "audio_file_info1:%d x %lums\n"
172                 "audio_file_info2:\n"
173                 "audio_file_info3:\n",
174                 num_chunks,
175                 tv2ms(&af->chunk_tv));
176 #endif
177 #if 1
178         {
179         struct timeval total_tv;
180         tv_scale(num_chunks, &af->chunk_tv, &total_tv);
181         *seconds = tv2ms(&total_tv) / 1000;
182         PARA_INFO_LOG("%d seconds, %d chunks\n", *seconds, num_chunks);
183         }
184 #endif
185         return 1;
186 }
187
188 /*
189  * Simple stream reposition routine
190  */
191 static int aac_reposition_stream(long unsigned request)
192 {
193         return -E_AAC_REPOS;
194 }
195
196 static __must_check int para_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
197 {
198         size_t res = fread(ptr, size, nmemb, stream);
199         if (res == nmemb)
200                 return size * nmemb;
201         if (feof(stream))
202                 return 0;
203         return -E_FREAD;
204 }
205
206 char *aac_read_chunk(long unsigned current_chunk, ssize_t *len)
207 {
208         int ret;
209         size_t pos;
210
211         *len = 0;
212         if (current_chunk >= num_chunks)
213                 return NULL;
214         if (!current_chunk) {
215                 *len = entry;
216                 pos = 0;
217         } else if (current_chunk == 1) {
218                 *len = chunk_table[0];
219                 pos = entry;
220         } else {
221                 *len = chunk_table[current_chunk - 1] - chunk_table[current_chunk - 2];
222                 pos = entry + chunk_table[current_chunk - 2];
223         }
224         if (inbuf_size < *len) {
225                 inbuf = para_realloc(inbuf, *len);
226                 inbuf_size = *len;
227         }
228 //      PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
229 //              pos, *len);
230         ret = fseek(infile, pos, SEEK_SET);
231         if (ret < 0)
232                 return NULL;
233         ret = para_fread(inbuf, *len, 1, infile);
234         if (ret != *len)
235                 return NULL;
236 //      PARA_DEBUG_LOG("ret: %d, inbuf[0]: %lx - %lx\n", ret, (long unsigned) inbuf[0],
237 //              (long unsigned) inbuf[4]);
238         return (char *)inbuf;
239 }
240
241 void aac_afh_init(void *p)
242 {
243         af = p;
244         af->reposition_stream = aac_reposition_stream;
245         af->get_file_info = aac_get_file_info,
246         af->read_chunk = aac_read_chunk;
247         af->close_audio_file = aac_close_audio_file;
248         af->get_header_info = NULL;
249         af->chunk_tv.tv_sec = 0;
250         af->chunk_tv.tv_usec = 23120;
251         tv_scale(3, &af->chunk_tv, &af->eof_tv);
252 }