1 /*****************************************************************************
2 * realaudio.c: a realaudio decoder that uses the realaudio library/dll
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
22 /*****************************************************************************
24 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
35 /* Need the w32dll loader from mplayer */
36 # include <wine/winerror.h>
37 # include <ldt_keeper.h>
38 # include <wine/windef.h>
40 void *WINAPI LoadLibraryA( char *name );
41 void *WINAPI GetProcAddress( void *handle, char *func );
42 int WINAPI FreeLibrary( void *handle );
49 #if defined(HAVE_DL_DLOPEN)
50 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
53 # if defined(HAVE_SYS_DL_H)
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
65 set_description( N_("RealAudio library decoder") )
66 set_capability( "decoder", 10 )
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_VCODEC )
69 set_callbacks( Open, Close )
72 /*****************************************************************************
74 *****************************************************************************/
75 static int OpenDll( decoder_t * );
76 static int OpenNativeDll( decoder_t *, char *, char * );
77 static int OpenWin32Dll( decoder_t *, char *, char * );
78 static void CloseDll( decoder_t * );
80 static aout_buffer_t *Decode( decoder_t *, block_t ** );
84 audio_date_t end_date;
92 short int i_codec_flavor;
95 unsigned long (*raCloseCodec)(void*);
96 unsigned long (*raDecode)(void*, char*, unsigned long, char*,
98 unsigned long (*raFlush)(unsigned long, unsigned long, unsigned long);
99 unsigned long (*raFreeDecoder)(void*);
100 void* (*raGetFlavorProperty)(void*, unsigned long,
101 unsigned long, int*);
102 unsigned long (*raInitDecoder)(void*, void*);
103 unsigned long (*raOpenCodec)(void*);
104 unsigned long (*raOpenCodec2)(void*, void*);
105 unsigned long (*raSetFlavor)(void*, unsigned long);
106 void (*raSetDLLAccessPath)(char*);
107 void (*raSetPwd)(char*, char*);
114 unsigned long (WINAPI *wraCloseCodec)(void*);
115 unsigned long (WINAPI *wraDecode)(void*, char*, unsigned long, char*,
116 unsigned int*, long);
117 unsigned long (WINAPI *wraFlush)(unsigned long, unsigned long,
119 unsigned long (WINAPI *wraFreeDecoder)(void*);
120 void* (WINAPI *wraGetFlavorProperty)(void*, unsigned long,
121 unsigned long, int*);
122 unsigned long (WINAPI *wraInitDecoder)(void*, void*);
123 unsigned long (WINAPI *wraOpenCodec)(void*);
124 unsigned long (WINAPI *wraOpenCodec2)(void*, void*);
125 unsigned long (WINAPI *wraSetFlavor)(void*, unsigned long);
126 void (WINAPI *wraSetDLLAccessPath)(char*);
127 void (WINAPI *wraSetPwd)(char*, char*);
130 /* linux dlls doesn't need packing */
131 typedef struct /*__attribute__((__packed__))*/ {
136 /* 2bytes padding here, by gcc */
143 /* windows dlls need packed structs (no padding) */
144 typedef struct __attribute__((__packed__)) {
155 void *__builtin_new(unsigned long size) {return malloc(size);}
156 void __builtin_delete(void *p) {free(p);}
158 static const int pi_channels_maps[7] =
162 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
163 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
164 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
165 | AOUT_CHAN_REARRIGHT,
166 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
167 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
168 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
169 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
172 /*****************************************************************************
173 * Open: probe the decoder and return score
174 *****************************************************************************
175 * Tries to launch a decoder and return score so that the interface is able
177 *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
180 decoder_t *p_dec = (decoder_t*)p_this;
181 decoder_sys_t *p_sys = p_dec->p_sys;
183 switch( p_dec->fmt_in.i_codec )
186 case VLC_CODEC_ATRAC3:
187 case VLC_FOURCC('s','i','p','r'):
194 /* Channel detection */
195 if( p_dec->fmt_in.audio.i_channels <= 0 ||
196 p_dec->fmt_in.audio.i_channels > 6 )
198 msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
199 p_dec->fmt_in.audio.i_channels );
203 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
207 /* Flavor for SIPR codecs */
208 p_sys->i_codec_flavor = -1;
209 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','i','p','r') )
211 p_sys->i_codec_flavor = p_dec->fmt_in.audio.i_flavor;
212 msg_Dbg( p_dec, "Got sipr flavor %d", p_sys->i_codec_flavor );
215 if( OpenDll( p_dec ) != VLC_SUCCESS )
222 if( p_sys->win32_dll ) Close( p_this );
225 es_format_Init( &p_dec->fmt_out, AUDIO_ES, VLC_CODEC_S16N );
226 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
227 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
228 p_dec->fmt_out.audio.i_bitspersample =
229 p_dec->fmt_in.audio.i_bitspersample;
230 p_dec->fmt_out.audio.i_physical_channels =
231 p_dec->fmt_out.audio.i_original_channels =
232 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
234 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
235 aout_DateSet( &p_sys->end_date, 0 );
237 p_dec->pf_decode_audio = Decode;
239 p_sys->p_out = malloc( 4096 * 10 );
250 /*****************************************************************************
252 *****************************************************************************/
253 static void Close( vlc_object_t *p_this )
255 decoder_t *p_dec = (decoder_t*)p_this;
258 free( p_dec->p_sys->p_out );
259 free( p_dec->p_sys );
262 /*****************************************************************************
264 *****************************************************************************/
265 static int OpenDll( decoder_t *p_dec )
270 /** Find the good path for the dlls.**/
275 "/usr/local/RealPlayer8/Codecs",
276 "/usr/RealPlayer8/Codecs",
277 "/usr/lib/RealPlayer8/Codecs",
278 "/opt/RealPlayer8/Codecs",
279 "/usr/lib/RealPlayer9/users/Real/Codecs",
280 "/usr/lib/RealPlayer10/codecs",
281 "/usr/lib/RealPlayer10GOLD/codecs",
282 "/usr/lib/helix/player/codecs",
283 "/usr/lib64/RealPlayer8/Codecs",
284 "/usr/lib64/RealPlayer9/users/Real/Codecs",
285 "/usr/lib64/RealPlayer10/codecs",
286 "/usr/lib64/RealPlayer10GOLD/codecs",
289 "/usr/local/lib/codecs",
297 char psz_win32_real_codecs[MAX_PATH + 1];
298 char psz_win32_helix_codecs[MAX_PATH + 1];
302 DWORD i_type, i_data = MAX_PATH + 1, i_index = 1;
305 p_data = psz_win32_real_codecs;
306 if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
307 _T("Software\\RealNetworks\\Preferences\\DT_Codecs"),
308 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
310 if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
311 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
314 int i_len = strlen( p_data );
315 if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
316 ppsz_path[i_index++] = p_data;
317 msg_Err( p_dec, "Real: %s", p_data );
319 RegCloseKey( h_key );
322 p_data = psz_win32_helix_codecs;
323 if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
324 _T("Helix\\HelixSDK\\10.0\\Preferences\\DT_Codecs"),
325 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
327 if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
328 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
331 int i_len = strlen( p_data );
332 if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
333 ppsz_path[i_index++] = p_data;
334 msg_Err( p_dec, "Real: %s", p_data );
336 RegCloseKey( h_key );
342 /** Try the native libraries first **/
344 for( i = 0; ppsz_path[i]; i++ )
347 if( asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
348 (char *)&p_dec->fmt_in.i_codec ) != -1 )
350 i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
352 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
356 if( asprintf( &psz_dll, "%s/%4.4s.so", ppsz_path[i],
357 (char *)&p_dec->fmt_in.i_codec ) != -1 )
359 i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
361 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
366 /** Or use the WIN32 dlls **/
367 #if defined(LOADER) || defined(WIN32)
368 for( i = 0; ppsz_path[i]; i++ )
371 if( asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
372 (char *)&p_dec->fmt_in.i_codec ) != -1 )
374 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
376 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
380 if( asprintf( &psz_dll, "%s\\%4.4s3260.dll", ppsz_path[i],
381 (char *)&p_dec->fmt_in.i_codec ) != -1 )
383 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
385 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
393 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
395 #if defined(HAVE_DL_DLOPEN)
396 decoder_sys_t *p_sys = p_dec->p_sys;
397 void *handle = 0, *context = 0;
398 unsigned int i_result;
402 ra_init_t init_data =
404 p_dec->fmt_in.audio.i_rate,
405 p_dec->fmt_in.audio.i_bitspersample,
406 p_dec->fmt_in.audio.i_channels,
408 p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
409 p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
410 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
413 msg_Dbg( p_dec, "opening library '%s'", psz_dll );
414 if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
416 msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
417 psz_dll, dlerror() );
421 p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
422 p_sys->raDecode = dlsym( handle, "RADecode" );
423 p_sys->raFlush = dlsym( handle, "RAFlush" );
424 p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
425 p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
426 p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
427 p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
428 p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
429 p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
430 p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
431 p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
433 if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
434 !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
435 !p_sys->raDecode || !p_sys->raFreeDecoder ||
436 !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
437 /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
442 if( p_sys->raOpenCodec2 )
443 i_result = p_sys->raOpenCodec2( &context, psz_path );
445 i_result = p_sys->raOpenCodec( &context );
449 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
453 i_result = p_sys->raInitDecoder( context, &init_data );
456 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
460 if( p_sys->i_codec_flavor >= 0 )
462 i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
465 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
470 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
472 msg_Dbg( p_dec, "audio codec: [%d] %s",
473 p_sys->i_codec_flavor, (char *)p_prop );
475 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
479 int i_bps = ((*((int*)p_prop))+4)/8;
480 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
481 (*((int*)p_prop))*0.001f, i_bps );
485 p_sys->context = context;
490 if( context ) p_sys->raFreeDecoder( context );
491 if( context ) p_sys->raCloseCodec( context );
498 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
500 #if defined(LOADER) || defined(WIN32)
501 decoder_sys_t *p_sys = p_dec->p_sys;
502 void *handle = 0, *context = 0;
503 unsigned int i_result;
507 wra_init_t init_data =
509 p_dec->fmt_in.audio.i_rate,
510 p_dec->fmt_in.audio.i_bitspersample,
511 p_dec->fmt_in.audio.i_channels,
513 p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
514 p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
515 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
518 msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
524 if( !(handle = LoadLibraryA( psz_dll )) )
526 msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
530 p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
531 p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
532 p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
533 p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
534 p_sys->wraGetFlavorProperty =
535 GetProcAddress( handle, "RAGetFlavorProperty" );
536 p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
537 p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
538 p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
539 p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
540 p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
542 GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
544 if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
545 !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
546 !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
547 !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
548 /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
550 FreeLibrary( handle );
554 if( p_sys->wraOpenCodec2 )
555 i_result = p_sys->wraOpenCodec2( &context, psz_path );
557 i_result = p_sys->wraOpenCodec( &context );
561 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
565 i_result = p_sys->wraInitDecoder( context, &init_data );
568 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
572 if( p_sys->i_codec_flavor >= 0 )
574 i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
577 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
582 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
584 msg_Dbg( p_dec, "audio codec: [%d] %s",
585 p_sys->i_codec_flavor, (char *)p_prop );
587 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
591 int i_bps = ((*((int*)p_prop))+4)/8;
592 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
593 (*((int*)p_prop))*0.001f, i_bps );
597 p_sys->context = context;
598 p_sys->win32_dll = handle;
602 if( context ) p_sys->wraFreeDecoder( context );
603 if( context ) p_sys->wraCloseCodec( context );
604 FreeLibrary( handle );
610 /*****************************************************************************
612 *****************************************************************************/
613 static void CloseDll( decoder_t *p_dec )
615 decoder_sys_t *p_sys = p_dec->p_sys;
617 if( p_sys->context && p_sys->dll )
619 p_sys->raFreeDecoder( p_sys->context );
620 p_sys->raCloseCodec( p_sys->context );
623 if( p_sys->context && p_sys->win32_dll )
625 p_sys->wraFreeDecoder( p_sys->context );
626 p_sys->wraCloseCodec( p_sys->context );
629 #if defined(HAVE_DL_DLOPEN)
630 if( p_sys->dll ) dlclose( p_sys->dll );
633 #if defined(LOADER) || defined(WIN32)
634 if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
636 #if 0 //def LOADER /* Segfaults */
637 Restore_LDT_Keeper( p_sys->ldt_fs );
638 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
643 p_sys->win32_dll = 0;
647 /*****************************************************************************
649 *****************************************************************************/
650 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
652 decoder_sys_t *p_sys = p_dec->p_sys;
653 aout_buffer_t *p_aout_buffer = 0;
654 unsigned int i_result;
659 if( !p_sys->win32_dll && !p_sys->dll )
661 /* We must do open and close in the same thread (unless we do
662 * Setup_LDT_Keeper in the main thread before all others */
663 if( OpenDll( p_dec ) != VLC_SUCCESS )
666 p_dec->b_error = true;
672 if( pp_block == NULL || *pp_block == NULL ) return NULL;
676 i_result = p_sys->raDecode( p_sys->context, (char *)p_block->p_buffer,
677 (unsigned long)p_block->i_buffer,
678 p_sys->p_out, &p_sys->i_out, -1 );
680 i_result = p_sys->wraDecode( p_sys->context, (char *)p_block->p_buffer,
681 (unsigned long)p_block->i_buffer,
682 p_sys->p_out, &p_sys->i_out, -1 );
685 msg_Err( p_dec, "decoded: %i samples (%i)",
686 p_sys->i_out * 8 / p_dec->fmt_out.audio.i_bitspersample /
687 p_dec->fmt_out.audio.i_channels, i_result );
690 /* Date management */
691 if( p_block->i_pts > 0 &&
692 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
694 aout_DateSet( &p_sys->end_date, p_block->i_pts );
697 if( !aout_DateGet( &p_sys->end_date ) )
699 /* We've just started the stream, wait for the first PTS. */
700 if( p_block ) block_Release( p_block );
704 i_samples = p_sys->i_out * 8 /
705 p_dec->fmt_out.audio.i_bitspersample /p_dec->fmt_out.audio.i_channels;
708 decoder_NewAudioBuffer( p_dec, i_samples );
711 memcpy( p_aout_buffer->p_buffer, p_sys->p_out, p_sys->i_out );
713 /* Date management */
714 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
715 p_aout_buffer->end_date =
716 aout_DateIncrement( &p_sys->end_date, i_samples );
719 block_Release( p_block );
721 return p_aout_buffer;