2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file mp3dec.c paraslash's mp3 decoder */
29 /** the output buffer size */
30 #define MP3_OUTBUF_SIZE 128 * 1024
32 /** \cond a helper macro */
33 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
34 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
38 * data specific to the mp3dec filter
40 * \sa filter, filter_node
42 struct private_mp3dec_data {
43 /** information on the current mp3 stream */
44 struct mad_stream stream;
45 /** information about the frame which is currently decoded */
46 struct mad_frame frame;
47 /** contains the PCM output */
48 struct mad_synth synth;
51 /* TODO: Convert all input if possible */
52 #define FRAME_HEADER_SIZE 4
53 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
56 struct private_mp3dec_data *pmd = fn->private_data;
57 size_t copy = PARA_MIN(len, 4096);
59 if (fn->loaded > fn->bufsize * 4 / 5)
61 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
62 pmd->stream.error = 0;
64 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
66 if (!MAD_RECOVERABLE(pmd->stream.error))
68 return FRAME_HEADER_SIZE;
70 fn->fc->samplerate = pmd->frame.header.samplerate;
71 fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
72 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
74 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
76 PARA_ERROR_LOG("fatal: ret = %d, loaded = %zd\n", ret, fn->loaded);
77 return -E_MAD_FRAME_DECODE;
79 mad_synth_frame(&pmd->synth, &pmd->frame);
81 for (i = 0; i < pmd->synth.pcm.length; i++) {
82 /* output format: unsigned 16 bit little endian */
83 signed short s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
84 fn->buf[fn->loaded++] = s & 0xff;
85 fn->buf[fn->loaded++] = s >> 8;
86 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
87 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
88 fn->buf[fn->loaded++] = s & 0xff;
89 fn->buf[fn->loaded++] = s >> 8;
91 if (fn->loaded != fn->bufsize) /* output buffer not full */
93 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
94 return -E_MP3DEC_OVERRUN;
96 if (fn->loaded <= fn->bufsize * 4 / 5)
99 if (pmd->stream.next_frame) { /* we still have some data */
100 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
101 // PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
102 // fn->fc->samplerate, copy - off);
108 static void mp3dec_close(struct filter_node *fn)
110 struct private_mp3dec_data *pmd = fn->private_data;
112 mad_synth_finish(&pmd->synth);
113 mad_frame_finish(&pmd->frame);
114 mad_stream_finish(&pmd->stream);
119 fn->private_data = NULL;
122 static void mp3dec_open(struct filter_node *fn)
124 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
125 struct private_mp3dec_data *pmd = fn->private_data;
127 mad_stream_init(&pmd->stream);
128 mad_frame_init(&pmd->frame);
129 mad_synth_init(&pmd->synth);
131 fn->bufsize = MP3_OUTBUF_SIZE;
132 fn->buf = para_calloc(fn->bufsize);
135 * the init function of the mp3dec filter
139 void mp3dec_init(struct filter *f)
141 f->open = mp3dec_open;
143 f->close = mp3dec_close;