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);
47 if (pmd->stream.error != MAD_ERROR_BUFLEN &&
48 pmd->stream.error != MAD_ERROR_LOSTSYNC)
49 PARA_DEBUG_LOG("header decode: %s\n",
50 mad_stream_errorstr(&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) ||
58 pmd->stream.error == MAD_ERROR_BUFLEN) {
59 PARA_DEBUG_LOG("frame decode: %s\n",
60 mad_stream_errorstr(&pmd->stream));
63 PARA_ERROR_LOG("frame decode: %s\n",
64 mad_stream_errorstr(&pmd->stream));
65 return -E_MAD_FRAME_DECODE;
67 mad_synth_frame(&pmd->synth, &pmd->frame);
69 for (i = 0; i < pmd->synth.pcm.length; i++) {
70 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
71 write_int16_host_endian(fn->buf + fn->loaded, s);
73 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
74 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
75 write_int16_host_endian(fn->buf + fn->loaded, s);
78 if (fn->loaded != fn->bufsize) /* output buffer not full */
80 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
81 return -E_MP3DEC_OVERRUN;
83 if (fn->loaded <= fn->bufsize * 4 / 5)
86 if (pmd->stream.next_frame) { /* we still have some data */
87 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
88 // PARA_INFO_LOG("off: %zd, rate: %u, returning %zd\n", off,
89 // fn->fc->samplerate, copy - off);
95 static void mp3dec_close(struct filter_node *fn)
97 struct private_mp3dec_data *pmd = fn->private_data;
99 mad_synth_finish(&pmd->synth);
100 mad_frame_finish(&pmd->frame);
101 mad_stream_finish(&pmd->stream);
106 fn->private_data = NULL;
109 static void mp3dec_open(struct filter_node *fn)
111 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
112 struct private_mp3dec_data *pmd = fn->private_data;
114 mad_stream_init(&pmd->stream);
115 mad_frame_init(&pmd->frame);
116 mad_synth_init(&pmd->synth);
118 fn->bufsize = MP3_OUTBUF_SIZE;
119 fn->buf = para_calloc(fn->bufsize);
123 * The init function of the mp3dec filter.
125 * \param f Pointer to the filter struct to initialize.
129 void mp3dec_init(struct filter *f)
131 f->open = mp3dec_open;
133 f->close = mp3dec_close;