vo_gl: Try to get a quadbuffered visual for corresponding 3D mode
[mplayer/glamo.git] / libmpcodecs / ad_mpc.c
blob33ed911f72aeab13a5eabc66758d5fc03c5b4260
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 if (sh->context)
125 free(sh->context);
126 sh->context = NULL;
129 static int init(sh_audio_t *sh) {
130 mpc_streaminfo info;
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");
135 return 0;
137 cd->header = (char *)sh->wf;
138 cd->header = &cd->header[sizeof(WAVEFORMATEX)];
139 cd->header_len = sh->wf->cbSize;
140 cd->sh = sh;
141 cd->pos = 0;
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");
149 return 0;
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;
156 sh->samplesize = 4;
157 sh->sample_format =
158 #if MPC_SAMPLE_FORMAT == float
159 AF_FORMAT_FLOAT_NE;
160 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
161 AF_FORMAT_S32_NE;
162 #else
163 #error musepack lib must use either float or mpc_int32_t sample format
164 #endif
166 mpc_decoder_setup(&cd->decoder, NULL);
167 mpc_decoder_set_streaminfo(&cd->decoder, &info);
168 return 1;
171 // FIXME: minlen is currently ignored
172 static int decode_audio(sh_audio_t *sh, unsigned char *buf,
173 int minlen, int maxlen) {
174 int status, len;
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");
181 return -1;
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
189 return -1;
191 status = MPC_FRAME_LENGTH * sh->channels; // one sample per channel
192 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
193 status *= 4;
194 #else
195 // should not happen
196 status *= 2;
197 #endif
198 return status;
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
209 float *p = buf;
210 if (len < 4) return 1;
211 len = -len / 4;
212 p = &p[-len];
213 do {
214 if (p[len] < -1 || p[len] > 1) return 0;
215 } while (++len);
216 #endif
217 return 1;
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);
223 int i;
224 int nr_ok = 0;
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;
230 free(buf);
232 return CONTROL_UNKNOWN;