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