1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file mp3dec_filter.c Paraslash's mp3 decoder. */
9 #include "filter_cmd.lsg.h"
13 #include "buffer_tree.h"
18 /** Convert a sample value from libmad to a signed short. */
19 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
20 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
22 /** Data specific to the mp3dec filter. */
23 struct private_mp3dec_data {
24 /** Information on the current mp3 stream. */
25 struct mad_stream stream;
26 /** Information about the frame which is currently decoded. */
27 struct mad_frame frame;
28 /** Contains the PCM output. */
29 struct mad_synth synth;
30 /** The number of channels of the current stream. */
31 unsigned int channels;
32 /** Current sample rate in Hz. */
33 unsigned int sample_rate;
36 /* Returns negative on serious errors. */
37 static int handle_decode_error(struct private_mp3dec_data *pmd)
39 if (!MAD_RECOVERABLE(pmd->stream.error)
40 && pmd->stream.error != MAD_ERROR_BUFLEN) {
41 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
42 return -E_MAD_FRAME_DECODE;
44 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
48 static void mp3dec_consume(struct btr_node *btrn, struct mad_stream *s,
55 else { /* we still have some data */
56 used = s->next_frame - s->buffer;
59 btr_consume(btrn, used);
62 static void mp3dec_close(struct filter_node *fn)
64 struct private_mp3dec_data *pmd = fn->private_data;
66 mad_synth_finish(&pmd->synth);
67 mad_frame_finish(&pmd->frame);
68 mad_stream_finish(&pmd->stream);
71 fn->private_data = NULL;
74 #define MP3DEC_MAX_FRAME 8192
76 static int mp3dec_post_monitor(__a_unused struct sched *s, void *context)
78 struct filter_node *fn = context;
80 struct private_mp3dec_data *pmd = fn->private_data;
81 struct btr_node *btrn = fn->btrn;
82 size_t loaded = 0, len;
83 char *inbuffer, *outbuffer;
86 pmd->stream.error = 0;
87 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
92 btr_merge(btrn, fn->min_iqs);
93 len = btr_next_buffer(btrn, &inbuffer);
95 * Decode at most 8K in one go to give the post_monitor() functions of
96 * other buffer tree nodes a chance to run. This is necessary to avoid
97 * buffer underruns on slow machines.
99 len = PARA_MIN(len, (size_t)MP3DEC_MAX_FRAME);
100 mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
102 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
104 mp3dec_consume(btrn, &pmd->stream, len);
105 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
106 if (fn->min_iqs > 0 && btr_no_parent(btrn)) {
111 ret = -E_MP3DEC_CORRUPT;
112 if (fn->min_iqs > MP3DEC_MAX_FRAME)
119 pmd->sample_rate = pmd->frame.header.samplerate;
120 pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
122 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
124 ret = handle_decode_error(pmd);
127 mad_stream_sync(&pmd->stream);
128 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
130 if (btr_no_parent(btrn))
133 ret = -E_MP3DEC_CORRUPT;
134 if (fn->min_iqs > MP3DEC_MAX_FRAME)
136 mp3dec_consume(btrn, &pmd->stream, len);
139 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
141 mp3dec_consume(btrn, &pmd->stream, len);
145 mad_synth_frame(&pmd->synth, &pmd->frame);
146 outbuffer = arr_alloc(pmd->synth.pcm.length, 2 * pmd->channels);
148 for (i = 0; i < pmd->synth.pcm.length; i++) {
149 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
150 write_int16_host_endian(outbuffer + loaded, sample);
152 if (pmd->channels == 2) { /* stereo */
153 sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
154 write_int16_host_endian(outbuffer + loaded, sample);
158 btr_add_output(outbuffer, loaded, btrn);
162 btr_remove_node(&fn->btrn);
166 static void mp3dec_open(struct filter_node *fn)
168 struct private_mp3dec_data *pmd = zalloc(sizeof(*pmd));
170 fn->private_data = pmd;
171 mad_stream_init(&pmd->stream);
172 mad_frame_init(&pmd->frame);
173 mad_synth_init(&pmd->synth);
174 if (FILTER_CMD_OPT_GIVEN(MP3DEC, IGNORE_CRC, fn->lpr))
175 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
178 static int mp3dec_execute(const struct btr_node *btrn, const char *cmd,
181 struct filter_node *fn = btr_context(btrn);
182 struct private_mp3dec_data *pmd = fn->private_data;
184 return decoder_execute(cmd, pmd->sample_rate, pmd->channels, result);
187 const struct filter lsg_filter_cmd_com_mp3dec_user_data = {
189 .close = mp3dec_close,
190 .pre_monitor = generic_filter_pre_monitor,
191 .post_monitor = mp3dec_post_monitor,
192 .execute = mp3dec_execute,