]> git.sesse.net Git - vlc/blob - src/interface/main.c
libmad plug-in, courtesy of Jean-Paul Saman <jpsaman@wxs.nl>.
[vlc] / src / interface / main.c
1 /*****************************************************************************
2  * main.c: main vlc source
3  * Includes the main() function for vlc. Parses command line, start interface
4  * and spawn threads.
5  *****************************************************************************
6  * Copyright (C) 1998-2001 VideoLAN
7  * $Id: main.c,v 1.129 2001/12/04 13:47:46 massiot Exp $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
33 #include <stdio.h>                                              /* sprintf() */
34 #include <setjmp.h>                                       /* longjmp, setjmp */
35
36 #ifdef HAVE_GETOPT_LONG
37 #   ifdef HAVE_GETOPT_H
38 #       include <getopt.h>                                       /* getopt() */
39 #   endif
40 #else
41 #   include "GNUgetopt/getopt.h"
42 #endif
43
44 #ifdef SYS_DARWIN
45 #   include <mach/mach.h>                               /* Altivec detection */
46 #   include <mach/mach_error.h>       /* some day the header files||compiler *
47                                                        will define it for us */
48 #   include <mach/bootstrap.h>
49 #endif
50
51 #ifndef WIN32
52 #   include <netinet/in.h>                            /* BSD: struct in_addr */
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #   include <unistd.h>
57 #elif defined( _MSC_VER ) && defined( _WIN32 )
58 #   include <io.h>
59 #endif
60
61 #include <errno.h>                                                 /* ENOMEM */
62 #include <stdlib.h>                                  /* getenv(), strtol(),  */
63 #include <string.h>                                            /* strerror() */
64 #include <fcntl.h>                                       /* open(), O_WRONLY */
65 #include <sys/stat.h>                                             /* S_IREAD */
66
67 #include "config.h"
68 #include "common.h"
69 #include "debug.h"
70 #include "intf_msg.h"
71 #include "threads.h"
72 #include "mtime.h"
73 #include "tests.h"                                              /* TestCPU() */
74 #include "modules.h"
75
76 #include "stream_control.h"
77 #include "input_ext-intf.h"
78
79 #include "intf_playlist.h"
80 #include "interface.h"
81
82 #include "audio_output.h"
83
84 #include "video.h"
85 #include "video_output.h"
86
87 #ifdef SYS_BEOS
88 #   include "beos_specific.h"
89 #endif
90
91 #ifdef SYS_DARWIN
92 #   include "darwin_specific.h"
93 #endif
94
95 #ifdef WIN32
96 #   include "win32_specific.h"
97 #endif
98
99 #include "netutils.h"                                 /* network_ChannelJoin */
100
101 #include "main.h"
102
103 /*****************************************************************************
104  * Command line options constants. If something is changed here, be sure that
105  * GetConfiguration and Usage are also changed.
106  *****************************************************************************/
107
108 /* Long options return values - note that values corresponding to short options
109  * chars, and in general any regular char, should be avoided */
110 #define OPT_NOAUDIO             150
111 #define OPT_STEREO              151
112 #define OPT_MONO                152
113 #define OPT_SPDIF               153
114 #define OPT_VOLUME              154
115
116 #define OPT_NOVIDEO             160
117 #define OPT_DISPLAY             161
118 #define OPT_WIDTH               162
119 #define OPT_HEIGHT              163
120 #define OPT_COLOR               164
121 #define OPT_FULLSCREEN          165
122 #define OPT_OVERLAY             166
123 #define OPT_XVADAPTOR           167
124 #define OPT_SMP                 168
125
126 #define OPT_CHANNELS            170
127 #define OPT_SERVER              171
128 #define OPT_PORT                172
129 #define OPT_BROADCAST           173
130 #define OPT_CHANNELSERVER       174
131
132 #define OPT_INPUT               180
133 #define OPT_MOTION              181
134 #define OPT_IDCT                182
135 #define OPT_YUV                 183
136 #define OPT_DOWNMIX             184
137 #define OPT_IMDCT               185
138 #define OPT_MEMCPY              186
139 #define OPT_DVDCSS_METHOD       187
140 #define OPT_DVDCSS_VERBOSE      188
141
142 #define OPT_SYNCHRO             190
143 #define OPT_WARNING             191
144 #define OPT_VERSION             192
145 #define OPT_STDOUT              193
146 #define OPT_STATS               194
147
148 #define OPT_MPEG_ADEC           200
149
150 /* Usage fashion */
151 #define USAGE                     0
152 #define SHORT_HELP                1
153 #define LONG_HELP                 2
154
155 /* Needed for x86 CPU capabilities detection */
156 #define cpuid( a )                 \
157     asm volatile ( "cpuid"         \
158                  : "=a" ( i_eax ), \
159                    "=b" ( i_ebx ), \
160                    "=c" ( i_ecx ), \
161                    "=d" ( i_edx )  \
162                  : "a"  ( a )      \
163                  : "cc" );
164
165 /* Long options */
166 static const struct option longopts[] =
167 {
168     /*  name,               has_arg,    flag,   val */
169
170     /* General/common options */
171     {   "help",             0,          0,      'h' },
172     {   "longhelp",         0,          0,      'H' },
173     {   "version",          0,          0,      OPT_VERSION },
174
175     /* Interface options */
176     {   "intf",             1,          0,      'I' },
177     {   "warning",          1,          0,      OPT_WARNING },
178     {   "stdout",           1,          0,      OPT_STDOUT },
179     {   "stats",            0,          0,      OPT_STATS },
180
181     /* Audio options */
182     {   "noaudio",          0,          0,      OPT_NOAUDIO },
183     {   "aout",             1,          0,      'A' },
184     {   "stereo",           0,          0,      OPT_STEREO },
185     {   "mono",             0,          0,      OPT_MONO },
186     {   "spdif",            0,          0,      OPT_SPDIF },
187     {   "downmix",          1,          0,      OPT_DOWNMIX },
188     {   "imdct",            1,          0,      OPT_IMDCT },
189     {   "volume",           1,          0,      OPT_VOLUME },
190
191     /* Video options */
192     {   "novideo",          0,          0,      OPT_NOVIDEO },
193     {   "vout",             1,          0,      'V' },
194     {   "display",          1,          0,      OPT_DISPLAY },
195     {   "width",            1,          0,      OPT_WIDTH },
196     {   "height",           1,          0,      OPT_HEIGHT },
197     {   "grayscale",        0,          0,      'g' },
198     {   "color",            0,          0,      OPT_COLOR },
199     {   "motion",           1,          0,      OPT_MOTION },
200     {   "idct",             1,          0,      OPT_IDCT },
201     {   "yuv",              1,          0,      OPT_YUV },
202     {   "fullscreen",       0,          0,      OPT_FULLSCREEN },
203     {   "overlay",          0,          0,      OPT_OVERLAY },
204     {   "xvadaptor",        1,          0,      OPT_XVADAPTOR },
205     {   "smp",              1,          0,      OPT_SMP },
206
207     /* DVD options */
208     {   "dvdtitle",         1,          0,      't' },
209     {   "dvdchapter",       1,          0,      'T' },
210     {   "dvdangle",         1,          0,      'u' },
211     {   "dvdaudio",         1,          0,      'a' },
212     {   "dvdchannel",       1,          0,      'c' },
213     {   "dvdsubtitle",      1,          0,      's' },
214     {   "dvdcss-method",    1,          0,      OPT_DVDCSS_METHOD },
215     {   "dvdcss-verbose",   1,          0,      OPT_DVDCSS_VERBOSE },
216     
217     /* Input options */
218     {   "input",            1,          0,      OPT_INPUT },
219     {   "channels",         0,          0,      OPT_CHANNELS },
220     {   "channelserver",    1,          0,      OPT_CHANNELSERVER },
221
222     /* Misc options */
223     {   "synchro",          1,          0,      OPT_SYNCHRO },
224     {   "memcpy",           1,          0,      OPT_MEMCPY },
225
226     /* Decoder options */
227     {   "mpeg_adec",        1,          0,      OPT_MPEG_ADEC },
228
229     {   0,                  0,          0,      0 }
230 };
231
232 /* Short options */
233 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
234
235 /*****************************************************************************
236  * Global variables - these are the only ones, see main.h and modules.h
237  *****************************************************************************/
238 main_t        *p_main;
239 module_bank_t *p_module_bank;
240 aout_bank_t   *p_aout_bank;
241 vout_bank_t   *p_vout_bank;
242 void*       ( *pf_fast_memcpy ) ( void *, const void *, size_t );
243
244 /*****************************************************************************
245  * Local prototypes
246  *****************************************************************************/
247 static int  GetConfiguration        ( int *pi_argc, char *ppsz_argv[],
248                                       char *ppsz_env[] );
249 static int  GetFilenames            ( int i_argc, char *ppsz_argv[] );
250 static void Usage                   ( int i_fashion );
251 static void Version                 ( void );
252
253 static void InitSignalHandler       ( void );
254 static void SimpleSignalHandler     ( int i_signal );
255 static void FatalSignalHandler      ( int i_signal );
256 static void InstructionSignalHandler( int i_signal );
257 static int  CPUCapabilities         ( void );
258
259 static int  RedirectSTDOUT          ( void );
260 static void ShowConsole             ( void );
261
262 static jmp_buf env;
263 static int  i_illegal;
264
265 /*****************************************************************************
266  * main: parse command line, start interface and spawn threads
267  *****************************************************************************
268  * Steps during program execution are:
269  *      -configuration parsing and messages interface initialization
270  *      -opening of audio output device and some global modules
271  *      -execution of interface, which exit on error or on user request
272  *      -closing of audio output device and some global modules
273  * On error, the spawned threads are canceled, and the open devices closed.
274  *****************************************************************************/
275 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
276 {
277     main_t        main_data;                /* root of all data - see main.h */
278     module_bank_t module_bank;
279     aout_bank_t   aout_bank;
280     vout_bank_t   vout_bank;
281
282     p_main        = &main_data;               /* set up the global variables */
283     p_module_bank = &module_bank;
284     p_aout_bank   = &aout_bank;
285     p_vout_bank   = &vout_bank;
286
287     /*
288      * Initialize threads system
289      */
290     vlc_threads_init( );
291
292     /*
293      * Test if our code is likely to run on this CPU 
294      */
295     p_main->i_cpu_capabilities = CPUCapabilities();
296     
297     /*
298      * System specific initialization code
299      */
300 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
301     system_Init( &i_argc, ppsz_argv, ppsz_env );
302
303 #elif defined( SYS_LINUX )
304 #   ifdef DEBUG
305     /* Activate malloc checking routines to detect heap corruptions. */
306     main_PutIntVariable( "MALLOC_CHECK_", 2 );
307 #   endif
308 #endif
309
310     /*
311      * Initialize messages interface
312      */
313     p_main->p_msg = intf_MsgCreate();
314     if( !p_main->p_msg )                         /* start messages interface */
315     {
316         fprintf( stderr, "error: can't initialize messages interface (%s)\n",
317                  strerror(errno) );
318         return( errno );
319     }
320
321     intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
322
323     /*
324      * Read configuration
325      */
326     if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
327     {
328         intf_MsgDestroy();
329         return( errno );
330     }
331
332     /*
333      * Redirect the standard output if required by the user, and on Win32 we
334      * also open a console to display the debug messages.
335      */
336     RedirectSTDOUT();
337
338     if( p_main->b_stats )
339     {
340         char          p_capabilities[200];
341         p_capabilities[0] = '\0';
342
343 #define PRINT_CAPABILITY( capability, string )                              \
344         if( p_main->i_cpu_capabilities & capability )                       \
345         {                                                                   \
346             strncat( p_capabilities, string " ",                            \
347                      sizeof(p_capabilities) - strlen(p_capabilities) );     \
348             p_capabilities[sizeof(p_capabilities) - 1] = '\0';              \
349         }
350
351         PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
352         PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
353         PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
354         PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
355         PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
356         PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
357         PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
358         PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
359         intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
360     }
361
362     /*
363      * Initialize playlist and get commandline files
364      */
365     p_main->p_playlist = intf_PlaylistCreate();
366     if( !p_main->p_playlist )
367     {
368         intf_ErrMsg( "playlist error: playlist initialization failed" );
369         intf_MsgDestroy();
370         return( errno );
371     }
372     intf_PlaylistInit( p_main->p_playlist );
373
374     /*
375      * Get input filenames given as commandline arguments
376      */
377     GetFilenames( i_argc, ppsz_argv );
378
379     /*
380      * Initialize module, aout and vout banks
381      */
382     module_InitBank();
383     aout_InitBank();
384     vout_InitBank();
385
386     /*
387      * Choose the best memcpy module
388      */
389     p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );
390
391     if( p_main->p_memcpy_module == NULL )
392     {
393         intf_ErrMsg( "intf error: no suitable memcpy module, "
394                      "using libc default" );
395         pf_fast_memcpy = memcpy;
396     }
397     else
398     {
399 #define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
400         pf_fast_memcpy = f.pf_fast_memcpy;
401 #undef f
402     }
403
404     /*
405      * Initialize shared resources and libraries
406      */
407     if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
408                              INPUT_NETWORK_CHANNEL_DEFAULT ) &&
409         network_ChannelCreate() )
410     {
411         /* On error during Channels initialization, switch off channels */
412         intf_Msg( "Channels initialization failed : "
413                   "Channel management is deactivated" );
414         main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
415     }
416
417     /*
418      * Try to run the interface
419      */
420     p_main->p_intf = intf_Create();
421     if( p_main->p_intf == NULL )
422     {
423         intf_ErrMsg( "intf error: interface initialization failed" );
424     }
425     else
426     {
427         /*
428          * Set signal handling policy for all threads
429          */
430         InitSignalHandler();
431
432         /*
433          * This is the main loop
434          */
435         p_main->p_intf->pf_run( p_main->p_intf );
436
437         /*
438          * Finished, destroy the interface
439          */
440         intf_Destroy( p_main->p_intf );
441
442         /*
443          * Go back into channel 0 which is the network
444          */
445         if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
446                                  INPUT_NETWORK_CHANNEL_DEFAULT ) )
447         {
448             network_ChannelJoin( COMMON_CHANNEL );
449         }
450     }
451
452     /*
453      * Free memcpy module
454      */
455     module_Unneed( p_main->p_memcpy_module );
456
457     /*
458      * Free module, aout and vout banks
459      */
460     vout_EndBank();
461     aout_EndBank();
462     module_EndBank();
463
464     /*
465      * Free playlist
466      */
467     intf_PlaylistDestroy( p_main->p_playlist );
468
469     /*
470      * System specific cleaning code
471      */
472 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
473     system_End();
474 #endif
475
476
477     /*
478      * Terminate messages interface and program
479      */
480     intf_Msg( "intf: program terminated" );
481     intf_MsgDestroy();
482
483     /*
484      * Stop threads system
485      */
486     vlc_threads_end( );
487
488     return 0;
489 }
490
491 /*****************************************************************************
492  * main_GetIntVariable: get the int value of an environment variable
493  *****************************************************************************
494  * This function is used to read some default parameters in modules.
495  *****************************************************************************/
496 int main_GetIntVariable( char *psz_name, int i_default )
497 {
498     char *      psz_env;                                /* environment value */
499     char *      psz_end;                             /* end of parsing index */
500     long int    i_value;                                            /* value */
501
502     psz_env = getenv( psz_name );
503     if( psz_env )
504     {
505         i_value = strtol( psz_env, &psz_end, 0 );
506         if( (*psz_env != '\0') && (*psz_end == '\0') )
507         {
508             return( i_value );
509         }
510     }
511     return( i_default );
512 }
513
514 /*****************************************************************************
515  * main_GetPszVariable: get the string value of an environment variable
516  *****************************************************************************
517  * This function is used to read some default parameters in modules.
518  *****************************************************************************/
519 char * main_GetPszVariable( char *psz_name, char *psz_default )
520 {
521     char *psz_env;
522
523     psz_env = getenv( psz_name );
524     if( psz_env )
525     {
526         return( psz_env );
527     }
528     return( psz_default );
529 }
530
531 /*****************************************************************************
532  * main_PutPszVariable: set the string value of an environment variable
533  *****************************************************************************
534  * This function is used to set some default parameters in modules. The use of
535  * this function will cause some memory leak: since some systems use the pointer
536  * passed to putenv to store the environment string, it can't be freed.
537  *****************************************************************************/
538 void main_PutPszVariable( char *psz_name, char *psz_value )
539 {
540     char *psz_env;
541
542     psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
543     if( psz_env == NULL )
544     {
545         intf_ErrMsg( "intf error: cannot create psz_env (%s)",
546                      strerror(ENOMEM) );
547     }
548     else
549     {
550         sprintf( psz_env, "%s=%s", psz_name, psz_value );
551         if( putenv( psz_env ) )
552         {
553             intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
554         }
555     }
556 }
557
558 /*****************************************************************************
559  * main_PutIntVariable: set the integer value of an environment variable
560  *****************************************************************************
561  * This function is used to set some default parameters in modules. The use of
562  * this function will cause some memory leak: since some systems use the pointer
563  * passed to putenv to store the environment string, it can't be freed.
564  *****************************************************************************/
565 void main_PutIntVariable( char *psz_name, int i_value )
566 {
567     char psz_value[ 256 ];                               /* buffer for value */
568
569     sprintf( psz_value, "%d", i_value );
570     main_PutPszVariable( psz_name, psz_value );
571 }
572
573 /* following functions are local */
574
575 /*****************************************************************************
576  * GetConfiguration: parse command line
577  *****************************************************************************
578  * Parse command line and configuration file for configuration. If the inline
579  * help is requested, the function Usage() is called and the function returns
580  * -1 (causing main() to exit). The messages interface is initialized at this
581  * stage, but most structures are not allocated, so only environment should
582  * be used.
583  *****************************************************************************/
584 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
585 {
586     int   i_cmd;
587     char *p_tmp;
588
589     /* Set default configuration and copy arguments */
590     p_main->i_argc    = *pi_argc;
591     p_main->ppsz_argv = ppsz_argv;
592     p_main->ppsz_env  = ppsz_env;
593
594     p_main->b_audio     = 1;
595     p_main->b_video     = 1;
596
597     p_main->i_warning_level = 0;
598     p_main->b_stats = 0;
599
600     p_main->p_channel = NULL;
601
602     /* Get the executable name (similar to the basename command) */
603     p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
604     while( *p_tmp )
605     {
606         if( *p_tmp == '/' )
607         {
608             p_main->psz_arg0 = ++p_tmp;
609         }
610         else
611         {
612             ++p_tmp;
613         }
614     }
615
616 #ifdef SYS_DARWIN
617     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
618      * is the PSN - process serial number (a unique PID-ish thingie)
619      * still ok for real Darwin & when run from command line */
620     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
621                                         /* for example -psn_0_9306113 */
622     {
623         /* GDMF!... I can't do this or else the MacOSX window server will
624          * not pick up the PSN and not register the app and we crash...
625          * hence the following kludge otherwise we'll get confused w/ argv[1]
626          * being an input file name */
627 #if 0
628         ppsz_argv[ 1 ] = NULL;
629 #endif
630         *pi_argc = *pi_argc - 1;
631         pi_argc--;
632         return( 0 );
633     }
634 #endif
635
636     /* Parse command line options */
637     opterr = 0;
638     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
639                                    psz_shortopts, longopts, 0 ) ) != EOF )
640     {
641         switch( i_cmd )
642         {
643         /* General/common options */
644         case 'h':                                              /* -h, --help */
645             ShowConsole();
646             RedirectSTDOUT();
647             Usage( SHORT_HELP );
648 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
649             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
650                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
651             {
652                 /* No stdout redirection has been asked for */
653                 intf_MsgImm( "\nPress the RETURN key to continue..." );
654                 getchar();
655             }
656 #endif
657             return( -1 );
658             break;
659         case 'H':                                          /* -H, --longhelp */
660             ShowConsole();
661             RedirectSTDOUT();
662             Usage( LONG_HELP );
663 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
664             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
665                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
666             {
667                 /* No stdout redirection has been asked for */
668                 intf_MsgImm( "\nPress the RETURN key to continue..." );
669                 getchar();
670             }
671 #endif
672             return( -1 );
673             break;
674         case OPT_VERSION:                                       /* --version */
675             ShowConsole();
676             RedirectSTDOUT();
677             Version();
678 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
679             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
680                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
681             {
682                 /* No stdout redirection has been asked for */
683                 intf_MsgImm( "\nPress the RETURN key to continue..." );
684                 getchar();
685             }
686 #endif
687             return( -1 );
688             break;
689         case 'v':                                           /* -v, --verbose */
690             p_main->i_warning_level++;
691             break;
692
693         /* Interface warning messages level */
694         case 'I':                                              /* -I, --intf */
695             main_PutPszVariable( INTF_METHOD_VAR, optarg );
696             break;
697         case OPT_WARNING:                                       /* --warning */
698             intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
699             p_main->i_warning_level = atoi(optarg);
700             break;
701
702         case OPT_STDOUT:                                         /* --stdout */
703             main_PutPszVariable( INTF_STDOUT_VAR, optarg );
704             break;
705
706         case OPT_STATS:
707             p_main->b_stats = 1;
708             break;
709
710         /* Audio options */
711         case OPT_NOAUDIO:                                       /* --noaudio */
712             p_main->b_audio = 0;
713             break;
714         case 'A':                                              /* -A, --aout */
715             main_PutPszVariable( AOUT_METHOD_VAR, optarg );
716             break;
717         case OPT_STEREO:                                         /* --stereo */
718             main_PutIntVariable( AOUT_STEREO_VAR, 1 );
719             break;
720         case OPT_MONO:                                             /* --mono */
721             main_PutIntVariable( AOUT_STEREO_VAR, 0 );
722             break;
723         case OPT_SPDIF:                                           /* --spdif */
724             main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
725             break;
726         case OPT_DOWNMIX:                                       /* --downmix */
727             main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
728             break;
729         case OPT_IMDCT:                                           /* --imdct */
730             main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
731             break;
732         case OPT_VOLUME:                                         /* --volume */
733             main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
734             break;
735
736         /* Video options */
737         case OPT_NOVIDEO:                                       /* --novideo */
738             p_main->b_video = 0;
739             break;
740         case 'V':                                              /* -V, --vout */
741             main_PutPszVariable( VOUT_METHOD_VAR, optarg );
742             break;
743         case OPT_DISPLAY:                                       /* --display */
744             main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
745             break;
746         case OPT_WIDTH:                                           /* --width */
747             main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
748             break;
749         case OPT_HEIGHT:                                         /* --height */
750             main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
751             break;
752         case 'g':                                         /* -g, --grayscale */
753             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
754             break;
755         case OPT_COLOR:                                           /* --color */
756             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
757             break;
758         case OPT_FULLSCREEN:                                 /* --fullscreen */
759             main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
760             break;
761         case OPT_OVERLAY:                                       /* --overlay */
762             main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
763             break;
764         case OPT_XVADAPTOR:                                   /* --xvadaptor */
765             main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
766             break;
767         case OPT_MOTION:                                         /* --motion */
768             main_PutPszVariable( MOTION_METHOD_VAR, optarg );
769             break;
770         case OPT_IDCT:                                             /* --idct */
771             main_PutPszVariable( IDCT_METHOD_VAR, optarg );
772             break;
773         case OPT_YUV:                                               /* --yuv */
774             main_PutPszVariable( YUV_METHOD_VAR, optarg );
775             break;
776         case OPT_SMP:                                               /* --smp */
777             main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
778             break;
779
780         /* DVD options */
781         case 't':                                              /* --dvdtitle */
782             main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
783             break;
784         case 'T':                                            /* --dvdchapter */
785             main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
786             break;
787         case 'u':                                              /* --dvdangle */
788             main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
789             break;
790         case 'a':                                              /* --dvdaudio */
791             if ( ! strcmp(optarg, "ac3") )
792                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
793             else if ( ! strcmp(optarg, "lpcm") )
794                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
795             else if ( ! strcmp(optarg, "mpeg") )
796                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
797             else
798                 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
799             break;
800         case 'c':                                            /* --dvdchannel */
801             main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
802             break;
803         case 's':                                           /* --dvdsubtitle */
804             main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
805             break;
806         case OPT_DVDCSS_METHOD:                           /* --dvdcss-method */
807             main_PutPszVariable( "DVDCSS_METHOD", optarg );
808             break;
809         case OPT_DVDCSS_VERBOSE:                         /* --dvdcss-verbose */
810             main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
811             break;
812
813         /* Input options */
814         case OPT_INPUT:                                           /* --input */
815             main_PutPszVariable( INPUT_METHOD_VAR, optarg );
816             break;
817         case OPT_CHANNELS:                                     /* --channels */
818             main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
819             break;
820         case OPT_CHANNELSERVER:                           /* --channelserver */
821             main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
822             break;
823
824         /* Misc options */
825         case OPT_SYNCHRO:                                      
826             main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
827             break;
828         case OPT_MEMCPY:                                      
829             main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
830             break;
831             
832         /* Decoder options */
833         case OPT_MPEG_ADEC:
834             main_PutPszVariable( ADEC_MPEG_VAR, optarg );
835             break;
836
837         /* Internal error: unknown option */
838         case '?':
839         default:
840             ShowConsole();
841             RedirectSTDOUT();
842             intf_ErrMsg( "intf error: unknown option `%s'",
843                          ppsz_argv[optind] );
844             Usage( USAGE );
845 #ifdef WIN32        /* Pause the console because it's destroyed when we exit */
846             if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
847                                                  INTF_STDOUT_DEFAULT ) ) == 0 )
848             {
849                 /* No stdout redirection has been asked for */
850                 intf_MsgImm( "\nPress the RETURN key to continue..." );
851                 getchar();
852             }
853 #endif
854             return( EINVAL );
855             break;
856         }
857     }
858
859     if( p_main->i_warning_level < 0 )
860     {
861         p_main->i_warning_level = 0;
862     }
863
864     return( 0 );
865 }
866
867 /*****************************************************************************
868  * GetFilenames: parse command line options which are not flags
869  *****************************************************************************
870  * Parse command line for input files.
871  *****************************************************************************/
872 static int GetFilenames( int i_argc, char *ppsz_argv[] )
873 {
874     int i_opt;
875
876     /* We assume that the remaining parameters are filenames */
877     for( i_opt = optind; i_opt < i_argc; i_opt++ )
878     {
879         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
880                           ppsz_argv[ i_opt ] );
881     }
882
883     return( 0 );
884 }
885
886 /*****************************************************************************
887  * Usage: print program usage
888  *****************************************************************************
889  * Print a short inline help. Message interface is initialized at this stage.
890  *****************************************************************************/
891 static void Usage( int i_fashion )
892 {
893     /* Usage */
894     intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
895                  p_main->psz_arg0 );
896
897     if( i_fashion == USAGE )
898     {
899         intf_MsgImm( "Try `%s --help' for more information.",
900                      p_main->psz_arg0 );
901         return;
902     }
903
904     /* Options */
905     intf_MsgImm( "\nOptions:"
906           "\n  -I, --intf <module>            \tinterface method"
907           "\n  -v, --verbose                  \tverbose mode (cumulative)"
908           "\n      --stdout <filename>        \tredirect console stdout"
909           "\n      --memcpy <module>          \tmemcpy method"
910           "\n"
911           "\n      --noaudio                  \tdisable audio"
912           "\n  -A, --aout <module>            \taudio output method"
913           "\n      --stereo, --mono           \tstereo/mono audio"
914           "\n      --spdif                    \tAC3 pass-through mode"
915           "\n      --downmix <module>         \tAC3 downmix method"
916           "\n      --imdct <module>           \tAC3 IMDCT method"
917           "\n      --volume [0..1024]         \tVLC output volume"
918           "\n"
919           "\n      --novideo                  \tdisable video"
920           "\n  -V, --vout <module>            \tvideo output method"
921           "\n      --display <display>        \tdisplay string"
922           "\n      --width <w>, --height <h>  \tdisplay dimensions"
923           "\n  -g, --grayscale                \tgrayscale output"
924           "\n      --fullscreen               \tfullscreen output"
925           "\n      --overlay                  \taccelerated display"
926           "\n      --xvadaptor <adaptor>      \tXVideo adaptor"
927           "\n      --color                    \tcolor output"
928           "\n      --motion <module>          \tmotion compensation method"
929           "\n      --idct <module>            \tIDCT method"
930           "\n      --yuv <module>             \tYUV method"
931           "\n      --synchro <type>           \tforce synchro algorithm"
932           "\n      --smp <number of threads>  \tuse several processors"
933           "\n"
934           "\n  -t, --dvdtitle <num>           \tchoose DVD title"
935           "\n  -T, --dvdchapter <num>         \tchoose DVD chapter"
936           "\n  -u, --dvdangle <num>           \tchoose DVD angle"
937           "\n  -a, --dvdaudio <type>          \tchoose DVD audio type"
938           "\n  -c, --dvdchannel <channel>     \tchoose DVD audio channel"
939           "\n  -s, --dvdsubtitle <channel>    \tchoose DVD subtitle channel"
940           "\n      --dvdcss-method <method>   \tselect dvdcss decryption method"
941           "\n      --dvdcss-verbose <level>   \tselect dvdcss verbose level"
942           "\n"
943           "\n      --input                    \tinput method"
944           "\n      --channels                 \tenable channels"
945           "\n      --channelserver <host>     \tchannel server address"
946           "\n"
947           "\n      --mpeg_adec <builtin|mad>  \tchoose audio decoder"
948           "\n"
949           "\n  -h, --help                     \tprint help and exit"
950           "\n  -H, --longhelp                 \tprint long help and exit"
951           "\n      --version                  \toutput version information and exit" );
952
953     if( i_fashion == SHORT_HELP )
954         return;
955
956     /* Interface parameters */
957     intf_MsgImm( "\nInterface parameters:"
958         "\n  " INTF_METHOD_VAR "=<method name>        \tinterface method"
959         "\n  " INTF_INIT_SCRIPT_VAR "=<filename>              \tinitialization script"
960         "\n  " INTF_CHANNELS_VAR "=<filename>         \tchannels list"
961         "\n  " INTF_STDOUT_VAR "=<filename>           \tredirect console stdout"
962         "\n  " MEMCPY_METHOD_VAR "=<method name>      \tmemcpy method" );
963
964     /* Audio parameters */
965     intf_MsgImm( "\nAudio parameters:"
966         "\n  " AOUT_METHOD_VAR "=<method name>        \taudio method"
967         "\n  " AOUT_DSP_VAR "=<filename>              \tdsp device path"
968         "\n  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output"
969         "\n  " AOUT_SPDIF_VAR "={1|0}                 \tAC3 pass-through mode"
970         "\n  " DOWNMIX_METHOD_VAR "=<method name>     \tAC3 downmix method"
971         "\n  " IMDCT_METHOD_VAR "=<method name>       \tAC3 IMDCT method"
972         "\n  " AOUT_VOLUME_VAR "=[0..1024]            \tVLC output volume"
973         "\n  " AOUT_RATE_VAR "=<rate>             \toutput rate" );
974
975     /* Video parameters */
976     intf_MsgImm( "\nVideo parameters:"
977         "\n  " VOUT_METHOD_VAR "=<method name>        \tdisplay method"
978         "\n  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used"
979         "\n  " VOUT_WIDTH_VAR "=<width>               \tdisplay width"
980         "\n  " VOUT_HEIGHT_VAR "=<height>             \tdislay height"
981         "\n  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path"
982         "\n  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output"
983         "\n  " VOUT_FULLSCREEN_VAR "={1|0}            \tfullscreen"
984         "\n  " VOUT_OVERLAY_VAR "={1|0}               \toverlay"
985         "\n  " VOUT_XVADAPTOR_VAR "=<adaptor>         \tXVideo adaptor"
986         "\n  " MOTION_METHOD_VAR "=<method name>      \tmotion compensation method"
987         "\n  " IDCT_METHOD_VAR "=<method name>        \tIDCT method"
988         "\n  " YUV_METHOD_VAR "=<method name>         \tYUV method"
989         "\n  " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB}   \tsynchro algorithm"
990         "\n  " VDEC_SMP_VAR "=<number of threads>     \tuse several processors" );
991
992     /* DVD parameters */
993     intf_MsgImm( "\nDVD parameters:"
994         "\n  " INPUT_DVD_DEVICE_VAR "=<device>        \tDVD device"
995         "\n  " INPUT_TITLE_VAR "=<title>              \ttitle number"
996         "\n  " INPUT_CHAPTER_VAR "=<chapter>          \tchapter number"
997         "\n  " INPUT_ANGLE_VAR "=<angle>              \tangle number"
998         "\n  " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off}  \taudio type"
999         "\n  " INPUT_CHANNEL_VAR "=[0-15]             \taudio channel"
1000         "\n  " INPUT_SUBTITLE_VAR "=[0-31]            \tsubtitle channel" );
1001
1002     /* Input parameters */
1003     intf_MsgImm( "\nInput parameters:"
1004         "\n  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface"
1005         "\n  " INPUT_CHANNEL_SERVER_VAR "=<hostname>  \tchannel server"
1006         "\n  " INPUT_CHANNEL_PORT_VAR "=<port>        \tchannel server port" );
1007
1008     /* Decoder parameters */
1009     intf_MsgImm( "\nDecoder parameters:"
1010         "\n  " ADEC_MPEG_VAR "=<builtin|mad>          \taudio decoder" );
1011 }
1012
1013 /*****************************************************************************
1014  * Version: print complete program version
1015  *****************************************************************************
1016  * Print complete program version and build number.
1017  *****************************************************************************/
1018 static void Version( void )
1019 {
1020     intf_MsgImm( VERSION_MESSAGE
1021         "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1022         "You may redistribute it under the terms of the GNU General Public License;\n"
1023         "see the file named COPYING for details.\n"
1024         "Written by the VideoLAN team at Ecole Centrale, Paris." );
1025 }
1026
1027 /*****************************************************************************
1028  * InitSignalHandler: system signal handler initialization
1029  *****************************************************************************
1030  * Set the signal handlers. SIGTERM is not intercepted, because we need at
1031  * at least a method to kill the program when all other methods failed, and
1032  * when we don't want to use SIGKILL.
1033  *****************************************************************************/
1034 static void InitSignalHandler( void )
1035 {
1036     /* Termination signals */
1037 #ifndef WIN32
1038     signal( SIGINT,  FatalSignalHandler );
1039     signal( SIGHUP,  FatalSignalHandler );
1040     signal( SIGQUIT, FatalSignalHandler );
1041
1042     /* Other signals */
1043     signal( SIGALRM, SimpleSignalHandler );
1044     signal( SIGPIPE, SimpleSignalHandler );
1045 #endif
1046 }
1047
1048
1049 /*****************************************************************************
1050  * SimpleSignalHandler: system signal handler
1051  *****************************************************************************
1052  * This function is called when a non fatal signal is received by the program.
1053  *****************************************************************************/
1054 static void SimpleSignalHandler( int i_signal )
1055 {
1056     /* Acknowledge the signal received */
1057     intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1058 }
1059
1060
1061 /*****************************************************************************
1062  * FatalSignalHandler: system signal handler
1063  *****************************************************************************
1064  * This function is called when a fatal signal is received by the program.
1065  * It tries to end the program in a clean way.
1066  *****************************************************************************/
1067 static void FatalSignalHandler( int i_signal )
1068 {
1069     /* Once a signal has been trapped, the termination sequence will be
1070      * armed and following signals will be ignored to avoid sending messages
1071      * to an interface having been destroyed */
1072 #ifndef WIN32
1073     signal( SIGINT,  SIG_IGN );
1074     signal( SIGHUP,  SIG_IGN );
1075     signal( SIGQUIT, SIG_IGN );
1076 #endif
1077
1078     /* Acknowledge the signal received */
1079     intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1080
1081     /* Try to terminate everything - this is done by requesting the end of the
1082      * interface thread */
1083     p_main->p_intf->b_die = 1;
1084 }
1085
1086 /*****************************************************************************
1087  * InstructionSignalHandler: system signal handler
1088  *****************************************************************************
1089  * This function is called when a illegal instruction signal is received by
1090  * the program.
1091  * We use this function to test OS and CPU_Capabilities
1092  *****************************************************************************/
1093 static void InstructionSignalHandler( int i_signal )
1094 {
1095     /* Once a signal has been trapped, the termination sequence will be
1096      * armed and following signals will be ignored to avoid sending messages
1097      * to an interface having been destroyed */
1098
1099     /* Acknowledge the signal received */
1100     i_illegal = 1;
1101     
1102 #ifdef HAVE_SIGRELSE
1103     sigrelse( i_signal );
1104 #endif
1105     longjmp( env, 1 );
1106 }
1107
1108 /*****************************************************************************
1109  * CPUCapabilities: list the processors MMX support and other capabilities
1110  *****************************************************************************
1111  * This function is called to list extensions the CPU may have.
1112  *****************************************************************************/
1113 static int CPUCapabilities( void )
1114 {
1115     volatile int i_capabilities = CPU_CAPABILITY_NONE;
1116
1117 #if defined( SYS_BEOS )
1118     i_capabilities |= CPU_CAPABILITY_FPU
1119                       | CPU_CAPABILITY_486
1120                       | CPU_CAPABILITY_586
1121                       | CPU_CAPABILITY_MMX;
1122
1123     return( i_capabilities );
1124
1125 #elif defined( SYS_DARWIN )
1126     struct host_basic_info hi;
1127     kern_return_t          ret;
1128     host_name_port_t       host;
1129
1130     int i_size;
1131     char *psz_name, *psz_subname;
1132
1133     i_capabilities |= CPU_CAPABILITY_FPU;
1134
1135     /* Should 'never' fail? */
1136     host = mach_host_self();
1137
1138     i_size = sizeof( hi ) / sizeof( int );
1139     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1140
1141     if( ret != KERN_SUCCESS )
1142     {
1143         fprintf( stderr, "error: couldn't get CPU information\n" );
1144         return( i_capabilities );
1145     }
1146
1147     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1148     /* FIXME: need better way to detect newer proccessors.
1149      * could do strncmp(a,b,5), but that's real ugly */
1150     if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1151     {
1152         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1153     }
1154
1155     return( i_capabilities );
1156
1157 #elif defined( __i386__ )
1158     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
1159     volatile boolean_t     b_amd;
1160
1161     i_capabilities |= CPU_CAPABILITY_FPU;
1162
1163     signal( SIGILL, InstructionSignalHandler );
1164     
1165     /* test for a 486 CPU */
1166     asm volatile ( "pushfl\n\t"
1167                    "popl %%eax\n\t"
1168                    "movl %%eax, %%ebx\n\t"
1169                    "xorl $0x200000, %%eax\n\t"
1170                    "pushl %%eax\n\t"
1171                    "popfl\n\t"
1172                    "pushfl\n\t"
1173                    "popl %%eax"
1174                  : "=a" ( i_eax ),
1175                    "=b" ( i_ebx )
1176                  :
1177                  : "cc" );
1178
1179     if( i_eax == i_ebx )
1180     {
1181         signal( SIGILL, NULL );     
1182         return( i_capabilities );
1183     }
1184
1185     i_capabilities |= CPU_CAPABILITY_486;
1186
1187     /* the CPU supports the CPUID instruction - get its level */
1188     cpuid( 0x00000000 );
1189
1190     if( !i_eax )
1191     {
1192         signal( SIGILL, NULL );     
1193         return( i_capabilities );
1194     }
1195
1196     /* FIXME: this isn't correct, since some 486s have cpuid */
1197     i_capabilities |= CPU_CAPABILITY_586;
1198
1199     /* borrowed from mpeg2dec */
1200     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1201                     && ( i_edx == 0x69746e65 );
1202
1203     /* test for the MMX flag */
1204     cpuid( 0x00000001 );
1205
1206     if( ! (i_edx & 0x00800000) )
1207     {
1208         signal( SIGILL, NULL );     
1209         return( i_capabilities );
1210     }
1211
1212     i_capabilities |= CPU_CAPABILITY_MMX;
1213
1214     if( i_edx & 0x02000000 )
1215     {
1216         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1217
1218 #ifdef CAN_COMPILE_SSE
1219         /* We test if OS support the SSE instructions */
1220         i_illegal = 0;
1221         if( setjmp( env ) == 0 )
1222         {
1223             /* Test a SSE instruction */
1224             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1225         }
1226
1227         if( i_illegal == 0 )
1228         {
1229             i_capabilities |= CPU_CAPABILITY_SSE;
1230         }
1231         else
1232         {
1233             fprintf( stderr, "warning: your OS doesn't have support for "
1234                              "SSE instructions, "
1235                              "some optimizations\nwill be disabled\n" );
1236 #ifdef SYS_LINUX
1237             fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1238 #endif
1239         }
1240 #endif
1241     }
1242     
1243     /* test for additional capabilities */
1244     cpuid( 0x80000000 );
1245
1246     if( i_eax < 0x80000001 )
1247     {
1248         signal( SIGILL, NULL );     
1249         return( i_capabilities );
1250     }
1251
1252     /* list these additional capabilities */
1253     cpuid( 0x80000001 );
1254
1255 #ifdef CAN_COMPILE_3DNOW
1256     if( i_edx & 0x80000000 )
1257     {
1258         i_illegal = 0;
1259         if( setjmp( env ) == 0 )
1260         {
1261             /* Test a 3D Now! instruction */
1262             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1263         }
1264
1265         if( i_illegal == 0 ) 
1266         {
1267             i_capabilities |= CPU_CAPABILITY_3DNOW;
1268         }
1269     }
1270 #endif
1271
1272     if( b_amd && ( i_edx & 0x00400000 ) )
1273     {
1274         i_capabilities |= CPU_CAPABILITY_MMXEXT;
1275     }
1276
1277     signal( SIGILL, NULL );     
1278     return( i_capabilities );
1279
1280 #elif defined( __powerpc__ )
1281
1282     i_capabilities |= CPU_CAPABILITY_FPU;
1283
1284     /* Test for Altivec */
1285     signal( SIGILL, InstructionSignalHandler );
1286
1287 #ifdef CAN_COMPILE_ALTIVEC
1288     i_illegal = 0;
1289     if( setjmp( env ) == 0 )
1290     {
1291         asm volatile ("mtspr 256,%0" : : "r" (-1));
1292     }
1293
1294     if( i_illegal == 0 )
1295     {
1296         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1297     }
1298 #endif
1299
1300     signal( SIGILL, NULL );     
1301     return( i_capabilities );
1302
1303 #else
1304     /* default behaviour */
1305     return( i_capabilities );
1306
1307 #endif
1308 }
1309
1310 /*****************************************************************************
1311  * RedirectSTDOUT: redirect stdout and stderr to a file
1312  *****************************************************************************
1313  * This function will redirect stdout and stderr to a file if the user has
1314  * specified so.
1315  *****************************************************************************/
1316 static int RedirectSTDOUT( void )
1317 {
1318     int  i_stdout_filedesc;
1319     char *psz_stdout_filename;
1320
1321     psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1322                                                INTF_STDOUT_DEFAULT );
1323     if( strcmp( "", psz_stdout_filename ) != 0 )
1324     {
1325         ShowConsole();
1326         i_stdout_filedesc = open( psz_stdout_filename,
1327                                   O_CREAT | O_TRUNC | O_RDWR,
1328                                   S_IREAD | S_IWRITE );
1329
1330         if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1331         {
1332             intf_ErrMsg( "warning: unable to redirect stdout" );
1333         }
1334
1335         if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1336         {
1337             intf_ErrMsg( "warning: unable to redirect stderr" );
1338         }
1339
1340         close( i_stdout_filedesc );
1341     }
1342     else
1343     {
1344         /* No stdout redirection has been asked so open a console */
1345         if( p_main->i_warning_level )
1346         {
1347             ShowConsole();
1348         }
1349
1350     }
1351
1352     return 0;
1353 }
1354
1355 /*****************************************************************************
1356  * ShowConsole: On Win32, create an output console for debug messages
1357  *****************************************************************************
1358  * This function is usefull only on Win32.
1359  *****************************************************************************/
1360 static void ShowConsole( void )
1361 {
1362 #ifdef WIN32 /*  */
1363     AllocConsole();
1364     freopen( "CONOUT$", "w", stdout );
1365     freopen( "CONOUT$", "w", stderr );
1366     freopen( "CONIN$", "r", stdin );
1367 #endif
1368     return;
1369 }
1370