2 * Copyright (C) 2005-2008 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 /** Convert a sample value from libmad to a signed short. */
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))
24 /** Data specific to the mp3dec filter. */
25 struct private_mp3dec_data {
26 /** Information on the current mp3 stream. */
27 struct mad_stream stream;
28 /** Information about the frame which is currently decoded. */
29 struct mad_frame frame;
30 /** Contains the PCM output. */
31 struct mad_synth synth;
34 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
37 struct private_mp3dec_data *pmd = fn->private_data;
38 size_t copy = PARA_MIN(len, 4096);
40 if (fn->loaded > fn->bufsize * 4 / 5)
42 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
43 pmd->stream.error = 0;
45 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
48 fn->fc->samplerate = pmd->frame.header.samplerate;
49 fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
50 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
52 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
54 PARA_ERROR_LOG("fatal: ret = %d, loaded = %zd\n", ret, fn->loaded);
55 return -E_MAD_FRAME_DECODE;
57 mad_synth_frame(&pmd->synth, &pmd->frame);
59 for (i = 0; i < pmd->synth.pcm.length; i++) {
60 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
61 write_int16_host_endian(fn->buf + fn->loaded, s);
63 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
64 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
65 write_int16_host_endian(fn->buf + fn->loaded, s);
68 if (fn->loaded != fn->bufsize) /* output buffer not full */
70 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
71 return -E_MP3DEC_OVERRUN;
73 if (fn->loaded <= fn->bufsize * 4 / 5)
76 if (pmd->stream.next_frame) { /* we still have some data */
77 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
78 // PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
79 // fn->fc->samplerate, copy - off);
85 static void mp3dec_close(struct filter_node *fn)
87 struct private_mp3dec_data *pmd = fn->private_data;
89 mad_synth_finish(&pmd->synth);
90 mad_frame_finish(&pmd->frame);
91 mad_stream_finish(&pmd->stream);
96 fn->private_data = NULL;
99 static void mp3dec_open(struct filter_node *fn)
101 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
102 struct private_mp3dec_data *pmd = fn->private_data;
104 mad_stream_init(&pmd->stream);
105 mad_frame_init(&pmd->frame);
106 mad_synth_init(&pmd->synth);
108 fn->bufsize = MP3_OUTBUF_SIZE;
109 fn->buf = para_calloc(fn->bufsize);
113 * The init function of the mp3dec filter.
115 * \param f Pointer to the filter struct to initialize.
119 void mp3dec_init(struct filter *f)
121 f->open = mp3dec_open;
123 f->close = mp3dec_close;