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