2 Copyright (c) 2005-2009, The Musepack Development Team
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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
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.
34 /// \file streaminfo.c
35 /// Implementation of streaminfo reading functions.
39 #include "streaminfo.h"
43 #include "mpc_bits_reader.h"
46 static const char na[] = "n.a.";
47 static char const * const versionNames[] = {
48 na, "'Unstable/Experimental'", na, na, na, "'quality 0'", "'quality 1'",
49 "'Telephone'", "'Thumb'", "'Radio'", "'Standard'", "'Extreme'", "'Insane'",
50 "'BrainDead'", "'quality 9'", "'quality 10'"
53 static const mpc_int32_t samplefreqs
[8] = { 44100, 48000, 37800, 32000 };
56 mpc_get_version_string(float profile) // profile is 0...15, where 7...13 is used
58 return profile >= sizeof versionNames / sizeof *versionNames ? na : versionNames[(int)profile];
63 mpc_get_encoder_string(mpc_streaminfo* si)
65 int ver = si->encoder_version;
66 if (si->stream_version >= 8)
67 ver = (si->encoder_version >> 24) * 100 + ((si->encoder_version >> 16) & 0xFF);
70 sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
74 sprintf(si->encoder, "Release %u.%u", ver / 100,
77 case 2: case 4: case 6: case 8:
78 sprintf(si->encoder, "Beta %u.%02u", ver / 100,
82 sprintf(si->encoder, "--Alpha-- %u.%02u",
83 ver / 100, ver % 100);
88 int major = si->encoder_version >> 24;
89 int minor = (si->encoder_version >> 16) & 0xFF;
90 int build = (si->encoder_version >> 8) & 0xFF;
91 char * tmp = "--Stable--";
96 sprintf(si->encoder, "%s %u.%u.%u", tmp, major, minor, build);
101 static mpc_status
check_streaminfo(mpc_streaminfo
* si
)
103 if (si
->max_band
== 0 || si
->max_band
>= 32
105 return MPC_STATUS_FILE
;
106 return MPC_STATUS_OK
;
109 /// Reads streaminfo from SV7 header.
111 streaminfo_read_header_sv7(mpc_streaminfo
* si
, mpc_bits_reader
* r
)
113 mpc_uint32_t frames
, last_frame_samples
;
116 frames
= (mpc_bits_read(r
, 16) << 16) | mpc_bits_read(r
, 16);
117 mpc_bits_read(r
, 1); // intensity stereo : should be 0
118 si
->ms
= mpc_bits_read(r
, 1);
119 si
->max_band
= mpc_bits_read(r
, 6);
120 si
->profile
= mpc_bits_read(r
, 4);
122 si->profile_name = mpc_get_version_string(si->profile);
124 mpc_bits_read(r
, 2); // Link ?
125 si
->sample_freq
= samplefreqs
[mpc_bits_read(r
, 2)];
126 mpc_bits_read(r
, 16); // Estimatedpeak_title
127 si
->gain_title
= (mpc_uint16_t
) mpc_bits_read(r
, 16);
128 si
->peak_title
= (mpc_uint16_t
) mpc_bits_read(r
, 16);
129 si
->gain_album
= (mpc_uint16_t
) mpc_bits_read(r
, 16);
130 si
->peak_album
= (mpc_uint16_t
) mpc_bits_read(r
, 16);
131 si
->is_true_gapless
= mpc_bits_read(r
, 1); // true gapless: used?
132 last_frame_samples
= mpc_bits_read(r
, 11); // true gapless: valid samples for last frame
133 si
->fast_seek
= mpc_bits_read(r
, 1); // fast seeking
134 mpc_bits_read(r
, 19); // unused
135 si
->encoder_version
= mpc_bits_read(r
, 8);
141 if (si->gain_title != 0) {
142 int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_title / 100.) * 256. + .5);
143 if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
144 si->gain_title = (mpc_int16_t) tmp;
147 if (si->gain_album != 0) {
148 int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_album / 100.) * 256. + .5);
149 if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
150 si->gain_album = (mpc_int16_t) tmp;
153 if (si->peak_title != 0)
154 si->peak_title = (mpc_uint16_t) (log10(si->peak_title) * 20 * 256 + .5);
156 if (si->peak_album != 0)
157 si->peak_album = (mpc_uint16_t) (log10(si->peak_album) * 20 * 256 + .5);
159 mpc_get_encoder_string(si);
162 if (last_frame_samples
== 0) last_frame_samples
= MPC_FRAME_LENGTH
;
163 si
->samples
= (mpc_int64_t
) frames
* MPC_FRAME_LENGTH
;
164 if (si
->is_true_gapless
)
165 si
->samples
-= (MPC_FRAME_LENGTH
- last_frame_samples
);
167 si
->samples
-= MPC_DECODER_SYNTH_DELAY
;
169 si
->average_bitrate
= (si
->tag_offset
- si
->header_position
) * 8.0
170 * si
->sample_freq
/ si
->samples
;
172 return check_streaminfo(si
);
175 /// Reads replay gain datas
176 void streaminfo_gain(mpc_streaminfo
* si
, const mpc_bits_reader
* r_in
)
178 mpc_bits_reader r
= *r_in
;
180 int version
= mpc_bits_read(&r
, 8); // gain version
181 if (version
!= 1) // we only know ver 1
183 si
->gain_title
= (mpc_uint16_t
) mpc_bits_read(&r
, 16);
184 si
->peak_title
= (mpc_uint16_t
) mpc_bits_read(&r
, 16);
185 si
->gain_album
= (mpc_uint16_t
) mpc_bits_read(&r
, 16);
186 si
->peak_album
= (mpc_uint16_t
) mpc_bits_read(&r
, 16);
189 /// Reads streaminfo from SV8 header.
191 streaminfo_read_header_sv8(mpc_streaminfo
* si
, const mpc_bits_reader
* r_in
,
192 mpc_size_t block_size
)
195 mpc_bits_reader r
= *r_in
;
197 CRC
= (mpc_bits_read(&r
, 16) << 16) | mpc_bits_read(&r
, 16);
198 if (CRC
!= mpc_crc32(r
.buff
+ 1 - (r
.count
>> 3), (int)block_size
- 4))
199 return MPC_STATUS_FILE
;
201 si
->stream_version
= mpc_bits_read(&r
, 8);
202 if (si
->stream_version
!= 8)
203 return MPC_STATUS_INVALIDSV
;
205 mpc_bits_get_size(&r
, &si
->samples
);
206 mpc_bits_get_size(&r
, &si
->beg_silence
);
208 si
->is_true_gapless
= 1;
209 si
->sample_freq
= samplefreqs
[mpc_bits_read(&r
, 3)];
210 si
->max_band
= mpc_bits_read(&r
, 5) + 1;
211 si
->channels
= mpc_bits_read(&r
, 4) + 1;
212 si
->ms
= mpc_bits_read(&r
, 1);
213 si
->block_pwr
= mpc_bits_read(&r
, 3) * 2;
217 if ((si
->samples
- si
->beg_silence
) != 0)
218 si
->average_bitrate
= (si
->tag_offset
- si
->header_position
) * 8.0
219 * si
->sample_freq
/ (si
->samples
- si
->beg_silence
);
221 return check_streaminfo(si
);
224 /// Reads encoder informations
225 void streaminfo_encoder_info(mpc_streaminfo
* si
, const mpc_bits_reader
* r_in
)
227 mpc_bits_reader r
= *r_in
;
229 si
->profile
= mpc_bits_read(&r
, 7) / 8.;
231 si->profile_name = mpc_get_version_string(si->profile);
233 si
->pns
= mpc_bits_read(&r
, 1);
234 si
->encoder_version
= mpc_bits_read(&r
, 8) << 24; // major
235 si
->encoder_version
|= mpc_bits_read(&r
, 8) << 16; // minor
236 si
->encoder_version
|= mpc_bits_read(&r
, 8) << 8; // build
239 mpc_get_encoder_string(si);
245 mpc_streaminfo_get_length(mpc_streaminfo * si)
247 return (double) (si->samples - si->beg_silence) / si->sample_freq;
250 mpc_int64_t mpc_streaminfo_get_length_samples(mpc_streaminfo *si)
252 return si->samples - si->beg_silence;