summaryrefslogtreecommitdiffstats
path: root/loader/dshow/DS_AudioDec.c
blob: b013f5a1d5a1d58d2d88480a890d782e695580c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/********************************************************

	DirectShow Audio decoder implementation
	Copyright 2000 Eugene Kuznetsov  (divx@euro.ru)
        Converted  C++ --> C  :) by A'rpi/ESP-team

*********************************************************/

//#include <config.h>

//#include "DS_AudioDecoder.h"
//#include <string.h>
#include <stdlib.h>
#include <except.h>
#define __MODULE__ "DirectShow_AudioDecoder"

#include <errno.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
//#include <loader.h>
//#include <wine/winbase.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strstream>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/mman.h>

#include <registry.h>
#include <wine/winreg.h>

#include "guids.h"
#include "interfaces.h"
#include "DS_Filter.h"

#include "BitmapInfo.h"

#include <string>
#include <default.h>

#include "DS_AudioDec.h"

const GUID FORMAT_WaveFormatEx={
0x05589f81, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a};
const GUID MEDIATYPE_Audio={
0x73647561, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
const GUID MEDIASUBTYPE_PCM={
0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};


using namespace std;
extern "C" char* def_path;

    static DS_Filter* dsf=0;

    static AM_MEDIA_TYPE m_sOurType, m_sDestType;
//    static void* m_pCust;
    static char* m_sVhdr;
    static char* m_sVhdr2;

    static WAVEFORMATEX in_fmt;

//    int m_iState=0;

extern "C" int DS_AudioDecoder_Open(char* dllname, GUID* guid, WAVEFORMATEX* wf)
{

    m_sVhdr=new char[18+wf->cbSize];
    memcpy(m_sVhdr, wf, 18+wf->cbSize);
    m_sVhdr2=new char[18];
    memcpy(m_sVhdr2, m_sVhdr, 18);
    WAVEFORMATEX* pWF=(WAVEFORMATEX*)m_sVhdr2;
    pWF->wFormatTag=1;
    pWF->wBitsPerSample=16;
    pWF->nBlockAlign=2*pWF->nChannels;
    pWF->cbSize=0;
    in_fmt=*wf;

    memset(&m_sOurType, 0, sizeof m_sOurType);
    m_sOurType.majortype=MEDIATYPE_Audio;
    m_sOurType.subtype=MEDIASUBTYPE_PCM;
    m_sOurType.subtype.f1=wf->wFormatTag;
    m_sOurType.formattype=FORMAT_WaveFormatEx;
    m_sOurType.lSampleSize=wf->nBlockAlign;
    m_sOurType.bFixedSizeSamples=true;
    m_sOurType.bTemporalCompression=false;
    m_sOurType.pUnk=0;
    m_sOurType.cbFormat=18+wf->cbSize;
    m_sOurType.pbFormat=m_sVhdr;

    memset(&m_sDestType, 0, sizeof m_sDestType);
    m_sDestType.majortype=MEDIATYPE_Audio;
    m_sDestType.subtype=MEDIASUBTYPE_PCM;
    m_sDestType.formattype=FORMAT_WaveFormatEx;
    m_sDestType.bFixedSizeSamples=true;
    m_sDestType.bTemporalCompression=false;
    m_sDestType.lSampleSize=2*wf->nChannels;
    m_sDestType.pUnk=0;
    m_sDestType.cbFormat=18;
    m_sDestType.pbFormat=m_sVhdr2;

    try
    {

        dsf=new DS_Filter();
	dsf->Create(dllname, guid, &m_sOurType, &m_sDestType);
        dsf->Start();

	ALLOCATOR_PROPERTIES props, props1;
	props.cBuffers=1;
        props.cbBuffer=m_sOurType.lSampleSize;
	props.cbAlign=props.cbPrefix=0;
	dsf->m_pAll->vt->SetProperties(dsf->m_pAll, &props, &props1);
	dsf->m_pAll->vt->Commit(dsf->m_pAll);
    }
    catch(FatalError e)
    {
	e.PrintAll();
	delete[] m_sVhdr;
	delete[] m_sVhdr2;
        return 1;
    }

    return 0;
}

extern "C" void DS_AudioDecoder_Close(){
    delete[] m_sVhdr;
    delete[] m_sVhdr2;
}

extern "C" int DS_AudioDecoder_GetSrcSize(int dest_size)
{
    double efficiency=in_fmt.nAvgBytesPerSec/double(in_fmt.nSamplesPerSec*in_fmt.nBlockAlign);
    int frames=(int)(dest_size*efficiency);
    if(frames<1)frames=1;
    return frames*in_fmt.nBlockAlign;
}


extern "C" int DS_AudioDecoder_Convert(unsigned char* in_data, unsigned in_size,
	     unsigned char* out_data, unsigned out_size,
	    unsigned* size_read, unsigned* size_written)
{
    if(in_data==0)return -1;
    if(out_data==0)return -1;
    int written=0;
    int read=0;
    in_size-=in_size%in_fmt.nBlockAlign;
    while(in_size>0)
    {
	long frame_size=0;
	char* frame_pointer;
//	m_pOurOutput->SetFramePointer(out_data+written);
	dsf->m_pOurOutput->SetFramePointer(&frame_pointer);
	dsf->m_pOurOutput->SetFrameSizePointer(&frame_size);
	IMediaSample* sample=0;
	dsf->m_pAll->vt->GetBuffer(dsf->m_pAll, &sample, 0, 0, 0);
	if(!sample)
	{
	    cerr<<"ERROR: null sample"<<endl;
	    break;
	}
	char* ptr;
	sample->vt->GetPointer(sample, (BYTE **)&ptr);
	memcpy(ptr, in_data+read, in_fmt.nBlockAlign);
	sample->vt->SetActualDataLength(sample, in_fmt.nBlockAlign);
	sample->vt->SetSyncPoint(sample, true);
	sample->vt->SetPreroll(sample, 0);
        int result=dsf->m_pImp->vt->Receive(dsf->m_pImp, sample);
        if(result) printf("Error putting data into input pin %x\n", result);
	if(written+frame_size>out_size)
	{
	    sample->vt->Release((IUnknown*)sample);
	    break;
	}
	memcpy(out_data+written, frame_pointer, frame_size);
        sample->vt->Release((IUnknown*)sample);
	read+=in_fmt.nBlockAlign;
	written+=frame_size;
    }
    if(size_read) *size_read=read;
    if(size_written) *size_written=written;
    return 0;
}