2 * Musepack audio files decoder for MPlayer
3 * by Reza Jelveh <reza.jelveh@tuhh.de> and
4 * Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
6 * This code may be be relicensed under the terms of the GNU LGPL when it
7 * becomes part of the FFmpeg project (ffmpeg.org)
15 #include "ad_internal.h"
16 #include "libaf/af_format.h"
17 #include "libvo/fastmemcpy.h"
19 static ad_info_t info
=
21 "Musepack audio decoder",
23 "Reza Jelveh and Reimar Döffinger",
28 LIBAD_EXTERN(libmusepack
)
30 #include <mpcdec/mpcdec.h>
32 // BUFFER_LENGTH is in MPC_SAMPLE_FORMAT units
33 #define MAX_FRAMESIZE (4 * MPC_DECODER_BUFFER_LENGTH)
34 //! this many frames should decode good after seeking
35 #define MIN_SEEK_GOOD 5
36 //! how many frames to discard at most after seeking
37 #define MAX_SEEK_DISCARD 50
39 typedef struct context_s
{
48 * \brief mpc_reader callback function for reading the header
50 static mpc_int32_t
cb_read(void *data
, void *buf
, mpc_int32_t size
) {
51 context_t
*d
= (context_t
*)data
;
52 char *p
= (char *)buf
;
54 if (d
->pos
< d
->header_len
) {
55 if (s
> d
->header_len
- d
->pos
)
56 s
= d
->header_len
- d
->pos
;
57 fast_memcpy(p
, &d
->header
[d
->pos
], s
);
60 memset(&p
[s
], 0, size
- s
);
66 * \brief dummy mpc_reader callback function for seeking
68 static mpc_bool_t
cb_seek(void *data
, mpc_int32_t offset
) {
69 context_t
*d
= (context_t
*)data
;
75 * \brief dummy mpc_reader callback function for getting stream position
77 static mpc_int32_t
cb_tell(void *data
) {
78 context_t
*d
= (context_t
*)data
;
83 * \brief dummy mpc_reader callback function for getting stream length
85 static mpc_int32_t
cb_get_size(void *data
) {
90 * \brief mpc_reader callback function, we cannot seek.
92 static mpc_bool_t
cb_canseek(void *data
) {
97 mpc_reader header_reader
= {
98 .read
= cb_read
, .seek
= cb_seek
, .tell
= cb_tell
,
99 .get_size
= cb_get_size
, .canseek
= cb_canseek
102 static int preinit(sh_audio_t
*sh
) {
103 sh
->audio_out_minsize
= MAX_FRAMESIZE
;
107 static void uninit(sh_audio_t
*sh
) {
113 static int init(sh_audio_t
*sh
) {
115 context_t
*cd
= malloc(sizeof(context_t
));
117 if (!sh
->wf
|| (sh
->wf
->cbSize
< 6 * 4)) {
118 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Missing extradata!\n");
121 cd
->header
= (char *)sh
->wf
;
122 cd
->header
= &cd
->header
[sizeof(WAVEFORMATEX
)];
123 cd
->header_len
= sh
->wf
->cbSize
;
126 sh
->context
= (char *)cd
;
128 /* read file's streaminfo data */
129 mpc_streaminfo_init(&info
);
130 header_reader
.data
= cd
;
131 if (mpc_streaminfo_read(&info
, &header_reader
) != ERROR_CODE_OK
) {
132 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Not a valid musepack file.\n");
135 // this value is nonsense, since it relies on the get_size function.
136 // use the value from the demuxer instead.
137 // sh->i_bps = info.average_bitrate / 8;
138 sh
->channels
= info
.channels
;
139 sh
->samplerate
= info
.sample_freq
;
142 #if MPC_SAMPLE_FORMAT == float
144 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
147 #error musepack lib must use either float or mpc_int32_t sample format
150 mpc_decoder_setup(&cd
->decoder
, NULL
);
151 mpc_decoder_set_streaminfo(&cd
->decoder
, &info
);
155 // FIXME: minlen is currently ignored
156 static int decode_audio(sh_audio_t
*sh
, unsigned char *buf
,
157 int minlen
, int maxlen
) {
159 MPC_SAMPLE_FORMAT
*sample_buffer
= (MPC_SAMPLE_FORMAT
*)buf
;
160 mpc_uint32_t
*packet
= NULL
;
162 context_t
*cd
= (context_t
*) sh
->context
;
163 if (maxlen
< MAX_FRAMESIZE
) {
164 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "maxlen too small in decode_audio\n");
167 len
= ds_get_packet(sh
->ds
, (unsigned char **)&packet
);
168 if (len
<= 0) return -1;
169 status
= mpc_decoder_decode_frame(&cd
->decoder
, packet
, len
, sample_buffer
);
170 if (status
== -1) // decode error
171 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Error decoding file.\n");
172 if (status
<= 0) // error or EOF
175 status
= MPC_FRAME_LENGTH
* sh
->channels
; // one sample per channel
176 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
186 * \brief check if the decoded values are in a sane range
187 * \param buf decoded buffer
188 * \param len length of buffer in bytes
189 * \return 1 if all values are in (-1.01, 1.01) range, 0 otherwise
191 static int check_clip(void *buf
, int len
) {
192 #if MPC_SAMPLE_FORMAT == float
194 if (len
< 4) return 1;
198 if (p
[len
] < -1 || p
[len
] > 1) return 0;
204 static int control(sh_audio_t
*sh
, int cmd
, void* arg
, ...) {
205 if (cmd
== ADCTRL_RESYNC_STREAM
) {
206 unsigned char *buf
= malloc(MAX_FRAMESIZE
);
209 for (i
= 0; i
< MAX_SEEK_DISCARD
; i
++) {
210 int len
= decode_audio(sh
, buf
, 0, MAX_FRAMESIZE
);
211 if (check_clip(buf
, len
)) nr_ok
++; else nr_ok
= 0;
212 if (nr_ok
> MIN_SEEK_GOOD
) break;
216 return CONTROL_UNKNOWN
;