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