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.134 2001/12/10 04:53:11 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;
289 if( ! setlocale(LC_MESSAGES, "") )
291 fprintf( stderr, "warning: unsupported locale.\n" );
294 if( ! bindtextdomain(PACKAGE, LOCALEDIR) )
296 fprintf( stderr, "warning: no domain %s in directory %s\n",
297 PACKAGE, LOCALEDIR );
300 textdomain( PACKAGE );
301 printf("main: %s\n", gettext("Net"));
305 * Initialize threads system
310 * Test if our code is likely to run on this CPU
312 p_main->i_cpu_capabilities = CPUCapabilities();
315 * System specific initialization code
317 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
318 system_Init( &i_argc, ppsz_argv, ppsz_env );
320 #elif defined( SYS_LINUX )
322 /* Activate malloc checking routines to detect heap corruptions. */
323 main_PutIntVariable( "MALLOC_CHECK_", 2 );
328 * Initialize messages interface
330 p_main->p_msg = intf_MsgCreate();
331 if( !p_main->p_msg ) /* start messages interface */
333 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
338 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
343 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
350 * Redirect the standard output if required by the user, and on Win32 we
351 * also open a console to display the debug messages.
355 if( p_main->b_stats )
357 char p_capabilities[200];
358 p_capabilities[0] = '\0';
360 #define PRINT_CAPABILITY( capability, string ) \
361 if( p_main->i_cpu_capabilities & capability ) \
363 strncat( p_capabilities, string " ", \
364 sizeof(p_capabilities) - strlen(p_capabilities) ); \
365 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
368 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
369 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
370 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
371 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
372 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
373 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
374 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
375 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
376 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
380 * Initialize playlist and get commandline files
382 p_main->p_playlist = intf_PlaylistCreate();
383 if( !p_main->p_playlist )
385 intf_ErrMsg( "playlist error: playlist initialization failed" );
389 intf_PlaylistInit( p_main->p_playlist );
392 * Get input filenames given as commandline arguments
394 GetFilenames( i_argc, ppsz_argv );
397 * Initialize module, aout and vout banks
404 * Choose the best memcpy module
406 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );
408 if( p_main->p_memcpy_module != NULL )
410 #define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
411 p_main->fast_memcpy = f.fast_memcpy;
416 intf_ErrMsg( "intf error: no suitable memcpy module, "
417 "using libc default" );
418 p_main->fast_memcpy = memcpy;
422 * Initialize shared resources and libraries
424 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
425 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
426 network_ChannelCreate() )
428 /* On error during Channels initialization, switch off channels */
429 intf_Msg( "Channels initialization failed : "
430 "Channel management is deactivated" );
431 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
435 * Try to run the interface
437 p_main->p_intf = intf_Create();
438 if( p_main->p_intf == NULL )
440 intf_ErrMsg( "intf error: interface initialization failed" );
445 * Set signal handling policy for all threads
450 * This is the main loop
452 p_main->p_intf->pf_run( p_main->p_intf );
455 * Finished, destroy the interface
457 intf_Destroy( p_main->p_intf );
460 * Go back into channel 0 which is the network
462 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
463 INPUT_NETWORK_CHANNEL_DEFAULT ) )
465 network_ChannelJoin( COMMON_CHANNEL );
472 if( p_main->p_memcpy_module != NULL )
474 module_Unneed( p_main->p_memcpy_module );
478 * Free module, aout and vout banks
487 intf_PlaylistDestroy( p_main->p_playlist );
490 * System specific cleaning code
492 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
498 * Terminate messages interface and program
500 intf_Msg( "intf: program terminated" );
504 * Stop threads system
511 /*****************************************************************************
512 * main_GetIntVariable: get the int value of an environment variable
513 *****************************************************************************
514 * This function is used to read some default parameters in modules.
515 *****************************************************************************/
516 int main_GetIntVariable( char *psz_name, int i_default )
518 char * psz_env; /* environment value */
519 char * psz_end; /* end of parsing index */
520 long int i_value; /* value */
522 psz_env = getenv( psz_name );
525 i_value = strtol( psz_env, &psz_end, 0 );
526 if( (*psz_env != '\0') && (*psz_end == '\0') )
534 /*****************************************************************************
535 * main_GetPszVariable: get the string value of an environment variable
536 *****************************************************************************
537 * This function is used to read some default parameters in modules.
538 *****************************************************************************/
539 char * main_GetPszVariable( char *psz_name, char *psz_default )
543 psz_env = getenv( psz_name );
548 return( psz_default );
551 /*****************************************************************************
552 * main_PutPszVariable: set the string value of an environment variable
553 *****************************************************************************
554 * This function is used to set some default parameters in modules. The use of
555 * this function will cause some memory leak: since some systems use the pointer
556 * passed to putenv to store the environment string, it can't be freed.
557 *****************************************************************************/
558 void main_PutPszVariable( char *psz_name, char *psz_value )
562 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
563 if( psz_env == NULL )
565 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
570 sprintf( psz_env, "%s=%s", psz_name, psz_value );
571 if( putenv( psz_env ) )
573 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
578 /*****************************************************************************
579 * main_PutIntVariable: set the integer value of an environment variable
580 *****************************************************************************
581 * This function is used to set some default parameters in modules. The use of
582 * this function will cause some memory leak: since some systems use the pointer
583 * passed to putenv to store the environment string, it can't be freed.
584 *****************************************************************************/
585 void main_PutIntVariable( char *psz_name, int i_value )
587 char psz_value[ 256 ]; /* buffer for value */
589 sprintf( psz_value, "%d", i_value );
590 main_PutPszVariable( psz_name, psz_value );
593 /* following functions are local */
595 /*****************************************************************************
596 * GetConfiguration: parse command line
597 *****************************************************************************
598 * Parse command line and configuration file for configuration. If the inline
599 * help is requested, the function Usage() is called and the function returns
600 * -1 (causing main() to exit). The messages interface is initialized at this
601 * stage, but most structures are not allocated, so only environment should
603 *****************************************************************************/
604 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
609 /* Set default configuration and copy arguments */
610 p_main->i_argc = *pi_argc;
611 p_main->ppsz_argv = ppsz_argv;
612 p_main->ppsz_env = ppsz_env;
617 p_main->i_warning_level = 0;
619 p_main->i_desync = 0; /* No desynchronization by default */
621 p_main->p_channel = NULL;
623 /* Get the executable name (similar to the basename command) */
624 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
629 p_main->psz_arg0 = ++p_tmp;
638 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
639 * is the PSN - process serial number (a unique PID-ish thingie)
640 * still ok for real Darwin & when run from command line */
641 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
642 /* for example -psn_0_9306113 */
644 /* GDMF!... I can't do this or else the MacOSX window server will
645 * not pick up the PSN and not register the app and we crash...
646 * hence the following kludge otherwise we'll get confused w/ argv[1]
647 * being an input file name */
649 ppsz_argv[ 1 ] = NULL;
651 *pi_argc = *pi_argc - 1;
657 /* Parse command line options */
659 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
660 psz_shortopts, longopts, 0 ) ) != EOF )
664 /* General/common options */
665 case 'h': /* -h, --help */
669 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
670 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
671 INTF_STDOUT_DEFAULT ) ) == 0 )
673 /* No stdout redirection has been asked for */
674 intf_MsgImm( "\nPress the RETURN key to continue..." );
680 case 'H': /* -H, --longhelp */
684 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
685 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
686 INTF_STDOUT_DEFAULT ) ) == 0 )
688 /* No stdout redirection has been asked for */
689 intf_MsgImm( "\nPress the RETURN key to continue..." );
695 case OPT_VERSION: /* --version */
699 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
700 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
701 INTF_STDOUT_DEFAULT ) ) == 0 )
703 /* No stdout redirection has been asked for */
704 intf_MsgImm( "\nPress the RETURN key to continue..." );
710 case 'v': /* -v, --verbose */
711 p_main->i_warning_level++;
714 /* Interface warning messages level */
715 case 'I': /* -I, --intf */
716 main_PutPszVariable( INTF_METHOD_VAR, optarg );
718 case OPT_WARNING: /* --warning */
719 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
720 p_main->i_warning_level = atoi(optarg);
723 case OPT_STDOUT: /* --stdout */
724 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
732 case OPT_NOAUDIO: /* --noaudio */
735 case 'A': /* -A, --aout */
736 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
738 case OPT_STEREO: /* --stereo */
739 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
741 case OPT_MONO: /* --mono */
742 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
744 case OPT_SPDIF: /* --spdif */
745 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
747 case OPT_DOWNMIX: /* --downmix */
748 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
750 case OPT_IMDCT: /* --imdct */
751 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
753 case OPT_VOLUME: /* --volume */
754 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
756 case OPT_DESYNC: /* --desync */
757 p_main->i_desync = atoi(optarg);
761 case OPT_NOVIDEO: /* --novideo */
764 case 'V': /* -V, --vout */
765 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
767 case OPT_DISPLAY: /* --display */
768 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
770 case OPT_WIDTH: /* --width */
771 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
773 case OPT_HEIGHT: /* --height */
774 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
776 case 'g': /* -g, --grayscale */
777 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
779 case OPT_COLOR: /* --color */
780 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
782 case OPT_FULLSCREEN: /* --fullscreen */
783 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
785 case OPT_OVERLAY: /* --overlay */
786 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
788 case OPT_XVADAPTOR: /* --xvadaptor */
789 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
791 case OPT_MOTION: /* --motion */
792 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
794 case OPT_IDCT: /* --idct */
795 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
797 case OPT_YUV: /* --yuv */
798 main_PutPszVariable( YUV_METHOD_VAR, optarg );
800 case OPT_SMP: /* --smp */
801 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
805 case 't': /* --dvdtitle */
806 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
808 case 'T': /* --dvdchapter */
809 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
811 case 'u': /* --dvdangle */
812 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
814 case 'a': /* --dvdaudio */
815 if ( ! strcmp(optarg, "ac3") )
816 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
817 else if ( ! strcmp(optarg, "lpcm") )
818 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
819 else if ( ! strcmp(optarg, "mpeg") )
820 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
822 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
824 case 'c': /* --dvdchannel */
825 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
827 case 's': /* --dvdsubtitle */
828 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
830 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
831 main_PutPszVariable( "DVDCSS_METHOD", optarg );
833 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
834 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
838 case OPT_INPUT: /* --input */
839 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
841 case OPT_CHANNELS: /* --channels */
842 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
844 case OPT_CHANNELSERVER: /* --channelserver */
845 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
850 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
853 main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
856 /* Decoder options */
858 main_PutPszVariable( ADEC_MPEG_VAR, optarg );
861 /* Internal error: unknown option */
866 intf_ErrMsg( "intf error: unknown option `%s'",
869 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
870 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
871 INTF_STDOUT_DEFAULT ) ) == 0 )
873 /* No stdout redirection has been asked for */
874 intf_MsgImm( "\nPress the RETURN key to continue..." );
883 if( p_main->i_warning_level < 0 )
885 p_main->i_warning_level = 0;
891 /*****************************************************************************
892 * GetFilenames: parse command line options which are not flags
893 *****************************************************************************
894 * Parse command line for input files.
895 *****************************************************************************/
896 static int GetFilenames( int i_argc, char *ppsz_argv[] )
900 /* We assume that the remaining parameters are filenames */
901 for( i_opt = optind; i_opt < i_argc; i_opt++ )
903 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
904 ppsz_argv[ i_opt ] );
910 /*****************************************************************************
911 * Usage: print program usage
912 *****************************************************************************
913 * Print a short inline help. Message interface is initialized at this stage.
914 *****************************************************************************/
915 static void Usage( int i_fashion )
918 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
921 if( i_fashion == USAGE )
923 intf_MsgImm( "Try `%s --help' for more information.",
929 intf_MsgImm( "\nOptions:"
930 "\n -I, --intf <module> \tinterface method"
931 "\n -v, --verbose \tverbose mode (cumulative)"
932 "\n --stdout <filename> \tredirect console stdout"
933 "\n --memcpy <module> \tmemcpy method"
935 "\n --noaudio \tdisable audio"
936 "\n -A, --aout <module> \taudio output method"
937 "\n --stereo, --mono \tstereo/mono audio"
938 "\n --spdif \tAC3 pass-through mode"
939 "\n --downmix <module> \tAC3 downmix method"
940 "\n --imdct <module> \tAC3 IMDCT method"
941 "\n --volume [0..1024] \tVLC output volume"
942 "\n --desync <time in ms> \tCompensate desynchronization of the audio"
944 "\n --novideo \tdisable video"
945 "\n -V, --vout <module> \tvideo output method"
946 "\n --display <display> \tdisplay string"
947 "\n --width <w>, --height <h> \tdisplay dimensions"
948 "\n -g, --grayscale \tgrayscale output"
949 "\n --fullscreen \tfullscreen output"
950 "\n --overlay \taccelerated display"
951 "\n --xvadaptor <adaptor> \tXVideo adaptor"
952 "\n --color \tcolor output"
953 "\n --motion <module> \tmotion compensation method"
954 "\n --idct <module> \tIDCT method"
955 "\n --yuv <module> \tYUV method"
956 "\n --synchro <type> \tforce synchro algorithm"
957 "\n --smp <number of threads> \tuse several processors"
959 "\n -t, --dvdtitle <num> \tchoose DVD title"
960 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
961 "\n -u, --dvdangle <num> \tchoose DVD angle"
962 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
963 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
964 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
965 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
966 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
968 "\n --input \tinput method"
969 "\n --channels \tenable channels"
970 "\n --channelserver <host> \tchannel server address"
972 "\n --mpeg_adec <builtin|mad> \tchoose audio decoder"
974 "\n -h, --help \tprint help and exit"
975 "\n -H, --longhelp \tprint long help and exit"
976 "\n --version \toutput version information and exit"
977 "\n\nPlaylist items :"
978 "\n *.mpg, *.vob \tPlain MPEG-1/2 files"
979 "\n dvd:<device>[@<raw device>] \tDVD device"
980 "\n vcd:<device> \tVCD device"
981 "\n udpstream:[<server>[:<server port>]][@[<bind address>][:<bind port>]]"
982 "\n \tUDP stream sent by VLS"
983 "\n vlc:loop \tLoop execution of the playlist"
984 "\n vlc:pause \tPause execution of playlist items"
985 "\n vlc:quit \tQuit VLC");
987 if( i_fashion == SHORT_HELP )
990 /* Interface parameters */
991 intf_MsgImm( "\nInterface parameters:"
992 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
993 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
994 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
995 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout"
996 "\n " MEMCPY_METHOD_VAR "=<method name> \tmemcpy method" );
998 /* Audio parameters */
999 intf_MsgImm( "\nAudio parameters:"
1000 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
1001 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
1002 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
1003 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
1004 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
1005 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
1006 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
1007 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
1009 /* Video parameters */
1010 intf_MsgImm( "\nVideo parameters:"
1011 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
1012 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
1013 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
1014 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
1015 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
1016 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
1017 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
1018 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
1019 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
1020 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
1021 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
1022 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
1023 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
1024 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
1026 /* DVD parameters */
1027 intf_MsgImm( "\nDVD parameters:"
1028 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
1029 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
1030 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
1031 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
1032 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
1033 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
1034 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
1036 /* Input parameters */
1037 intf_MsgImm( "\nInput parameters:"
1038 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
1039 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
1040 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
1042 /* Decoder parameters */
1043 intf_MsgImm( "\nDecoder parameters:"
1044 "\n " ADEC_MPEG_VAR "=<builtin|mad> \taudio decoder" );
1047 /*****************************************************************************
1048 * Version: print complete program version
1049 *****************************************************************************
1050 * Print complete program version and build number.
1051 *****************************************************************************/
1052 static void Version( void )
1054 intf_MsgImm( VERSION_MESSAGE
1055 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1056 "You may redistribute it under the terms of the GNU General Public License;\n"
1057 "see the file named COPYING for details.\n"
1058 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1061 /*****************************************************************************
1062 * InitSignalHandler: system signal handler initialization
1063 *****************************************************************************
1064 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1065 * at least a method to kill the program when all other methods failed, and
1066 * when we don't want to use SIGKILL.
1067 *****************************************************************************/
1068 static void InitSignalHandler( void )
1070 /* Termination signals */
1072 signal( SIGINT, FatalSignalHandler );
1073 signal( SIGHUP, FatalSignalHandler );
1074 signal( SIGQUIT, FatalSignalHandler );
1077 signal( SIGALRM, SimpleSignalHandler );
1078 signal( SIGPIPE, SimpleSignalHandler );
1083 /*****************************************************************************
1084 * SimpleSignalHandler: system signal handler
1085 *****************************************************************************
1086 * This function is called when a non fatal signal is received by the program.
1087 *****************************************************************************/
1088 static void SimpleSignalHandler( int i_signal )
1090 /* Acknowledge the signal received */
1091 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1095 /*****************************************************************************
1096 * FatalSignalHandler: system signal handler
1097 *****************************************************************************
1098 * This function is called when a fatal signal is received by the program.
1099 * It tries to end the program in a clean way.
1100 *****************************************************************************/
1101 static void FatalSignalHandler( int i_signal )
1103 /* Once a signal has been trapped, the termination sequence will be
1104 * armed and following signals will be ignored to avoid sending messages
1105 * to an interface having been destroyed */
1107 signal( SIGINT, SIG_IGN );
1108 signal( SIGHUP, SIG_IGN );
1109 signal( SIGQUIT, SIG_IGN );
1112 /* Acknowledge the signal received */
1113 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1115 /* Try to terminate everything - this is done by requesting the end of the
1116 * interface thread */
1117 p_main->p_intf->b_die = 1;
1120 /*****************************************************************************
1121 * InstructionSignalHandler: system signal handler
1122 *****************************************************************************
1123 * This function is called when a illegal instruction signal is received by
1125 * We use this function to test OS and CPU_Capabilities
1126 *****************************************************************************/
1127 static void InstructionSignalHandler( int i_signal )
1129 /* Once a signal has been trapped, the termination sequence will be
1130 * armed and following signals will be ignored to avoid sending messages
1131 * to an interface having been destroyed */
1133 /* Acknowledge the signal received */
1136 #ifdef HAVE_SIGRELSE
1137 sigrelse( i_signal );
1142 /*****************************************************************************
1143 * CPUCapabilities: list the processors MMX support and other capabilities
1144 *****************************************************************************
1145 * This function is called to list extensions the CPU may have.
1146 *****************************************************************************/
1147 static int CPUCapabilities( void )
1149 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1151 #if defined( SYS_BEOS )
1152 i_capabilities |= CPU_CAPABILITY_FPU
1153 | CPU_CAPABILITY_486
1154 | CPU_CAPABILITY_586
1155 | CPU_CAPABILITY_MMX;
1157 return( i_capabilities );
1159 #elif defined( SYS_DARWIN )
1160 struct host_basic_info hi;
1162 host_name_port_t host;
1165 char *psz_name, *psz_subname;
1167 i_capabilities |= CPU_CAPABILITY_FPU;
1169 /* Should 'never' fail? */
1170 host = mach_host_self();
1172 i_size = sizeof( hi ) / sizeof( int );
1173 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1175 if( ret != KERN_SUCCESS )
1177 fprintf( stderr, "error: couldn't get CPU information\n" );
1178 return( i_capabilities );
1181 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1182 /* FIXME: need better way to detect newer proccessors.
1183 * could do strncmp(a,b,5), but that's real ugly */
1184 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1186 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1189 return( i_capabilities );
1191 #elif defined( __i386__ )
1192 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1193 volatile boolean_t b_amd;
1195 i_capabilities |= CPU_CAPABILITY_FPU;
1197 signal( SIGILL, InstructionSignalHandler );
1199 /* test for a 486 CPU */
1200 asm volatile ( "pushfl\n\t"
1202 "movl %%eax, %%ebx\n\t"
1203 "xorl $0x200000, %%eax\n\t"
1213 if( i_eax == i_ebx )
1215 signal( SIGILL, NULL );
1216 return( i_capabilities );
1219 i_capabilities |= CPU_CAPABILITY_486;
1221 /* the CPU supports the CPUID instruction - get its level */
1222 cpuid( 0x00000000 );
1226 signal( SIGILL, NULL );
1227 return( i_capabilities );
1230 /* FIXME: this isn't correct, since some 486s have cpuid */
1231 i_capabilities |= CPU_CAPABILITY_586;
1233 /* borrowed from mpeg2dec */
1234 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1235 && ( i_edx == 0x69746e65 );
1237 /* test for the MMX flag */
1238 cpuid( 0x00000001 );
1240 if( ! (i_edx & 0x00800000) )
1242 signal( SIGILL, NULL );
1243 return( i_capabilities );
1246 i_capabilities |= CPU_CAPABILITY_MMX;
1248 if( i_edx & 0x02000000 )
1250 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1252 #ifdef CAN_COMPILE_SSE
1253 /* We test if OS support the SSE instructions */
1255 if( setjmp( env ) == 0 )
1257 /* Test a SSE instruction */
1258 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1261 if( i_illegal == 0 )
1263 i_capabilities |= CPU_CAPABILITY_SSE;
1267 fprintf( stderr, "warning: your OS doesn't have support for "
1268 "SSE instructions, "
1269 "some optimizations\nwill be disabled\n" );
1271 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1277 /* test for additional capabilities */
1278 cpuid( 0x80000000 );
1280 if( i_eax < 0x80000001 )
1282 signal( SIGILL, NULL );
1283 return( i_capabilities );
1286 /* list these additional capabilities */
1287 cpuid( 0x80000001 );
1289 #ifdef CAN_COMPILE_3DNOW
1290 if( i_edx & 0x80000000 )
1293 if( setjmp( env ) == 0 )
1295 /* Test a 3D Now! instruction */
1296 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1299 if( i_illegal == 0 )
1301 i_capabilities |= CPU_CAPABILITY_3DNOW;
1306 if( b_amd && ( i_edx & 0x00400000 ) )
1308 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1311 signal( SIGILL, NULL );
1312 return( i_capabilities );
1314 #elif defined( __powerpc__ )
1316 i_capabilities |= CPU_CAPABILITY_FPU;
1318 /* Test for Altivec */
1319 signal( SIGILL, InstructionSignalHandler );
1321 #ifdef CAN_COMPILE_ALTIVEC
1323 if( setjmp( env ) == 0 )
1325 asm volatile ("mtspr 256, %0\n\t"
1326 "vand %%v0, %%v0, %%v0"
1331 if( i_illegal == 0 )
1333 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1337 signal( SIGILL, NULL );
1338 return( i_capabilities );
1341 /* default behaviour */
1342 return( i_capabilities );
1347 /*****************************************************************************
1348 * RedirectSTDOUT: redirect stdout and stderr to a file
1349 *****************************************************************************
1350 * This function will redirect stdout and stderr to a file if the user has
1352 *****************************************************************************/
1353 static int RedirectSTDOUT( void )
1358 psz_filename = main_GetPszVariable( INTF_STDOUT_VAR, INTF_STDOUT_DEFAULT );
1363 i_fd = open( psz_filename, O_CREAT | O_TRUNC | O_RDWR,
1364 S_IREAD | S_IWRITE );
1365 if( dup2( i_fd, fileno(stdout) ) == -1 )
1367 intf_ErrMsg( "warning: unable to redirect stdout" );
1370 if( dup2( i_fd, fileno(stderr) ) == -1 )
1372 intf_ErrMsg( "warning: unable to redirect stderr" );
1379 /* No stdout redirection has been asked so open a console */
1380 if( p_main->i_warning_level )
1390 /*****************************************************************************
1391 * ShowConsole: On Win32, create an output console for debug messages
1392 *****************************************************************************
1393 * This function is usefull only on Win32.
1394 *****************************************************************************/
1395 static void ShowConsole( void )
1399 freopen( "CONOUT$", "w", stdout );
1400 freopen( "CONOUT$", "w", stderr );
1401 freopen( "CONIN$", "r", stdin );