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