8325
|
1 /********************************************************
|
|
2
|
|
3 DirectShow audio decoder
|
|
4 Copyright 2001 Eugene Kuznetsov (divx@euro.ru)
|
|
5
|
|
6 *********************************************************/
|
|
7
|
|
8 #ifndef NOAVIFILE_HEADERS
|
|
9 #include "audiodecoder.h"
|
|
10 #include "except.h"
|
|
11 #else
|
|
12 #include "libwin32.h"
|
8451
|
13 #include "ldt_keeper.h"
|
8325
|
14 #endif
|
|
15
|
|
16 #include "DMO_Filter.h"
|
|
17 #include "DMO_AudioDecoder.h"
|
|
18
|
|
19 struct _DMO_AudioDecoder
|
|
20 {
|
|
21 DMO_MEDIA_TYPE m_sOurType, m_sDestType;
|
|
22 DMO_Filter* m_pDMO_Filter;
|
|
23 char* m_sVhdr;
|
|
24 char* m_sVhdr2;
|
|
25 int m_iFlushed;
|
|
26 };
|
|
27
|
|
28 #include "DMO_AudioDecoder.h"
|
|
29
|
|
30 #include <string.h>
|
|
31 #include <stdio.h>
|
|
32 #include <stdlib.h>
|
|
33
|
|
34 #define __MODULE__ "DirectShow audio decoder"
|
|
35
|
|
36 typedef long STDCALL (*GETCLASS) (GUID*, GUID*, void**);
|
8451
|
37 extern void print_wave_header(WAVEFORMATEX *h);
|
8325
|
38
|
|
39 DMO_AudioDecoder * DMO_AudioDecoder_Open(char* dllname, GUID* guid, WAVEFORMATEX* wf,int out_channels)
|
|
40 //DMO_AudioDecoder * DMO_AudioDecoder_Create(const CodecInfo * info, const WAVEFORMATEX* wf)
|
|
41 {
|
|
42 DMO_AudioDecoder *this;
|
|
43 int sz;
|
|
44 WAVEFORMATEX* pWF;
|
|
45
|
|
46 Setup_LDT_Keeper();
|
|
47 Setup_FS_Segment();
|
|
48
|
|
49 this = malloc(sizeof(DMO_AudioDecoder));
|
|
50
|
|
51 this->m_iFlushed=1;
|
|
52
|
|
53 sz = 18 + wf->cbSize;
|
|
54 this->m_sVhdr = malloc(sz);
|
|
55 memcpy(this->m_sVhdr, wf, sz);
|
|
56 this->m_sVhdr2 = malloc(18);
|
|
57 memcpy(this->m_sVhdr2, this->m_sVhdr, 18);
|
|
58
|
|
59 pWF = (WAVEFORMATEX*)this->m_sVhdr2;
|
|
60 pWF->wFormatTag = 1;
|
|
61 pWF->wBitsPerSample = 16;
|
|
62 pWF->nChannels = out_channels;
|
|
63 pWF->nBlockAlign = 2*pWF->nChannels; //pWF->nChannels * (pWF->wBitsPerSample + 7) / 8;
|
|
64 pWF->nAvgBytesPerSec = pWF->nBlockAlign * pWF->nSamplesPerSec;
|
|
65 pWF->cbSize = 0;
|
|
66
|
|
67 memset(&this->m_sOurType, 0, sizeof(this->m_sOurType));
|
|
68 this->m_sOurType.majortype=MEDIATYPE_Audio;
|
|
69 this->m_sOurType.subtype=MEDIASUBTYPE_PCM;
|
|
70 this->m_sOurType.subtype.f1=wf->wFormatTag;
|
|
71 this->m_sOurType.formattype=FORMAT_WaveFormatEx;
|
|
72 this->m_sOurType.lSampleSize=wf->nBlockAlign;
|
|
73 this->m_sOurType.bFixedSizeSamples=1;
|
|
74 this->m_sOurType.bTemporalCompression=0;
|
|
75 this->m_sOurType.cbFormat=sz;
|
|
76 this->m_sOurType.pbFormat=this->m_sVhdr;
|
|
77
|
|
78 memset(&this->m_sDestType, 0, sizeof(this->m_sDestType));
|
|
79 this->m_sDestType.majortype=MEDIATYPE_Audio;
|
|
80 this->m_sDestType.subtype=MEDIASUBTYPE_PCM;
|
|
81 this->m_sDestType.formattype=FORMAT_WaveFormatEx;
|
|
82 this->m_sDestType.bFixedSizeSamples=1;
|
|
83 this->m_sDestType.bTemporalCompression=0;
|
|
84 this->m_sDestType.lSampleSize=pWF->nBlockAlign;
|
|
85 this->m_sDestType.cbFormat=18; //pWF->cbSize;
|
|
86 this->m_sDestType.pbFormat=this->m_sVhdr2;
|
|
87
|
8451
|
88 print_wave_header((WAVEFORMATEX *)this->m_sVhdr);
|
|
89 print_wave_header((WAVEFORMATEX *)this->m_sVhdr2);
|
8325
|
90
|
|
91 this->m_pDMO_Filter = DMO_FilterCreate(dllname, guid, &this->m_sOurType, &this->m_sDestType);
|
|
92 if( !this->m_pDMO_Filter ) {
|
|
93 free(this);
|
|
94 return NULL;
|
|
95 }
|
|
96
|
|
97 return this;
|
|
98 }
|
|
99
|
|
100 void DMO_AudioDecoder_Destroy(DMO_AudioDecoder *this)
|
|
101 {
|
|
102 free(this->m_sVhdr);
|
|
103 free(this->m_sVhdr2);
|
|
104 DMO_Filter_Destroy(this->m_pDMO_Filter);
|
|
105 free(this);
|
|
106 }
|
|
107
|
|
108 int DMO_AudioDecoder_Convert(DMO_AudioDecoder *this, const void* in_data, unsigned int in_size,
|
|
109 void* out_data, unsigned int out_size,
|
|
110 unsigned int* size_read, unsigned int* size_written)
|
|
111 {
|
|
112 DMO_OUTPUT_DATA_BUFFER db;
|
|
113 CMediaBuffer* bufferin;
|
8451
|
114 unsigned long written = 0;
|
|
115 unsigned long read = 0;
|
8325
|
116 int r = 0;
|
|
117
|
|
118 if (!in_data || !out_data)
|
|
119 return -1;
|
|
120
|
|
121 Setup_FS_Segment();
|
|
122
|
|
123 //m_pDMO_Filter->m_pMedia->vt->Lock(m_pDMO_Filter->m_pMedia, 1);
|
|
124 bufferin = CMediaBufferCreate(in_size, (void*)in_data, in_size, 1);
|
|
125 r = this->m_pDMO_Filter->m_pMedia->vt->ProcessInput(this->m_pDMO_Filter->m_pMedia, 0,
|
|
126 (IMediaBuffer*)bufferin,
|
|
127 (this->m_iFlushed) ? DMO_INPUT_DATA_BUFFERF_SYNCPOINT : 0,
|
|
128 0, 0);
|
|
129 if (r == 0){
|
|
130 ((IMediaBuffer*)bufferin)->vt->GetBufferAndLength((IMediaBuffer*)bufferin, 0, &read);
|
|
131 this->m_iFlushed = 0;
|
|
132 }
|
|
133
|
|
134 ((IMediaBuffer*)bufferin)->vt->Release((IUnknown*)bufferin);
|
|
135
|
|
136 //printf("RESULTA: %d 0x%x %ld %d %d\n", r, r, read, m_iFlushed, out_size);
|
|
137 if (r == 0 || (unsigned)r == DMO_E_NOTACCEPTING){
|
|
138 unsigned long status = 0;
|
|
139 /* something for process */
|
|
140 db.rtTimestamp = 0;
|
|
141 db.rtTimelength = 0;
|
|
142 db.dwStatus = 0;
|
|
143 db.pBuffer = (IMediaBuffer*) CMediaBufferCreate(out_size, out_data, 0, 0);
|
|
144 //printf("OUTSIZE %d\n", out_size);
|
|
145 r = this->m_pDMO_Filter->m_pMedia->vt->ProcessOutput(this->m_pDMO_Filter->m_pMedia,
|
|
146 0, 1, &db, &status);
|
|
147
|
|
148 ((IMediaBuffer*)db.pBuffer)->vt->GetBufferAndLength((IMediaBuffer*)db.pBuffer, 0, &written);
|
|
149 ((IMediaBuffer*)db.pBuffer)->vt->Release((IUnknown*)db.pBuffer);
|
|
150
|
|
151 //printf("RESULTB: %d 0x%x %ld\n", r, r, written);
|
|
152 //printf("Converted %d -> %d\n", in_size, out_size);
|
|
153 }
|
|
154 else if (in_size > 0)
|
|
155 printf("ProcessInputError r:0x%x=%d\n", r, r);
|
|
156
|
|
157 if (size_read)
|
|
158 *size_read = read;
|
|
159 if (size_written)
|
|
160 *size_written = written;
|
|
161 return r;
|
|
162 }
|
|
163
|
|
164 int DMO_AudioDecoder_GetSrcSize(DMO_AudioDecoder *this, int dest_size)
|
|
165 {
|
|
166 // unsigned long inputs, outputs;
|
|
167 // Setup_FS_Segment();
|
|
168 // this->m_pDMO_Filter->m_pMedia->vt->GetOutputSizeInfo(this->m_pDMO_Filter->m_pMedia, 0, &inputs, &outputs);
|
|
169 return ((WAVEFORMATEX*)this->m_sVhdr)->nBlockAlign*4;
|
|
170 }
|