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 /** Data specific to the mp3dec filter. */
24 struct private_mp3dec_data {
25 /** Information on the current mp3 stream. */
26 struct mad_stream stream;
27 /** Information about the frame which is currently decoded. */
28 struct mad_frame frame;
29 /** Contains the PCM output. */
30 struct mad_synth synth;
33 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
36 struct private_mp3dec_data *pmd = fn->private_data;
37 size_t copy = PARA_MIN(len, (size_t)4096);
39 if (fn->loaded + 16384 > fn->bufsize)
41 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
42 pmd->stream.error = 0;
44 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
46 if (pmd->stream.error != MAD_ERROR_BUFLEN &&
47 pmd->stream.error != MAD_ERROR_LOSTSYNC)
48 PARA_DEBUG_LOG("header decode: %s\n",
49 mad_stream_errorstr(&pmd->stream));
52 fn->fc->samplerate = pmd->frame.header.samplerate;
53 fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
54 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
56 if (MAD_RECOVERABLE(pmd->stream.error) ||
57 pmd->stream.error == MAD_ERROR_BUFLEN) {
58 PARA_DEBUG_LOG("frame decode: %s\n",
59 mad_stream_errorstr(&pmd->stream));
62 PARA_ERROR_LOG("frame decode: %s\n",
63 mad_stream_errorstr(&pmd->stream));
64 return -E_MAD_FRAME_DECODE;
66 mad_synth_frame(&pmd->synth, &pmd->frame);
68 for (i = 0; i < pmd->synth.pcm.length; i++) {
69 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
70 write_int16_host_endian(fn->buf + fn->loaded, s);
72 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
73 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
74 write_int16_host_endian(fn->buf + fn->loaded, s);
77 if (fn->loaded != fn->bufsize) /* output buffer not full */
79 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
80 return -E_MP3DEC_OVERRUN;
82 if (fn->loaded + 16384 <= fn->bufsize)
85 if (pmd->stream.next_frame) { /* we still have some data */
86 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
87 if (fn->loaded + 16384 <= fn->bufsize && off > 2048)
94 static void mp3dec_close(struct filter_node *fn)
96 struct private_mp3dec_data *pmd = fn->private_data;
98 mad_synth_finish(&pmd->synth);
99 mad_frame_finish(&pmd->frame);
100 mad_stream_finish(&pmd->stream);
105 fn->private_data = NULL;
108 static void mp3dec_open(struct filter_node *fn)
110 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
111 struct mp3dec_filter_args_info *mp3_conf = fn->conf;
113 fn->private_data = pmd;
114 mad_stream_init(&pmd->stream);
115 mad_frame_init(&pmd->frame);
116 mad_synth_init(&pmd->synth);
118 fn->bufsize = mp3_conf->bufsize_arg * 1024;
119 fn->buf = para_calloc(fn->bufsize);
120 if (mp3_conf->ignore_crc_given)
121 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
124 static int mp3dec_parse_config(int argc, char **argv, void **config)
127 struct mp3dec_filter_args_info *mp3_conf;
129 mp3_conf = para_calloc(sizeof(*mp3_conf));
130 ret = -E_MP3DEC_SYNTAX;
131 if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
133 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
134 if (mp3_conf->bufsize_arg < 32)
136 if (mp3_conf->bufsize_arg >= INT_MAX / 1024)
146 * The init function of the mp3dec filter.
148 * \param f Pointer to the filter struct to initialize.
152 void mp3dec_filter_init(struct filter *f)
154 struct mp3dec_filter_args_info dummy;
156 mp3dec_cmdline_parser_init(&dummy);
157 f->open = mp3dec_open;
159 f->close = mp3dec_close;
160 f->parse_config = mp3dec_parse_config;
161 f->help = (struct ggo_help) {
162 .short_help = mp3dec_filter_args_info_help,
163 .detailed_help = mp3dec_filter_args_info_detailed_help