1 // SAMPLE audio decoder - you can use this file as template when creating new codec!
11 #include "ad_internal.h"
13 static ad_info_t info
= {
14 "libmad mpeg audio decoder",
18 "based on Xine's libmad/xine_decoder.c"
25 typedef struct mad_decoder_s
{
27 struct mad_synth synth
;
28 struct mad_stream stream
;
29 struct mad_frame frame
;
33 int output_sampling_rate
;
39 static int preinit(sh_audio_t
*sh
){
41 mad_decoder_t
*this = (mad_decoder_t
*) malloc(sizeof(mad_decoder_t
));
42 memset(this,0,sizeof(mad_decoder_t
));
45 mad_synth_init (&this->synth
);
46 mad_stream_init (&this->stream
);
47 mad_frame_init (&this->frame
);
49 sh
->audio_out_minsize
=2*4608;
50 sh
->audio_in_minsize
=4096;
55 static int read_frame(sh_audio_t
*sh
){
56 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
59 while((len
=demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
60 sh
->a_in_buffer_size
-sh
->a_in_buffer_len
))>0){
61 sh
->a_in_buffer_len
+=len
;
64 mad_stream_buffer (&this->stream
, sh
->a_in_buffer
, sh
->a_in_buffer_len
);
65 ret
=mad_frame_decode (&this->frame
, &this->stream
);
66 if (this->stream
.next_frame
) {
68 (char*)sh
->a_in_buffer
+sh
->a_in_buffer_len
- (char*)this->stream
.next_frame
;
69 memmove(sh
->a_in_buffer
, this->stream
.next_frame
, num_bytes
);
70 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"libmad: %d bytes processed\n",sh
->a_in_buffer_len
-num_bytes
);
71 sh
->a_in_buffer_len
= num_bytes
;
73 if (ret
== 0) return 1; // OK!!!
74 // error! try to resync!
75 if(this->stream
.error
==MAD_ERROR_BUFLEN
) break;
78 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,"Cannot sync MAD frame\n");
82 static int init(sh_audio_t
*sh
){
83 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
85 this->have_frame
=read_frame(sh
);
86 if(!this->have_frame
) return 0; // failed to sync...
88 sh
->channels
=(this->frame
.header
.mode
== MAD_MODE_SINGLE_CHANNEL
) ? 1 : 2;
89 sh
->samplerate
=this->frame
.header
.samplerate
;
90 sh
->i_bps
=this->frame
.header
.bitrate
/8;
96 static void uninit(sh_audio_t
*sh
){
97 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
98 mad_synth_finish (&this->synth
);
99 mad_frame_finish (&this->frame
);
100 mad_stream_finish(&this->stream
);
104 /* utility to scale and round samples to 16 bits */
105 static inline signed int scale(mad_fixed_t sample
) {
107 sample
+= (1L << (MAD_F_FRACBITS
- 16));
110 if (sample
>= MAD_F_ONE
)
111 sample
= MAD_F_ONE
- 1;
112 else if (sample
< -MAD_F_ONE
)
116 return sample
>> (MAD_F_FRACBITS
+ 1 - 16);
119 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
120 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
123 while(len
<minlen
&& len
+4608<=maxlen
){
124 if(!this->have_frame
) this->have_frame
=read_frame(sh
);
125 if(!this->have_frame
) break; // failed to sync... or EOF
128 mad_synth_frame (&this->synth
, &this->frame
);
130 { unsigned int nchannels
, nsamples
;
131 mad_fixed_t
const *left_ch
, *right_ch
;
132 struct mad_pcm
*pcm
= &this->synth
.pcm
;
133 uint16_t *output
= (uint16_t*) buf
;
135 nchannels
= pcm
->channels
;
136 nsamples
= pcm
->length
;
137 left_ch
= pcm
->samples
[0];
138 right_ch
= pcm
->samples
[1];
140 len
+=2*nchannels
*nsamples
;
141 buf
+=2*nchannels
*nsamples
;
144 /* output sample(s) in 16-bit signed little-endian PCM */
146 *output
++ = scale(*left_ch
++);
149 *output
++ = scale(*right_ch
++);
158 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
159 mad_decoder_t
*this = (mad_decoder_t
*) sh
->context
;
160 // various optional functions you MAY implement:
162 case ADCTRL_RESYNC_STREAM
:
164 mad_synth_init (&this->synth
);
165 mad_stream_init (&this->stream
);
166 mad_frame_init (&this->frame
);
168 case ADCTRL_SKIP_FRAME
:
169 this->have_frame
=read_frame(sh
);
172 return CONTROL_UNKNOWN
;