cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / libmpcodecs / ad_qtaudio.c
blob1edd87cd17920ffddf8daf3807bfb6524bae147a
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <inttypes.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "mpbswap.h"
27 #include "ad_internal.h"
29 #ifdef CONFIG_QUICKTIME
30 #include <QuickTime/QuickTimeComponents.h>
31 #else
32 #include "loader/ldt_keeper.h"
33 #include "loader/wine/winbase.h"
34 #include "loader/wine/windef.h"
35 #endif
37 static const ad_info_t info = {
38 "QuickTime Audio Decoder",
39 "qtaudio",
40 "A'rpi",
41 "Sascha Sommer",
42 "uses win32 quicktime DLLs"
45 LIBAD_EXTERN(qtaudio)
47 #ifndef CONFIG_QUICKTIME
48 typedef struct OpaqueSoundConverter* SoundConverter;
49 typedef unsigned long OSType;
50 typedef unsigned long UnsignedFixed;
51 typedef uint8_t Byte;
52 typedef struct SoundComponentData {
53 long flags;
54 OSType format;
55 short numChannels;
56 short sampleSize;
57 UnsignedFixed sampleRate;
58 long sampleCount;
59 Byte * buffer;
60 long reserved;
61 }SoundComponentData;
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,
74 const void *inputPtr,
75 unsigned long inputFrames,
76 void *outputPtr,
77 unsigned long *outputFrames,
78 unsigned long *outputBytes );
79 typedef int (__cdecl* LPFUNC8)(SoundConverter sc,
80 void *outputPtr,
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)
102 #ifdef WIN32_LOADER
103 Setup_LDT_Keeper();
104 #endif
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" );
110 return 1;
112 qtml_dll = LoadLibraryA("qtmlClient.dll");
113 if( qtml_dll == (HMODULE)NULL )
115 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed loading qtmlClient.dll\n" );
116 return 1;
118 InitializeQTML = (LPFUNC1)GetProcAddress(qtml_dll,"InitializeQTML");
119 if ( InitializeQTML == NULL )
121 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed geting proc address InitializeQTML\n");
122 return 1;
124 SoundConverterOpen = (LPFUNC2)GetProcAddress(qtml_dll,"SoundConverterOpen");
125 if ( SoundConverterOpen == NULL )
127 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterOpen\n");
128 return 1;
130 SoundConverterClose = (LPFUNC3)GetProcAddress(qtml_dll,"SoundConverterClose");
131 if ( SoundConverterClose == NULL )
133 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterClose\n");
134 return 1;
136 TerminateQTML = (LPFUNC4)GetProcAddress(qtml_dll,"TerminateQTML");
137 if ( TerminateQTML == NULL )
139 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address TerminateQTML\n");
140 return 1;
142 SoundConverterSetInfo = (LPFUNC5)GetProcAddress(qtml_dll,"SoundConverterSetInfo");
143 if ( SoundConverterSetInfo == NULL )
145 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterSetInfo\n");
146 return 1;
148 SoundConverterGetBufferSizes = (LPFUNC6)GetProcAddress(qtml_dll,"SoundConverterGetBufferSizes");
149 if ( SoundConverterGetBufferSizes == NULL )
151 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterGetBufferSizes\n");
152 return 1;
154 SoundConverterConvertBuffer = (LPFUNC7)GetProcAddress(qtml_dll,"SoundConverterConvertBuffer");
155 if ( SoundConverterConvertBuffer == NULL )
157 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterConvertBuffer1\n");
158 return 1;
160 SoundConverterEndConversion = (LPFUNC8)GetProcAddress(qtml_dll,"SoundConverterEndConversion");
161 if ( SoundConverterEndConversion == NULL )
163 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterEndConversion\n");
164 return 1;
166 SoundConverterBeginConversion = (LPFUNC9)GetProcAddress(qtml_dll,"SoundConverterBeginConversion");
167 if ( SoundConverterBeginConversion == NULL )
169 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterBeginConversion\n");
170 return 1;
172 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"loader_init DONE???\n");
173 return 0;
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){
184 int error;
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
189 void* codecdata = sh->codecdata;
191 if (!sh->codecdata_len && sh->wf && sh->wf->cbSize){
192 codecdata = sh->wf + 1;
195 #ifdef CONFIG_QUICKTIME
196 EnterMovies();
197 #else
198 if(loader_init()) return 0; // failed to load DLL
200 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"loader_init DONE!\n");
202 error = InitializeQTML(6+16);
203 if(error){
204 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"InitializeQTML:%i\n",error);
205 return 0;
207 #endif
209 OutputFormatInfo.flags = InputFormatInfo.flags = 0;
210 OutputFormatInfo.sampleCount = InputFormatInfo.sampleCount = 0;
211 OutputFormatInfo.buffer = InputFormatInfo.buffer = NULL;
212 OutputFormatInfo.reserved = InputFormatInfo.reserved = 0;
213 OutputFormatInfo.numChannels = InputFormatInfo.numChannels = sh->wf->nChannels;
214 InputFormatInfo.sampleSize = sh->wf->wBitsPerSample;
215 OutputFormatInfo.sampleSize = 16;
216 OutputFormatInfo.sampleRate = InputFormatInfo.sampleRate = sh->wf->nSamplesPerSec;
217 InputFormatInfo.format = bswap_32(sh->format); //1363430706;///*1768775988;//*/1902406962;//qdm2//1768775988;//FOUR_CHAR_CODE('ima4');
218 OutputFormatInfo.format = 1313820229;// FOUR_CHAR_CODE('NONE');
220 error = SoundConverterOpen(&InputFormatInfo, &OutputFormatInfo, &myConverter);
221 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterOpen:%i\n",error);
222 if(error) return 0;
224 if(codecdata){
225 error = SoundConverterSetInfo(myConverter,siDecompressionParams,codecdata);
226 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterSetInfo:%i\n",error);
227 // if(error) return 0;
230 WantedBufferSize=OutputFormatInfo.numChannels*OutputFormatInfo.sampleRate*2;
231 error = SoundConverterGetBufferSizes(myConverter,
232 WantedBufferSize,&FramesToGet,&InputBufferSize,&OutputBufferSize);
233 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterGetBufferSizes:%i\n",error);
234 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"WantedBufferSize = %li\n",WantedBufferSize);
235 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"InputBufferSize = %li\n",InputBufferSize);
236 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"OutputBufferSize = %li\n",OutputBufferSize);
237 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FramesToGet = %li\n",FramesToGet);
239 InFrameSize=(InputBufferSize+FramesToGet-1)/FramesToGet;
240 OutFrameSize=OutputBufferSize/FramesToGet;
242 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FrameSize: %i -> %i\n",InFrameSize,OutFrameSize);
244 error = SoundConverterBeginConversion(myConverter);
245 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterBeginConversion:%i\n",error);
246 if(error) return 0;
248 sh->audio_out_minsize=OutputBufferSize;
249 sh->audio_in_minsize=InputBufferSize;
251 sh->channels=sh->wf->nChannels;
252 sh->samplerate=sh->wf->nSamplesPerSec;
253 sh->samplesize=2; //(sh->wf->wBitsPerSample+7)/8;
255 sh->i_bps=sh->wf->nAvgBytesPerSec;
256 //InputBufferSize*WantedBufferSize/OutputBufferSize;
258 if(sh->format==0x3343414D){
259 // MACE 3:1
260 sh->ds->ss_div = 2*3; // 1 samples/packet
261 sh->ds->ss_mul = sh->channels*2*1; // 1 bytes/packet
262 } else
263 if(sh->format==0x3643414D){
264 // MACE 6:1
265 sh->ds->ss_div = 2*6; // 1 samples/packet
266 sh->ds->ss_mul = sh->channels*2*1; // 1 bytes/packet
269 return 1; // return values: 1=OK 0=ERROR
272 static int init(sh_audio_t *sh_audio){
274 return 1; // return values: 1=OK 0=ERROR
277 static void uninit(sh_audio_t *sh){
278 int error;
279 unsigned long ConvertedFrames=0;
280 unsigned long ConvertedBytes=0;
282 #if defined(WIN32_LOADER) && !defined(CONFIG_QUICKTIME)
283 Setup_FS_Segment();
284 #endif
286 error=SoundConverterEndConversion(myConverter,NULL,&ConvertedFrames,&ConvertedBytes);
287 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterEndConversion:%i\n",error);
288 error = SoundConverterClose(myConverter);
289 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterClose:%i\n",error);
290 // error = TerminateQTML();
291 // printf("TerminateQTML:%i\n",error);
292 // FreeLibrary( qtml_dll );
293 // qtml_dll = NULL;
294 // FreeLibrary( qtime_qts );
295 // qtime_qts = NULL;
296 // printf("qt dll loader uninit done\n");
297 #ifdef CONFIG_QUICKTIME
298 ExitMovies();
299 #endif
302 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
303 unsigned long FramesToGet=0; //how many frames the demuxer has to get
304 unsigned long InputBufferSize=0; //size of the input buffer
305 unsigned long ConvertedFrames=0;
306 unsigned long ConvertedBytes=0;
308 #if defined(WIN32_LOADER) && !defined(CONFIG_QUICKTIME)
309 Setup_FS_Segment();
310 #endif
312 FramesToGet=minlen/OutFrameSize;
313 if(FramesToGet*OutFrameSize<minlen &&
314 (FramesToGet+1)*OutFrameSize<=maxlen) ++FramesToGet;
315 if(FramesToGet*InFrameSize>sh->a_in_buffer_size)
316 FramesToGet=sh->a_in_buffer_size/InFrameSize;
318 InputBufferSize=FramesToGet*InFrameSize;
320 // printf("FramesToGet = %li (%li -> %li bytes)\n",FramesToGet,
321 // InputBufferSize, FramesToGet*OutFrameSize);
323 if(InputBufferSize>sh->a_in_buffer_len){
324 int x=demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
325 InputBufferSize-sh->a_in_buffer_len);
326 if(x>0) sh->a_in_buffer_len+=x;
327 if(InputBufferSize>sh->a_in_buffer_len)
328 FramesToGet=sh->a_in_buffer_len/InFrameSize; // not enough data!
331 // printf("\nSoundConverterConvertBuffer(myConv=%p,inbuf=%p,frames=%d,outbuf=%p,&convframes=%p,&convbytes=%p)\n",
332 // myConverter,sh->a_in_buffer,FramesToGet,buf,&ConvertedFrames,&ConvertedBytes);
333 SoundConverterConvertBuffer(myConverter,sh->a_in_buffer,
334 FramesToGet,buf,&ConvertedFrames,&ConvertedBytes);
335 // printf("SoundConverterConvertBuffer:%i\n",error);
336 // printf("ConvertedFrames = %li\n",ConvertedFrames);
337 // printf("ConvertedBytes = %li\n",ConvertedBytes);
339 // InputBufferSize=(ConvertedBytes/OutFrameSize)*InFrameSize; // FIXME!!
340 InputBufferSize=FramesToGet*InFrameSize;
341 sh->a_in_buffer_len-=InputBufferSize;
342 if(sh->a_in_buffer_len<0) sh->a_in_buffer_len=0; // should not happen...
343 else if(sh->a_in_buffer_len>0){
344 memcpy(sh->a_in_buffer,&sh->a_in_buffer[InputBufferSize],sh->a_in_buffer_len);
347 return ConvertedBytes;
350 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
351 // various optional functions you MAY implement:
352 return CONTROL_UNKNOWN;