small compilation fix
[mplayer/glamo.git] / libmpcodecs / ad_libmad.c
blob67f35ef97b47c7920d7e0e416f932bb856ba2ab7
1 // SAMPLE audio decoder - you can use this file as template when creating new codec!
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 #include "config.h"
9 #ifdef USE_LIBMAD
11 #include "ad_internal.h"
13 static ad_info_t info = {
14 "libmad mpeg audio decoder",
15 "libmad",
16 "A'rpi",
17 "libmad...",
18 "based on Xine's libmad/xine_decoder.c"
21 LIBAD_EXTERN(libmad)
23 #include <mad.h>
25 typedef struct mad_decoder_s {
27 struct mad_synth synth;
28 struct mad_stream stream;
29 struct mad_frame frame;
31 int have_frame;
33 int output_sampling_rate;
34 int output_open;
35 int output_mode;
37 } mad_decoder_t;
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));
43 sh->context = this;
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;
52 return 1;
55 static int read_frame(sh_audio_t *sh){
56 mad_decoder_t *this = (mad_decoder_t *) sh->context;
57 int len;
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;
62 while(1){
63 int ret;
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) {
67 int num_bytes =
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");
79 return 0;
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;
91 sh->samplesize=2;
93 return 1;
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);
101 free(sh->context);
104 /* utility to scale and round samples to 16 bits */
105 static inline signed int scale(mad_fixed_t sample) {
106 /* round */
107 sample += (1L << (MAD_F_FRACBITS - 16));
109 /* clip */
110 if (sample >= MAD_F_ONE)
111 sample = MAD_F_ONE - 1;
112 else if (sample < -MAD_F_ONE)
113 sample = -MAD_F_ONE;
115 /* quantize */
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;
121 int len=0;
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
126 this->have_frame=0;
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;
143 while (nsamples--) {
144 /* output sample(s) in 16-bit signed little-endian PCM */
146 *output++ = scale(*left_ch++);
148 if (nchannels == 2)
149 *output++ = scale(*right_ch++);
155 return len?len:-1;
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:
161 switch(cmd){
162 case ADCTRL_RESYNC_STREAM:
163 this->have_frame=0;
164 mad_synth_init (&this->synth);
165 mad_stream_init (&this->stream);
166 mad_frame_init (&this->frame);
167 return CONTROL_TRUE;
168 case ADCTRL_SKIP_FRAME:
169 this->have_frame=read_frame(sh);
170 return CONTROL_TRUE;
172 return CONTROL_UNKNOWN;
174 #endif