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 /* These functions are required for loading Real binary libs.
45 * Add forward declarations to avoid warnings with -Wmissing-prototypes. */
46 void *__builtin_new(unsigned long size
);
47 void __builtin_delete(void *ize
);
48 void *__builtin_vec_new(unsigned long size
);
49 void __builtin_vec_delete(void *mem
);
50 void __pure_virtual(void);
52 void *__builtin_new(unsigned long size
)
57 void __builtin_delete(void* ize
)
62 void *__builtin_vec_new(unsigned long size
)
67 void __builtin_vec_delete(void *mem
)
72 void __pure_virtual(void)
74 printf("FATAL: __pure_virtual() called!\n");
78 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
79 void ___brk_addr(void);
80 void ___brk_addr(void) {exit(0);}
81 char **__environ
={NULL
};
87 static unsigned long (*raCloseCodec
)(void*);
88 static unsigned long (*raDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
89 static unsigned long (*raFreeDecoder
)(void*);
90 //static unsigned long (*raGetNumberOfFlavors2)(void);
91 static unsigned long (*raInitDecoder
)(void*, void*);
92 static unsigned long (*raOpenCodec
)(void*);
93 static unsigned long (*raOpenCodec2
)(void*, void*);
94 static unsigned long (*raSetFlavor
)(void*,unsigned long);
95 static void (*raSetDLLAccessPath
)(char*);
96 static void (*raSetPwd
)(char*,char*);
97 #ifdef CONFIG_WIN32DLL
98 static unsigned long WINAPI (*wraCloseCodec
)(void*);
99 static unsigned long WINAPI (*wraDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
100 static unsigned long WINAPI (*wraFreeDecoder
)(void*);
101 static unsigned long WINAPI (*wraInitDecoder
)(void*, void*);
102 static unsigned long WINAPI (*wraOpenCodec
)(void*);
103 static unsigned long WINAPI (*wraOpenCodec2
)(void*, void*);
104 static unsigned long WINAPI (*wraSetFlavor
)(void*,unsigned long);
105 static void WINAPI (*wraSetDLLAccessPath
)(char*);
106 static void WINAPI (*wraSetPwd
)(char*,char*);
108 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
111 static void *rv_handle
= NULL
;
127 Probably the linux .so-s were compiled with old GCC without setting
128 packing, so it adds 2 bytes padding after the quality field.
129 In windows it seems that there's no padding in it.
134 /* linux dlls doesn't need packing */
135 typedef struct /*__attribute__((__packed__))*/ {
140 /* 2bytes padding here, by gcc */
147 /* windows dlls need packed structs (no padding) */
148 typedef struct __attribute__((__packed__
)) {
161 static int load_syms_linux(char *path
)
165 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening shared obj '%s'\n", path
);
166 handle
= dlopen(path
, RTLD_LAZY
);
169 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error: %s\n", dlerror());
173 raCloseCodec
= dlsym(handle
, "RACloseCodec");
174 raDecode
= dlsym(handle
, "RADecode");
175 raFreeDecoder
= dlsym(handle
, "RAFreeDecoder");
176 raOpenCodec
= dlsym(handle
, "RAOpenCodec");
177 raOpenCodec2
= dlsym(handle
, "RAOpenCodec2");
178 raInitDecoder
= dlsym(handle
, "RAInitDecoder");
179 raSetFlavor
= dlsym(handle
, "RASetFlavor");
180 raSetDLLAccessPath
= dlsym(handle
, "SetDLLAccessPath");
181 raSetPwd
= dlsym(handle
, "RASetPwd"); // optional, used by SIPR
183 if (raCloseCodec
&& raDecode
&& raFreeDecoder
&&
184 (raOpenCodec
||raOpenCodec2
) && raSetFlavor
&&
185 /*raSetDLLAccessPath &&*/ raInitDecoder
)
191 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
197 #ifdef CONFIG_WIN32DLL
200 #include "loader/ldt_keeper.h"
202 void* WINAPI
LoadLibraryA(char* name
);
203 void* WINAPI
GetProcAddress(void* handle
,char *func
);
204 int WINAPI
FreeLibrary(void *handle
);
206 static int load_syms_windows(char *path
)
210 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening win32 dll '%s'\n", path
);
214 handle
= LoadLibraryA(path
);
217 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error loading dll\n");
221 wraCloseCodec
= GetProcAddress(handle
, "RACloseCodec");
222 wraDecode
= GetProcAddress(handle
, "RADecode");
223 wraFreeDecoder
= GetProcAddress(handle
, "RAFreeDecoder");
224 wraOpenCodec
= GetProcAddress(handle
, "RAOpenCodec");
225 wraOpenCodec2
= GetProcAddress(handle
, "RAOpenCodec2");
226 wraInitDecoder
= GetProcAddress(handle
, "RAInitDecoder");
227 wraSetFlavor
= GetProcAddress(handle
, "RASetFlavor");
228 wraSetDLLAccessPath
= GetProcAddress(handle
, "SetDLLAccessPath");
229 wraSetPwd
= GetProcAddress(handle
, "RASetPwd"); // optional, used by SIPR
231 if (wraCloseCodec
&& wraDecode
&& wraFreeDecoder
&&
232 (wraOpenCodec
|| wraOpenCodec2
) && wraSetFlavor
&&
233 /*wraSetDLLAccessPath &&*/ wraInitDecoder
)
240 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
248 static int preinit(sh_audio_t
*sh
){
249 // let's check if the driver is available, return 0 if not.
250 // (you should do that if you use external lib(s) which is optional)
254 path
= malloc(strlen(BINARY_CODECS_PATH
) + strlen(sh
->codec
->dll
) + 2);
256 sprintf(path
, BINARY_CODECS_PATH
"/%s", sh
->codec
->dll
);
258 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
259 then try to load the windows ones */
262 if (strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
264 #ifdef CONFIG_WIN32DLL
265 if (!load_syms_windows(sh
->codec
->dll
))
268 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MissingDLLcodec
, sh
->codec
->dll
);
269 mp_msg(MSGT_DECVIDEO
, MSGL_HINT
, "Read the RealAudio section of the DOCS!\n");
274 #ifdef CONFIG_WIN32DLL
275 if((raSetDLLAccessPath
&& dll_type
== 0) || (wraSetDLLAccessPath
&& dll_type
== 1)){
277 if(raSetDLLAccessPath
){
280 path
= realloc(path
, strlen(BINARY_CODECS_PATH
) + 13);
281 sprintf(path
, "DT_Codecs=" BINARY_CODECS_PATH
);
282 if(path
[strlen(path
)-1]!='/'){
283 path
[strlen(path
)+1]=0;
284 path
[strlen(path
)]='/';
286 path
[strlen(path
)+1]=0;
287 #ifdef CONFIG_WIN32DLL
291 for (i
=0; i
< strlen(path
); i
++)
292 if (path
[i
] == '/') path
[i
] = '\\';
293 wraSetDLLAccessPath(path
);
297 raSetDLLAccessPath(path
);
300 #ifdef CONFIG_WIN32DLL
303 result
= wraOpenCodec2(&sh
->context
, BINARY_CODECS_PATH
"\\");
305 result
=wraOpenCodec(&sh
->context
);
309 result
= raOpenCodec2(&sh
->context
, BINARY_CODECS_PATH
"/");
311 result
=raOpenCodec(&sh
->context
);
313 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder open failed, error code: 0x%X\n",result
);
316 // printf("opencodec ok (result: %x)\n", result);
317 free(path
); /* after this it isn't used anymore */
319 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
320 sh
->samplesize
=sh
->wf
->wBitsPerSample
/8;
321 sh
->channels
=sh
->wf
->nChannels
;
324 ra_init_t init_data
={
325 sh
->wf
->nSamplesPerSec
,
326 sh
->wf
->wBitsPerSample
,
329 sh
->wf
->nBlockAlign
, // subpacket size
330 sh
->wf
->nBlockAlign
, // coded frame size
331 sh
->wf
->cbSize
, // codec data length
332 (char*)(sh
->wf
+1) // extras
334 #ifdef CONFIG_WIN32DLL
335 wra_init_t winit_data
={
336 sh
->wf
->nSamplesPerSec
,
337 sh
->wf
->wBitsPerSample
,
340 sh
->wf
->nBlockAlign
, // subpacket size
341 sh
->wf
->nBlockAlign
, // coded frame size
342 sh
->wf
->cbSize
, // codec data length
343 (char*)(sh
->wf
+1) // extras
346 #ifdef CONFIG_WIN32DLL
348 result
=wraInitDecoder(sh
->context
,&winit_data
);
351 result
=raInitDecoder(sh
->context
,&init_data
);
354 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder init failed, error code: 0x%X\n",result
);
357 // printf("initdecoder ok (result: %x)\n", result);
360 #ifdef CONFIG_WIN32DLL
361 if((raSetPwd
&& dll_type
== 0) || (wraSetPwd
&& dll_type
== 1)){
366 #ifdef CONFIG_WIN32DLL
368 wraSetPwd(sh
->context
,"Ardubancel Quazanga");
371 raSetPwd(sh
->context
,"Ardubancel Quazanga"); // set password... lol.
374 if (sh
->format
== mmioFOURCC('s','i','p','r')) {
377 if (sh
->wf
->nAvgBytesPerSec
> 1531)
379 else if (sh
->wf
->nAvgBytesPerSec
> 937)
381 else if (sh
->wf
->nAvgBytesPerSec
> 719)
385 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Got sipr flavor %d from bitrate %d\n",flavor
, sh
->wf
->nAvgBytesPerSec
);
387 #ifdef CONFIG_WIN32DLL
389 result
=wraSetFlavor(sh
->context
,flavor
);
392 result
=raSetFlavor(sh
->context
,flavor
);
394 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder flavor setup failed, error code: 0x%X\n",result
);
399 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
401 sh
->audio_out_minsize
=128000; // no idea how to get... :(
402 sh
->audio_in_minsize
= sh
->wf
->nBlockAlign
;
404 return 1; // return values: 1=OK 0=ERROR
407 static int init(sh_audio_t
*sh_audio
){
408 // initialize the decoder, set tables etc...
410 // you can store HANDLE or private struct pointer at sh->context
411 // you can access WAVEFORMATEX header at sh->wf
413 // set sample format/rate parameters if you didn't do it in preinit() yet.
415 return 1; // return values: 1=OK 0=ERROR
418 static void uninit(sh_audio_t
*sh
){
419 // uninit the decoder etc...
420 // again: you don't have to free() a_in_buffer here! it's done by the core.
421 #ifdef CONFIG_WIN32DLL
424 if (wraFreeDecoder
) wraFreeDecoder(sh
->context
);
425 if (wraCloseCodec
) wraCloseCodec(sh
->context
);
429 if (raFreeDecoder
) raFreeDecoder(sh
->context
);
430 if (raCloseCodec
) raCloseCodec(sh
->context
);
433 #ifdef CONFIG_WIN32DLL
436 if (rv_handle
) FreeLibrary(rv_handle
);
439 // this dlclose() causes some memory corruption, and crashes soon (in caller):
440 // if (rv_handle) dlclose(rv_handle);
444 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
448 if(sh
->a_in_buffer_len
<=0){
452 demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->wf
->nBlockAlign
);
453 sh
->a_in_buffer_size
=
454 sh
->a_in_buffer_len
=sh
->wf
->nBlockAlign
;
457 #ifdef CONFIG_WIN32DLL
459 result
=wraDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
463 result
=raDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
465 sh
->a_in_buffer_len
-=sh
->wf
->nBlockAlign
;
467 // printf("radecode: %d bytes, res=0x%X \n",len,result);
469 return len
; // return value: number of _bytes_ written to output buffer,
470 // or -1 for EOF (or uncorrectable error)
473 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
474 // various optional functions you MAY implement:
476 case ADCTRL_RESYNC_STREAM
:
477 // it is called once after seeking, to resync.
478 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
480 case ADCTRL_SKIP_FRAME
:
481 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
482 // of audio data - used to sync audio to video after seeking
483 // if you don't return CONTROL_TRUE, it will defaults to:
484 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
487 return CONTROL_UNKNOWN
;