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