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
|
3059
|
29 DS_AudioDecoder * DS_AudioDecoder_Create(const CodecInfo * info, const WAVEFORMATEX* wf)
|
1545
|
30 {
|
3059
|
31 DS_AudioDecoder *this;
|
|
32 int sz;
|
|
33 WAVEFORMATEX* pWF;
|
|
34
|
|
35 this = malloc(sizeof(DS_AudioDecoder));
|
|
36
|
|
37 sz = 18 + wf->cbSize;
|
|
38 this->m_sVhdr = malloc(sz);
|
|
39 memcpy(this->m_sVhdr, wf, sz);
|
|
40 this->m_sVhdr2 = malloc(sz);
|
|
41 memcpy(this->m_sVhdr2, this->m_sVhdr, sz);
|
|
42
|
|
43 pWF = (WAVEFORMATEX*)this->m_sVhdr2;
|
|
44 pWF->wFormatTag = 1;
|
|
45 pWF->wBitsPerSample = 16;
|
|
46 pWF->nBlockAlign = 2*pWF->nChannels;
|
|
47 pWF->cbSize = 0;
|
|
48
|
|
49 memcpy(&this->in_fmt,wf,sizeof(WAVEFORMATEX));
|
1545
|
50
|
3059
|
51 memset(&this->m_sOurType, 0, sizeof(this->m_sOurType));
|
|
52 this->m_sOurType.majortype=MEDIATYPE_Audio;
|
|
53 this->m_sOurType.subtype=MEDIASUBTYPE_PCM;
|
|
54 this->m_sOurType.subtype.f1=wf->wFormatTag;
|
|
55 this->m_sOurType.formattype=FORMAT_WaveFormatEx;
|
|
56 this->m_sOurType.lSampleSize=wf->nBlockAlign;
|
|
57 this->m_sOurType.bFixedSizeSamples=1;
|
|
58 this->m_sOurType.bTemporalCompression=0;
|
|
59 this->m_sOurType.pUnk=0;
|
|
60 this->m_sOurType.cbFormat=sz;
|
|
61 this->m_sOurType.pbFormat=this->m_sVhdr;
|
1545
|
62
|
3059
|
63 memset(&this->m_sDestType, 0, sizeof(this->m_sDestType));
|
|
64 this->m_sDestType.majortype=MEDIATYPE_Audio;
|
|
65 this->m_sDestType.subtype=MEDIASUBTYPE_PCM;
|
|
66 this->m_sDestType.subtype.f1=pWF->wFormatTag;
|
|
67 this->m_sDestType.formattype=FORMAT_WaveFormatEx;
|
|
68 this->m_sDestType.bFixedSizeSamples=1;
|
|
69 this->m_sDestType.bTemporalCompression=0;
|
|
70 this->m_sDestType.lSampleSize=2*wf->nChannels;
|
|
71 this->m_sDestType.pUnk=0;
|
|
72 this->m_sDestType.cbFormat=pWF->cbSize;
|
|
73 this->m_sDestType.pbFormat=this->m_sVhdr2;
|
1545
|
74
|
3059
|
75 /*try*/
|
1545
|
76 {
|
3059
|
77 ALLOCATOR_PROPERTIES props, props1;
|
3063
|
78 this->m_pDS_Filter = DS_FilterCreate((const char*)info->dll, info->guid, &this->m_sOurType, &this->m_sDestType);
|
3059
|
79 if( !this->m_pDS_Filter ) {
|
|
80 free(this);
|
|
81 return NULL;
|
|
82 }
|
|
83
|
3063
|
84 this->m_pDS_Filter->Start(this->m_pDS_Filter);
|
1545
|
85
|
|
86 props.cBuffers=1;
|
3059
|
87 props.cbBuffer=this->m_sOurType.lSampleSize;
|
1545
|
88 props.cbAlign=props.cbPrefix=0;
|
3059
|
89 this->m_pDS_Filter->m_pAll->vt->SetProperties(this->m_pDS_Filter->m_pAll, &props, &props1);
|
|
90 this->m_pDS_Filter->m_pAll->vt->Commit(this->m_pDS_Filter->m_pAll);
|
1545
|
91 }
|
3059
|
92 /*
|
|
93 catch (FatalError& e)
|
1545
|
94 {
|
|
95 e.PrintAll();
|
|
96 delete[] m_sVhdr;
|
|
97 delete[] m_sVhdr2;
|
|
98 delete m_pDS_Filter;
|
|
99 throw;
|
|
100 }
|
3059
|
101 */
|
|
102 return this;
|
1545
|
103 }
|
|
104
|
3059
|
105 void DS_AudioDecoder_Destroy(DS_AudioDecoder *this)
|
1545
|
106 {
|
3059
|
107 free(this->m_sVhdr);
|
|
108 free(this->m_sVhdr2);
|
|
109 DS_Filter_Destroy(this->m_pDS_Filter);
|
|
110 free(this);
|
1545
|
111 }
|
|
112
|
3059
|
113 int DS_AudioDecoder_Convert(DS_AudioDecoder *this, const void* in_data, uint_t in_size,
|
|
114 void* out_data, uint_t out_size,
|
|
115 uint_t* size_read, uint_t* size_written)
|
1545
|
116 {
|
3059
|
117 uint_t written = 0;
|
|
118 uint_t read = 0;
|
|
119
|
1545
|
120 if (!in_data || !out_data)
|
|
121 return -1;
|
|
122
|
3059
|
123 in_size -= in_size%this->in_fmt.nBlockAlign;
|
1545
|
124 while (in_size>0)
|
|
125 {
|
3059
|
126 uint_t frame_size = 0;
|
1545
|
127 char* frame_pointer;
|
|
128 IMediaSample* sample=0;
|
3059
|
129 char* ptr;
|
|
130 int result;
|
|
131
|
|
132 // this->m_pOurOutput->SetFramePointer(out_data+written);
|
3063
|
133 this->m_pDS_Filter->m_pOurOutput->SetFramePointer(this->m_pDS_Filter->m_pOurOutput,&frame_pointer);
|
|
134 this->m_pDS_Filter->m_pOurOutput->SetFrameSizePointer(this->m_pDS_Filter->m_pOurOutput,(long*)&frame_size);
|
3059
|
135 this->m_pDS_Filter->m_pAll->vt->GetBuffer(this->m_pDS_Filter->m_pAll, &sample, 0, 0, 0);
|
|
136 if (!sample)
|
1545
|
137 {
|
2069
|
138 Debug printf("DS_AudioDecoder::Convert() Error: null sample\n");
|
1545
|
139 break;
|
|
140 }
|
|
141 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
3059
|
142 memcpy(ptr, (const uint8_t*)in_data + read, this->in_fmt.nBlockAlign);
|
|
143 sample->vt->SetActualDataLength(sample, this->in_fmt.nBlockAlign);
|
|
144 sample->vt->SetSyncPoint(sample, 1);
|
1545
|
145 sample->vt->SetPreroll(sample, 0);
|
3059
|
146 result = this->m_pDS_Filter->m_pImp->vt->Receive(this->m_pDS_Filter->m_pImp, sample);
|
1545
|
147 if (result)
|
|
148 Debug printf("DS_AudioDecoder::Convert() Error: putting data into input pin %x\n", result);
|
|
149 if ((written + frame_size) > out_size)
|
|
150 {
|
|
151 sample->vt->Release((IUnknown*)sample);
|
|
152 break;
|
|
153 }
|
|
154 memcpy((uint8_t*)out_data + written, frame_pointer, frame_size);
|
|
155 sample->vt->Release((IUnknown*)sample);
|
3059
|
156 read+=this->in_fmt.nBlockAlign;
|
1545
|
157 written+=frame_size;
|
|
158 }
|
|
159 if (size_read)
|
|
160 *size_read = read;
|
|
161 if (size_written)
|
|
162 *size_written = written;
|
|
163 return 0;
|
|
164 }
|
|
165
|
3059
|
166 int DS_AudioDecoder_GetSrcSize(DS_AudioDecoder *this, int dest_size)
|
1545
|
167 {
|
3059
|
168 double efficiency =(double) this->in_fmt.nAvgBytesPerSec
|
|
169 / (this->in_fmt.nSamplesPerSec*this->in_fmt.nBlockAlign);
|
|
170 int frames = (int)(dest_size*efficiency);;
|
|
171
|
1545
|
172 if (frames < 1)
|
|
173 frames = 1;
|
3059
|
174 return frames * this->in_fmt.nBlockAlign;
|
1545
|
175 }
|