2 * Copyright (C) 2005-2011 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. */
14 #include "mp3dec_filter.cmdline.h"
18 #include "buffer_tree.h"
23 /** Convert a sample value from libmad to a signed short. */
24 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
25 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
27 /** State of the decoding process. */
29 /** Bad main_data_begin pointer encounterd. */
30 MP3DEC_FLAG_BAD_DATA
= 1,
31 /** Some output has already been produced. */
32 MP3DEC_FLAG_DECODE_STARTED
= 2,
33 MP3DEC_FLAG_NEED_MORE
= 4,
36 /** Data specific to the mp3dec filter. */
37 struct private_mp3dec_data
{
38 /** Information on the current mp3 stream. */
39 struct mad_stream stream
;
40 /** Information about the frame which is currently decoded. */
41 struct mad_frame frame
;
42 /** Contains the PCM output. */
43 struct mad_synth synth
;
44 /** See \ref mp3dec_flags. */
46 /** Defer decoding until this time. */
47 struct timeval stream_start_barrier
;
48 /** Wait until this many input bytes are available. */
49 size_t input_len_barrier
;
50 /** The number of channels of the current stream. */
51 unsigned int channels
;
52 /** Current sample rate in Hz. */
53 unsigned int sample_rate
;
56 static int need_bad_data_delay(struct private_mp3dec_data
*pmd
,
57 size_t bytes_available
)
59 if (!(pmd
->flags
& MP3DEC_FLAG_BAD_DATA
))
61 if (pmd
->flags
& MP3DEC_FLAG_DECODE_STARTED
)
63 if (bytes_available
>= pmd
->input_len_barrier
)
65 if (tv_diff(now
, &pmd
->stream_start_barrier
, NULL
) > 0)
71 * Returns negative on serious errors, zero if the error should be ignored and
72 * positive on bad data pointer errors at stream start.
74 static int handle_decode_error(struct private_mp3dec_data
*pmd
, size_t len
)
76 const struct timeval delay
= {0, 60 * 1000};
77 if (!MAD_RECOVERABLE(pmd
->stream
.error
)
78 && pmd
->stream
.error
!= MAD_ERROR_BUFLEN
) {
79 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd
->stream
));
80 return -E_MAD_FRAME_DECODE
;
82 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd
->stream
));
83 if (pmd
->stream
.error
!= MAD_ERROR_BADDATAPTR
)
85 if (pmd
->flags
& MP3DEC_FLAG_DECODE_STARTED
)
88 * Bad data pointer at stream start. Defer decoding until the amount of
89 * data we are about to skip is available again, but wait at most 60ms.
91 pmd
->flags
|= MP3DEC_FLAG_BAD_DATA
;
92 pmd
->input_len_barrier
= len
;
93 tv_add(now
, &delay
, &pmd
->stream_start_barrier
);
97 static size_t used_mad_buffer_bytes(struct mad_stream
*s
, size_t max
)
103 /* we still have some data */
104 rv
= s
->next_frame
- s
->buffer
;
109 static void mp3dec_close(struct filter_node
*fn
)
111 struct private_mp3dec_data
*pmd
= fn
->private_data
;
113 mad_synth_finish(&pmd
->synth
);
114 mad_frame_finish(&pmd
->frame
);
115 mad_stream_finish(&pmd
->stream
);
118 fn
->private_data
= NULL
;
121 static void mp3dec_post_select(__a_unused
struct sched
*s
, struct task
*t
)
123 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
125 struct private_mp3dec_data
*pmd
= fn
->private_data
;
126 struct btr_node
*btrn
= fn
->btrn
;
127 size_t loaded
, used
, len
, iqs
;
128 char *inbuffer
, *outbuffer
;
131 pmd
->stream
.error
= 0;
133 iqs
= btr_get_input_queue_size(btrn
);
134 ret
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
137 if (need_bad_data_delay(pmd
, iqs
))
141 btr_merge(btrn
, fn
->min_iqs
);
142 len
= btr_next_buffer(btrn
, &inbuffer
);
144 * Decode at most 8K in one go to give the post_select() functions of
145 * other buffer tree nodes a chance to run. This is necessary to avoid
146 * buffer underruns on slow machines.
148 len
= PARA_MIN(len
, (size_t)8192);
149 mad_stream_buffer(&pmd
->stream
, (unsigned char *)inbuffer
, len
);
151 ret
= mad_header_decode(&pmd
->frame
.header
, &pmd
->stream
);
153 used
= used_mad_buffer_bytes(&pmd
->stream
, len
);
154 btr_consume(btrn
, used
);
155 if (pmd
->stream
.error
== MAD_ERROR_BUFLEN
) {
156 if (len
== iqs
&& btr_no_parent(btrn
)) {
162 } else if (pmd
->stream
.error
!= MAD_ERROR_LOSTSYNC
)
163 PARA_DEBUG_LOG("header decode: %s\n",
164 mad_stream_errorstr(&pmd
->stream
));
168 pmd
->sample_rate
= pmd
->frame
.header
.samplerate
;
169 pmd
->channels
= MAD_NCHANNELS(&pmd
->frame
.header
);
170 ret
= mad_frame_decode(&pmd
->frame
, &pmd
->stream
);
172 PARA_INFO_LOG("frame decode: %s\n", mad_stream_errorstr(&pmd
->stream
));
173 used
= used_mad_buffer_bytes(&pmd
->stream
, len
);
174 ret
= handle_decode_error(pmd
, used
);
175 btr_consume(btrn
, used
);
182 mad_synth_frame(&pmd
->synth
, &pmd
->frame
);
183 pmd
->flags
|= MP3DEC_FLAG_DECODE_STARTED
;
185 outbuffer
= para_malloc(pmd
->synth
.pcm
.length
* 4);
187 for (i
= 0; i
< pmd
->synth
.pcm
.length
; i
++) {
188 int sample
= MAD_TO_SHORT(pmd
->synth
.pcm
.samples
[0][i
]);
189 write_int16_host_endian(outbuffer
+ loaded
, sample
);
191 if (MAD_NCHANNELS(&pmd
->frame
.header
) == 2) { /* stereo */
192 sample
= MAD_TO_SHORT(pmd
->synth
.pcm
.samples
[1][i
]);
193 write_int16_host_endian(outbuffer
+ loaded
, sample
);
197 btr_add_output(outbuffer
, loaded
, btrn
);
202 btr_remove_node(btrn
);
205 static void mp3dec_open(struct filter_node
*fn
)
207 struct private_mp3dec_data
*pmd
= para_calloc(sizeof(*pmd
));
208 struct mp3dec_filter_args_info
*mp3_conf
= fn
->conf
;
210 fn
->private_data
= pmd
;
211 mad_stream_init(&pmd
->stream
);
212 mad_frame_init(&pmd
->frame
);
213 mad_synth_init(&pmd
->synth
);
214 if (mp3_conf
->ignore_crc_given
)
215 mad_stream_options(&pmd
->stream
, MAD_OPTION_IGNORECRC
);
218 static int mp3dec_parse_config(int argc
, char **argv
, void **config
)
221 struct mp3dec_filter_args_info
*mp3_conf
;
223 mp3_conf
= para_calloc(sizeof(*mp3_conf
));
224 ret
= -E_MP3DEC_SYNTAX
;
225 if (mp3dec_cmdline_parser(argc
, argv
, mp3_conf
))
234 static int mp3dec_execute(struct btr_node
*btrn
, const char *cmd
, char **result
)
236 struct filter_node
*fn
= btr_context(btrn
);
237 struct private_mp3dec_data
*pmd
= fn
->private_data
;
239 return decoder_execute(cmd
, pmd
->sample_rate
, pmd
->channels
, result
);
242 static void mp3dec_free_config(void *conf
)
244 mp3dec_cmdline_parser_free(conf
);
247 * The init function of the mp3dec filter.
249 * \param f Pointer to the filter struct to initialize.
253 void mp3dec_filter_init(struct filter
*f
)
255 struct mp3dec_filter_args_info dummy
;
257 mp3dec_cmdline_parser_init(&dummy
);
258 f
->open
= mp3dec_open
;
259 f
->close
= mp3dec_close
;
260 f
->parse_config
= mp3dec_parse_config
;
261 f
->free_config
= mp3dec_free_config
;
262 f
->pre_select
= generic_filter_pre_select
;
263 f
->post_select
= mp3dec_post_select
;
264 f
->execute
= mp3dec_execute
;
265 f
->help
= (struct ggo_help
) {
266 .short_help
= mp3dec_filter_args_info_help
,
267 .detailed_help
= mp3dec_filter_args_info_detailed_help