net.c: Fix a typo in the documentation.
[paraslash.git] / mp3dec.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file mp3dec.c paraslash's mp3 decoder */
8
9 #include "para.h"
10 #include "list.h"
11 #include "sched.h"
12 #include "filter.h"
13 #include "error.h"
14 #include <mad.h>
15 #include "string.h"
16
17 /** the output buffer size */
18 #define MP3_OUTBUF_SIZE 128 * 1024
19
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))
23 /** \endcond */
24
25 /**
26 * data specific to the mp3dec filter
27 *
28 * \sa filter, filter_node
29 */
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;
37 };
38
39 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
40 {
41 int i, ret;
42 struct private_mp3dec_data *pmd = fn->private_data;
43 size_t copy = PARA_MIN(len, 4096);
44
45 if (fn->loaded > fn->bufsize * 4 / 5)
46 return 0;
47 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
48 pmd->stream.error = 0;
49 next_frame:
50 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
51 if (ret < 0)
52 goto out;
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);
56 if (ret) {
57 if (MAD_RECOVERABLE(pmd->stream.error) || pmd->stream.error == MAD_ERROR_BUFLEN)
58 goto out;
59 PARA_ERROR_LOG("fatal: ret = %d, loaded = %zd\n", ret, fn->loaded);
60 return -E_MAD_FRAME_DECODE;
61 }
62 mad_synth_frame(&pmd->synth, &pmd->frame);
63
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);
67 fn->loaded += 2;
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);
71 fn->loaded += 2;
72 }
73 if (fn->loaded != fn->bufsize) /* output buffer not full */
74 continue;
75 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
76 return -E_MP3DEC_OVERRUN;
77 }
78 if (fn->loaded <= fn->bufsize * 4 / 5)
79 goto next_frame;
80 out:
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);
85 return copy - off;
86 }
87 return copy;
88 }
89
90 static void mp3dec_close(struct filter_node *fn)
91 {
92 struct private_mp3dec_data *pmd = fn->private_data;
93
94 mad_synth_finish(&pmd->synth);
95 mad_frame_finish(&pmd->frame);
96 mad_stream_finish(&pmd->stream);
97
98 free(fn->buf);
99 fn->buf = NULL;
100 free(pmd);
101 fn->private_data = NULL;
102 }
103
104 static void mp3dec_open(struct filter_node *fn)
105 {
106 fn->private_data = para_calloc(sizeof(struct private_mp3dec_data));
107 struct private_mp3dec_data *pmd = fn->private_data;
108
109 mad_stream_init(&pmd->stream);
110 mad_frame_init(&pmd->frame);
111 mad_synth_init(&pmd->synth);
112 fn->loaded = 0;
113 fn->bufsize = MP3_OUTBUF_SIZE;
114 fn->buf = para_calloc(fn->bufsize);
115 }
116
117 /**
118 * the init function of the mp3dec filter
119 *
120 * \param f pointer to the filter struct to initialize
121 *
122 * \sa filter::init
123 */
124 void mp3dec_init(struct filter *f)
125 {
126 f->open = mp3dec_open;
127 f->convert = mp3dec;
128 f->close = mp3dec_close;
129 }