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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 *****************************************************************************/
22 /*****************************************************************************
24 *****************************************************************************/
28 #include <vlc/decoder.h>
31 /* Need the w32dll loader from mplayer */
32 # include <wine/winerror.h>
33 # include <ldt_keeper.h>
34 # include <wine/windef.h>
36 void *WINAPI LoadLibraryA( char *name );
37 void *WINAPI GetProcAddress( void *handle, char *func );
38 int WINAPI FreeLibrary( void *handle );
45 #if defined(HAVE_DL_DLOPEN)
46 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
49 # if defined(HAVE_SYS_DL_H)
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
61 set_description( _("RealAudio library decoder") );
62 set_capability( "decoder", 10 );
63 set_category( CAT_INPUT );
64 set_subcategory( SUBCAT_INPUT_VCODEC );
65 set_callbacks( Open, Close );
68 /*****************************************************************************
70 *****************************************************************************/
71 static int OpenDll( decoder_t * );
72 static int OpenNativeDll( decoder_t *, char *, char * );
73 static int OpenWin32Dll( decoder_t *, char *, char * );
74 static void CloseDll( decoder_t * );
76 static aout_buffer_t *Decode( decoder_t *, block_t ** );
80 audio_date_t end_date;
82 /* Frame buffer for data reordering */
99 int i_coded_frame_size;
104 unsigned long (*raCloseCodec)(void*);
105 unsigned long (*raDecode)(void*, char*, unsigned long, char*,
106 unsigned int*, long);
107 unsigned long (*raFlush)(unsigned long, unsigned long, unsigned long);
108 unsigned long (*raFreeDecoder)(void*);
109 void* (*raGetFlavorProperty)(void*, unsigned long,
110 unsigned long, int*);
111 unsigned long (*raInitDecoder)(void*, void*);
112 unsigned long (*raOpenCodec)(void*);
113 unsigned long (*raOpenCodec2)(void*, void*);
114 unsigned long (*raSetFlavor)(void*, unsigned long);
115 void (*raSetDLLAccessPath)(char*);
116 void (*raSetPwd)(char*, char*);
123 unsigned long WINAPI (*wraCloseCodec)(void*);
124 unsigned long WINAPI (*wraDecode)(void*, char*, unsigned long, char*,
125 unsigned int*, long);
126 unsigned long WINAPI (*wraFlush)(unsigned long, unsigned long,
128 unsigned long WINAPI (*wraFreeDecoder)(void*);
129 void* WINAPI (*wraGetFlavorProperty)(void*, unsigned long,
130 unsigned long, int*);
131 unsigned long WINAPI (*wraInitDecoder)(void*, void*);
132 unsigned long WINAPI (*wraOpenCodec)(void*);
133 unsigned long WINAPI (*wraOpenCodec2)(void*, void*);
134 unsigned long WINAPI (*wraSetFlavor)(void*, unsigned long);
135 void WINAPI (*wraSetDLLAccessPath)(char*);
136 void WINAPI (*wraSetPwd)(char*, char*);
139 /* linux dlls doesn't need packing */
140 typedef struct /*__attribute__((__packed__))*/ {
145 /* 2bytes padding here, by gcc */
152 /* windows dlls need packed structs (no padding) */
153 typedef struct __attribute__((__packed__)) {
164 void *__builtin_new(unsigned long size) {return malloc(size);}
165 void __builtin_delete(void *p) {free(p);}
167 static int pi_channels_maps[7] =
171 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
172 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
173 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
174 | AOUT_CHAN_REARRIGHT,
175 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
176 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
177 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
178 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
181 /*****************************************************************************
182 * Open: probe the decoder and return score
183 *****************************************************************************
184 * Tries to launch a decoder and return score so that the interface is able
186 *****************************************************************************/
187 static int Open( vlc_object_t *p_this )
189 decoder_t *p_dec = (decoder_t*)p_this;
190 decoder_sys_t *p_sys = p_dec->p_sys;
192 switch( p_dec->fmt_in.i_codec )
194 case VLC_FOURCC('c','o','o','k'):
201 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
202 memset( p_sys, 0, sizeof(decoder_sys_t) );
204 if( p_dec->fmt_in.i_extra >= 10 )
206 p_sys->i_subpacket_size = ((short *)(p_dec->fmt_in.p_extra))[0];
207 p_sys->i_subpacket_h = ((short *)(p_dec->fmt_in.p_extra))[1];
208 p_sys->i_codec_flavor = ((short *)(p_dec->fmt_in.p_extra))[2];
209 p_sys->i_coded_frame_size = ((short *)(p_dec->fmt_in.p_extra))[3];
210 p_sys->i_extra = ((short*)(p_dec->fmt_in.p_extra))[4];
211 p_sys->p_extra = p_dec->fmt_in.p_extra + 10;
214 if( OpenDll( p_dec ) != VLC_SUCCESS )
221 if( p_sys->win32_dll ) Close( p_this );
224 es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
225 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
226 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
227 p_dec->fmt_out.audio.i_bitspersample =
228 p_dec->fmt_in.audio.i_bitspersample;
229 p_dec->fmt_out.audio.i_physical_channels =
230 p_dec->fmt_out.audio.i_original_channels =
231 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
233 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
234 aout_DateSet( &p_sys->end_date, 0 );
236 p_dec->pf_decode_audio = Decode;
238 p_sys->i_frame_size =
239 p_dec->fmt_in.audio.i_blockalign * p_sys->i_subpacket_h;
240 p_sys->p_frame = malloc( p_sys->i_frame_size );
241 p_sys->i_packet_pts = 0;
242 p_sys->i_subpacket = 0;
245 p_sys->p_out_buffer = malloc( 4096 * 10 );
252 /*****************************************************************************
254 *****************************************************************************/
255 static void Close( vlc_object_t *p_this )
257 decoder_t *p_dec = (decoder_t*)p_this;
260 if( p_dec->p_sys->p_frame ) free( p_dec->p_sys->p_frame );
261 if( p_dec->p_sys->p_out_buffer ) free( p_dec->p_sys->p_out_buffer );
262 free( p_dec->p_sys );
265 /*****************************************************************************
267 *****************************************************************************/
268 static int OpenDll( decoder_t *p_dec )
277 "/usr/local/RealPlayer8/Codecs",
278 "/usr/RealPlayer8/Codecs",
279 "/usr/lib/RealPlayer8/Codecs",
280 "/opt/RealPlayer8/Codecs",
281 "/usr/lib/RealPlayer9/users/Real/Codecs",
282 "/usr/lib64/RealPlayer8/Codecs",
283 "/usr/lib64/RealPlayer9/users/Real/Codecs",
292 char psz_win32_real_codecs[MAX_PATH + 1];
293 char psz_win32_helix_codecs[MAX_PATH + 1];
296 for( i = 0; ppsz_path[i]; i++ )
298 asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
299 (char *)&p_dec->fmt_in.i_codec );
300 i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
302 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
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 );
347 for( i = 0; ppsz_path[i]; i++ )
350 asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
351 (char *)&p_dec->fmt_in.i_codec );
352 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
354 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
357 asprintf( &psz_dll, "%s\\%4.4s3260.dll", ppsz_path[i],
358 (char *)&p_dec->fmt_in.i_codec );
359 i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
361 if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
367 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
369 #if defined(HAVE_DL_DLOPEN)
370 decoder_sys_t *p_sys = p_dec->p_sys;
371 void *handle = 0, *context = 0;
372 unsigned int i_result;
376 ra_init_t init_data =
378 p_dec->fmt_in.audio.i_rate,
379 p_dec->fmt_in.audio.i_bitspersample,
380 p_dec->fmt_in.audio.i_channels,
382 p_sys->i_subpacket_size,
383 p_sys->i_coded_frame_size,
384 p_sys->i_extra, p_sys->p_extra
387 msg_Dbg( p_dec, "opening library '%s'", psz_dll );
388 if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
390 msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
391 psz_dll, dlerror() );
395 p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
396 p_sys->raDecode = dlsym( handle, "RADecode" );
397 p_sys->raFlush = dlsym( handle, "RAFlush" );
398 p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
399 p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
400 p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
401 p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
402 p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
403 p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
404 p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
405 p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
407 if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
408 !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
409 !p_sys->raDecode || !p_sys->raFreeDecoder ||
410 !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
411 /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
416 if( p_sys->raOpenCodec2 )
417 i_result = p_sys->raOpenCodec2( &context, psz_path );
419 i_result = p_sys->raOpenCodec( &context );
423 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
427 i_result = p_sys->raInitDecoder( context, &init_data );
430 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
434 i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
437 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
442 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
444 msg_Dbg( p_dec, "audio codec: [%d] %s",
445 p_sys->i_codec_flavor, (char *)p_prop );
447 p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
451 int i_bps = ((*((int*)p_prop))+4)/8;
452 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
453 (*((int*)p_prop))*0.001f, i_bps );
456 p_sys->context = context;
461 if( context ) p_sys->raFreeDecoder( context );
462 if( context ) p_sys->raCloseCodec( context );
469 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
471 #if defined(LOADER) || defined(WIN32)
472 decoder_sys_t *p_sys = p_dec->p_sys;
473 void *handle = 0, *context = 0;
474 unsigned int i_result;
478 wra_init_t init_data =
480 p_dec->fmt_in.audio.i_rate,
481 p_dec->fmt_in.audio.i_bitspersample,
482 p_dec->fmt_in.audio.i_channels,
484 p_sys->i_subpacket_size,
485 p_sys->i_coded_frame_size,
486 p_sys->i_extra, p_sys->p_extra
489 msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
495 if( !(handle = LoadLibraryA( psz_dll )) )
497 msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
501 p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
502 p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
503 p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
504 p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
505 p_sys->wraGetFlavorProperty =
506 GetProcAddress( handle, "RAGetFlavorProperty" );
507 p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
508 p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
509 p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
510 p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
511 p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
513 GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
515 if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
516 !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
517 !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
518 !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
519 /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
521 FreeLibrary( handle );
525 if( p_sys->wraOpenCodec2 )
526 i_result = p_sys->wraOpenCodec2( &context, psz_path );
528 i_result = p_sys->wraOpenCodec( &context );
532 msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
536 i_result = p_sys->wraInitDecoder( context, &init_data );
539 msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
543 i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
546 msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
551 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
553 msg_Dbg( p_dec, "audio codec: [%d] %s",
554 p_sys->i_codec_flavor, (char *)p_prop );
556 p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
560 int i_bps = ((*((int*)p_prop))+4)/8;
561 msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
562 (*((int*)p_prop))*0.001f, i_bps );
565 p_sys->context = context;
566 p_sys->win32_dll = handle;
570 if( context ) p_sys->wraFreeDecoder( context );
571 if( context ) p_sys->wraCloseCodec( context );
572 FreeLibrary( handle );
578 /*****************************************************************************
580 *****************************************************************************/
581 static void CloseDll( decoder_t *p_dec )
583 decoder_sys_t *p_sys = p_dec->p_sys;
585 if( p_sys->context && p_sys->dll )
587 p_sys->raFreeDecoder( p_sys->context );
588 p_sys->raCloseCodec( p_sys->context );
591 if( p_sys->context && p_sys->win32_dll )
593 p_sys->wraFreeDecoder( p_sys->context );
594 p_sys->wraCloseCodec( p_sys->context );
597 #if defined(HAVE_DL_DLOPEN)
598 if( p_sys->dll ) dlclose( p_sys->dll );
601 #if defined(LOADER) || defined(WIN32)
602 if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
604 #if 0 //def LOADER /* Segfaults */
605 Restore_LDT_Keeper( p_sys->ldt_fs );
606 msg_Dbg( p_dec, "Restore_LDT_Keeper" );
611 p_sys->win32_dll = 0;
615 /*****************************************************************************
617 *****************************************************************************/
618 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
620 decoder_sys_t *p_sys = p_dec->p_sys;
621 aout_buffer_t *p_aout_buffer = 0;
625 if( !p_sys->win32_dll && !p_sys->dll )
627 /* We must do open and close in the same thread (unless we do
628 * Setup_LDT_Keeper in the main thread before all others */
629 if( OpenDll( p_dec ) != VLC_SUCCESS )
632 p_dec->b_error = VLC_TRUE;
638 /* Output 1024 samples at a time
639 * (the audio output doesn't like big audio packets) */
643 int i_samples = p_sys->i_out * 8 /
644 p_dec->fmt_out.audio.i_bitspersample /
645 p_dec->fmt_out.audio.i_channels;
647 i_samples = __MIN( i_samples, 1024 );
648 i_out = i_samples * p_dec->fmt_out.audio.i_bitspersample *
649 p_dec->fmt_out.audio.i_channels / 8;
651 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
652 if( p_aout_buffer == NULL ) return NULL;
653 memcpy( p_aout_buffer->p_buffer, p_sys->p_out, i_out );
655 p_sys->i_out -= i_out;
656 p_sys->p_out += i_out;
658 /* Date management */
659 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
660 p_aout_buffer->end_date =
661 aout_DateIncrement( &p_sys->end_date, i_samples );
663 return p_aout_buffer;
666 /* If we have a full frame ready then we can start decoding */
669 unsigned int i_result;
671 p_sys->p_out = p_sys->p_out_buffer;
674 i_result = p_sys->raDecode( p_sys->context, p_sys->p_frame +
675 p_sys->i_frame_size - p_sys->i_frame,
676 p_dec->fmt_in.audio.i_blockalign,
677 p_sys->p_out, &p_sys->i_out, -1 );
679 i_result = p_sys->wraDecode( p_sys->context, p_sys->p_frame +
680 p_sys->i_frame_size - p_sys->i_frame,
681 p_dec->fmt_in.audio.i_blockalign,
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 p_sys->i_frame -= p_dec->fmt_in.audio.i_blockalign;
692 return Decode( p_dec, pp_block );
696 if( pp_block == NULL || *pp_block == NULL ) return NULL;
699 //msg_Err( p_dec, "Decode: "I64Fd", %i", p_block->i_pts, p_block->i_buffer );
701 /* Detect missing subpackets */
702 if( p_sys->i_subpacket && p_block->i_pts > 0 &&
703 p_block->i_pts != p_sys->i_packet_pts )
705 /* All subpackets in a packet should have the same pts so we must
706 * have dropped some. Clear current frame buffer. */
707 p_sys->i_subpacket = 0;
708 msg_Dbg( p_dec, "detected dropped subpackets" );
710 if( p_block->i_pts > 0 ) p_sys->i_packet_pts = p_block->i_pts;
712 /* Date management */
713 if( /* !p_sys->i_subpacket && */ p_block && p_block->i_pts > 0 &&
714 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
716 aout_DateSet( &p_sys->end_date, p_block->i_pts );
720 if( !aout_DateGet( &p_sys->end_date ) )
722 /* We've just started the stream, wait for the first PTS. */
723 if( p_block ) block_Release( p_block );
728 if( p_sys->i_subpacket_size )
731 int sps = p_sys->i_subpacket_size;
732 int w = p_dec->fmt_in.audio.i_blockalign;
733 int h = p_sys->i_subpacket_h;
734 /* int cfs = p_sys->i_coded_frame_size; */
738 y = p_sys->i_subpacket;
740 for( x = 0; x < w; x++ )
742 memcpy( p_sys->p_frame + sps * (h*x+((h+1)/2)*(y&1)+(y>>1)),
743 p_block->p_buffer, sps );
744 p_block->p_buffer += sps;
747 p_sys->i_subpacket = (p_sys->i_subpacket + 1) % p_sys->i_subpacket_h;
749 if( !p_sys->i_subpacket )
751 /* We have a complete frame */
752 p_sys->i_frame = p_sys->i_frame_size;
753 p_aout_buffer = Decode( p_dec, pp_block );
757 block_Release( p_block );
759 return p_aout_buffer;