summaryrefslogtreecommitdiff
path: root/plugins/musepack/streaminfo.c
blob: a1fedb9da438ffa175cc8aafc52c0500524f7425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
  Copyright (c) 2005-2009, The Musepack Development Team
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

  * Neither the name of the The Musepack Development Team nor the
  names of its contributors may be used to endorse or promote
  products derived from this software without specific prior
  written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/// \file streaminfo.c
/// Implementation of streaminfo reading functions.

#include <math.h>
#include <mpc/mpcdec.h>
#include <mpc/streaminfo.h>
#include <stdio.h>
#include "internal.h"
#include "huffman.h"
#include "mpc_bits_reader.h"

unsigned long crc32(unsigned char *buf, int len);

static const char na[] = "n.a.";
static char const * const versionNames[] = {
    na, "'Unstable/Experimental'", na, na, na, "'quality 0'", "'quality 1'",
    "'Telephone'", "'Thumb'", "'Radio'", "'Standard'", "'Extreme'", "'Insane'",
    "'BrainDead'", "'quality 9'", "'quality 10'"
};
static const mpc_int32_t samplefreqs[8] = { 44100, 48000, 37800, 32000 };

static const char *
mpc_get_version_string(float profile) // profile is 0...15, where 7...13 is used
{
	return profile >= sizeof versionNames / sizeof *versionNames ? na : versionNames[(int)profile];
}

static void
mpc_get_encoder_string(mpc_streaminfo* si)
{
	int ver = si->encoder_version;
	if (si->stream_version >= 8)
		ver = (si->encoder_version >> 24) * 100 + ((si->encoder_version >> 16) & 0xFF);
	if (ver <= 116) {
		if (ver == 0) {
			sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
		} else {
			switch (ver % 10) {
				case 0:
					sprintf(si->encoder, "Release %u.%u", ver / 100,
							ver / 10 % 10);
					break;
				case 2: case 4: case 6: case 8:
					sprintf(si->encoder, "Beta %u.%02u", ver / 100,
							ver % 100);
					break;
				default:
					sprintf(si->encoder, "--Alpha-- %u.%02u",
							ver / 100, ver % 100);
					break;
			}
		}
	} else {
		int major = si->encoder_version >> 24;
		int minor = (si->encoder_version >> 16) & 0xFF;
		int build = (si->encoder_version >> 8) & 0xFF;
		char * tmp = "--Stable--";

		if (minor & 1)
			tmp = "--Unstable--";

		sprintf(si->encoder, "%s %u.%u.%u", tmp, major, minor, build);
	}
}

static mpc_status check_streaminfo(mpc_streaminfo * si)
{
	if (si->max_band == 0 || si->max_band >= 32
	    || si->channels > 2)
		return MPC_STATUS_FILE;
	return MPC_STATUS_OK;
}

/// Reads streaminfo from SV7 header.
mpc_status
streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_bits_reader * r)
{
    mpc_uint16_t Estimatedpeak_title = 0;
	mpc_uint32_t frames, last_frame_samples;

	si->bitrate            = 0;
	frames                 = (mpc_bits_read(r, 16) << 16) | mpc_bits_read(r, 16);
	mpc_bits_read(r, 1); // intensity stereo : should be 0
	si->ms                 = mpc_bits_read(r, 1);
	si->max_band           = mpc_bits_read(r, 6);
	si->profile            = mpc_bits_read(r, 4);
	si->profile_name       = mpc_get_version_string(si->profile);
	mpc_bits_read(r, 2); // Link ?
	si->sample_freq        = samplefreqs[mpc_bits_read(r, 2)];
	Estimatedpeak_title    = (mpc_uint16_t) mpc_bits_read(r, 16);   // read the ReplayGain data
	si->gain_title         = (mpc_uint16_t) mpc_bits_read(r, 16);
	si->peak_title         = (mpc_uint16_t) mpc_bits_read(r, 16);
	si->gain_album         = (mpc_uint16_t) mpc_bits_read(r, 16);
	si->peak_album         = (mpc_uint16_t) mpc_bits_read(r, 16);
	si->is_true_gapless    = mpc_bits_read(r, 1); // true gapless: used?
	last_frame_samples     = mpc_bits_read(r, 11); // true gapless: valid samples for last frame
	si->fast_seek          = mpc_bits_read(r, 1); // fast seeking
	mpc_bits_read(r, 19); // unused
	si->encoder_version    = mpc_bits_read(r, 8);
    si->channels           = 2;
	si->block_pwr          = 0;

	// convert gain info
	if (si->gain_title != 0) {
		int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_title / 100.) * 256. + .5);
		if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
		si->gain_title = (mpc_int16_t) tmp;
	}

	if (si->gain_album != 0) {
		int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_album / 100.) * 256. + .5);
		if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
		si->gain_album = (mpc_int16_t) tmp;
	}

	if (si->peak_title != 0)
 		si->peak_title = (mpc_uint16_t) (log10(si->peak_title) * 20 * 256 + .5);

	if (si->peak_album != 0)
		si->peak_album = (mpc_uint16_t) (log10(si->peak_album) * 20 * 256 + .5);

	mpc_get_encoder_string(si);

	if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
	si->samples = (mpc_int64_t) frames * MPC_FRAME_LENGTH;
	if (si->is_true_gapless)
		si->samples -= (MPC_FRAME_LENGTH - last_frame_samples);
	else
		si->samples -= MPC_DECODER_SYNTH_DELAY;

	si->average_bitrate = (si->tag_offset  - si->header_position) * 8.0
			*  si->sample_freq / si->samples;

	return check_streaminfo(si);
}

