Various wma fixes.
[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
12 #include "para.h"
13 #include "mp3dec_filter.cmdline.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "filter.h"
18 #include "error.h"
19 #include "string.h"
20
21 /** Convert a sample value from libmad to a signed short. */
22 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
23         (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
24
25 /** State of the decoding process. */
26 enum mp3dec_flags {
27         /** Bad main_data_begin pointer encounterd. */
28         MP3DEC_FLAG_BAD_DATA = 1,
29         /** Some output has already been produced. */
30         MP3DEC_FLAG_DECODE_STARTED = 2,
31 };
32
33 /** Data specific to the mp3dec filter. */
34 struct private_mp3dec_data {
35         /** Information on the current mp3 stream. */
36         struct mad_stream stream;
37         /** Information about the frame which is currently decoded. */
38         struct mad_frame frame;
39         /** Contains the PCM output. */
40         struct mad_synth synth;
41         /** See \ref mp3dec_flags. */
42         unsigned flags;
43         /** Defer decoding until this time. */
44         struct timeval stream_start_barrier;
45         /** Wait until this many input bytes are available. */
46         size_t input_len_barrier;
47 };
48
49 static int need_bad_data_delay(struct private_mp3dec_data *pmd,
50                 size_t bytes_available)
51 {
52         if (!(pmd->flags & MP3DEC_FLAG_BAD_DATA))
53                 return 0;
54         if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED)
55                 return 0;
56         if (bytes_available >= pmd->input_len_barrier)
57                 return 0;
58         if (tv_diff(now, &pmd->stream_start_barrier, NULL) > 0)
59                 return 0;
60         return 1;
61 }
62
63 /*
64  * Returns negative on serious errors, zero if the error should be ignored and
65  * positive on bad data pointer errors at stream start.
66  */
67 static int handle_decode_error(struct private_mp3dec_data *pmd, size_t len)
68 {
69         if (!MAD_RECOVERABLE(pmd->stream.error)
70                         && pmd->stream.error != MAD_ERROR_BUFLEN) {
71                 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
72                 return -E_MAD_FRAME_DECODE;
73         }
74         PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
75         if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
76                 return 0;
77         if (pmd->flags & MP3DEC_FLAG_DECODE_STARTED)
78                 return 0;
79         /*
80          * Bad data pointer at stream start. Defer decoding until the amount of
81          * data we are about to skip is available again, but wait at most 60ms.
82          */
83         pmd->flags |= MP3DEC_FLAG_BAD_DATA;
84         pmd->input_len_barrier = len;
85         tv_add(now, &(struct timeval){0, 60 * 1000},
86                 &pmd->stream_start_barrier);
87         return 1;
88 }
89
90 static ssize_t mp3dec(char *inbuffer, size_t len, struct filter_node *fn)
91 {
92         int i, ret;
93         struct private_mp3dec_data *pmd = fn->private_data;
94         size_t copy = PARA_MIN(len, (size_t)4096);
95
96         if (fn->loaded + 16384 > fn->bufsize)
97                 return 0;
98         if (need_bad_data_delay(pmd, len))
99                 return 0;
100         mad_stream_buffer(&pmd->stream, (unsigned char *) inbuffer, copy);
101         pmd->stream.error = 0;
102 next_frame:
103         ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
104         if (ret < 0) {
105                 if (pmd->stream.error != MAD_ERROR_BUFLEN &&
106                         pmd->stream.error != MAD_ERROR_LOSTSYNC)
107                         PARA_DEBUG_LOG("header decode: %s\n",
108                                 mad_stream_errorstr(&pmd->stream));
109                 goto out;
110         }
111         fn->fc->samplerate = pmd->frame.header.samplerate;
112         fn->fc->channels = MAD_NCHANNELS(&pmd->frame.header);
113         ret = mad_frame_decode(&pmd->frame, &pmd->stream);
114         if (ret != 0) {
115                 ret = handle_decode_error(pmd, len);
116                 if (ret < 0)
117                         return ret;
118                 if (ret == 0)
119                         goto out;
120                 ret = copy - (pmd->stream.bufend - pmd->stream.next_frame);
121                 PARA_NOTICE_LOG("skipping %d input bytes\n", ret);
122                 return ret;
123         }
124         mad_synth_frame(&pmd->synth, &pmd->frame);
125         pmd->flags |= MP3DEC_FLAG_DECODE_STARTED;
126
127         for (i = 0; i < pmd->synth.pcm.length; i++) {
128                 int s = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
129                 write_int16_host_endian(fn->buf + fn->loaded, s);
130                 fn->loaded += 2;
131                 if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
132                         s = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
133                         write_int16_host_endian(fn->buf + fn->loaded, s);
134                         fn->loaded += 2;
135                 }
136                 if (fn->loaded != fn->bufsize) /* output buffer not full */
137                         continue;
138                 PARA_ERROR_LOG("output buffer full: %zd\n", fn->loaded);
139                         return -E_MP3DEC_OVERRUN;
140         }
141         if (fn->loaded + 16384 <= fn->bufsize)
142                 goto next_frame;
143 out:
144         if (pmd->stream.next_frame) { /* we still have some data */
145                 size_t off = pmd->stream.bufend - pmd->stream.next_frame;
146                 if (fn->loaded + 16384 <= fn->bufsize && off > 2048)
147                         goto next_frame;
148                 return copy - off;
149         }
150         return copy;
151 }
152
153 static void mp3dec_close(struct filter_node *fn)
154 {
155         struct private_mp3dec_data *pmd = fn->private_data;
156
157         mad_synth_finish(&pmd->synth);
158         mad_frame_finish(&pmd->frame);
159         mad_stream_finish(&pmd->stream);
160
161         free(fn->buf);
162         fn->buf = NULL;
163         free(pmd);
164         fn->private_data = NULL;
165 }
166
167 static void mp3dec_open(struct filter_node *fn)
168 {
169         struct private_mp3dec_data *pmd = para_calloc(sizeof(*pmd));
170         struct mp3dec_filter_args_info *mp3_conf = fn->conf;
171
172         fn->private_data = pmd;
173         mad_stream_init(&pmd->stream);
174         mad_frame_init(&pmd->frame);
175         mad_synth_init(&pmd->synth);
176         fn->loaded = 0;
177         fn->bufsize = mp3_conf->bufsize_arg * 1024;
178         fn->buf = para_calloc(fn->bufsize);
179         if (mp3_conf->ignore_crc_given)
180                 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
181 }
182
183 static int mp3dec_parse_config(int argc, char **argv, void **config)
184 {
185         int ret;
186         struct mp3dec_filter_args_info *mp3_conf;
187
188         mp3_conf = para_calloc(sizeof(*mp3_conf));
189         ret = -E_MP3DEC_SYNTAX;
190         if (mp3dec_cmdline_parser(argc, argv, mp3_conf))
191                 goto err;
192         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
193         if (mp3_conf->bufsize_arg < 32)
194                 goto err;
195         if (mp3_conf->bufsize_arg >= INT_MAX / 1024)
196                 goto err;
197         *config = mp3_conf;
198         return 1;
199 err:
200         free(mp3_conf);
201         return ret;
202 }
203
204 /**
205  * The init function of the mp3dec filter.
206  *
207  * \param f Pointer to the filter struct to initialize.
208  *
209  * \sa filter::init.
210  */
211 void mp3dec_filter_init(struct filter *f)
212 {
213         struct mp3dec_filter_args_info dummy;
214
215         mp3dec_cmdline_parser_init(&dummy);
216         f->open = mp3dec_open;
217         f->convert = mp3dec;
218         f->close = mp3dec_close;
219         f->parse_config = mp3dec_parse_config;
220         f->help = (struct ggo_help) {
221                 .short_help = mp3dec_filter_args_info_help,
222                 .detailed_help = mp3dec_filter_args_info_detailed_help
223         };
224 }