Put decoder array into IRAM. Improves Coldfire (h300) performance 218MHz ->102.84...
[kugel-rb.git] / apps / codecs / libmusepack / streaminfo.c
blob2d8ece704ac473ccfdb46b2cfaa83e1d0f5b00de
1 /*
2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
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
20 written permission.
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.
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"
45 /* rockbox: not used
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 };
54 /* rockbox: not used
55 static const char *
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];
61 /* rockbox: not used
62 static void
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);
68 if (ver <= 116) {
69 if (ver == 0) {
70 sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
71 } else {
72 switch (ver % 10) {
73 case 0:
74 sprintf(si->encoder, "Release %u.%u", ver / 100,
75 ver / 10 % 10);
76 break;
77 case 2: case 4: case 6: case 8:
78 sprintf(si->encoder, "Beta %u.%02u", ver / 100,
79 ver % 100);
80 break;
81 default:
82 sprintf(si->encoder, "--Alpha-- %u.%02u",
83 ver / 100, ver % 100);
84 break;
87 } else {
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--";
93 if (minor & 1)
94 tmp = "--Unstable--";
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
104 || si->channels > 2)
105 return MPC_STATUS_FILE;
106 return MPC_STATUS_OK;
109 /// Reads streaminfo from SV7 header.
110 mpc_status
111 streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_bits_reader * r)
113 mpc_uint16_t Estimatedpeak_title = 0;
114 mpc_uint32_t frames, last_frame_samples;
116 si->bitrate = 0;
117 frames = (mpc_bits_read(r, 16) << 16) | mpc_bits_read(r, 16);
118 mpc_bits_read(r, 1); // intensity stereo : should be 0
119 si->ms = mpc_bits_read(r, 1);
120 si->max_band = mpc_bits_read(r, 6);
121 si->profile = mpc_bits_read(r, 4);
122 /* rockbox: not used
123 si->profile_name = mpc_get_version_string(si->profile);
125 mpc_bits_read(r, 2); // Link ?
126 si->sample_freq = samplefreqs[mpc_bits_read(r, 2)];
127 Estimatedpeak_title = (mpc_uint16_t) mpc_bits_read(r, 16); // read the ReplayGain data
128 si->gain_title = (mpc_uint16_t) mpc_bits_read(r, 16);
129 si->peak_title = (mpc_uint16_t) mpc_bits_read(r, 16);
130 si->gain_album = (mpc_uint16_t) mpc_bits_read(r, 16);
131 si->peak_album = (mpc_uint16_t) mpc_bits_read(r, 16);
132 si->is_true_gapless = mpc_bits_read(r, 1); // true gapless: used?
133 last_frame_samples = mpc_bits_read(r, 11); // true gapless: valid samples for last frame
134 si->fast_seek = mpc_bits_read(r, 1); // fast seeking
135 mpc_bits_read(r, 19); // unused
136 si->encoder_version = mpc_bits_read(r, 8);
137 si->channels = 2;
138 si->block_pwr = 0;
140 /* rockbox: not used
141 // convert gain info
142 if (si->gain_title != 0) {
143 int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_title / 100.) * 256. + .5);
144 if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
145 si->gain_title = (mpc_int16_t) tmp;
148 if (si->gain_album != 0) {
149 int tmp = (int)((MPC_OLD_GAIN_REF - (mpc_int16_t)si->gain_album / 100.) * 256. + .5);
150 if (tmp >= (1 << 16) || tmp < 0) tmp = 0;
151 si->gain_album = (mpc_int16_t) tmp;
154 if (si->peak_title != 0)
155 si->peak_title = (mpc_uint16_t) (log10(si->peak_title) * 20 * 256 + .5);
157 if (si->peak_album != 0)
158 si->peak_album = (mpc_uint16_t) (log10(si->peak_album) * 20 * 256 + .5);
160 mpc_get_encoder_string(si);
163 if (last_frame_samples == 0) last_frame_samples = MPC_FRAME_LENGTH;
164 si->samples = (mpc_int64_t) frames * MPC_FRAME_LENGTH;
165 if (si->is_true_gapless)
166 si->samples -= (MPC_FRAME_LENGTH - last_frame_samples);
167 else
168 si->samples -= MPC_DECODER_SYNTH_DELAY;
170 si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
171 * si->sample_freq / si->samples;
173 return check_streaminfo(si);
176 /// Reads replay gain datas
177 void streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in)
179 mpc_bits_reader r = *r_in;
181 int version = mpc_bits_read(&r, 8); // gain version
182 if (version != 1) // we only know ver 1
183 return;
184 si->gain_title = (mpc_uint16_t) mpc_bits_read(&r, 16);
185 si->peak_title = (mpc_uint16_t) mpc_bits_read(&r, 16);
186 si->gain_album = (mpc_uint16_t) mpc_bits_read(&r, 16);
187 si->peak_album = (mpc_uint16_t) mpc_bits_read(&r, 16);
190 /// Reads streaminfo from SV8 header.
191 mpc_status
192 streaminfo_read_header_sv8(mpc_streaminfo* si, const mpc_bits_reader * r_in,
193 mpc_size_t block_size)
195 mpc_uint32_t CRC;
196 mpc_bits_reader r = *r_in;
198 CRC = (mpc_bits_read(&r, 16) << 16) | mpc_bits_read(&r, 16);
199 if (CRC != mpc_crc32(r.buff + 1 - (r.count >> 3), (int)block_size - 4))
200 return MPC_STATUS_FILE;
202 si->stream_version = mpc_bits_read(&r, 8);
203 if (si->stream_version != 8)
204 return MPC_STATUS_INVALIDSV;
206 mpc_bits_get_size(&r, &si->samples);
207 mpc_bits_get_size(&r, &si->beg_silence);
209 si->is_true_gapless = 1;
210 si->sample_freq = samplefreqs[mpc_bits_read(&r, 3)];
211 si->max_band = mpc_bits_read(&r, 5) + 1;
212 si->channels = mpc_bits_read(&r, 4) + 1;
213 si->ms = mpc_bits_read(&r, 1);
214 si->block_pwr = mpc_bits_read(&r, 3) * 2;
216 si->bitrate = 0;
218 if ((si->samples - si->beg_silence) != 0)
219 si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
220 * si->sample_freq / (si->samples - si->beg_silence);
222 return check_streaminfo(si);
225 /// Reads encoder informations
226 void streaminfo_encoder_info(mpc_streaminfo* si, const mpc_bits_reader * r_in)
228 mpc_bits_reader r = *r_in;
230 si->profile = mpc_bits_read(&r, 7) / 8.;
231 /* rockbox: not used
232 si->profile_name = mpc_get_version_string(si->profile);
234 si->pns = mpc_bits_read(&r, 1);
235 si->encoder_version = mpc_bits_read(&r, 8) << 24; // major
236 si->encoder_version |= mpc_bits_read(&r, 8) << 16; // minor
237 si->encoder_version |= mpc_bits_read(&r, 8) << 8; // build
239 /* rockbox: not used
240 mpc_get_encoder_string(si);
244 /* rockbox: not used
245 double
246 mpc_streaminfo_get_length(mpc_streaminfo * si)
248 return (double) (si->samples - si->beg_silence) / si->sample_freq;
251 mpc_int64_t mpc_streaminfo_get_length_samples(mpc_streaminfo *si)
253 return si->samples - si->beg_silence;