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_select(__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
, iqs
;
83 char *inbuffer
, *outbuffer
;
86 pmd
->stream
.error
= 0;
87 iqs
= btr_get_input_queue_size(btrn
);
88 ret
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
93 btr_merge(btrn
, fn
->min_iqs
);
94 len
= btr_next_buffer(btrn
, &inbuffer
);
96 * Decode at most 8K in one go to give the post_select() functions of
97 * other buffer tree nodes a chance to run. This is necessary to avoid
98 * buffer underruns on slow machines.
100 len
= PARA_MIN(len
, (size_t)MP3DEC_MAX_FRAME
);
101 mad_stream_buffer(&pmd
->stream
, (unsigned char *)inbuffer
, len
);
103 ret
= mad_header_decode(&pmd
->frame
.header
, &pmd
->stream
);
105 mp3dec_consume(btrn
, &pmd
->stream
, len
);
106 if (pmd
->stream
.error
== MAD_ERROR_BUFLEN
) {
107 if (len
== iqs
&& btr_no_parent(btrn
)) {
112 ret
= -E_MP3DEC_CORRUPT
;
113 if (fn
->min_iqs
> MP3DEC_MAX_FRAME
)
120 pmd
->sample_rate
= pmd
->frame
.header
.samplerate
;
121 pmd
->channels
= MAD_NCHANNELS(&pmd
->frame
.header
);
123 ret
= mad_frame_decode(&pmd
->frame
, &pmd
->stream
);
125 ret
= handle_decode_error(pmd
);
128 mad_stream_sync(&pmd
->stream
);
129 if (pmd
->stream
.error
== MAD_ERROR_BUFLEN
) {
131 if (len
== iqs
&& btr_no_parent(btrn
))
134 ret
= -E_MP3DEC_CORRUPT
;
135 if (fn
->min_iqs
> MP3DEC_MAX_FRAME
)
137 mp3dec_consume(btrn
, &pmd
->stream
, len
);
140 if (pmd
->stream
.error
!= MAD_ERROR_BADDATAPTR
)
142 mp3dec_consume(btrn
, &pmd
->stream
, len
);
146 mad_synth_frame(&pmd
->synth
, &pmd
->frame
);
147 outbuffer
= para_malloc(pmd
->synth
.pcm
.length
* 2 * pmd
->channels
);
149 for (i
= 0; i
< pmd
->synth
.pcm
.length
; i
++) {
150 int sample
= MAD_TO_SHORT(pmd
->synth
.pcm
.samples
[0][i
]);
151 write_int16_host_endian(outbuffer
+ loaded
, sample
);
153 if (pmd
->channels
== 2) { /* stereo */
154 sample
= MAD_TO_SHORT(pmd
->synth
.pcm
.samples
[1][i
]);
155 write_int16_host_endian(outbuffer
+ loaded
, sample
);
159 btr_add_output(outbuffer
, loaded
, btrn
);
163 btr_remove_node(&fn
->btrn
);
167 static void mp3dec_open(struct filter_node
*fn
)
169 struct private_mp3dec_data
*pmd
= para_calloc(sizeof(*pmd
));
171 fn
->private_data
= pmd
;
172 mad_stream_init(&pmd
->stream
);
173 mad_frame_init(&pmd
->frame
);
174 mad_synth_init(&pmd
->synth
);
175 if (FILTER_CMD_OPT_GIVEN(MP3DEC
, IGNORE_CRC
, fn
->lpr
))
176 mad_stream_options(&pmd
->stream
, MAD_OPTION_IGNORECRC
);
179 static int mp3dec_execute(struct btr_node
*btrn
, const char *cmd
, char **result
)
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_select
= generic_filter_pre_select
,
191 .post_select
= mp3dec_post_select
,
192 .execute
= mp3dec_execute
,