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