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