paraslash 0.7.3
[paraslash.git] / mp3dec_filter.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file mp3dec_filter.c Paraslash's mp3 decoder. */
4
5 #include <mad.h>
6 #include <regex.h>
7 #include <lopsub.h>
8
9 #include "filter_cmd.lsg.h"
10 #include "para.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "buffer_tree.h"
14 #include "filter.h"
15 #include "error.h"
16 #include "string.h"
17
18 /** Convert a sample value from libmad to a signed short. */
19 #define MAD_TO_SHORT(f) (f) >= MAD_F_ONE? SHRT_MAX :\
20         (f) <= -MAD_F_ONE? -SHRT_MAX : (signed short) ((f) >> (MAD_F_FRACBITS - 15))
21
22 /** Data specific to the mp3dec filter. */
23 struct private_mp3dec_data {
24         /** Information on the current mp3 stream. */
25         struct mad_stream stream;
26         /** Information about the frame which is currently decoded. */
27         struct mad_frame frame;
28         /** Contains the PCM output. */
29         struct mad_synth synth;
30         /** The number of channels of the current stream. */
31         unsigned int channels;
32         /** Current sample rate in Hz. */
33         unsigned int sample_rate;
34 };
35
36 /* Returns negative on serious errors. */
37 static int handle_decode_error(struct private_mp3dec_data *pmd)
38 {
39         if (!MAD_RECOVERABLE(pmd->stream.error)
40                         && pmd->stream.error != MAD_ERROR_BUFLEN) {
41                 PARA_ERROR_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
42                 return -E_MAD_FRAME_DECODE;
43         }
44         PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd->stream));
45         return 0;
46 }
47
48 static void mp3dec_consume(struct btr_node *btrn, struct mad_stream *s,
49                 size_t max)
50 {
51         size_t used;
52
53         if (!s->next_frame)
54                 used = max;
55         else { /* we still have some data */
56                 used = s->next_frame - s->buffer;
57                 assert(used <= max);
58         }
59         btr_consume(btrn, used);
60 }
61
62 static void mp3dec_close(struct filter_node *fn)
63 {
64         struct private_mp3dec_data *pmd = fn->private_data;
65
66         mad_synth_finish(&pmd->synth);
67         mad_frame_finish(&pmd->frame);
68         mad_stream_finish(&pmd->stream);
69
70         free(pmd);
71         fn->private_data = NULL;
72 }
73
74 #define MP3DEC_MAX_FRAME 8192
75
76 static int mp3dec_post_monitor(__a_unused struct sched *s, void *context)
77 {
78         struct filter_node *fn = context;
79         int i, ret;
80         struct private_mp3dec_data *pmd = fn->private_data;
81         struct btr_node *btrn = fn->btrn;
82         size_t loaded = 0, len, iqs;
83         char *inbuffer, *outbuffer;
84
85 next_buffer:
86         pmd->stream.error = 0;
87         iqs = btr_get_input_queue_size(btrn);
88         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
89         if (ret < 0)
90                 goto err;
91         if (ret == 0)
92                 return 0;
93         btr_merge(btrn, fn->min_iqs);
94         len = btr_next_buffer(btrn, &inbuffer);
95         /*
96          * Decode at most 8K in one go to give the post_monitor() functions of
97          * other buffer tree nodes a chance to run. This is necessary to avoid
98          * buffer underruns on slow machines.
99          */
100         len = PARA_MIN(len, (size_t)MP3DEC_MAX_FRAME);
101         mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
102 next_frame:
103         ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
104         if (ret < 0) {
105                 mp3dec_consume(btrn, &pmd->stream, len);
106                 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
107                         if (len == iqs && btr_no_parent(btrn)) {
108                                 ret = -E_MP3DEC_EOF;
109                                 goto err;
110                         }
111                         fn->min_iqs += 100;
112                         ret = -E_MP3DEC_CORRUPT;
113                         if (fn->min_iqs > MP3DEC_MAX_FRAME)
114                                 goto err;
115                 }
116                 if (loaded == 0)
117                         goto next_buffer;
118                 return 0;
119         }
120         pmd->sample_rate = pmd->frame.header.samplerate;
121         pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
122 decode:
123         ret = mad_frame_decode(&pmd->frame, &pmd->stream);
124         if (ret != 0) {
125                 ret = handle_decode_error(pmd);
126                 if (ret < 0)
127                         goto err;
128                 mad_stream_sync(&pmd->stream);
129                 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
130                         ret = -E_MP3DEC_EOF;
131                         if (len == iqs && btr_no_parent(btrn))
132                                 goto err;
133                         fn->min_iqs += 100;
134                         ret = -E_MP3DEC_CORRUPT;
135                         if (fn->min_iqs > MP3DEC_MAX_FRAME)
136                                 goto err;
137                         mp3dec_consume(btrn, &pmd->stream, len);
138                         return 0;
139                 }
140                 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
141                         goto decode;
142                 mp3dec_consume(btrn, &pmd->stream, len);
143                 return 0;
144         }
145         fn->min_iqs = 0;
146         mad_synth_frame(&pmd->synth, &pmd->frame);
147         outbuffer = arr_alloc(pmd->synth.pcm.length, 2 * pmd->channels);
148         loaded = 0;
149         for (i = 0; i < pmd->synth.pcm.length; i++) {
150                 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
151                 write_int16_host_endian(outbuffer + loaded, sample);
152                 loaded += 2;
153                 if (pmd->channels == 2) { /* stereo */
154                         sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
155                         write_int16_host_endian(outbuffer + loaded, sample);
156                         loaded += 2;
157                 }
158         }
159         btr_add_output(outbuffer, loaded, btrn);
160         goto next_frame;
161 err:
162         assert(ret < 0);
163         btr_remove_node(&fn->btrn);
164         return ret;
165 }
166
167 static void mp3dec_open(struct filter_node *fn)
168 {
169         struct private_mp3dec_data *pmd = zalloc(sizeof(*pmd));
170
171         fn->private_data = pmd;
172         mad_stream_init(&pmd->stream);
173         mad_frame_init(&pmd->frame);
174         mad_synth_init(&pmd->synth);
175         if (FILTER_CMD_OPT_GIVEN(MP3DEC, IGNORE_CRC, fn->lpr))
176                 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
177 }
178
179 static int mp3dec_execute(struct btr_node *btrn, const char *cmd, char **result)
180 {
181         struct filter_node *fn = btr_context(btrn);
182         struct private_mp3dec_data *pmd = fn->private_data;
183
184         return decoder_execute(cmd, pmd->sample_rate, pmd->channels, result);
185 }
186
187 const struct filter lsg_filter_cmd_com_mp3dec_user_data = {
188         .open = mp3dec_open,
189         .close = mp3dec_close,
190         .pre_monitor = generic_filter_pre_monitor,
191         .post_monitor = mp3dec_post_monitor,
192         .execute = mp3dec_execute,
193 };