1 /*
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>
5 *
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)
8 *
9 * This file is part of MPlayer.
10 *
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.
15 *
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.
20 *
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.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include "config.h"
31 #include "mp_msg.h"
32 #include "ad_internal.h"
33 #include "libaf/af_format.h"
34 #include "libvo/fastmemcpy.h"
35
36 static const ad_info_t info =
37 {
38 "Musepack audio decoder",
39 "mpcdec",
40 "Reza Jelveh and Reimar Döffinger",
41 "",
42 ""
43 };
44
45 LIBAD_EXTERN(libmusepack)
46
47 #include <mpcdec/mpcdec.h>
48
49 // BUFFER_LENGTH is in MPC_SAMPLE_FORMAT units
50 #define MAX_FRAMESIZE (4 * MPC_DECODER_BUFFER_LENGTH)
51 //! this many frames should decode good after seeking
52 #define MIN_SEEK_GOOD 5
53 //! how many frames to discard at most after seeking
54 #define MAX_SEEK_DISCARD 50
55
56 typedef struct context_s {
57 char *header;
58 int header_len;
59 sh_audio_t *sh;
60 uint32_t pos;
61 mpc_decoder decoder;
62 } context_t;
63
64 /**
65 * \brief mpc_reader callback function for reading the header
66 */
cb_read(void * data,void * buf,mpc_int32_t size)67 static mpc_int32_t cb_read(void *data, void *buf, mpc_int32_t size) {
68 context_t *d = (context_t *)data;
69 char *p = (char *)buf;
70 int s = size;
71 if (d->pos < d->header_len) {
72 if (s > d->header_len - d->pos)
73 s = d->header_len - d->pos;
74 fast_memcpy(p, &d->header[d->pos], s);
75 } else
76 s = 0;
77 memset(&p[s], 0, size - s);
78 d->pos += size;
79 return size;
80 }
81
82 /**
83 * \brief dummy mpc_reader callback function for seeking
84 */
cb_seek(void * data,mpc_int32_t offset)85 static mpc_bool_t cb_seek(void *data, mpc_int32_t offset ) {
86 context_t *d = (context_t *)data;
87 d->pos = offset;
88 return 1;
89 }
90
91 /**
92 * \brief dummy mpc_reader callback function for getting stream position
93 */
cb_tell(void * data)94 static mpc_int32_t cb_tell(void *data) {
95 context_t *d = (context_t *)data;
96 return d->pos;
97 }
98
99 /**
100 * \brief dummy mpc_reader callback function for getting stream length
101 */
cb_get_size(void * data)102 static mpc_int32_t cb_get_size(void *data) {
103 return 1 << 30;
104 }
105
106 /**
107 * \brief mpc_reader callback function, we cannot seek.
108 */
cb_canseek(void * data)109 static mpc_bool_t cb_canseek(void *data) {
110 return 0;
111 }
112
113
114 mpc_reader header_reader = {
115 .read = cb_read, .seek = cb_seek, .tell = cb_tell,
116 .get_size = cb_get_size, .canseek = cb_canseek
117 };
118
preinit(sh_audio_t * sh)119 static int preinit(sh_audio_t *sh) {
120 sh->audio_out_minsize = MAX_FRAMESIZE;
121 return 1;
122 }
123
uninit(sh_audio_t * sh)124 static void uninit(sh_audio_t *sh) {
125 free(sh->context);
126 sh->context = NULL;
127 }
128
init(sh_audio_t * sh)129 static int init(sh_audio_t *sh) {
130 mpc_streaminfo info;
131 context_t *cd = malloc(sizeof(context_t));
132
133 if (!sh->wf || (sh->wf->cbSize < 6 * 4)) {
134 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Missing extradata!\n");
135 return 0;
136 }
137 cd->header = (char *)(sh->wf + 1);
138 cd->header_len = sh->wf->cbSize;
139 cd->sh = sh;
140 cd->pos = 0;
141 sh->context = (char *)cd;
142
143 /* read file's streaminfo data */
144 mpc_streaminfo_init(&info);
145 header_reader.data = cd;
146 if (mpc_streaminfo_read(&info, &header_reader) != ERROR_CODE_OK) {
147 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Not a valid musepack file.\n");
148 return 0;
149 }
150 // this value is nonsense, since it relies on the get_size function.
151 // use the value from the demuxer instead.
152 // sh->i_bps = info.average_bitrate / 8;
153 sh->channels = info.channels;
154 sh->samplerate = info.sample_freq;
155 sh->samplesize = 4;
156 sh->sample_format =
157 #if MPC_SAMPLE_FORMAT == float
158 AF_FORMAT_FLOAT_NE;
159 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
160 AF_FORMAT_S32_NE;
161 #else
162 #error musepack lib must use either float or mpc_int32_t sample format
163 #endif
164
165 mpc_decoder_setup(&cd->decoder, NULL);
166 mpc_decoder_set_streaminfo(&cd->decoder, &info);
167 return 1;
168 }
169
170 // FIXME: minlen is currently ignored
decode_audio(sh_audio_t * sh,unsigned char * buf,int minlen,int maxlen)171 static int decode_audio(sh_audio_t *sh, unsigned char *buf,
172 int minlen, int maxlen) {
173 int status, len;
174 MPC_SAMPLE_FORMAT *sample_buffer = (MPC_SAMPLE_FORMAT *)buf;
175 mpc_uint32_t *packet = NULL;
176
177 context_t *cd = (context_t *) sh->context;
178 if (maxlen < MAX_FRAMESIZE) {
179 mp_msg(MSGT_DECAUDIO, MSGL_V, "maxlen too small in decode_audio\n");
180 return -1;
181 }
182 len = ds_get_packet(sh->ds, (unsigned char **)&packet);
183 if (len <= 0) return -1;
184 status = mpc_decoder_decode_frame(&cd->decoder, packet, len, sample_buffer);
185 if (status == -1) // decode error
186 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Error decoding file.\n");
187 if (status <= 0) // error or EOF
188 return -1;
189
190 status = MPC_FRAME_LENGTH * sh->channels; // one sample per channel
191 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
192 status *= 4;
193 #else
194 // should not happen
195 status *= 2;
196 #endif
197 return status;
198 }
199
200 /**
201 * \brief check if the decoded values are in a sane range
202 * \param buf decoded buffer
203 * \param len length of buffer in bytes
204 * \return 1 if all values are in (-1.01, 1.01) range, 0 otherwise
205 */
check_clip(void * buf,int len)206 static int check_clip(void *buf, int len) {
207 #if MPC_SAMPLE_FORMAT == float
208 float *p = buf;
209 if (len < 4) return 1;
210 len = -len / 4;
211 p = &p[-len];
212 do {
213 if (p[len] < -1 || p[len] > 1) return 0;
214 } while (++len);
215 #endif
216 return 1;
217 }
218
control(sh_audio_t * sh,int cmd,void * arg,...)219 static int control(sh_audio_t *sh, int cmd, void* arg, ...) {
220 if (cmd == ADCTRL_RESYNC_STREAM) {
221 unsigned char *buf = malloc(MAX_FRAMESIZE);
222 int i;
223 int nr_ok = 0;
224 for (i = 0; i < MAX_SEEK_DISCARD; i++) {
225 int len = decode_audio(sh, buf, 0, MAX_FRAMESIZE);
226 if (check_clip(buf, len)) nr_ok++; else nr_ok = 0;
227 if (nr_ok > MIN_SEEK_GOOD) break;
228 }
229 free(buf);
230 }
231 return CONTROL_UNKNOWN;
232 }
233