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