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