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