0100d04ea7d8a191a57c7264972870756d53ded4
[paraslash.git] / mp3dec.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file mp3dec.c paraslash's mp3 decoder */
20
21 #include "gcc-compat.h"
22 #include "para.h"
23
24 #include "list.h"
25 #include "filter.h"
26 #include "error.h"
27 #include <mad.h>
28 #include "string.h"
29
30 /** the output buffer size */
31 #define MP3_OUTBUF_SIZE 128 * 1024
32
33 /** \cond a helper macro */
34 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
35 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
36 /** \endcond */
37
38 /**
39 * data specific to the mp3dec filter
40 *
41 * \sa filter, filter_node
42 */
43 struct private_mp3dec_data {
44 /** information on the current mp3 stream */
45 struct mad_stream stream;
46 /** information about the frame which is currently decoded */
47 struct mad_frame frame;
48 /** contains the PCM output */
49 struct mad_synth synth;
50 };
51
52 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
53 {
54 int i, ret;
55 struct private_mp3dec_data *pmd = fn->private_data;
56 size_t copy = MIN(len, 4096);
57
58 if (fn->loaded > fn->bufsize * 4 / 5)
59 return 0;
60 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
61 pmd->stream.error = 0;
62 next_frame:
63 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
64 if (ret) {
65 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
66 goto out;
67 PARA_ERROR_LOG("fatal: ret = %d, loaded = %d\n", ret, fn->loaded);
68 return -E_MAD_FRAME_DECODE;
69 }
70 mad_synth_frame(&pmd->synth, &pmd->frame);
71 fn->fci->samplerate = pmd->frame.header.samplerate;
72 fn->fci->channels = MAD_NCHANNELS(&pmd->frame.header);
73
74 for (i = 0; i < pmd->synth.pcm.length; i++) {
75 /* output format: unsigned 16 bit little endian */
76 signed short s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
77 fn->buf[fn->loaded++] = s & 0xff;
78 fn->buf[fn->loaded++] = s >> 8;
79 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
80 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
81 fn->buf[fn->loaded++] = s & 0xff;
82 fn->buf[fn->loaded++] = s >> 8;
83 }
84 if (fn->loaded != fn->bufsize) /* output buffer not full */
85 continue;
86 PARA_ERROR_LOG("output buffer full: %d\n", fn->loaded);
87 return -E_MP3DEC_OVERRUN;
88 }
89 if (fn->loaded <= fn->bufsize * 4 / 5)
90 goto next_frame;
91 out:
92 if (pmd->stream.next_frame) { /* we still have some data */
93 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
94 PARA_DEBUG_LOG("converted %d, %d input bytes, %d output bytes\n",
95 len - off, off, fn->loaded);
96 return copy - off;
97 }
98 return copy;
99 }
100
101 static void mp3dec_close(struct filter_node *fn)
102 {
103 struct private_mp3dec_data *pmd = fn->private_data;
104
105 mad_synth_finish(&pmd->synth);
106 mad_frame_finish(&pmd->frame);
107 mad_stream_finish(&pmd->stream);
108
109 free(fn->buf);
110 fn->buf = NULL;
111 free(pmd);
112 fn->private_data = NULL;
113 }
114
115 static void mp3dec_open(struct filter_node *fn)
116 {
117 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
118 struct private_mp3dec_data *pmd = fn->private_data;
119
120 mad_stream_init(&pmd->stream);
121 mad_frame_init(&pmd->frame);
122 mad_synth_init(&pmd->synth);
123 fn->loaded = 0;
124 fn->bufsize = MP3_OUTBUF_SIZE;
125 fn->buf = para_calloc(fn->bufsize);
126 }
127 /**
128 * the init function of the mp3dec filter
129 *
130 * \sa filter::init
131 */
132 void mp3dec_init(struct filter *f)
133 {
134 f->open = mp3dec_open;
135 f->convert = mp3dec;
136 f->close = mp3dec_close;
137 }