1 /*
2   Copyright (c) 2005-2009, The Musepack Development Team
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8 
9   * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
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.
16 
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.
21 
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.
33 */
34 /// \file mpcdec.h
35 /// Top level include file for libmpcdec.
36 #ifndef _MPCDEC_H_
37 #define _MPCDEC_H_
38 #ifdef WIN32
39 #pragma once
40 #endif
41 
42 #include "reader.h"
43 #include "streaminfo.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 enum {
50     MPC_FRAME_LENGTH          = (36 * 32),              ///< Samples per mpc frame
51     MPC_DECODER_BUFFER_LENGTH = (MPC_FRAME_LENGTH * 4), ///< Required buffer size for decoder
52     MPC_DECODER_SYNTH_DELAY   = 481
53 };
54 
55 typedef struct mpc_decoder_t mpc_decoder;
56 typedef struct mpc_demux_t mpc_demux;
57 
58 typedef struct mpc_bits_reader_t {
59 	unsigned char * buff; /// pointer on current byte
60 	unsigned int count; /// unread bits in current byte
61 } mpc_bits_reader;
62 
63 typedef struct mpc_frame_info_t {
64 	mpc_uint32_t samples;		/// number of samples in the frame (counting once for multiple channels)
65 	mpc_int32_t bits;			/// number of bits consumed by this frame (-1) if end of stream
66 	MPC_SAMPLE_FORMAT * buffer;	/// frame samples buffer (size = samples * channels * sizeof(MPC_SAMPLE_FORMAT))
67 	mpc_bool_t is_key_frame; 	/// 1 if this frame is a key frame (first in block) 0 else. Set by the demuxer.
68 } mpc_frame_info;
69 
70 typedef struct mpc_chap_info_t {
71 	mpc_uint64_t sample;	/// sample where the chapter starts
72 	mpc_uint16_t gain;		/// replaygain chapter value
73 	mpc_uint16_t peak;		/// peak chapter loudness level
74 	mpc_uint_t tag_size;	/// size of the tag element (0 if no tag is present for this chapter)
75 	char * tag;				/// pointer to an APEv2 tag without the preamble
76 } mpc_chap_info;
77 
78 /// Initializes mpc decoder with the supplied stream info parameters.
79 /// \param si streaminfo structure indicating format of source stream
80 /// \return pointer on the initialized decoder structure if successful, 0 if not
81 MPC_API mpc_decoder * mpc_decoder_init(mpc_streaminfo *si);
82 
83 /// Releases input mpc decoder
84 MPC_API void mpc_decoder_exit(mpc_decoder *p_dec);
85 
86 /**
87  * Sets decoder sample scaling factor.  All decoded samples will be multiplied
88  * by this factor. Useful for applying replay gain.
89  * @param scale_factor multiplicative scaling factor
90  */
91 MPC_API void mpc_decoder_scale_output(mpc_decoder *p_dec, double scale_factor);
92 
93 MPC_API void mpc_decoder_decode_frame(mpc_decoder * d, mpc_bits_reader * r, mpc_frame_info * i);
94 
95 // This is the gain reference used in old replaygain
96 #define MPC_OLD_GAIN_REF 64.82
97 
98 /**
99  * init demuxer
100  * @param p_reader initialized mpc_reader pointer
101  * @return an initialized mpc_demux pointer
102  */
103 MPC_API mpc_demux * mpc_demux_init(mpc_reader * p_reader);
104 /// free demuxer
105 MPC_API void mpc_demux_exit(mpc_demux * d);
106 /**
107  * Calls mpc_decoder_scale_output to set the scaling factor according to the
108  * replay gain stream information and the supplied ouput level
109  * @param d pointer to a musepack demuxer
110  * @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
111  * @param use_gain set it to MPC_TRUE if you want to set the scaling factor according to the stream gain
112  * @param use_title MPC_TRUE : uses the title gain, MPC_FALSE : uses the album gain
113  * @param clip_prevention MPC_TRUE : uses cliping prevention
114  */
115 MPC_API void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain,
116                           mpc_bool_t use_title, mpc_bool_t clip_prevention);
117 /// decode frame
118 MPC_API mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i);
119 /// get streaminfo
120 MPC_API void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i);
121 /// seeks to a given sample
122 MPC_API mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample);
123 /// seeks to a given second
124 MPC_API mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds);
125 
126 /// \return the current position in the stream (in bits) from the beginning of the file
127 MPC_API mpc_seek_t mpc_demux_pos(mpc_demux * d);
128 
129 /// chapters : only for sv8 streams
130 /**
131  * Gets the number of chapters in the stream
132  * @param d pointer to a musepack demuxer
133  * @return the number of chapters found in the stream
134  */
135 MPC_API mpc_int_t mpc_demux_chap_nb(mpc_demux * d);
136 /**
137  * Gets datas associated to a given chapter
138  * The chapter tag is an APEv2 tag without the preamble
139  * @param d pointer to a musepack demuxer
140  * @param chap_nb chapter number you want datas (from 0 to mpc_demux_chap_nb(d) - 1)
141  * @return the chapter information structure
142  */
143 MPC_API mpc_chap_info const * mpc_demux_chap(mpc_demux * d, int chap_nb);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 #endif
149