14 #include "ad_internal.h"
15 #include "loader/wine/windef.h"
17 static const ad_info_t info
= {
21 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
22 "binary real audio codecs"
27 void *__builtin_new(unsigned long size
) {
31 // required for cook's uninit:
32 void __builtin_delete(void* ize
) {
36 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
40 static unsigned long (*raCloseCodec
)(void*);
41 static unsigned long (*raDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
42 static unsigned long (*raFreeDecoder
)(void*);
43 //static unsigned long (*raGetNumberOfFlavors2)(void);
44 static unsigned long (*raInitDecoder
)(void*, void*);
45 static unsigned long (*raOpenCodec
)(void*);
46 static unsigned long (*raOpenCodec2
)(void*, void*);
47 static unsigned long (*raSetFlavor
)(void*,unsigned long);
48 static void (*raSetDLLAccessPath
)(char*);
49 static void (*raSetPwd
)(char*,char*);
50 #ifdef CONFIG_WIN32DLL
51 static unsigned long WINAPI (*wraCloseCodec
)(void*);
52 static unsigned long WINAPI (*wraDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
53 static unsigned long WINAPI (*wraFreeDecoder
)(void*);
54 static unsigned long WINAPI (*wraInitDecoder
)(void*, void*);
55 static unsigned long WINAPI (*wraOpenCodec
)(void*);
56 static unsigned long WINAPI (*wraOpenCodec2
)(void*, void*);
57 static unsigned long WINAPI (*wraSetFlavor
)(void*,unsigned long);
58 static void WINAPI (*wraSetDLLAccessPath
)(char*);
59 static void WINAPI (*wraSetPwd
)(char*,char*);
61 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
64 static void *rv_handle
= NULL
;
80 Probably the linux .so-s were compiled with old GCC without setting
81 packing, so it adds 2 bytes padding after the quality field.
82 In windows it seems that there's no padding in it.
87 /* linux dlls doesn't need packing */
88 typedef struct /*__attribute__((__packed__))*/ {
93 /* 2bytes padding here, by gcc */
100 /* windows dlls need packed structs (no padding) */
101 typedef struct __attribute__((__packed__
)) {
114 static int load_syms_linux(char *path
)
118 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening shared obj '%s'\n", path
);
119 handle
= dlopen(path
, RTLD_LAZY
);
122 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error: %s\n", dlerror());
126 raCloseCodec
= dlsym(handle
, "RACloseCodec");
127 raDecode
= dlsym(handle
, "RADecode");
128 raFreeDecoder
= dlsym(handle
, "RAFreeDecoder");
129 raOpenCodec
= dlsym(handle
, "RAOpenCodec");
130 raOpenCodec2
= dlsym(handle
, "RAOpenCodec2");
131 raInitDecoder
= dlsym(handle
, "RAInitDecoder");
132 raSetFlavor
= dlsym(handle
, "RASetFlavor");
133 raSetDLLAccessPath
= dlsym(handle
, "SetDLLAccessPath");
134 raSetPwd
= dlsym(handle
, "RASetPwd"); // optional, used by SIPR
136 if (raCloseCodec
&& raDecode
&& raFreeDecoder
&&
137 (raOpenCodec
||raOpenCodec2
) && raSetFlavor
&&
138 /*raSetDLLAccessPath &&*/ raInitDecoder
)
144 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
150 #ifdef CONFIG_WIN32DLL
153 #include "loader/ldt_keeper.h"
155 void* WINAPI
LoadLibraryA(char* name
);
156 void* WINAPI
GetProcAddress(void* handle
,char *func
);
157 int WINAPI
FreeLibrary(void *handle
);
159 static int load_syms_windows(char *path
)
163 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening win32 dll '%s'\n", path
);
167 handle
= LoadLibraryA(path
);
170 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error loading dll\n");
174 wraCloseCodec
= GetProcAddress(handle
, "RACloseCodec");
175 wraDecode
= GetProcAddress(handle
, "RADecode");
176 wraFreeDecoder
= GetProcAddress(handle
, "RAFreeDecoder");
177 wraOpenCodec
= GetProcAddress(handle
, "RAOpenCodec");
178 wraOpenCodec2
= GetProcAddress(handle
, "RAOpenCodec2");
179 wraInitDecoder
= GetProcAddress(handle
, "RAInitDecoder");
180 wraSetFlavor
= GetProcAddress(handle
, "RASetFlavor");
181 wraSetDLLAccessPath
= GetProcAddress(handle
, "SetDLLAccessPath");
182 wraSetPwd
= GetProcAddress(handle
, "RASetPwd"); // optional, used by SIPR
184 if (wraCloseCodec
&& wraDecode
&& wraFreeDecoder
&&
185 (wraOpenCodec
|| wraOpenCodec2
) && wraSetFlavor
&&
186 /*wraSetDLLAccessPath &&*/ wraInitDecoder
)
193 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
201 static int preinit(sh_audio_t
*sh
){
202 // let's check if the driver is available, return 0 if not.
203 // (you should do that if you use external lib(s) which is optional)
207 path
= malloc(strlen(REALCODEC_PATH
)+strlen(sh
->codec
->dll
)+2);
209 sprintf(path
, REALCODEC_PATH
"/%s", sh
->codec
->dll
);
211 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
212 then try to load the windows ones */
215 if (strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
217 #ifdef CONFIG_WIN32DLL
218 if (!load_syms_windows(sh
->codec
->dll
))
221 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "ERROR: Could not open required DirectShow codec %s.\n", sh
->codec
->dll
);
222 mp_msg(MSGT_DECVIDEO
, MSGL_HINT
, "Read the RealAudio section of the DOCS!\n");
227 #ifdef CONFIG_WIN32DLL
228 if((raSetDLLAccessPath
&& dll_type
== 0) || (wraSetDLLAccessPath
&& dll_type
== 1)){
230 if(raSetDLLAccessPath
){
233 path
= realloc(path
, strlen(REALCODEC_PATH
) + 13);
234 sprintf(path
, "DT_Codecs=" REALCODEC_PATH
);
235 if(path
[strlen(path
)-1]!='/'){
236 path
[strlen(path
)+1]=0;
237 path
[strlen(path
)]='/';
239 path
[strlen(path
)+1]=0;
240 #ifdef CONFIG_WIN32DLL
244 for (i
=0; i
< strlen(path
); i
++)
245 if (path
[i
] == '/') path
[i
] = '\\';
246 wraSetDLLAccessPath(path
);
250 raSetDLLAccessPath(path
);
253 #ifdef CONFIG_WIN32DLL
256 result
=wraOpenCodec2(&sh
->context
,REALCODEC_PATH
"\\");
258 result
=wraOpenCodec(&sh
->context
);
262 result
=raOpenCodec2(&sh
->context
,REALCODEC_PATH
"/");
264 result
=raOpenCodec(&sh
->context
);
266 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder open failed, error code: 0x%X\n",result
);
269 // printf("opencodec ok (result: %x)\n", result);
270 free(path
); /* after this it isn't used anymore */
272 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
273 sh
->samplesize
=sh
->wf
->wBitsPerSample
/8;
274 sh
->channels
=sh
->wf
->nChannels
;
277 ra_init_t init_data
={
278 sh
->wf
->nSamplesPerSec
,
279 sh
->wf
->wBitsPerSample
,
282 sh
->wf
->nBlockAlign
, // subpacket size
283 sh
->wf
->nBlockAlign
, // coded frame size
284 sh
->wf
->cbSize
, // codec data length
285 (char*)(sh
->wf
+1) // extras
287 #ifdef CONFIG_WIN32DLL
288 wra_init_t winit_data
={
289 sh
->wf
->nSamplesPerSec
,
290 sh
->wf
->wBitsPerSample
,
293 sh
->wf
->nBlockAlign
, // subpacket size
294 sh
->wf
->nBlockAlign
, // coded frame size
295 sh
->wf
->cbSize
, // codec data length
296 (char*)(sh
->wf
+1) // extras
299 #ifdef CONFIG_WIN32DLL
301 result
=wraInitDecoder(sh
->context
,&winit_data
);
304 result
=raInitDecoder(sh
->context
,&init_data
);
307 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder init failed, error code: 0x%X\n",result
);
310 // printf("initdecoder ok (result: %x)\n", result);
313 #ifdef CONFIG_WIN32DLL
314 if((raSetPwd
&& dll_type
== 0) || (wraSetPwd
&& dll_type
== 1)){
319 #ifdef CONFIG_WIN32DLL
321 wraSetPwd(sh
->context
,"Ardubancel Quazanga");
324 raSetPwd(sh
->context
,"Ardubancel Quazanga"); // set password... lol.
327 if (sh
->format
== mmioFOURCC('s','i','p','r')) {
330 if (sh
->wf
->nAvgBytesPerSec
> 1531)
332 else if (sh
->wf
->nAvgBytesPerSec
> 937)
334 else if (sh
->wf
->nAvgBytesPerSec
> 719)
338 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Got sipr flavor %d from bitrate %d\n",flavor
, sh
->wf
->nAvgBytesPerSec
);
340 #ifdef CONFIG_WIN32DLL
342 result
=wraSetFlavor(sh
->context
,flavor
);
345 result
=raSetFlavor(sh
->context
,flavor
);
347 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder flavor setup failed, error code: 0x%X\n",result
);
352 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
354 sh
->audio_out_minsize
=128000; // no idea how to get... :(
355 sh
->audio_in_minsize
= sh
->wf
->nBlockAlign
;
357 return 1; // return values: 1=OK 0=ERROR
360 static int init(sh_audio_t
*sh_audio
){
361 // initialize the decoder, set tables etc...
363 // you can store HANDLE or private struct pointer at sh->context
364 // you can access WAVEFORMATEX header at sh->wf
366 // set sample format/rate parameters if you didn't do it in preinit() yet.
368 return 1; // return values: 1=OK 0=ERROR
371 static void uninit(sh_audio_t
*sh
){
372 // uninit the decoder etc...
373 // again: you don't have to free() a_in_buffer here! it's done by the core.
374 #ifdef CONFIG_WIN32DLL
377 if (wraFreeDecoder
) wraFreeDecoder(sh
->context
);
378 if (wraCloseCodec
) wraCloseCodec(sh
->context
);
382 if (raFreeDecoder
) raFreeDecoder(sh
->context
);
383 if (raCloseCodec
) raCloseCodec(sh
->context
);
386 #ifdef CONFIG_WIN32DLL
389 if (rv_handle
) FreeLibrary(rv_handle
);
392 // this dlclose() causes some memory corruption, and crashes soon (in caller):
393 // if (rv_handle) dlclose(rv_handle);
397 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
401 if(sh
->a_in_buffer_len
<=0){
405 demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->wf
->nBlockAlign
);
406 sh
->a_in_buffer_size
=
407 sh
->a_in_buffer_len
=sh
->wf
->nBlockAlign
;
410 #ifdef CONFIG_WIN32DLL
412 result
=wraDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
416 result
=raDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
418 sh
->a_in_buffer_len
-=sh
->wf
->nBlockAlign
;
420 // printf("radecode: %d bytes, res=0x%X \n",len,result);
422 return len
; // return value: number of _bytes_ written to output buffer,
423 // or -1 for EOF (or uncorrectable error)
426 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
427 // various optional functions you MAY implement:
429 case ADCTRL_RESYNC_STREAM
:
430 // it is called once after seeking, to resync.
431 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
433 case ADCTRL_SKIP_FRAME
:
434 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
435 // of audio data - used to sync audio to video after seeking
436 // if you don't return CONTROL_TRUE, it will defaults to:
437 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
440 return CONTROL_UNKNOWN
;