4599df90b6fd42f712a7203069220c45bf579ec1
[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 "para.h"
22
23 #include "list.h"
24 #include "filter.h"
25 #include "error.h"
26 #include <mad.h>
27 #include "string.h"
28
29 /** the output buffer size */
30 #define MP3_OUTBUF_SIZE 128 * 1024
31
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))
35 /** \endcond */
36
37 /**
38 * data specific to the mp3dec filter
39 *
40 * \sa filter, filter_node
41 */
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;
49 };
50
51 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
52 {
53 int i, ret;
54 struct private_mp3dec_data *pmd = fn->private_data;
55 size_t copy = PARA_MIN(len, 4096);
56
57 if (fn->loaded > fn->bufsize * 4 / 5)
58 return 0;
59 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
60 pmd->stream.error = 0;
61 next_frame:
62 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
63 if (ret) {
64 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
65 goto out;
66 PARA_ERROR_LOG("fatal: ret = %d, loaded = %d\n", ret, fn->loaded);
67 return -E_MAD_FRAME_DECODE;
68 }
69 mad_synth_frame(&pmd->synth, &pmd->frame);
70 fn->fci->samplerate = pmd->frame.header.samplerate;
71 fn->fci->channels = MAD_NCHANNELS(&pmd->frame.header);
72
73 for (i = 0; i < pmd->synth.pcm.length; i++) {
74 /* output format: unsigned 16 bit little endian */
75 signed short s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
76 fn->buf[fn->loaded++] = s & 0xff;
77 fn->buf[fn->loaded++] = s >> 8;
78 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
79 s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
80 fn->buf[fn->loaded++] = s & 0xff;
81 fn->buf[fn->loaded++] = s >> 8;
82 }
83 if (fn->loaded != fn->bufsize) /* output buffer not full */
84 continue;
85 PARA_ERROR_LOG("output buffer full: %d\n", fn->loaded);
86 return -E_MP3DEC_OVERRUN;
87 }
88 if (fn->loaded <= fn->bufsize * 4 / 5)
89 goto next_frame;
90 out:
91 if (pmd->stream.next_frame) { /* we still have some data */
92 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
93 PARA_DEBUG_LOG("converted %d, %d input bytes, %d output bytes\n",
94 len - off, off, fn->loaded);
95 return copy - off;
96 }
97 return copy;
98 }
99
100 static void mp3dec_close(struct filter_node *fn)
101 {
102 struct private_mp3dec_data *pmd = fn->private_data;
103
104 mad_synth_finish(&pmd->synth);
105 mad_frame_finish(&pmd->frame);
106 mad_stream_finish(&pmd->stream);
107
108 free(fn->buf);
109 fn->buf = NULL;
110 free(pmd);
111 fn->private_data = NULL;
112 }
113
114 static void mp3dec_open(struct filter_node *fn)
115 {
116 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
117 struct private_mp3dec_data *pmd = fn->private_data;
118
119 mad_stream_init(&pmd->stream);
120 mad_frame_init(&pmd->frame);
121 mad_synth_init(&pmd->synth);
122 fn->loaded = 0;
123 fn->bufsize = MP3_OUTBUF_SIZE;
124 fn->buf = para_calloc(fn->bufsize);
125 }
126 /**
127 * the init function of the mp3dec filter
128 *
129 * \sa filter::init
130 */
131 void mp3dec_init(struct filter *f)
132 {
133 f->open = mp3dec_open;
134 f->convert = mp3dec;
135 f->close = mp3dec_close;
136 }