2 * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mp3dec_filter.c Paraslash's mp3 decoder. */
13 #include "mp3dec_filter.cmdline.h"
17 #include "buffer_tree.h"
22 /** Convert a sample value from libmad to a signed short. */
23 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
24 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
26 /** Data specific to the mp3dec filter. */
27 struct private_mp3dec_data {
28 /** Information on the current mp3 stream. */
29 struct mad_stream stream;
30 /** Information about the frame which is currently decoded. */
31 struct mad_frame frame;
32 /** Contains the PCM output. */
33 struct mad_synth synth;
34 /** The number of channels of the current stream. */
35 unsigned int channels;
36 /** Current sample rate in Hz. */
37 unsigned int sample_rate;
40 /* Returns negative on serious errors. */
41 static int handle_decode_error(struct private_mp3dec_data *pmd)
43 if (!MAD_RECOVERABLE(pmd->stream.error)
44 && pmd->stream.error != MAD_ERROR_BUFLEN) {
45 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
46 return -E_MAD_FRAME_DECODE;
48 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
52 static void mp3dec_consume(struct btr_node *btrn, struct mad_stream *s,
59 else { /* we still have some data */
60 used = s->next_frame - s->buffer;
63 btr_consume(btrn, used);
66 static void mp3dec_close(struct filter_node *fn)
68 struct private_mp3dec_data *pmd = fn->private_data;
70 mad_synth_finish(&pmd->synth);
71 mad_frame_finish(&pmd->frame);
72 mad_stream_finish(&pmd->stream);
75 fn->private_data = NULL;
78 #define MP3DEC_MAX_FRAME 8192
80 static void mp3dec_post_select(__a_unused struct sched *s, struct task *t)
82 struct filter_node *fn = container_of(t, struct filter_node, task);
84 struct private_mp3dec_data *pmd = fn->private_data;
85 struct btr_node *btrn = fn->btrn;
86 size_t loaded = 0, len, iqs;
87 char *inbuffer, *outbuffer;
90 pmd->stream.error = 0;
92 iqs = btr_get_input_queue_size(btrn);
93 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
98 btr_merge(btrn, fn->min_iqs);
99 len = btr_next_buffer(btrn, &inbuffer);
101 * Decode at most 8K in one go to give the post_select() functions of
102 * other buffer tree nodes a chance to run. This is necessary to avoid
103 * buffer underruns on slow machines.
105 len = PARA_MIN(len, (size_t)MP3DEC_MAX_FRAME);
106 mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
108 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
110 mp3dec_consume(btrn, &pmd->stream, len);
111 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
112 if (len == iqs && btr_no_parent(btrn)) {
117 ret = -E_MP3DEC_CORRUPT;
118 if (fn->min_iqs > MP3DEC_MAX_FRAME)
125 pmd->sample_rate = pmd->frame.header.samplerate;
126 pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
128 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
130 ret = handle_decode_error(pmd);
133 mad_stream_sync(&pmd->stream);
134 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
136 if (len == iqs && btr_no_parent(btrn))
139 ret = -E_MP3DEC_CORRUPT;
140 if (fn->min_iqs > MP3DEC_MAX_FRAME)
142 mp3dec_consume(btrn, &pmd->stream, len);
145 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
147 mp3dec_consume(btrn, &pmd->stream, len);
151 mad_synth_frame(&pmd->synth, &pmd->frame);
152 outbuffer = para_malloc(pmd->synth.pcm.length * 2 * pmd->channels);
154 for (i = 0; i < pmd->synth.pcm.length; i++) {
155 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
156 write_int16_host_endian(outbuffer + loaded, sample);
158 if (pmd->channels == 2) { /* stereo */
159 sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
160 write_int16_host_endian(outbuffer + loaded, sample);
164 btr_add_output(outbuffer, loaded, btrn);
169 btr_remove_node(&fn->btrn);
172 static void mp3dec_open(struct filter_node *fn)
174 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
175 struct mp3dec_filter_args_info *mp3_conf = fn->conf;
177 fn->private_data = pmd;
178 mad_stream_init(&pmd->stream);
179 mad_frame_init(&pmd->frame);
180 mad_synth_init(&pmd->synth);
181 if (mp3_conf->ignore_crc_given)
182 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
185 static int mp3dec_parse_config(int argc, char **argv, void **config)
187 struct mp3dec_filter_args_info *conf = para_calloc(sizeof(*conf));
189 mp3dec_filter_cmdline_parser(argc, argv, conf);
194 static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
196 struct filter_node *fn = btr_context(btrn);
197 struct private_mp3dec_data *pmd = fn->private_data;
199 return decoder_execute(cmd, pmd->sample_rate, pmd->channels, result);
202 static void mp3dec_free_config(void *conf)
204 mp3dec_filter_cmdline_parser_free(conf);
207 * The init function of the mp3dec filter.
209 * \param f Pointer to the filter struct to initialize.
213 void mp3dec_filter_init(struct filter *f)
215 struct mp3dec_filter_args_info dummy;
217 mp3dec_filter_cmdline_parser_init(&dummy);
218 f->open = mp3dec_open;
219 f->close = mp3dec_close;
220 f->parse_config = mp3dec_parse_config;
221 f->free_config = mp3dec_free_config;
222 f->pre_select = generic_filter_pre_select;
223 f->post_select = mp3dec_post_select;
224 f->execute = mp3dec_execute;
225 f->help = (struct ggo_help) {
226 .short_help = mp3dec_filter_args_info_help,
227 .detailed_help = mp3dec_filter_args_info_detailed_help