2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mp3dec_filter.c Paraslash's mp3 decoder. */
13 #include "mp3dec_filter.cmdline.h"
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))
25 /** State of the decoding process. */
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,
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. */
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
;
49 static int need_bad_data_delay(struct private_mp3dec_data
*pmd
,
50 size_t bytes_available
)
52 if (!(pmd
->flags
& MP3DEC_FLAG_BAD_DATA
))
54 if (pmd
->flags
& MP3DEC_FLAG_DECODE_STARTED
)
56 if (bytes_available
>= pmd
->input_len_barrier
)
58 if (tv_diff(now
, &pmd
->stream_start_barrier
, NULL
) > 0)
64 * Returns negative on serious errors, zero if the error should be ignored and
65 * positive on bad data pointer errors at stream start.
67 static int handle_decode_error(struct private_mp3dec_data
*pmd
, size_t len
)
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
;
74 PARA_DEBUG_LOG("%s\n", mad_stream_errorstr(&pmd
->stream
));
75 if (pmd
->stream
.error
!= MAD_ERROR_BADDATAPTR
)
77 if (pmd
->flags
& MP3DEC_FLAG_DECODE_STARTED
)
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.
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
);
90 static ssize_t
mp3dec(char *inbuffer
, size_t len
, struct filter_node
*fn
)
93 struct private_mp3dec_data
*pmd
= fn
->private_data
;
94 size_t copy
= PARA_MIN(len
, (size_t)4096);
96 if (fn
->loaded
+ 16384 > fn
->bufsize
)
98 if (need_bad_data_delay(pmd
, len
))
100 mad_stream_buffer(&pmd
->stream
, (unsigned char *) inbuffer
, copy
);
101 pmd
->stream
.error
= 0;
103 ret
= mad_header_decode(&pmd
->frame
.header
, &pmd
->stream
);
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
));
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
);
115 ret
= handle_decode_error(pmd
, len
);
120 ret
= copy
- (pmd
->stream
.bufend
- pmd
->stream
.next_frame
);
121 PARA_NOTICE_LOG("skipping %d input bytes\n", ret
);
124 mad_synth_frame(&pmd
->synth
, &pmd
->frame
);
125 pmd
->flags
|= MP3DEC_FLAG_DECODE_STARTED
;
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
);
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
);
136 if (fn
->loaded
!= fn
->bufsize
) /* output buffer not full */
138 PARA_ERROR_LOG("output buffer full: %zd\n", fn
->loaded
);
139 return -E_MP3DEC_OVERRUN
;
141 if (fn
->loaded
+ 16384 <= fn
->bufsize
)
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)
153 static void mp3dec_close(struct filter_node
*fn
)
155 struct private_mp3dec_data
*pmd
= fn
->private_data
;
157 mad_synth_finish(&pmd
->synth
);
158 mad_frame_finish(&pmd
->frame
);
159 mad_stream_finish(&pmd
->stream
);
164 fn
->private_data
= NULL
;
167 static void mp3dec_open(struct filter_node
*fn
)
169 struct private_mp3dec_data
*pmd
= para_calloc(sizeof(*pmd
));
170 struct mp3dec_filter_args_info
*mp3_conf
= fn
->conf
;
172 fn
->private_data
= pmd
;
173 mad_stream_init(&pmd
->stream
);
174 mad_frame_init(&pmd
->frame
);
175 mad_synth_init(&pmd
->synth
);
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
);
183 static int mp3dec_parse_config(int argc
, char **argv
, void **config
)
186 struct mp3dec_filter_args_info
*mp3_conf
;
188 mp3_conf
= para_calloc(sizeof(*mp3_conf
));
189 ret
= -E_MP3DEC_SYNTAX
;
190 if (mp3dec_cmdline_parser(argc
, argv
, mp3_conf
))
192 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
193 if (mp3_conf
->bufsize_arg
< 32)
195 if (mp3_conf
->bufsize_arg
>= INT_MAX
/ 1024)
205 * The init function of the mp3dec filter.
207 * \param f Pointer to the filter struct to initialize.
211 void mp3dec_filter_init(struct filter
*f
)
213 struct mp3dec_filter_args_info dummy
;
215 mp3dec_cmdline_parser_init(&dummy
);
216 f
->open
= mp3dec_open
;
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