ao_write: Avoid segfault on exit.
[paraslash.git] / mp3dec_filter.c
1 /*
2  * Copyright (C) 2005-2014 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
12 #include "para.h"
13 #include "mp3dec_filter.cmdline.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 /** Convert a sample value from libmad to a signed short. */
23 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
24         (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
25
26 /** Data specific to the mp3dec filter. */
27 struct private_mp3dec_data {
28         /** Information on the current mp3 stream. */
29         struct mad_stream stream;
30         /** Information about the frame which is currently decoded. */
31         struct mad_frame frame;
32         /** Contains the PCM output. */
33         struct mad_synth synth;
34         /** The number of channels of the current stream. */
35         unsigned int channels;
36         /** Current sample rate in Hz. */
37         unsigned int sample_rate;
38 };
39
40 /* Returns negative on serious errors. */
41 static int handle_decode_error(struct private_mp3dec_data *pmd)
42 {
43         if (!MAD_RECOVERABLE(pmd->stream.error)
44                         && pmd->stream.error != MAD_ERROR_BUFLEN) {
45                 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
46                 return -E_MAD_FRAME_DECODE;
47         }
48         PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
49         return 0;
50 }
51
52 static void mp3dec_consume(struct btr_node *btrn, struct mad_stream *s,
53                 size_t max)
54 {
55         size_t used;
56
57         if (!s->next_frame)
58                 used = max;
59         else { /* we still have some data */
60                 used = s->next_frame - s->buffer;
61                 assert(used <= max);
62         }
63         btr_consume(btrn, used);
64 }
65
66 static void mp3dec_close(struct filter_node *fn)
67 {
68         struct private_mp3dec_data *pmd = fn->private_data;
69
70         mad_synth_finish(&pmd->synth);
71         mad_frame_finish(&pmd->frame);
72         mad_stream_finish(&pmd->stream);
73
74         free(pmd);
75         fn->private_data = NULL;
76 }
77
78 #define MP3DEC_MAX_FRAME 8192
79
80 static int mp3dec_post_select(__a_unused struct sched *s, struct task *t)
81 {
82         struct filter_node *fn = container_of(t, struct filter_node, task);
83         int i, ret;
84         struct private_mp3dec_data *pmd = fn->private_data;
85         struct btr_node *btrn = fn->btrn;
86         size_t loaded = 0, len, iqs;
87         char *inbuffer, *outbuffer;
88
89 next_buffer:
90         pmd->stream.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 0;
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                 mp3dec_consume(btrn, &pmd->stream, len);
110                 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
111                         if (len == iqs && btr_no_parent(btrn)) {
112                                 ret = -E_MP3DEC_EOF;
113                                 goto err;
114                         }
115                         fn->min_iqs += 100;
116                         ret = -E_MP3DEC_CORRUPT;
117                         if (fn->min_iqs > MP3DEC_MAX_FRAME)
118                                 goto err;
119                 }
120                 if (loaded == 0)
121                         goto next_buffer;
122                 return 0;
123         }
124         pmd->sample_rate = pmd->frame.header.samplerate;
125         pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
126 decode:
127         ret = mad_frame_decode(&pmd->frame, &pmd->stream);
128         if (ret != 0) {
129                 ret = handle_decode_error(pmd);
130                 if (ret < 0)
131                         goto err;
132                 mad_stream_sync(&pmd->stream);
133                 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
134                         ret = -E_MP3DEC_EOF;
135                         if (len == iqs && btr_no_parent(btrn))
136                                 goto err;
137                         fn->min_iqs += 100;
138                         ret = -E_MP3DEC_CORRUPT;
139                         if (fn->min_iqs > MP3DEC_MAX_FRAME)
140                                 goto err;
141                         mp3dec_consume(btrn, &pmd->stream, len);
142                         return 0;
143                 }
144                 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
145                         goto decode;
146                 mp3dec_consume(btrn, &pmd->stream, len);
147                 return 0;
148         }
149         fn->min_iqs = 0;
150         mad_synth_frame(&pmd->synth, &pmd->frame);
151         outbuffer = para_malloc(pmd->synth.pcm.length * 2 * pmd->channels);
152         loaded = 0;
153         for (i = 0; i < pmd->synth.pcm.length; i++) {
154                 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
155                 write_int16_host_endian(outbuffer + loaded, sample);
156                 loaded += 2;
157                 if (pmd->channels == 2) { /* stereo */
158                         sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
159                         write_int16_host_endian(outbuffer + loaded, sample);
160                         loaded += 2;
161                 }
162         }
163         btr_add_output(outbuffer, loaded, btrn);
164         goto next_frame;
165 err:
166         assert(ret < 0);
167         btr_remove_node(&fn->btrn);
168         return ret;
169 }
170
171 static void mp3dec_open(struct filter_node *fn)
172 {
173         struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
174         struct mp3dec_filter_args_info *mp3_conf = fn->conf;
175
176         fn->private_data = pmd;
177         mad_stream_init(&pmd->stream);
178         mad_frame_init(&pmd->frame);
179         mad_synth_init(&pmd->synth);
180         if (mp3_conf->ignore_crc_given)
181                 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
182 }
183
184 static int mp3dec_parse_config(int argc, char **argv, void **config)
185 {
186         struct mp3dec_filter_args_info *conf = para_calloc(sizeof(*conf));
187
188         mp3dec_filter_cmdline_parser(argc, argv, conf);
189         *config = conf;
190         return 1;
191 }
192
193 static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
194 {
195         struct filter_node *fn = btr_context(btrn);
196         struct private_mp3dec_data *pmd = fn->private_data;
197
198         return decoder_execute(cmd, pmd->sample_rate, pmd->channels, result);
199 }
200
201 static void mp3dec_free_config(void *conf)
202 {
203         mp3dec_filter_cmdline_parser_free(conf);
204 }
205 /**
206  * The init function of the mp3dec filter.
207  *
208  * \param f Pointer to the filter struct to initialize.
209  *
210  * \sa filter::init.
211  */
212 void mp3dec_filter_init(struct filter *f)
213 {
214         struct mp3dec_filter_args_info dummy;
215
216         mp3dec_filter_cmdline_parser_init(&dummy);
217         f->open = mp3dec_open;
218         f->close = mp3dec_close;
219         f->parse_config = mp3dec_parse_config;
220         f->free_config = mp3dec_free_config;
221         f->pre_select = generic_filter_pre_select;
222         f->post_select = mp3dec_post_select;
223         f->execute = mp3dec_execute;
224         f->help = (struct ggo_help)DEFINE_GGO_HELP(mp3dec_filter);
225 }