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 *****************************************************************************/
27 #include <vlc_codec.h>
30 /* Need the w32dll loader from mplayer */
31 # include <wine/winerror.h>
32 # include <ldt_keeper.h>
33 # include <wine/windef.h>
35 void *WINAPI LoadLibraryA( char *name );
36 void *WINAPI GetProcAddress( void *handle, char *func );
37 int WINAPI FreeLibrary( void *handle );
44 #if defined(HAVE_DL_DLOPEN)
45 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
48 # if defined(HAVE_SYS_DL_H)
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
60 set_description( _("RealAudio library decoder") );
61 set_capability( "decoder", 10 );
62 set_category( CAT_INPUT );
63 set_subcategory( SUBCAT_INPUT_VCODEC );
64 set_callbacks( Open, Close );
67 /*****************************************************************************
69 *****************************************************************************/
70 static int OpenDll( decoder_t * );
71 static int OpenNativeDll( decoder_t *, char *, char * );
72 static int OpenWin32Dll( decoder_t *, char *, char * );
73 static void CloseDll( decoder_t * );
75 static aout_buffer_t *Decode( decoder_t *, block_t ** );
79 audio_date_t end_date;
90 unsigned long (*raCloseCodec)(void*);
91 unsigned long (*raDecode)(void*, char*, unsigned long, char*,
93 unsigned long (*raFlush)(unsigned long, unsigned long, unsigned long);
94 unsigned long (*raFreeDecoder)(void*);
95 void* (*raGetFlavorProperty)(void*, unsigned long,
97 unsigned long (*raInitDecoder)(void*, void*);
98 unsigned long (*raOpenCodec)(void*);
99 unsigned long (*raOpenCodec2)(void*, void*);
100 unsigned long (*raSetFlavor)(void*, unsigned long);
101 void (*raSetDLLAccessPath)(char*);
102 void (*raSetPwd)(char*, char*);
109 unsigned long (WINAPI *wraCloseCodec)(void*);
110 unsigned long (WINAPI *wraDecode)(void*, char*, unsigned long, char*,
111 unsigned int*, long);
112 unsigned long (WINAPI *wraFlush)(unsigned long, unsigned long,
114 unsigned long (WINAPI *wraFreeDecoder)(void*);
115 void* (WINAPI *wraGetFlavorProperty)(void*, unsigned long,
116 unsigned long, int*);
117 unsigned long (WINAPI *wraInitDecoder)(void*, void*);
118 unsigned long (WINAPI *wraOpenCodec)(void*);
119 unsigned long (WINAPI *wraOpenCodec2)(void*, void*);
120 unsigned long (WINAPI *wraSetFlavor)(void*, unsigned long);
121 void (WINAPI *wraSetDLLAccessPath)(char*);
122 void (WINAPI *wraSetPwd)(char*, char*);
125 /* linux dlls doesn't need packing */
126 typedef struct /*__attribute__((__packed__))*/ {
131 /* 2bytes padding here, by gcc */
138 /* windows dlls need packed structs (no padding) */
139 typedef struct __attribute__((__packed__)) {
150 void *__builtin_new(unsigned long size) {return malloc(size);}
151 void __builtin_delete(void *p) {free(p);}
153 static int pi_channels_maps[7] =
157 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
158 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
159 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
160 | AOUT_CHAN_REARRIGHT,
161 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
162 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
163 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
164 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
167 /*****************************************************************************
168 * Open: probe the decoder and return score
169 *****************************************************************************
170 * Tries to launch a decoder and return score so that the interface is able
172 *****************************************************************************/
173 static int Open( vlc_object_t *p_this )
175 decoder_t *p_dec = (decoder_t*)p_this;
176 decoder_sys_t *p_sys = p_dec->p_sys;
178 switch( p_dec->fmt_in.i_codec )
180 case VLC_FOURCC('c','o','o','k'):
187 if( p_dec->fmt_in.audio.i_channels <= 0 ||
188 p_dec->fmt_in.audio.i_channels > 6 )
190 msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
191 p_dec->fmt_in.audio.i_channels );
195 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
196 memset( p_sys, 0, sizeof(decoder_sys_t) );
198 p_sys->i_codec_flavor = -1;
199 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','i','p','r') )
201 if( p_dec->fmt_in.audio.i_bitspersample > 1531 )
202 p_sys->i_codec_flavor = 3;
203 else if( p_dec->fmt_in.audio.i_bitspersample > 937 )
204 p_sys->i_codec_flavor = 1;
205 else if( p_dec->fmt_in.audio.i_bitspersample > 719 )
206 p_sys->i_codec_flavor = 0;
208 p_sys->i_codec_flavor = 2;
211 if( OpenDll( p_dec ) != VLC_SUCCESS )
218 if( p_sys->win32_dll ) Close( p_this );
221 es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
222 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
223 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
224 p_dec->fmt_out.audio.i_bitspersample =
225 p_dec->fmt_in.audio.i_bitspersample;
226 p_dec->fmt_out.audio.i_physical_channels =
227 p_dec->fmt_out.audio.i_original_channels =
228 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
230 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
231 aout_DateSet( &p_sys->end_date, 0 );
233 p_dec->pf_decode_audio = Decode;
235 p_sys->p_out = malloc( 4096 * 10 );
241 /*****************************************************************************
243 *****************************************************************************/
244 static void Close( vlc_object_t *p_this )
246 decoder_t *p_dec = (decoder_t*)p_this;
249 if( p_dec->p_sys->p_out ) free( p_dec->p_sys->p_out );
250 free( p_dec->p_sys );
253 /*****************************************************************************
255 *****************************************************************************/
256 static int OpenDll( decoder_t *p_dec )
265 "/usr/local/RealPlayer8/Codecs",
266 "/usr/RealPlayer8/Codecs",
267 "/usr/lib/RealPlayer8/Codecs",
268 "/opt/RealPlayer8/Codecs",
269 "/usr/lib/RealPlayer9/users/Real/Codecs",
270 "/usr/lib/RealPlayer10/codecs",
271 "/usr/lib/RealPlayer10GOLD/codecs",
272 "/usr/lib/helix/player/codecs",
273 "/usr/lib64/RealPlayer8/Codecs",
274 "/usr/lib64/RealPlayer9/users/Real/Codecs",
275 "/usr/lib64/RealPlayer10/codecs",
276 "/usr/lib64/RealPlayer10GOLD/codecs",
279 "/usr/local/lib/codecs",
287 char psz_win32_real_codecs[MAX_PATH + 1];
288 char psz_win32_helix_codecs[MAX_PATH + 1];
291 for( i = 0; ppsz_path[i]; i++ )
294 asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
295 (char *)&p_dec->fmt_in.i_codec );
296 i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
298 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
301 asprintf( &psz_dll, "%s/%4.4s.so", ppsz_path[i],
302 (char *)&p_dec->fmt_in.i_codec );
303 i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
305 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
312 DWORD i_type, i_data = MAX_PATH + 1, i_index = 1;
315 p_data = psz_win32_real_codecs;
316 if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
317 _T("Software\\RealNetworks\\Preferences\\DT_Codecs"),
318 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
320 if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
321 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
324 int i_len = strlen( p_data );
325 if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
326 ppsz_path[i_index++] = p_data;
327 msg_Err( p_dec, "Real: %s", p_data );
329 RegCloseKey( h_key );
332 p_data = psz_win32_helix_codecs;
333 if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
334 _T("Helix\\HelixSDK\\10.0\\Preferences\\DT_Codecs"),
335 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
337 if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
338 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
341 int i_len = strlen( p_data );
342 if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
343 ppsz_path[i_index++] = p_data;
344 msg_Err( p_dec, "Real: %s", p_data );
346 RegCloseKey( h_key );
351 for( i = 0; ppsz_path[i]; i++ )
354 asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
355 (char *)&p_dec->fmt_in.i_codec );
356 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
358 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
361 asprintf( &psz_dll, "%s\\%4.4s3260.dll", ppsz_path[i],
362 (char *)&p_dec->fmt_in.i_codec );
363 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
365 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
371 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
373 #if defined(HAVE_DL_DLOPEN)
374 decoder_sys_t *p_sys = p_dec->p_sys;
375 void *handle = 0, *context = 0;
376 unsigned int i_result;
380 ra_init_t init_data =
382 p_dec->fmt_in.audio.i_rate,
383 p_dec->fmt_in.audio.i_bitspersample,
384 p_dec->fmt_in.audio.i_channels,
386 p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
387 p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
388 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
391 msg_Dbg( p_dec, "opening library '%s'", psz_dll );
392 if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
394 msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
395 psz_dll, dlerror() );
399 p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
400 p_sys->raDecode = dlsym( handle, "RADecode" );
401 p_sys->raFlush = dlsym( handle, "RAFlush" );
402 p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
403 p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
404 p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
405 p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
406 p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
407 p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
408 p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
409 p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
411 if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
412 !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
413 !p_sys->raDecode || !p_sys->raFreeDecoder ||
414 !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
415 /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
420 if( p_sys->raOpenCodec2 )
421 i_result = p_sys->raOpenCodec2( &context, psz_path );
423 i_result = p_sys->raOpenCodec( &context );
427 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
431 i_result = p_sys->raInitDecoder( context, &init_data );
434 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
438 if( p_sys->i_codec_flavor >= 0 )
440 i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
443 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
448 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
450 msg_Dbg( p_dec, "audio codec: [%d] %s",
451 p_sys->i_codec_flavor, (char *)p_prop );
453 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
457 int i_bps = ((*((int*)p_prop))+4)/8;
458 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
459 (*((int*)p_prop))*0.001f, i_bps );
463 p_sys->context = context;
468 if( context ) p_sys->raFreeDecoder( context );
469 if( context ) p_sys->raCloseCodec( context );
476 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
478 #if defined(LOADER) || defined(WIN32)
479 decoder_sys_t *p_sys = p_dec->p_sys;
480 void *handle = 0, *context = 0;
481 unsigned int i_result;
485 wra_init_t init_data =
487 p_dec->fmt_in.audio.i_rate,
488 p_dec->fmt_in.audio.i_bitspersample,
489 p_dec->fmt_in.audio.i_channels,
491 p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
492 p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
493 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
496 msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
502 if( !(handle = LoadLibraryA( psz_dll )) )
504 msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
508 p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
509 p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
510 p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
511 p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
512 p_sys->wraGetFlavorProperty =
513 GetProcAddress( handle, "RAGetFlavorProperty" );
514 p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
515 p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
516 p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
517 p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
518 p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
520 GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
522 if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
523 !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
524 !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
525 !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
526 /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
528 FreeLibrary( handle );
532 if( p_sys->wraOpenCodec2 )
533 i_result = p_sys->wraOpenCodec2( &context, psz_path );
535 i_result = p_sys->wraOpenCodec( &context );
539 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
543 i_result = p_sys->wraInitDecoder( context, &init_data );
546 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
550 if( p_sys->i_codec_flavor >= 0 )
552 i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
555 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
560 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
562 msg_Dbg( p_dec, "audio codec: [%d] %s",
563 p_sys->i_codec_flavor, (char *)p_prop );
565 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
569 int i_bps = ((*((int*)p_prop))+4)/8;
570 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
571 (*((int*)p_prop))*0.001f, i_bps );
575 p_sys->context = context;
576 p_sys->win32_dll = handle;
580 if( context ) p_sys->wraFreeDecoder( context );
581 if( context ) p_sys->wraCloseCodec( context );
582 FreeLibrary( handle );
588 /*****************************************************************************
590 *****************************************************************************/
591 static void CloseDll( decoder_t *p_dec )
593 decoder_sys_t *p_sys = p_dec->p_sys;
595 if( p_sys->context && p_sys->dll )
597 p_sys->raFreeDecoder( p_sys->context );
598 p_sys->raCloseCodec( p_sys->context );
601 if( p_sys->context && p_sys->win32_dll )
603 p_sys->wraFreeDecoder( p_sys->context );
604 p_sys->wraCloseCodec( p_sys->context );
607 #if defined(HAVE_DL_DLOPEN)
608 if( p_sys->dll ) dlclose( p_sys->dll );
611 #if defined(LOADER) || defined(WIN32)
612 if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
614 #if 0 //def LOADER /* Segfaults */
615 Restore_LDT_Keeper( p_sys->ldt_fs );
616 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
621 p_sys->win32_dll = 0;
625 /*****************************************************************************
627 *****************************************************************************/
628 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
630 decoder_sys_t *p_sys = p_dec->p_sys;
631 aout_buffer_t *p_aout_buffer = 0;
632 unsigned int i_result;
637 if( !p_sys->win32_dll && !p_sys->dll )
639 /* We must do open and close in the same thread (unless we do
640 * Setup_LDT_Keeper in the main thread before all others */
641 if( OpenDll( p_dec ) != VLC_SUCCESS )
644 p_dec->b_error = VLC_TRUE;
650 if( pp_block == NULL || *pp_block == NULL ) return NULL;
654 i_result = p_sys->raDecode( p_sys->context, (char *)p_block->p_buffer,
655 (unsigned long)p_block->i_buffer,
656 p_sys->p_out, &p_sys->i_out, -1 );
658 i_result = p_sys->wraDecode( p_sys->context, (char *)p_block->p_buffer,
659 (unsigned long)p_block->i_buffer,
660 p_sys->p_out, &p_sys->i_out, -1 );
663 msg_Err( p_dec, "decoded: %i samples (%i)",
664 p_sys->i_out * 8 / p_dec->fmt_out.audio.i_bitspersample /
665 p_dec->fmt_out.audio.i_channels, i_result );
668 /* Date management */
669 if( p_block->i_pts > 0 &&
670 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
672 aout_DateSet( &p_sys->end_date, p_block->i_pts );
675 if( !aout_DateGet( &p_sys->end_date ) )
677 /* We've just started the stream, wait for the first PTS. */
678 if( p_block ) block_Release( p_block );
682 i_samples = p_sys->i_out * 8 /
683 p_dec->fmt_out.audio.i_bitspersample /p_dec->fmt_out.audio.i_channels;
686 p_dec->pf_aout_buffer_new( p_dec, i_samples );
689 memcpy( p_aout_buffer->p_buffer, p_sys->p_out, p_sys->i_out );
691 /* Date management */
692 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
693 p_aout_buffer->end_date =
694 aout_DateIncrement( &p_sys->end_date, i_samples );
697 block_Release( p_block );
699 return p_aout_buffer;