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