annotate loader/dshow/DS_AudioDecoder.c @ 2624:64844fccf623

partly upgraded to mp_msg and fixed minor bug in parse_command_line
author alex
date Fri, 02 Nov 2001 02:55:51 +0000
parents 4b3f3f850896
children 24fa494bedb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
1 /********************************************************
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
2
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
3 DirectShow audio decoder
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
4 Copyright 2001 Eugene Kuznetsov (divx@euro.ru)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
5
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
6 *********************************************************/
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
7
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
8 #include "DS_AudioDecoder.h"
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
9 #include <string.h>
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
10 #include <stdio.h>
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
11
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
12 // using namespace std;
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
13
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
14 #define __MODULE__ "DirectShow audio decoder"
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
15 const GUID FORMAT_WaveFormatEx = {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
16 0x05589f81, 0xc356, 0x11CE,
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
17 { 0xBF, 0x01, 0x00, 0xAA, 0x00, 0x55, 0x59, 0x5A }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
18 };
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
19 const GUID MEDIATYPE_Audio = {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
20 0x73647561, 0x0000, 0x0010,
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
21 { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
22 };
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
23 const GUID MEDIASUBTYPE_PCM = {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
24 0x00000001, 0x0000, 0x0010,
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
25 { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
26 };
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
27
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
28 typedef long STDCALL (*GETCLASS) (GUID*, GUID*, void**);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
29
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
30 DS_AudioDecoder::DS_AudioDecoder(const CodecInfo& info, const WAVEFORMATEX* wf)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
31 : IAudioDecoder(info, wf), m_pDS_Filter(0), m_sVhdr(0), m_sVhdr2(0)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
32 {
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
33 int sz = 18 + wf->cbSize;
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
34 m_sVhdr=new char[sz];
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
35 memcpy(m_sVhdr, wf, sz);
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
36 m_sVhdr2=new char[sz];
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
37 memcpy(m_sVhdr2, m_sVhdr, sz);
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
38 WAVEFORMATEX* pWF=(WAVEFORMATEX*)m_sVhdr2;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
39 pWF->wFormatTag=1;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
40 pWF->wBitsPerSample=16;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
41 pWF->nBlockAlign=2*pWF->nChannels;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
42 pWF->cbSize=0;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
43 in_fmt=*wf;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
44
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
45 memset(&m_sOurType, 0, sizeof(m_sOurType));
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
46 m_sOurType.majortype=MEDIATYPE_Audio;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
47 m_sOurType.subtype=MEDIASUBTYPE_PCM;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
48 m_sOurType.subtype.f1=wf->wFormatTag;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
49 m_sOurType.formattype=FORMAT_WaveFormatEx;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
50 m_sOurType.lSampleSize=wf->nBlockAlign;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
51 m_sOurType.bFixedSizeSamples=true;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
52 m_sOurType.bTemporalCompression=false;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
53 m_sOurType.pUnk=0;
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
54 m_sOurType.cbFormat=sz;
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
55 m_sOurType.pbFormat=m_sVhdr;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
56
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
57 memset(&m_sDestType, 0, sizeof(m_sDestType));
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
58 m_sDestType.majortype=MEDIATYPE_Audio;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
59 m_sDestType.subtype=MEDIASUBTYPE_PCM;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
60 m_sDestType.formattype=FORMAT_WaveFormatEx;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
61 m_sDestType.bFixedSizeSamples=true;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
62 m_sDestType.bTemporalCompression=false;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
63 m_sDestType.lSampleSize=2*wf->nChannels;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
64 m_sDestType.pUnk=0;
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
65 m_sDestType.cbFormat=pWF->cbSize;
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
66 m_sDestType.pbFormat=m_sVhdr2;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
67
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
68 try
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
69 {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
70 m_pDS_Filter = new DS_Filter();
2072
4b3f3f850896 killed unneeded dependency on c++ headers
arpi
parents: 2069
diff changeset
71 m_pDS_Filter->Create(info.dll, &info.guid, &m_sOurType, &m_sDestType);
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
72 m_pDS_Filter->Start();
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
73
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
74 ALLOCATOR_PROPERTIES props, props1;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
75 props.cBuffers=1;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
76 props.cbBuffer=m_sOurType.lSampleSize;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
77 props.cbAlign=props.cbPrefix=0;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
78 m_pDS_Filter->m_pAll->vt->SetProperties(m_pDS_Filter->m_pAll, &props, &props1);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
79 m_pDS_Filter->m_pAll->vt->Commit(m_pDS_Filter->m_pAll);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
80 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
81 catch (FatalError e)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
82 {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
83 e.PrintAll();
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
84 delete[] m_sVhdr;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
85 delete[] m_sVhdr2;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
86 delete m_pDS_Filter;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
87 throw;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
88 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
89 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
90
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
91 DS_AudioDecoder::~DS_AudioDecoder()
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
92 {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
93 delete[] m_sVhdr;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
94 delete[] m_sVhdr2;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
95 delete m_pDS_Filter;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
96 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
97
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
98 int DS_AudioDecoder::Convert(const void* in_data, uint_t in_size,
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
99 void* out_data, uint_t out_size,
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
100 uint_t* size_read, uint_t* size_written)
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
101 {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
102 if (!in_data || !out_data)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
103 return -1;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
104
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
105 uint_t written = 0;
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
106 uint_t read = 0;
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
107 in_size -= in_size%in_fmt.nBlockAlign;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
108 while (in_size>0)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
109 {
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
110 uint_t frame_size=0;
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
111 char* frame_pointer;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
112 // m_pOurOutput->SetFramePointer(out_data+written);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
113 m_pDS_Filter->m_pOurOutput->SetFramePointer(&frame_pointer);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
114 m_pDS_Filter->m_pOurOutput->SetFrameSizePointer((long*)&frame_size);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
115 IMediaSample* sample=0;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
116 m_pDS_Filter->m_pAll->vt->GetBuffer(m_pDS_Filter->m_pAll, &sample, 0, 0, 0);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
117 if(!sample)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
118 {
2069
ce45cce7f7a5 sync with avifile
arpi
parents: 1545
diff changeset
119 Debug printf("DS_AudioDecoder::Convert() Error: null sample\n");
1545
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
120 break;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
121 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
122 char* ptr;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
123 sample->vt->GetPointer(sample, (BYTE **)&ptr);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
124 memcpy(ptr, (const uint8_t*)in_data + read, in_fmt.nBlockAlign);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
125 sample->vt->SetActualDataLength(sample, in_fmt.nBlockAlign);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
126 sample->vt->SetSyncPoint(sample, true);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
127 sample->vt->SetPreroll(sample, 0);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
128 int result = m_pDS_Filter->m_pImp->vt->Receive(m_pDS_Filter->m_pImp, sample);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
129 if (result)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
130 Debug printf("DS_AudioDecoder::Convert() Error: putting data into input pin %x\n", result);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
131 if ((written + frame_size) > out_size)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
132 {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
133 sample->vt->Release((IUnknown*)sample);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
134 break;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
135 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
136 memcpy((uint8_t*)out_data + written, frame_pointer, frame_size);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
137 sample->vt->Release((IUnknown*)sample);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
138 read+=in_fmt.nBlockAlign;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
139 written+=frame_size;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
140 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
141 if (size_read)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
142 *size_read = read;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
143 if (size_written)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
144 *size_written = written;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
145 return 0;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
146 }
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
147
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
148 int DS_AudioDecoder::GetSrcSize(int dest_size)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
149 {
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
150 double efficiency = (double) in_fmt.nAvgBytesPerSec
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
151 / (in_fmt.nSamplesPerSec*in_fmt.nBlockAlign);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
152 int frames = int(dest_size*efficiency);
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
153 if (frames < 1)
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
154 frames = 1;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
155 return frames * in_fmt.nBlockAlign;
da26060c81ef big avifile sync - from now we have common code
arpi
parents:
diff changeset
156 }