1 /*****************************************************************************
2 * main.c: main vlc source
3 * Includes the main() function for vlc. Parses command line, start interface
5 *****************************************************************************
6 * Copyright (C) 1998-2001 VideoLAN
7 * $Id: main.c,v 1.133 2001/12/09 17:01:37 sam Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
33 #include <stdio.h> /* sprintf() */
34 #include <setjmp.h> /* longjmp, setjmp */
36 #ifdef HAVE_GETOPT_LONG
38 # include <getopt.h> /* getopt() */
41 # include "GNUgetopt/getopt.h"
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>
52 # include <netinet/in.h> /* BSD: struct in_addr */
57 #elif defined( _MSC_VER ) && defined( _WIN32 )
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 */
72 #include "tests.h" /* TestCPU() */
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
78 #include "intf_playlist.h"
79 #include "interface.h"
81 #include "audio_output.h"
84 #include "video_output.h"
87 # include "beos_specific.h"
91 # include "darwin_specific.h"
95 # include "win32_specific.h"
98 #include "netutils.h" /* network_ChannelJoin */
100 /*****************************************************************************
101 * Command line options constants. If something is changed here, be sure that
102 * GetConfiguration and Usage are also changed.
103 *****************************************************************************/
105 /* Long options return values - note that values corresponding to short options
106 * chars, and in general any regular char, should be avoided */
107 #define OPT_NOAUDIO 150
108 #define OPT_STEREO 151
110 #define OPT_SPDIF 153
111 #define OPT_VOLUME 154
112 #define OPT_DESYNC 155
114 #define OPT_NOVIDEO 160
115 #define OPT_DISPLAY 161
116 #define OPT_WIDTH 162
117 #define OPT_HEIGHT 163
118 #define OPT_COLOR 164
119 #define OPT_FULLSCREEN 165
120 #define OPT_OVERLAY 166
121 #define OPT_XVADAPTOR 167
124 #define OPT_CHANNELS 170
125 #define OPT_SERVER 171
127 #define OPT_BROADCAST 173
128 #define OPT_CHANNELSERVER 174
130 #define OPT_INPUT 180
131 #define OPT_MOTION 181
134 #define OPT_DOWNMIX 184
135 #define OPT_IMDCT 185
136 #define OPT_MEMCPY 186
137 #define OPT_DVDCSS_METHOD 187
138 #define OPT_DVDCSS_VERBOSE 188
140 #define OPT_SYNCHRO 190
141 #define OPT_WARNING 191
142 #define OPT_VERSION 192
143 #define OPT_STDOUT 193
144 #define OPT_STATS 194
146 #define OPT_MPEG_ADEC 200
153 /* Needed for x86 CPU capabilities detection */
155 asm volatile ( "cpuid" \
164 static const struct option longopts[] =
166 /* name, has_arg, flag, val */
168 /* General/common options */
169 { "help", 0, 0, 'h' },
170 { "longhelp", 0, 0, 'H' },
171 { "version", 0, 0, OPT_VERSION },
173 /* Interface options */
174 { "intf", 1, 0, 'I' },
175 { "warning", 1, 0, OPT_WARNING },
176 { "stdout", 1, 0, OPT_STDOUT },
177 { "stats", 0, 0, OPT_STATS },
180 { "noaudio", 0, 0, OPT_NOAUDIO },
181 { "aout", 1, 0, 'A' },
182 { "stereo", 0, 0, OPT_STEREO },
183 { "mono", 0, 0, OPT_MONO },
184 { "spdif", 0, 0, OPT_SPDIF },
185 { "downmix", 1, 0, OPT_DOWNMIX },
186 { "imdct", 1, 0, OPT_IMDCT },
187 { "volume", 1, 0, OPT_VOLUME },
188 { "desync", 1, 0, OPT_DESYNC },
191 { "novideo", 0, 0, OPT_NOVIDEO },
192 { "vout", 1, 0, 'V' },
193 { "display", 1, 0, OPT_DISPLAY },
194 { "width", 1, 0, OPT_WIDTH },
195 { "height", 1, 0, OPT_HEIGHT },
196 { "grayscale", 0, 0, 'g' },
197 { "color", 0, 0, OPT_COLOR },
198 { "motion", 1, 0, OPT_MOTION },
199 { "idct", 1, 0, OPT_IDCT },
200 { "yuv", 1, 0, OPT_YUV },
201 { "fullscreen", 0, 0, OPT_FULLSCREEN },
202 { "overlay", 0, 0, OPT_OVERLAY },
203 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
204 { "smp", 1, 0, OPT_SMP },
207 { "dvdtitle", 1, 0, 't' },
208 { "dvdchapter", 1, 0, 'T' },
209 { "dvdangle", 1, 0, 'u' },
210 { "dvdaudio", 1, 0, 'a' },
211 { "dvdchannel", 1, 0, 'c' },
212 { "dvdsubtitle", 1, 0, 's' },
213 { "dvdcss-method", 1, 0, OPT_DVDCSS_METHOD },
214 { "dvdcss-verbose", 1, 0, OPT_DVDCSS_VERBOSE },
217 { "input", 1, 0, OPT_INPUT },
218 { "channels", 0, 0, OPT_CHANNELS },
219 { "channelserver", 1, 0, OPT_CHANNELSERVER },
222 { "synchro", 1, 0, OPT_SYNCHRO },
223 { "memcpy", 1, 0, OPT_MEMCPY },
225 /* Decoder options */
226 { "mpeg_adec", 1, 0, OPT_MPEG_ADEC },
232 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
234 /*****************************************************************************
235 * Global variables - these are the only ones, see main.h and modules.h
236 *****************************************************************************/
238 module_bank_t *p_module_bank;
239 aout_bank_t *p_aout_bank;
240 vout_bank_t *p_vout_bank;
242 /*****************************************************************************
244 *****************************************************************************/
245 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
247 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
248 static void Usage ( int i_fashion );
249 static void Version ( void );
251 static void InitSignalHandler ( void );
252 static void SimpleSignalHandler ( int i_signal );
253 static void FatalSignalHandler ( int i_signal );
254 static void InstructionSignalHandler( int i_signal );
255 static int CPUCapabilities ( void );
257 static int RedirectSTDOUT ( void );
258 static void ShowConsole ( void );
261 static int i_illegal;
263 /*****************************************************************************
264 * main: parse command line, start interface and spawn threads
265 *****************************************************************************
266 * Steps during program execution are:
267 * -configuration parsing and messages interface initialization
268 * -opening of audio output device and some global modules
269 * -execution of interface, which exit on error or on user request
270 * -closing of audio output device and some global modules
271 * On error, the spawned threads are canceled, and the open devices closed.
272 *****************************************************************************/
273 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
275 main_t main_data; /* root of all data - see main.h */
276 module_bank_t module_bank;
277 aout_bank_t aout_bank;
278 vout_bank_t vout_bank;
280 p_main = &main_data; /* set up the global variables */
281 p_module_bank = &module_bank;
282 p_aout_bank = &aout_bank;
283 p_vout_bank = &vout_bank;
286 * Initialize threads system
291 * Test if our code is likely to run on this CPU
293 p_main->i_cpu_capabilities = CPUCapabilities();
296 * System specific initialization code
298 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
299 system_Init( &i_argc, ppsz_argv, ppsz_env );
301 #elif defined( SYS_LINUX )
303 /* Activate malloc checking routines to detect heap corruptions. */
304 main_PutIntVariable( "MALLOC_CHECK_", 2 );
309 * Initialize messages interface
311 p_main->p_msg = intf_MsgCreate();
312 if( !p_main->p_msg ) /* start messages interface */
314 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
319 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
324 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
331 * Redirect the standard output if required by the user, and on Win32 we
332 * also open a console to display the debug messages.
336 if( p_main->b_stats )
338 char p_capabilities[200];
339 p_capabilities[0] = '\0';
341 #define PRINT_CAPABILITY( capability, string ) \
342 if( p_main->i_cpu_capabilities & capability ) \
344 strncat( p_capabilities, string " ", \
345 sizeof(p_capabilities) - strlen(p_capabilities) ); \
346 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
349 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
350 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
351 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
352 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
353 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
354 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
355 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
356 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
357 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
361 * Initialize playlist and get commandline files
363 p_main->p_playlist = intf_PlaylistCreate();
364 if( !p_main->p_playlist )
366 intf_ErrMsg( "playlist error: playlist initialization failed" );
370 intf_PlaylistInit( p_main->p_playlist );
373 * Get input filenames given as commandline arguments
375 GetFilenames( i_argc, ppsz_argv );
378 * Initialize module, aout and vout banks
385 * Choose the best memcpy module
387 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );
389 if( p_main->p_memcpy_module == NULL )
391 intf_ErrMsg( "intf error: no suitable memcpy module, "
392 "using libc default" );
393 p_main->fast_memcpy = memcpy;
397 #define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
398 p_main->fast_memcpy = f.fast_memcpy;
403 * Initialize shared resources and libraries
405 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
406 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
407 network_ChannelCreate() )
409 /* On error during Channels initialization, switch off channels */
410 intf_Msg( "Channels initialization failed : "
411 "Channel management is deactivated" );
412 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
416 * Try to run the interface
418 p_main->p_intf = intf_Create();
419 if( p_main->p_intf == NULL )
421 intf_ErrMsg( "intf error: interface initialization failed" );
426 * Set signal handling policy for all threads
431 * This is the main loop
433 p_main->p_intf->pf_run( p_main->p_intf );
436 * Finished, destroy the interface
438 intf_Destroy( p_main->p_intf );
441 * Go back into channel 0 which is the network
443 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
444 INPUT_NETWORK_CHANNEL_DEFAULT ) )
446 network_ChannelJoin( COMMON_CHANNEL );
453 module_Unneed( p_main->p_memcpy_module );
456 * Free module, aout and vout banks
465 intf_PlaylistDestroy( p_main->p_playlist );
468 * System specific cleaning code
470 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
476 * Terminate messages interface and program
478 intf_Msg( "intf: program terminated" );
482 * Stop threads system
489 /*****************************************************************************
490 * main_GetIntVariable: get the int value of an environment variable
491 *****************************************************************************
492 * This function is used to read some default parameters in modules.
493 *****************************************************************************/
494 int main_GetIntVariable( char *psz_name, int i_default )
496 char * psz_env; /* environment value */
497 char * psz_end; /* end of parsing index */
498 long int i_value; /* value */
500 psz_env = getenv( psz_name );
503 i_value = strtol( psz_env, &psz_end, 0 );
504 if( (*psz_env != '\0') && (*psz_end == '\0') )
512 /*****************************************************************************
513 * main_GetPszVariable: get the string value of an environment variable
514 *****************************************************************************
515 * This function is used to read some default parameters in modules.
516 *****************************************************************************/
517 char * main_GetPszVariable( char *psz_name, char *psz_default )
521 psz_env = getenv( psz_name );
526 return( psz_default );
529 /*****************************************************************************
530 * main_PutPszVariable: set the string value of an environment variable
531 *****************************************************************************
532 * This function is used to set some default parameters in modules. The use of
533 * this function will cause some memory leak: since some systems use the pointer
534 * passed to putenv to store the environment string, it can't be freed.
535 *****************************************************************************/
536 void main_PutPszVariable( char *psz_name, char *psz_value )
540 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
541 if( psz_env == NULL )
543 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
548 sprintf( psz_env, "%s=%s", psz_name, psz_value );
549 if( putenv( psz_env ) )
551 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
556 /*****************************************************************************
557 * main_PutIntVariable: set the integer value of an environment variable
558 *****************************************************************************
559 * This function is used to set some default parameters in modules. The use of
560 * this function will cause some memory leak: since some systems use the pointer
561 * passed to putenv to store the environment string, it can't be freed.
562 *****************************************************************************/
563 void main_PutIntVariable( char *psz_name, int i_value )
565 char psz_value[ 256 ]; /* buffer for value */
567 sprintf( psz_value, "%d", i_value );
568 main_PutPszVariable( psz_name, psz_value );
571 /* following functions are local */
573 /*****************************************************************************
574 * GetConfiguration: parse command line
575 *****************************************************************************
576 * Parse command line and configuration file for configuration. If the inline
577 * help is requested, the function Usage() is called and the function returns
578 * -1 (causing main() to exit). The messages interface is initialized at this
579 * stage, but most structures are not allocated, so only environment should
581 *****************************************************************************/
582 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
587 /* Set default configuration and copy arguments */
588 p_main->i_argc = *pi_argc;
589 p_main->ppsz_argv = ppsz_argv;
590 p_main->ppsz_env = ppsz_env;
595 p_main->i_warning_level = 0;
597 p_main->i_desync = 0; /* No desynchronization by default */
599 p_main->p_channel = NULL;
601 /* Get the executable name (similar to the basename command) */
602 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
607 p_main->psz_arg0 = ++p_tmp;
616 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
617 * is the PSN - process serial number (a unique PID-ish thingie)
618 * still ok for real Darwin & when run from command line */
619 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
620 /* for example -psn_0_9306113 */
622 /* GDMF!... I can't do this or else the MacOSX window server will
623 * not pick up the PSN and not register the app and we crash...
624 * hence the following kludge otherwise we'll get confused w/ argv[1]
625 * being an input file name */
627 ppsz_argv[ 1 ] = NULL;
629 *pi_argc = *pi_argc - 1;
635 /* Parse command line options */
637 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
638 psz_shortopts, longopts, 0 ) ) != EOF )
642 /* General/common options */
643 case 'h': /* -h, --help */
647 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
648 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
649 INTF_STDOUT_DEFAULT ) ) == 0 )
651 /* No stdout redirection has been asked for */
652 intf_MsgImm( "\nPress the RETURN key to continue..." );
658 case 'H': /* -H, --longhelp */
662 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
663 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
664 INTF_STDOUT_DEFAULT ) ) == 0 )
666 /* No stdout redirection has been asked for */
667 intf_MsgImm( "\nPress the RETURN key to continue..." );
673 case OPT_VERSION: /* --version */
677 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
678 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
679 INTF_STDOUT_DEFAULT ) ) == 0 )
681 /* No stdout redirection has been asked for */
682 intf_MsgImm( "\nPress the RETURN key to continue..." );
688 case 'v': /* -v, --verbose */
689 p_main->i_warning_level++;
692 /* Interface warning messages level */
693 case 'I': /* -I, --intf */
694 main_PutPszVariable( INTF_METHOD_VAR, optarg );
696 case OPT_WARNING: /* --warning */
697 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
698 p_main->i_warning_level = atoi(optarg);
701 case OPT_STDOUT: /* --stdout */
702 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
710 case OPT_NOAUDIO: /* --noaudio */
713 case 'A': /* -A, --aout */
714 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
716 case OPT_STEREO: /* --stereo */
717 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
719 case OPT_MONO: /* --mono */
720 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
722 case OPT_SPDIF: /* --spdif */
723 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
725 case OPT_DOWNMIX: /* --downmix */
726 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
728 case OPT_IMDCT: /* --imdct */
729 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
731 case OPT_VOLUME: /* --volume */
732 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
734 case OPT_DESYNC: /* --desync */
735 p_main->i_desync = atoi(optarg);
739 case OPT_NOVIDEO: /* --novideo */
742 case 'V': /* -V, --vout */
743 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
745 case OPT_DISPLAY: /* --display */
746 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
748 case OPT_WIDTH: /* --width */
749 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
751 case OPT_HEIGHT: /* --height */
752 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
754 case 'g': /* -g, --grayscale */
755 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
757 case OPT_COLOR: /* --color */
758 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
760 case OPT_FULLSCREEN: /* --fullscreen */
761 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
763 case OPT_OVERLAY: /* --overlay */
764 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
766 case OPT_XVADAPTOR: /* --xvadaptor */
767 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
769 case OPT_MOTION: /* --motion */
770 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
772 case OPT_IDCT: /* --idct */
773 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
775 case OPT_YUV: /* --yuv */
776 main_PutPszVariable( YUV_METHOD_VAR, optarg );
778 case OPT_SMP: /* --smp */
779 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
783 case 't': /* --dvdtitle */
784 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
786 case 'T': /* --dvdchapter */
787 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
789 case 'u': /* --dvdangle */
790 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
792 case 'a': /* --dvdaudio */
793 if ( ! strcmp(optarg, "ac3") )
794 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
795 else if ( ! strcmp(optarg, "lpcm") )
796 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
797 else if ( ! strcmp(optarg, "mpeg") )
798 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
800 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
802 case 'c': /* --dvdchannel */
803 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
805 case 's': /* --dvdsubtitle */
806 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
808 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
809 main_PutPszVariable( "DVDCSS_METHOD", optarg );
811 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
812 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
816 case OPT_INPUT: /* --input */
817 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
819 case OPT_CHANNELS: /* --channels */
820 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
822 case OPT_CHANNELSERVER: /* --channelserver */
823 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
828 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
831 main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
834 /* Decoder options */
836 main_PutPszVariable( ADEC_MPEG_VAR, optarg );
839 /* Internal error: unknown option */
844 intf_ErrMsg( "intf error: unknown option `%s'",
847 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
848 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
849 INTF_STDOUT_DEFAULT ) ) == 0 )
851 /* No stdout redirection has been asked for */
852 intf_MsgImm( "\nPress the RETURN key to continue..." );
861 if( p_main->i_warning_level < 0 )
863 p_main->i_warning_level = 0;
869 /*****************************************************************************
870 * GetFilenames: parse command line options which are not flags
871 *****************************************************************************
872 * Parse command line for input files.
873 *****************************************************************************/
874 static int GetFilenames( int i_argc, char *ppsz_argv[] )
878 /* We assume that the remaining parameters are filenames */
879 for( i_opt = optind; i_opt < i_argc; i_opt++ )
881 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
882 ppsz_argv[ i_opt ] );
888 /*****************************************************************************
889 * Usage: print program usage
890 *****************************************************************************
891 * Print a short inline help. Message interface is initialized at this stage.
892 *****************************************************************************/
893 static void Usage( int i_fashion )
896 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
899 if( i_fashion == USAGE )
901 intf_MsgImm( "Try `%s --help' for more information.",
907 intf_MsgImm( "\nOptions:"
908 "\n -I, --intf <module> \tinterface method"
909 "\n -v, --verbose \tverbose mode (cumulative)"
910 "\n --stdout <filename> \tredirect console stdout"
911 "\n --memcpy <module> \tmemcpy method"
913 "\n --noaudio \tdisable audio"
914 "\n -A, --aout <module> \taudio output method"
915 "\n --stereo, --mono \tstereo/mono audio"
916 "\n --spdif \tAC3 pass-through mode"
917 "\n --downmix <module> \tAC3 downmix method"
918 "\n --imdct <module> \tAC3 IMDCT method"
919 "\n --volume [0..1024] \tVLC output volume"
920 "\n --desync <time in ms> \tCompensate desynchronization of the audio"
922 "\n --novideo \tdisable video"
923 "\n -V, --vout <module> \tvideo output method"
924 "\n --display <display> \tdisplay string"
925 "\n --width <w>, --height <h> \tdisplay dimensions"
926 "\n -g, --grayscale \tgrayscale output"
927 "\n --fullscreen \tfullscreen output"
928 "\n --overlay \taccelerated display"
929 "\n --xvadaptor <adaptor> \tXVideo adaptor"
930 "\n --color \tcolor output"
931 "\n --motion <module> \tmotion compensation method"
932 "\n --idct <module> \tIDCT method"
933 "\n --yuv <module> \tYUV method"
934 "\n --synchro <type> \tforce synchro algorithm"
935 "\n --smp <number of threads> \tuse several processors"
937 "\n -t, --dvdtitle <num> \tchoose DVD title"
938 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
939 "\n -u, --dvdangle <num> \tchoose DVD angle"
940 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
941 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
942 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
943 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
944 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
946 "\n --input \tinput method"
947 "\n --channels \tenable channels"
948 "\n --channelserver <host> \tchannel server address"
950 "\n --mpeg_adec <builtin|mad> \tchoose audio decoder"
952 "\n -h, --help \tprint help and exit"
953 "\n -H, --longhelp \tprint long help and exit"
954 "\n --version \toutput version information and exit"
955 "\n\nPlaylist items :"
956 "\n *.mpg, *.vob \tPlain MPEG-1/2 files"
957 "\n dvd:<device>[@<raw device>] \tDVD device"
958 "\n vcd:<device> \tVCD device"
959 "\n udpstream:[<server>[:<server port>]][@[<bind address>][:<bind port>]]"
960 "\n \tUDP stream sent by VLS"
961 "\n vlc:loop \tLoop execution of the playlist"
962 "\n vlc:pause \tPause execution of playlist items"
963 "\n vlc:quit \tQuit VLC");
965 if( i_fashion == SHORT_HELP )
968 /* Interface parameters */
969 intf_MsgImm( "\nInterface parameters:"
970 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
971 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
972 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
973 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout"
974 "\n " MEMCPY_METHOD_VAR "=<method name> \tmemcpy method" );
976 /* Audio parameters */
977 intf_MsgImm( "\nAudio parameters:"
978 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
979 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
980 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
981 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
982 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
983 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
984 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
985 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
987 /* Video parameters */
988 intf_MsgImm( "\nVideo parameters:"
989 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
990 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
991 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
992 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
993 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
994 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
995 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
996 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
997 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
998 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
999 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
1000 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
1001 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
1002 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
1004 /* DVD parameters */
1005 intf_MsgImm( "\nDVD parameters:"
1006 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
1007 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
1008 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
1009 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
1010 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
1011 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
1012 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
1014 /* Input parameters */
1015 intf_MsgImm( "\nInput parameters:"
1016 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
1017 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
1018 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
1020 /* Decoder parameters */
1021 intf_MsgImm( "\nDecoder parameters:"
1022 "\n " ADEC_MPEG_VAR "=<builtin|mad> \taudio decoder" );
1025 /*****************************************************************************
1026 * Version: print complete program version
1027 *****************************************************************************
1028 * Print complete program version and build number.
1029 *****************************************************************************/
1030 static void Version( void )
1032 intf_MsgImm( VERSION_MESSAGE
1033 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1034 "You may redistribute it under the terms of the GNU General Public License;\n"
1035 "see the file named COPYING for details.\n"
1036 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1039 /*****************************************************************************
1040 * InitSignalHandler: system signal handler initialization
1041 *****************************************************************************
1042 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1043 * at least a method to kill the program when all other methods failed, and
1044 * when we don't want to use SIGKILL.
1045 *****************************************************************************/
1046 static void InitSignalHandler( void )
1048 /* Termination signals */
1050 signal( SIGINT, FatalSignalHandler );
1051 signal( SIGHUP, FatalSignalHandler );
1052 signal( SIGQUIT, FatalSignalHandler );
1055 signal( SIGALRM, SimpleSignalHandler );
1056 signal( SIGPIPE, SimpleSignalHandler );
1061 /*****************************************************************************
1062 * SimpleSignalHandler: system signal handler
1063 *****************************************************************************
1064 * This function is called when a non fatal signal is received by the program.
1065 *****************************************************************************/
1066 static void SimpleSignalHandler( int i_signal )
1068 /* Acknowledge the signal received */
1069 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1073 /*****************************************************************************
1074 * FatalSignalHandler: system signal handler
1075 *****************************************************************************
1076 * This function is called when a fatal signal is received by the program.
1077 * It tries to end the program in a clean way.
1078 *****************************************************************************/
1079 static void FatalSignalHandler( int i_signal )
1081 /* Once a signal has been trapped, the termination sequence will be
1082 * armed and following signals will be ignored to avoid sending messages
1083 * to an interface having been destroyed */
1085 signal( SIGINT, SIG_IGN );
1086 signal( SIGHUP, SIG_IGN );
1087 signal( SIGQUIT, SIG_IGN );
1090 /* Acknowledge the signal received */
1091 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1093 /* Try to terminate everything - this is done by requesting the end of the
1094 * interface thread */
1095 p_main->p_intf->b_die = 1;
1098 /*****************************************************************************
1099 * InstructionSignalHandler: system signal handler
1100 *****************************************************************************
1101 * This function is called when a illegal instruction signal is received by
1103 * We use this function to test OS and CPU_Capabilities
1104 *****************************************************************************/
1105 static void InstructionSignalHandler( int i_signal )
1107 /* Once a signal has been trapped, the termination sequence will be
1108 * armed and following signals will be ignored to avoid sending messages
1109 * to an interface having been destroyed */
1111 /* Acknowledge the signal received */
1114 #ifdef HAVE_SIGRELSE
1115 sigrelse( i_signal );
1120 /*****************************************************************************
1121 * CPUCapabilities: list the processors MMX support and other capabilities
1122 *****************************************************************************
1123 * This function is called to list extensions the CPU may have.
1124 *****************************************************************************/
1125 static int CPUCapabilities( void )
1127 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1129 #if defined( SYS_BEOS )
1130 i_capabilities |= CPU_CAPABILITY_FPU
1131 | CPU_CAPABILITY_486
1132 | CPU_CAPABILITY_586
1133 | CPU_CAPABILITY_MMX;
1135 return( i_capabilities );
1137 #elif defined( SYS_DARWIN )
1138 struct host_basic_info hi;
1140 host_name_port_t host;
1143 char *psz_name, *psz_subname;
1145 i_capabilities |= CPU_CAPABILITY_FPU;
1147 /* Should 'never' fail? */
1148 host = mach_host_self();
1150 i_size = sizeof( hi ) / sizeof( int );
1151 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1153 if( ret != KERN_SUCCESS )
1155 fprintf( stderr, "error: couldn't get CPU information\n" );
1156 return( i_capabilities );
1159 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1160 /* FIXME: need better way to detect newer proccessors.
1161 * could do strncmp(a,b,5), but that's real ugly */
1162 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1164 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1167 return( i_capabilities );
1169 #elif defined( __i386__ )
1170 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1171 volatile boolean_t b_amd;
1173 i_capabilities |= CPU_CAPABILITY_FPU;
1175 signal( SIGILL, InstructionSignalHandler );
1177 /* test for a 486 CPU */
1178 asm volatile ( "pushfl\n\t"
1180 "movl %%eax, %%ebx\n\t"
1181 "xorl $0x200000, %%eax\n\t"
1191 if( i_eax == i_ebx )
1193 signal( SIGILL, NULL );
1194 return( i_capabilities );
1197 i_capabilities |= CPU_CAPABILITY_486;
1199 /* the CPU supports the CPUID instruction - get its level */
1200 cpuid( 0x00000000 );
1204 signal( SIGILL, NULL );
1205 return( i_capabilities );
1208 /* FIXME: this isn't correct, since some 486s have cpuid */
1209 i_capabilities |= CPU_CAPABILITY_586;
1211 /* borrowed from mpeg2dec */
1212 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1213 && ( i_edx == 0x69746e65 );
1215 /* test for the MMX flag */
1216 cpuid( 0x00000001 );
1218 if( ! (i_edx & 0x00800000) )
1220 signal( SIGILL, NULL );
1221 return( i_capabilities );
1224 i_capabilities |= CPU_CAPABILITY_MMX;
1226 if( i_edx & 0x02000000 )
1228 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1230 #ifdef CAN_COMPILE_SSE
1231 /* We test if OS support the SSE instructions */
1233 if( setjmp( env ) == 0 )
1235 /* Test a SSE instruction */
1236 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1239 if( i_illegal == 0 )
1241 i_capabilities |= CPU_CAPABILITY_SSE;
1245 fprintf( stderr, "warning: your OS doesn't have support for "
1246 "SSE instructions, "
1247 "some optimizations\nwill be disabled\n" );
1249 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1255 /* test for additional capabilities */
1256 cpuid( 0x80000000 );
1258 if( i_eax < 0x80000001 )
1260 signal( SIGILL, NULL );
1261 return( i_capabilities );
1264 /* list these additional capabilities */
1265 cpuid( 0x80000001 );
1267 #ifdef CAN_COMPILE_3DNOW
1268 if( i_edx & 0x80000000 )
1271 if( setjmp( env ) == 0 )
1273 /* Test a 3D Now! instruction */
1274 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1277 if( i_illegal == 0 )
1279 i_capabilities |= CPU_CAPABILITY_3DNOW;
1284 if( b_amd && ( i_edx & 0x00400000 ) )
1286 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1289 signal( SIGILL, NULL );
1290 return( i_capabilities );
1292 #elif defined( __powerpc__ )
1294 i_capabilities |= CPU_CAPABILITY_FPU;
1296 /* Test for Altivec */
1297 signal( SIGILL, InstructionSignalHandler );
1299 #ifdef CAN_COMPILE_ALTIVEC
1301 if( setjmp( env ) == 0 )
1303 asm volatile ("mtspr 256, %0\n\t"
1304 "vand %%v0, %%v0, %%v0"
1309 if( i_illegal == 0 )
1311 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1315 signal( SIGILL, NULL );
1316 return( i_capabilities );
1319 /* default behaviour */
1320 return( i_capabilities );
1325 /*****************************************************************************
1326 * RedirectSTDOUT: redirect stdout and stderr to a file
1327 *****************************************************************************
1328 * This function will redirect stdout and stderr to a file if the user has
1330 *****************************************************************************/
1331 static int RedirectSTDOUT( void )
1336 psz_filename = main_GetPszVariable( INTF_STDOUT_VAR, INTF_STDOUT_DEFAULT );
1341 i_fd = open( psz_filename, O_CREAT | O_TRUNC | O_RDWR,
1342 S_IREAD | S_IWRITE );
1343 if( dup2( i_fd, fileno(stdout) ) == -1 )
1345 intf_ErrMsg( "warning: unable to redirect stdout" );
1348 if( dup2( i_fd, fileno(stderr) ) == -1 )
1350 intf_ErrMsg( "warning: unable to redirect stderr" );
1357 /* No stdout redirection has been asked so open a console */
1358 if( p_main->i_warning_level )
1368 /*****************************************************************************
1369 * ShowConsole: On Win32, create an output console for debug messages
1370 *****************************************************************************
1371 * This function is usefull only on Win32.
1372 *****************************************************************************/
1373 static void ShowConsole( void )
1377 freopen( "CONOUT$", "w", stdout );
1378 freopen( "CONOUT$", "w", stderr );
1379 freopen( "CONIN$", "r", stdin );