5e5df865b50b17373c404a0fe494373b607dbde2
[paraslash.git] / mp3dec_filter.c
1 /*
2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file mp3dec_filter.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 /** 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))
23
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;
32 };
33
34 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
35 {
36 int i, ret;
37 struct private_mp3dec_data *pmd = fn->private_data;
38 size_t copy = PARA_MIN(len, (size_t)4096);
39
40 if (fn->loaded > fn->bufsize * 4 / 5)
41 return 0;
42 mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
43 pmd->stream.error = 0;
44 next_frame:
45 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
46 if (ret < 0) {
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));
51 goto out;
52 }
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) ||
58 pmd->stream.error == MAD_ERROR_BUFLEN) {
59 PARA_DEBUG_LOG("frame decode: %s\n",
60 mad_stream_errorstr(&pmd->stream));
61 goto out;
62 }
63 PARA_ERROR_LOG("frame decode: %s\n",
64 mad_stream_errorstr(&pmd->stream));
65 return -E_MAD_FRAME_DECODE;
66 }
67 mad_synth_frame(&pmd->synth, &pmd->frame);
68
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);
72 fn->loaded += 2;
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);
76 fn->loaded += 2;
77 }
78 if (fn->loaded != fn->bufsize) /* output buffer not full */
79 continue;
80 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
81 return -E_MP3DEC_OVERRUN;
82 }
83 if (fn->loaded <= fn->bufsize * 4 / 5)
84 goto next_frame;
85 out:
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);
90 return copy - off;
91 }
92 return copy;
93 }
94
95 static void mp3dec_close(struct filter_node *fn)
96 {
97 struct private_mp3dec_data *pmd = fn->private_data;
98
99 mad_synth_finish(&pmd->synth);
100 mad_frame_finish(&pmd->frame);
101 mad_stream_finish(&pmd->stream);
102
103 free(fn->buf);
104 fn->buf = NULL;
105 free(pmd);
106 fn->private_data = NULL;
107 }
108
109 static void mp3dec_open(struct filter_node *fn)
110 {
111 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
112
113 fn->private_data = pmd;
114 mad_stream_init(&pmd->stream);
115 mad_frame_init(&pmd->frame);
116 mad_synth_init(&pmd->synth);
117 fn->loaded = 0;
118 fn->bufsize = MP3_OUTBUF_SIZE;
119 fn->buf = para_calloc(fn->bufsize);
120 }
121
122 /**
123 * The init function of the mp3dec filter.
124 *
125 * \param f Pointer to the filter struct to initialize.
126 *
127 * \sa filter::init.
128 */
129 void mp3dec_filter_init(struct filter *f)
130 {
131 f->open = mp3dec_open;
132 f->convert = mp3dec;
133 f->close = mp3dec_close;
134 }