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