2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "ad_internal.h"
29 #ifdef CONFIG_QUICKTIME
30 #include <QuickTime/QuickTimeComponents.h>
32 #include "loader/ldt_keeper.h"
33 #include "loader/wine/winbase.h"
34 #include "loader/wine/windef.h"
37 static const ad_info_t info
= {
38 "QuickTime Audio Decoder",
42 "uses win32 quicktime DLLs"
47 #ifndef CONFIG_QUICKTIME
48 typedef struct OpaqueSoundConverter
* SoundConverter
;
49 typedef unsigned long OSType
;
50 typedef unsigned long UnsignedFixed
;
52 typedef struct SoundComponentData
{
57 UnsignedFixed sampleRate
;
63 typedef int (__cdecl
* LPFUNC1
)(long flag
);
64 typedef int (__cdecl
* LPFUNC2
)(const SoundComponentData
*, const SoundComponentData
*,SoundConverter
*);
65 typedef int (__cdecl
* LPFUNC3
)(SoundConverter sc
);
66 typedef int (__cdecl
* LPFUNC4
)(void);
67 typedef int (__cdecl
* LPFUNC5
)(SoundConverter sc
, OSType selector
,void * infoPtr
);
68 typedef int (__cdecl
* LPFUNC6
)(SoundConverter sc
,
69 unsigned long inputBytesTarget
,
70 unsigned long *inputFrames
,
71 unsigned long *inputBytes
,
72 unsigned long *outputBytes
);
73 typedef int (__cdecl
* LPFUNC7
)(SoundConverter sc
,
75 unsigned long inputFrames
,
77 unsigned long *outputFrames
,
78 unsigned long *outputBytes
);
79 typedef int (__cdecl
* LPFUNC8
)(SoundConverter sc
,
81 unsigned long *outputFrames
,
82 unsigned long *outputBytes
);
83 typedef int (__cdecl
* LPFUNC9
)(SoundConverter sc
) ;
85 static HINSTANCE qtime_qts
; // handle to the preloaded quicktime.qts
86 static HINSTANCE qtml_dll
;
87 static LPFUNC1 InitializeQTML
;
88 static LPFUNC2 SoundConverterOpen
;
89 static LPFUNC3 SoundConverterClose
;
90 static LPFUNC4 TerminateQTML
;
91 static LPFUNC5 SoundConverterSetInfo
;
92 static LPFUNC6 SoundConverterGetBufferSizes
;
93 static LPFUNC7 SoundConverterConvertBuffer
;
94 static LPFUNC8 SoundConverterEndConversion
;
95 static LPFUNC9 SoundConverterBeginConversion
;
97 #define siDecompressionParams 2002876005 // siDecompressionParams = FOUR_CHAR_CODE('wave')
99 static int loader_init(void)
105 //preload quicktime.qts to avoid the problems caused by the hardcoded path inside the dll
106 qtime_qts
= LoadLibraryA("QuickTime.qts");
107 if( qtime_qts
== (HMODULE
)NULL
)
109 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed loading QuickTime.qts\n" );
112 qtml_dll
= LoadLibraryA("qtmlClient.dll");
113 if( qtml_dll
== (HMODULE
)NULL
)
115 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed loading qtmlClient.dll\n" );
118 InitializeQTML
= (LPFUNC1
)GetProcAddress(qtml_dll
,"InitializeQTML");
119 if ( InitializeQTML
== NULL
)
121 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed geting proc address InitializeQTML\n");
124 SoundConverterOpen
= (LPFUNC2
)GetProcAddress(qtml_dll
,"SoundConverterOpen");
125 if ( SoundConverterOpen
== NULL
)
127 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterOpen\n");
130 SoundConverterClose
= (LPFUNC3
)GetProcAddress(qtml_dll
,"SoundConverterClose");
131 if ( SoundConverterClose
== NULL
)
133 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterClose\n");
136 TerminateQTML
= (LPFUNC4
)GetProcAddress(qtml_dll
,"TerminateQTML");
137 if ( TerminateQTML
== NULL
)
139 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address TerminateQTML\n");
142 SoundConverterSetInfo
= (LPFUNC5
)GetProcAddress(qtml_dll
,"SoundConverterSetInfo");
143 if ( SoundConverterSetInfo
== NULL
)
145 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterSetInfo\n");
148 SoundConverterGetBufferSizes
= (LPFUNC6
)GetProcAddress(qtml_dll
,"SoundConverterGetBufferSizes");
149 if ( SoundConverterGetBufferSizes
== NULL
)
151 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterGetBufferSizes\n");
154 SoundConverterConvertBuffer
= (LPFUNC7
)GetProcAddress(qtml_dll
,"SoundConverterConvertBuffer");
155 if ( SoundConverterConvertBuffer
== NULL
)
157 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterConvertBuffer1\n");
160 SoundConverterEndConversion
= (LPFUNC8
)GetProcAddress(qtml_dll
,"SoundConverterEndConversion");
161 if ( SoundConverterEndConversion
== NULL
)
163 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterEndConversion\n");
166 SoundConverterBeginConversion
= (LPFUNC9
)GetProcAddress(qtml_dll
,"SoundConverterBeginConversion");
167 if ( SoundConverterBeginConversion
== NULL
)
169 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"failed getting proc address SoundConverterBeginConversion\n");
172 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"loader_init DONE???\n");
175 #endif /* #ifndef CONFIG_QUICKTIME */
177 static SoundConverter myConverter
= NULL
;
178 static SoundComponentData InputFormatInfo
,OutputFormatInfo
;
180 static int InFrameSize
;
181 static int OutFrameSize
;
183 static int preinit(sh_audio_t
*sh
){
185 unsigned long FramesToGet
=0; //how many frames the demuxer has to get
186 unsigned long InputBufferSize
=0; //size of the input buffer
187 unsigned long OutputBufferSize
=0; //size of the output buffer
188 unsigned long WantedBufferSize
=0; //the size you want your buffers to be
191 #ifdef CONFIG_QUICKTIME
194 if(loader_init()) return 0; // failed to load DLL
196 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"loader_init DONE!\n");
198 error
= InitializeQTML(6+16);
200 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"InitializeQTML:%i\n",error
);
205 OutputFormatInfo
.flags
= InputFormatInfo
.flags
= 0;
206 OutputFormatInfo
.sampleCount
= InputFormatInfo
.sampleCount
= 0;
207 OutputFormatInfo
.buffer
= InputFormatInfo
.buffer
= NULL
;
208 OutputFormatInfo
.reserved
= InputFormatInfo
.reserved
= 0;
209 OutputFormatInfo
.numChannels
= InputFormatInfo
.numChannels
= sh
->wf
->nChannels
;
210 InputFormatInfo
.sampleSize
= sh
->wf
->wBitsPerSample
;
211 OutputFormatInfo
.sampleSize
= 16;
212 OutputFormatInfo
.sampleRate
= InputFormatInfo
.sampleRate
= sh
->wf
->nSamplesPerSec
;
213 InputFormatInfo
.format
= bswap_32(sh
->format
); //1363430706;///*1768775988;//*/1902406962;//qdm2//1768775988;//FOUR_CHAR_CODE('ima4');
214 OutputFormatInfo
.format
= 1313820229;// FOUR_CHAR_CODE('NONE');
216 error
= SoundConverterOpen(&InputFormatInfo
, &OutputFormatInfo
, &myConverter
);
217 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"SoundConverterOpen:%i\n",error
);
221 error
= SoundConverterSetInfo(myConverter
,siDecompressionParams
,sh
->codecdata
);
222 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"SoundConverterSetInfo:%i\n",error
);
223 // if(error) return 0;
226 WantedBufferSize
=OutputFormatInfo
.numChannels
*OutputFormatInfo
.sampleRate
*2;
227 error
= SoundConverterGetBufferSizes(myConverter
,
228 WantedBufferSize
,&FramesToGet
,&InputBufferSize
,&OutputBufferSize
);
229 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"SoundConverterGetBufferSizes:%i\n",error
);
230 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"WantedBufferSize = %li\n",WantedBufferSize
);
231 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"InputBufferSize = %li\n",InputBufferSize
);
232 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"OutputBufferSize = %li\n",OutputBufferSize
);
233 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FramesToGet = %li\n",FramesToGet
);
235 InFrameSize
=(InputBufferSize
+FramesToGet
-1)/FramesToGet
;
236 OutFrameSize
=OutputBufferSize
/FramesToGet
;
238 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"FrameSize: %i -> %i\n",InFrameSize
,OutFrameSize
);
240 error
= SoundConverterBeginConversion(myConverter
);
241 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"SoundConverterBeginConversion:%i\n",error
);
244 sh
->audio_out_minsize
=OutputBufferSize
;
245 sh
->audio_in_minsize
=InputBufferSize
;
247 sh
->channels
=sh
->wf
->nChannels
;
248 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
249 sh
->samplesize
=2; //(sh->wf->wBitsPerSample+7)/8;
251 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
252 //InputBufferSize*WantedBufferSize/OutputBufferSize;
254 if(sh
->format
==0x3343414D){
256 sh
->ds
->ss_div
= 2*3; // 1 samples/packet
257 sh
->ds
->ss_mul
= sh
->channels
*2*1; // 1 bytes/packet
259 if(sh
->format
==0x3643414D){
261 sh
->ds
->ss_div
= 2*6; // 1 samples/packet
262 sh
->ds
->ss_mul
= sh
->channels
*2*1; // 1 bytes/packet
265 return 1; // return values: 1=OK 0=ERROR
268 static int init(sh_audio_t
*sh_audio
){
270 return 1; // return values: 1=OK 0=ERROR
273 static void uninit(sh_audio_t
*sh
){
275 unsigned long ConvertedFrames
=0;
276 unsigned long ConvertedBytes
=0;
282 error
=SoundConverterEndConversion(myConverter
,NULL
,&ConvertedFrames
,&ConvertedBytes
);
283 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"SoundConverterEndConversion:%i\n",error
);
284 error
= SoundConverterClose(myConverter
);
285 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"SoundConverterClose:%i\n",error
);
286 // error = TerminateQTML();
287 // printf("TerminateQTML:%i\n",error);
288 // FreeLibrary( qtml_dll );
290 // FreeLibrary( qtime_qts );
292 // printf("qt dll loader uninit done\n");
293 #ifdef CONFIG_QUICKTIME
298 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
300 unsigned long FramesToGet
=0; //how many frames the demuxer has to get
301 unsigned long InputBufferSize
=0; //size of the input buffer
302 unsigned long ConvertedFrames
=0;
303 unsigned long ConvertedBytes
=0;
309 FramesToGet
=minlen
/OutFrameSize
;
310 if(FramesToGet
*OutFrameSize
<minlen
&&
311 (FramesToGet
+1)*OutFrameSize
<=maxlen
) ++FramesToGet
;
312 if(FramesToGet
*InFrameSize
>sh
->a_in_buffer_size
)
313 FramesToGet
=sh
->a_in_buffer_size
/InFrameSize
;
315 InputBufferSize
=FramesToGet
*InFrameSize
;
317 // printf("FramesToGet = %li (%li -> %li bytes)\n",FramesToGet,
318 // InputBufferSize, FramesToGet*OutFrameSize);
320 if(InputBufferSize
>sh
->a_in_buffer_len
){
321 int x
=demux_read_data(sh
->ds
,&sh
->a_in_buffer
[sh
->a_in_buffer_len
],
322 InputBufferSize
-sh
->a_in_buffer_len
);
323 if(x
>0) sh
->a_in_buffer_len
+=x
;
324 if(InputBufferSize
>sh
->a_in_buffer_len
)
325 FramesToGet
=sh
->a_in_buffer_len
/InFrameSize
; // not enough data!
328 // printf("\nSoundConverterConvertBuffer(myConv=%p,inbuf=%p,frames=%d,outbuf=%p,&convframes=%p,&convbytes=%p)\n",
329 // myConverter,sh->a_in_buffer,FramesToGet,buf,&ConvertedFrames,&ConvertedBytes);
330 error
= SoundConverterConvertBuffer(myConverter
,sh
->a_in_buffer
,
331 FramesToGet
,buf
,&ConvertedFrames
,&ConvertedBytes
);
332 // printf("SoundConverterConvertBuffer:%i\n",error);
333 // printf("ConvertedFrames = %li\n",ConvertedFrames);
334 // printf("ConvertedBytes = %li\n",ConvertedBytes);
336 // InputBufferSize=(ConvertedBytes/OutFrameSize)*InFrameSize; // FIXME!!
337 InputBufferSize
=FramesToGet
*InFrameSize
;
338 sh
->a_in_buffer_len
-=InputBufferSize
;
339 if(sh
->a_in_buffer_len
<0) sh
->a_in_buffer_len
=0; // should not happen...
340 else if(sh
->a_in_buffer_len
>0){
341 memcpy(sh
->a_in_buffer
,&sh
->a_in_buffer
[InputBufferSize
],sh
->a_in_buffer_len
);
344 return ConvertedBytes
;
347 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
348 // various optional functions you MAY implement:
349 return CONTROL_UNKNOWN
;