summaryrefslogtreecommitdiff
path: root/apps/codecs/libmusepack/streaminfo.c
blob: d9c925dbfbb8d981d0776432de26582506e76a1e (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
  Copyright (c) 2005, 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 "musepack.h"
#include "internal.h"

static const char *
Stringify(mpc_uint32_t profile) // profile is 0...15, where 7...13 is used
{
    static const char na[] = "n.a.";
    static const char *Names[] = {
        na, "'Unstable/Experimental'", na, na,
        na, "'quality 0'", "'quality 1'", "'Telephone'",
        "'Thumb'", "'Radio'", "'Standard'", "'Xtreme'",
        "'Insane'", "'BrainDead'", "'quality 9'", "'quality 10'"
    };

    return profile >= sizeof(Names) / sizeof(*Names) ? na : Names[profile];
}

void
mpc_streaminfo_init(mpc_streaminfo * si)
{
    memset(si, 0, sizeof(mpc_streaminfo));
}

/// Reads streaminfo from SV7 header. 
static mpc_int32_t
streaminfo_read_header_sv7(mpc_streaminfo * si, mpc_uint32_t HeaderData[8])
{
    const mpc_int32_t samplefreqs[4] = { 44100, 48000, 37800, 32000 };

    //mpc_uint32_t    HeaderData [8];
    mpc_uint16_t Estimatedpeak_title = 0;

    if (si->stream_version > 0x71) {
        //        Update (si->stream_version);
        return 0;
    }

    /*
       if ( !fp->seek ( si->header_position ) )         // seek to header start
       return ERROR_CODE_FILE;
       if ( fp->read ( HeaderData, sizeof HeaderData) != sizeof HeaderData )
       return ERROR_CODE_FILE;
     */

    si->bitrate = 0;
    si->frames = HeaderData[1];
    si->is = 0;
    si->ms = (HeaderData[2] >> 30) & 0x0001;
    si->max_band = (HeaderData[2] >> 24) & 0x003F;
    si->block_size = 1;
    si->profile = (HeaderData[2] << 8) >> 28;
    si->profile_name = Stringify(si->profile);
    si->sample_freq = samplefreqs[(HeaderData[2] >> 16) & 0x0003];
    Estimatedpeak_title = (mpc_uint16_t) (HeaderData[2] & 0xFFFF);   // read the ReplayGain data
    si->gain_title = (mpc_uint16_t) ((HeaderData[3] >> 16) & 0xFFFF);
    si->peak_title = (mpc_uint16_t) (HeaderData[3] & 0xFFFF);
    si->gain_album = (mpc_uint16_t) ((HeaderData[4] >> 16) & 0xFFFF);
    si->peak_album = (mpc_uint16_t) (HeaderData[4] & 0xFFFF);
    si->is_true_gapless = (HeaderData[5] >> 31) & 0x0001; // true gapless: used?
    si->last_frame_samples = (HeaderData[5] >> 20) & 0x07FF;  // true gapless: valid samples for last frame
    si->encoder_version = (HeaderData[6] >> 24) & 0x00FF;

    if (si->encoder_version == 0) {
        //sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
    }
    else {
        switch (si->encoder_version % 10) {
        case 0:
            //sprintf(si->encoder, "Release %u.%u", si->encoder_version / 100,
            //        si->encoder_version / 10 % 10);
            break;
        case 2:
        case 4:
        case 6:
        case 8:
            //sprintf(si->encoder, "Beta %u.%02u", si->encoder_version / 100,
            //        si->encoder_version % 100);
            break;
        default:
            //sprintf(si->encoder, "--Alpha-- %u.%02u",
            //        si->encoder_version / 100, si->encoder_version % 100);
            break;
        }
    }

    //    if ( si->peak_title == 0 )                                      // there is no correct peak_title contained within header
    //        si->peak_title = (mpc_uint16_t)(Estimatedpeak_title * 1.18);
    //    if ( si->peak_album == 0 )
    //        si->peak_album = si->peak_title;                          // no correct peak_album, use peak_title

    //si->sample_freq    = 44100;                                     // AB: used by all files up to SV7
    si->channels = 2;

    return ERROR_CODE_OK;
}

// read information from SV4-SV6 header
#ifdef MPC_SUPPORT_SV456
static mpc_int32_t
streaminfo_read_header_sv6(mpc_streaminfo * si, mpc_uint32_t HeaderData[8])
{
    //mpc_uint32_t    HeaderData [8];

    /*
       if ( !fp->seek (  si->header_position ) )         // seek to header start
       return ERROR_CODE_FILE;
       if ( fp->read ( HeaderData, sizeof HeaderData ) != sizeof HeaderData )
       return ERROR_CODE_FILE;
     */

    si->bitrate = (HeaderData[0] >> 23) & 0x01FF;   // read the file-header (SV6 and below)
    si->is = (HeaderData[0] >> 22) & 0x0001;
    si->ms = (HeaderData[0] >> 21) & 0x0001;
    si->stream_version = (HeaderData[0] >> 11) & 0x03FF;
    si->max_band = (HeaderData[0] >> 6) & 0x001F;
    si->block_size = (HeaderData[0]) & 0x003F;
    si->profile = 0;
    si->profile_name = Stringify((mpc_uint32_t) (-1));
    if (si->stream_version >= 5)
        si->frames = HeaderData[1]; // 32 bit
    else
        si->frames = (HeaderData[1] >> 16); // 16 bit

    si->gain_title = 0;          // not supported
    si->peak_title = 0;
    si->gain_album = 0;
    si->peak_album = 0;

    si->last_frame_samples = 0;
    si->is_true_gapless = 0;

    si->encoder_version = 0;
    si->encoder[0] = '\0';

    if (si->stream_version == 7)
        return ERROR_CODE_SV7BETA;  // are there any unsupported parameters used?
    if (si->bitrate != 0)
        return ERROR_CODE_CBR;
    if (si->is != 0)
        return ERROR_CODE_IS;
    if (si->block_size != 1)
        return ERROR_CODE_BLOCKSIZE;

    if (si->stream_version < 6) // Bugfix: last frame was invalid for up to SV5
        si->frames -= 1;

    si->sample_freq = 44100;     // AB: used by all files up to SV7
    si->channels = 2;

    if (si->stream_version < 4 || si->stream_version > 7)
        return ERROR_CODE_INVALIDSV;

    return ERROR_CODE_OK;
}
#endif
// reads file header and tags
mpc_int32_t
mpc_streaminfo_read(mpc_streaminfo * si, mpc_reader * r)
{
    mpc_uint32_t HeaderData[8];
    mpc_int32_t Error = 0;

    // get header position
    if ((si->header_position = JumpID3v2(r)) < 0) {
        return ERROR_CODE_FILE;
    }
    // seek to first byte of mpc data
    if (!r->seek(r->data, si->header_position)) {
        return ERROR_CODE_FILE;
    }
    if (r->read(r->data, HeaderData, 8 * 4) != 8 * 4) {
        return ERROR_CODE_FILE;
    }
    if (!r->seek(r->data, si->header_position + 6 * 4)) {
        return ERROR_CODE_FILE;
    }

    si->total_file_length = r->get_size(r->data);
    si->tag_offset = si->total_file_length;
    if (memcmp(HeaderData, "MP+", 3) == 0) {
#ifndef MPC_LITTLE_ENDIAN
        mpc_uint32_t ptr;
        for (ptr = 0; ptr < 8; ptr++) {
            HeaderData[ptr] = mpc_swap32(HeaderData[ptr]);
        }
#endif
        si->stream_version = HeaderData[0] >> 24;

        // stream version 8
        if ((si->stream_version & 15) >= 8) {
            return ERROR_CODE_INVALIDSV;
        }
        // stream version 7
        else if ((si->stream_version & 15) == 7) {
            Error = streaminfo_read_header_sv7(si, HeaderData);
            if (Error != ERROR_CODE_OK) return Error;
        }
    } else {
#ifdef MPC_SUPPORT_SV456
#ifndef MPC_LITTLE_ENDIAN
        mpc_uint32_t ptr;
        for (ptr = 0; ptr < 8; ptr++) {
            HeaderData[ptr] = mpc_swap32(HeaderData[ptr]);
        }
#endif
        // stream version 4-6
        Error = streaminfo_read_header_sv6(si, HeaderData);
        if (Error != ERROR_CODE_OK) return Error;
#else
        return ERROR_CODE_INVALIDSV;
#endif
    }

    // estimation, exact value needs too much time
    si->pcm_samples = 1152 * si->frames - 576;

    if (si->pcm_samples > 0) {
        si->average_bitrate =
            (si->tag_offset -
             si->header_position) * 8.0 * si->sample_freq / si->pcm_samples;
    }
    else {
        si->average_bitrate = 0;
    }

    return ERROR_CODE_OK;
}

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

mpc_int64_t
mpc_streaminfo_get_length_samples(mpc_streaminfo * si)
{
    mpc_int64_t samples = (mpc_int64_t) si->frames * MPC_FRAME_LENGTH;
    if (si->is_true_gapless) {
        samples -= (MPC_FRAME_LENGTH - si->last_frame_samples);
    }
    else {
        samples -= MPC_DECODER_SYNTH_DELAY;
    }
    return samples;
}