2001-04-06 01:18:59 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <unistd.h>
|
2001-04-23 03:42:17 +00:00
|
|
|
|
2001-06-05 18:40:44 +00:00
|
|
|
#include "config.h"
|
2001-08-17 00:40:25 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
|
2001-06-08 23:31:06 +00:00
|
|
|
#include "libao2/afmt.h"
|
2001-05-11 01:22:35 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
extern int verbose; // defined in mplayer.c
|
|
|
|
|
2001-05-08 21:49:00 +00:00
|
|
|
#ifdef USE_FAKE_MONO
|
|
|
|
int fakemono=0;
|
|
|
|
#endif
|
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include "stream.h"
|
|
|
|
#include "demuxer.h"
|
|
|
|
|
|
|
|
#include "wine/mmreg.h"
|
|
|
|
#include "wine/avifmt.h"
|
|
|
|
#include "wine/vfw.h"
|
|
|
|
|
|
|
|
#include "codec-cfg.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
|
|
|
|
#include "mp3lib/mp3.h"
|
|
|
|
#include "libac3/ac3.h"
|
|
|
|
|
2001-05-25 18:26:24 +00:00
|
|
|
#include "alaw.h"
|
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include "xa/xa_gsm.h"
|
|
|
|
|
2001-08-15 11:50:55 +00:00
|
|
|
#include "ac3-iec958.h"
|
|
|
|
|
2001-07-08 00:21:20 +00:00
|
|
|
#ifdef USE_DIRECTSHOW
|
2001-04-23 23:24:42 +00:00
|
|
|
#include "loader/DirectShow/DS_AudioDec.h"
|
2001-07-08 00:21:20 +00:00
|
|
|
#endif
|
2001-06-05 18:40:44 +00:00
|
|
|
|
2001-07-16 12:53:48 +00:00
|
|
|
extern int init_acm_audio_codec(sh_audio_t *sh_audio);
|
|
|
|
extern int acm_decode_audio(sh_audio_t *sh_audio, void* a_buffer,int minlen,int maxlen);
|
|
|
|
|
|
|
|
|
2001-07-07 18:46:15 +00:00
|
|
|
static sh_audio_t* dec_audio_sh=NULL;
|
2001-04-23 03:42:17 +00:00
|
|
|
|
|
|
|
// AC3 decoder buffer callback:
|
|
|
|
static void ac3_fill_buffer(uint8_t **start,uint8_t **end){
|
2001-07-07 18:46:15 +00:00
|
|
|
int len=ds_get_packet(dec_audio_sh->ds,start);
|
2001-04-23 03:42:17 +00:00
|
|
|
//printf("<ac3:%d>\n",len);
|
|
|
|
if(len<0)
|
|
|
|
*start = *end = NULL;
|
|
|
|
else
|
|
|
|
*end = *start + len;
|
|
|
|
}
|
|
|
|
|
2001-07-07 18:46:15 +00:00
|
|
|
// MP3 decoder buffer callback:
|
|
|
|
int mplayer_audio_read(char *buf,int size){
|
|
|
|
int len;
|
|
|
|
len=demux_read_data(dec_audio_sh->ds,buf,size);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2001-04-06 16:31:18 +00:00
|
|
|
int init_audio(sh_audio_t *sh_audio){
|
2001-04-07 21:27:57 +00:00
|
|
|
int driver=sh_audio->codec->driver;
|
2001-04-06 16:31:18 +00:00
|
|
|
|
|
|
|
sh_audio->samplesize=2;
|
2001-07-14 16:58:55 +00:00
|
|
|
#if WORDS_BIGENDIAN
|
|
|
|
sh_audio->sample_format=AFMT_S16_BE;
|
|
|
|
#else
|
2001-05-11 01:22:35 +00:00
|
|
|
sh_audio->sample_format=AFMT_S16_LE;
|
2001-07-14 16:58:55 +00:00
|
|
|
#endif
|
2001-04-07 21:27:57 +00:00
|
|
|
sh_audio->samplerate=0;
|
2001-04-14 15:25:11 +00:00
|
|
|
//sh_audio->pcm_bswap=0;
|
2001-08-15 12:09:00 +00:00
|
|
|
sh_audio->o_bps=0;
|
2001-04-07 21:27:57 +00:00
|
|
|
|
2001-05-11 01:22:35 +00:00
|
|
|
sh_audio->a_buffer_size=0;
|
2001-04-07 21:27:57 +00:00
|
|
|
sh_audio->a_buffer=NULL;
|
2001-04-06 16:31:18 +00:00
|
|
|
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->a_in_buffer_len=0;
|
|
|
|
|
2001-05-11 01:22:35 +00:00
|
|
|
// setup required min. in/out buffer size:
|
|
|
|
sh_audio->audio_out_minsize=8192;// default size, maybe not enough for Win32/ACM
|
|
|
|
|
|
|
|
switch(driver){
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_ACM:
|
2001-08-14 18:28:56 +00:00
|
|
|
#ifndef USE_WIN32DLL
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"Win32/ACM audio codec disabled, or unavailable on non-x86 CPU -> force nosound :(\n");
|
2001-07-12 15:35:52 +00:00
|
|
|
driver=0;
|
|
|
|
#else
|
2001-04-06 16:31:18 +00:00
|
|
|
// Win32 ACM audio codec:
|
|
|
|
if(init_acm_audio_codec(sh_audio)){
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
|
2001-04-06 16:31:18 +00:00
|
|
|
sh_audio->channels=sh_audio->o_wf.nChannels;
|
|
|
|
sh_audio->samplerate=sh_audio->o_wf.nSamplesPerSec;
|
2001-05-11 01:22:35 +00:00
|
|
|
// if(sh_audio->audio_out_minsize>16384) sh_audio->audio_out_minsize=16384;
|
|
|
|
// sh_audio->a_buffer_size=sh_audio->audio_out_minsize;
|
|
|
|
// if(sh_audio->a_buffer_size<sh_audio->audio_out_minsize+MAX_OUTBURST)
|
|
|
|
// sh_audio->a_buffer_size=sh_audio->audio_out_minsize+MAX_OUTBURST;
|
2001-04-06 16:31:18 +00:00
|
|
|
} else {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"Could not load/initialize Win32/ACM AUDIO codec (missing DLL file?)\n");
|
2001-04-07 21:27:57 +00:00
|
|
|
driver=0;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
2001-07-12 15:35:52 +00:00
|
|
|
#endif
|
2001-05-11 01:22:35 +00:00
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_DSHOW:
|
2001-04-06 16:31:18 +00:00
|
|
|
#ifndef USE_DIRECTSHOW
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"Compiled without DirectShow support -> force nosound :(\n");
|
2001-04-07 21:27:57 +00:00
|
|
|
driver=0;
|
2001-04-06 16:31:18 +00:00
|
|
|
#else
|
|
|
|
// Win32 DShow audio codec:
|
2001-04-11 01:38:57 +00:00
|
|
|
// printf("DShow_audio: channs=%d rate=%d\n",sh_audio->channels,sh_audio->samplerate);
|
2001-04-15 14:33:49 +00:00
|
|
|
if(DS_AudioDecoder_Open(sh_audio->codec->dll,&sh_audio->codec->guid,sh_audio->wf)){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"ERROR: Could not load/initialize Win32/DirctShow AUDIO codec: %s\n",sh_audio->codec->dll);
|
2001-04-07 21:27:57 +00:00
|
|
|
driver=0;
|
|
|
|
} else {
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
|
2001-04-15 14:33:49 +00:00
|
|
|
sh_audio->channels=sh_audio->wf->nChannels;
|
|
|
|
sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
|
|
|
|
sh_audio->audio_in_minsize=2*sh_audio->wf->nBlockAlign;
|
2001-04-06 16:31:18 +00:00
|
|
|
if(sh_audio->audio_in_minsize<8192) sh_audio->audio_in_minsize=8192;
|
|
|
|
sh_audio->a_in_buffer_size=sh_audio->audio_in_minsize;
|
|
|
|
sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
|
|
|
|
sh_audio->a_in_buffer_len=0;
|
2001-05-11 01:22:35 +00:00
|
|
|
sh_audio->audio_out_minsize=16384;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-05-11 01:22:35 +00:00
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_PCM:
|
|
|
|
case AFM_DVDPCM:
|
|
|
|
case AFM_ALAW:
|
2001-05-11 01:22:35 +00:00
|
|
|
// PCM, aLaw
|
|
|
|
sh_audio->audio_out_minsize=2048;
|
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_AC3:
|
2001-05-11 01:22:35 +00:00
|
|
|
// Dolby AC3 audio:
|
|
|
|
sh_audio->audio_out_minsize=4*256*6;
|
|
|
|
break;
|
2001-08-15 11:50:55 +00:00
|
|
|
case AFM_HWAC3:
|
|
|
|
// Dolby AC3 audio:
|
|
|
|
sh_audio->audio_out_minsize=4*256*6;
|
|
|
|
sh_audio->sample_format = AFMT_AC3;
|
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_GSM:
|
2001-05-11 01:22:35 +00:00
|
|
|
// MS-GSM audio codec:
|
|
|
|
sh_audio->audio_out_minsize=4*320;
|
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_MPEG:
|
2001-05-11 01:22:35 +00:00
|
|
|
// MPEG Audio:
|
|
|
|
sh_audio->audio_out_minsize=4608;
|
|
|
|
break;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
|
|
|
|
2001-04-07 21:27:57 +00:00
|
|
|
if(!driver) return 0;
|
2001-04-06 16:31:18 +00:00
|
|
|
|
|
|
|
// allocate audio out buffer:
|
2001-05-11 01:22:35 +00:00
|
|
|
sh_audio->a_buffer_size=sh_audio->audio_out_minsize+MAX_OUTBURST; // worst case calc.
|
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_V,"dec_audio: Allocating %d + %d = %d bytes for output buffer\n",
|
2001-05-11 01:22:35 +00:00
|
|
|
sh_audio->audio_out_minsize,MAX_OUTBURST,sh_audio->a_buffer_size);
|
|
|
|
|
2001-04-06 16:31:18 +00:00
|
|
|
sh_audio->a_buffer=malloc(sh_audio->a_buffer_size);
|
2001-05-11 01:22:35 +00:00
|
|
|
if(!sh_audio->a_buffer){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"Cannot allocate audio out buffer\n");
|
2001-05-11 01:22:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-04-06 16:31:18 +00:00
|
|
|
memset(sh_audio->a_buffer,0,sh_audio->a_buffer_size);
|
|
|
|
sh_audio->a_buffer_len=0;
|
|
|
|
|
2001-04-07 21:27:57 +00:00
|
|
|
switch(driver){
|
2001-08-14 18:28:56 +00:00
|
|
|
#ifdef USE_WIN32DLL
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_ACM: {
|
2001-05-11 01:22:35 +00:00
|
|
|
int ret=acm_decode_audio(sh_audio,sh_audio->a_buffer,4096,sh_audio->a_buffer_size);
|
2001-04-06 16:31:18 +00:00
|
|
|
if(ret<0){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"ACM decoding error: %d\n",ret);
|
2001-04-07 21:27:57 +00:00
|
|
|
driver=0;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
2001-04-26 01:57:31 +00:00
|
|
|
sh_audio->a_buffer_len=ret;
|
2001-04-07 21:27:57 +00:00
|
|
|
break;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
2001-07-12 15:35:52 +00:00
|
|
|
#endif
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_PCM: {
|
2001-04-06 16:31:18 +00:00
|
|
|
// AVI PCM Audio:
|
2001-04-15 14:33:49 +00:00
|
|
|
WAVEFORMATEX *h=sh_audio->wf;
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->i_bps=h->nAvgBytesPerSec;
|
2001-04-06 16:31:18 +00:00
|
|
|
sh_audio->channels=h->nChannels;
|
|
|
|
sh_audio->samplerate=h->nSamplesPerSec;
|
|
|
|
sh_audio->samplesize=(h->wBitsPerSample+7)/8;
|
2001-05-11 01:22:35 +00:00
|
|
|
switch(sh_audio->format){ // hardware formats:
|
|
|
|
case 0x6: sh_audio->sample_format=AFMT_A_LAW;break;
|
|
|
|
case 0x7: sh_audio->sample_format=AFMT_MU_LAW;break;
|
|
|
|
case 0x11: sh_audio->sample_format=AFMT_IMA_ADPCM;break;
|
|
|
|
case 0x50: sh_audio->sample_format=AFMT_MPEG;break;
|
|
|
|
// case 0x2000: sh_audio->sample_format=AFMT_AC3;
|
|
|
|
default: sh_audio->sample_format=(sh_audio->samplesize==2)?AFMT_S16_LE:AFMT_U8;
|
|
|
|
}
|
2001-04-14 15:25:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_DVDPCM: {
|
2001-04-14 15:25:11 +00:00
|
|
|
// DVD PCM Audio:
|
|
|
|
sh_audio->channels=2;
|
|
|
|
sh_audio->samplerate=48000;
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->i_bps=2*2*48000;
|
2001-04-06 16:31:18 +00:00
|
|
|
// sh_audio->pcm_bswap=1;
|
2001-04-07 21:27:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_AC3: {
|
2001-04-06 16:31:18 +00:00
|
|
|
// Dolby AC3 audio:
|
2001-07-07 18:46:15 +00:00
|
|
|
dec_audio_sh=sh_audio; // save sh_audio for the callback:
|
2001-04-06 16:31:18 +00:00
|
|
|
ac3_config.fill_buffer_callback = ac3_fill_buffer;
|
|
|
|
ac3_config.num_output_ch = 2;
|
|
|
|
ac3_config.flags = 0;
|
|
|
|
#ifdef HAVE_MMX
|
|
|
|
ac3_config.flags |= AC3_MMX_ENABLE;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_3DNOW
|
|
|
|
ac3_config.flags |= AC3_3DNOW_ENABLE;
|
|
|
|
#endif
|
|
|
|
ac3_init();
|
|
|
|
sh_audio->ac3_frame = ac3_decode_frame();
|
|
|
|
if(sh_audio->ac3_frame){
|
2001-05-10 03:39:54 +00:00
|
|
|
ac3_frame_t* fr=(ac3_frame_t*)sh_audio->ac3_frame;
|
|
|
|
sh_audio->samplerate=fr->sampling_rate;
|
2001-04-06 16:31:18 +00:00
|
|
|
sh_audio->channels=2;
|
2001-05-10 03:39:54 +00:00
|
|
|
// 1 frame: 6*256 samples 1 sec: sh_audio->samplerate samples
|
|
|
|
//sh_audio->i_bps=fr->frame_size*fr->sampling_rate/(6*256);
|
|
|
|
sh_audio->i_bps=fr->bit_rate*(1000/8);
|
2001-04-07 21:27:57 +00:00
|
|
|
} else {
|
|
|
|
driver=0; // bad frame -> disable audio
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-08-15 11:50:55 +00:00
|
|
|
case AFM_HWAC3: {
|
|
|
|
unsigned char *buffer;
|
|
|
|
struct hwac3info ai;
|
|
|
|
int len, skipped;
|
2001-08-15 12:09:00 +00:00
|
|
|
len = ds_get_packet(sh_audio->ds, &buffer); // maybe 1 packet is not enough,
|
|
|
|
// at least for mpeg, PS packets contain about max. 2000 bytes of data.
|
2001-08-15 11:50:55 +00:00
|
|
|
if(ac3_iec958_parse_syncinfo(buffer, len, &ai, &skipped) < 0) {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR, "AC3 stream not valid.\n");
|
2001-08-15 11:50:55 +00:00
|
|
|
driver = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(ai.samplerate != 48000) {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_ERR,"Only 48000 Hz streams supported.\n");
|
2001-08-15 11:50:55 +00:00
|
|
|
driver = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sh_audio->samplerate=ai.samplerate;
|
2001-08-15 12:09:00 +00:00
|
|
|
sh_audio->samplesize=ai.framesize;
|
2001-08-15 11:50:55 +00:00
|
|
|
sh_audio->channels=1;
|
|
|
|
sh_audio->i_bps=ai.bitrate*(1000/8);
|
|
|
|
sh_audio->ac3_frame=malloc(6144);
|
2001-08-15 12:09:00 +00:00
|
|
|
sh_audio->o_bps=sh_audio->i_bps; // XXX FIXME!!! XXX
|
2001-08-15 11:50:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_ALAW: {
|
2001-04-06 16:31:18 +00:00
|
|
|
// aLaw audio codec:
|
2001-04-15 14:33:49 +00:00
|
|
|
sh_audio->channels=sh_audio->wf->nChannels;
|
|
|
|
sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->i_bps=sh_audio->channels*sh_audio->samplerate;
|
2001-04-07 21:27:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_GSM: {
|
2001-04-06 16:31:18 +00:00
|
|
|
// MS-GSM audio codec:
|
|
|
|
GSM_Init();
|
2001-04-15 14:33:49 +00:00
|
|
|
sh_audio->channels=sh_audio->wf->nChannels;
|
|
|
|
sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
|
2001-05-10 03:39:54 +00:00
|
|
|
// decodes 65 byte -> 320 short
|
|
|
|
// 1 sec: sh_audio->channels*sh_audio->samplerate samples
|
|
|
|
// 1 frame: 320 samples
|
|
|
|
sh_audio->i_bps=65*(sh_audio->channels*sh_audio->samplerate)/320; // 1:10
|
2001-04-07 21:27:57 +00:00
|
|
|
break;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_MPEG: {
|
2001-04-06 16:31:18 +00:00
|
|
|
// MPEG Audio:
|
2001-07-07 18:46:15 +00:00
|
|
|
dec_audio_sh=sh_audio; // save sh_audio for the callback:
|
2001-05-08 21:49:00 +00:00
|
|
|
#ifdef USE_FAKE_MONO
|
|
|
|
MP3_Init(fakemono);
|
|
|
|
#else
|
2001-04-06 16:31:18 +00:00
|
|
|
MP3_Init();
|
2001-05-08 21:49:00 +00:00
|
|
|
#endif
|
2001-04-06 16:31:18 +00:00
|
|
|
MP3_samplerate=MP3_channels=0;
|
|
|
|
// printf("[\n");
|
|
|
|
sh_audio->a_buffer_len=MP3_DecodeFrame(sh_audio->a_buffer,-1);
|
|
|
|
// printf("]\n");
|
|
|
|
sh_audio->channels=2; // hack
|
|
|
|
sh_audio->samplerate=MP3_samplerate;
|
2001-05-10 03:39:54 +00:00
|
|
|
sh_audio->i_bps=MP3_bitrate*(1000/8);
|
2001-04-07 21:27:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!sh_audio->channels || !sh_audio->samplerate){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_WARN,"Unknown/missing audio format, using nosound\n");
|
2001-04-07 21:27:57 +00:00
|
|
|
driver=0;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
|
|
|
|
2001-04-07 21:27:57 +00:00
|
|
|
if(!driver){
|
|
|
|
if(sh_audio->a_buffer) free(sh_audio->a_buffer);
|
2001-07-13 08:54:24 +00:00
|
|
|
sh_audio->a_buffer=NULL;
|
2001-04-07 21:27:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-04-06 16:31:18 +00:00
|
|
|
|
2001-08-15 12:09:00 +00:00
|
|
|
if(!sh_audio->o_bps)
|
2001-04-07 21:27:57 +00:00
|
|
|
sh_audio->o_bps=sh_audio->channels*sh_audio->samplerate*sh_audio->samplesize;
|
|
|
|
return driver;
|
2001-04-06 16:31:18 +00:00
|
|
|
}
|
|
|
|
|
2001-05-10 03:39:54 +00:00
|
|
|
// Audio decoding:
|
|
|
|
|
|
|
|
// Decode a single frame (mp3,acm etc) or 'minlen' bytes (pcm/alaw etc)
|
|
|
|
// buffer length is 'maxlen' bytes, it shouldn't be exceeded...
|
2001-04-06 01:18:59 +00:00
|
|
|
|
2001-05-10 03:39:54 +00:00
|
|
|
int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen){
|
2001-04-06 01:18:59 +00:00
|
|
|
int len=-1;
|
2001-04-07 21:27:57 +00:00
|
|
|
switch(sh_audio->codec->driver){
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_MPEG: // MPEG layer 2 or 3
|
2001-04-06 01:18:59 +00:00
|
|
|
len=MP3_DecodeFrame(buf,-1);
|
2001-08-11 20:37:33 +00:00
|
|
|
// len=MP3_DecodeFrame(buf,3);
|
2001-04-06 01:18:59 +00:00
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_PCM: // AVI PCM
|
2001-06-08 23:31:06 +00:00
|
|
|
len=demux_read_data(sh_audio->ds,buf,minlen);
|
2001-04-14 15:25:11 +00:00
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_DVDPCM: // DVD PCM
|
2001-04-14 15:25:11 +00:00
|
|
|
{ int j;
|
2001-05-10 03:39:54 +00:00
|
|
|
len=demux_read_data(sh_audio->ds,buf,minlen);
|
2001-04-06 01:18:59 +00:00
|
|
|
//if(i&1){ printf("Warning! pcm_audio_size&1 !=0 (%d)\n",i);i&=~1; }
|
2001-05-10 03:39:54 +00:00
|
|
|
// swap endian:
|
2001-04-06 01:18:59 +00:00
|
|
|
for(j=0;j<len;j+=2){
|
|
|
|
char x=buf[j];
|
|
|
|
buf[j]=buf[j+1];
|
|
|
|
buf[j+1]=x;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_ALAW: // aLaw decoder
|
2001-05-10 03:39:54 +00:00
|
|
|
{ int l=demux_read_data(sh_audio->ds,buf,minlen/2);
|
2001-04-06 01:18:59 +00:00
|
|
|
unsigned short *d=(unsigned short *) buf;
|
|
|
|
unsigned char *s=buf;
|
|
|
|
len=2*l;
|
2001-05-11 01:22:35 +00:00
|
|
|
if(sh_audio->format==6){
|
|
|
|
// aLaw
|
2001-05-25 18:26:24 +00:00
|
|
|
while(l>0){ --l; d[l]=alaw2short[s[l]]; }
|
2001-05-11 01:22:35 +00:00
|
|
|
} else {
|
|
|
|
// uLaw
|
2001-05-25 18:26:24 +00:00
|
|
|
while(l>0){ --l; d[l]=ulaw2short[s[l]]; }
|
2001-04-06 01:18:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_GSM: // MS-GSM decoder
|
2001-04-06 01:18:59 +00:00
|
|
|
{ unsigned char buf[65]; // 65 bytes / frame
|
2001-05-10 03:39:54 +00:00
|
|
|
if(demux_read_data(sh_audio->ds,buf,65)!=65) break; // EOF
|
|
|
|
XA_MSGSM_Decoder(buf,(unsigned short *) buf); // decodes 65 byte -> 320 short
|
|
|
|
// XA_GSM_Decoder(buf,(unsigned short *) &sh_audio->a_buffer[sh_audio->a_buffer_len]); // decodes 33 byte -> 160 short
|
|
|
|
len=2*320;
|
2001-04-06 01:18:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_AC3: // AC3 decoder
|
2001-04-06 01:18:59 +00:00
|
|
|
//printf("{1:%d}",avi_header.idx_pos);fflush(stdout);
|
|
|
|
if(!sh_audio->ac3_frame) sh_audio->ac3_frame=ac3_decode_frame();
|
|
|
|
//printf("{2:%d}",avi_header.idx_pos);fflush(stdout);
|
|
|
|
if(sh_audio->ac3_frame){
|
2001-04-06 16:31:18 +00:00
|
|
|
len = 256 * 6 *sh_audio->channels*sh_audio->samplesize;
|
2001-04-23 03:42:17 +00:00
|
|
|
memcpy(buf,((ac3_frame_t*)sh_audio->ac3_frame)->audio_data,len);
|
2001-04-06 01:18:59 +00:00
|
|
|
sh_audio->ac3_frame=NULL;
|
|
|
|
}
|
|
|
|
//printf("{3:%d}",avi_header.idx_pos);fflush(stdout);
|
|
|
|
break;
|
2001-08-15 11:50:55 +00:00
|
|
|
case AFM_HWAC3: // AC3 through SPDIF
|
|
|
|
if(demux_read_data(sh_audio->ds,sh_audio->ac3_frame, 6144) != 6144)
|
|
|
|
break; //EOF
|
|
|
|
ac3_iec958_build_burst(1536, 0x1F, 1, buf, sh_audio->ac3_frame);
|
|
|
|
len = 6144;
|
|
|
|
break;
|
2001-08-14 18:28:56 +00:00
|
|
|
#ifdef USE_WIN32DLL
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_ACM:
|
2001-05-11 01:22:35 +00:00
|
|
|
// len=sh_audio->audio_out_minsize; // optimal decoded fragment size
|
|
|
|
// if(len<minlen) len=minlen; else
|
|
|
|
// if(len>maxlen) len=maxlen;
|
|
|
|
// len=acm_decode_audio(sh_audio,buf,len);
|
|
|
|
len=acm_decode_audio(sh_audio,buf,minlen,maxlen);
|
2001-04-06 01:18:59 +00:00
|
|
|
break;
|
2001-07-12 15:35:52 +00:00
|
|
|
#endif
|
2001-05-10 03:39:54 +00:00
|
|
|
|
2001-04-06 01:18:59 +00:00
|
|
|
#ifdef USE_DIRECTSHOW
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_DSHOW: // DirectShow
|
2001-07-16 12:53:48 +00:00
|
|
|
{ int size_in=0;
|
2001-04-06 01:18:59 +00:00
|
|
|
int size_out=0;
|
|
|
|
int srcsize=DS_AudioDecoder_GetSrcSize(maxlen);
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DECAUDIO,MSGL_DBG3,"DShow says: srcsize=%d (buffsize=%d) out_size=%d\n",srcsize,sh_audio->a_in_buffer_size,maxlen);
|
2001-04-06 01:18:59 +00:00
|
|
|
if(srcsize>sh_audio->a_in_buffer_size) srcsize=sh_audio->a_in_buffer_size; // !!!!!!
|
|
|
|
if(sh_audio->a_in_buffer_len<srcsize){
|
|
|
|
sh_audio->a_in_buffer_len+=
|
|
|
|
demux_read_data(sh_audio->ds,&sh_audio->a_in_buffer[sh_audio->a_in_buffer_len],
|
|
|
|
srcsize-sh_audio->a_in_buffer_len);
|
|
|
|
}
|
|
|
|
DS_AudioDecoder_Convert(sh_audio->a_in_buffer,sh_audio->a_in_buffer_len,
|
|
|
|
buf,maxlen, &size_in,&size_out);
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_dbg(MSGT_DECAUDIO,MSGL_DBG2,"DShow: audio %d -> %d converted (in_buf_len=%d of %d) %d\n",size_in,size_out,sh_audio->a_in_buffer_len,sh_audio->a_in_buffer_size,ds_tell_pts(sh_audio->ds));
|
2001-04-06 01:18:59 +00:00
|
|
|
if(size_in>=sh_audio->a_in_buffer_len){
|
|
|
|
sh_audio->a_in_buffer_len=0;
|
|
|
|
} else {
|
|
|
|
sh_audio->a_in_buffer_len-=size_in;
|
|
|
|
memcpy(sh_audio->a_in_buffer,&sh_audio->a_in_buffer[size_in],sh_audio->a_in_buffer_len);
|
|
|
|
}
|
|
|
|
len=size_out;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2001-07-16 12:53:48 +00:00
|
|
|
void resync_audio_stream(sh_audio_t *sh_audio){
|
|
|
|
switch(sh_audio->codec->driver){
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_MPEG:
|
2001-07-16 12:53:48 +00:00
|
|
|
MP3_DecodeFrame(NULL,-2); // resync
|
|
|
|
MP3_DecodeFrame(NULL,-2); // resync
|
|
|
|
MP3_DecodeFrame(NULL,-2); // resync
|
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_AC3:
|
2001-07-16 12:53:48 +00:00
|
|
|
ac3_bitstream_reset(); // reset AC3 bitstream buffer
|
|
|
|
// if(verbose){ printf("Resyncing AC3 audio...");fflush(stdout);}
|
|
|
|
sh_audio->ac3_frame=ac3_decode_frame(); // resync
|
|
|
|
// if(verbose) printf(" OK!\n");
|
|
|
|
break;
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_ACM:
|
|
|
|
case AFM_DSHOW:
|
2001-08-15 11:50:55 +00:00
|
|
|
case AFM_HWAC3:
|
2001-07-16 12:53:48 +00:00
|
|
|
sh_audio->a_in_buffer_len=0; // reset ACM/DShow audio buffer
|
|
|
|
break;
|
|
|
|
}
|
2001-07-29 00:39:47 +00:00
|
|
|
|
2001-07-16 12:53:48 +00:00
|
|
|
}
|
2001-04-06 01:18:59 +00:00
|
|
|
|
2001-07-16 12:53:48 +00:00
|
|
|
void skip_audio_frame(sh_audio_t *sh_audio){
|
|
|
|
switch(sh_audio->codec->driver){
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_MPEG: MP3_DecodeFrame(NULL,-2);break; // skip MPEG frame
|
|
|
|
case AFM_AC3: sh_audio->ac3_frame=ac3_decode_frame();break; // skip AC3 frame
|
|
|
|
case AFM_ACM:
|
|
|
|
case AFM_DSHOW: {
|
2001-07-29 00:39:47 +00:00
|
|
|
int skip=sh_audio->wf->nBlockAlign;
|
|
|
|
if(skip<16){
|
|
|
|
skip=(sh_audio->wf->nAvgBytesPerSec/16)&(~7);
|
|
|
|
if(skip<16) skip=16;
|
|
|
|
}
|
|
|
|
demux_read_data(sh_audio->ds,NULL,skip);
|
|
|
|
break;
|
|
|
|
}
|
2001-07-29 02:08:27 +00:00
|
|
|
case AFM_PCM:
|
|
|
|
case AFM_DVDPCM:
|
|
|
|
case AFM_ALAW: {
|
2001-07-29 00:39:47 +00:00
|
|
|
int skip=sh_audio->i_bps/16;
|
|
|
|
skip=skip&(~3);
|
|
|
|
demux_read_data(sh_audio->ds,NULL,skip);
|
|
|
|
break;
|
|
|
|
}
|
2001-07-16 12:53:48 +00:00
|
|
|
default: ds_fill_buffer(sh_audio->ds); // skip PCM frame
|
|
|
|
}
|
|
|
|
}
|