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