2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mp3dec.c paraslash's mp3 decoder */
17 /** the output buffer size */
18 #define MP3_OUTBUF_SIZE 128 * 1024
20 /** \cond a helper macro */
21 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
22 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
26 * data specific to the mp3dec filter
28 * \sa filter, filter_node
30 struct private_mp3dec_data {
31 /** information on the current mp3 stream */
32 struct mad_stream stream;
33 /** information about the frame which is currently decoded */
34 struct mad_frame frame;
35 /** contains the PCM output */
36 struct mad_synth synth;
39 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
42 struct private_mp3dec_data *pmd = fn->private_data;
43 size_t copy = PARA_MIN(len, 4096);
45 if (fn->loaded > fn->bufsize * 4 / 5)
47 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
48 pmd->stream.error = 0;
50 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
53 fn->fc->samplerate = pmd->frame.header.samplerate;
54 fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
55 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
57 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
59 PARA_ERROR_LOG("fatal: ret = %d, loaded = %zd\n", ret, fn->loaded);
60 return -E_MAD_FRAME_DECODE;
62 mad_synth_frame(&pmd->synth, &pmd->frame);
64 for (i = 0; i < pmd->synth.pcm.length; i++) {
65 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
66 write_int16_host_endian(fn->buf + fn->loaded, s);
68 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
69 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
70 write_int16_host_endian(fn->buf + fn->loaded, s);
73 if (fn->loaded != fn->bufsize) /* output buffer not full */
75 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
76 return -E_MP3DEC_OVERRUN;
78 if (fn->loaded <= fn->bufsize * 4 / 5)
81 if (pmd->stream.next_frame) { /* we still have some data */
82 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
83 // PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
84 // fn->fc->samplerate, copy - off);
90 static void mp3dec_close(struct filter_node *fn)
92 struct private_mp3dec_data *pmd = fn->private_data;
94 mad_synth_finish(&pmd->synth);
95 mad_frame_finish(&pmd->frame);
96 mad_stream_finish(&pmd->stream);
101 fn->private_data = NULL;
104 static void mp3dec_open(struct filter_node *fn)
106 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
107 struct private_mp3dec_data *pmd = fn->private_data;
109 mad_stream_init(&pmd->stream);
110 mad_frame_init(&pmd->frame);
111 mad_synth_init(&pmd->synth);
113 fn->bufsize = MP3_OUTBUF_SIZE;
114 fn->buf = para_calloc(fn->bufsize);
118 * the init function of the mp3dec filter
120 * \param f pointer to the filter struct to initialize
124 void mp3dec_init(struct filter *f)
126 f->open = mp3dec_open;
128 f->close = mp3dec_close;