2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-03-02 11:31:15 +01:00
|
|
|
#include "../winlib/winlib.h"
|
2010-11-10 06:51:05 +01:00
|
|
|
#include "sound.h"
|
|
|
|
#include "fplay.h"
|
|
|
|
|
|
|
|
|
|
|
|
astream_t astream;
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
extern uint8_t *decoder_buffer;
|
2012-11-28 13:06:34 +01:00
|
|
|
int resampler_size;
|
|
|
|
volatile int sound_level_0;
|
|
|
|
volatile int sound_level_1;
|
2012-02-13 23:34:36 +01:00
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
volatile enum player_state player_state;
|
|
|
|
volatile enum player_state decoder_state;
|
|
|
|
volatile enum player_state sound_state;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
extern volatile uint32_t driver_lock;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
static SNDBUF hBuff;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
static int snd_format;
|
|
|
|
int sample_rate;
|
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
static uint32_t samples_written = 0;
|
2012-05-27 12:41:27 +02:00
|
|
|
double audio_base = -1.0;
|
|
|
|
|
|
|
|
double get_audio_base();
|
2012-03-02 11:31:15 +01:00
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
int init_audio(int format)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int version =-1;
|
|
|
|
char *errstr;
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
if((err = InitSound(&version)) !=0 )
|
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
mutex_unlock(&driver_lock);
|
2010-11-10 06:51:05 +01:00
|
|
|
errstr = "Sound service not installed\n\r";
|
|
|
|
goto exit_whith_error;
|
2012-02-13 23:34:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
// printf("sound version 0x%x\n", version);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
if( (SOUND_VERSION>(version&0xFFFF)) ||
|
|
|
|
(SOUND_VERSION<(version >> 16)))
|
|
|
|
{
|
|
|
|
errstr = "Sound service version mismatch\n\r";
|
|
|
|
goto exit_whith_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_format = format;
|
|
|
|
|
|
|
|
create_thread(audio_thread, 0, 163840);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
exit_whith_error:
|
|
|
|
|
|
|
|
printf(errstr);
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
void set_audio_volume(int left, int right)
|
|
|
|
{
|
|
|
|
SetVolume(hBuff, left, right);
|
|
|
|
};
|
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
static uint64_t samples_lost;
|
|
|
|
static double audio_delta;
|
2012-03-02 11:31:15 +01:00
|
|
|
static double last_time_stamp;
|
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
double get_master_clock(void)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
|
|
|
double tstamp;
|
|
|
|
|
|
|
|
GetTimeStamp(hBuff, &tstamp);
|
|
|
|
return tstamp - audio_delta;
|
|
|
|
};
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
int decode_audio(AVCodecContext *ctx, queue_t *qa)
|
|
|
|
{
|
|
|
|
AVPacket pkt;
|
|
|
|
AVPacket pkt_tmp;
|
|
|
|
|
|
|
|
int len;
|
|
|
|
int data_size=0;
|
|
|
|
|
|
|
|
if( astream.count > AVCODEC_MAX_AUDIO_FRAME_SIZE*7)
|
2012-11-28 13:06:34 +01:00
|
|
|
return -1;
|
2012-02-13 23:34:36 +01:00
|
|
|
|
|
|
|
if( get_packet(qa, &pkt) == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// __asm__("int3");
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
pkt_tmp = pkt;
|
|
|
|
|
|
|
|
while(pkt_tmp.size > 0)
|
|
|
|
{
|
|
|
|
data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
|
|
|
|
|
|
|
|
len = avcodec_decode_audio3(ctx,(int16_t*)decoder_buffer,
|
|
|
|
&data_size, &pkt_tmp);
|
|
|
|
|
|
|
|
if(len >= 0)
|
|
|
|
{
|
2012-05-27 12:41:27 +02:00
|
|
|
// if(audio_base == -1.0)
|
|
|
|
// {
|
|
|
|
// if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
// audio_base = get_audio_base() * pkt.pts;
|
2012-11-28 13:06:34 +01:00
|
|
|
// printf("audio base %f\n", audio_base);
|
2012-05-27 12:41:27 +02:00
|
|
|
// };
|
2012-11-28 13:06:34 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
pkt_tmp.data += len;
|
|
|
|
pkt_tmp.size -= len;
|
|
|
|
|
|
|
|
mutex_lock(&astream.lock);
|
|
|
|
memcpy(astream.buffer+astream.count, decoder_buffer, data_size);
|
|
|
|
astream.count += data_size;
|
|
|
|
mutex_unlock(&astream.lock);
|
|
|
|
}
|
|
|
|
else pkt_tmp.size = 0;
|
|
|
|
}
|
|
|
|
av_free_packet(&pkt);
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
static void sync_audio(SNDBUF hbuff, int buffsize)
|
|
|
|
{
|
|
|
|
SND_EVENT evnt;
|
|
|
|
uint32_t offset;
|
|
|
|
double time_stamp;
|
|
|
|
|
|
|
|
#ifdef BLACK_MAGIC_SOUND
|
|
|
|
|
|
|
|
while( player_state != CLOSED)
|
|
|
|
{
|
|
|
|
GetNotify(&evnt);
|
|
|
|
|
|
|
|
if(evnt.code != 0xFF000001)
|
|
|
|
{
|
|
|
|
printf("invalid event code %d\n\r", evnt.code);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evnt.stream != hbuff)
|
|
|
|
{
|
|
|
|
printf("invalid stream %x hBuff= %x\n\r",
|
|
|
|
evnt.stream, hbuff);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetTimeStamp(hbuff, &time_stamp);
|
|
|
|
audio_delta = time_stamp - last_time_stamp;
|
|
|
|
|
|
|
|
offset = evnt.offset;
|
|
|
|
|
|
|
|
mutex_lock(&astream.lock);
|
|
|
|
{
|
2012-11-28 13:06:34 +01:00
|
|
|
if(astream.count < buffsize)
|
|
|
|
{
|
|
|
|
memset(astream.buffer+astream.count,
|
|
|
|
0, buffsize-astream.count);
|
|
|
|
astream.count = buffsize;
|
|
|
|
};
|
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
SetBuffer(hbuff, astream.buffer, offset, buffsize);
|
|
|
|
samples_written+= buffsize/4;
|
2012-11-28 13:06:34 +01:00
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
astream.count -= buffsize;
|
|
|
|
if(astream.count)
|
|
|
|
memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
|
|
|
|
mutex_unlock(&astream.lock);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
int audio_thread(void *param)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
|
|
|
SND_EVENT evnt;
|
2012-03-02 11:31:15 +01:00
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
int buffsize;
|
|
|
|
int samples;
|
|
|
|
int err;
|
|
|
|
char *errstr;
|
2012-03-02 11:31:15 +01:00
|
|
|
int active;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
if((err = CreateBuffer(snd_format|PCM_RING,0, &hBuff)) != 0)
|
|
|
|
{
|
|
|
|
errstr = "Cannot create sound buffer\n\r";
|
|
|
|
goto exit_whith_error;
|
|
|
|
};
|
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
SetVolume(hBuff,-1875,-1875);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
if((err = GetBufferSize(hBuff, &buffsize)) != 0)
|
|
|
|
{
|
|
|
|
errstr = "Cannot get buffer size\n\r";
|
|
|
|
goto exit_whith_error;
|
|
|
|
};
|
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
resampler_size = buffsize = buffsize/2;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
samples = buffsize/4;
|
|
|
|
|
2012-03-02 11:31:15 +01:00
|
|
|
while( player_state != CLOSED)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
|
|
|
uint32_t offset;
|
2012-03-02 11:31:15 +01:00
|
|
|
double event_stamp, wait_stamp;
|
|
|
|
int too_late = 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
switch(sound_state)
|
2012-05-27 12:41:27 +02:00
|
|
|
{
|
2012-11-28 13:06:34 +01:00
|
|
|
case PREPARE:
|
|
|
|
|
|
|
|
mutex_lock(&astream.lock);
|
|
|
|
if(astream.count < buffsize*2)
|
|
|
|
{
|
|
|
|
memset(astream.buffer+astream.count,
|
|
|
|
0, buffsize*2-astream.count);
|
|
|
|
astream.count = buffsize*2;
|
|
|
|
};
|
|
|
|
|
|
|
|
SetBuffer(hBuff, astream.buffer, 0, buffsize*2);
|
|
|
|
astream.count -= buffsize*2;
|
|
|
|
if(astream.count)
|
|
|
|
memcpy(astream.buffer, astream.buffer+buffsize*2, astream.count);
|
|
|
|
mutex_unlock(&astream.lock);
|
|
|
|
|
|
|
|
SetTimeBase(hBuff, audio_base);
|
|
|
|
|
|
|
|
case PAUSE_2_PLAY:
|
|
|
|
GetTimeStamp(hBuff, &last_time_stamp);
|
|
|
|
// printf("last audio time stamp %f\n", last_time_stamp);
|
|
|
|
|
|
|
|
if((err = PlayBuffer(hBuff, 0)) !=0 )
|
|
|
|
{
|
|
|
|
errstr = "Cannot play buffer\n\r";
|
|
|
|
goto exit_whith_error;
|
|
|
|
};
|
|
|
|
active = 1;
|
|
|
|
sync_audio(hBuff, buffsize);
|
|
|
|
sound_state = PLAY;
|
|
|
|
// printf("render: set audio latency to %f\n", audio_delta);
|
|
|
|
|
|
|
|
/* breaktrough */
|
|
|
|
|
|
|
|
case PLAY:
|
|
|
|
GetNotify(&evnt);
|
|
|
|
|
|
|
|
if(evnt.code != 0xFF000001)
|
|
|
|
{
|
|
|
|
printf("invalid event code %d\n\r", evnt.code);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(evnt.stream != hBuff)
|
|
|
|
{
|
|
|
|
printf("invalid stream %x hBuff= %x\n\r",
|
|
|
|
evnt.stream, hBuff);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
offset = evnt.offset;
|
|
|
|
|
|
|
|
mutex_lock(&astream.lock);
|
|
|
|
if(astream.count < buffsize)
|
|
|
|
{
|
|
|
|
memset(astream.buffer+astream.count,
|
|
|
|
0, buffsize-astream.count);
|
|
|
|
astream.count = buffsize;
|
|
|
|
};
|
|
|
|
|
|
|
|
SetBuffer(hBuff, astream.buffer, offset, buffsize);
|
|
|
|
|
|
|
|
{
|
|
|
|
double val = 0;
|
|
|
|
int16_t *src = (int16_t*)astream.buffer;
|
|
|
|
int samples = buffsize/2;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0, val = 0; i < samples/2; i++, src++)
|
|
|
|
if(val < abs(*src))
|
|
|
|
val= abs(*src); // * *src;
|
|
|
|
|
|
|
|
sound_level_0 = val; //sqrt(val / (samples/2));
|
|
|
|
|
|
|
|
for(i = 0, val = 0; i < samples/2; i++, src++)
|
|
|
|
if(val < abs(*src))
|
|
|
|
val= abs(*src); // * *src;
|
|
|
|
|
|
|
|
sound_level_1 = val; //sqrt(val / (samples/2));
|
|
|
|
|
|
|
|
// printf("%d\n", sound_level);
|
|
|
|
};
|
|
|
|
|
|
|
|
samples_written+= buffsize/4;
|
|
|
|
|
|
|
|
astream.count -= buffsize;
|
|
|
|
if(astream.count)
|
|
|
|
memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
|
|
|
|
mutex_unlock(&astream.lock);
|
|
|
|
break;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
case PLAY_2_STOP:
|
|
|
|
if( active )
|
|
|
|
{
|
|
|
|
ResetBuffer(hBuff, SND_RESET_ALL);
|
|
|
|
audio_base = -1.0;
|
|
|
|
active = 0;
|
|
|
|
}
|
|
|
|
sound_state = STOP;
|
2010-11-10 06:51:05 +01:00
|
|
|
break;
|
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
case PLAY_2_PAUSE:
|
|
|
|
if( active )
|
|
|
|
{
|
|
|
|
StopBuffer(hBuff);
|
|
|
|
};
|
|
|
|
sound_state = PAUSE;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-11-28 13:06:34 +01:00
|
|
|
case PAUSE:
|
|
|
|
case STOP:
|
|
|
|
delay(1);
|
|
|
|
};
|
2010-11-10 06:51:05 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
StopBuffer(hBuff);
|
|
|
|
DestroyBuffer(hBuff);
|
|
|
|
|
|
|
|
return 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
exit_whith_error:
|
|
|
|
|
|
|
|
printf(errstr);
|
2012-02-13 23:34:36 +01:00
|
|
|
return -1;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|