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.128 2001/12/03 16:18: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 */
73 #include "tests.h" /* TestCPU() */
76 #include "stream_control.h"
77 #include "input_ext-intf.h"
79 #include "intf_playlist.h"
80 #include "interface.h"
82 #include "audio_output.h"
85 #include "video_output.h"
88 # include "beos_specific.h"
92 # include "darwin_specific.h"
96 # include "win32_specific.h"
99 #include "netutils.h" /* network_ChannelJoin */
103 /*****************************************************************************
104 * Command line options constants. If something is changed here, be sure that
105 * GetConfiguration and Usage are also changed.
106 *****************************************************************************/
108 /* Long options return values - note that values corresponding to short options
109 * chars, and in general any regular char, should be avoided */
110 #define OPT_NOAUDIO 150
111 #define OPT_STEREO 151
113 #define OPT_SPDIF 153
114 #define OPT_VOLUME 154
116 #define OPT_NOVIDEO 160
117 #define OPT_DISPLAY 161
118 #define OPT_WIDTH 162
119 #define OPT_HEIGHT 163
120 #define OPT_COLOR 164
121 #define OPT_FULLSCREEN 165
122 #define OPT_OVERLAY 166
123 #define OPT_XVADAPTOR 167
126 #define OPT_CHANNELS 170
127 #define OPT_SERVER 171
129 #define OPT_BROADCAST 173
130 #define OPT_CHANNELSERVER 174
132 #define OPT_INPUT 180
133 #define OPT_MOTION 181
136 #define OPT_DOWNMIX 184
137 #define OPT_IMDCT 185
138 #define OPT_MEMCPY 186
139 #define OPT_DVDCSS_METHOD 187
140 #define OPT_DVDCSS_VERBOSE 188
142 #define OPT_SYNCHRO 190
143 #define OPT_WARNING 191
144 #define OPT_VERSION 192
145 #define OPT_STDOUT 193
146 #define OPT_STATS 194
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 },
190 { "novideo", 0, 0, OPT_NOVIDEO },
191 { "vout", 1, 0, 'V' },
192 { "display", 1, 0, OPT_DISPLAY },
193 { "width", 1, 0, OPT_WIDTH },
194 { "height", 1, 0, OPT_HEIGHT },
195 { "grayscale", 0, 0, 'g' },
196 { "color", 0, 0, OPT_COLOR },
197 { "motion", 1, 0, OPT_MOTION },
198 { "idct", 1, 0, OPT_IDCT },
199 { "yuv", 1, 0, OPT_YUV },
200 { "fullscreen", 0, 0, OPT_FULLSCREEN },
201 { "overlay", 0, 0, OPT_OVERLAY },
202 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
203 { "smp", 1, 0, OPT_SMP },
206 { "dvdtitle", 1, 0, 't' },
207 { "dvdchapter", 1, 0, 'T' },
208 { "dvdangle", 1, 0, 'u' },
209 { "dvdaudio", 1, 0, 'a' },
210 { "dvdchannel", 1, 0, 'c' },
211 { "dvdsubtitle", 1, 0, 's' },
212 { "dvdcss-method", 1, 0, OPT_DVDCSS_METHOD },
213 { "dvdcss-verbose", 1, 0, OPT_DVDCSS_VERBOSE },
216 { "input", 1, 0, OPT_INPUT },
217 { "channels", 0, 0, OPT_CHANNELS },
218 { "channelserver", 1, 0, OPT_CHANNELSERVER },
221 { "synchro", 1, 0, OPT_SYNCHRO },
222 { "memcpy", 1, 0, OPT_MEMCPY },
227 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
229 /*****************************************************************************
230 * Global variables - these are the only ones, see main.h and modules.h
231 *****************************************************************************/
233 module_bank_t *p_module_bank;
234 aout_bank_t *p_aout_bank;
235 vout_bank_t *p_vout_bank;
236 void* ( *pf_fast_memcpy ) ( void *, const void *, size_t );
238 /*****************************************************************************
240 *****************************************************************************/
241 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
243 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
244 static void Usage ( int i_fashion );
245 static void Version ( void );
247 static void InitSignalHandler ( void );
248 static void SimpleSignalHandler ( int i_signal );
249 static void FatalSignalHandler ( int i_signal );
250 static void InstructionSignalHandler( int i_signal );
251 static int CPUCapabilities ( void );
253 static int RedirectSTDOUT ( void );
254 static void ShowConsole ( void );
257 static int i_illegal;
259 /*****************************************************************************
260 * main: parse command line, start interface and spawn threads
261 *****************************************************************************
262 * Steps during program execution are:
263 * -configuration parsing and messages interface initialization
264 * -opening of audio output device and some global modules
265 * -execution of interface, which exit on error or on user request
266 * -closing of audio output device and some global modules
267 * On error, the spawned threads are canceled, and the open devices closed.
268 *****************************************************************************/
269 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
271 main_t main_data; /* root of all data - see main.h */
272 module_bank_t module_bank;
273 aout_bank_t aout_bank;
274 vout_bank_t vout_bank;
276 p_main = &main_data; /* set up the global variables */
277 p_module_bank = &module_bank;
278 p_aout_bank = &aout_bank;
279 p_vout_bank = &vout_bank;
282 * Initialize threads system
287 * Test if our code is likely to run on this CPU
289 p_main->i_cpu_capabilities = CPUCapabilities();
292 * System specific initialization code
294 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
295 system_Init( &i_argc, ppsz_argv, ppsz_env );
297 #elif defined( SYS_LINUX )
299 /* Activate malloc checking routines to detect heap corruptions. */
300 main_PutIntVariable( "MALLOC_CHECK_", 2 );
305 * Initialize messages interface
307 p_main->p_msg = intf_MsgCreate();
308 if( !p_main->p_msg ) /* start messages interface */
310 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
315 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
320 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
327 * Redirect the standard output if required by the user, and on Win32 we
328 * also open a console to display the debug messages.
332 if( p_main->b_stats )
334 char p_capabilities[200];
335 p_capabilities[0] = '\0';
337 #define PRINT_CAPABILITY( capability, string ) \
338 if( p_main->i_cpu_capabilities & capability ) \
340 strncat( p_capabilities, string " ", \
341 sizeof(p_capabilities) - strlen(p_capabilities) ); \
342 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
345 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
346 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
347 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
348 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
349 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
350 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
351 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
352 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
353 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
357 * Initialize playlist and get commandline files
359 p_main->p_playlist = intf_PlaylistCreate();
360 if( !p_main->p_playlist )
362 intf_ErrMsg( "playlist error: playlist initialization failed" );
366 intf_PlaylistInit( p_main->p_playlist );
369 * Get input filenames given as commandline arguments
371 GetFilenames( i_argc, ppsz_argv );
374 * Initialize module, aout and vout banks
381 * Choose the best memcpy module
383 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );
385 if( p_main->p_memcpy_module == NULL )
387 intf_ErrMsg( "intf error: no suitable memcpy module, "
388 "using libc default" );
389 pf_fast_memcpy = memcpy;
393 #define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
394 pf_fast_memcpy = f.pf_fast_memcpy;
399 * Initialize shared resources and libraries
401 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
402 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
403 network_ChannelCreate() )
405 /* On error during Channels initialization, switch off channels */
406 intf_Msg( "Channels initialization failed : "
407 "Channel management is deactivated" );
408 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
412 * Try to run the interface
414 p_main->p_intf = intf_Create();
415 if( p_main->p_intf == NULL )
417 intf_ErrMsg( "intf error: interface initialization failed" );
422 * Set signal handling policy for all threads
427 * This is the main loop
429 p_main->p_intf->pf_run( p_main->p_intf );
432 * Finished, destroy the interface
434 intf_Destroy( p_main->p_intf );
437 * Go back into channel 0 which is the network
439 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
440 INPUT_NETWORK_CHANNEL_DEFAULT ) )
442 network_ChannelJoin( COMMON_CHANNEL );
449 module_Unneed( p_main->p_memcpy_module );
452 * Free module, aout and vout banks
461 intf_PlaylistDestroy( p_main->p_playlist );
464 * System specific cleaning code
466 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
472 * Terminate messages interface and program
474 intf_Msg( "intf: program terminated" );
478 * Stop threads system
485 /*****************************************************************************
486 * main_GetIntVariable: get the int value of an environment variable
487 *****************************************************************************
488 * This function is used to read some default parameters in modules.
489 *****************************************************************************/
490 int main_GetIntVariable( char *psz_name, int i_default )
492 char * psz_env; /* environment value */
493 char * psz_end; /* end of parsing index */
494 long int i_value; /* value */
496 psz_env = getenv( psz_name );
499 i_value = strtol( psz_env, &psz_end, 0 );
500 if( (*psz_env != '\0') && (*psz_end == '\0') )
508 /*****************************************************************************
509 * main_GetPszVariable: get the string value of an environment variable
510 *****************************************************************************
511 * This function is used to read some default parameters in modules.
512 *****************************************************************************/
513 char * main_GetPszVariable( char *psz_name, char *psz_default )
517 psz_env = getenv( psz_name );
522 return( psz_default );
525 /*****************************************************************************
526 * main_PutPszVariable: set the string value of an environment variable
527 *****************************************************************************
528 * This function is used to set some default parameters in modules. The use of
529 * this function will cause some memory leak: since some systems use the pointer
530 * passed to putenv to store the environment string, it can't be freed.
531 *****************************************************************************/
532 void main_PutPszVariable( char *psz_name, char *psz_value )
536 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
537 if( psz_env == NULL )
539 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
544 sprintf( psz_env, "%s=%s", psz_name, psz_value );
545 if( putenv( psz_env ) )
547 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
552 /*****************************************************************************
553 * main_PutIntVariable: set the integer value of an environment variable
554 *****************************************************************************
555 * This function is used to set some default parameters in modules. The use of
556 * this function will cause some memory leak: since some systems use the pointer
557 * passed to putenv to store the environment string, it can't be freed.
558 *****************************************************************************/
559 void main_PutIntVariable( char *psz_name, int i_value )
561 char psz_value[ 256 ]; /* buffer for value */
563 sprintf( psz_value, "%d", i_value );
564 main_PutPszVariable( psz_name, psz_value );
567 /* following functions are local */
569 /*****************************************************************************
570 * GetConfiguration: parse command line
571 *****************************************************************************
572 * Parse command line and configuration file for configuration. If the inline
573 * help is requested, the function Usage() is called and the function returns
574 * -1 (causing main() to exit). The messages interface is initialized at this
575 * stage, but most structures are not allocated, so only environment should
577 *****************************************************************************/
578 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
583 /* Set default configuration and copy arguments */
584 p_main->i_argc = *pi_argc;
585 p_main->ppsz_argv = ppsz_argv;
586 p_main->ppsz_env = ppsz_env;
591 p_main->i_warning_level = 0;
594 p_main->p_channel = NULL;
596 /* Get the executable name (similar to the basename command) */
597 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
602 p_main->psz_arg0 = ++p_tmp;
611 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
612 * is the PSN - process serial number (a unique PID-ish thingie)
613 * still ok for real Darwin & when run from command line */
614 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
615 /* for example -psn_0_9306113 */
617 /* GDMF!... I can't do this or else the MacOSX window server will
618 * not pick up the PSN and not register the app and we crash...
619 * hence the following kludge otherwise we'll get confused w/ argv[1]
620 * being an input file name */
622 ppsz_argv[ 1 ] = NULL;
624 *pi_argc = *pi_argc - 1;
630 /* Parse command line options */
632 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
633 psz_shortopts, longopts, 0 ) ) != EOF )
637 /* General/common options */
638 case 'h': /* -h, --help */
642 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
643 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
644 INTF_STDOUT_DEFAULT ) ) == 0 )
646 /* No stdout redirection has been asked for */
647 intf_MsgImm( "\nPress the RETURN key to continue..." );
653 case 'H': /* -H, --longhelp */
657 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
658 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
659 INTF_STDOUT_DEFAULT ) ) == 0 )
661 /* No stdout redirection has been asked for */
662 intf_MsgImm( "\nPress the RETURN key to continue..." );
668 case OPT_VERSION: /* --version */
672 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
673 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
674 INTF_STDOUT_DEFAULT ) ) == 0 )
676 /* No stdout redirection has been asked for */
677 intf_MsgImm( "\nPress the RETURN key to continue..." );
683 case 'v': /* -v, --verbose */
684 p_main->i_warning_level++;
687 /* Interface warning messages level */
688 case 'I': /* -I, --intf */
689 main_PutPszVariable( INTF_METHOD_VAR, optarg );
691 case OPT_WARNING: /* --warning */
692 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
693 p_main->i_warning_level = atoi(optarg);
696 case OPT_STDOUT: /* --stdout */
697 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
705 case OPT_NOAUDIO: /* --noaudio */
708 case 'A': /* -A, --aout */
709 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
711 case OPT_STEREO: /* --stereo */
712 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
714 case OPT_MONO: /* --mono */
715 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
717 case OPT_SPDIF: /* --spdif */
718 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
720 case OPT_DOWNMIX: /* --downmix */
721 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
723 case OPT_IMDCT: /* --imdct */
724 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
726 case OPT_VOLUME: /* --volume */
727 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
731 case OPT_NOVIDEO: /* --novideo */
734 case 'V': /* -V, --vout */
735 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
737 case OPT_DISPLAY: /* --display */
738 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
740 case OPT_WIDTH: /* --width */
741 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
743 case OPT_HEIGHT: /* --height */
744 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
746 case 'g': /* -g, --grayscale */
747 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
749 case OPT_COLOR: /* --color */
750 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
752 case OPT_FULLSCREEN: /* --fullscreen */
753 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
755 case OPT_OVERLAY: /* --overlay */
756 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
758 case OPT_XVADAPTOR: /* --xvadaptor */
759 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
761 case OPT_MOTION: /* --motion */
762 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
764 case OPT_IDCT: /* --idct */
765 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
767 case OPT_YUV: /* --yuv */
768 main_PutPszVariable( YUV_METHOD_VAR, optarg );
770 case OPT_SMP: /* --smp */
771 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
775 case 't': /* --dvdtitle */
776 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
778 case 'T': /* --dvdchapter */
779 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
781 case 'u': /* --dvdangle */
782 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
784 case 'a': /* --dvdaudio */
785 if ( ! strcmp(optarg, "ac3") )
786 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
787 else if ( ! strcmp(optarg, "lpcm") )
788 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
789 else if ( ! strcmp(optarg, "mpeg") )
790 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
792 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
794 case 'c': /* --dvdchannel */
795 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
797 case 's': /* --dvdsubtitle */
798 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
800 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
801 main_PutPszVariable( "DVDCSS_METHOD", optarg );
803 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
804 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
808 case OPT_INPUT: /* --input */
809 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
811 case OPT_CHANNELS: /* --channels */
812 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
814 case OPT_CHANNELSERVER: /* --channelserver */
815 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
820 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
823 main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
826 /* Internal error: unknown option */
831 intf_ErrMsg( "intf error: unknown option `%s'",
834 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
835 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
836 INTF_STDOUT_DEFAULT ) ) == 0 )
838 /* No stdout redirection has been asked for */
839 intf_MsgImm( "\nPress the RETURN key to continue..." );
848 if( p_main->i_warning_level < 0 )
850 p_main->i_warning_level = 0;
856 /*****************************************************************************
857 * GetFilenames: parse command line options which are not flags
858 *****************************************************************************
859 * Parse command line for input files.
860 *****************************************************************************/
861 static int GetFilenames( int i_argc, char *ppsz_argv[] )
865 /* We assume that the remaining parameters are filenames */
866 for( i_opt = optind; i_opt < i_argc; i_opt++ )
868 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
869 ppsz_argv[ i_opt ] );
875 /*****************************************************************************
876 * Usage: print program usage
877 *****************************************************************************
878 * Print a short inline help. Message interface is initialized at this stage.
879 *****************************************************************************/
880 static void Usage( int i_fashion )
883 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
886 if( i_fashion == USAGE )
888 intf_MsgImm( "Try `%s --help' for more information.",
894 intf_MsgImm( "\nOptions:"
895 "\n -I, --intf <module> \tinterface method"
896 "\n -v, --verbose \tverbose mode (cumulative)"
897 "\n --stdout <filename> \tredirect console stdout"
898 "\n --memcpy <module> \tmemcpy method"
900 "\n --noaudio \tdisable audio"
901 "\n -A, --aout <module> \taudio output method"
902 "\n --stereo, --mono \tstereo/mono audio"
903 "\n --spdif \tAC3 pass-through mode"
904 "\n --downmix <module> \tAC3 downmix method"
905 "\n --imdct <module> \tAC3 IMDCT method"
906 "\n --volume [0..1024] \tVLC output volume"
908 "\n --novideo \tdisable video"
909 "\n -V, --vout <module> \tvideo output method"
910 "\n --display <display> \tdisplay string"
911 "\n --width <w>, --height <h> \tdisplay dimensions"
912 "\n -g, --grayscale \tgrayscale output"
913 "\n --fullscreen \tfullscreen output"
914 "\n --overlay \taccelerated display"
915 "\n --xvadaptor <adaptor> \tXVideo adaptor"
916 "\n --color \tcolor output"
917 "\n --motion <module> \tmotion compensation method"
918 "\n --idct <module> \tIDCT method"
919 "\n --yuv <module> \tYUV method"
920 "\n --synchro <type> \tforce synchro algorithm"
921 "\n --smp <number of threads> \tuse several processors"
923 "\n -t, --dvdtitle <num> \tchoose DVD title"
924 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
925 "\n -u, --dvdangle <num> \tchoose DVD angle"
926 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
927 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
928 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
929 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
930 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
932 "\n --input \tinput method"
933 "\n --channels \tenable channels"
934 "\n --channelserver <host> \tchannel server address"
936 "\n -h, --help \tprint help and exit"
937 "\n -H, --longhelp \tprint long help and exit"
938 "\n --version \toutput version information and exit" );
940 if( i_fashion == SHORT_HELP )
943 /* Interface parameters */
944 intf_MsgImm( "\nInterface parameters:"
945 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
946 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
947 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
948 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout"
949 "\n " MEMCPY_METHOD_VAR "=<method name> \tmemcpy method" );
951 /* Audio parameters */
952 intf_MsgImm( "\nAudio parameters:"
953 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
954 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
955 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
956 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
957 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
958 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
959 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
960 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
962 /* Video parameters */
963 intf_MsgImm( "\nVideo parameters:"
964 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
965 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
966 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
967 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
968 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
969 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
970 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
971 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
972 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
973 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
974 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
975 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
976 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
977 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
980 intf_MsgImm( "\nDVD parameters:"
981 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
982 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
983 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
984 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
985 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
986 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
987 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
989 /* Input parameters */
990 intf_MsgImm( "\nInput parameters:"
991 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
992 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
993 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
997 /*****************************************************************************
998 * Version: print complete program version
999 *****************************************************************************
1000 * Print complete program version and build number.
1001 *****************************************************************************/
1002 static void Version( void )
1004 intf_MsgImm( VERSION_MESSAGE
1005 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1006 "You may redistribute it under the terms of the GNU General Public License;\n"
1007 "see the file named COPYING for details.\n"
1008 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1011 /*****************************************************************************
1012 * InitSignalHandler: system signal handler initialization
1013 *****************************************************************************
1014 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1015 * at least a method to kill the program when all other methods failed, and
1016 * when we don't want to use SIGKILL.
1017 *****************************************************************************/
1018 static void InitSignalHandler( void )
1020 /* Termination signals */
1022 signal( SIGINT, FatalSignalHandler );
1023 signal( SIGHUP, FatalSignalHandler );
1024 signal( SIGQUIT, FatalSignalHandler );
1027 signal( SIGALRM, SimpleSignalHandler );
1028 signal( SIGPIPE, SimpleSignalHandler );
1033 /*****************************************************************************
1034 * SimpleSignalHandler: system signal handler
1035 *****************************************************************************
1036 * This function is called when a non fatal signal is received by the program.
1037 *****************************************************************************/
1038 static void SimpleSignalHandler( int i_signal )
1040 /* Acknowledge the signal received */
1041 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1045 /*****************************************************************************
1046 * FatalSignalHandler: system signal handler
1047 *****************************************************************************
1048 * This function is called when a fatal signal is received by the program.
1049 * It tries to end the program in a clean way.
1050 *****************************************************************************/
1051 static void FatalSignalHandler( int i_signal )
1053 /* Once a signal has been trapped, the termination sequence will be
1054 * armed and following signals will be ignored to avoid sending messages
1055 * to an interface having been destroyed */
1057 signal( SIGINT, SIG_IGN );
1058 signal( SIGHUP, SIG_IGN );
1059 signal( SIGQUIT, SIG_IGN );
1062 /* Acknowledge the signal received */
1063 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1065 /* Try to terminate everything - this is done by requesting the end of the
1066 * interface thread */
1067 p_main->p_intf->b_die = 1;
1070 /*****************************************************************************
1071 * InstructionSignalHandler: system signal handler
1072 *****************************************************************************
1073 * This function is called when a illegal instruction signal is received by
1075 * We use this function to test OS and CPU_Capabilities
1076 *****************************************************************************/
1077 static void InstructionSignalHandler( int i_signal )
1079 /* Once a signal has been trapped, the termination sequence will be
1080 * armed and following signals will be ignored to avoid sending messages
1081 * to an interface having been destroyed */
1083 /* Acknowledge the signal received */
1086 #ifdef HAVE_SIGRELSE
1087 sigrelse( i_signal );
1092 /*****************************************************************************
1093 * CPUCapabilities: list the processors MMX support and other capabilities
1094 *****************************************************************************
1095 * This function is called to list extensions the CPU may have.
1096 *****************************************************************************/
1097 static int CPUCapabilities( void )
1099 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1101 #if defined( SYS_BEOS )
1102 i_capabilities |= CPU_CAPABILITY_486
1103 | CPU_CAPABILITY_586
1104 | CPU_CAPABILITY_MMX;
1106 return( i_capabilities );
1108 #elif defined( SYS_DARWIN )
1109 struct host_basic_info hi;
1111 host_name_port_t host;
1114 char *psz_name, *psz_subname;
1116 /* Should 'never' fail? */
1117 host = mach_host_self();
1119 i_size = sizeof( hi ) / sizeof( int );
1120 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1122 if( ret != KERN_SUCCESS )
1124 fprintf( stderr, "error: couldn't get CPU information\n" );
1125 return( i_capabilities );
1128 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1129 /* FIXME: need better way to detect newer proccessors.
1130 * could do strncmp(a,b,5), but that's real ugly */
1131 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1133 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1136 return( i_capabilities );
1138 #elif defined( __i386__ )
1139 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1140 volatile boolean_t b_amd;
1142 signal( SIGILL, InstructionSignalHandler );
1144 /* test for a 486 CPU */
1145 asm volatile ( "pushfl\n\t"
1147 "movl %%eax, %%ebx\n\t"
1148 "xorl $0x200000, %%eax\n\t"
1158 if( i_eax == i_ebx )
1160 signal( SIGILL, NULL );
1161 return( i_capabilities );
1164 i_capabilities |= CPU_CAPABILITY_486;
1166 /* the CPU supports the CPUID instruction - get its level */
1167 cpuid( 0x00000000 );
1171 signal( SIGILL, NULL );
1172 return( i_capabilities );
1175 /* FIXME: this isn't correct, since some 486s have cpuid */
1176 i_capabilities |= CPU_CAPABILITY_586;
1178 /* borrowed from mpeg2dec */
1179 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1180 && ( i_edx == 0x69746e65 );
1182 /* test for the MMX flag */
1183 cpuid( 0x00000001 );
1185 if( ! (i_edx & 0x00800000) )
1187 signal( SIGILL, NULL );
1188 return( i_capabilities );
1191 i_capabilities |= CPU_CAPABILITY_MMX;
1193 if( i_edx & 0x02000000 )
1195 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1197 #ifdef CAN_COMPILE_SSE
1198 /* We test if OS support the SSE instructions */
1200 if( setjmp( env ) == 0 )
1202 /* Test a SSE instruction */
1203 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1206 if( i_illegal == 0 )
1208 i_capabilities |= CPU_CAPABILITY_SSE;
1212 fprintf( stderr, "warning: your OS doesn't have support for "
1213 "SSE instructions, "
1214 "some optimizations\nwill be disabled\n" );
1216 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1222 /* test for additional capabilities */
1223 cpuid( 0x80000000 );
1225 if( i_eax < 0x80000001 )
1227 signal( SIGILL, NULL );
1228 return( i_capabilities );
1231 /* list these additional capabilities */
1232 cpuid( 0x80000001 );
1234 #ifdef CAN_COMPILE_3DNOW
1235 if( i_edx & 0x80000000 )
1238 if( setjmp( env ) == 0 )
1240 /* Test a 3D Now! instruction */
1241 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1244 if( i_illegal == 0 )
1246 i_capabilities |= CPU_CAPABILITY_3DNOW;
1251 if( b_amd && ( i_edx & 0x00400000 ) )
1253 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1256 signal( SIGILL, NULL );
1257 return( i_capabilities );
1259 #elif defined( __powerpc__ )
1261 /* Test for Altivec */
1262 signal( SIGILL, InstructionSignalHandler );
1264 #ifdef CAN_COMPILE_ALTIVEC
1266 if( setjmp( env ) == 0 )
1268 asm volatile ("mtspr 256,%0" : : "r" (-1));
1271 if( i_illegal == 0 )
1273 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1277 signal( SIGILL, NULL );
1278 return( i_capabilities );
1281 /* default behaviour */
1282 return( i_capabilities );
1287 /*****************************************************************************
1288 * RedirectSTDOUT: redirect stdout and stderr to a file
1289 *****************************************************************************
1290 * This function will redirect stdout and stderr to a file if the user has
1292 *****************************************************************************/
1293 static int RedirectSTDOUT( void )
1295 int i_stdout_filedesc;
1296 char *psz_stdout_filename;
1298 psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1299 INTF_STDOUT_DEFAULT );
1300 if( strcmp( "", psz_stdout_filename ) != 0 )
1303 i_stdout_filedesc = open( psz_stdout_filename,
1304 O_CREAT | O_TRUNC | O_RDWR,
1305 S_IREAD | S_IWRITE );
1307 if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1309 intf_ErrMsg( "warning: unable to redirect stdout" );
1312 if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1314 intf_ErrMsg( "warning: unable to redirect stderr" );
1317 close( i_stdout_filedesc );
1321 /* No stdout redirection has been asked so open a console */
1322 if( p_main->i_warning_level )
1332 /*****************************************************************************
1333 * ShowConsole: On Win32, create an output console for debug messages
1334 *****************************************************************************
1335 * This function is usefull only on Win32.
1336 *****************************************************************************/
1337 static void ShowConsole( void )
1341 freopen( "CONOUT$", "w", stdout );
1342 freopen( "CONOUT$", "w", stderr );
1343 freopen( "CONIN$", "r", stdin );