vo_gl: Some small whitespace and {} cosmetics
[mplayer/glamo.git] / libmpcodecs / ad_libmad.c
blob08dbe72857517cc3ef4d615ef6c6ee43fa65f4c5
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 #include "config.h"
25 #include "ad_internal.h"
27 static const ad_info_t info = {
28 "libmad mpeg audio decoder",
29 "libmad",
30 "A'rpi",
31 "libmad...",
32 "based on Xine's libmad/xine_decoder.c"
35 LIBAD_EXTERN(libmad)
37 #include <mad.h>
39 typedef struct mad_decoder_s {
41 struct mad_synth synth;
42 struct mad_stream stream;
43 struct mad_frame frame;
45 int have_frame;
47 int output_sampling_rate;
48 int output_open;
49 int output_mode;
51 } mad_decoder_t;
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));
57 sh->context = this;
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;
66 return 1;
69 static int read_frame(sh_audio_t *sh){
70 mad_decoder_t *this = (mad_decoder_t *) sh->context;
71 int len;
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;
76 while(1){
77 int ret;
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) {
81 int num_bytes =
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");
93 return 0;
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;
105 sh->samplesize=2;
107 return 1;
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);
115 free(sh->context);
118 /* utility to scale and round samples to 16 bits */
119 static inline signed int scale(mad_fixed_t sample) {
120 /* round */
121 sample += (1L << (MAD_F_FRACBITS - 16));
123 /* clip */
124 if (sample >= MAD_F_ONE)
125 sample = MAD_F_ONE - 1;
126 else if (sample < -MAD_F_ONE)
127 sample = -MAD_F_ONE;
129 /* quantize */
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;
135 int len=0;
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
140 this->have_frame=0;
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;
157 while (nsamples--) {
158 /* output sample(s) in 16-bit signed little-endian PCM */
160 *output++ = scale(*left_ch++);
162 if (nchannels == 2)
163 *output++ = scale(*right_ch++);
169 return len?len:-1;
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:
175 switch(cmd){
176 case ADCTRL_RESYNC_STREAM:
177 this->have_frame=0;
178 mad_synth_init (&this->synth);
179 mad_stream_init (&this->stream);
180 mad_frame_init (&this->frame);
181 return CONTROL_TRUE;
182 case ADCTRL_SKIP_FRAME:
183 this->have_frame=read_frame(sh);
184 return CONTROL_TRUE;
186 return CONTROL_UNKNOWN;