2002-03-31 20:08:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2002-11-17 12:39:00 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <math.h>
|
2002-03-31 20:08:19 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "ad_internal.h"
|
|
|
|
|
2002-11-17 12:39:00 +00:00
|
|
|
#ifdef USE_SETLOCALE
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-31 20:08:19 +00:00
|
|
|
#ifdef HAVE_OGGVORBIS
|
|
|
|
|
|
|
|
static ad_info_t info =
|
|
|
|
{
|
|
|
|
"Ogg/Vorbis audio decoder",
|
|
|
|
"libvorbis",
|
|
|
|
"Felix Buenemann, A'rpi",
|
|
|
|
"libvorbis",
|
2002-08-31 13:09:23 +00:00
|
|
|
""
|
2002-03-31 20:08:19 +00:00
|
|
|
};
|
|
|
|
|
2002-08-30 20:06:22 +00:00
|
|
|
LIBAD_EXTERN(libvorbis)
|
2002-03-31 20:08:19 +00:00
|
|
|
|
2002-12-04 20:27:36 +00:00
|
|
|
#ifdef TREMOR
|
|
|
|
#include <tremor/ivorbiscodec.h>
|
|
|
|
#else
|
2002-03-31 20:08:19 +00:00
|
|
|
#include <vorbis/codec.h>
|
2002-12-04 20:27:36 +00:00
|
|
|
#endif
|
2002-03-31 20:08:19 +00:00
|
|
|
|
|
|
|
// This struct is also defined in demux_ogg.c => common header ?
|
|
|
|
typedef struct ov_struct_st {
|
|
|
|
vorbis_info vi; /* struct that stores all the static vorbis bitstream
|
|
|
|
settings */
|
|
|
|
vorbis_comment vc; /* struct that stores all the bitstream user comments */
|
|
|
|
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
|
|
|
|
vorbis_block vb; /* local working space for packet->PCM decode */
|
2002-11-17 12:39:00 +00:00
|
|
|
float rg_scale; /* replaygain scale */
|
2002-12-04 20:27:36 +00:00
|
|
|
#ifdef TREMOR
|
|
|
|
int rg_scale_int;
|
|
|
|
#endif
|
2002-03-31 20:08:19 +00:00
|
|
|
} ov_struct_t;
|
|
|
|
|
2002-11-17 12:39:00 +00:00
|
|
|
static int read_vorbis_comment( char* ptr, char* comment, char* format, ... ) {
|
|
|
|
va_list va;
|
|
|
|
int clen, ret;
|
|
|
|
|
|
|
|
va_start( va, format );
|
|
|
|
clen = strlen( comment );
|
|
|
|
#ifdef USE_SETLOCALE
|
|
|
|
setlocale( LC_NUMERIC, "C" );
|
|
|
|
#endif
|
|
|
|
ret = strncasecmp( ptr, comment, clen) == 0 ? vsscanf( ptr+clen, format, va ) : 0;
|
|
|
|
#ifdef USE_SETLOCALE
|
|
|
|
setlocale( LC_NUMERIC, "" );
|
|
|
|
#endif
|
|
|
|
va_end( va );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-03-31 20:08:19 +00:00
|
|
|
static int preinit(sh_audio_t *sh)
|
|
|
|
{
|
|
|
|
sh->audio_out_minsize=1024*4; // 1024 samples/frame
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init(sh_audio_t *sh)
|
|
|
|
{
|
|
|
|
ogg_packet op;
|
|
|
|
vorbis_comment vc;
|
|
|
|
struct ov_struct_st *ov;
|
2002-08-22 23:28:08 +00:00
|
|
|
#define ERROR() { \
|
|
|
|
vorbis_comment_clear(&vc); \
|
|
|
|
vorbis_info_clear(&ov->vi); \
|
|
|
|
free(ov); \
|
|
|
|
return 0; \
|
2002-04-21 18:06:00 +00:00
|
|
|
}
|
2002-03-31 20:08:19 +00:00
|
|
|
/// Init the decoder with the 3 header packets
|
|
|
|
ov = (struct ov_struct_st*)malloc(sizeof(struct ov_struct_st));
|
|
|
|
vorbis_info_init(&ov->vi);
|
|
|
|
vorbis_comment_init(&vc);
|
|
|
|
op.bytes = ds_get_packet(sh->ds,&op.packet);
|
|
|
|
op.b_o_s = 1;
|
|
|
|
/// Header
|
|
|
|
if(vorbis_synthesis_headerin(&ov->vi,&vc,&op) <0) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"OggVorbis: initial (identification) header broken!\n");
|
2002-08-22 23:28:08 +00:00
|
|
|
ERROR();
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
|
|
|
op.bytes = ds_get_packet(sh->ds,&op.packet);
|
|
|
|
op.b_o_s = 0;
|
|
|
|
/// Comments
|
|
|
|
if(vorbis_synthesis_headerin(&ov->vi,&vc,&op) <0) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"OggVorbis: comment header broken!\n");
|
2002-08-22 23:28:08 +00:00
|
|
|
ERROR();
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
|
|
|
op.bytes = ds_get_packet(sh->ds,&op.packet);
|
|
|
|
//// Codebook
|
|
|
|
if(vorbis_synthesis_headerin(&ov->vi,&vc,&op)<0) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"OggVorbis: codebook header broken!\n");
|
2002-08-22 23:28:08 +00:00
|
|
|
ERROR();
|
2002-03-31 20:08:19 +00:00
|
|
|
} else { /// Print the infos
|
2002-11-17 12:39:00 +00:00
|
|
|
float rg_gain=0.f, rg_peak=0.f;
|
2002-03-31 20:08:19 +00:00
|
|
|
char **ptr=vc.user_comments;
|
|
|
|
while(*ptr){
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbisComment: %s\n",*ptr);
|
2002-11-17 12:39:00 +00:00
|
|
|
/* replaygain */
|
|
|
|
read_vorbis_comment( *ptr, "replaygain_album_gain=", "%f", &rg_gain );
|
|
|
|
read_vorbis_comment( *ptr, "rg_audiophile=", "%f", &rg_gain );
|
|
|
|
if( !rg_gain ) {
|
|
|
|
read_vorbis_comment( *ptr, "replaygain_track_gain=", "%f", &rg_gain );
|
|
|
|
read_vorbis_comment( *ptr, "rg_radio=", "%f", &rg_gain );
|
|
|
|
}
|
|
|
|
read_vorbis_comment( *ptr, "replaygain_album_peak=", "%f", &rg_peak );
|
|
|
|
if( !rg_peak ) {
|
|
|
|
read_vorbis_comment( *ptr, "replaygain_track_peak=", "%f", &rg_peak );
|
|
|
|
read_vorbis_comment( *ptr, "rg_peak=", "%f", &rg_peak );
|
|
|
|
}
|
2002-03-31 20:08:19 +00:00
|
|
|
++ptr;
|
|
|
|
}
|
2002-11-17 12:39:00 +00:00
|
|
|
/* replaygain: scale */
|
|
|
|
if(!rg_gain)
|
|
|
|
ov->rg_scale = 1.f; /* just in case pow() isn't standard-conformant */
|
|
|
|
else
|
|
|
|
ov->rg_scale = pow(10.f, rg_gain/20);
|
|
|
|
/* replaygain: anticlip */
|
|
|
|
if(ov->rg_scale * rg_peak > 1.f)
|
|
|
|
ov->rg_scale = 1.f / rg_peak;
|
|
|
|
/* replaygain: security */
|
|
|
|
if(ov->rg_scale > 15.)
|
|
|
|
ov->rg_scale = 15.;
|
2002-12-04 20:27:36 +00:00
|
|
|
#ifdef TREMOR
|
2002-12-04 20:50:53 +00:00
|
|
|
ov->rg_scale_int = (int)(ov->rg_scale*128.f);
|
2002-12-04 20:27:36 +00:00
|
|
|
#endif
|
2002-11-17 12:39:00 +00:00
|
|
|
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,
|
2002-03-31 20:08:19 +00:00
|
|
|
(ov->vi.bitrate_lower!=ov->vi.bitrate_nominal)||(ov->vi.bitrate_upper!=ov->vi.bitrate_nominal)?'V':'C');
|
2002-11-17 12:39:00 +00:00
|
|
|
if(rg_gain || rg_peak)
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Gain = %+.2f dB, Peak = %.4f, Scale = %.2f\n", rg_gain, rg_peak, ov->rg_scale);
|
2002-03-31 20:08:19 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Encoded by: %s\n",vc.vendor);
|
|
|
|
}
|
2002-04-21 18:06:00 +00:00
|
|
|
vorbis_comment_clear(&vc);
|
2002-03-31 20:08:19 +00:00
|
|
|
|
|
|
|
// printf("lower=%d upper=%d \n",(int)ov->vi.bitrate_lower,(int)ov->vi.bitrate_upper);
|
|
|
|
|
|
|
|
// Setup the decoder
|
|
|
|
sh->channels=ov->vi.channels;
|
|
|
|
sh->samplerate=ov->vi.rate;
|
|
|
|
// assume 128kbit if bitrate not specified in the header
|
|
|
|
sh->i_bps=((ov->vi.bitrate_nominal>0) ? ov->vi.bitrate_nominal : 128000)/8;
|
|
|
|
sh->context = ov;
|
|
|
|
|
|
|
|
/// Finish the decoder init
|
|
|
|
vorbis_synthesis_init(&ov->vd,&ov->vi);
|
|
|
|
vorbis_block_init(&ov->vd,&ov->vb);
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"OggVorbis: Init OK!\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(sh_audio_t *sh)
|
|
|
|
{
|
2002-04-21 18:06:00 +00:00
|
|
|
struct ov_struct_st *ov = sh->context;
|
|
|
|
vorbis_block_clear(&ov->vb);
|
|
|
|
vorbis_info_clear(&ov->vi);
|
|
|
|
free(ov);
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int control(sh_audio_t *sh,int cmd,void* arg, ...)
|
|
|
|
{
|
|
|
|
switch(cmd)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
case ADCTRL_RESYNC_STREAM:
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
case ADCTRL_SKIP_FRAME:
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
int samples;
|
|
|
|
float **pcm;
|
|
|
|
ogg_packet op;
|
|
|
|
struct ov_struct_st *ov = sh->context;
|
|
|
|
op.b_o_s = op.e_o_s = 0;
|
|
|
|
while(len < minlen) {
|
|
|
|
op.bytes = ds_get_packet(sh->ds,&op.packet);
|
|
|
|
if(!op.packet)
|
|
|
|
break;
|
|
|
|
if(vorbis_synthesis(&ov->vb,&op)==0) /* test for success! */
|
|
|
|
vorbis_synthesis_blockin(&ov->vd,&ov->vb);
|
|
|
|
while((samples=vorbis_synthesis_pcmout(&ov->vd,&pcm))>0){
|
|
|
|
int i,j;
|
|
|
|
int clipflag=0;
|
|
|
|
int convsize=(maxlen-len)/(2*ov->vi.channels); // max size!
|
|
|
|
int bout=(samples<convsize?samples:convsize);
|
|
|
|
|
|
|
|
if(bout<=0) break;
|
|
|
|
|
|
|
|
/* convert floats to 16 bit signed ints (host order) and
|
|
|
|
interleave */
|
|
|
|
for(i=0;i<ov->vi.channels;i++){
|
|
|
|
ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]);
|
|
|
|
ogg_int16_t *ptr=convbuffer+i;
|
2002-12-04 20:27:36 +00:00
|
|
|
#ifdef TREMOR
|
|
|
|
ogg_int32_t *mono=pcm[i];
|
|
|
|
for(j=0;j<bout;j++){
|
2002-12-04 20:50:53 +00:00
|
|
|
int val=(mono[j]*ov->rg_scale_int)>>(9+7);
|
2002-12-04 20:27:36 +00:00
|
|
|
#else
|
2002-03-31 20:08:19 +00:00
|
|
|
float *mono=pcm[i];
|
|
|
|
for(j=0;j<bout;j++){
|
2002-11-17 12:39:00 +00:00
|
|
|
int val=mono[j]*32767.f*ov->rg_scale;
|
2002-12-04 20:27:36 +00:00
|
|
|
#endif /* TREMOR */
|
2002-03-31 20:08:19 +00:00
|
|
|
/* might as well guard against clipping */
|
|
|
|
if(val>32767){
|
|
|
|
val=32767;
|
|
|
|
clipflag=1;
|
|
|
|
}
|
|
|
|
if(val<-32768){
|
|
|
|
val=-32768;
|
|
|
|
clipflag=1;
|
|
|
|
}
|
|
|
|
*ptr=val;
|
|
|
|
ptr+=ov->vi.channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(clipflag)
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"Clipping in frame %ld\n",(long)(ov->vd.sequence));
|
|
|
|
len+=2*ov->vi.channels*bout;
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"\n[decoded: %d / %d ]\n",bout,samples);
|
|
|
|
vorbis_synthesis_read(&ov->vd,bout); /* tell libvorbis how
|
|
|
|
many samples we
|
|
|
|
actually consumed */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_OGGVORBIS */
|
|
|
|
|