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