/// Reads replay gain datas
void  streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in)
{
	mpc_bits_reader r = *r_in;

	int version = mpc_bits_read(&r, 8); // gain version
	if (version != 1) // we only know ver 1
		return;
	si->gain_title         = (mpc_uint16_t) mpc_bits_read(&r, 16);
	si->peak_title         = (mpc_uint16_t) mpc_bits_read(&r, 16);
	si->gain_album         = (mpc_uint16_t) mpc_bits_read(&r, 16);
	si->peak_album         = (mpc_uint16_t) mpc_bits_read(&r, 16);
}

/// Reads streaminfo from SV8 header.
mpc_status
streaminfo_read_header_sv8(mpc_streaminfo* si, const mpc_bits_reader * r_in,
						   mpc_size_t block_size)
{
	mpc_uint32_t CRC;
	mpc_bits_reader r = *r_in;

	CRC = (mpc_bits_read(&r, 16) << 16) | mpc_bits_read(&r, 16);
	if (CRC != crc32(r.buff + 1 - (r.count >> 3), (int)block_size - 4))
		return MPC_STATUS_FILE;

	si->stream_version = mpc_bits_read(&r, 8);
	if (si->stream_version != 8)
		return MPC_STATUS_INVALIDSV;

	mpc_bits_get_size(&r, &si->samples);
	mpc_bits_get_size(&r, &si->beg_silence);

	si->is_true_gapless = 1;
	si->sample_freq = samplefreqs[mpc_bits_read(&r, 3)];
	si->max_band = mpc_bits_read(&r, 5) + 1;
	si->channels = mpc_bits_read(&r, 4) + 1;
	si->ms = mpc_bits_read(&r, 1);
	si->block_pwr = mpc_bits_read(&r, 3) * 2;

	si->bitrate = 0;

	if ((si->samples - si->beg_silence) != 0)
		si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
				*  si->sample_freq / (si->samples - si->beg_silence);

	return check_streaminfo(si);
}

/// Reads encoder informations
void  streaminfo_encoder_info(mpc_streaminfo* si, const mpc_bits_reader * r_in)
{
	mpc_bits_reader r = *r_in;

	si->profile            = mpc_bits_read(&r, 7) / 8.;
	si->profile_name       = mpc_get_version_string(si->profile);
	si->pns                = mpc_bits_read(&r, 1);
	si->encoder_version = mpc_bits_read(&r, 8) << 24; // major
	si->encoder_version |= mpc_bits_read(&r, 8) << 16; // minor
	si->encoder_version |= mpc_bits_read(&r, 8) << 8; // build


	mpc_get_encoder_string(si);
}

double
mpc_streaminfo_get_length(mpc_streaminfo * si)
{
	return (double) (si->samples - si->beg_silence) / si->sample_freq;
}

mpc_int64_t mpc_streaminfo_get_length_samples(mpc_streaminfo *si)
{
	return si->samples - si->beg_silence;
}