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