]> git.sesse.net Git - vlc/blob - modules/codec/realaudio.c
jvlc: code formatted
[vlc] / modules / codec / realaudio.c
1 /*****************************************************************************
2  * realaudio.c: a realaudio decoder that uses the realaudio library/dll
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 /*****************************************************************************
23  * Preamble
24  *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_aout.h>
32 #include <vlc_codec.h>
33
34 #ifdef LOADER
35 /* Need the w32dll loader from mplayer */
36 #   include <wine/winerror.h>
37 #   include <ldt_keeper.h>
38 #   include <wine/windef.h>
39
40 void *WINAPI LoadLibraryA( char *name );
41 void *WINAPI GetProcAddress( void *handle, char *func );
42 int WINAPI FreeLibrary( void *handle );
43 #endif
44
45 #ifndef WINAPI
46 #   define WINAPI
47 #endif
48
49 #if defined(HAVE_DL_DLOPEN)
50 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
51 #       include <dlfcn.h>
52 #   endif
53 #   if defined(HAVE_SYS_DL_H)
54 #       include <sys/dl.h>
55 #   endif
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 vlc_module_begin();
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 );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * Local prototypes
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 * );
79
80 static aout_buffer_t *Decode( decoder_t *, block_t ** );
81
82 struct decoder_sys_t
83 {
84     audio_date_t end_date;
85
86     /* Output buffer */
87     char *p_out;
88     unsigned int i_out;
89
90     /* Codec params */
91     void *context;
92     short int i_codec_flavor;
93
94     void *dll;
95     unsigned long (*raCloseCodec)(void*);
96     unsigned long (*raDecode)(void*, char*, unsigned long, char*,
97                               unsigned int*, long);
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*);
108
109 #if defined(LOADER)
110     ldt_fs_t *ldt_fs;
111 #endif
112
113     void *win32_dll;
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,
118                                      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*);
128 };
129
130 /* linux dlls doesn't need packing */
131 typedef struct /*__attribute__((__packed__))*/ {
132     int samplerate;
133     short bits;
134     short channels;
135     short quality;
136     /* 2bytes padding here, by gcc */
137     int bits_per_frame;
138     int packetsize;
139     int extradata_len;
140     void* extradata;
141 } ra_init_t;
142
143 /* windows dlls need packed structs (no padding) */
144 typedef struct __attribute__((__packed__)) {
145     int samplerate;
146     short bits;
147     short channels;
148     short quality;
149     int bits_per_frame;
150     int packetsize;
151     int extradata_len;
152     void* extradata;
153 } wra_init_t;
154
155 void *__builtin_new(unsigned long size) {return malloc(size);}
156 void __builtin_delete(void *p) {free(p);}
157
158 static int pi_channels_maps[7] =
159 {
160     0,
161     AOUT_CHAN_CENTER,
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
170 };
171
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
176  * to choose.
177  *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
179 {
180     decoder_t *p_dec = (decoder_t*)p_this;
181     decoder_sys_t *p_sys = p_dec->p_sys;
182
183     switch( p_dec->fmt_in.i_codec )
184     {
185     case VLC_FOURCC('c','o','o','k'):
186     case VLC_FOURCC('a','t','r','c'):
187     case VLC_FOURCC('s','i','p','r'):
188         break;
189
190     default:
191         return VLC_EGENERIC;
192     }
193
194     /* Channel detection */
195     if( p_dec->fmt_in.audio.i_channels <= 0 ||
196         p_dec->fmt_in.audio.i_channels > 6 )
197     {
198         msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
199                  p_dec->fmt_in.audio.i_channels );
200         return VLC_EGENERIC;
201     }
202
203     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
204     if( !p_sys )
205         return VLC_ENOMEM;
206     memset( p_sys, 0, sizeof(decoder_sys_t) );
207
208     /* Flavor for SIPR codecs */
209     p_sys->i_codec_flavor = -1;
210     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','i','p','r') )
211     {
212         p_sys->i_codec_flavor = p_dec->fmt_in.audio.i_flavor;
213         msg_Dbg( p_dec, "Got sipr flavor %d", p_sys->i_codec_flavor );
214     }
215
216     if( OpenDll( p_dec ) != VLC_SUCCESS )
217     {
218         free( p_sys );
219         return VLC_EGENERIC;
220     }
221
222 #ifdef LOADER
223     if( p_sys->win32_dll ) Close( p_this );
224 #endif
225
226     es_format_Init( &p_dec->fmt_out, AUDIO_ES, AOUT_FMT_S16_NE );
227     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
228     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
229     p_dec->fmt_out.audio.i_bitspersample =
230         p_dec->fmt_in.audio.i_bitspersample;
231     p_dec->fmt_out.audio.i_physical_channels =
232     p_dec->fmt_out.audio.i_original_channels =
233         pi_channels_maps[p_dec->fmt_out.audio.i_channels];
234
235     aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
236     aout_DateSet( &p_sys->end_date, 0 );
237
238     p_dec->pf_decode_audio = Decode;
239
240     p_sys->p_out = malloc( 4096 * 10 );
241     if( !p_sys->p_out )
242     {
243         free( p_sys );
244         return VLC_ENOMEM;
245     }
246     p_sys->i_out = 0;
247
248     return VLC_SUCCESS;
249 }
250
251 /*****************************************************************************
252  * Close:
253  *****************************************************************************/
254 static void Close( vlc_object_t *p_this )
255 {
256     decoder_t *p_dec = (decoder_t*)p_this;
257
258     CloseDll( p_dec );
259     free( p_dec->p_sys->p_out );
260     free( p_dec->p_sys );
261 }
262
263 /*****************************************************************************
264  * OpenDll:
265  *****************************************************************************/
266 static int OpenDll( decoder_t *p_dec )
267 {
268     char *psz_dll;
269     int i, i_result;
270
271     /** Find the good path for the dlls.**/
272     char *ppsz_path[] =
273     {
274       ".",
275 #ifndef WIN32
276       "/usr/local/RealPlayer8/Codecs",
277       "/usr/RealPlayer8/Codecs",
278       "/usr/lib/RealPlayer8/Codecs",
279       "/opt/RealPlayer8/Codecs",
280       "/usr/lib/RealPlayer9/users/Real/Codecs",
281       "/usr/lib/RealPlayer10/codecs",
282       "/usr/lib/RealPlayer10GOLD/codecs",
283       "/usr/lib/helix/player/codecs",
284       "/usr/lib64/RealPlayer8/Codecs",
285       "/usr/lib64/RealPlayer9/users/Real/Codecs",
286       "/usr/lib64/RealPlayer10/codecs",
287       "/usr/lib64/RealPlayer10GOLD/codecs",
288       "/usr/lib/win32",
289       "/usr/lib/codecs",
290       "/usr/local/lib/codecs",
291 #endif
292       NULL,
293       NULL,
294       NULL
295     };
296
297 #ifdef WIN32
298     char psz_win32_real_codecs[MAX_PATH + 1];
299     char psz_win32_helix_codecs[MAX_PATH + 1];
300
301     {
302         HKEY h_key;
303         DWORD i_type, i_data = MAX_PATH + 1, i_index = 1;
304         char *p_data;
305
306         p_data = psz_win32_real_codecs;
307         if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
308                           _T("Software\\RealNetworks\\Preferences\\DT_Codecs"),
309                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
310         {
311              if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
312                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
313                  i_type == REG_SZ )
314              {
315                  int i_len = strlen( p_data );
316                  if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
317                  ppsz_path[i_index++] = p_data;
318                  msg_Err( p_dec, "Real: %s", p_data );
319              }
320              RegCloseKey( h_key );
321         }
322
323         p_data = psz_win32_helix_codecs;
324         if( RegOpenKeyEx( HKEY_CLASSES_ROOT,
325                           _T("Helix\\HelixSDK\\10.0\\Preferences\\DT_Codecs"),
326                           0, KEY_READ, &h_key ) == ERROR_SUCCESS )
327         {
328              if( RegQueryValueEx( h_key, _T(""), 0, &i_type,
329                                   (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS &&
330                  i_type == REG_SZ )
331              {
332                  int i_len = strlen( p_data );
333                  if( i_len && p_data[i_len-1] == '\\' ) p_data[i_len-1] = 0;
334                  ppsz_path[i_index++] = p_data;
335                  msg_Err( p_dec, "Real: %s", p_data );
336              }
337              RegCloseKey( h_key );
338         }
339     }
340 #endif
341
342
343     /** Try the native libraries first **/
344 #ifndef WIN32
345     for( i = 0; ppsz_path[i]; i++ )
346     {
347         /* Old format */
348         asprintf( &psz_dll, "%s/%4.4s.so.6.0", ppsz_path[i],
349                   (char *)&p_dec->fmt_in.i_codec );
350         i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
351         free( psz_dll );
352         if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
353
354         /* New format */
355         asprintf( &psz_dll, "%s/%4.4s.so", ppsz_path[i],
356                   (char *)&p_dec->fmt_in.i_codec );
357         i_result = OpenNativeDll( p_dec, ppsz_path[i], psz_dll );
358         free( psz_dll );
359         if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
360
361     }
362 #endif
363
364     /** Or use the WIN32 dlls **/
365 #if defined(LOADER) || defined(WIN32)
366     for( i = 0; ppsz_path[i]; i++ )
367     {
368         /* New format */
369         asprintf( &psz_dll, "%s\\%4.4s.dll", ppsz_path[i],
370                   (char *)&p_dec->fmt_in.i_codec );
371         i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
372         free( psz_dll );
373         if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
374
375         /* Old format */
376         asprintf( &psz_dll, "%s\\%4.4s3260.dll", ppsz_path[i],
377                   (char *)&p_dec->fmt_in.i_codec );
378         i_result = OpenWin32Dll( p_dec, ppsz_path[i], psz_dll );
379         free( psz_dll );
380         if( i_result == VLC_SUCCESS ) return VLC_SUCCESS;
381     }
382 #endif
383
384     return VLC_EGENERIC;
385 }
386
387 static int OpenNativeDll( decoder_t *p_dec, char *psz_path, char *psz_dll )
388 {
389 #if defined(HAVE_DL_DLOPEN)
390     decoder_sys_t *p_sys = p_dec->p_sys;
391     void *handle = 0, *context = 0;
392     unsigned int i_result;
393     void *p_prop;
394     int i_prop;
395
396     ra_init_t init_data =
397     {
398         p_dec->fmt_in.audio.i_rate,
399         p_dec->fmt_in.audio.i_bitspersample,
400         p_dec->fmt_in.audio.i_channels,
401         100, /* quality */
402         p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
403         p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
404         p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
405     };
406
407     msg_Dbg( p_dec, "opening library '%s'", psz_dll );
408     if( !(handle = dlopen( psz_dll, RTLD_LAZY )) )
409     {
410         msg_Dbg( p_dec, "couldn't load library '%s' (%s)",
411                  psz_dll, dlerror() );
412         return VLC_EGENERIC;
413     }
414
415     p_sys->raCloseCodec = dlsym( handle, "RACloseCodec" );
416     p_sys->raDecode = dlsym( handle, "RADecode" );
417     p_sys->raFlush = dlsym( handle, "RAFlush" );
418     p_sys->raFreeDecoder = dlsym( handle, "RAFreeDecoder" );
419     p_sys->raGetFlavorProperty = dlsym( handle, "RAGetFlavorProperty" );
420     p_sys->raOpenCodec = dlsym( handle, "RAOpenCodec" );
421     p_sys->raOpenCodec2 = dlsym( handle, "RAOpenCodec2" );
422     p_sys->raInitDecoder = dlsym( handle, "RAInitDecoder" );
423     p_sys->raSetFlavor = dlsym( handle, "RASetFlavor" );
424     p_sys->raSetDLLAccessPath = dlsym( handle, "SetDLLAccessPath" );
425     p_sys->raSetPwd = dlsym( handle, "RASetPwd" ); // optional, used by SIPR
426
427     if( !(p_sys->raOpenCodec || p_sys->raOpenCodec2) ||
428         !p_sys->raCloseCodec || !p_sys->raInitDecoder ||
429         !p_sys->raDecode || !p_sys->raFreeDecoder ||
430         !p_sys->raGetFlavorProperty || !p_sys->raSetFlavor
431         /* || !p_sys->raFlush || !p_sys->raSetDLLAccessPath */ )
432     {
433         goto error_native;
434     }
435
436     if( p_sys->raOpenCodec2 )
437         i_result = p_sys->raOpenCodec2( &context, psz_path );
438     else
439         i_result = p_sys->raOpenCodec( &context );
440
441     if( i_result )
442     {
443         msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
444         goto error_native;
445     }
446
447     i_result = p_sys->raInitDecoder( context, &init_data );
448     if( i_result )
449     {
450         msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
451         goto error_native;
452     }
453
454     if( p_sys->i_codec_flavor >= 0 )
455     {
456         i_result = p_sys->raSetFlavor( context, p_sys->i_codec_flavor );
457         if( i_result )
458         {
459             msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
460                      i_result );
461             goto error_native;
462         }
463
464         p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
465                                              0, &i_prop );
466         msg_Dbg( p_dec, "audio codec: [%d] %s",
467                  p_sys->i_codec_flavor, (char *)p_prop );
468
469         p_prop = p_sys->raGetFlavorProperty( context, p_sys->i_codec_flavor,
470                                              1, &i_prop );
471         if( p_prop )
472         {
473             int i_bps = ((*((int*)p_prop))+4)/8;
474             msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
475                      (*((int*)p_prop))*0.001f, i_bps );
476         }
477     }
478
479     p_sys->context = context;
480     p_sys->dll = handle;
481     return VLC_SUCCESS;
482
483  error_native:
484     if( context ) p_sys->raFreeDecoder( context );
485     if( context ) p_sys->raCloseCodec( context );
486     dlclose( handle );
487 #endif
488
489     return VLC_EGENERIC;
490 }
491
492 static int OpenWin32Dll( decoder_t *p_dec, char *psz_path, char *psz_dll )
493 {
494 #if defined(LOADER) || defined(WIN32)
495     decoder_sys_t *p_sys = p_dec->p_sys;
496     void *handle = 0, *context = 0;
497     unsigned int i_result;
498     void *p_prop;
499     int i_prop;
500
501     wra_init_t init_data =
502     {
503         p_dec->fmt_in.audio.i_rate,
504         p_dec->fmt_in.audio.i_bitspersample,
505         p_dec->fmt_in.audio.i_channels,
506         100, /* quality */
507         p_dec->fmt_in.audio.i_blockalign, /* subpacket size */
508         p_dec->fmt_in.audio.i_blockalign, /* coded frame size */
509         p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra
510     };
511
512     msg_Dbg( p_dec, "opening win32 dll '%s'", psz_dll );
513
514 #ifdef LOADER
515     Setup_LDT_Keeper();
516 #endif
517
518     if( !(handle = LoadLibraryA( psz_dll )) )
519     {
520         msg_Dbg( p_dec, "couldn't load dll '%s'", psz_dll );
521         return VLC_EGENERIC;
522     }
523
524     p_sys->wraCloseCodec = GetProcAddress( handle, "RACloseCodec" );
525     p_sys->wraDecode = GetProcAddress( handle, "RADecode" );
526     p_sys->wraFlush = GetProcAddress( handle, "RAFlush" );
527     p_sys->wraFreeDecoder = GetProcAddress( handle, "RAFreeDecoder" );
528     p_sys->wraGetFlavorProperty =
529         GetProcAddress( handle, "RAGetFlavorProperty" );
530     p_sys->wraOpenCodec = GetProcAddress( handle, "RAOpenCodec" );
531     p_sys->wraOpenCodec2 = GetProcAddress( handle, "RAOpenCodec2" );
532     p_sys->wraInitDecoder = GetProcAddress( handle, "RAInitDecoder" );
533     p_sys->wraSetFlavor = GetProcAddress( handle, "RASetFlavor" );
534     p_sys->wraSetDLLAccessPath = GetProcAddress( handle, "SetDLLAccessPath" );
535     p_sys->wraSetPwd =
536         GetProcAddress( handle, "RASetPwd" ); // optional, used by SIPR
537
538     if( !(p_sys->wraOpenCodec || p_sys->wraOpenCodec2) ||
539         !p_sys->wraCloseCodec || !p_sys->wraInitDecoder ||
540         !p_sys->wraDecode || !p_sys->wraFreeDecoder ||
541         !p_sys->wraGetFlavorProperty || !p_sys->wraSetFlavor
542         /* || !p_sys->wraFlush || !p_sys->wraSetDLLAccessPath */ )
543     {
544         FreeLibrary( handle );
545         return VLC_EGENERIC;
546     }
547
548     if( p_sys->wraOpenCodec2 )
549         i_result = p_sys->wraOpenCodec2( &context, psz_path );
550     else
551         i_result = p_sys->wraOpenCodec( &context );
552
553     if( i_result )
554     {
555         msg_Err( p_dec, "decoder open failed, error code: 0x%x", i_result );
556         goto error_win32;
557     }
558
559     i_result = p_sys->wraInitDecoder( context, &init_data );
560     if( i_result )
561     {
562         msg_Err( p_dec, "decoder init failed, error code: 0x%x", i_result );
563         goto error_win32;
564     }
565
566     if( p_sys->i_codec_flavor >= 0 )
567     {
568         i_result = p_sys->wraSetFlavor( context, p_sys->i_codec_flavor );
569         if( i_result )
570         {
571             msg_Err( p_dec, "decoder flavor setup failed, error code: 0x%x",
572                      i_result );
573             goto error_win32;
574         }
575
576         p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
577                                               0, &i_prop );
578         msg_Dbg( p_dec, "audio codec: [%d] %s",
579                  p_sys->i_codec_flavor, (char *)p_prop );
580
581         p_prop = p_sys->wraGetFlavorProperty( context, p_sys->i_codec_flavor,
582                                               1, &i_prop );
583         if( p_prop )
584         {
585             int i_bps = ((*((int*)p_prop))+4)/8;
586             msg_Dbg( p_dec, "audio bitrate: %5.3f kbit/s (%d bps)",
587                      (*((int*)p_prop))*0.001f, i_bps );
588         }
589     }
590
591     p_sys->context = context;
592     p_sys->win32_dll = handle;
593     return VLC_SUCCESS;
594
595  error_win32:
596     if( context ) p_sys->wraFreeDecoder( context );
597     if( context ) p_sys->wraCloseCodec( context );
598     FreeLibrary( handle );
599 #endif
600
601     return VLC_EGENERIC;
602 }
603
604 /*****************************************************************************
605  * CloseDll:
606  *****************************************************************************/
607 static void CloseDll( decoder_t *p_dec )
608 {
609     decoder_sys_t *p_sys = p_dec->p_sys;
610
611     if( p_sys->context && p_sys->dll )
612     {
613         p_sys->raFreeDecoder( p_sys->context );
614         p_sys->raCloseCodec( p_sys->context );
615     }
616
617     if( p_sys->context && p_sys->win32_dll )
618     {
619         p_sys->wraFreeDecoder( p_sys->context );
620         p_sys->wraCloseCodec( p_sys->context );
621     }
622
623 #if defined(HAVE_DL_DLOPEN)
624     if( p_sys->dll ) dlclose( p_sys->dll );
625 #endif
626
627 #if defined(LOADER) || defined(WIN32)
628     if( p_sys->win32_dll ) FreeLibrary( p_sys->win32_dll );
629
630 #if 0 //def LOADER /* Segfaults */
631     Restore_LDT_Keeper( p_sys->ldt_fs );
632     msg_Dbg( p_dec, "Restore_LDT_Keeper" );
633 #endif
634 #endif
635
636     p_sys->dll = 0;
637     p_sys->win32_dll = 0;
638     p_sys->context = 0;
639 }
640
641 /*****************************************************************************
642  * DecodeAudio:
643  *****************************************************************************/
644 static aout_buffer_t *Decode( decoder_t *p_dec, block_t **pp_block )
645 {
646     decoder_sys_t *p_sys = p_dec->p_sys;
647     aout_buffer_t *p_aout_buffer = 0;
648     unsigned int i_result;
649     int i_samples;
650     block_t *p_block;
651
652 #ifdef LOADER
653     if( !p_sys->win32_dll && !p_sys->dll )
654     {
655         /* We must do open and close in the same thread (unless we do
656          * Setup_LDT_Keeper in the main thread before all others */
657         if( OpenDll( p_dec ) != VLC_SUCCESS )
658         {
659             /* Fatal */
660             p_dec->b_error = true;
661             return NULL;
662         }
663     }
664 #endif
665
666     if( pp_block == NULL || *pp_block == NULL ) return NULL;
667     p_block = *pp_block;
668
669     if( p_sys->dll )
670         i_result = p_sys->raDecode( p_sys->context, (char *)p_block->p_buffer,
671                                     (unsigned long)p_block->i_buffer,
672                                     p_sys->p_out, &p_sys->i_out, -1 );
673     else
674         i_result = p_sys->wraDecode( p_sys->context, (char *)p_block->p_buffer,
675                                      (unsigned long)p_block->i_buffer,
676                                      p_sys->p_out, &p_sys->i_out, -1 );
677
678 #if 0
679     msg_Err( p_dec, "decoded: %i samples (%i)",
680              p_sys->i_out * 8 / p_dec->fmt_out.audio.i_bitspersample /
681              p_dec->fmt_out.audio.i_channels, i_result );
682 #endif
683
684     /* Date management */
685     if( p_block->i_pts > 0 &&
686         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
687     {
688         aout_DateSet( &p_sys->end_date, p_block->i_pts );
689     }
690
691     if( !aout_DateGet( &p_sys->end_date ) )
692     {
693         /* We've just started the stream, wait for the first PTS. */
694         if( p_block ) block_Release( p_block );
695         return NULL;
696     }
697
698     i_samples = p_sys->i_out * 8 /
699         p_dec->fmt_out.audio.i_bitspersample /p_dec->fmt_out.audio.i_channels;
700
701     p_aout_buffer =
702         p_dec->pf_aout_buffer_new( p_dec, i_samples );
703     if( p_aout_buffer )
704     {
705         memcpy( p_aout_buffer->p_buffer, p_sys->p_out, p_sys->i_out );
706
707         /* Date management */
708         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
709         p_aout_buffer->end_date =
710             aout_DateIncrement( &p_sys->end_date, i_samples );
711     }
712
713     block_Release( p_block );
714     *pp_block = 0;
715     return p_aout_buffer;
716 }