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 * );
77 static int OpenNativeDll( decoder_t *, char *, char * );
79 #if defined(LOADER) || defined(WIN32)
80 static int OpenWin32Dll( decoder_t *, char *, char * );
82 static void CloseDll( decoder_t * );
84 static aout_buffer_t *Decode( decoder_t *, block_t ** );
96 short int i_codec_flavor;
99 unsigned long (*raCloseCodec)(void*);
100 unsigned long (*raDecode)(void*, char*, unsigned long, char*,
101 unsigned int*, long);
102 unsigned long (*raFlush)(unsigned long, unsigned long, unsigned long);
103 unsigned long (*raFreeDecoder)(void*);
104 void* (*raGetFlavorProperty)(void*, unsigned long,
105 unsigned long, int*);
106 unsigned long (*raInitDecoder)(void*, void*);
107 unsigned long (*raOpenCodec)(void*);
108 unsigned long (*raOpenCodec2)(void*, void*);
109 unsigned long (*raSetFlavor)(void*, unsigned long);
110 void (*raSetDLLAccessPath)(char*);
111 void (*raSetPwd)(char*, char*);
118 unsigned long (WINAPI *wraCloseCodec)(void*);
119 unsigned long (WINAPI *wraDecode)(void*, char*, unsigned long, char*,
120 unsigned int*, long);
121 unsigned long (WINAPI *wraFlush)(unsigned long, unsigned long,
123 unsigned long (WINAPI *wraFreeDecoder)(void*);
124 void* (WINAPI *wraGetFlavorProperty)(void*, unsigned long,
125 unsigned long, int*);
126 unsigned long (WINAPI *wraInitDecoder)(void*, void*);
127 unsigned long (WINAPI *wraOpenCodec)(void*);
128 unsigned long (WINAPI *wraOpenCodec2)(void*, void*);
129 unsigned long (WINAPI *wraSetFlavor)(void*, unsigned long);
130 void (WINAPI *wraSetDLLAccessPath)(char*);
131 void (WINAPI *wraSetPwd)(char*, char*);
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__)) {
159 #if 0 /* I have no idea what this is doing here */
160 void *__builtin_new(unsigned long size) {return malloc(size);}
161 void __builtin_delete(void *p) {free(p);}
164 static const int pi_channels_maps[7] =
168 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
169 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
170 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
171 | AOUT_CHAN_REARRIGHT,
172 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
173 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
174 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
175 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
178 /*****************************************************************************
179 * Open: probe the decoder and return score
180 *****************************************************************************
181 * Tries to launch a decoder and return score so that the interface is able
183 *****************************************************************************/
184 static int Open( vlc_object_t *p_this )
186 decoder_t *p_dec = (decoder_t*)p_this;
187 decoder_sys_t *p_sys = p_dec->p_sys;
189 switch( p_dec->fmt_in.i_codec )
192 case VLC_CODEC_ATRAC3:
193 case VLC_FOURCC('s','i','p','r'):
200 /* Channel detection */
201 if( p_dec->fmt_in.audio.i_channels <= 0 ||
202 p_dec->fmt_in.audio.i_channels > 6 )
204 msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
205 p_dec->fmt_in.audio.i_channels );
209 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
213 /* Flavor for SIPR codecs */
214 p_sys->i_codec_flavor = -1;
215 if( p_dec->fmt_in.i_codec == VLC_FOURCC_SIPR )
217 p_sys->i_codec_flavor = p_dec->fmt_in.audio.i_flavor;
218 msg_Dbg( p_dec, "Got sipr flavor %d", p_sys->i_codec_flavor );
221 if( OpenDll( p_dec ) != VLC_SUCCESS )
228 if( p_sys->win32_dll ) Close( p_this );
231 es_format_Init( &p_dec->fmt_out, AUDIO_ES, VLC_CODEC_S16N );
232 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
233 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
234 p_dec->fmt_out.audio.i_bitspersample =
235 p_dec->fmt_in.audio.i_bitspersample;
236 p_dec->fmt_out.audio.i_physical_channels =
237 p_dec->fmt_out.audio.i_original_channels =
238 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
240 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
241 date_Set( &p_sys->end_date, 0 );
243 p_dec->pf_decode_audio = Decode;
245 p_sys->p_out = malloc( 4096 * 10 );
256 /*****************************************************************************
258 *****************************************************************************/
259 static void Close( vlc_object_t *p_this )
261 decoder_t *p_dec = (decoder_t*)p_this;
264 free( p_dec->p_sys->p_out );
265 free( p_dec->p_sys );
268 /*****************************************************************************
270 *****************************************************************************/
271 static int OpenDll( decoder_t *p_dec )
276 /** Find the good path for the dlls.**/
277 const char *ppsz_path[] =
281 "/usr/local/RealPlayer8/Codecs",
282 "/usr/RealPlayer8/Codecs",
283 "/usr/lib/RealPlayer8/Codecs",
284 "/opt/RealPlayer8/Codecs",
285 "/usr/lib/RealPlayer9/users/Real/Codecs",
286 "/usr/lib/RealPlayer10/codecs",
287 "/usr/lib/RealPlayer10GOLD/codecs",
288 "/usr/lib/helix/player/codecs",
289 "/usr/lib64/RealPlayer8/Codecs",
290 "/usr/lib64/RealPlayer9/users/Real/Codecs",
291 "/usr/lib64/RealPlayer10/codecs",
292 "/usr/lib64/RealPlayer10GOLD/codecs",
295 "/usr/local/lib/codecs",
303 char psz_win32_real_codecs[MAX_PATH + 1];
304 char psz_win32_helix_codecs[MAX_PATH + 1];
308 DWORD i_type, i_data = MAX_PATH + 1, i_index = 1;
311 p_data = psz_win32_real_codecs;
312 if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
313 _T("Software\\RealNetworks\\Preferences\\DT_Codecs"),
314 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
316 if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
317 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
320 int i_len = strlen( p_data );
321 if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
322 ppsz_path[i_index++] = p_data;
323 msg_Err( p_dec, "Real: %s", p_data );
325 RegCloseKey( h_key );
328 p_data = psz_win32_helix_codecs;
329 if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
330 _T("Helix\\HelixSDK\\10.0\\Preferences\\DT_Codecs"),
331 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
333 if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
334 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
337 int i_len = strlen( p_data );
338 if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
339 ppsz_path[i_index++] = p_data;
340 msg_Err( p_dec, "Real: %s", p_data );
342 RegCloseKey( h_key );
348 /** Try the native libraries first **/
350 for( i = 0; ppsz_path[i]; i++ )
353 if( asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
354 (char *)&p_dec->fmt_in.i_codec ) != -1 )
356 i_result = OpenNativeDll( p_dec, (char *)ppsz_path[i], psz_dll );
358 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
362 if( asprintf( &psz_dll, "%s/%4.4s.so", ppsz_path[i],
363 (char *)&p_dec->fmt_in.i_codec ) != -1 )
365 i_result = OpenNativeDll( p_dec, (char *)ppsz_path[i], psz_dll );
367 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
372 /** Or use the WIN32 dlls **/
373 #if defined(LOADER) || defined(WIN32)
374 for( i = 0; ppsz_path[i]; i++ )
377 if( asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
378 (char *)&p_dec->fmt_in.i_codec ) != -1 )
380 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
382 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
386 if( asprintf( &psz_dll, "%s\\%4.4s3260.dll", ppsz_path[i],
387 (char *)&p_dec->fmt_in.i_codec ) != -1 )
389 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
391 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
400 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
402 #if defined(HAVE_DL_DLOPEN)
403 decoder_sys_t *p_sys = p_dec->p_sys;
404 void *handle = 0, *context = 0;
405 unsigned int i_result;
409 ra_init_t init_data =
411 p_dec->fmt_in.audio.i_rate,
412 p_dec->fmt_in.audio.i_bitspersample,
413 p_dec->fmt_in.audio.i_channels,
415 p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
416 p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
417 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
420 msg_Dbg( p_dec, "opening library '%s'", psz_dll );
421 if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
423 msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
424 psz_dll, dlerror() );
428 p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
429 p_sys->raDecode = dlsym( handle, "RADecode" );
430 p_sys->raFlush = dlsym( handle, "RAFlush" );
431 p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
432 p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
433 p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
434 p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
435 p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
436 p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
437 p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
438 p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
440 if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
441 !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
442 !p_sys->raDecode || !p_sys->raFreeDecoder ||
443 !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
444 /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
449 if( p_sys->raOpenCodec2 )
450 i_result = p_sys->raOpenCodec2( &context, psz_path );
452 i_result = p_sys->raOpenCodec( &context );
456 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
460 i_result = p_sys->raInitDecoder( context, &init_data );
463 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
467 if( p_sys->i_codec_flavor >= 0 )
469 i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
472 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
477 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
479 msg_Dbg( p_dec, "audio codec: [%d] %s",
480 p_sys->i_codec_flavor, (char *)p_prop );
482 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
486 int i_bps = ((*((int*)p_prop))+4)/8;
487 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
488 (*((int*)p_prop))*0.001f, i_bps );
492 p_sys->context = context;
497 if( context ) p_sys->raFreeDecoder( context );
498 if( context ) p_sys->raCloseCodec( context );
502 VLC_UNUSED( psz_path);
503 VLC_UNUSED( psz_dll );
510 #if defined(LOADER) || defined(WIN32)
511 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
513 decoder_sys_t *p_sys = p_dec->p_sys;
514 void *handle = 0, *context = 0;
515 unsigned int i_result;
519 wra_init_t init_data =
521 p_dec->fmt_in.audio.i_rate,
522 p_dec->fmt_in.audio.i_bitspersample,
523 p_dec->fmt_in.audio.i_channels,
525 p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
526 p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
527 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
530 msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
536 if( !(handle = LoadLibraryA( psz_dll )) )
538 msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
542 p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
543 p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
544 p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
545 p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
546 p_sys->wraGetFlavorProperty =
547 GetProcAddress( handle, "RAGetFlavorProperty" );
548 p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
549 p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
550 p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
551 p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
552 p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
554 GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
556 if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
557 !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
558 !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
559 !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
560 /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
562 FreeLibrary( handle );
566 if( p_sys->wraOpenCodec2 )
567 i_result = p_sys->wraOpenCodec2( &context, psz_path );
569 i_result = p_sys->wraOpenCodec( &context );
573 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
577 i_result = p_sys->wraInitDecoder( context, &init_data );
580 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
584 if( p_sys->i_codec_flavor >= 0 )
586 i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
589 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
594 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
596 msg_Dbg( p_dec, "audio codec: [%d] %s",
597 p_sys->i_codec_flavor, (char *)p_prop );
599 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
603 int i_bps = ((*((int*)p_prop))+4)/8;
604 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
605 (*((int*)p_prop))*0.001f, i_bps );
609 p_sys->context = context;
610 p_sys->win32_dll = handle;
614 if( context ) p_sys->wraFreeDecoder( context );
615 if( context ) p_sys->wraCloseCodec( context );
616 FreeLibrary( handle );
622 /*****************************************************************************
624 *****************************************************************************/
625 static void CloseDll( decoder_t *p_dec )
627 decoder_sys_t *p_sys = p_dec->p_sys;
629 if( p_sys->context && p_sys->dll )
631 p_sys->raFreeDecoder( p_sys->context );
632 p_sys->raCloseCodec( p_sys->context );
635 if( p_sys->context && p_sys->win32_dll )
637 p_sys->wraFreeDecoder( p_sys->context );
638 p_sys->wraCloseCodec( p_sys->context );
641 #if defined(HAVE_DL_DLOPEN)
642 if( p_sys->dll ) dlclose( p_sys->dll );
645 #if defined(LOADER) || defined(WIN32)
646 if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
648 #if 0 //def LOADER /* Segfaults */
649 Restore_LDT_Keeper( p_sys->ldt_fs );
650 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
655 p_sys->win32_dll = 0;
659 /*****************************************************************************
661 *****************************************************************************/
662 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
664 decoder_sys_t *p_sys = p_dec->p_sys;
665 aout_buffer_t *p_aout_buffer = 0;
666 unsigned int i_result;
671 if( !p_sys->win32_dll && !p_sys->dll )
673 /* We must do open and close in the same thread (unless we do
674 * Setup_LDT_Keeper in the main thread before all others */
675 if( OpenDll( p_dec ) != VLC_SUCCESS )
678 p_dec->b_error = true;
684 if( pp_block == NULL || *pp_block == NULL ) return NULL;
688 i_result = p_sys->raDecode( p_sys->context, (char *)p_block->p_buffer,
689 (unsigned long)p_block->i_buffer,
690 p_sys->p_out, &p_sys->i_out, -1 );
692 i_result = p_sys->wraDecode( p_sys->context, (char *)p_block->p_buffer,
693 (unsigned long)p_block->i_buffer,
694 p_sys->p_out, &p_sys->i_out, -1 );
697 msg_Err( p_dec, "decoded: %i samples (%i)",
698 p_sys->i_out * 8 / p_dec->fmt_out.audio.i_bitspersample /
699 p_dec->fmt_out.audio.i_channels, i_result );
702 /* Date management */
703 if( p_block->i_pts > VLC_TS_INVALID &&
704 p_block->i_pts != date_Get( &p_sys->end_date ) )
706 date_Set( &p_sys->end_date, p_block->i_pts );
709 if( !date_Get( &p_sys->end_date ) )
711 /* We've just started the stream, wait for the first PTS. */
712 if( p_block ) block_Release( p_block );
716 i_samples = p_sys->i_out * 8 /
717 p_dec->fmt_out.audio.i_bitspersample /p_dec->fmt_out.audio.i_channels;
720 decoder_NewAudioBuffer( p_dec, i_samples );
723 memcpy( p_aout_buffer->p_buffer, p_sys->p_out, p_sys->i_out );
725 /* Date management */
726 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
727 p_aout_buffer->i_length = date_Increment( &p_sys->end_date, i_samples )
728 - p_aout_buffer->i_pts;
731 block_Release( p_block );
733 return p_aout_buffer;