1545
|
1 /********************************************************
|
|
2
|
|
3 DirectShow audio decoder
|
|
4 Copyright 2001 Eugene Kuznetsov (divx@euro.ru)
|
|
5
|
|
6 *********************************************************/
|
|
7
|
3946
|
8 #ifndef NOAVIFILE_HEADERS
|
|
9 #include "audiodecoder.h"
|
|
10 #include "except.h"
|
|
11 #else
|
|
12 #include "libwin32.h"
|
|
13 #endif
|
|
14
|
|
15 #include "DS_Filter.h"
|
|
16
|
|
17 struct _DS_AudioDecoder
|
|
18 {
|
|
19 WAVEFORMATEX in_fmt;
|
|
20 AM_MEDIA_TYPE m_sOurType, m_sDestType;
|
|
21 DS_Filter* m_pDS_Filter;
|
|
22 char* m_sVhdr;
|
|
23 char* m_sVhdr2;
|
|
24 };
|
|
25
|
2069
|
26 #include "DS_AudioDecoder.h"
|
3946
|
27
|
2069
|
28 #include <string.h>
|
|
29 #include <stdio.h>
|
3059
|
30 #include <stdlib.h>
|
2069
|
31
|
1545
|
32 #define __MODULE__ "DirectShow audio decoder"
|
|
33 const GUID FORMAT_WaveFormatEx = {
|
|
34 0x05589f81, 0xc356, 0x11CE,
|
|
35 { 0xBF, 0x01, 0x00, 0xAA, 0x00, 0x55, 0x59, 0x5A }
|
|
36 };
|
|
37 const GUID MEDIATYPE_Audio = {
|
|
38 0x73647561, 0x0000, 0x0010,
|
|
39 { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
|
|
40 };
|
|
41 const GUID MEDIASUBTYPE_PCM = {
|
|
42 0x00000001, 0x0000, 0x0010,
|
|
43 { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
|
|
44 };
|
|
45
|
|
46 typedef long STDCALL (*GETCLASS) (GUID*, GUID*, void**);
|
|
47
|
3444
|
48 DS_AudioDecoder * DS_AudioDecoder_Open(char* dllname, GUID* guid, WAVEFORMATEX* wf)
|
|
49 //DS_AudioDecoder * DS_AudioDecoder_Create(const CodecInfo * info, const WAVEFORMATEX* wf)
|
1545
|
50 {
|
3059
|
51 DS_AudioDecoder *this;
|
|
52 int sz;
|
|
53 WAVEFORMATEX* pWF;
|
3444
|
54
|
|
55 Setup_LDT_Keeper();
|
|
56 Setup_FS_Segment();
|
3059
|
57
|
|
58 this = malloc(sizeof(DS_AudioDecoder));
|
|
59
|
|
60 sz = 18 + wf->cbSize;
|
|
61 this->m_sVhdr = malloc(sz);
|
|
62 memcpy(this->m_sVhdr, wf, sz);
|
3915
|
63 this->m_sVhdr2 = malloc(18);
|
|
64 memcpy(this->m_sVhdr2, this->m_sVhdr, 18);
|
3059
|
65
|
|
66 pWF = (WAVEFORMATEX*)this->m_sVhdr2;
|
|
67 pWF->wFormatTag = 1;
|
|
68 pWF->wBitsPerSample = 16;
|
3915
|
69 pWF->nBlockAlign = pWF->nChannels * (pWF->wBitsPerSample + 7) / 8;
|
3059
|
70 pWF->cbSize = 0;
|
3915
|
71 pWF->nAvgBytesPerSec = pWF->nBlockAlign * pWF->nSamplesPerSec;
|
3059
|
72
|
|
73 memcpy(&this->in_fmt,wf,sizeof(WAVEFORMATEX));
|
1545
|
74
|
3059
|
75 memset(&this->m_sOurType, 0, sizeof(this->m_sOurType));
|
|
76 this->m_sOurType.majortype=MEDIATYPE_Audio;
|
|
77 this->m_sOurType.subtype=MEDIASUBTYPE_PCM;
|
|
78 this->m_sOurType.subtype.f1=wf->wFormatTag;
|
|
79 this->m_sOurType.formattype=FORMAT_WaveFormatEx;
|
|
80 this->m_sOurType.lSampleSize=wf->nBlockAlign;
|
|
81 this->m_sOurType.bFixedSizeSamples=1;
|
|
82 this->m_sOurType.bTemporalCompression=0;
|
|
83 this->m_sOurType.pUnk=0;
|
|
84 this->m_sOurType.cbFormat=sz;
|
|
85 this->m_sOurType.pbFormat=this->m_sVhdr;
|
1545
|
86
|
3059
|
87 memset(&this->m_sDestType, 0, sizeof(this->m_sDestType));
|
|
88 this->m_sDestType.majortype=MEDIATYPE_Audio;
|
|
89 this->m_sDestType.subtype=MEDIASUBTYPE_PCM;
|
3915
|
90 // this->m_sDestType.subtype.f1=pWF->wFormatTag;
|
3059
|
91 this->m_sDestType.formattype=FORMAT_WaveFormatEx;
|
|
92 this->m_sDestType.bFixedSizeSamples=1;
|
|
93 this->m_sDestType.bTemporalCompression=0;
|
3915
|
94 this->m_sDestType.lSampleSize=pWF->nBlockAlign;
|
3466
|
95 if (wf->wFormatTag == 0x130)
|
|
96 // ACEL hack to prevent memory corruption
|
|
97 // obviosly we are missing something here
|
|
98 this->m_sDestType.lSampleSize *= 288;
|
3059
|
99 this->m_sDestType.pUnk=0;
|
3915
|
100 this->m_sDestType.cbFormat=18; //pWF->cbSize;
|
3059
|
101 this->m_sDestType.pbFormat=this->m_sVhdr2;
|
1545
|
102
|
3915
|
103 print_wave_header(this->m_sVhdr);
|
|
104 print_wave_header(this->m_sVhdr2);
|
|
105
|
3059
|
106 /*try*/
|
1545
|
107 {
|
3059
|
108 ALLOCATOR_PROPERTIES props, props1;
|
3444
|
109 this->m_pDS_Filter = DS_FilterCreate(dllname, guid, &this->m_sOurType, &this->m_sDestType);
|
3059
|
110 if( !this->m_pDS_Filter ) {
|
|
111 free(this);
|
|
112 return NULL;
|
|
113 }
|
|
114
|
3063
|
115 this->m_pDS_Filter->Start(this->m_pDS_Filter);
|
1545
|
116
|
|
117 props.cBuffers=1;
|
3059
|
118 props.cbBuffer=this->m_sOurType.lSampleSize;
|
1545
|
119 props.cbAlign=props.cbPrefix=0;
|
3059
|
120 this->m_pDS_Filter->m_pAll->vt->SetProperties(this->m_pDS_Filter->m_pAll, &props, &props1);
|
|
121 this->m_pDS_Filter->m_pAll->vt->Commit(this->m_pDS_Filter->m_pAll);
|
1545
|
122 }
|
3059
|
123 /*
|
|
124 catch (FatalError& e)
|
1545
|
125 {
|
|
126 e.PrintAll();
|
|
127 delete[] m_sVhdr;
|
|
128 delete[] m_sVhdr2;
|
|
129 delete m_pDS_Filter;
|
|
130 throw;
|
|
131 }
|
3059
|
132 */
|
|
133 return this;
|
1545
|
134 }
|
|
135
|
3059
|
136 void DS_AudioDecoder_Destroy(DS_AudioDecoder *this)
|
1545
|
137 {
|
3059
|
138 free(this->m_sVhdr);
|
|
139 free(this->m_sVhdr2);
|
|
140 DS_Filter_Destroy(this->m_pDS_Filter);
|
|
141 free(this);
|
1545
|
142 }
|
|
143
|
3946
|
144 int DS_AudioDecoder_Convert(DS_AudioDecoder *this, const void* in_data, unsigned int in_size,
|
|
145 void* out_data, unsigned int out_size,
|
|
146 unsigned int* size_read, unsigned int* size_written)
|
1545
|
147 {
|
3946
|
148 unsigned int written = 0;
|
|
149 unsigned int read = 0;
|
3059
|
150
|
1545
|
151 if (!in_data || !out_data)
|
|
152 return -1;
|
|
153
|
3444
|
154 Setup_FS_Segment();
|
|
155
|
3059
|
156 in_size -= in_size%this->in_fmt.nBlockAlign;
|
1545
|
157 while (in_size>0)
|
|
158 {
|
3946
|
159 unsigned int frame_size = 0;
|
1545
|
160 char* frame_pointer;
|
|
161 IMediaSample* sample=0;
|
3059
|
162 char* ptr;
|
|
163 int result;
|
|
164
|
|
165 // this->m_pOurOutput->SetFramePointer(out_data+written);
|
3063
|
166 this->m_pDS_Filter->m_pOurOutput->SetFramePointer(this->m_pDS_Filter->m_pOurOutput,&frame_pointer);
|
|
167 this->m_pDS_Filter->m_pOurOutput->SetFrameSizePointer(this->m_pDS_Filter->m_pOurOutput,(long*)&frame_size);
|
3059
|
168 this->m_pDS_Filter->m_pAll->vt->GetBuffer(this->m_pDS_Filter->m_pAll, &sample, 0, 0, 0);
|
|
169 if (!sample)
|
1545
|
170 {
|
2069
|
171 Debug printf("DS_AudioDecoder::Convert() Error: null sample\n");
|
1545
|
172 break;
|
|
173 }
|
3466
|
174 sample->vt->SetActualDataLength(sample, this->in_fmt.nBlockAlign);
|
1545
|
175 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
3059
|
176 memcpy(ptr, (const uint8_t*)in_data + read, this->in_fmt.nBlockAlign);
|
|
177 sample->vt->SetSyncPoint(sample, 1);
|
1545
|
178 sample->vt->SetPreroll(sample, 0);
|
3059
|
179 result = this->m_pDS_Filter->m_pImp->vt->Receive(this->m_pDS_Filter->m_pImp, sample);
|
1545
|
180 if (result)
|
|
181 Debug printf("DS_AudioDecoder::Convert() Error: putting data into input pin %x\n", result);
|
|
182 if ((written + frame_size) > out_size)
|
|
183 {
|
|
184 sample->vt->Release((IUnknown*)sample);
|
|
185 break;
|
|
186 }
|
|
187 memcpy((uint8_t*)out_data + written, frame_pointer, frame_size);
|
|
188 sample->vt->Release((IUnknown*)sample);
|
3059
|
189 read+=this->in_fmt.nBlockAlign;
|
1545
|
190 written+=frame_size;
|
3466
|
191 break;
|
1545
|
192 }
|
|
193 if (size_read)
|
|
194 *size_read = read;
|
|
195 if (size_written)
|
|
196 *size_written = written;
|
|
197 return 0;
|
|
198 }
|
|
199
|
3059
|
200 int DS_AudioDecoder_GetSrcSize(DS_AudioDecoder *this, int dest_size)
|
1545
|
201 {
|
3059
|
202 double efficiency =(double) this->in_fmt.nAvgBytesPerSec
|
|
203 / (this->in_fmt.nSamplesPerSec*this->in_fmt.nBlockAlign);
|
|
204 int frames = (int)(dest_size*efficiency);;
|
|
205
|
1545
|
206 if (frames < 1)
|
|
207 frames = 1;
|
3059
|
208 return frames * this->in_fmt.nBlockAlign;
|
1545
|
209 }
|