189
|
1 /********************************************************
|
|
2
|
|
3 DirectShow Audio decoder implementation
|
|
4 Copyright 2000 Eugene Kuznetsov (divx@euro.ru)
|
|
5 Converted C++ --> C :) by A'rpi/ESP-team
|
|
6
|
|
7 *********************************************************/
|
|
8
|
|
9 //#include <config.h>
|
|
10
|
|
11 //#include "DS_AudioDecoder.h"
|
|
12 #include <string.h>
|
|
13 #include <stdlib.h>
|
|
14 #include <except.h>
|
|
15 #define __MODULE__ "DirectShow_AudioDecoder"
|
|
16
|
|
17 #include <errno.h>
|
|
18 #ifdef HAVE_MALLOC_H
|
|
19 #include <malloc.h>
|
|
20 #endif
|
|
21 //#include <loader.h>
|
|
22 //#include <wine/winbase.h>
|
|
23 #include <stdio.h>
|
|
24 #include <unistd.h>
|
|
25 #include <fcntl.h>
|
|
26 #include <strstream>
|
|
27 #include <dlfcn.h>
|
|
28 #include <sys/types.h>
|
|
29 #include <sys/mman.h>
|
|
30
|
|
31 #include <registry.h>
|
|
32 #include <wine/winreg.h>
|
|
33
|
|
34 #include "guids.h"
|
|
35 #include "interfaces.h"
|
|
36 #include "DS_Filter.h"
|
|
37
|
|
38 #include "BitmapInfo.h"
|
|
39
|
|
40 #include <string>
|
|
41 #include <default.h>
|
|
42
|
|
43 #include "DS_AudioDec.h"
|
|
44
|
|
45 const GUID FORMAT_WaveFormatEx={
|
|
46 0x05589f81, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a};
|
|
47 const GUID MEDIATYPE_Audio={
|
|
48 0x73647561, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
|
|
49 const GUID MEDIASUBTYPE_PCM={
|
|
50 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};
|
|
51
|
|
52
|
|
53 using namespace std;
|
|
54 extern "C" char* def_path;
|
|
55
|
|
56 static DS_Filter* dsf=0;
|
|
57
|
|
58 static AM_MEDIA_TYPE m_sOurType, m_sDestType;
|
|
59 // static void* m_pCust;
|
|
60 static char* m_sVhdr;
|
|
61 static char* m_sVhdr2;
|
|
62
|
|
63 static WAVEFORMATEX in_fmt;
|
|
64
|
|
65 // int m_iState=0;
|
|
66
|
|
67 extern "C" int DS_AudioDecoder_Open(char* dllname, GUID* guid, WAVEFORMATEX* wf)
|
|
68 {
|
|
69
|
|
70 m_sVhdr=new char[18+wf->cbSize];
|
|
71 memcpy(m_sVhdr, wf, 18+wf->cbSize);
|
|
72 m_sVhdr2=new char[18];
|
|
73 memcpy(m_sVhdr2, m_sVhdr, 18);
|
|
74 WAVEFORMATEX* pWF=(WAVEFORMATEX*)m_sVhdr2;
|
|
75 pWF->wFormatTag=1;
|
|
76 pWF->wBitsPerSample=16;
|
|
77 pWF->nBlockAlign=2*pWF->nChannels;
|
|
78 pWF->cbSize=0;
|
|
79 in_fmt=*wf;
|
|
80
|
|
81 memset(&m_sOurType, 0, sizeof m_sOurType);
|
|
82 m_sOurType.majortype=MEDIATYPE_Audio;
|
|
83 m_sOurType.subtype=MEDIASUBTYPE_PCM;
|
|
84 m_sOurType.subtype.f1=wf->wFormatTag;
|
|
85 m_sOurType.formattype=FORMAT_WaveFormatEx;
|
|
86 m_sOurType.lSampleSize=wf->nBlockAlign;
|
|
87 m_sOurType.bFixedSizeSamples=true;
|
|
88 m_sOurType.bTemporalCompression=false;
|
|
89 m_sOurType.pUnk=0;
|
|
90 m_sOurType.cbFormat=18+wf->cbSize;
|
|
91 m_sOurType.pbFormat=m_sVhdr;
|
|
92
|
|
93 memset(&m_sDestType, 0, sizeof m_sDestType);
|
|
94 m_sDestType.majortype=MEDIATYPE_Audio;
|
|
95 m_sDestType.subtype=MEDIASUBTYPE_PCM;
|
|
96 m_sDestType.formattype=FORMAT_WaveFormatEx;
|
|
97 m_sDestType.bFixedSizeSamples=true;
|
|
98 m_sDestType.bTemporalCompression=false;
|
|
99 m_sDestType.lSampleSize=2*wf->nChannels;
|
|
100 m_sDestType.pUnk=0;
|
|
101 m_sDestType.cbFormat=18;
|
|
102 m_sDestType.pbFormat=m_sVhdr2;
|
|
103
|
|
104 try
|
|
105 {
|
|
106
|
|
107 dsf=new DS_Filter();
|
|
108 dsf->Create(dllname, guid, &m_sOurType, &m_sDestType);
|
|
109 dsf->Start();
|
|
110
|
|
111 ALLOCATOR_PROPERTIES props, props1;
|
|
112 props.cBuffers=1;
|
|
113 props.cbBuffer=m_sOurType.lSampleSize;
|
|
114 props.cbAlign=props.cbPrefix=0;
|
|
115 dsf->m_pAll->vt->SetProperties(dsf->m_pAll, &props, &props1);
|
|
116 dsf->m_pAll->vt->Commit(dsf->m_pAll);
|
|
117 }
|
|
118 catch(FatalError e)
|
|
119 {
|
|
120 e.PrintAll();
|
|
121 delete[] m_sVhdr;
|
|
122 delete[] m_sVhdr2;
|
|
123 return 1;
|
|
124 }
|
|
125
|
|
126 return 0;
|
|
127 }
|
|
128
|
|
129 extern "C" void DS_AudioDecoder_Close(){
|
|
130 delete[] m_sVhdr;
|
|
131 delete[] m_sVhdr2;
|
|
132 }
|
|
133
|
|
134 extern "C" int DS_AudioDecoder_GetSrcSize(int dest_size)
|
|
135 {
|
|
136 double efficiency=in_fmt.nAvgBytesPerSec/double(in_fmt.nSamplesPerSec*in_fmt.nBlockAlign);
|
|
137 int frames=(int)(dest_size*efficiency);
|
|
138 if(frames<1)frames=1;
|
|
139 return frames*in_fmt.nBlockAlign;
|
|
140 }
|
|
141
|
|
142
|
|
143 extern "C" int DS_AudioDecoder_Convert(unsigned char* in_data, unsigned in_size,
|
|
144 unsigned char* out_data, unsigned out_size,
|
|
145 unsigned* size_read, unsigned* size_written)
|
|
146 {
|
|
147 if(in_data==0)return -1;
|
|
148 if(out_data==0)return -1;
|
|
149 int written=0;
|
|
150 int read=0;
|
|
151 in_size-=in_size%in_fmt.nBlockAlign;
|
|
152 while(in_size>0)
|
|
153 {
|
|
154 long frame_size=0;
|
|
155 char* frame_pointer;
|
|
156 // m_pOurOutput->SetFramePointer(out_data+written);
|
|
157 dsf->m_pOurOutput->SetFramePointer(&frame_pointer);
|
|
158 dsf->m_pOurOutput->SetFrameSizePointer(&frame_size);
|
|
159 IMediaSample* sample=0;
|
|
160 dsf->m_pAll->vt->GetBuffer(dsf->m_pAll, &sample, 0, 0, 0);
|
|
161 if(!sample)
|
|
162 {
|
|
163 cerr<<"ERROR: null sample"<<endl;
|
|
164 break;
|
|
165 }
|
|
166 char* ptr;
|
|
167 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
|
168 memcpy(ptr, in_data+read, in_fmt.nBlockAlign);
|
|
169 sample->vt->SetActualDataLength(sample, in_fmt.nBlockAlign);
|
|
170 sample->vt->SetSyncPoint(sample, true);
|
|
171 sample->vt->SetPreroll(sample, 0);
|
|
172 int result=dsf->m_pImp->vt->Receive(dsf->m_pImp, sample);
|
|
173 if(result) printf("Error putting data into input pin %x\n", result);
|
|
174 if(written+frame_size>out_size)
|
|
175 {
|
|
176 sample->vt->Release((IUnknown*)sample);
|
|
177 break;
|
|
178 }
|
|
179 memcpy(out_data+written, frame_pointer, frame_size);
|
|
180 sample->vt->Release((IUnknown*)sample);
|
|
181 read+=in_fmt.nBlockAlign;
|
|
182 written+=frame_size;
|
|
183 }
|
|
184 if(size_read) *size_read=read;
|
|
185 if(size_written) *size_written=written;
|
|
186 return 0;
|
|
187 }
|
|
188
|