mp3dec: Fix possible endless loop.
[paraslash.git] / mp3dec_filter.c
1 /*
2 * Copyright (C) 2005-2011 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 /** Data specific to the mp3dec filter. */
28 struct private_mp3dec_data {
29 /** Information on the current mp3 stream. */
30 struct mad_stream stream;
31 /** Information about the frame which is currently decoded. */
32 struct mad_frame frame;
33 /** Contains the PCM output. */
34 struct mad_synth synth;
35 /** The number of channels of the current stream. */
36 unsigned int channels;
37 /** Current sample rate in Hz. */
38 unsigned int sample_rate;
39 };
40
41 /* Returns negative on serious errors. */
42 static int handle_decode_error(struct private_mp3dec_data *pmd)
43 {
44 if (!MAD_RECOVERABLE(pmd->stream.error)
45 && pmd->stream.error != MAD_ERROR_BUFLEN) {
46 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
47 return -E_MAD_FRAME_DECODE;
48 }
49 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
50 return 0;
51 }
52
53 static size_t used_mad_buffer_bytes(struct mad_stream *s, size_t max)
54 {
55 size_t rv;
56
57 if (!s->next_frame)
58 return max;
59 /* we still have some data */
60 rv = s->next_frame - s->buffer;
61 assert(rv <= max);
62 return rv;
63 }
64
65 static void mp3dec_close(struct filter_node *fn)
66 {
67 struct private_mp3dec_data *pmd = fn->private_data;
68
69 mad_synth_finish(&pmd->synth);
70 mad_frame_finish(&pmd->frame);
71 mad_stream_finish(&pmd->stream);
72
73 free(pmd);
74 fn->private_data = NULL;
75 }
76
77 #define MP3DEC_MAX_FRAME 8192
78
79 static void mp3dec_post_select(__a_unused struct sched *s, struct task *t)
80 {
81 struct filter_node *fn = container_of(t, struct filter_node, task);
82 int i, ret;
83 struct private_mp3dec_data *pmd = fn->private_data;
84 struct btr_node *btrn = fn->btrn;
85 size_t loaded = 0, used, len, iqs;
86 char *inbuffer, *outbuffer;
87
88 next_buffer:
89 pmd->stream.error = 0;
90 t->error = 0;
91 iqs = btr_get_input_queue_size(btrn);
92 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
93 if (ret < 0)
94 goto err;
95 if (ret == 0)
96 return;
97 btr_merge(btrn, fn->min_iqs);
98 len = btr_next_buffer(btrn, &inbuffer);
99 /*
100 * Decode at most 8K in one go to give the post_select() functions of
101 * other buffer tree nodes a chance to run. This is necessary to avoid
102 * buffer underruns on slow machines.
103 */
104 len = PARA_MIN(len, (size_t)MP3DEC_MAX_FRAME);
105 mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
106 next_frame:
107 ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
108 if (ret < 0) {
109 used = used_mad_buffer_bytes(&pmd->stream, len);
110 btr_consume(btrn, used);
111 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
112 if (len == iqs && btr_no_parent(btrn)) {
113 ret = -E_MP3DEC_EOF;
114 goto err;
115 }
116 fn->min_iqs += 100;
117 ret = -E_MP3DEC_CORRUPT;
118 if (fn->min_iqs > MP3DEC_MAX_FRAME)
119 goto err;
120 }
121 if (loaded == 0)
122 goto next_buffer;
123 return;
124 }
125 pmd->sample_rate = pmd->frame.header.samplerate;
126 pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
127 decode:
128 ret = mad_frame_decode(&pmd->frame, &pmd->stream);
129 if (ret != 0) {
130 ret = handle_decode_error(pmd);
131 if (ret < 0)
132 goto err;
133 ret = mad_stream_sync(&pmd->stream);
134 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
135 ret = -E_MP3DEC_EOF;
136 if (len == iqs && btr_no_parent(btrn))
137 goto err;
138 fn->min_iqs += 100;
139 ret = -E_MP3DEC_CORRUPT;
140 if (fn->min_iqs > MP3DEC_MAX_FRAME)
141 goto err;
142 return;
143 }
144 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
145 goto decode;
146 used = used_mad_buffer_bytes(&pmd->stream, len);
147 btr_consume(btrn, used);
148 return;
149 }
150 fn->min_iqs = 0;
151 mad_synth_frame(&pmd->synth, &pmd->frame);
152 outbuffer = para_malloc(pmd->synth.pcm.length * 2 * pmd->channels);
153 loaded = 0;
154 for (i = 0; i < pmd->synth.pcm.length; i++) {
155 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
156 write_int16_host_endian(outbuffer + loaded, sample);
157 loaded += 2;
158 if (pmd->channels == 2) { /* stereo */
159 sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
160 write_int16_host_endian(outbuffer + loaded, sample);
161 loaded += 2;
162 }
163 }
164 btr_add_output(outbuffer, loaded, btrn);
165 goto next_frame;
166 err:
167 assert(ret < 0);
168 t->error = ret;
169 btr_remove_node(btrn);
170 }
171
172 static void mp3dec_open(struct filter_node *fn)
173 {
174 struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
175 struct mp3dec_filter_args_info *mp3_conf = fn->conf;
176
177 fn->private_data = pmd;
178 mad_stream_init(&pmd->stream);
179 mad_frame_init(&pmd->frame);
180 mad_synth_init(&pmd->synth);
181 if (mp3_conf->ignore_crc_given)
182 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
183 }
184
185 static int mp3dec_parse_config(int argc, char **argv, void **config)
186 {
187 int ret;
188 struct mp3dec_filter_args_info *mp3_conf;
189
190 mp3_conf = para_calloc(sizeof(*mp3_conf));
191 ret = -E_MP3DEC_SYNTAX;
192 if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
193 goto err;
194 *config = mp3_conf;
195 return 1;
196 err:
197 free(mp3_conf);
198 return ret;
199 }
200
201 static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
202 {
203 struct filter_node *fn = btr_context(btrn);
204 struct private_mp3dec_data *pmd = fn->private_data;
205
206 return decoder_execute(cmd, pmd->sample_rate, pmd->channels, result);
207 }
208
209 static void mp3dec_free_config(void *conf)
210 {
211 mp3dec_cmdline_parser_free(conf);
212 }
213 /**
214 * The init function of the mp3dec filter.
215 *
216 * \param f Pointer to the filter struct to initialize.
217 *
218 * \sa filter::init.
219 */
220 void mp3dec_filter_init(struct filter *f)
221 {
222 struct mp3dec_filter_args_info dummy;
223
224 mp3dec_cmdline_parser_init(&dummy);
225 f->open = mp3dec_open;
226 f->close = mp3dec_close;
227 f->parse_config = mp3dec_parse_config;
228 f->free_config = mp3dec_free_config;
229 f->pre_select = generic_filter_pre_select;
230 f->post_select = mp3dec_post_select;
231 f->execute = mp3dec_execute;
232 f->help = (struct ggo_help) {
233 .short_help = mp3dec_filter_args_info_help,
234 .detailed_help = mp3dec_filter_args_info_detailed_help
235 };
236 }