stream_ffmpeg.c: change reads back to url_read_complete()
[mplayer/glamo.git] / libmpcodecs / ad_libvorbis.c
bloba768095187e4a14e26880c42fe0b71d04a59f960
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>
22 #include <stdarg.h>
23 #include <math.h>
25 #include "config.h"
26 #include "ad_internal.h"
27 #include "libaf/reorder_ch.h"
29 static const ad_info_t info =
31 "Ogg/Vorbis audio decoder",
32 #ifdef CONFIG_TREMOR
33 "tremor",
34 #else
35 "libvorbis",
36 #endif
37 "Felix Buenemann, A'rpi",
38 "libvorbis",
42 LIBAD_EXTERN(libvorbis)
44 #ifdef CONFIG_TREMOR
45 #include <tremor/ivorbiscodec.h>
46 #else
47 #include <vorbis/codec.h>
48 #endif
50 // This struct is also defined in demux_ogg.c => common header ?
51 typedef struct ov_struct_st {
52 vorbis_info vi; /* struct that stores all the static vorbis bitstream
53 settings */
54 vorbis_comment vc; /* struct that stores all the bitstream user comments */
55 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
56 vorbis_block vb; /* local working space for packet->PCM decode */
57 float rg_scale; /* replaygain scale */
58 #ifdef CONFIG_TREMOR
59 int rg_scale_int;
60 #endif
61 } ov_struct_t;
63 static int read_vorbis_comment( char* ptr, const char* comment, const char* format, ... ) {
64 va_list va;
65 int clen, ret;
67 va_start( va, format );
68 clen = strlen( comment );
69 ret = strncasecmp( ptr, comment, clen) == 0 ? vsscanf( ptr+clen, format, va ) : 0;
70 va_end( va );
72 return ret;
75 static int preinit(sh_audio_t *sh)
77 sh->audio_out_minsize=1024*4; // 1024 samples/frame
78 return 1;
81 static int init(sh_audio_t *sh)
83 unsigned int offset, i, length, hsizes[3];
84 void *headers[3];
85 unsigned char* extradata;
86 ogg_packet op;
87 vorbis_comment vc;
88 struct ov_struct_st *ov;
89 #define ERROR() { \
90 vorbis_comment_clear(&vc); \
91 vorbis_info_clear(&ov->vi); \
92 free(ov); \
93 return 0; \
96 /// Init the decoder with the 3 header packets
97 ov = malloc(sizeof(struct ov_struct_st));
98 vorbis_info_init(&ov->vi);
99 vorbis_comment_init(&vc);
101 if(! sh->wf) {
102 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ad_vorbis, extradata seems to be absent! exit\n");
103 ERROR();
106 if(! sh->wf->cbSize) {
107 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ad_vorbis, extradata seems to be absent!, exit\n");
108 ERROR();
111 mp_msg(MSGT_DECAUDIO,MSGL_V,"ad_vorbis, extradata seems is %d bytes long\n", sh->wf->cbSize);
112 extradata = (char*) (sh->wf+1);
113 if(!extradata) {
114 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ad_vorbis, extradata seems to be NULL!, exit\n");
115 ERROR();
118 if(*extradata != 2) {
119 mp_msg (MSGT_DEMUX, MSGL_WARN, "ad_vorbis: Vorbis track does not contain valid headers.\n");
120 ERROR();
123 offset = 1;
124 for (i=0; i < 2; i++) {
125 length = 0;
126 while ((extradata[offset] == (unsigned char) 0xFF) && length < sh->wf->cbSize) {
127 length += 255;
128 offset++;
130 if(offset >= (sh->wf->cbSize - 1)) {
131 mp_msg (MSGT_DEMUX, MSGL_WARN, "ad_vorbis: Vorbis track does not contain valid headers.\n");
132 ERROR();
134 length += extradata[offset];
135 offset++;
136 mp_msg (MSGT_DEMUX, MSGL_V, "ad_vorbis, offset: %u, length: %u\n", offset, length);
137 hsizes[i] = length;
140 headers[0] = &extradata[offset];
141 headers[1] = &extradata[offset + hsizes[0]];
142 headers[2] = &extradata[offset + hsizes[0] + hsizes[1]];
143 hsizes[2] = sh->wf->cbSize - offset - hsizes[0] - hsizes[1];
144 mp_msg (MSGT_DEMUX, MSGL_V, "ad_vorbis, header sizes: %d %d %d\n", hsizes[0], hsizes[1], hsizes[2]);
146 for(i=0; i<3; i++) {
147 op.bytes = hsizes[i];
148 op.packet = headers[i];
149 op.b_o_s = (i == 0);
150 if(vorbis_synthesis_headerin(&ov->vi,&vc,&op) <0) {
151 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"OggVorbis: header n. %d broken! len=%ld\n", i, op.bytes);
152 ERROR();
154 if(i == 2) {
155 float rg_gain=0.f, rg_peak=0.f;
156 char **ptr=vc.user_comments;
157 while(*ptr){
158 mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbisComment: %s\n",*ptr);
159 /* replaygain */
160 read_vorbis_comment( *ptr, "replaygain_album_gain=", "%f", &rg_gain );
161 read_vorbis_comment( *ptr, "rg_audiophile=", "%f", &rg_gain );
162 if( !rg_gain ) {
163 read_vorbis_comment( *ptr, "replaygain_track_gain=", "%f", &rg_gain );
164 read_vorbis_comment( *ptr, "rg_radio=", "%f", &rg_gain );
166 read_vorbis_comment( *ptr, "replaygain_album_peak=", "%f", &rg_peak );
167 if( !rg_peak ) {
168 read_vorbis_comment( *ptr, "replaygain_track_peak=", "%f", &rg_peak );
169 read_vorbis_comment( *ptr, "rg_peak=", "%f", &rg_peak );
171 ++ptr;
173 /* replaygain: scale */
174 if(!rg_gain)
175 ov->rg_scale = 1.f; /* just in case pow() isn't standard-conformant */
176 else
177 ov->rg_scale = pow(10.f, rg_gain/20);
178 /* replaygain: anticlip */
179 if(ov->rg_scale * rg_peak > 1.f)
180 ov->rg_scale = 1.f / rg_peak;
181 /* replaygain: security */
182 if(ov->rg_scale > 15.)
183 ov->rg_scale = 15.;
184 #ifdef CONFIG_TREMOR
185 ov->rg_scale_int = (int)(ov->rg_scale*64.f);
186 #endif
187 mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Bitstream is %d channel%s, %dHz, %dbit/s %cBR\n",(int)ov->vi.channels,ov->vi.channels>1?"s":"",(int)ov->vi.rate,(int)ov->vi.bitrate_nominal,
188 (ov->vi.bitrate_lower!=ov->vi.bitrate_nominal)||(ov->vi.bitrate_upper!=ov->vi.bitrate_nominal)?'V':'C');
189 if(rg_gain || rg_peak)
190 mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Gain = %+.2f dB, Peak = %.4f, Scale = %.2f\n", rg_gain, rg_peak, ov->rg_scale);
191 mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Encoded by: %s\n",vc.vendor);
195 vorbis_comment_clear(&vc);
197 // printf("lower=%d upper=%d \n",(int)ov->vi.bitrate_lower,(int)ov->vi.bitrate_upper);
199 // Setup the decoder
200 sh->channels=ov->vi.channels;
201 sh->samplerate=ov->vi.rate;
202 sh->samplesize=2;
203 // assume 128kbit if bitrate not specified in the header
204 sh->i_bps=((ov->vi.bitrate_nominal>0) ? ov->vi.bitrate_nominal : 128000)/8;
205 sh->context = ov;
207 /// Finish the decoder init
208 vorbis_synthesis_init(&ov->vd,&ov->vi);
209 vorbis_block_init(&ov->vd,&ov->vb);
210 mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Init OK!\n");
212 return 1;
215 static void uninit(sh_audio_t *sh)
217 struct ov_struct_st *ov = sh->context;
218 vorbis_dsp_clear(&ov->vd);
219 vorbis_block_clear(&ov->vb);
220 vorbis_info_clear(&ov->vi);
221 free(ov);
224 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
226 switch(cmd)
228 #if 0
229 case ADCTRL_RESYNC_STREAM:
230 return CONTROL_TRUE;
231 case ADCTRL_SKIP_FRAME:
232 return CONTROL_TRUE;
233 #endif
235 return CONTROL_UNKNOWN;
238 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen)
240 int len = 0;
241 int samples;
242 #ifdef CONFIG_TREMOR
243 ogg_int32_t **pcm;
244 #else
245 float scale;
246 float **pcm;
247 #endif
248 struct ov_struct_st *ov = sh->context;
249 while(len < minlen) {
250 while((samples=vorbis_synthesis_pcmout(&ov->vd,&pcm))<=0){
251 ogg_packet op;
252 double pts;
253 memset(&op,0,sizeof(op)); //op.b_o_s = op.e_o_s = 0;
254 op.bytes = ds_get_packet_pts(sh->ds,&op.packet, &pts);
255 if(op.bytes<=0) break;
256 if (pts != MP_NOPTS_VALUE) {
257 sh->pts = pts;
258 sh->pts_bytes = 0;
260 if(vorbis_synthesis(&ov->vb,&op)==0) /* test for success! */
261 vorbis_synthesis_blockin(&ov->vd,&ov->vb);
263 if(samples<=0) break; // error/EOF
264 while(samples>0){
265 int i,j;
266 int clipflag=0;
267 int convsize=(maxlen-len)/(2*ov->vi.channels); // max size!
268 int bout=((samples<convsize)?samples:convsize);
270 if(bout<=0) break; // no buffer space
272 /* convert floats to 16 bit signed ints (host order) and
273 interleave */
274 #ifdef CONFIG_TREMOR
275 if (ov->rg_scale_int == 64) {
276 for(i=0;i<ov->vi.channels;i++){
277 ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]);
278 ogg_int16_t *ptr=convbuffer+i;
279 ogg_int32_t *mono=pcm[i];
280 for(j=0;j<bout;j++){
281 int val=mono[j]>>9;
282 /* might as well guard against clipping */
283 if(val>32767){
284 val=32767;
285 clipflag=1;
287 if(val<-32768){
288 val=-32768;
289 clipflag=1;
291 *ptr=val;
292 ptr+=ov->vi.channels;
295 } else
296 #endif /* CONFIG_TREMOR */
298 #ifndef CONFIG_TREMOR
299 scale = 32767.f * ov->rg_scale;
300 #endif
301 for(i=0;i<ov->vi.channels;i++){
302 ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]);
303 ogg_int16_t *ptr=convbuffer+i;
304 #ifdef CONFIG_TREMOR
305 ogg_int32_t *mono=pcm[i];
306 for(j=0;j<bout;j++){
307 int val=(mono[j]*ov->rg_scale_int)>>(9+6);
308 #else
309 float *mono=pcm[i];
310 for(j=0;j<bout;j++){
311 int val=mono[j]*scale;
312 /* might as well guard against clipping */
313 if(val>32767){
314 val=32767;
315 clipflag=1;
317 if(val<-32768){
318 val=-32768;
319 clipflag=1;
321 #endif /* CONFIG_TREMOR */
322 *ptr=val;
323 ptr+=ov->vi.channels;
328 if(clipflag)
329 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"Clipping in frame %ld\n",(long)(ov->vd.sequence));
330 len+=2*ov->vi.channels*bout;
331 sh->pts_bytes += 2*ov->vi.channels*bout;
332 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"\n[decoded: %d / %d ]\n",bout,samples);
333 samples-=bout;
334 vorbis_synthesis_read(&ov->vd,bout); /* tell libvorbis how
335 many samples we
336 actually consumed */
337 } //while(samples>0)
338 // if (!samples) break; // why? how?
341 if (len > 0 && ov->vi.channels >= 5) {
342 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_VORBIS_DEFAULT,
343 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
344 ov->vi.channels, len / sh->samplesize,
345 sh->samplesize);
349 return len;