2001-08-16 00:50:02 +00:00
|
|
|
/********************************************************
|
|
|
|
|
|
|
|
DirectShow audio decoder
|
|
|
|
Copyright 2001 Eugene Kuznetsov (divx@euro.ru)
|
|
|
|
|
|
|
|
*********************************************************/
|
2003-04-21 21:06:08 +00:00
|
|
|
#include "config.h"
|
2001-10-04 02:21:47 +00:00
|
|
|
|
2002-01-02 17:11:09 +00:00
|
|
|
#include "libwin32.h"
|
|
|
|
|
|
|
|
#include "DS_Filter.h"
|
|
|
|
|
2008-01-20 17:16:39 +00:00
|
|
|
struct DS_AudioDecoder
|
2002-01-02 17:11:09 +00:00
|
|
|
{
|
|
|
|
WAVEFORMATEX in_fmt;
|
|
|
|
AM_MEDIA_TYPE m_sOurType, m_sDestType;
|
|
|
|
DS_Filter* m_pDS_Filter;
|
|
|
|
char* m_sVhdr;
|
|
|
|
char* m_sVhdr2;
|
|
|
|
};
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
#include "DS_AudioDecoder.h"
|
2003-04-21 21:06:08 +00:00
|
|
|
#ifdef WIN32_LOADER
|
2008-04-28 14:50:24 +00:00
|
|
|
#include "ldt_keeper.h"
|
2003-04-21 21:06:08 +00:00
|
|
|
#endif
|
2002-01-02 17:11:09 +00:00
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2001-11-21 23:42:06 +00:00
|
|
|
#include <stdlib.h>
|
2001-08-16 00:50:02 +00:00
|
|
|
|
|
|
|
typedef long STDCALL (*GETCLASS) (GUID*, GUID*, void**);
|
|
|
|
|
2007-03-02 18:52:10 +00:00
|
|
|
static SampleProcUserData sampleProcData;
|
|
|
|
|
|
|
|
|
2001-12-10 21:53:13 +00:00
|
|
|
DS_AudioDecoder * DS_AudioDecoder_Open(char* dllname, GUID* guid, WAVEFORMATEX* wf)
|
|
|
|
//DS_AudioDecoder * DS_AudioDecoder_Create(const CodecInfo * info, const WAVEFORMATEX* wf)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2001-11-21 23:42:06 +00:00
|
|
|
DS_AudioDecoder *this;
|
|
|
|
int sz;
|
|
|
|
WAVEFORMATEX* pWF;
|
2001-12-10 21:53:13 +00:00
|
|
|
|
2003-04-21 21:06:08 +00:00
|
|
|
#ifdef WIN32_LOADER
|
2001-12-10 21:53:13 +00:00
|
|
|
Setup_LDT_Keeper();
|
|
|
|
Setup_FS_Segment();
|
2003-04-21 21:06:08 +00:00
|
|
|
#endif
|
2001-11-21 23:42:06 +00:00
|
|
|
|
|
|
|
this = malloc(sizeof(DS_AudioDecoder));
|
|
|
|
|
|
|
|
sz = 18 + wf->cbSize;
|
|
|
|
this->m_sVhdr = malloc(sz);
|
|
|
|
memcpy(this->m_sVhdr, wf, sz);
|
2001-12-31 04:35:20 +00:00
|
|
|
this->m_sVhdr2 = malloc(18);
|
|
|
|
memcpy(this->m_sVhdr2, this->m_sVhdr, 18);
|
2001-11-21 23:42:06 +00:00
|
|
|
|
|
|
|
pWF = (WAVEFORMATEX*)this->m_sVhdr2;
|
|
|
|
pWF->wFormatTag = 1;
|
|
|
|
pWF->wBitsPerSample = 16;
|
2001-12-31 04:35:20 +00:00
|
|
|
pWF->nBlockAlign = pWF->nChannels * (pWF->wBitsPerSample + 7) / 8;
|
2001-11-21 23:42:06 +00:00
|
|
|
pWF->cbSize = 0;
|
2001-12-31 04:35:20 +00:00
|
|
|
pWF->nAvgBytesPerSec = pWF->nBlockAlign * pWF->nSamplesPerSec;
|
2001-11-21 23:42:06 +00:00
|
|
|
|
|
|
|
memcpy(&this->in_fmt,wf,sizeof(WAVEFORMATEX));
|
2001-08-16 00:50:02 +00:00
|
|
|
|
2001-11-21 23:42:06 +00:00
|
|
|
memset(&this->m_sOurType, 0, sizeof(this->m_sOurType));
|
|
|
|
this->m_sOurType.majortype=MEDIATYPE_Audio;
|
|
|
|
this->m_sOurType.subtype=MEDIASUBTYPE_PCM;
|
|
|
|
this->m_sOurType.subtype.f1=wf->wFormatTag;
|
|
|
|
this->m_sOurType.formattype=FORMAT_WaveFormatEx;
|
|
|
|
this->m_sOurType.lSampleSize=wf->nBlockAlign;
|
|
|
|
this->m_sOurType.bFixedSizeSamples=1;
|
|
|
|
this->m_sOurType.bTemporalCompression=0;
|
|
|
|
this->m_sOurType.pUnk=0;
|
|
|
|
this->m_sOurType.cbFormat=sz;
|
|
|
|
this->m_sOurType.pbFormat=this->m_sVhdr;
|
2001-08-16 00:50:02 +00:00
|
|
|
|
2001-11-21 23:42:06 +00:00
|
|
|
memset(&this->m_sDestType, 0, sizeof(this->m_sDestType));
|
|
|
|
this->m_sDestType.majortype=MEDIATYPE_Audio;
|
|
|
|
this->m_sDestType.subtype=MEDIASUBTYPE_PCM;
|
2001-12-31 04:35:20 +00:00
|
|
|
// this->m_sDestType.subtype.f1=pWF->wFormatTag;
|
2001-11-21 23:42:06 +00:00
|
|
|
this->m_sDestType.formattype=FORMAT_WaveFormatEx;
|
|
|
|
this->m_sDestType.bFixedSizeSamples=1;
|
|
|
|
this->m_sDestType.bTemporalCompression=0;
|
2001-12-31 04:35:20 +00:00
|
|
|
this->m_sDestType.lSampleSize=pWF->nBlockAlign;
|
2001-12-11 23:29:11 +00:00
|
|
|
if (wf->wFormatTag == 0x130)
|
|
|
|
// ACEL hack to prevent memory corruption
|
|
|
|
// obviosly we are missing something here
|
|
|
|
this->m_sDestType.lSampleSize *= 288;
|
2001-11-21 23:42:06 +00:00
|
|
|
this->m_sDestType.pUnk=0;
|
2001-12-31 04:35:20 +00:00
|
|
|
this->m_sDestType.cbFormat=18; //pWF->cbSize;
|
2001-11-21 23:42:06 +00:00
|
|
|
this->m_sDestType.pbFormat=this->m_sVhdr2;
|
2001-08-16 00:50:02 +00:00
|
|
|
|
2006-03-27 17:25:41 +00:00
|
|
|
//print_wave_header(this->m_sVhdr, MSGL_V);
|
|
|
|
//print_wave_header(this->m_sVhdr2, MSGL_V);
|
2001-12-31 04:35:20 +00:00
|
|
|
|
2001-11-21 23:42:06 +00:00
|
|
|
/*try*/
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2007-03-02 18:52:10 +00:00
|
|
|
this->m_pDS_Filter = DS_FilterCreate(dllname, guid, &this->m_sOurType, &this->m_sDestType,&sampleProcData);
|
2001-11-21 23:42:06 +00:00
|
|
|
if( !this->m_pDS_Filter ) {
|
|
|
|
free(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:07:07 +00:00
|
|
|
//Commit should be done before binary codec start
|
|
|
|
this->m_pDS_Filter->m_pAll->vt->Commit(this->m_pDS_Filter->m_pAll);
|
|
|
|
|
2001-11-22 03:16:31 +00:00
|
|
|
this->m_pDS_Filter->Start(this->m_pDS_Filter);
|
2001-08-16 00:50:02 +00:00
|
|
|
|
|
|
|
}
|
2001-11-21 23:42:06 +00:00
|
|
|
/*
|
|
|
|
catch (FatalError& e)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
|
|
|
e.PrintAll();
|
|
|
|
delete[] m_sVhdr;
|
|
|
|
delete[] m_sVhdr2;
|
|
|
|
delete m_pDS_Filter;
|
|
|
|
throw;
|
|
|
|
}
|
2001-11-21 23:42:06 +00:00
|
|
|
*/
|
|
|
|
return this;
|
2001-08-16 00:50:02 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 23:42:06 +00:00
|
|
|
void DS_AudioDecoder_Destroy(DS_AudioDecoder *this)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2001-11-21 23:42:06 +00:00
|
|
|
free(this->m_sVhdr);
|
|
|
|
free(this->m_sVhdr2);
|
|
|
|
DS_Filter_Destroy(this->m_pDS_Filter);
|
|
|
|
free(this);
|
2001-08-16 00:50:02 +00:00
|
|
|
}
|
|
|
|
|
2002-01-02 17:11:09 +00:00
|
|
|
int DS_AudioDecoder_Convert(DS_AudioDecoder *this, const void* in_data, unsigned int in_size,
|
|
|
|
void* out_data, unsigned int out_size,
|
|
|
|
unsigned int* size_read, unsigned int* size_written)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2002-01-02 17:11:09 +00:00
|
|
|
unsigned int written = 0;
|
|
|
|
unsigned int read = 0;
|
2001-11-21 23:42:06 +00:00
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
if (!in_data || !out_data)
|
|
|
|
return -1;
|
|
|
|
|
2003-04-21 21:06:08 +00:00
|
|
|
#ifdef WIN32_LOADER
|
2001-12-10 21:53:13 +00:00
|
|
|
Setup_FS_Segment();
|
2003-04-21 21:06:08 +00:00
|
|
|
#endif
|
2001-12-10 21:53:13 +00:00
|
|
|
|
2001-11-21 23:42:06 +00:00
|
|
|
in_size -= in_size%this->in_fmt.nBlockAlign;
|
2001-08-16 00:50:02 +00:00
|
|
|
while (in_size>0)
|
|
|
|
{
|
|
|
|
IMediaSample* sample=0;
|
2001-11-21 23:42:06 +00:00
|
|
|
char* ptr;
|
|
|
|
int result;
|
|
|
|
this->m_pDS_Filter->m_pAll->vt->GetBuffer(this->m_pDS_Filter->m_pAll, &sample, 0, 0, 0);
|
|
|
|
if (!sample)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2001-10-04 02:21:47 +00:00
|
|
|
Debug printf("DS_AudioDecoder::Convert() Error: null sample\n");
|
2001-08-16 00:50:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-12-11 23:29:11 +00:00
|
|
|
sample->vt->SetActualDataLength(sample, this->in_fmt.nBlockAlign);
|
2001-08-16 00:50:02 +00:00
|
|
|
sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
2001-11-21 23:42:06 +00:00
|
|
|
memcpy(ptr, (const uint8_t*)in_data + read, this->in_fmt.nBlockAlign);
|
|
|
|
sample->vt->SetSyncPoint(sample, 1);
|
2001-08-16 00:50:02 +00:00
|
|
|
sample->vt->SetPreroll(sample, 0);
|
2001-11-21 23:42:06 +00:00
|
|
|
result = this->m_pDS_Filter->m_pImp->vt->Receive(this->m_pDS_Filter->m_pImp, sample);
|
2001-08-16 00:50:02 +00:00
|
|
|
if (result)
|
|
|
|
Debug printf("DS_AudioDecoder::Convert() Error: putting data into input pin %x\n", result);
|
2007-03-02 18:52:10 +00:00
|
|
|
if ((written + sampleProcData.frame_size) > out_size)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
|
|
|
sample->vt->Release((IUnknown*)sample);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-02 18:52:10 +00:00
|
|
|
memcpy((uint8_t*)out_data + written, sampleProcData.frame_pointer, sampleProcData.frame_size);
|
2001-08-16 00:50:02 +00:00
|
|
|
sample->vt->Release((IUnknown*)sample);
|
2001-11-21 23:42:06 +00:00
|
|
|
read+=this->in_fmt.nBlockAlign;
|
2007-03-02 18:52:10 +00:00
|
|
|
written+=sampleProcData.frame_size;
|
2001-12-11 23:29:11 +00:00
|
|
|
break;
|
2001-08-16 00:50:02 +00:00
|
|
|
}
|
|
|
|
if (size_read)
|
|
|
|
*size_read = read;
|
|
|
|
if (size_written)
|
|
|
|
*size_written = written;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-21 23:42:06 +00:00
|
|
|
int DS_AudioDecoder_GetSrcSize(DS_AudioDecoder *this, int dest_size)
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2001-11-21 23:42:06 +00:00
|
|
|
double efficiency =(double) this->in_fmt.nAvgBytesPerSec
|
|
|
|
/ (this->in_fmt.nSamplesPerSec*this->in_fmt.nBlockAlign);
|
|
|
|
int frames = (int)(dest_size*efficiency);;
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
if (frames < 1)
|
|
|
|
frames = 1;
|
2001-11-21 23:42:06 +00:00
|
|
|
return frames * this->in_fmt.nBlockAlign;
|
2001-08-16 00:50:02 +00:00
|
|
|
}
|