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.
31 #include "ad_internal.h"
32 #include "loader/wine/windef.h"
34 static const ad_info_t info
= {
38 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
39 "binary real audio codecs"
44 void *__builtin_new(unsigned long size
) {
48 // required for cook's uninit:
49 void __builtin_delete(void* ize
) {
53 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
57 static unsigned long (*raCloseCodec
)(void*);
58 static unsigned long (*raDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
59 static unsigned long (*raFreeDecoder
)(void*);
60 //static unsigned long (*raGetNumberOfFlavors2)(void);
61 static unsigned long (*raInitDecoder
)(void*, void*);
62 static unsigned long (*raOpenCodec
)(void*);
63 static unsigned long (*raOpenCodec2
)(void*, void*);
64 static unsigned long (*raSetFlavor
)(void*,unsigned long);
65 static void (*raSetDLLAccessPath
)(char*);
66 static void (*raSetPwd
)(char*,char*);
67 #ifdef CONFIG_WIN32DLL
68 static unsigned long WINAPI (*wraCloseCodec
)(void*);
69 static unsigned long WINAPI (*wraDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
70 static unsigned long WINAPI (*wraFreeDecoder
)(void*);
71 static unsigned long WINAPI (*wraInitDecoder
)(void*, void*);
72 static unsigned long WINAPI (*wraOpenCodec
)(void*);
73 static unsigned long WINAPI (*wraOpenCodec2
)(void*, void*);
74 static unsigned long WINAPI (*wraSetFlavor
)(void*,unsigned long);
75 static void WINAPI (*wraSetDLLAccessPath
)(char*);
76 static void WINAPI (*wraSetPwd
)(char*,char*);
78 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
81 static void *rv_handle
= NULL
;
97 Probably the linux .so-s were compiled with old GCC without setting
98 packing, so it adds 2 bytes padding after the quality field.
99 In windows it seems that there's no padding in it.
104 /* linux dlls doesn't need packing */
105 typedef struct /*__attribute__((__packed__))*/ {
110 /* 2bytes padding here, by gcc */
117 /* windows dlls need packed structs (no padding) */
118 typedef struct __attribute__((__packed__
)) {
131 static int load_syms_linux(char *path
)
135 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening shared obj '%s'\n", path
);
136 handle
= dlopen(path
, RTLD_LAZY
);
139 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error: %s\n", dlerror());
143 raCloseCodec
= dlsym(handle
, "RACloseCodec");
144 raDecode
= dlsym(handle
, "RADecode");
145 raFreeDecoder
= dlsym(handle
, "RAFreeDecoder");
146 raOpenCodec
= dlsym(handle
, "RAOpenCodec");
147 raOpenCodec2
= dlsym(handle
, "RAOpenCodec2");
148 raInitDecoder
= dlsym(handle
, "RAInitDecoder");
149 raSetFlavor
= dlsym(handle
, "RASetFlavor");
150 raSetDLLAccessPath
= dlsym(handle
, "SetDLLAccessPath");
151 raSetPwd
= dlsym(handle
, "RASetPwd"); // optional, used by SIPR
153 if (raCloseCodec
&& raDecode
&& raFreeDecoder
&&
154 (raOpenCodec
||raOpenCodec2
) && raSetFlavor
&&
155 /*raSetDLLAccessPath &&*/ raInitDecoder
)
161 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
167 #ifdef CONFIG_WIN32DLL
170 #include "loader/ldt_keeper.h"
172 void* WINAPI
LoadLibraryA(char* name
);
173 void* WINAPI
GetProcAddress(void* handle
,char *func
);
174 int WINAPI
FreeLibrary(void *handle
);
176 static int load_syms_windows(char *path
)
180 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening win32 dll '%s'\n", path
);
184 handle
= LoadLibraryA(path
);
187 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error loading dll\n");
191 wraCloseCodec
= GetProcAddress(handle
, "RACloseCodec");
192 wraDecode
= GetProcAddress(handle
, "RADecode");
193 wraFreeDecoder
= GetProcAddress(handle
, "RAFreeDecoder");
194 wraOpenCodec
= GetProcAddress(handle
, "RAOpenCodec");
195 wraOpenCodec2
= GetProcAddress(handle
, "RAOpenCodec2");
196 wraInitDecoder
= GetProcAddress(handle
, "RAInitDecoder");
197 wraSetFlavor
= GetProcAddress(handle
, "RASetFlavor");
198 wraSetDLLAccessPath
= GetProcAddress(handle
, "SetDLLAccessPath");
199 wraSetPwd
= GetProcAddress(handle
, "RASetPwd"); // optional, used by SIPR
201 if (wraCloseCodec
&& wraDecode
&& wraFreeDecoder
&&
202 (wraOpenCodec
|| wraOpenCodec2
) && wraSetFlavor
&&
203 /*wraSetDLLAccessPath &&*/ wraInitDecoder
)
210 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
218 static int preinit(sh_audio_t
*sh
){
219 // let's check if the driver is available, return 0 if not.
220 // (you should do that if you use external lib(s) which is optional)
224 path
= malloc(strlen(codec_path
) + strlen(sh
->codec
->dll
) + 2);
226 sprintf(path
, "%s/%s", codec_path
, sh
->codec
->dll
);
228 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
229 then try to load the windows ones */
232 if (strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
234 #ifdef CONFIG_WIN32DLL
235 if (!load_syms_windows(sh
->codec
->dll
))
238 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "ERROR: Could not open required DirectShow codec %s.\n", sh
->codec
->dll
);
239 mp_msg(MSGT_DECVIDEO
, MSGL_HINT
, "Read the RealAudio section of the DOCS!\n");
244 #ifdef CONFIG_WIN32DLL
245 if((raSetDLLAccessPath
&& dll_type
== 0) || (wraSetDLLAccessPath
&& dll_type
== 1)){
247 if(raSetDLLAccessPath
){
250 path
= realloc(path
, strlen(codec_path
) + 13);
251 sprintf(path
, "DT_Codecs=%s", codec_path
);
252 if(path
[strlen(path
)-1]!='/'){
253 path
[strlen(path
)+1]=0;
254 path
[strlen(path
)]='/';
256 path
[strlen(path
)+1]=0;
257 #ifdef CONFIG_WIN32DLL
261 for (i
=0; i
< strlen(path
); i
++)
262 if (path
[i
] == '/') path
[i
] = '\\';
263 wraSetDLLAccessPath(path
);
267 raSetDLLAccessPath(path
);
270 #ifdef CONFIG_WIN32DLL
273 sprintf(path
, "%s\\", codec_path
);
274 result
= wraOpenCodec2(&sh
->context
, path
);
276 result
=wraOpenCodec(&sh
->context
);
280 sprintf(path
, "%s/", codec_path
);
281 result
= raOpenCodec2(&sh
->context
, path
);
283 result
=raOpenCodec(&sh
->context
);
285 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder open failed, error code: 0x%X\n",result
);
288 // printf("opencodec ok (result: %x)\n", result);
289 free(path
); /* after this it isn't used anymore */
291 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
292 sh
->samplesize
=sh
->wf
->wBitsPerSample
/8;
293 sh
->channels
=sh
->wf
->nChannels
;
296 ra_init_t init_data
={
297 sh
->wf
->nSamplesPerSec
,
298 sh
->wf
->wBitsPerSample
,
301 sh
->wf
->nBlockAlign
, // subpacket size
302 sh
->wf
->nBlockAlign
, // coded frame size
303 sh
->wf
->cbSize
, // codec data length
304 (char*)(sh
->wf
+1) // extras
306 #ifdef CONFIG_WIN32DLL
307 wra_init_t winit_data
={
308 sh
->wf
->nSamplesPerSec
,
309 sh
->wf
->wBitsPerSample
,
312 sh
->wf
->nBlockAlign
, // subpacket size
313 sh
->wf
->nBlockAlign
, // coded frame size
314 sh
->wf
->cbSize
, // codec data length
315 (char*)(sh
->wf
+1) // extras
318 #ifdef CONFIG_WIN32DLL
320 result
=wraInitDecoder(sh
->context
,&winit_data
);
323 result
=raInitDecoder(sh
->context
,&init_data
);
326 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder init failed, error code: 0x%X\n",result
);
329 // printf("initdecoder ok (result: %x)\n", result);
332 #ifdef CONFIG_WIN32DLL
333 if((raSetPwd
&& dll_type
== 0) || (wraSetPwd
&& dll_type
== 1)){
338 #ifdef CONFIG_WIN32DLL
340 wraSetPwd(sh
->context
,"Ardubancel Quazanga");
343 raSetPwd(sh
->context
,"Ardubancel Quazanga"); // set password... lol.
346 if (sh
->format
== mmioFOURCC('s','i','p','r')) {
349 if (sh
->wf
->nAvgBytesPerSec
> 1531)
351 else if (sh
->wf
->nAvgBytesPerSec
> 937)
353 else if (sh
->wf
->nAvgBytesPerSec
> 719)
357 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Got sipr flavor %d from bitrate %d\n",flavor
, sh
->wf
->nAvgBytesPerSec
);
359 #ifdef CONFIG_WIN32DLL
361 result
=wraSetFlavor(sh
->context
,flavor
);
364 result
=raSetFlavor(sh
->context
,flavor
);
366 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder flavor setup failed, error code: 0x%X\n",result
);
371 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
373 sh
->audio_out_minsize
=128000; // no idea how to get... :(
374 sh
->audio_in_minsize
= sh
->wf
->nBlockAlign
;
376 return 1; // return values: 1=OK 0=ERROR
379 static int init(sh_audio_t
*sh_audio
){
380 // initialize the decoder, set tables etc...
382 // you can store HANDLE or private struct pointer at sh->context
383 // you can access WAVEFORMATEX header at sh->wf
385 // set sample format/rate parameters if you didn't do it in preinit() yet.
387 return 1; // return values: 1=OK 0=ERROR
390 static void uninit(sh_audio_t
*sh
){
391 // uninit the decoder etc...
392 // again: you don't have to free() a_in_buffer here! it's done by the core.
393 #ifdef CONFIG_WIN32DLL
396 if (wraFreeDecoder
) wraFreeDecoder(sh
->context
);
397 if (wraCloseCodec
) wraCloseCodec(sh
->context
);
401 if (raFreeDecoder
) raFreeDecoder(sh
->context
);
402 if (raCloseCodec
) raCloseCodec(sh
->context
);
405 #ifdef CONFIG_WIN32DLL
408 if (rv_handle
) FreeLibrary(rv_handle
);
411 // this dlclose() causes some memory corruption, and crashes soon (in caller):
412 // if (rv_handle) dlclose(rv_handle);
416 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
420 if(sh
->a_in_buffer_len
<=0){
424 demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->wf
->nBlockAlign
);
425 sh
->a_in_buffer_size
=
426 sh
->a_in_buffer_len
=sh
->wf
->nBlockAlign
;
429 #ifdef CONFIG_WIN32DLL
431 result
=wraDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
435 result
=raDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
437 sh
->a_in_buffer_len
-=sh
->wf
->nBlockAlign
;
439 // printf("radecode: %d bytes, res=0x%X \n",len,result);
441 return len
; // return value: number of _bytes_ written to output buffer,
442 // or -1 for EOF (or uncorrectable error)
445 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
446 // various optional functions you MAY implement:
448 case ADCTRL_RESYNC_STREAM
:
449 // it is called once after seeking, to resync.
450 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
452 case ADCTRL_SKIP_FRAME
:
453 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
454 // of audio data - used to sync audio to video after seeking
455 // if you don't return CONTROL_TRUE, it will defaults to:
456 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
459 return CONTROL_UNKNOWN
;