b13c602bbf6a4ef03b20093254bcf80d87013399
[paraslash.git] / mp3dec_filter.c
1 /*
2 * Copyright (C) 2005-2009 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 <mad.h>
10 #include <regex.h>
11 #include <stdbool.h>
12
13 #include "para.h"
14 #include "mp3dec_filter.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "buffer_tree.h"
19 #include "filter.h"
20 #include "error.h"
21 #include "string.h"
22
23 /** Convert a sample value from libmad to a signed short. */
24 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
25 (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
26
27 /** State of the decoding process. */
28 enum mp3dec_flags {
29 /** Bad main_data_begin pointer encounterd. */
30 MP3DEC_FLAG_BAD_DATA = 1,
31 /** Some output has already been produced. */
32 MP3DEC_FLAG_DECODE_STARTED = 2,
33 MP3DEC_FLAG_NEED_MORE = 4,
34 };
35
36 /** Data specific to the mp3dec filter. */
37 struct private_mp3dec_data {
38 /** Information on the current mp3 stream. */
39 struct mad_stream stream;
40 /** Information about the frame which is currently decoded. */
41 struct mad_frame frame;
42 /** Contains the PCM output. */
43 struct mad_synth synth;
44 /** See \ref mp3dec_flags. */
45 unsigned flags;
46 /** Defer decoding until this time. */
47 struct timeval stream_start_barrier;
48 /** Wait until this many input bytes are available. */
49 size_t input_len_barrier;
50 /** The number of channels of the current stream. */
51 unsigned int channels;
52 /** Current sample rate in Hz. */
53 unsigned int samplerate;
54 };
55
56 static int need_bad_data_delay(struct private_mp3dec_data *pmd,
57 size_t bytes_available)
58 {
59 if (!(pmd->flags & MP3DEC_FLAG_BAD_DATA))
60 return 0;
61 if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED)
62 return 0;
63 if (bytes_available >= pmd->input_len_barrier)
64 return 0;
65 if (tv_diff(now, &pmd->stream_start_barrier, NULL) > 0)
66 return 0;
67 return 1;
68 }
69
70 /*
71 * Returns negative on serious errors, zero if the error should be ignored and
72 * positive on bad data pointer errors at stream start.
73 */
74 static int handle_decode_error(struct private_mp3dec_data *pmd, size_t len)
75 {
76 const struct timeval delay = {0, 60 * 1000};
77 if (!MAD_RECOVERABLE(pmd->stream.error)
78 && pmd->stream.error != MAD_ERROR_BUFLEN) {
79 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
80 return -E_MAD_FRAME_DECODE;
81 }
82 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
83 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
84 return 0;
85 if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED)
86 return 0;
87 /*
88 * Bad data pointer at stream start. Defer decoding until the amount of
89 * data we are about to skip is available again, but wait at most 60ms.
90 */
91 pmd->flags |= MP3DEC_FLAG_BAD_DATA;
92 pmd->input_len_barrier = len;
93 tv_add(now, &delay, &pmd->stream_start_barrier);
94 return 1;
95 }
96
97 static size_t used_mad_buffer_bytes(struct mad_stream *s, size_t max)
98 {
99 size_t rv;
100
101 if (!s->next_frame)
102 return max;
103 /* we still have some data */
104 rv = s->next_frame - s->buffer;
105 assert(rv <= max);
106 return rv;
107 }
108
109 static void mp3dec_close(struct filter_node *fn)
110 {
111 struct private_mp3dec_data *pmd = fn->private_data;
112
113 mad_synth_finish(&pmd->synth);
114 mad_frame_finish(&pmd->frame);
115 mad_stream_finish(&pmd->stream);
116
117 free(fn->buf);
118 fn->buf = NULL;
119 free(pmd);
120 fn->private_data = NULL;
121 }
122
123 static void mp3dec_post_select(__a_unused struct sched *s, struct task *t)
124 {
125 struct filter_node *fn = container_of(t, struct filter_node, task);
126 int i, ret;
127 struct private_mp3dec_data *pmd = fn->private_data;
128 struct btr_node *btrn = fn->btrn;
129 size_t loaded, used, len, iqs;
130 char *inbuffer, *outbuffer;
131
132 next_buffer:
133 pmd->stream.error = 0;
134 t->error = 0;
135 iqs = btr_get_input_queue_size(btrn);
136 if (need_bad_data_delay(pmd, iqs))
137 return;
138 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
139 if (ret < 0)
140 goto err;
141 if (ret == 0)
142 return;
143 btr_merge(btrn, fn->min_iqs);
144 len = btr_next_buffer(btrn, &inbuffer);
145 mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
146 next_frame:
147 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
148 if (ret < 0) {
149 used = used_mad_buffer_bytes(&pmd->stream, len);
150 btr_consume(btrn, used);
151 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
152 if (len == iqs && btr_no_parent(btrn)) {
153 ret = -E_MP3DEC_EOF;
154 goto err;
155 }
156 fn->min_iqs += 100;
157 goto next_buffer;
158 } else if (pmd->stream.error != MAD_ERROR_LOSTSYNC)
159 PARA_DEBUG_LOG("header decode: %s\n",
160 mad_stream_errorstr(&pmd->stream));
161 goto next_buffer;
162 }
163 fn->min_iqs = 0;
164 pmd->samplerate = pmd->frame.header.samplerate;
165 pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
166 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
167 if (ret != 0) {
168 PARA_INFO_LOG("frame decode: %s\n", mad_stream_errorstr(&pmd->stream));
169 used = used_mad_buffer_bytes(&pmd->stream, len);
170 ret = handle_decode_error(pmd, used);
171 btr_consume(btrn, used);
172 if (ret < 0)
173 goto err;
174 if (ret == 0)
175 goto next_buffer;
176 return;
177 }
178 mad_synth_frame(&pmd->synth, &pmd->frame);
179 pmd->flags |= MP3DEC_FLAG_DECODE_STARTED;
180
181 outbuffer = para_malloc(pmd->synth.pcm.length * 4);
182 loaded = 0;
183 for (i = 0; i < pmd->synth.pcm.length; i++) {
184 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
185 write_int16_host_endian(outbuffer + loaded, sample);
186 loaded += 2;
187 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
188 sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
189 write_int16_host_endian(outbuffer + loaded, sample);
190 loaded += 2;
191 }
192 }
193 btr_add_output(outbuffer, loaded, btrn);
194 goto next_frame;
195 err:
196 assert(ret < 0);
197 t->error = ret;
198 btr_remove_node(btrn);
199 }
200
201 static void mp3dec_open(struct filter_node *fn)
202 {
203 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
204 struct mp3dec_filter_args_info *mp3_conf = fn->conf;
205
206 fn->private_data = pmd;
207 mad_stream_init(&pmd->stream);
208 mad_frame_init(&pmd->frame);
209 mad_synth_init(&pmd->synth);
210 fn->loaded = 0;
211 fn->bufsize = mp3_conf->bufsize_arg * 1024;
212 fn->buf = para_calloc(fn->bufsize);
213 if (mp3_conf->ignore_crc_given)
214 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
215 }
216
217 static int mp3dec_parse_config(int argc, char **argv, void **config)
218 {
219 int ret;
220 struct mp3dec_filter_args_info *mp3_conf;
221
222 mp3_conf = para_calloc(sizeof(*mp3_conf));
223 ret = -E_MP3DEC_SYNTAX;
224 if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
225 goto err;
226 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
227 if (mp3_conf->bufsize_arg < 32)
228 goto err;
229 if (mp3_conf->bufsize_arg >= INT_MAX / 1024)
230 goto err;
231 *config = mp3_conf;
232 return 1;
233 err:
234 free(mp3_conf);
235 return ret;
236 }
237
238 static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
239 {
240 struct filter_node *fn = btr_context(btrn);
241 struct private_mp3dec_data *pmd = fn->private_data;
242
243 if (!strcmp(cmd, "samplerate")) {
244 if (pmd->samplerate == 0)
245 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
246 *result = make_message("%u", pmd->samplerate);
247 return 1;
248 }
249 if (!strcmp(cmd, "channels")) {
250 if (pmd->channels == 0)
251 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
252 *result = make_message("%u", pmd->channels);
253 return 1;
254 }
255 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
256 }
257
258 static void mp3dec_free_config(void *conf)
259 {
260 mp3dec_cmdline_parser_free(conf);
261 }
262 /**
263 * The init function of the mp3dec filter.
264 *
265 * \param f Pointer to the filter struct to initialize.
266 *
267 * \sa filter::init.
268 */
269 void mp3dec_filter_init(struct filter *f)
270 {
271 struct mp3dec_filter_args_info dummy;
272
273 mp3dec_cmdline_parser_init(&dummy);
274 f->open = mp3dec_open;
275 f->close = mp3dec_close;
276 f->parse_config = mp3dec_parse_config;
277 f->free_config = mp3dec_free_config;
278 f->pre_select = generic_filter_pre_select;
279 f->post_select = mp3dec_post_select;
280 f->execute = mp3dec_execute;
281 f->help = (struct ggo_help) {
282 .short_help = mp3dec_filter_args_info_help,
283 .detailed_help = mp3dec_filter_args_info_detailed_help
284 };
285 }