summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/codecs/libmusepack/mpcdec.h
blob: 02003cb01b6c5f9bcb3dccc9cbc96a7ebc465f17 (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
/*
  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 mpcdec.h
/// Top level include file for libmpcdec.
#ifndef _MPCDEC_H_
#define _MPCDEC_H_
#ifdef WIN32
#pragma once
#endif

#include "platform.h"
#include "reader.h"
#include "streaminfo.h"

#ifdef __cplusplus
extern "C" {
#endif

#if   (CONFIG_CPU == MCF5250)
/* Enough IRAM but performance suffers with ICODE_ATTR. */
#define IBSS_ATTR_MPC_LARGE_IRAM   IBSS_ATTR
#define ICODE_ATTR_MPC_LARGE_IRAM
#define ICONST_ATTR_MPC_LARGE_IRAM ICONST_ATTR
/* Does not fit into IRAM. */
#define IBSS_ATTR_MPC_BITBUFFER
#define ICODE_ATTR_MPC_SV8_BS_DEC
/* Keep the data arrays of bitsreadr.c in IRAM. */
#define ICONST_ATTR_MPC_BITSREADER ICONST_ATTR

#elif (CONFIG_CPU == PP5022) || (CONFIG_CPU == PP5024)
/* Enough IRAM to move additional data and code to it. */
#define IBSS_ATTR_MPC_LARGE_IRAM   IBSS_ATTR
#define ICODE_ATTR_MPC_LARGE_IRAM  ICODE_ATTR
#define ICONST_ATTR_MPC_LARGE_IRAM ICONST_ATTR
/* Does not fit into IRAM. */
#define IBSS_ATTR_MPC_BITBUFFER
#define ICODE_ATTR_MPC_SV8_BS_DEC
/* Not putting the data arrays of bitsreader.c to IRAM allows to move the
 * sv7/sv8 bitstream demuxing into IRAM. This config is faster. */
#define ICONST_ATTR_MPC_BITSREADER

#elif defined(CPU_S5L870X)
/* Enough IRAM to move additional data and code to it. */
#define IBSS_ATTR_MPC_LARGE_IRAM   IBSS_ATTR
#define ICODE_ATTR_MPC_LARGE_IRAM  ICODE_ATTR
#define ICONST_ATTR_MPC_LARGE_IRAM ICONST_ATTR
/* Faster when moved to IRAM. */
#define IBSS_ATTR_MPC_BITBUFFER    IBSS_ATTR
#define ICODE_ATTR_MPC_SV8_BS_DEC  ICODE_ATTR
/* Not faster when moved to IRAM. */
#define ICONST_ATTR_MPC_BITSREADER

#else
/* Not enough IRAM available. */
#define IBSS_ATTR_MPC_LARGE_IRAM
#define ICODE_ATTR_MPC_LARGE_IRAM
#define ICONST_ATTR_MPC_LARGE_IRAM
#define IBSS_ATTR_MPC_BITBUFFER
#define ICODE_ATTR_MPC_SV8_BS_DEC
#define ICONST_ATTR_MPC_BITSREADER
#endif

enum {
    MPC_FRAME_LENGTH          = (36 * 32),              ///< Samples per mpc frame
    MPC_DECODER_BUFFER_LENGTH = (MPC_FRAME_LENGTH * 2), ///< Required buffer size for decoder (2 channels)
    MPC_DECODER_SYNTH_DELAY   = 481
};

typedef struct mpc_decoder_t mpc_decoder;
typedef struct mpc_demux_t mpc_demux;

typedef struct mpc_bits_reader_t {
    unsigned char * buff;       /// pointer on current byte
    unsigned int count;         /// unread bits in current byte
    mpc_uint8_t *buffered_addr; /// used for rockbox Coldfire optimization only
    mpc_uint32_t buffered_code; /// used for rockbox Coldfire optimization only
} mpc_bits_reader;

typedef struct mpc_frame_info_t {
    mpc_uint32_t samples;       /// number of samples in the frame (counting once for multiple channels)
    mpc_int32_t bits;           /// number of bits consumed by this frame (-1) if end of stream
    MPC_SAMPLE_FORMAT * buffer; /// frame samples buffer (size = samples * channels * sizeof(MPC_SAMPLE_FORMAT))
    mpc_bool_t is_key_frame;    /// 1 if this frame is a key frame (first in block) 0 else. Set by the demuxer.
} mpc_frame_info;

/* rockbox: not used
typedef struct mpc_chap_info_t {
    mpc_uint64_t sample;    /// sample where the chapter starts
    mpc_uint16_t gain;      /// replaygain chapter value
    mpc_uint16_t peak;      /// peak chapter loudness level
    mpc_uint_t tag_size;    /// size of the tag element (0 if no tag is present for this chapter)
    char * tag;             /// pointer to an APEv2 tag without the preamble
} mpc_chap_info;
*/

/// Initializes mpc decoder with the supplied stream info parameters.
/// \param si streaminfo structure indicating format of source stream
/// \return pointer on the initialized decoder structure if successful, 0 if not
MPC_API mpc_decoder * mpc_decoder_init(mpc_streaminfo *si);

/* rockbox: not used
/// Releases input mpc decoder
MPC_API void mpc_decoder_exit(mpc_decoder *p_dec);
*/

/**
 * Sets decoder sample scaling factor.  All decoded samples will be multiplied
 * by this factor. Useful for applying replay gain.
 * @param scale_factor multiplicative scaling factor
 */
/* rockbox: changed to static
MPC_API void mpc_decoder_scale_output(mpc_decoder *p_dec, double scale_factor);
*/

MPC_API void mpc_decoder_decode_frame(mpc_decoder * d, mpc_bits_reader * r, mpc_frame_info * i);

// This is the gain reference used in old replaygain
#define MPC_OLD_GAIN_REF 64.82

/**
 * init demuxer
 * @param p_reader initialized mpc_reader pointer
 * @return an initialized mpc_demux pointer
 */
MPC_API mpc_demux * mpc_demux_init(mpc_reader * p_reader);
/* rockbox: not used
/// free demuxer
MPC_API void mpc_demux_exit(mpc_demux * d);
*/
/**
 * Calls mpc_decoder_scale_output to set the scaling factor according to the
 * replay gain stream information and the supplied ouput level
 * @param d pointer to a musepack demuxer
 * @param level the desired ouput level (in db). Must be MPC_OLD_GAIN_REF (64.82 db) if you want to get the old replaygain behavior
 * @param use_gain set it to MPC_TRUE if you want to set the scaling factor according to the stream gain
 * @param use_title MPC_TRUE : uses the title gain, MPC_FALSE : uses the album gain
 * @param clip_prevention MPC_TRUE : uses cliping prevention
 */
/* rockbox: not used
MPC_API void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain,
                          mpc_bool_t use_title, mpc_bool_t clip_prevention);
*/
/// decode frame
MPC_API mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i);
/// get streaminfo
MPC_API void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i);
/// seeks to a given sample
MPC_API mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample);
/* rockbox: not used
/// seeks to a given second
MPC_API mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds);
*/

/* rockbox: keep static
/// \return the current position in the stream (in bits) from the beginning of the file
MPC_API mpc_seek_t mpc_demux_pos(mpc_demux * d);
*/

/// chapters : only for sv8 streams
/**
 * Gets the number of chapters in the stream
 * @param d pointer to a musepack demuxer
 * @return the number of chapters found in the stream
 */
/* rockbox: not used
MPC_API mpc_int_t mpc_demux_chap_nb(mpc_demux * d);
*/
/**
 * Gets datas associated to a given chapter
 * The chapter tag is an APEv2 tag without the preamble
 * @param d pointer to a musepack demuxer
 * @param chap_nb chapter number you want datas (from 0 to mpc_demux_chap_nb(d) - 1)
 * @return the chapter information structure
 */
/* rockbox: not used
MPC_API mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb);
*/
#ifdef __cplusplus
}
#endif
#endif