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