options: change -alang and -slang to use string list type
[mplayer.git] / libmpcodecs / ad_mpc.c
blob979dce617807d5e0062d27dfd9de04d099b26a49
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>
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.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
30 #include "config.h"
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",
38 "mpcdec",
39 "Reza Jelveh and Reimar Döffinger",
40 "",
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 {
56 char *header;
57 int header_len;
58 sh_audio_t *sh;
59 uint32_t pos;
60 mpc_decoder decoder;
61 } context_t;
63 /**
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;
69 int s = size;
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);
74 } else
75 s = 0;
76 memset(&p[s], 0, size - s);
77 d->pos += size;
78 return size;
81 /**
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;
86 d->pos = offset;
87 return 1;
90 /**
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;
95 return d->pos;
98 /**
99 * \brief dummy mpc_reader callback function for getting stream length
101 static mpc_int32_t cb_get_size(void *data) {
102 return 1 << 30;
106 * \brief mpc_reader callback function, we cannot seek.
108 static mpc_bool_t cb_canseek(void *data) {
109 return 0;
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;
120 return 1;
123 static void uninit(sh_audio_t *sh) {
124 free(sh->context);
125 sh->context = NULL;
128 static int init(sh_audio_t *sh) {
129 mpc_streaminfo info;
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");
134 return 0;
136 cd->header = (char *)(sh->wf + 1);
137 cd->header_len = sh->wf->cbSize;
138 cd->sh = sh;
139 cd->pos = 0;
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");
147 return 0;
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;
154 sh->samplesize = 4;
155 sh->sample_format =
156 #if MPC_SAMPLE_FORMAT == float
157 AF_FORMAT_FLOAT_NE;
158 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
159 AF_FORMAT_S32_NE;
160 #else
161 #error musepack lib must use either float or mpc_int32_t sample format
162 #endif
164 mpc_decoder_setup(&cd->decoder, NULL);
165 mpc_decoder_set_streaminfo(&cd->decoder, &info);
166 return 1;
169 // FIXME: minlen is currently ignored
170 static int decode_audio(sh_audio_t *sh, unsigned char *buf,
171 int minlen, int maxlen) {
172 int status, len;
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");
179 return -1;
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
187 return -1;
189 status = MPC_FRAME_LENGTH * sh->channels; // one sample per channel
190 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
191 status *= 4;
192 #else
193 // should not happen
194 status *= 2;
195 #endif
196 return status;
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
207 float *p = buf;
208 if (len < 4) return 1;
209 len = -len / 4;
210 p = &p[-len];
211 do {
212 if (p[len] < -1 || p[len] > 1) return 0;
213 } while (++len);
214 #endif
215 return 1;
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);
221 int i;
222 int nr_ok = 0;
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;
228 free(buf);
230 return CONTROL_UNKNOWN;