sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / libmpcodecs / ad_qtaudio.c
blob188238233c5f9804ce5a31c173bff871a054075d
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/windef.h"
34 #endif
36 static const ad_info_t info = {
37 "QuickTime Audio Decoder",
38 "qtaudio",
39 "A'rpi",
40 "Sascha Sommer",
41 "uses win32 quicktime DLLs"
44 LIBAD_EXTERN(qtaudio)
46 #ifndef CONFIG_QUICKTIME
47 typedef struct OpaqueSoundConverter* SoundConverter;
48 typedef unsigned long OSType;
49 typedef unsigned long UnsignedFixed;
50 typedef uint8_t Byte;
51 typedef struct SoundComponentData {
52 long flags;
53 OSType format;
54 short numChannels;
55 short sampleSize;
56 UnsignedFixed sampleRate;
57 long sampleCount;
58 Byte * buffer;
59 long reserved;
60 }SoundComponentData;
62 typedef int (__cdecl* LPFUNC1)(long flag);
63 typedef int (__cdecl* LPFUNC2)(const SoundComponentData *, const SoundComponentData *,SoundConverter *);
64 typedef int (__cdecl* LPFUNC3)(SoundConverter sc);
65 typedef int (__cdecl* LPFUNC4)(void);
66 typedef int (__cdecl* LPFUNC5)(SoundConverter sc, OSType selector,void * infoPtr);
67 typedef int (__cdecl* LPFUNC6)(SoundConverter sc,
68 unsigned long inputBytesTarget,
69 unsigned long *inputFrames,
70 unsigned long *inputBytes,
71 unsigned long *outputBytes );
72 typedef int (__cdecl* LPFUNC7)(SoundConverter sc,
73 const void *inputPtr,
74 unsigned long inputFrames,
75 void *outputPtr,
76 unsigned long *outputFrames,
77 unsigned long *outputBytes );
78 typedef int (__cdecl* LPFUNC8)(SoundConverter sc,
79 void *outputPtr,
80 unsigned long *outputFrames,
81 unsigned long *outputBytes);
82 typedef int (__cdecl* LPFUNC9)(SoundConverter sc) ;
84 static HINSTANCE qtime_qts; // handle to the preloaded quicktime.qts
85 static HINSTANCE qtml_dll;
86 static LPFUNC1 InitializeQTML;
87 static LPFUNC2 SoundConverterOpen;
88 static LPFUNC3 SoundConverterClose;
89 static LPFUNC4 TerminateQTML;
90 static LPFUNC5 SoundConverterSetInfo;
91 static LPFUNC6 SoundConverterGetBufferSizes;
92 static LPFUNC7 SoundConverterConvertBuffer;
93 static LPFUNC8 SoundConverterEndConversion;
94 static LPFUNC9 SoundConverterBeginConversion;
96 #define siDecompressionParams 2002876005 // siDecompressionParams = FOUR_CHAR_CODE('wave')
98 HMODULE WINAPI LoadLibraryA(LPCSTR);
99 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
100 int WINAPI FreeLibrary(HMODULE);
102 static int loader_init(void)
105 #ifdef WIN32_LOADER
106 Setup_LDT_Keeper();
107 #endif
108 //preload quicktime.qts to avoid the problems caused by the hardcoded path inside the dll
109 qtime_qts = LoadLibraryA("QuickTime.qts");
110 if( qtime_qts == (HMODULE)NULL )
112 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed loading QuickTime.qts\n" );
113 return 1;
115 qtml_dll = LoadLibraryA("qtmlClient.dll");
116 if( qtml_dll == (HMODULE)NULL )
118 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed loading qtmlClient.dll\n" );
119 return 1;
121 InitializeQTML = (LPFUNC1)GetProcAddress(qtml_dll,"InitializeQTML");
122 if ( InitializeQTML == NULL )
124 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed geting proc address InitializeQTML\n");
125 return 1;
127 SoundConverterOpen = (LPFUNC2)GetProcAddress(qtml_dll,"SoundConverterOpen");
128 if ( SoundConverterOpen == NULL )
130 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterOpen\n");
131 return 1;
133 SoundConverterClose = (LPFUNC3)GetProcAddress(qtml_dll,"SoundConverterClose");
134 if ( SoundConverterClose == NULL )
136 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterClose\n");
137 return 1;
139 TerminateQTML = (LPFUNC4)GetProcAddress(qtml_dll,"TerminateQTML");
140 if ( TerminateQTML == NULL )
142 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address TerminateQTML\n");
143 return 1;
145 SoundConverterSetInfo = (LPFUNC5)GetProcAddress(qtml_dll,"SoundConverterSetInfo");
146 if ( SoundConverterSetInfo == NULL )
148 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterSetInfo\n");
149 return 1;
151 SoundConverterGetBufferSizes = (LPFUNC6)GetProcAddress(qtml_dll,"SoundConverterGetBufferSizes");
152 if ( SoundConverterGetBufferSizes == NULL )
154 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterGetBufferSizes\n");
155 return 1;
157 SoundConverterConvertBuffer = (LPFUNC7)GetProcAddress(qtml_dll,"SoundConverterConvertBuffer");
158 if ( SoundConverterConvertBuffer == NULL )
160 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterConvertBuffer1\n");
161 return 1;
163 SoundConverterEndConversion = (LPFUNC8)GetProcAddress(qtml_dll,"SoundConverterEndConversion");
164 if ( SoundConverterEndConversion == NULL )
166 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterEndConversion\n");
167 return 1;
169 SoundConverterBeginConversion = (LPFUNC9)GetProcAddress(qtml_dll,"SoundConverterBeginConversion");
170 if ( SoundConverterBeginConversion == NULL )
172 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"failed getting proc address SoundConverterBeginConversion\n");
173 return 1;
175 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"loader_init DONE???\n");
176 return 0;
178 #endif /* #ifndef CONFIG_QUICKTIME */
180 static SoundConverter myConverter = NULL;
181 static SoundComponentData InputFormatInfo,OutputFormatInfo;
183 static int InFrameSize;
184 static int OutFrameSize;
186 static int preinit(sh_audio_t *sh){
187 int error;
188 unsigned long FramesToGet=0; //how many frames the demuxer has to get
189 unsigned long InputBufferSize=0; //size of the input buffer
190 unsigned long OutputBufferSize=0; //size of the output buffer
191 unsigned long WantedBufferSize=0; //the size you want your buffers to be
194 #ifdef CONFIG_QUICKTIME
195 EnterMovies();
196 #else
197 if(loader_init()) return 0; // failed to load DLL
199 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"loader_init DONE!\n");
201 error = InitializeQTML(6+16);
202 if(error){
203 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"InitializeQTML:%i\n",error);
204 return 0;
206 #endif
208 OutputFormatInfo.flags = InputFormatInfo.flags = 0;
209 OutputFormatInfo.sampleCount = InputFormatInfo.sampleCount = 0;
210 OutputFormatInfo.buffer = InputFormatInfo.buffer = NULL;
211 OutputFormatInfo.reserved = InputFormatInfo.reserved = 0;
212 OutputFormatInfo.numChannels = InputFormatInfo.numChannels = sh->wf->nChannels;
213 InputFormatInfo.sampleSize = sh->wf->wBitsPerSample;
214 OutputFormatInfo.sampleSize = 16;
215 OutputFormatInfo.sampleRate = InputFormatInfo.sampleRate = sh->wf->nSamplesPerSec;
216 InputFormatInfo.format = bswap_32(sh->format); //1363430706;///*1768775988;//*/1902406962;//qdm2//1768775988;//FOUR_CHAR_CODE('ima4');
217 OutputFormatInfo.format = 1313820229;// FOUR_CHAR_CODE('NONE');
219 error = SoundConverterOpen(&InputFormatInfo, &OutputFormatInfo, &myConverter);
220 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterOpen:%i\n",error);
221 if(error) return 0;
223 if(sh->codecdata){
224 error = SoundConverterSetInfo(myConverter,siDecompressionParams,sh->codecdata);
225 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterSetInfo:%i\n",error);
226 // if(error) return 0;
229 WantedBufferSize=OutputFormatInfo.numChannels*OutputFormatInfo.sampleRate*2;
230 error = SoundConverterGetBufferSizes(myConverter,
231 WantedBufferSize,&FramesToGet,&InputBufferSize,&OutputBufferSize);
232 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterGetBufferSizes:%i\n",error);
233 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"WantedBufferSize = %li\n",WantedBufferSize);
234 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"InputBufferSize = %li\n",InputBufferSize);
235 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"OutputBufferSize = %li\n",OutputBufferSize);
236 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FramesToGet = %li\n",FramesToGet);
238 InFrameSize=(InputBufferSize+FramesToGet-1)/FramesToGet;
239 OutFrameSize=OutputBufferSize/FramesToGet;
241 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FrameSize: %i -> %i\n",InFrameSize,OutFrameSize);
243 error = SoundConverterBeginConversion(myConverter);
244 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterBeginConversion:%i\n",error);
245 if(error) return 0;
247 sh->audio_out_minsize=OutputBufferSize;
248 sh->audio_in_minsize=InputBufferSize;
250 sh->channels=sh->wf->nChannels;
251 sh->samplerate=sh->wf->nSamplesPerSec;
252 sh->samplesize=2; //(sh->wf->wBitsPerSample+7)/8;
254 sh->i_bps=sh->wf->nAvgBytesPerSec;
255 //InputBufferSize*WantedBufferSize/OutputBufferSize;
257 if(sh->format==0x3343414D){
258 // MACE 3:1
259 sh->ds->ss_div = 2*3; // 1 samples/packet
260 sh->ds->ss_mul = sh->channels*2*1; // 1 bytes/packet
261 } else
262 if(sh->format==0x3643414D){
263 // MACE 6:1
264 sh->ds->ss_div = 2*6; // 1 samples/packet
265 sh->ds->ss_mul = sh->channels*2*1; // 1 bytes/packet
268 return 1; // return values: 1=OK 0=ERROR
271 static int init(sh_audio_t *sh_audio){
273 return 1; // return values: 1=OK 0=ERROR
276 static void uninit(sh_audio_t *sh){
277 int error;
278 unsigned long ConvertedFrames=0;
279 unsigned long ConvertedBytes=0;
280 error=SoundConverterEndConversion(myConverter,NULL,&ConvertedFrames,&ConvertedBytes);
281 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterEndConversion:%i\n",error);
282 error = SoundConverterClose(myConverter);
283 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"SoundConverterClose:%i\n",error);
284 // error = TerminateQTML();
285 // printf("TerminateQTML:%i\n",error);
286 // FreeLibrary( qtml_dll );
287 // qtml_dll = NULL;
288 // FreeLibrary( qtime_qts );
289 // qtime_qts = NULL;
290 // printf("qt dll loader uninit done\n");
291 #ifdef CONFIG_QUICKTIME
292 ExitMovies();
293 #endif
296 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
297 int error;
298 unsigned long FramesToGet=0; //how many frames the demuxer has to get
299 unsigned long InputBufferSize=0; //size of the input buffer
300 unsigned long ConvertedFrames=0;
301 unsigned long ConvertedBytes=0;
303 FramesToGet=minlen/OutFrameSize;
304 if(FramesToGet*OutFrameSize<minlen &&
305 (FramesToGet+1)*OutFrameSize<=maxlen) ++FramesToGet;
306 if(FramesToGet*InFrameSize>sh->a_in_buffer_size)
307 FramesToGet=sh->a_in_buffer_size/InFrameSize;
309 InputBufferSize=FramesToGet*InFrameSize;
311 // printf("FramesToGet = %li (%li -> %li bytes)\n",FramesToGet,
312 // InputBufferSize, FramesToGet*OutFrameSize);
314 if(InputBufferSize>sh->a_in_buffer_len){
315 int x=demux_read_data(sh->ds,&sh->a_in_buffer[sh->a_in_buffer_len],
316 InputBufferSize-sh->a_in_buffer_len);
317 if(x>0) sh->a_in_buffer_len+=x;
318 if(InputBufferSize>sh->a_in_buffer_len)
319 FramesToGet=sh->a_in_buffer_len/InFrameSize; // not enough data!
322 // printf("\nSoundConverterConvertBuffer(myConv=%p,inbuf=%p,frames=%d,outbuf=%p,&convframes=%p,&convbytes=%p)\n",
323 // myConverter,sh->a_in_buffer,FramesToGet,buf,&ConvertedFrames,&ConvertedBytes);
324 error = SoundConverterConvertBuffer(myConverter,sh->a_in_buffer,
325 FramesToGet,buf,&ConvertedFrames,&ConvertedBytes);
326 // printf("SoundConverterConvertBuffer:%i\n",error);
327 // printf("ConvertedFrames = %li\n",ConvertedFrames);
328 // printf("ConvertedBytes = %li\n",ConvertedBytes);
330 // InputBufferSize=(ConvertedBytes/OutFrameSize)*InFrameSize; // FIXME!!
331 InputBufferSize=FramesToGet*InFrameSize;
332 sh->a_in_buffer_len-=InputBufferSize;
333 if(sh->a_in_buffer_len<0) sh->a_in_buffer_len=0; // should not happen...
334 else if(sh->a_in_buffer_len>0){
335 memcpy(sh->a_in_buffer,&sh->a_in_buffer[InputBufferSize],sh->a_in_buffer_len);
338 return ConvertedBytes;
341 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
342 // various optional functions you MAY implement:
343 return CONTROL_UNKNOWN;