30421
|
1 /*
|
|
2 * This file is part of MPlayer.
|
|
3 *
|
|
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.
|
|
8 *
|
|
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.
|
|
13 *
|
|
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.
|
|
17 */
|
|
18
|
5479
|
19 #include <stdio.h>
|
|
20 #include <stdlib.h>
|
|
21 #include <unistd.h>
|
|
22
|
|
23 #include "config.h"
|
|
24
|
|
25 #include "ad_internal.h"
|
|
26
|
31160
|
27 static const ad_info_t info = {
|
5479
|
28 "libmad mpeg audio decoder",
|
|
29 "libmad",
|
|
30 "A'rpi",
|
|
31 "libmad...",
|
|
32 "based on Xine's libmad/xine_decoder.c"
|
|
33 };
|
|
34
|
|
35 LIBAD_EXTERN(libmad)
|
|
36
|
|
37 #include <mad.h>
|
|
38
|
|
39 typedef struct mad_decoder_s {
|
|
40
|
29263
|
41 struct mad_synth synth;
|
5479
|
42 struct mad_stream stream;
|
|
43 struct mad_frame frame;
|
29263
|
44
|
5479
|
45 int have_frame;
|
|
46
|
|
47 int output_sampling_rate;
|
|
48 int output_open;
|
|
49 int output_mode;
|
|
50
|
|
51 } mad_decoder_t;
|
|
52
|
|
53 static int preinit(sh_audio_t *sh){
|
|
54
|
30702
|
55 mad_decoder_t *this = malloc(sizeof(mad_decoder_t));
|
5479
|
56 memset(this,0,sizeof(mad_decoder_t));
|
|
57 sh->context = this;
|
29263
|
58
|
5479
|
59 mad_synth_init (&this->synth);
|
|
60 mad_stream_init (&this->stream);
|
|
61 mad_frame_init (&this->frame);
|
29263
|
62
|
5479
|
63 sh->audio_out_minsize=2*4608;
|
|
64 sh->audio_in_minsize=4096;
|
29263
|
65
|
5479
|
66 return 1;
|
|
67 }
|
|
68
|
|
69 static int read_frame(sh_audio_t *sh){
|
|
70 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
71 int len;
|
|
72
|
|
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;
|
|
86 }
|
|
87 if (ret == 0) return 1; // OK!!!
|
|
88 // error! try to resync!
|
|
89 if(this->stream.error==MAD_ERROR_BUFLEN) break;
|
|
90 }
|
|
91 }
|
|
92 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"Cannot sync MAD frame\n");
|
|
93 return 0;
|
|
94 }
|
|
95
|
|
96 static int init(sh_audio_t *sh){
|
|
97 mad_decoder_t *this = (mad_decoder_t *) sh->context;
|
|
98
|
|
99 this->have_frame=read_frame(sh);
|
|
100 if(!this->have_frame) return 0; // failed to sync...
|
29263
|
101
|
5479
|
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;
|
13430
|
105 sh->samplesize=2;
|
29263
|
106
|
5479
|
107 return 1;
|
|
108 }
|
|
109
|
|
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);
|
|
116 }
|
|
117
|
|
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));
|
|
122
|
|
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;
|
|
128
|
|
129 /* quantize */
|
|
130 return sample >> (MAD_F_FRACBITS + 1 - 16);
|
|
131 }
|
|
132
|
|
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;
|
|
136
|
|
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;
|
|
141
|
|
142 mad_synth_frame (&this->synth, &this->frame);
|
|
143
|
|
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;
|
|
148
|
|
149 nchannels = pcm->channels;
|
|
150 nsamples = pcm->length;
|
|
151 left_ch = pcm->samples[0];
|
|
152 right_ch = pcm->samples[1];
|
|
153
|
|
154 len+=2*nchannels*nsamples;
|
|
155 buf+=2*nchannels*nsamples;
|
29263
|
156
|
5479
|
157 while (nsamples--) {
|
|
158 /* output sample(s) in 16-bit signed little-endian PCM */
|
29263
|
159
|
5479
|
160 *output++ = scale(*left_ch++);
|
29263
|
161
|
|
162 if (nchannels == 2)
|
5479
|
163 *output++ = scale(*right_ch++);
|
|
164
|
|
165 }
|
|
166 }
|
|
167 }
|
29263
|
168
|
5479
|
169 return len?len:-1;
|
|
170 }
|
|
171
|
|
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;
|
|
185 }
|
|
186 return CONTROL_UNKNOWN;
|
|
187 }
|