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.
30 #include "ad_internal.h"
31 #include "loader/wine/windef.h"
33 static const ad_info_t info
= {
37 "Florian Schneider, Arpad Gereoffy, Alex Beregszaszi, Donnie Smith",
38 "binary real audio codecs"
43 void *__builtin_new(unsigned long size
) {
47 // required for cook's uninit:
48 void __builtin_delete(void* ize
) {
52 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
56 static unsigned long (*raCloseCodec
)(void*);
57 static unsigned long (*raDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
58 static unsigned long (*raFreeDecoder
)(void*);
59 //static unsigned long (*raGetNumberOfFlavors2)(void);
60 static unsigned long (*raInitDecoder
)(void*, void*);
61 static unsigned long (*raOpenCodec
)(void*);
62 static unsigned long (*raOpenCodec2
)(void*, void*);
63 static unsigned long (*raSetFlavor
)(void*,unsigned long);
64 static void (*raSetDLLAccessPath
)(char*);
65 static void (*raSetPwd
)(char*,char*);
66 #ifdef CONFIG_WIN32DLL
67 static unsigned long WINAPI (*wraCloseCodec
)(void*);
68 static unsigned long WINAPI (*wraDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
69 static unsigned long WINAPI (*wraFreeDecoder
)(void*);
70 static unsigned long WINAPI (*wraInitDecoder
)(void*, void*);
71 static unsigned long WINAPI (*wraOpenCodec
)(void*);
72 static unsigned long WINAPI (*wraOpenCodec2
)(void*, void*);
73 static unsigned long WINAPI (*wraSetFlavor
)(void*,unsigned long);
74 static void WINAPI (*wraSetDLLAccessPath
)(char*);
75 static void WINAPI (*wraSetPwd
)(char*,char*);
77 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
80 static void *rv_handle
= NULL
;
96 Probably the linux .so-s were compiled with old GCC without setting
97 packing, so it adds 2 bytes padding after the quality field.
98 In windows it seems that there's no padding in it.
103 /* linux dlls doesn't need packing */
104 typedef struct /*__attribute__((__packed__))*/ {
109 /* 2bytes padding here, by gcc */
116 /* windows dlls need packed structs (no padding) */
117 typedef struct __attribute__((__packed__
)) {
130 static int load_syms_linux(char *path
)
134 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening shared obj '%s'\n", path
);
135 handle
= dlopen(path
, RTLD_LAZY
);
138 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error: %s\n", dlerror());
142 raCloseCodec
= dlsym(handle
, "RACloseCodec");
143 raDecode
= dlsym(handle
, "RADecode");
144 raFreeDecoder
= dlsym(handle
, "RAFreeDecoder");
145 raOpenCodec
= dlsym(handle
, "RAOpenCodec");
146 raOpenCodec2
= dlsym(handle
, "RAOpenCodec2");
147 raInitDecoder
= dlsym(handle
, "RAInitDecoder");
148 raSetFlavor
= dlsym(handle
, "RASetFlavor");
149 raSetDLLAccessPath
= dlsym(handle
, "SetDLLAccessPath");
150 raSetPwd
= dlsym(handle
, "RASetPwd"); // optional, used by SIPR
152 if (raCloseCodec
&& raDecode
&& raFreeDecoder
&&
153 (raOpenCodec
||raOpenCodec2
) && raSetFlavor
&&
154 /*raSetDLLAccessPath &&*/ raInitDecoder
)
160 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
166 #ifdef CONFIG_WIN32DLL
169 #include "loader/ldt_keeper.h"
171 void* WINAPI
LoadLibraryA(char* name
);
172 void* WINAPI
GetProcAddress(void* handle
,char *func
);
173 int WINAPI
FreeLibrary(void *handle
);
175 static int load_syms_windows(char *path
)
179 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening win32 dll '%s'\n", path
);
183 handle
= LoadLibraryA(path
);
186 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error loading dll\n");
190 wraCloseCodec
= GetProcAddress(handle
, "RACloseCodec");
191 wraDecode
= GetProcAddress(handle
, "RADecode");
192 wraFreeDecoder
= GetProcAddress(handle
, "RAFreeDecoder");
193 wraOpenCodec
= GetProcAddress(handle
, "RAOpenCodec");
194 wraOpenCodec2
= GetProcAddress(handle
, "RAOpenCodec2");
195 wraInitDecoder
= GetProcAddress(handle
, "RAInitDecoder");
196 wraSetFlavor
= GetProcAddress(handle
, "RASetFlavor");
197 wraSetDLLAccessPath
= GetProcAddress(handle
, "SetDLLAccessPath");
198 wraSetPwd
= GetProcAddress(handle
, "RASetPwd"); // optional, used by SIPR
200 if (wraCloseCodec
&& wraDecode
&& wraFreeDecoder
&&
201 (wraOpenCodec
|| wraOpenCodec2
) && wraSetFlavor
&&
202 /*wraSetDLLAccessPath &&*/ wraInitDecoder
)
209 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
217 static int preinit(sh_audio_t
*sh
){
218 // let's check if the driver is available, return 0 if not.
219 // (you should do that if you use external lib(s) which is optional)
223 path
= malloc(strlen(BINARY_CODECS_PATH
) + strlen(sh
->codec
->dll
) + 2);
225 sprintf(path
, BINARY_CODECS_PATH
"/%s", sh
->codec
->dll
);
227 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
228 then try to load the windows ones */
231 if (strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
233 #ifdef CONFIG_WIN32DLL
234 if (!load_syms_windows(sh
->codec
->dll
))
237 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "ERROR: Could not open required DirectShow codec %s.\n", sh
->codec
->dll
);
238 mp_msg(MSGT_DECVIDEO
, MSGL_HINT
, "Read the RealAudio section of the DOCS!\n");
243 #ifdef CONFIG_WIN32DLL
244 if((raSetDLLAccessPath
&& dll_type
== 0) || (wraSetDLLAccessPath
&& dll_type
== 1)){
246 if(raSetDLLAccessPath
){
249 path
= realloc(path
, strlen(BINARY_CODECS_PATH
) + 13);
250 sprintf(path
, "DT_Codecs=" BINARY_CODECS_PATH
);
251 if(path
[strlen(path
)-1]!='/'){
252 path
[strlen(path
)+1]=0;
253 path
[strlen(path
)]='/';
255 path
[strlen(path
)+1]=0;
256 #ifdef CONFIG_WIN32DLL
260 for (i
=0; i
< strlen(path
); i
++)
261 if (path
[i
] == '/') path
[i
] = '\\';
262 wraSetDLLAccessPath(path
);
266 raSetDLLAccessPath(path
);
269 #ifdef CONFIG_WIN32DLL
272 result
= wraOpenCodec2(&sh
->context
, BINARY_CODECS_PATH
"\\");
274 result
=wraOpenCodec(&sh
->context
);
278 result
= raOpenCodec2(&sh
->context
, BINARY_CODECS_PATH
"/");
280 result
=raOpenCodec(&sh
->context
);
282 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder open failed, error code: 0x%X\n",result
);
285 // printf("opencodec ok (result: %x)\n", result);
286 free(path
); /* after this it isn't used anymore */
288 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
289 sh
->samplesize
=sh
->wf
->wBitsPerSample
/8;
290 sh
->channels
=sh
->wf
->nChannels
;
293 ra_init_t init_data
={
294 sh
->wf
->nSamplesPerSec
,
295 sh
->wf
->wBitsPerSample
,
298 sh
->wf
->nBlockAlign
, // subpacket size
299 sh
->wf
->nBlockAlign
, // coded frame size
300 sh
->wf
->cbSize
, // codec data length
301 (char*)(sh
->wf
+1) // extras
303 #ifdef CONFIG_WIN32DLL
304 wra_init_t winit_data
={
305 sh
->wf
->nSamplesPerSec
,
306 sh
->wf
->wBitsPerSample
,
309 sh
->wf
->nBlockAlign
, // subpacket size
310 sh
->wf
->nBlockAlign
, // coded frame size
311 sh
->wf
->cbSize
, // codec data length
312 (char*)(sh
->wf
+1) // extras
315 #ifdef CONFIG_WIN32DLL
317 result
=wraInitDecoder(sh
->context
,&winit_data
);
320 result
=raInitDecoder(sh
->context
,&init_data
);
323 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder init failed, error code: 0x%X\n",result
);
326 // printf("initdecoder ok (result: %x)\n", result);
329 #ifdef CONFIG_WIN32DLL
330 if((raSetPwd
&& dll_type
== 0) || (wraSetPwd
&& dll_type
== 1)){
335 #ifdef CONFIG_WIN32DLL
337 wraSetPwd(sh
->context
,"Ardubancel Quazanga");
340 raSetPwd(sh
->context
,"Ardubancel Quazanga"); // set password... lol.
343 if (sh
->format
== mmioFOURCC('s','i','p','r')) {
346 if (sh
->wf
->nAvgBytesPerSec
> 1531)
348 else if (sh
->wf
->nAvgBytesPerSec
> 937)
350 else if (sh
->wf
->nAvgBytesPerSec
> 719)
354 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Got sipr flavor %d from bitrate %d\n",flavor
, sh
->wf
->nAvgBytesPerSec
);
356 #ifdef CONFIG_WIN32DLL
358 result
=wraSetFlavor(sh
->context
,flavor
);
361 result
=raSetFlavor(sh
->context
,flavor
);
363 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder flavor setup failed, error code: 0x%X\n",result
);
368 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
370 sh
->audio_out_minsize
=128000; // no idea how to get... :(
371 sh
->audio_in_minsize
= sh
->wf
->nBlockAlign
;
373 return 1; // return values: 1=OK 0=ERROR
376 static int init(sh_audio_t
*sh_audio
){
377 // initialize the decoder, set tables etc...
379 // you can store HANDLE or private struct pointer at sh->context
380 // you can access WAVEFORMATEX header at sh->wf
382 // set sample format/rate parameters if you didn't do it in preinit() yet.
384 return 1; // return values: 1=OK 0=ERROR
387 static void uninit(sh_audio_t
*sh
){
388 // uninit the decoder etc...
389 // again: you don't have to free() a_in_buffer here! it's done by the core.
390 #ifdef CONFIG_WIN32DLL
393 if (wraFreeDecoder
) wraFreeDecoder(sh
->context
);
394 if (wraCloseCodec
) wraCloseCodec(sh
->context
);
398 if (raFreeDecoder
) raFreeDecoder(sh
->context
);
399 if (raCloseCodec
) raCloseCodec(sh
->context
);
402 #ifdef CONFIG_WIN32DLL
405 if (rv_handle
) FreeLibrary(rv_handle
);
408 // this dlclose() causes some memory corruption, and crashes soon (in caller):
409 // if (rv_handle) dlclose(rv_handle);
413 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
417 if(sh
->a_in_buffer_len
<=0){
421 demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->wf
->nBlockAlign
);
422 sh
->a_in_buffer_size
=
423 sh
->a_in_buffer_len
=sh
->wf
->nBlockAlign
;
426 #ifdef CONFIG_WIN32DLL
428 result
=wraDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
432 result
=raDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
434 sh
->a_in_buffer_len
-=sh
->wf
->nBlockAlign
;
436 // printf("radecode: %d bytes, res=0x%X \n",len,result);
438 return len
; // return value: number of _bytes_ written to output buffer,
439 // or -1 for EOF (or uncorrectable error)
442 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
443 // various optional functions you MAY implement:
445 case ADCTRL_RESYNC_STREAM
:
446 // it is called once after seeking, to resync.
447 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
449 case ADCTRL_SKIP_FRAME
:
450 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
451 // of audio data - used to sync audio to video after seeking
452 // if you don't return CONTROL_TRUE, it will defaults to:
453 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
456 return CONTROL_UNKNOWN
;