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