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