2 * Copyright (C) 2005-2009 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. */
10 #include "mp3dec_filter.cmdline.h"
19 /** Convert a sample value from libmad to a signed short. */
20 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
21 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
23 /** State of the decoding process. */
25 /** Bad main_data_begin pointer encounterd. */
26 MP3DEC_FLAG_BAD_DATA = 1,
27 /** Some output has already been produced. */
28 MP3DEC_FLAG_DECODE_STARTED = 2,
31 /** Data specific to the mp3dec filter. */
32 struct private_mp3dec_data {
33 /** Information on the current mp3 stream. */
34 struct mad_stream stream;
35 /** Information about the frame which is currently decoded. */
36 struct mad_frame frame;
37 /** Contains the PCM output. */
38 struct mad_synth synth;
39 /** See \ref mp3dec_flags. */
41 /** Defer decoding until this time. */
42 struct timeval stream_start_barrier;
43 /** Wait until this many input bytes are available. */
44 size_t input_len_barrier;
47 static int need_bad_data_delay(struct private_mp3dec_data *pmd,
48 size_t bytes_available)
50 if (!(pmd->flags & MP3DEC_FLAG_BAD_DATA))
52 if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED)
54 if (bytes_available >= pmd->input_len_barrier)
56 if (tv_diff(now, &pmd->stream_start_barrier, NULL) > 0)
62 * Returns negative on serious errors, zero if the error should be ignored and
63 * positive on bad data pointer errors at stream start.
65 static int handle_decode_error(struct private_mp3dec_data *pmd, size_t len)
67 if (!MAD_RECOVERABLE(pmd->stream.error)
68 && pmd->stream.error != MAD_ERROR_BUFLEN) {
69 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
70 return -E_MAD_FRAME_DECODE;
72 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
73 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
75 if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED)
78 * Bad data pointer at stream start. Defer decoding until the amount of
79 * data we are about to skip is available again, but wait at most 60ms.
81 pmd->flags |= MP3DEC_FLAG_BAD_DATA;
82 pmd->input_len_barrier = len;
83 tv_add(now, &(struct timeval){0, 60 * 1000},
84 &pmd->stream_start_barrier);
88 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
91 struct private_mp3dec_data *pmd = fn->private_data;
92 size_t copy = PARA_MIN(len, (size_t)4096);
94 if (fn->loaded + 16384 > fn->bufsize)
96 if (need_bad_data_delay(pmd, len))
98 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
99 pmd->stream.error = 0;
101 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
103 if (pmd->stream.error != MAD_ERROR_BUFLEN &&
104 pmd->stream.error != MAD_ERROR_LOSTSYNC)
105 PARA_DEBUG_LOG("header decode: %s\n",
106 mad_stream_errorstr(&pmd->stream));
109 fn->fc->samplerate = pmd->frame.header.samplerate;
110 fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
111 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
113 ret = handle_decode_error(pmd, len);
118 ret = copy - (pmd->stream.bufend - pmd->stream.next_frame);
119 PARA_NOTICE_LOG("skipping %d input bytes\n", ret);
122 mad_synth_frame(&pmd->synth, &pmd->frame);
123 pmd->flags |= MP3DEC_FLAG_DECODE_STARTED;
125 for (i = 0; i < pmd->synth.pcm.length; i++) {
126 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
127 write_int16_host_endian(fn->buf + fn->loaded, s);
129 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
130 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
131 write_int16_host_endian(fn->buf + fn->loaded, s);
134 if (fn->loaded != fn->bufsize) /* output buffer not full */
136 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
137 return -E_MP3DEC_OVERRUN;
139 if (fn->loaded + 16384 <= fn->bufsize)
142 if (pmd->stream.next_frame) { /* we still have some data */
143 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
144 if (fn->loaded + 16384 <= fn->bufsize && off > 2048)
151 static void mp3dec_close(struct filter_node *fn)
153 struct private_mp3dec_data *pmd = fn->private_data;
155 mad_synth_finish(&pmd->synth);
156 mad_frame_finish(&pmd->frame);
157 mad_stream_finish(&pmd->stream);
162 fn->private_data = NULL;
165 static void mp3dec_open(struct filter_node *fn)
167 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
168 struct mp3dec_filter_args_info *mp3_conf = fn->conf;
170 fn->private_data = pmd;
171 mad_stream_init(&pmd->stream);
172 mad_frame_init(&pmd->frame);
173 mad_synth_init(&pmd->synth);
175 fn->bufsize = mp3_conf->bufsize_arg * 1024;
176 fn->buf = para_calloc(fn->bufsize);
177 if (mp3_conf->ignore_crc_given)
178 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
181 static int mp3dec_parse_config(int argc, char **argv, void **config)
184 struct mp3dec_filter_args_info *mp3_conf;
186 mp3_conf = para_calloc(sizeof(*mp3_conf));
187 ret = -E_MP3DEC_SYNTAX;
188 if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
190 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
191 if (mp3_conf->bufsize_arg < 32)
193 if (mp3_conf->bufsize_arg >= INT_MAX / 1024)
203 * The init function of the mp3dec filter.
205 * \param f Pointer to the filter struct to initialize.
209 void mp3dec_filter_init(struct filter *f)
211 struct mp3dec_filter_args_info dummy;
213 mp3dec_cmdline_parser_init(&dummy);
214 f->open = mp3dec_open;
216 f->close = mp3dec_close;
217 f->parse_config = mp3dec_parse_config;
218 f->help = (struct ggo_help) {
219 .short_help = mp3dec_filter_args_info_help,
220 .detailed_help = mp3dec_filter_args_info_detailed_help