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"
|
2008-06-07 13:04:53 +00:00
|
|
|
#include "libaf/reorder_ch.h"
|
2002-03-31 20:08:19 +00:00
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
static ad_info_t info =
|
2002-03-31 20:08:19 +00:00
|
|
|
{
|
|
|
|
"Ogg/Vorbis audio decoder",
|
2008-11-24 08:31:44 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
|
|
|
"tremor",
|
|
|
|
#else
|
2002-03-31 20:08:19 +00:00
|
|
|
"libvorbis",
|
2008-11-24 08:31:44 +00:00
|
|
|
#endif
|
2002-03-31 20:08:19 +00:00
|
|
|
"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
|
|
|
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
2002-12-04 20:27:36 +00:00
|
|
|
#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 */
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
2002-12-04 20:27:36 +00:00
|
|
|
int rg_scale_int;
|
|
|
|
#endif
|
2002-03-31 20:08:19 +00:00
|
|
|
} ov_struct_t;
|
|
|
|
|
2006-07-15 16:03:12 +00:00
|
|
|
static int read_vorbis_comment( char* ptr, const char* comment, const char* format, ... ) {
|
2002-11-17 12:39:00 +00:00
|
|
|
va_list va;
|
|
|
|
int clen, ret;
|
|
|
|
|
|
|
|
va_start( va, format );
|
|
|
|
clen = strlen( comment );
|
|
|
|
ret = strncasecmp( ptr, comment, clen) == 0 ? vsscanf( ptr+clen, format, va ) : 0;
|
|
|
|
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)
|
|
|
|
{
|
2005-12-05 01:28:17 +00:00
|
|
|
unsigned int offset, i, length, hsizes[3];
|
|
|
|
void *headers[3];
|
2005-05-12 17:35:58 +00:00
|
|
|
unsigned char* extradata;
|
2002-03-31 20:08:19 +00:00
|
|
|
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
|
|
|
}
|
2005-05-12 17:35:58 +00:00
|
|
|
|
2005-05-12 23:32:26 +00:00
|
|
|
/// Init the decoder with the 3 header packets
|
2006-07-02 08:17:07 +00:00
|
|
|
ov = malloc(sizeof(struct ov_struct_st));
|
2005-05-12 23:32:26 +00:00
|
|
|
vorbis_info_init(&ov->vi);
|
|
|
|
vorbis_comment_init(&vc);
|
|
|
|
|
2005-05-12 17:35:58 +00:00
|
|
|
if(! sh->wf) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ad_vorbis, extradata seems to be absent! exit\n");
|
|
|
|
ERROR();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(! sh->wf->cbSize) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ad_vorbis, extradata seems to be absent!, exit\n");
|
2002-08-22 23:28:08 +00:00
|
|
|
ERROR();
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
2005-05-12 17:35:58 +00:00
|
|
|
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"ad_vorbis, extradata seems is %d bytes long\n", sh->wf->cbSize);
|
|
|
|
extradata = (char*) (sh->wf+1);
|
|
|
|
if(!extradata) {
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ad_vorbis, extradata seems to be NULL!, exit\n");
|
2002-08-22 23:28:08 +00:00
|
|
|
ERROR();
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
2005-05-12 17:35:58 +00:00
|
|
|
|
|
|
|
if(*extradata != 2) {
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN, "ad_vorbis: Vorbis track does not contain valid headers.\n");
|
2002-08-22 23:28:08 +00:00
|
|
|
ERROR();
|
2005-05-12 17:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset = 1;
|
|
|
|
for (i=0; i < 2; i++) {
|
|
|
|
length = 0;
|
|
|
|
while ((extradata[offset] == (unsigned char) 0xFF) && length < sh->wf->cbSize) {
|
|
|
|
length += 255;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
if(offset >= (sh->wf->cbSize - 1)) {
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN, "ad_vorbis: Vorbis track does not contain valid headers.\n");
|
|
|
|
ERROR();
|
|
|
|
}
|
|
|
|
length += extradata[offset];
|
|
|
|
offset++;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "ad_vorbis, offset: %u, length: %u\n", offset, length);
|
|
|
|
hsizes[i] = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
headers[0] = &extradata[offset];
|
|
|
|
headers[1] = &extradata[offset + hsizes[0]];
|
|
|
|
headers[2] = &extradata[offset + hsizes[0] + hsizes[1]];
|
|
|
|
hsizes[2] = sh->wf->cbSize - offset - hsizes[0] - hsizes[1];
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "ad_vorbis, header sizes: %d %d %d\n", hsizes[0], hsizes[1], hsizes[2]);
|
|
|
|
|
|
|
|
for(i=0; i<3; i++) {
|
|
|
|
op.bytes = hsizes[i];
|
|
|
|
op.packet = headers[i];
|
|
|
|
op.b_o_s = (i == 0);
|
|
|
|
if(vorbis_synthesis_headerin(&ov->vi,&vc,&op) <0) {
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"OggVorbis: header n. %d broken! len=%ld\n", i, op.bytes);
|
2005-05-12 17:35:58 +00:00
|
|
|
ERROR();
|
|
|
|
}
|
|
|
|
if(i == 2) {
|
|
|
|
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 */
|
2009-05-13 02:58:57 +00:00
|
|
|
if(ov->rg_scale > 15.)
|
2002-11-17 12:39:00 +00:00
|
|
|
ov->rg_scale = 15.;
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
2003-01-13 19:18:26 +00:00
|
|
|
ov->rg_scale_int = (int)(ov->rg_scale*64.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);
|
2005-05-12 17:35:58 +00:00
|
|
|
}
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
2005-05-12 17:35:58 +00:00
|
|
|
|
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
|
2009-05-13 02:58:57 +00:00
|
|
|
sh->channels=ov->vi.channels;
|
2002-03-31 20:08:19 +00:00
|
|
|
sh->samplerate=ov->vi.rate;
|
2004-09-18 04:20:13 +00:00
|
|
|
sh->samplesize=2;
|
2002-03-31 20:08:19 +00:00
|
|
|
// 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;
|
2004-10-14 15:09:35 +00:00
|
|
|
vorbis_dsp_clear(&ov->vd);
|
2002-04-21 18:06:00 +00:00
|
|
|
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)
|
|
|
|
{
|
2009-05-13 02:58:57 +00:00
|
|
|
#if 0
|
2002-03-31 20:08:19 +00:00
|
|
|
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;
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
2005-08-06 15:43:54 +00:00
|
|
|
ogg_int32_t **pcm;
|
|
|
|
#else
|
2007-05-28 08:18:23 +00:00
|
|
|
float scale;
|
2002-03-31 20:08:19 +00:00
|
|
|
float **pcm;
|
2005-08-06 15:43:54 +00:00
|
|
|
#endif
|
2002-03-31 20:08:19 +00:00
|
|
|
struct ov_struct_st *ov = sh->context;
|
|
|
|
while(len < minlen) {
|
2003-01-29 22:45:07 +00:00
|
|
|
while((samples=vorbis_synthesis_pcmout(&ov->vd,&pcm))<=0){
|
|
|
|
ogg_packet op;
|
2006-04-24 10:58:40 +00:00
|
|
|
double pts;
|
2003-01-29 22:45:07 +00:00
|
|
|
memset(&op,0,sizeof(op)); //op.b_o_s = op.e_o_s = 0;
|
2006-04-24 10:58:40 +00:00
|
|
|
op.bytes = ds_get_packet_pts(sh->ds,&op.packet, &pts);
|
2003-01-29 22:45:07 +00:00
|
|
|
if(op.bytes<=0) break;
|
2006-04-24 10:58:40 +00:00
|
|
|
if (pts != MP_NOPTS_VALUE) {
|
|
|
|
sh->pts = pts;
|
|
|
|
sh->pts_bytes = 0;
|
|
|
|
}
|
2003-01-29 22:45:07 +00:00
|
|
|
if(vorbis_synthesis(&ov->vb,&op)==0) /* test for success! */
|
|
|
|
vorbis_synthesis_blockin(&ov->vd,&ov->vb);
|
|
|
|
}
|
|
|
|
if(samples<=0) break; // error/EOF
|
|
|
|
while(samples>0){
|
2002-03-31 20:08:19 +00:00
|
|
|
int i,j;
|
|
|
|
int clipflag=0;
|
|
|
|
int convsize=(maxlen-len)/(2*ov->vi.channels); // max size!
|
2003-01-29 22:45:07 +00:00
|
|
|
int bout=((samples<convsize)?samples:convsize);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2003-01-29 22:45:07 +00:00
|
|
|
if(bout<=0) break; // no buffer space
|
2002-03-31 20:08:19 +00:00
|
|
|
|
|
|
|
/* convert floats to 16 bit signed ints (host order) and
|
|
|
|
interleave */
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
2003-01-13 19:38:52 +00:00
|
|
|
if (ov->rg_scale_int == 64) {
|
|
|
|
for(i=0;i<ov->vi.channels;i++){
|
|
|
|
ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]);
|
|
|
|
ogg_int16_t *ptr=convbuffer+i;
|
|
|
|
ogg_int32_t *mono=pcm[i];
|
|
|
|
for(j=0;j<bout;j++){
|
|
|
|
int val=mono[j]>>9;
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2008-08-07 12:20:50 +00:00
|
|
|
#endif /* CONFIG_TREMOR */
|
2003-01-13 19:38:52 +00:00
|
|
|
{
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifndef CONFIG_TREMOR
|
2003-01-13 19:38:52 +00:00
|
|
|
scale = 32767.f * ov->rg_scale;
|
|
|
|
#endif
|
2002-03-31 20:08:19 +00:00
|
|
|
for(i=0;i<ov->vi.channels;i++){
|
|
|
|
ogg_int16_t *convbuffer=(ogg_int16_t *)(&buf[len]);
|
|
|
|
ogg_int16_t *ptr=convbuffer+i;
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_TREMOR
|
2002-12-04 20:27:36 +00:00
|
|
|
ogg_int32_t *mono=pcm[i];
|
|
|
|
for(j=0;j<bout;j++){
|
2003-01-13 19:18:26 +00:00
|
|
|
int val=(mono[j]*ov->rg_scale_int)>>(9+6);
|
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++){
|
2003-01-13 19:38:52 +00:00
|
|
|
int val=mono[j]*scale;
|
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;
|
|
|
|
}
|
2008-08-07 12:20:50 +00:00
|
|
|
#endif /* CONFIG_TREMOR */
|
2002-03-31 20:08:19 +00:00
|
|
|
*ptr=val;
|
|
|
|
ptr+=ov->vi.channels;
|
|
|
|
}
|
|
|
|
}
|
2003-01-13 19:38:52 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-03-31 20:08:19 +00:00
|
|
|
if(clipflag)
|
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"Clipping in frame %ld\n",(long)(ov->vd.sequence));
|
|
|
|
len+=2*ov->vi.channels*bout;
|
2006-04-24 10:58:40 +00:00
|
|
|
sh->pts_bytes += 2*ov->vi.channels*bout;
|
2002-03-31 20:08:19 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"\n[decoded: %d / %d ]\n",bout,samples);
|
2003-01-29 22:45:07 +00:00
|
|
|
samples-=bout;
|
2002-03-31 20:08:19 +00:00
|
|
|
vorbis_synthesis_read(&ov->vd,bout); /* tell libvorbis how
|
|
|
|
many samples we
|
|
|
|
actually consumed */
|
2003-01-29 22:45:07 +00:00
|
|
|
} //while(samples>0)
|
|
|
|
// if (!samples) break; // why? how?
|
2002-03-31 20:08:19 +00:00
|
|
|
}
|
|
|
|
|
2008-06-07 13:04:53 +00:00
|
|
|
if (len > 0 && ov->vi.channels >= 5) {
|
|
|
|
reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_VORBIS_DEFAULT,
|
|
|
|
AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
|
|
|
|
ov->vi.channels, len / sh->samplesize,
|
|
|
|
sh->samplesize);
|
|
|
|
}
|
2002-03-31 20:08:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|