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
) {
128 static int init(sh_audio_t
*sh
) {
130 context_t
*cd
= malloc(sizeof(context_t
));
132 if (!sh
->wf
|| (sh
->wf
->cbSize
< 6 * 4)) {
133 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Missing extradata!\n");
136 cd
->header
= (char *)(sh
->wf
+ 1);
137 cd
->header_len
= sh
->wf
->cbSize
;
140 sh
->context
= (char *)cd
;
142 /* read file's streaminfo data */
143 mpc_streaminfo_init(&info
);
144 header_reader
.data
= cd
;
145 if (mpc_streaminfo_read(&info
, &header_reader
) != ERROR_CODE_OK
) {
146 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Not a valid musepack file.\n");
149 // this value is nonsense, since it relies on the get_size function.
150 // use the value from the demuxer instead.
151 // sh->i_bps = info.average_bitrate / 8;
152 sh
->channels
= info
.channels
;
153 sh
->samplerate
= info
.sample_freq
;
156 #if MPC_SAMPLE_FORMAT == float
158 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
161 #error musepack lib must use either float or mpc_int32_t sample format
164 mpc_decoder_setup(&cd
->decoder
, NULL
);
165 mpc_decoder_set_streaminfo(&cd
->decoder
, &info
);
169 // FIXME: minlen is currently ignored
170 static int decode_audio(sh_audio_t
*sh
, unsigned char *buf
,
171 int minlen
, int maxlen
) {
173 MPC_SAMPLE_FORMAT
*sample_buffer
= (MPC_SAMPLE_FORMAT
*)buf
;
174 mpc_uint32_t
*packet
= NULL
;
176 context_t
*cd
= (context_t
*) sh
->context
;
177 if (maxlen
< MAX_FRAMESIZE
) {
178 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "maxlen too small in decode_audio\n");
181 len
= ds_get_packet(sh
->ds
, (unsigned char **)&packet
);
182 if (len
<= 0) return -1;
183 status
= mpc_decoder_decode_frame(&cd
->decoder
, packet
, len
, sample_buffer
);
184 if (status
== -1) // decode error
185 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Error decoding file.\n");
186 if (status
<= 0) // error or EOF
189 status
= MPC_FRAME_LENGTH
* sh
->channels
; // one sample per channel
190 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
200 * \brief check if the decoded values are in a sane range
201 * \param buf decoded buffer
202 * \param len length of buffer in bytes
203 * \return 1 if all values are in (-1.01, 1.01) range, 0 otherwise
205 static int check_clip(void *buf
, int len
) {
206 #if MPC_SAMPLE_FORMAT == float
208 if (len
< 4) return 1;
212 if (p
[len
] < -1 || p
[len
] > 1) return 0;
218 static int control(sh_audio_t
*sh
, int cmd
, void* arg
, ...) {
219 if (cmd
== ADCTRL_RESYNC_STREAM
) {
220 unsigned char *buf
= malloc(MAX_FRAMESIZE
);
223 for (i
= 0; i
< MAX_SEEK_DISCARD
; i
++) {
224 int len
= decode_audio(sh
, buf
, 0, MAX_FRAMESIZE
);
225 if (check_clip(buf
, len
)) nr_ok
++; else nr_ok
= 0;
226 if (nr_ok
> MIN_SEEK_GOOD
) break;
230 return CONTROL_UNKNOWN
;