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