1 /*
2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16
17 * Neither the name of the The Musepack Development Team nor the
18 names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior
20 written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /// \file streaminfo.c
35 /// Implementation of streaminfo reading functions.
36
37 #include <math.h>
38 #include "mpcdec.h"
39 #include "streaminfo.h"
40 #include <stdio.h>
41 #include "internal.h"
42 #include "huffman.h"
43 #include "mpc_bits_reader.h"
44
45 unsigned long mpc_crc32(unsigned char *buf, int len);
46
47 static const char na[] = "n.a.";
48 static char const * const versionNames[] = {
49 na, "'Unstable/Experimental'", na, na, na, "'quality 0'", "'quality 1'",
50 "'Telephone'", "'Thumb'", "'Radio'", "'Standard'", "'Extreme'", "'Insane'",
51 "'BrainDead'", "'quality 9'", "'quality 10'"
52 };
53 static const mpc_int32_t samplefreqs[8] = { 44100, 48000, 37800, 32000 };
54
55 static const char *
mpc_get_version_string(float profile)56 mpc_get_version_string(float profile) // profile is 0...15, where 7...13 is used
57 {
58 return profile >= sizeof versionNames / sizeof *versionNames ? na : versionNames[(int)profile];
59 }
60
61 static void
mpc_get_encoder_string(mpc_streaminfo * si)62 mpc_get_encoder_string(mpc_streaminfo* si)
63 {
64 int ver = si->encoder_version;
65 if (si->stream_version >= 8)
66 ver = (si->encoder_version >> 24) * 100 + ((si->encoder_version >> 16) & 0xFF);
67 if (ver <= 116) {
68 if (ver == 0) {
69 sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
70 } else {
71 switch (ver % 10) {
72 case 0:
73 sprintf(si->encoder, "Release %u.%u", ver / 100,
74 ver / 10 % 10);
75 break;
76 case 2: case 4: case 6: case 8:
77 sprintf(si->encoder, "Beta %u.%02u", ver / 100,
78 ver % 100);
79 break;
80 default:
81 sprintf(si->encoder, "--Alpha-- %u.%02u",
82 ver / 100, ver % 100);
83 break;
84 }
85 }
86 } else {
87 int major = si->encoder_version >> 24;
88 int minor = (si->encoder_version >> 16) & 0xFF;
89 int build = (si->encoder_version >> 8) & 0xFF;
90 const char * tmp = "--Stable--";
91
92 if (minor & 1)
93 tmp = "--Unstable--";
94
95 sprintf(si->encoder, "%s %u.%u.%u", tmp, major, minor, build);
96 }
97 }
98
check_streaminfo(mpc_streaminfo * si)99 static mpc_status check_streaminfo(mpc_streaminfo * si)
100 {
101 if (si->max_band == 0 || si->max_band >= 32
102 || si->channels > 2 || si->channels == 0 || si->sample_freq == 0)
103 return MPC_STATUS_FAIL;
104 return MPC_STATUS_OK;
105 }
106
107 /// Reads streaminfo from SV7 header.
108 mpc_status
streaminfo_read_header_sv7(mpc_streaminfo * si,mpc_bits_reader * r)109 streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_bits_reader * r)
110 {
111 mpc_uint32_t frames, last_frame_samples;
112
113 si->bitrate = 0;
114 frames = (mpc_bits_read(r, 16) << 16) | mpc_bits_read(r, 16);
115 mpc_bits_read(r, 1); // intensity stereo : should be 0
116 si->ms = mpc_bits_read(r, 1);
117 si->max_band = mpc_bits_read(r, 6);
118 si->profile = mpc_bits_read(r, 4);
119 si->profile_name = mpc_get_version_string(si->profile);
120 mpc_bits_read(r, 2); // Link ?
121 si->sample_freq = samplefreqs[mpc_bits_read(r, 2)];
122 mpc_bits_read(r, 16); // skip MaxLevel (maximum level of input PCM)
123 si->gain_title = (mpc_uint16_t) mpc_bits_read(r, 16);
124 si->peak_title = (mpc_uint16_t) mpc_bits_read(r, 16);
125 si->gain_album = (mpc_uint16_t) mpc_bits_read(r, 16);
126 si->peak_album = (mpc_uint16_t) mpc_bits_read(r, 16);
127 si->is_true_gapless = mpc_bits_read(r, 1); // true gapless: used?
128 last_frame_samples = mpc_bits_read(r, 11); // true gapless: valid samples for last frame
129 si->fast_seek = mpc_bits_read(r, 1); // fast seeking
130 mpc_bits_read(r, 19); // unused
131 si->encoder_version = mpc_bits_read(r, 8);
132 si->channels = 2;
133 si->block_pwr = 0;
134
135 // convert gain info
136 if (si->gain_title != 0) {
137 int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_title / 100.) * 256. + .5);
138 if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
139 si->gain_title = (mpc_int16_t) tmp;
140 }
141
142 if (si->gain_album != 0) {
143 int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_album / 100.) * 256. + .5);
144 if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
145 si->gain_album = (mpc_int16_t) tmp;
146 }
147
148 if (si->peak_title != 0)
149 si->peak_title = (mpc_uint16_t) (log10(si->peak_title) * 20 * 256 + .5);
150
151 if (si->peak_album != 0)
152 si->peak_album = (mpc_uint16_t) (log10(si->peak_album) * 20 * 256 + .5);
153
154 mpc_get_encoder_string(si);
155
156 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
157 else if (last_frame_samples > MPC_FRAME_LENGTH) return MPC_STATUS_FAIL;
158 si->samples = (mpc_int64_t) frames * MPC_FRAME_LENGTH;
159 if (si->is_true_gapless)
160 si->samples -= (MPC_FRAME_LENGTH - last_frame_samples);
161 else
162 si->samples -= MPC_DECODER_SYNTH_DELAY;
163
164 si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
165 * si->sample_freq / si->samples;
166
167 return check_streaminfo(si);
168 }
169
170 /// Reads replay gain datas
streaminfo_gain(mpc_streaminfo * si,const mpc_bits_reader * r_in)171 void streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in)
172 {
173 mpc_bits_reader r = *r_in;
174
175 int version = mpc_bits_read(&r, 8); // gain version
176 if (version != 1) // we only know ver 1
177 return;
178 si->gain_title = (mpc_uint16_t) mpc_bits_read(&r, 16);
179 si->peak_title = (mpc_uint16_t) mpc_bits_read(&r, 16);
180 si->gain_album = (mpc_uint16_t) mpc_bits_read(&r, 16);
181 si->peak_album = (mpc_uint16_t) mpc_bits_read(&r, 16);
182 }
183
184 /// Reads streaminfo from SV8 header.
185 mpc_status
streaminfo_read_header_sv8(mpc_streaminfo * si,const mpc_bits_reader * r_in,mpc_size_t block_size)186 streaminfo_read_header_sv8(mpc_streaminfo* si, const mpc_bits_reader * r_in,
187 mpc_size_t block_size)
188 {
189 mpc_uint32_t CRC;
190 mpc_bits_reader r = *r_in;
191
192 CRC = (mpc_bits_read(&r, 16) << 16) | mpc_bits_read(&r, 16);
193 if (CRC != mpc_crc32(r.buff + 1 - (r.count >> 3), (int)block_size - 4))
194 return MPC_STATUS_FAIL;
195
196 si->stream_version = mpc_bits_read(&r, 8);
197 if (si->stream_version != 8)
198 return MPC_STATUS_FAIL;
199
200 mpc_bits_get_size(&r, &si->samples);
201 mpc_bits_get_size(&r, &si->beg_silence);
202
203 si->is_true_gapless = 1;
204 si->sample_freq = samplefreqs[mpc_bits_read(&r, 3)];
205 si->max_band = mpc_bits_read(&r, 5) + 1;
206 si->channels = mpc_bits_read(&r, 4) + 1;
207 si->ms = mpc_bits_read(&r, 1);
208 si->block_pwr = mpc_bits_read(&r, 3) * 2;
209
210 si->bitrate = 0;
211
212 if ((si->samples - si->beg_silence) != 0)
213 si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
214 * si->sample_freq / (si->samples - si->beg_silence);
215
216 return check_streaminfo(si);
217 }
218
219 /// Reads encoder informations
streaminfo_encoder_info(mpc_streaminfo * si,const mpc_bits_reader * r_in)220 void streaminfo_encoder_info(mpc_streaminfo* si, const mpc_bits_reader * r_in)
221 {
222 mpc_bits_reader r = *r_in;
223
224 si->profile = mpc_bits_read(&r, 7) / 8.;
225 si->profile_name = mpc_get_version_string(si->profile);
226 si->pns = mpc_bits_read(&r, 1);
227 si->encoder_version = mpc_bits_read(&r, 8) << 24; // major
228 si->encoder_version |= mpc_bits_read(&r, 8) << 16; // minor
229 si->encoder_version |= mpc_bits_read(&r, 8) << 8; // build
230
231
232 mpc_get_encoder_string(si);
233 }
234
235 double
mpc_streaminfo_get_length(mpc_streaminfo * si)236 mpc_streaminfo_get_length(mpc_streaminfo * si)
237 {
238 return (double) (si->samples - si->beg_silence) / si->sample_freq;
239 }
240
mpc_streaminfo_get_length_samples(mpc_streaminfo * si)241 mpc_int64_t mpc_streaminfo_get_length_samples(mpc_streaminfo *si)
242 {
243 return si->samples - si->beg_silence;
244 }
245