2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "ad_internal.h"
27 static const ad_info_t info
= {
28 "libmad mpeg audio decoder",
32 "based on Xine's libmad/xine_decoder.c"
39 typedef struct mad_decoder_s
{
41 struct mad_synth synth
;
42 struct mad_stream stream
;
43 struct mad_frame frame
;
47 int output_sampling_rate
;
53 static int preinit(sh_audio_t
*sh
){
55 mad_decoder_t
*this = malloc(sizeof(mad_decoder_t
));
56 memset(this,0,sizeof(mad_decoder_t
));
59 mad_synth_init (&this->synth
);
60 mad_stream_init (&this->stream
);
61 mad_frame_init (&this->frame
);
63 sh
->audio_out_minsize
=2*4608;
64 sh
->audio_in_minsize
=4096;
69 static int read_frame(sh_audio_t
*sh
){
70 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
73 while((len
=demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
74 sh
->a_in_buffer_size
-sh
->a_in_buffer_len
))>0){
75 sh
->a_in_buffer_len
+=len
;
78 mad_stream_buffer (&this->stream
, sh
->a_in_buffer
, sh
->a_in_buffer_len
);
79 ret
=mad_frame_decode (&this->frame
, &this->stream
);
80 if (this->stream
.next_frame
) {
82 (char*)sh
->a_in_buffer
+sh
->a_in_buffer_len
- (char*)this->stream
.next_frame
;
83 memmove(sh
->a_in_buffer
, this->stream
.next_frame
, num_bytes
);
84 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"libmad: %d bytes processed\n",sh
->a_in_buffer_len
-num_bytes
);
85 sh
->a_in_buffer_len
= num_bytes
;
87 if (ret
== 0) return 1; // OK!!!
88 // error! try to resync!
89 if(this->stream
.error
==MAD_ERROR_BUFLEN
) break;
92 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,"Cannot sync MAD frame\n");
96 static int init(sh_audio_t
*sh
){
97 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
99 this->have_frame
=read_frame(sh
);
100 if(!this->have_frame
) return 0; // failed to sync...
102 sh
->channels
=(this->frame
.header
.mode
== MAD_MODE_SINGLE_CHANNEL
) ? 1 : 2;
103 sh
->samplerate
=this->frame
.header
.samplerate
;
104 sh
->i_bps
=this->frame
.header
.bitrate
/8;
110 static void uninit(sh_audio_t
*sh
){
111 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
112 mad_synth_finish (&this->synth
);
113 mad_frame_finish (&this->frame
);
114 mad_stream_finish(&this->stream
);
118 /* utility to scale and round samples to 16 bits */
119 static inline signed int scale(mad_fixed_t sample
) {
121 sample
+= (1L << (MAD_F_FRACBITS
- 16));
124 if (sample
>= MAD_F_ONE
)
125 sample
= MAD_F_ONE
- 1;
126 else if (sample
< -MAD_F_ONE
)
130 return sample
>> (MAD_F_FRACBITS
+ 1 - 16);
133 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
134 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
137 while(len
<minlen
&& len
+4608<=maxlen
){
138 if(!this->have_frame
) this->have_frame
=read_frame(sh
);
139 if(!this->have_frame
) break; // failed to sync... or EOF
142 mad_synth_frame (&this->synth
, &this->frame
);
144 { unsigned int nchannels
, nsamples
;
145 mad_fixed_t
const *left_ch
, *right_ch
;
146 struct mad_pcm
*pcm
= &this->synth
.pcm
;
147 uint16_t *output
= (uint16_t*) buf
;
149 nchannels
= pcm
->channels
;
150 nsamples
= pcm
->length
;
151 left_ch
= pcm
->samples
[0];
152 right_ch
= pcm
->samples
[1];
154 len
+=2*nchannels
*nsamples
;
155 buf
+=2*nchannels
*nsamples
;
158 /* output sample(s) in 16-bit signed little-endian PCM */
160 *output
++ = scale(*left_ch
++);
163 *output
++ = scale(*right_ch
++);
172 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
173 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
174 // various optional functions you MAY implement:
176 case ADCTRL_RESYNC_STREAM
:
178 mad_synth_init (&this->synth
);
179 mad_stream_init (&this->stream
);
180 mad_frame_init (&this->frame
);
182 case ADCTRL_SKIP_FRAME
:
183 this->have_frame
=read_frame(sh
);
186 return CONTROL_UNKNOWN
;