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 "libavutil/attributes.h"
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
);
45 void __builtin_delete(void *ize
);
47 void *__builtin_new(unsigned long size
) {
51 // required for cook's uninit:
52 void __builtin_delete(void* ize
) {
56 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
60 static unsigned long (*raCloseCodec
)(void*);
61 static unsigned long (*raDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
62 static unsigned long (*raFreeDecoder
)(void*);
63 //static unsigned long (*raGetNumberOfFlavors2)(void);
64 static unsigned long (*raInitDecoder
)(void*, void*);
65 static unsigned long (*raOpenCodec
)(void*);
66 static unsigned long (*raOpenCodec2
)(void*, void*);
67 static unsigned long (*raSetFlavor
)(void*,unsigned long);
68 static void (*raSetDLLAccessPath
)(char*);
69 static void (*raSetPwd
)(char*,char*);
70 #ifdef CONFIG_WIN32DLL
71 static unsigned long WINAPI (*wraCloseCodec
)(void*);
72 static unsigned long WINAPI (*wraDecode
)(void*, char*,unsigned long,char*,unsigned int*,long);
73 static unsigned long WINAPI (*wraFreeDecoder
)(void*);
74 static unsigned long WINAPI (*wraInitDecoder
)(void*, void*);
75 static unsigned long WINAPI (*wraOpenCodec
)(void*);
76 static unsigned long WINAPI (*wraOpenCodec2
)(void*, void*);
77 static unsigned long WINAPI (*wraSetFlavor
)(void*,unsigned long);
78 static void WINAPI (*wraSetDLLAccessPath
)(char*);
79 static void WINAPI (*wraSetPwd
)(char*,char*);
81 static int dll_type
= 0; /* 0 = unix dlopen, 1 = win32 dll */
84 static void *rv_handle
= NULL
;
100 Probably the linux .so-s were compiled with old GCC without setting
101 packing, so it adds 2 bytes padding after the quality field.
102 In windows it seems that there's no padding in it.
107 /* linux dlls doesn't need packing */
108 typedef struct /*__attribute__((__packed__))*/ {
113 /* 2bytes padding here, by gcc */
120 /* windows dlls need packed structs (no padding) */
121 typedef struct __attribute__((__packed__
)) {
134 static int load_syms_linux(char *path
)
138 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening shared obj '%s'\n", path
);
139 handle
= dlopen(path
, RTLD_LAZY
);
142 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error: %s\n", dlerror());
146 raCloseCodec
= dlsym(handle
, "RACloseCodec");
147 raDecode
= dlsym(handle
, "RADecode");
148 raFreeDecoder
= dlsym(handle
, "RAFreeDecoder");
149 raOpenCodec
= dlsym(handle
, "RAOpenCodec");
150 raOpenCodec2
= dlsym(handle
, "RAOpenCodec2");
151 raInitDecoder
= dlsym(handle
, "RAInitDecoder");
152 raSetFlavor
= dlsym(handle
, "RASetFlavor");
153 raSetDLLAccessPath
= dlsym(handle
, "SetDLLAccessPath");
154 raSetPwd
= dlsym(handle
, "RASetPwd"); // optional, used by SIPR
156 if (raCloseCodec
&& raDecode
&& raFreeDecoder
&&
157 (raOpenCodec
||raOpenCodec2
) && raSetFlavor
&&
158 /*raSetDLLAccessPath &&*/ raInitDecoder
)
164 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
170 #ifdef CONFIG_WIN32DLL
173 #include "loader/ldt_keeper.h"
175 void* WINAPI
LoadLibraryA(char* name
);
176 void* WINAPI
GetProcAddress(void* handle
,char *func
);
177 int WINAPI
FreeLibrary(void *handle
);
179 static int load_syms_windows(char *path
)
183 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "opening win32 dll '%s'\n", path
);
187 handle
= LoadLibraryA(path
);
190 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error loading dll\n");
194 wraCloseCodec
= GetProcAddress(handle
, "RACloseCodec");
195 wraDecode
= GetProcAddress(handle
, "RADecode");
196 wraFreeDecoder
= GetProcAddress(handle
, "RAFreeDecoder");
197 wraOpenCodec
= GetProcAddress(handle
, "RAOpenCodec");
198 wraOpenCodec2
= GetProcAddress(handle
, "RAOpenCodec2");
199 wraInitDecoder
= GetProcAddress(handle
, "RAInitDecoder");
200 wraSetFlavor
= GetProcAddress(handle
, "RASetFlavor");
201 wraSetDLLAccessPath
= GetProcAddress(handle
, "SetDLLAccessPath");
202 wraSetPwd
= GetProcAddress(handle
, "RASetPwd"); // optional, used by SIPR
204 if (wraCloseCodec
&& wraDecode
&& wraFreeDecoder
&&
205 (wraOpenCodec
|| wraOpenCodec2
) && wraSetFlavor
&&
206 /*wraSetDLLAccessPath &&*/ wraInitDecoder
)
213 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Cannot resolve symbols - incompatible dll: %s\n",path
);
221 static int preinit(sh_audio_t
*sh
){
222 // let's check if the driver is available, return 0 if not.
223 // (you should do that if you use external lib(s) which is optional)
227 path
= malloc(strlen(codec_path
) + strlen(sh
->codec
->dll
) + 2);
229 sprintf(path
, "%s/%s", codec_path
, sh
->codec
->dll
);
231 /* first try to load linux dlls, if failed and we're supporting win32 dlls,
232 then try to load the windows ones */
235 if (strstr(sh
->codec
->dll
,".dll") || !load_syms_linux(path
))
237 #ifdef CONFIG_WIN32DLL
238 if (!load_syms_windows(sh
->codec
->dll
))
241 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "ERROR: Could not open required DirectShow codec %s.\n", sh
->codec
->dll
);
242 mp_msg(MSGT_DECVIDEO
, MSGL_HINT
, "Read the RealAudio section of the DOCS!\n");
247 #ifdef CONFIG_WIN32DLL
248 if((raSetDLLAccessPath
&& dll_type
== 0) || (wraSetDLLAccessPath
&& dll_type
== 1)){
250 if(raSetDLLAccessPath
){
253 path
= realloc(path
, strlen(codec_path
) + 13);
254 sprintf(path
, "DT_Codecs=%s", codec_path
);
255 if(path
[strlen(path
)-1]!='/'){
256 path
[strlen(path
)+1]=0;
257 path
[strlen(path
)]='/';
259 path
[strlen(path
)+1]=0;
260 #ifdef CONFIG_WIN32DLL
264 for (i
=0; i
< strlen(path
); i
++)
265 if (path
[i
] == '/') path
[i
] = '\\';
266 wraSetDLLAccessPath(path
);
270 raSetDLLAccessPath(path
);
273 #ifdef CONFIG_WIN32DLL
276 sprintf(path
, "%s\\", codec_path
);
277 result
= wraOpenCodec2(&sh
->context
, path
);
279 result
=wraOpenCodec(&sh
->context
);
283 sprintf(path
, "%s/", codec_path
);
284 result
= raOpenCodec2(&sh
->context
, path
);
286 result
=raOpenCodec(&sh
->context
);
288 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder open failed, error code: 0x%X\n",result
);
291 // printf("opencodec ok (result: %x)\n", result);
292 free(path
); /* after this it isn't used anymore */
294 sh
->samplerate
=sh
->wf
->nSamplesPerSec
;
295 sh
->samplesize
=sh
->wf
->wBitsPerSample
/8;
296 sh
->channels
=sh
->wf
->nChannels
;
299 ra_init_t init_data
={
300 sh
->wf
->nSamplesPerSec
,
301 sh
->wf
->wBitsPerSample
,
304 sh
->wf
->nBlockAlign
, // subpacket size
305 sh
->wf
->nBlockAlign
, // coded frame size
306 sh
->wf
->cbSize
, // codec data length
307 (char*)(sh
->wf
+1) // extras
309 #ifdef CONFIG_WIN32DLL
310 wra_init_t winit_data
={
311 sh
->wf
->nSamplesPerSec
,
312 sh
->wf
->wBitsPerSample
,
315 sh
->wf
->nBlockAlign
, // subpacket size
316 sh
->wf
->nBlockAlign
, // coded frame size
317 sh
->wf
->cbSize
, // codec data length
318 (char*)(sh
->wf
+1) // extras
321 #ifdef CONFIG_WIN32DLL
323 result
=wraInitDecoder(sh
->context
,&winit_data
);
326 result
=raInitDecoder(sh
->context
,&init_data
);
329 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder init failed, error code: 0x%X\n",result
);
332 // printf("initdecoder ok (result: %x)\n", result);
335 #ifdef CONFIG_WIN32DLL
336 if((raSetPwd
&& dll_type
== 0) || (wraSetPwd
&& dll_type
== 1)){
341 #ifdef CONFIG_WIN32DLL
343 wraSetPwd(sh
->context
,"Ardubancel Quazanga");
346 raSetPwd(sh
->context
,"Ardubancel Quazanga"); // set password... lol.
349 if (sh
->format
== mmioFOURCC('s','i','p','r')) {
352 if (sh
->wf
->nAvgBytesPerSec
> 1531)
354 else if (sh
->wf
->nAvgBytesPerSec
> 937)
356 else if (sh
->wf
->nAvgBytesPerSec
> 719)
360 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Got sipr flavor %d from bitrate %d\n",flavor
, sh
->wf
->nAvgBytesPerSec
);
362 #ifdef CONFIG_WIN32DLL
364 result
=wraSetFlavor(sh
->context
,flavor
);
367 result
=raSetFlavor(sh
->context
,flavor
);
369 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"Decoder flavor setup failed, error code: 0x%X\n",result
);
374 sh
->i_bps
=sh
->wf
->nAvgBytesPerSec
;
376 sh
->audio_out_minsize
=128000; // no idea how to get... :(
377 sh
->audio_in_minsize
= sh
->wf
->nBlockAlign
;
379 return 1; // return values: 1=OK 0=ERROR
382 static int init(sh_audio_t
*sh_audio
){
383 // initialize the decoder, set tables etc...
385 // you can store HANDLE or private struct pointer at sh->context
386 // you can access WAVEFORMATEX header at sh->wf
388 // set sample format/rate parameters if you didn't do it in preinit() yet.
390 return 1; // return values: 1=OK 0=ERROR
393 static void uninit(sh_audio_t
*sh
){
394 // uninit the decoder etc...
395 // again: you don't have to free() a_in_buffer here! it's done by the core.
396 #ifdef CONFIG_WIN32DLL
399 if (wraFreeDecoder
) wraFreeDecoder(sh
->context
);
400 if (wraCloseCodec
) wraCloseCodec(sh
->context
);
404 if (raFreeDecoder
) raFreeDecoder(sh
->context
);
405 if (raCloseCodec
) raCloseCodec(sh
->context
);
408 #ifdef CONFIG_WIN32DLL
411 if (rv_handle
) FreeLibrary(rv_handle
);
414 // this dlclose() causes some memory corruption, and crashes soon (in caller):
415 // if (rv_handle) dlclose(rv_handle);
419 static int decode_audio(sh_audio_t
*sh
,unsigned char *buf
,int minlen
,int maxlen
){
420 int result av_unused
;
423 if(sh
->a_in_buffer_len
<=0){
427 demux_read_data(sh
->ds
, sh
->a_in_buffer
, sh
->wf
->nBlockAlign
);
428 sh
->a_in_buffer_size
=
429 sh
->a_in_buffer_len
=sh
->wf
->nBlockAlign
;
432 #ifdef CONFIG_WIN32DLL
434 result
=wraDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
438 result
=raDecode(sh
->context
, sh
->a_in_buffer
+sh
->a_in_buffer_size
-sh
->a_in_buffer_len
, sh
->wf
->nBlockAlign
,
440 sh
->a_in_buffer_len
-=sh
->wf
->nBlockAlign
;
442 // printf("radecode: %d bytes, res=0x%X \n",len,result);
444 return len
; // return value: number of _bytes_ written to output buffer,
445 // or -1 for EOF (or uncorrectable error)
448 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...){
449 // various optional functions you MAY implement:
451 case ADCTRL_RESYNC_STREAM
:
452 // it is called once after seeking, to resync.
453 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
455 case ADCTRL_SKIP_FRAME
:
456 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
457 // of audio data - used to sync audio to video after seeking
458 // if you don't return CONTROL_TRUE, it will defaults to:
459 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
462 return CONTROL_UNKNOWN
;