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 /** Data specific to the mp3dec filter. */
26 struct private_mp3dec_data
{
27 /** Information on the current mp3 stream. */
28 struct mad_stream stream
;
29 /** Information about the frame which is currently decoded. */
30 struct mad_frame frame
;
31 /** Contains the PCM output. */
32 struct mad_synth synth
;
35 static ssize_t
mp3dec(char *inbuffer
, size_t len
, struct filter_node
*fn
)
38 struct private_mp3dec_data
*pmd
= fn
->private_data
;
39 size_t copy
= PARA_MIN(len
, (size_t)4096);
41 if (fn
->loaded
+ 16384 > fn
->bufsize
)
43 mad_stream_buffer(&pmd
->stream
, (unsigned char *) inbuffer
, copy
);
44 pmd
->stream
.error
= 0;
46 ret
= mad_header_decode(&pmd
->frame
.header
, &pmd
->stream
);
48 if (pmd
->stream
.error
!= MAD_ERROR_BUFLEN
&&
49 pmd
->stream
.error
!= MAD_ERROR_LOSTSYNC
)
50 PARA_DEBUG_LOG("header decode: %s\n",
51 mad_stream_errorstr(&pmd
->stream
));
54 fn
->fc
->samplerate
= pmd
->frame
.header
.samplerate
;
55 fn
->fc
->channels
= MAD_NCHANNELS(&pmd
->frame
.header
);
56 ret
= mad_frame_decode(&pmd
->frame
, &pmd
->stream
);
58 if (MAD_RECOVERABLE(pmd
->stream
.error
) ||
59 pmd
->stream
.error
== MAD_ERROR_BUFLEN
) {
60 PARA_DEBUG_LOG("frame decode: %s\n",
61 mad_stream_errorstr(&pmd
->stream
));
64 PARA_ERROR_LOG("frame decode: %s\n",
65 mad_stream_errorstr(&pmd
->stream
));
66 return -E_MAD_FRAME_DECODE
;
68 mad_synth_frame(&pmd
->synth
, &pmd
->frame
);
70 for (i
= 0; i
< pmd
->synth
.pcm
.length
; i
++) {
71 int s
= MAD_TO_SHORT(pmd
->synth
.pcm
.samples
[0][i
]);
72 write_int16_host_endian(fn
->buf
+ fn
->loaded
, s
);
74 if (MAD_NCHANNELS(&pmd
->frame
.header
) == 2) { /* stereo */
75 s
= MAD_TO_SHORT(pmd
->synth
.pcm
.samples
[1][i
]);
76 write_int16_host_endian(fn
->buf
+ fn
->loaded
, s
);
79 if (fn
->loaded
!= fn
->bufsize
) /* output buffer not full */
81 PARA_ERROR_LOG("output buffer full: %zd\n", fn
->loaded
);
82 return -E_MP3DEC_OVERRUN
;
84 if (fn
->loaded
+ 16384 <= fn
->bufsize
)
87 if (pmd
->stream
.next_frame
) { /* we still have some data */
88 size_t off
= pmd
->stream
.bufend
- pmd
->stream
.next_frame
;
89 if (fn
->loaded
+ 16384 <= fn
->bufsize
&& off
> 2048)
96 static void mp3dec_close(struct filter_node
*fn
)
98 struct private_mp3dec_data
*pmd
= fn
->private_data
;
100 mad_synth_finish(&pmd
->synth
);
101 mad_frame_finish(&pmd
->frame
);
102 mad_stream_finish(&pmd
->stream
);
107 fn
->private_data
= NULL
;
110 static void mp3dec_open(struct filter_node
*fn
)
112 struct private_mp3dec_data
*pmd
= para_calloc(sizeof(*pmd
));
113 struct mp3dec_filter_args_info
*mp3_conf
= fn
->conf
;
115 fn
->private_data
= pmd
;
116 mad_stream_init(&pmd
->stream
);
117 mad_frame_init(&pmd
->frame
);
118 mad_synth_init(&pmd
->synth
);
120 fn
->bufsize
= mp3_conf
->bufsize_arg
* 1024;
121 fn
->buf
= para_calloc(fn
->bufsize
);
122 if (mp3_conf
->ignore_crc_given
)
123 mad_stream_options(&pmd
->stream
, MAD_OPTION_IGNORECRC
);
126 static int mp3dec_parse_config(int argc
, char **argv
, void **config
)
129 struct mp3dec_filter_args_info
*mp3_conf
;
131 mp3_conf
= para_calloc(sizeof(*mp3_conf
));
132 ret
= -E_MP3DEC_SYNTAX
;
133 if (mp3dec_cmdline_parser(argc
, argv
, mp3_conf
))
135 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
136 if (mp3_conf
->bufsize_arg
< 32)
138 if (mp3_conf
->bufsize_arg
>= INT_MAX
/ 1024)
148 * The init function of the mp3dec filter.
150 * \param f Pointer to the filter struct to initialize.
154 void mp3dec_filter_init(struct filter
*f
)
156 struct mp3dec_filter_args_info dummy
;
158 mp3dec_cmdline_parser_init(&dummy
);
159 f
->open
= mp3dec_open
;
161 f
->close
= mp3dec_close
;
162 f
->parse_config
= mp3dec_parse_config
;
163 f
->help
= (struct ggo_help
) {
164 .short_help
= mp3dec_filter_args_info_help
,
165 .detailed_help
= mp3dec_filter_args_info_detailed_help