16 #include "ad_internal.h"
17 #include "wine/windef.h"
19 static ad_info_t info
= {
23 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
24 "binary real audio codecs"
29 void *__builtin_new(unsigned long size
) {
33 // required for cook's uninit:
34 void __builtin_delete(void* ize
) {
38 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
42 static unsigned long (*raCloseCodec
)(void*);
43 static unsigned long (*raDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
44 static unsigned long (*raFlush
)(unsigned long,unsigned long,unsigned long);
45 static unsigned long (*raFreeDecoder
)(void*);
46 static void* (*raGetFlavorProperty
)(void*,unsigned long,unsigned long,int*);
47 //static unsigned long (*raGetNumberOfFlavors2)(void);
48 static unsigned long (*raInitDecoder
)(void*, void*);
49 static unsigned long (*raOpenCodec
)(void*);
50 static unsigned long (*raOpenCodec2
)(void*, void*);
51 static unsigned long (*raSetFlavor
)(void*,unsigned long);
52 static void (*raSetDLLAccessPath
)(char*);
53 static void (*raSetPwd
)(char*,char*);
55 static unsigned long WINAPI (*wraCloseCodec
)(void*);
56 static unsigned long WINAPI (*wraDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
57 static unsigned long WINAPI (*wraFlush
)(unsigned long,unsigned long,unsigned long);
58 static unsigned long WINAPI (*wraFreeDecoder
)(void*);
59 static void* WINAPI (*wraGetFlavorProperty
)(void*,unsigned long,unsigned long,int*);
60 static unsigned long WINAPI (*wraInitDecoder
)(void*, void*);
61 static unsigned long WINAPI (*wraOpenCodec
)(void*);
62 static unsigned long WINAPI (*wraOpenCodec2
)(void*, void*);
63 static unsigned long WINAPI (*wraSetFlavor
)(void*,unsigned long);
64 static void WINAPI (*wraSetDLLAccessPath
)(char*);
65 static void WINAPI (*wraSetPwd
)(char*,char*);
67 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
70 static void *rv_handle
= NULL
;
86 Probably the linux .so-s were compiled with old GCC without setting
87 packing, so it adds 2 bytes padding after the quality field.
88 In windows it seems that there's no padding in it.
93 /* linux dlls doesn't need packing */
94 typedef struct /*__attribute__((__packed__))*/ {
99 /* 2bytes padding here, by gcc */
106 /* windows dlls need packed structs (no padding) */
107 typedef struct __attribute__((__packed__
)) {
120 static int load_syms_linux(char *path
)
124 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening shared obj '%s'\n", path
);
125 handle
= dlopen(path
, RTLD_LAZY
);
128 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error: %s\n", dlerror());
132 raCloseCodec
= dlsym(handle
, "RACloseCodec");
133 raDecode
= dlsym(handle
, "RADecode");
134 raFlush
= dlsym(handle
, "RAFlush");
135 raFreeDecoder
= dlsym(handle
, "RAFreeDecoder");
136 raGetFlavorProperty
= dlsym(handle
, "RAGetFlavorProperty");
137 raOpenCodec
= dlsym(handle
, "RAOpenCodec");
138 raOpenCodec2
= dlsym(handle
, "RAOpenCodec2");
139 raInitDecoder
= dlsym(handle
, "RAInitDecoder");
140 raSetFlavor
= dlsym(handle
, "RASetFlavor");
141 raSetDLLAccessPath
= dlsym(handle
, "SetDLLAccessPath");
142 raSetPwd
= dlsym(handle
, "RASetPwd"); // optional, used by SIPR
144 if (raCloseCodec
&& raDecode
&& /*raFlush && */raFreeDecoder
&&
145 raGetFlavorProperty
&& (raOpenCodec
||raOpenCodec2
) && raSetFlavor
&&
146 /*raSetDLLAccessPath &&*/ raInitDecoder
)
152 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
161 #include "loader/ldt_keeper.h"
163 void* WINAPI
LoadLibraryA(char* name
);
164 void* WINAPI
GetProcAddress(void* handle
,char *func
);
165 int WINAPI
FreeLibrary(void *handle
);
167 static int load_syms_windows(char *path
)
171 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening win32 dll '%s'\n", path
);
175 handle
= LoadLibraryA(path
);
178 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error loading dll\n");
182 wraCloseCodec
= GetProcAddress(handle
, "RACloseCodec");
183 wraDecode
= GetProcAddress(handle
, "RADecode");
184 wraFlush
= GetProcAddress(handle
, "RAFlush");
185 wraFreeDecoder
= GetProcAddress(handle
, "RAFreeDecoder");
186 wraGetFlavorProperty
= GetProcAddress(handle
, "RAGetFlavorProperty");
187 wraOpenCodec
= GetProcAddress(handle
, "RAOpenCodec");
188 wraOpenCodec2
= GetProcAddress(handle
, "RAOpenCodec2");
189 wraInitDecoder
= GetProcAddress(handle
, "RAInitDecoder");
190 wraSetFlavor
= GetProcAddress(handle
, "RASetFlavor");
191 wraSetDLLAccessPath
= GetProcAddress(handle
, "SetDLLAccessPath");
192 wraSetPwd
= GetProcAddress(handle
, "RASetPwd"); // optional, used by SIPR
194 if (wraCloseCodec
&& wraDecode
&& /*wraFlush && */wraFreeDecoder
&&
195 wraGetFlavorProperty
&& (wraOpenCodec
|| wraOpenCodec2
) && wraSetFlavor
&&
196 /*wraSetDLLAccessPath &&*/ wraInitDecoder
)
203 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
211 static int preinit(sh_audio_t
*sh
){
212 // let's check if the driver is available, return 0 if not.
213 // (you should do that if you use external lib(s) which is optional)
219 path
= malloc(strlen(REALCODEC_PATH
)+strlen(sh
->codec
->dll
)+2);
221 sprintf(path
, REALCODEC_PATH
"/%s", sh
->codec
->dll
);
223 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
224 then try to load the windows ones */
227 if (strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
230 if (!load_syms_windows(sh
->codec
->dll
))
233 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MissingDLLcodec
, sh
->codec
->dll
);
234 mp_msg(MSGT_DECVIDEO
, MSGL_HINT
, "Read the RealAudio section of the DOCS!\n");
240 if((raSetDLLAccessPath
&& dll_type
== 0) || (wraSetDLLAccessPath
&& dll_type
== 1)){
242 if(raSetDLLAccessPath
){
246 path
= realloc(path
, strlen(REALCODEC_PATH
) + 13);
247 sprintf(path
, "DT_Codecs=" REALCODEC_PATH
);
248 if(path
[strlen(path
)-1]!='/'){
249 path
[strlen(path
)+1]=0;
250 path
[strlen(path
)]='/';
252 path
[strlen(path
)+1]=0;
256 for (i
=0; i
< strlen(path
); i
++)
257 if (path
[i
] == '/') path
[i
] = '\\';
258 wraSetDLLAccessPath(path
);
262 raSetDLLAccessPath(path
);
268 result
=wraOpenCodec2(&sh
->context
,REALCODEC_PATH
"\\");
270 result
=wraOpenCodec(&sh
->context
);
274 result
=raOpenCodec2(&sh
->context
,REALCODEC_PATH
"/");
276 result
=raOpenCodec(&sh
->context
);
278 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder open failed, error code: 0x%X\n",result
);
281 // printf("opencodec ok (result: %x)\n", result);
282 free(path
); /* after this it isn't used anymore */
284 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
285 sh
->samplesize
=sh
->wf
->wBitsPerSample
/8;
286 sh
->channels
=sh
->wf
->nChannels
;
289 ra_init_t init_data
={
290 sh
->wf
->nSamplesPerSec
,
291 sh
->wf
->wBitsPerSample
,
294 sh
->wf
->nBlockAlign
, // subpacket size
295 sh
->wf
->nBlockAlign
, // coded frame size
296 sh
->wf
->cbSize
, // codec data length
297 (char*)(sh
->wf
+1) // extras
300 wra_init_t winit_data
={
301 sh
->wf
->nSamplesPerSec
,
302 sh
->wf
->wBitsPerSample
,
305 sh
->wf
->nBlockAlign
, // subpacket size
306 sh
->wf
->nBlockAlign
, // coded frame size
307 sh
->wf
->cbSize
, // codec data length
308 (char*)(sh
->wf
+1) // extras
313 result
=wraInitDecoder(sh
->context
,&winit_data
);
316 result
=raInitDecoder(sh
->context
,&init_data
);
319 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder init failed, error code: 0x%X\n",result
);
322 // printf("initdecoder ok (result: %x)\n", result);
326 if((raSetPwd
&& dll_type
== 0) || (wraSetPwd
&& dll_type
== 1)){
333 wraSetPwd(sh
->context
,"Ardubancel Quazanga");
336 raSetPwd(sh
->context
,"Ardubancel Quazanga"); // set password... lol.
339 if (sh
->format
== mmioFOURCC('s','i','p','r')) {
342 if (sh
->wf
->nAvgBytesPerSec
> 1531)
344 else if (sh
->wf
->nAvgBytesPerSec
> 937)
346 else if (sh
->wf
->nAvgBytesPerSec
> 719)
350 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Got sipr flavor %d from bitrate %d\n",flavor
, sh
->wf
->nAvgBytesPerSec
);
354 result
=wraSetFlavor(sh
->context
,flavor
);
357 result
=raSetFlavor(sh
->context
,flavor
);
359 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder flavor setup failed, error code: 0x%X\n",result
);
364 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
366 sh
->audio_out_minsize
=128000; // no idea how to get... :(
367 sh
->audio_in_minsize
= sh
->wf
->nBlockAlign
;
369 return 1; // return values: 1=OK 0=ERROR
372 static int init(sh_audio_t
*sh_audio
){
373 // initialize the decoder, set tables etc...
375 // you can store HANDLE or private struct pointer at sh->context
376 // you can access WAVEFORMATEX header at sh->wf
378 // set sample format/rate parameters if you didn't do it in preinit() yet.
380 return 1; // return values: 1=OK 0=ERROR
383 static void uninit(sh_audio_t
*sh
){
384 // uninit the decoder etc...
385 // again: you don't have to free() a_in_buffer here! it's done by the core.
389 if (wraFreeDecoder
) wraFreeDecoder(sh
->context
);
390 if (wraCloseCodec
) wraCloseCodec(sh
->context
);
394 if (raFreeDecoder
) raFreeDecoder(sh
->context
);
395 if (raCloseCodec
) raCloseCodec(sh
->context
);
401 if (rv_handle
) FreeLibrary(rv_handle
);
404 // this dlclose() causes some memory corruption, and crashes soon (in caller):
405 // if (rv_handle) dlclose(rv_handle);
409 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
413 if(sh
->a_in_buffer_len
<=0){
415 demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->wf
->nBlockAlign
);
416 sh
->a_in_buffer_size
=
417 sh
->a_in_buffer_len
=sh
->wf
->nBlockAlign
;
422 result
=wraDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
426 result
=raDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
428 sh
->a_in_buffer_len
-=sh
->wf
->nBlockAlign
;
430 // printf("radecode: %d bytes, res=0x%X \n",len,result);
432 return len
; // return value: number of _bytes_ written to output buffer,
433 // or -1 for EOF (or uncorrectable error)
436 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
437 // various optional functions you MAY implement:
439 case ADCTRL_RESYNC_STREAM
:
440 // it is called once after seeking, to resync.
441 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
443 case ADCTRL_SKIP_FRAME
:
444 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
445 // of audio data - used to sync audio to video after seeking
446 // if you don't return CONTROL_TRUE, it will defaults to:
447 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
450 return CONTROL_UNKNOWN
;