]> git.tuebingen.mpg.de Git - paraslash.git/blob - mp3dec_filter.c
Merge topic branch t/sf_float into pu
[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;
83         char *inbuffer, *outbuffer;
84
85 next_buffer:
86         pmd->stream.error = 0;
87         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
88         if (ret < 0)
89                 goto err;
90         if (ret == 0)
91                 return 0;
92         btr_merge(btrn, fn->min_iqs);
93         len = btr_next_buffer(btrn, &inbuffer);
94         /*
95          * Decode at most 8K in one go to give the post_monitor() functions of
96          * other buffer tree nodes a chance to run. This is necessary to avoid
97          * buffer underruns on slow machines.
98          */
99         len = PARA_MIN(len, (size_t)MP3DEC_MAX_FRAME);
100         mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
101 next_frame:
102         ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
103         if (ret < 0) {
104                 mp3dec_consume(btrn, &pmd->stream, len);
105                 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
106                         if (fn->min_iqs > 0 && btr_no_parent(btrn)) {
107                                 ret = -E_EOF;
108                                 goto err;
109                         }
110                         fn->min_iqs += 100;
111                         ret = -E_MP3DEC_CORRUPT;
112                         if (fn->min_iqs > MP3DEC_MAX_FRAME)
113                                 goto err;
114                 }
115                 if (loaded == 0)
116                         goto next_buffer;
117                 return 0;
118         }
119         pmd->sample_rate = pmd->frame.header.samplerate;
120         pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
121 decode:
122         ret = mad_frame_decode(&pmd->frame, &pmd->stream);
123         if (ret != 0) {
124                 ret = handle_decode_error(pmd);
125                 if (ret < 0)
126                         goto err;
127                 mad_stream_sync(&pmd->stream);
128                 if (pmd->stream.error == MAD_ERROR_BUFLEN) {
129                         ret = -E_EOF;
130                         if (btr_no_parent(btrn))
131                                 goto err;
132                         fn->min_iqs += 100;
133                         ret = -E_MP3DEC_CORRUPT;
134                         if (fn->min_iqs > MP3DEC_MAX_FRAME)
135                                 goto err;
136                         mp3dec_consume(btrn, &pmd->stream, len);
137                         return 0;
138                 }
139                 if (pmd->stream.error != MAD_ERROR_BADDATAPTR)
140                         goto decode;
141                 mp3dec_consume(btrn, &pmd->stream, len);
142                 return 0;
143         }
144         fn->min_iqs = 0;
145         mad_synth_frame(&pmd->synth, &pmd->frame);
146         outbuffer = arr_alloc(pmd->synth.pcm.length, 2 * pmd->channels);
147         loaded = 0;
148         for (i = 0; i < pmd->synth.pcm.length; i++) {
149                 int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
150                 write_int16_host_endian(outbuffer + loaded, sample);
151                 loaded += 2;
152                 if (pmd->channels == 2) { /* stereo */
153                         sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
154                         write_int16_host_endian(outbuffer + loaded, sample);
155                         loaded += 2;
156                 }
157         }
158         btr_add_output(outbuffer, loaded, btrn);
159         goto next_frame;
160 err:
161         assert(ret < 0);
162         btr_remove_node(&fn->btrn);
163         return ret;
164 }
165
166 static void mp3dec_open(struct filter_node *fn)
167 {
168         struct private_mp3dec_data *pmd = zalloc(sizeof(*pmd));
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         if (FILTER_CMD_OPT_GIVEN(MP3DEC, IGNORE_CRC, fn->lpr))
175                 mad_stream_options(&pmd->stream, MAD_OPTION_IGNORECRC);
176 }
177
178 static int mp3dec_execute(const struct btr_node *btrn, const char *cmd,
179                 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 };