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;
|
|
75 this->m_sDestType.pUnk=0;
|
|
76 this->m_sDestType.cbFormat=pWF->cbSize;
|
|
77 this->m_sDestType.pbFormat=this->m_sVhdr2;
|
1545
|
78
|
3059
|
79 /*try*/
|
1545
|
80 {
|
3059
|
81 ALLOCATOR_PROPERTIES props, props1;
|
3444
|
82 this->m_pDS_Filter = DS_FilterCreate(dllname, guid, &this->m_sOurType, &this->m_sDestType);
|
3059
|
83 if( !this->m_pDS_Filter ) {
|
|
84 free(this);
|
|
85 return NULL;
|
|
86 }
|
|
87
|
3063
|
88 this->m_pDS_Filter->Start(this->m_pDS_Filter);
|
1545
|
89
|
|
90 props.cBuffers=1;
|
3059
|
91 props.cbBuffer=this->m_sOurType.lSampleSize;
|
1545
|
92 props.cbAlign=props.cbPrefix=0;
|
3059
|
93 this->m_pDS_Filter->m_pAll->vt->SetProperties(this->m_pDS_Filter->m_pAll, &props, &props1);
|
|
94 this->m_pDS_Filter->m_pAll->vt->Commit(this->m_pDS_Filter->m_pAll);
|
1545
|
95 }
|
3059
|
96 /*
|
|
97 catch (FatalError& e)
|
1545
|
98 {
|
|
99 e.PrintAll();
|
|
100 delete[] m_sVhdr;
|
|
101 delete[] m_sVhdr2;
|
|
102 delete m_pDS_Filter;
|
|
103 throw;
|
|
104 }
|
3059
|
105 */
|
|
106 return this;
|
1545
|
107 }
|
|
108
|
3059
|
109 void DS_AudioDecoder_Destroy(DS_AudioDecoder *this)
|
1545
|
110 {
|
3059
|
111 free(this->m_sVhdr);
|
|
112 free(this->m_sVhdr2);
|
|
113 DS_Filter_Destroy(this->m_pDS_Filter);
|
|
114 free(this);
|
1545
|
115 }
|
|
116
|
3059
|
117 int DS_AudioDecoder_Convert(DS_AudioDecoder *this, const void* in_data, uint_t in_size,
|
|
118 void* out_data, uint_t out_size,
|
|
119 uint_t* size_read, uint_t* size_written)
|
1545
|
120 {
|
3059
|
121 uint_t written = 0;
|
|
122 uint_t read = 0;
|
|
123
|
1545
|
124 if (!in_data || !out_data)
|
|
125 return -1;
|
|
126
|
3444
|
127 Setup_FS_Segment();
|
|
128
|
3059
|
129 in_size -= in_size%this->in_fmt.nBlockAlign;
|
1545
|
130 while (in_size>0)
|
|
131 {
|
3059
|
132 uint_t frame_size = 0;
|
1545
|
133 char* frame_pointer;
|
|
134 IMediaSample* sample=0;
|
3059
|
135 char* ptr;
|
|
136 int result;
|
|
137
|
|
138 // this->m_pOurOutput->SetFramePointer(out_data+written);
|
3063
|
139 this->m_pDS_Filter->m_pOurOutput->SetFramePointer(this->m_pDS_Filter->m_pOurOutput,&frame_pointer);
|
|
140 this->m_pDS_Filter->m_pOurOutput->SetFrameSizePointer(this->m_pDS_Filter->m_pOurOutput,(long*)&frame_size);
|
3059
|
141 this->m_pDS_Filter->m_pAll->vt->GetBuffer(this->m_pDS_Filter->m_pAll, &sample, 0, 0, 0);
|
|
142 if (!sample)
|
1545
|
143 {
|
2069
|
144 Debug printf("DS_AudioDecoder::Convert() Error: null sample\n");
|
1545
|
145 break;
|
|
146 }
|
|
147 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
3059
|
148 memcpy(ptr, (const uint8_t*)in_data + read, this->in_fmt.nBlockAlign);
|
|
149 sample->vt->SetActualDataLength(sample, this->in_fmt.nBlockAlign);
|
|
150 sample->vt->SetSyncPoint(sample, 1);
|
1545
|
151 sample->vt->SetPreroll(sample, 0);
|
3059
|
152 result = this->m_pDS_Filter->m_pImp->vt->Receive(this->m_pDS_Filter->m_pImp, sample);
|
1545
|
153 if (result)
|
|
154 Debug printf("DS_AudioDecoder::Convert() Error: putting data into input pin %x\n", result);
|
|
155 if ((written + frame_size) > out_size)
|
|
156 {
|
|
157 sample->vt->Release((IUnknown*)sample);
|
|
158 break;
|
|
159 }
|
|
160 memcpy((uint8_t*)out_data + written, frame_pointer, frame_size);
|
|
161 sample->vt->Release((IUnknown*)sample);
|
3059
|
162 read+=this->in_fmt.nBlockAlign;
|
1545
|
163 written+=frame_size;
|
|
164 }
|
|
165 if (size_read)
|
|
166 *size_read = read;
|
|
167 if (size_written)
|
|
168 *size_written = written;
|
|
169 return 0;
|
|
170 }
|
|
171
|
3059
|
172 int DS_AudioDecoder_GetSrcSize(DS_AudioDecoder *this, int dest_size)
|
1545
|
173 {
|
3059
|
174 double efficiency =(double) this->in_fmt.nAvgBytesPerSec
|
|
175 / (this->in_fmt.nSamplesPerSec*this->in_fmt.nBlockAlign);
|
|
176 int frames = (int)(dest_size*efficiency);;
|
|
177
|
1545
|
178 if (frames < 1)
|
|
179 frames = 1;
|
3059
|
180 return frames * this->in_fmt.nBlockAlign;
|
1545
|
181 }
|