1545
|
1 /********************************************************
|
|
2
|
|
3 DirectShow audio decoder
|
|
4 Copyright 2001 Eugene Kuznetsov (divx@euro.ru)
|
|
5
|
|
6 *********************************************************/
|
|
7
|
2069
|
8 #include "DS_AudioDecoder.h"
|
|
9 #include <string.h>
|
|
10 #include <stdio.h>
|
|
11
|
|
12 // using namespace std;
|
|
13
|
1545
|
14 #define __MODULE__ "DirectShow audio decoder"
|
|
15 const GUID FORMAT_WaveFormatEx = {
|
|
16 0x05589f81, 0xc356, 0x11CE,
|
|
17 { 0xBF, 0x01, 0x00, 0xAA, 0x00, 0x55, 0x59, 0x5A }
|
|
18 };
|
|
19 const GUID MEDIATYPE_Audio = {
|
|
20 0x73647561, 0x0000, 0x0010,
|
|
21 { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
|
|
22 };
|
|
23 const GUID MEDIASUBTYPE_PCM = {
|
|
24 0x00000001, 0x0000, 0x0010,
|
|
25 { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
|
|
26 };
|
|
27
|
|
28 typedef long STDCALL (*GETCLASS) (GUID*, GUID*, void**);
|
|
29
|
|
30 DS_AudioDecoder::DS_AudioDecoder(const CodecInfo& info, const WAVEFORMATEX* wf)
|
|
31 : IAudioDecoder(info, wf), m_pDS_Filter(0), m_sVhdr(0), m_sVhdr2(0)
|
|
32 {
|
2069
|
33 int sz = 18 + wf->cbSize;
|
|
34 m_sVhdr=new char[sz];
|
|
35 memcpy(m_sVhdr, wf, sz);
|
|
36 m_sVhdr2=new char[sz];
|
|
37 memcpy(m_sVhdr2, m_sVhdr, sz);
|
1545
|
38 WAVEFORMATEX* pWF=(WAVEFORMATEX*)m_sVhdr2;
|
|
39 pWF->wFormatTag=1;
|
|
40 pWF->wBitsPerSample=16;
|
|
41 pWF->nBlockAlign=2*pWF->nChannels;
|
|
42 pWF->cbSize=0;
|
|
43 in_fmt=*wf;
|
|
44
|
|
45 memset(&m_sOurType, 0, sizeof(m_sOurType));
|
|
46 m_sOurType.majortype=MEDIATYPE_Audio;
|
|
47 m_sOurType.subtype=MEDIASUBTYPE_PCM;
|
|
48 m_sOurType.subtype.f1=wf->wFormatTag;
|
|
49 m_sOurType.formattype=FORMAT_WaveFormatEx;
|
|
50 m_sOurType.lSampleSize=wf->nBlockAlign;
|
|
51 m_sOurType.bFixedSizeSamples=true;
|
|
52 m_sOurType.bTemporalCompression=false;
|
|
53 m_sOurType.pUnk=0;
|
2069
|
54 m_sOurType.cbFormat=sz;
|
1545
|
55 m_sOurType.pbFormat=m_sVhdr;
|
|
56
|
|
57 memset(&m_sDestType, 0, sizeof(m_sDestType));
|
|
58 m_sDestType.majortype=MEDIATYPE_Audio;
|
|
59 m_sDestType.subtype=MEDIASUBTYPE_PCM;
|
|
60 m_sDestType.formattype=FORMAT_WaveFormatEx;
|
|
61 m_sDestType.bFixedSizeSamples=true;
|
|
62 m_sDestType.bTemporalCompression=false;
|
|
63 m_sDestType.lSampleSize=2*wf->nChannels;
|
|
64 m_sDestType.pUnk=0;
|
2069
|
65 m_sDestType.cbFormat=pWF->cbSize;
|
1545
|
66 m_sDestType.pbFormat=m_sVhdr2;
|
|
67
|
|
68 try
|
|
69 {
|
|
70 m_pDS_Filter = new DS_Filter();
|
2072
|
71 m_pDS_Filter->Create(info.dll, &info.guid, &m_sOurType, &m_sDestType);
|
1545
|
72 m_pDS_Filter->Start();
|
|
73
|
|
74 ALLOCATOR_PROPERTIES props, props1;
|
|
75 props.cBuffers=1;
|
|
76 props.cbBuffer=m_sOurType.lSampleSize;
|
|
77 props.cbAlign=props.cbPrefix=0;
|
|
78 m_pDS_Filter->m_pAll->vt->SetProperties(m_pDS_Filter->m_pAll, &props, &props1);
|
|
79 m_pDS_Filter->m_pAll->vt->Commit(m_pDS_Filter->m_pAll);
|
|
80 }
|
|
81 catch (FatalError e)
|
|
82 {
|
|
83 e.PrintAll();
|
|
84 delete[] m_sVhdr;
|
|
85 delete[] m_sVhdr2;
|
|
86 delete m_pDS_Filter;
|
|
87 throw;
|
|
88 }
|
|
89 }
|
|
90
|
|
91 DS_AudioDecoder::~DS_AudioDecoder()
|
|
92 {
|
|
93 delete[] m_sVhdr;
|
|
94 delete[] m_sVhdr2;
|
|
95 delete m_pDS_Filter;
|
|
96 }
|
|
97
|
2069
|
98 int DS_AudioDecoder::Convert(const void* in_data, uint_t in_size,
|
|
99 void* out_data, uint_t out_size,
|
|
100 uint_t* size_read, uint_t* size_written)
|
1545
|
101 {
|
|
102 if (!in_data || !out_data)
|
|
103 return -1;
|
|
104
|
2069
|
105 uint_t written = 0;
|
|
106 uint_t read = 0;
|
1545
|
107 in_size -= in_size%in_fmt.nBlockAlign;
|
|
108 while (in_size>0)
|
|
109 {
|
2069
|
110 uint_t frame_size=0;
|
1545
|
111 char* frame_pointer;
|
|
112 // m_pOurOutput->SetFramePointer(out_data+written);
|
|
113 m_pDS_Filter->m_pOurOutput->SetFramePointer(&frame_pointer);
|
|
114 m_pDS_Filter->m_pOurOutput->SetFrameSizePointer((long*)&frame_size);
|
|
115 IMediaSample* sample=0;
|
|
116 m_pDS_Filter->m_pAll->vt->GetBuffer(m_pDS_Filter->m_pAll, &sample, 0, 0, 0);
|
|
117 if(!sample)
|
|
118 {
|
2069
|
119 Debug printf("DS_AudioDecoder::Convert() Error: null sample\n");
|
1545
|
120 break;
|
|
121 }
|
|
122 char* ptr;
|
|
123 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
|
124 memcpy(ptr, (const uint8_t*)in_data + read, in_fmt.nBlockAlign);
|
|
125 sample->vt->SetActualDataLength(sample, in_fmt.nBlockAlign);
|
|
126 sample->vt->SetSyncPoint(sample, true);
|
|
127 sample->vt->SetPreroll(sample, 0);
|
|
128 int result = m_pDS_Filter->m_pImp->vt->Receive(m_pDS_Filter->m_pImp, sample);
|
|
129 if (result)
|
|
130 Debug printf("DS_AudioDecoder::Convert() Error: putting data into input pin %x\n", result);
|
|
131 if ((written + frame_size) > out_size)
|
|
132 {
|
|
133 sample->vt->Release((IUnknown*)sample);
|
|
134 break;
|
|
135 }
|
|
136 memcpy((uint8_t*)out_data + written, frame_pointer, frame_size);
|
|
137 sample->vt->Release((IUnknown*)sample);
|
|
138 read+=in_fmt.nBlockAlign;
|
|
139 written+=frame_size;
|
|
140 }
|
|
141 if (size_read)
|
|
142 *size_read = read;
|
|
143 if (size_written)
|
|
144 *size_written = written;
|
|
145 return 0;
|
|
146 }
|
|
147
|
|
148 int DS_AudioDecoder::GetSrcSize(int dest_size)
|
|
149 {
|
|
150 double efficiency = (double) in_fmt.nAvgBytesPerSec
|
|
151 / (in_fmt.nSamplesPerSec*in_fmt.nBlockAlign);
|
|
152 int frames = int(dest_size*efficiency);
|
|
153 if (frames < 1)
|
|
154 frames = 1;
|
|
155 return frames * in_fmt.nBlockAlign;
|
|
156 }
|