Makefile: Fix osdep/mplayer.rc.o vs. osdep/mplayer-rc.o typo
[mplayer.git] / libmpcodecs / ad_qtaudio.c
blobd171c1620ce83e72b711a41b6c4e9a4223670682
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
191 #ifdef CONFIG_QUICKTIME
192 EnterMovies();
193 #else
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);
199 if(error){
200 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"InitializeQTML:%i\n",error);
201 return 0;
203 #endif
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);
218 if(error) return 0;
220 if(sh->codecdata){
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);
242 if(error) return 0;
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){
255 // MACE 3:1
256 sh->ds->ss_div = 2*3; // 1 samples/packet
257 sh->ds->ss_mul = sh->channels*2*1; // 1 bytes/packet
258 } else
259 if(sh->format==0x3643414D){
260 // MACE 6:1
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){
274 int error;
275 unsigned long ConvertedFrames=0;
276 unsigned long ConvertedBytes=0;
278 #ifdef WIN32_LOADER
279 Setup_FS_Segment();
280 #endif
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 );
289 // qtml_dll = NULL;
290 // FreeLibrary( qtime_qts );
291 // qtime_qts = NULL;
292 // printf("qt dll loader uninit done\n");
293 #ifdef CONFIG_QUICKTIME
294 ExitMovies();
295 #endif
298 static int decode_audio(sh_audio_t *sh,unsigned char *buf,int minlen,int maxlen){
299 int error;
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;
305 #ifdef WIN32_LOADER
306 Setup_FS_Segment();
307 #endif
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;