2 * Copyright (C) 2005-2008 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"
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;
32 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
35 struct private_mp3dec_data *pmd = fn->private_data;
36 size_t copy = PARA_MIN(len, (size_t)4096);
38 if (fn->loaded + 16384 > fn->bufsize)
40 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
41 pmd->stream.error = 0;
43 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
45 if (pmd->stream.error != MAD_ERROR_BUFLEN &&
46 pmd->stream.error != MAD_ERROR_LOSTSYNC)
47 PARA_DEBUG_LOG("header decode: %s\n",
48 mad_stream_errorstr(&pmd->stream));
51 fn->fc->samplerate = pmd->frame.header.samplerate;
52 fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
53 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
55 if (MAD_RECOVERABLE(pmd->stream.error) ||
56 pmd->stream.error == MAD_ERROR_BUFLEN) {
57 PARA_DEBUG_LOG("frame decode: %s\n",
58 mad_stream_errorstr(&pmd->stream));
61 PARA_ERROR_LOG("frame decode: %s\n",
62 mad_stream_errorstr(&pmd->stream));
63 return -E_MAD_FRAME_DECODE;
65 mad_synth_frame(&pmd->synth, &pmd->frame);
67 for (i = 0; i < pmd->synth.pcm.length; i++) {
68 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
69 write_int16_host_endian(fn->buf + fn->loaded, s);
71 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
72 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
73 write_int16_host_endian(fn->buf + fn->loaded, s);
76 if (fn->loaded != fn->bufsize) /* output buffer not full */
78 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
79 return -E_MP3DEC_OVERRUN;
81 if (fn->loaded + 16384 <= fn->bufsize)
84 if (pmd->stream.next_frame) { /* we still have some data */
85 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
86 // PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
87 // fn->fc->samplerate, copy - off);
93 static void mp3dec_close(struct filter_node *fn)
95 struct private_mp3dec_data *pmd = fn->private_data;
97 mad_synth_finish(&pmd->synth);
98 mad_frame_finish(&pmd->frame);
99 mad_stream_finish(&pmd->stream);
104 fn->private_data = NULL;
107 static void mp3dec_open(struct filter_node *fn)
109 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
110 struct mp3dec_filter_args_info *mp3_conf = fn->conf;
112 fn->private_data = pmd;
113 mad_stream_init(&pmd->stream);
114 mad_frame_init(&pmd->frame);
115 mad_synth_init(&pmd->synth);
117 fn->bufsize = mp3_conf->bufsize_arg * 1024;
118 fn->buf = para_calloc(fn->bufsize);
119 if (mp3_conf->ignore_crc_given)
120 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
123 static int mp3dec_parse_config(int argc, char **argv, void **config)
126 struct mp3dec_filter_args_info *mp3_conf;
128 mp3_conf = para_calloc(sizeof(*mp3_conf));
129 ret = -E_MP3DEC_SYNTAX;
130 if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
132 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
133 if (mp3_conf->bufsize_arg < 32)
135 if (mp3_conf->bufsize_arg >= INT_MAX / 1024)
145 * The init function of the mp3dec filter.
147 * \param f Pointer to the filter struct to initialize.
151 void mp3dec_filter_init(struct filter *f)
153 f->open = mp3dec_open;
155 f->close = mp3dec_close;
156 f->parse_config = mp3dec_parse_config;