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)
9 * This file is part of MPlayer.
11 * MPlayer is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * MPlayer is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "ad_internal.h"
32 #include "libaf/af_format.h"
33 #include "libvo/fastmemcpy.h"
35 static const ad_info_t info
=
37 "Musepack audio decoder",
39 "Reza Jelveh and Reimar Döffinger",
44 LIBAD_EXTERN(libmusepack
)
46 #include <mpcdec/mpcdec.h>
48 // BUFFER_LENGTH is in MPC_SAMPLE_FORMAT units
49 #define MAX_FRAMESIZE (4 * MPC_DECODER_BUFFER_LENGTH)
50 //! this many frames should decode good after seeking
51 #define MIN_SEEK_GOOD 5
52 //! how many frames to discard at most after seeking
53 #define MAX_SEEK_DISCARD 50
55 typedef struct context_s
{
64 * \brief mpc_reader callback function for reading the header
66 static mpc_int32_t
cb_read(void *data
, void *buf
, mpc_int32_t size
) {
67 context_t
*d
= (context_t
*)data
;
68 char *p
= (char *)buf
;
70 if (d
->pos
< d
->header_len
) {
71 if (s
> d
->header_len
- d
->pos
)
72 s
= d
->header_len
- d
->pos
;
73 fast_memcpy(p
, &d
->header
[d
->pos
], s
);
76 memset(&p
[s
], 0, size
- s
);
82 * \brief dummy mpc_reader callback function for seeking
84 static mpc_bool_t
cb_seek(void *data
, mpc_int32_t offset
) {
85 context_t
*d
= (context_t
*)data
;
91 * \brief dummy mpc_reader callback function for getting stream position
93 static mpc_int32_t
cb_tell(void *data
) {
94 context_t
*d
= (context_t
*)data
;
99 * \brief dummy mpc_reader callback function for getting stream length
101 static mpc_int32_t
cb_get_size(void *data
) {
106 * \brief mpc_reader callback function, we cannot seek.
108 static mpc_bool_t
cb_canseek(void *data
) {
113 mpc_reader header_reader
= {
114 .read
= cb_read
, .seek
= cb_seek
, .tell
= cb_tell
,
115 .get_size
= cb_get_size
, .canseek
= cb_canseek
118 static int preinit(sh_audio_t
*sh
) {
119 sh
->audio_out_minsize
= MAX_FRAMESIZE
;
123 static void uninit(sh_audio_t
*sh
) {
129 static int init(sh_audio_t
*sh
) {
131 context_t
*cd
= malloc(sizeof(context_t
));
133 if (!sh
->wf
|| (sh
->wf
->cbSize
< 6 * 4)) {
134 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Missing extradata!\n");
137 cd
->header
= (char *)sh
->wf
;
138 cd
->header
= &cd
->header
[sizeof(WAVEFORMATEX
)];
139 cd
->header_len
= sh
->wf
->cbSize
;
142 sh
->context
= (char *)cd
;
144 /* read file's streaminfo data */
145 mpc_streaminfo_init(&info
);
146 header_reader
.data
= cd
;
147 if (mpc_streaminfo_read(&info
, &header_reader
) != ERROR_CODE_OK
) {
148 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Not a valid musepack file.\n");
151 // this value is nonsense, since it relies on the get_size function.
152 // use the value from the demuxer instead.
153 // sh->i_bps = info.average_bitrate / 8;
154 sh
->channels
= info
.channels
;
155 sh
->samplerate
= info
.sample_freq
;
158 #if MPC_SAMPLE_FORMAT == float
160 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
163 #error musepack lib must use either float or mpc_int32_t sample format
166 mpc_decoder_setup(&cd
->decoder
, NULL
);
167 mpc_decoder_set_streaminfo(&cd
->decoder
, &info
);
171 // FIXME: minlen is currently ignored
172 static int decode_audio(sh_audio_t
*sh
, unsigned char *buf
,
173 int minlen
, int maxlen
) {
175 MPC_SAMPLE_FORMAT
*sample_buffer
= (MPC_SAMPLE_FORMAT
*)buf
;
176 mpc_uint32_t
*packet
= NULL
;
178 context_t
*cd
= (context_t
*) sh
->context
;
179 if (maxlen
< MAX_FRAMESIZE
) {
180 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "maxlen too small in decode_audio\n");
183 len
= ds_get_packet(sh
->ds
, (unsigned char **)&packet
);
184 if (len
<= 0) return -1;
185 status
= mpc_decoder_decode_frame(&cd
->decoder
, packet
, len
, sample_buffer
);
186 if (status
== -1) // decode error
187 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Error decoding file.\n");
188 if (status
<= 0) // error or EOF
191 status
= MPC_FRAME_LENGTH
* sh
->channels
; // one sample per channel
192 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
202 * \brief check if the decoded values are in a sane range
203 * \param buf decoded buffer
204 * \param len length of buffer in bytes
205 * \return 1 if all values are in (-1.01, 1.01) range, 0 otherwise
207 static int check_clip(void *buf
, int len
) {
208 #if MPC_SAMPLE_FORMAT == float
210 if (len
< 4) return 1;
214 if (p
[len
] < -1 || p
[len
] > 1) return 0;
220 static int control(sh_audio_t
*sh
, int cmd
, void* arg
, ...) {
221 if (cmd
== ADCTRL_RESYNC_STREAM
) {
222 unsigned char *buf
= malloc(MAX_FRAMESIZE
);
225 for (i
= 0; i
< MAX_SEEK_DISCARD
; i
++) {
226 int len
= decode_audio(sh
, buf
, 0, MAX_FRAMESIZE
);
227 if (check_clip(buf
, len
)) nr_ok
++; else nr_ok
= 0;
228 if (nr_ok
> MIN_SEEK_GOOD
) break;
232 return CONTROL_UNKNOWN
;