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.138 2001/12/16 16:18:36 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 )
65 #include <errno.h> /* ENOMEM */
66 #include <stdlib.h> /* getenv(), strtol(), */
67 #include <string.h> /* strerror() */
68 #include <fcntl.h> /* open(), O_WRONLY */
69 #include <sys/stat.h> /* S_IREAD */
76 #include "tests.h" /* TestCPU() */
79 #include "stream_control.h"
80 #include "input_ext-intf.h"
82 #include "intf_playlist.h"
83 #include "interface.h"
85 #include "audio_output.h"
88 #include "video_output.h"
91 # include "beos_specific.h"
95 # include "darwin_specific.h"
99 # include "win32_specific.h"
102 #include "netutils.h" /* network_ChannelJoin */
104 /*****************************************************************************
105 * Command line options constants. If something is changed here, be sure that
106 * GetConfiguration and Usage are also changed.
107 *****************************************************************************/
109 /* Long options return values - note that values corresponding to short options
110 * chars, and in general any regular char, should be avoided */
111 #define OPT_NOAUDIO 150
112 #define OPT_STEREO 151
114 #define OPT_SPDIF 153
115 #define OPT_VOLUME 154
116 #define OPT_DESYNC 155
118 #define OPT_NOVIDEO 160
119 #define OPT_DISPLAY 161
120 #define OPT_WIDTH 162
121 #define OPT_HEIGHT 163
122 #define OPT_COLOR 164
123 #define OPT_FULLSCREEN 165
124 #define OPT_OVERLAY 166
125 #define OPT_XVADAPTOR 167
127 #define OPT_FILTER 169
129 #define OPT_CHANNELS 170
130 #define OPT_SERVER 171
132 #define OPT_BROADCAST 173
133 #define OPT_CHANNELSERVER 174
135 #define OPT_INPUT 180
136 #define OPT_MOTION 181
139 #define OPT_DOWNMIX 184
140 #define OPT_IMDCT 185
141 #define OPT_MEMCPY 186
142 #define OPT_DVDCSS_METHOD 187
143 #define OPT_DVDCSS_VERBOSE 188
145 #define OPT_SYNCHRO 190
146 #define OPT_WARNING 191
147 #define OPT_VERSION 192
148 #define OPT_STDOUT 193
149 #define OPT_STATS 194
151 #define OPT_MPEG_ADEC 200
158 /* Needed for x86 CPU capabilities detection */
160 asm volatile ( "cpuid" \
169 static const struct option longopts[] =
171 /* name, has_arg, flag, val */
173 /* General/common options */
174 { "help", 0, 0, 'h' },
175 { "longhelp", 0, 0, 'H' },
176 { "version", 0, 0, OPT_VERSION },
178 /* Interface options */
179 { "intf", 1, 0, 'I' },
180 { "warning", 1, 0, OPT_WARNING },
181 { "stdout", 1, 0, OPT_STDOUT },
182 { "stats", 0, 0, OPT_STATS },
185 { "noaudio", 0, 0, OPT_NOAUDIO },
186 { "aout", 1, 0, 'A' },
187 { "stereo", 0, 0, OPT_STEREO },
188 { "mono", 0, 0, OPT_MONO },
189 { "spdif", 0, 0, OPT_SPDIF },
190 { "downmix", 1, 0, OPT_DOWNMIX },
191 { "imdct", 1, 0, OPT_IMDCT },
192 { "volume", 1, 0, OPT_VOLUME },
193 { "desync", 1, 0, OPT_DESYNC },
196 { "novideo", 0, 0, OPT_NOVIDEO },
197 { "vout", 1, 0, 'V' },
198 { "display", 1, 0, OPT_DISPLAY },
199 { "width", 1, 0, OPT_WIDTH },
200 { "height", 1, 0, OPT_HEIGHT },
201 { "grayscale", 0, 0, 'g' },
202 { "color", 0, 0, OPT_COLOR },
203 { "motion", 1, 0, OPT_MOTION },
204 { "idct", 1, 0, OPT_IDCT },
205 { "yuv", 1, 0, OPT_YUV },
206 { "fullscreen", 0, 0, OPT_FULLSCREEN },
207 { "overlay", 0, 0, OPT_OVERLAY },
208 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
209 { "smp", 1, 0, OPT_SMP },
210 { "filter", 1, 0, OPT_FILTER },
213 { "dvdtitle", 1, 0, 't' },
214 { "dvdchapter", 1, 0, 'T' },
215 { "dvdangle", 1, 0, 'u' },
216 { "dvdaudio", 1, 0, 'a' },
217 { "dvdchannel", 1, 0, 'c' },
218 { "dvdsubtitle", 1, 0, 's' },
219 { "dvdcss-method", 1, 0, OPT_DVDCSS_METHOD },
220 { "dvdcss-verbose", 1, 0, OPT_DVDCSS_VERBOSE },
223 { "input", 1, 0, OPT_INPUT },
224 { "channels", 0, 0, OPT_CHANNELS },
225 { "channelserver", 1, 0, OPT_CHANNELSERVER },
228 { "synchro", 1, 0, OPT_SYNCHRO },
229 { "memcpy", 1, 0, OPT_MEMCPY },
231 /* Decoder options */
232 { "mpeg_adec", 1, 0, OPT_MPEG_ADEC },
238 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
240 /*****************************************************************************
241 * Global variables - these are the only ones, see main.h and modules.h
242 *****************************************************************************/
244 module_bank_t *p_module_bank;
245 aout_bank_t *p_aout_bank;
246 vout_bank_t *p_vout_bank;
248 /*****************************************************************************
250 *****************************************************************************/
251 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
253 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
254 static void Usage ( int i_fashion );
255 static void Version ( void );
257 static void InitSignalHandler ( void );
258 static void SimpleSignalHandler ( int i_signal );
259 static void FatalSignalHandler ( int i_signal );
260 static void InstructionSignalHandler( int i_signal );
261 static int CPUCapabilities ( void );
263 static int RedirectSTDOUT ( void );
264 static void ShowConsole ( void );
267 static int i_illegal;
269 /*****************************************************************************
270 * main: parse command line, start interface and spawn threads
271 *****************************************************************************
272 * Steps during program execution are:
273 * -configuration parsing and messages interface initialization
274 * -opening of audio output device and some global modules
275 * -execution of interface, which exit on error or on user request
276 * -closing of audio output device and some global modules
277 * On error, the spawned threads are canceled, and the open devices closed.
278 *****************************************************************************/
279 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
281 main_t main_data; /* root of all data - see main.h */
282 module_bank_t module_bank;
283 aout_bank_t aout_bank;
284 vout_bank_t vout_bank;
286 p_main = &main_data; /* set up the global variables */
287 p_module_bank = &module_bank;
288 p_aout_bank = &aout_bank;
289 p_vout_bank = &vout_bank;
295 #if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
296 if( !setlocale( LC_MESSAGES, "" ) )
298 fprintf( stderr, "warning: unsupported locale.\n" );
302 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
304 fprintf( stderr, "warning: no domain %s in directory %s\n",
305 PACKAGE, LOCALEDIR );
308 textdomain( PACKAGE );
312 * Initialize threads system
317 * Test if our code is likely to run on this CPU
319 p_main->i_cpu_capabilities = CPUCapabilities();
322 * System specific initialization code
324 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
325 system_Init( &i_argc, ppsz_argv, ppsz_env );
327 #elif defined( SYS_LINUX )
329 /* Activate malloc checking routines to detect heap corruptions. */
330 main_PutIntVariable( "MALLOC_CHECK_", 2 );
335 * Initialize messages interface
337 p_main->p_msg = intf_MsgCreate();
338 if( !p_main->p_msg ) /* start messages interface */
340 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
345 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
350 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
357 * Redirect the standard output if required by the user, and on Win32 we
358 * also open a console to display the debug messages.
362 if( p_main->b_stats )
364 char p_capabilities[200];
365 p_capabilities[0] = '\0';
367 #define PRINT_CAPABILITY( capability, string ) \
368 if( p_main->i_cpu_capabilities & capability ) \
370 strncat( p_capabilities, string " ", \
371 sizeof(p_capabilities) - strlen(p_capabilities) ); \
372 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
375 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
376 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
377 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
378 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
379 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
380 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
381 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
382 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
383 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
387 * Initialize playlist and get commandline files
389 p_main->p_playlist = intf_PlaylistCreate();
390 if( !p_main->p_playlist )
392 intf_ErrMsg( "playlist error: playlist initialization failed" );
396 intf_PlaylistInit( p_main->p_playlist );
399 * Get input filenames given as commandline arguments
401 GetFilenames( i_argc, ppsz_argv );
404 * Initialize module, aout and vout banks
411 * Choose the best memcpy module
413 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );
415 if( p_main->p_memcpy_module != NULL )
417 #define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
418 p_main->fast_memcpy = f.fast_memcpy;
423 intf_ErrMsg( "intf error: no suitable memcpy module, "
424 "using libc default" );
425 p_main->fast_memcpy = memcpy;
429 * Initialize shared resources and libraries
431 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
432 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
433 network_ChannelCreate() )
435 /* On error during Channels initialization, switch off channels */
436 intf_Msg( "Channels initialization failed : "
437 "Channel management is deactivated" );
438 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
442 * Try to run the interface
444 p_main->p_intf = intf_Create();
445 if( p_main->p_intf == NULL )
447 intf_ErrMsg( "intf error: interface initialization failed" );
452 * Set signal handling policy for all threads
457 * This is the main loop
459 p_main->p_intf->pf_run( p_main->p_intf );
462 * Finished, destroy the interface
464 intf_Destroy( p_main->p_intf );
467 * Go back into channel 0 which is the network
469 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
470 INPUT_NETWORK_CHANNEL_DEFAULT ) )
472 network_ChannelJoin( COMMON_CHANNEL );
479 if( p_main->p_memcpy_module != NULL )
481 module_Unneed( p_main->p_memcpy_module );
485 * Free module, aout and vout banks
494 intf_PlaylistDestroy( p_main->p_playlist );
497 * System specific cleaning code
499 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
505 * Terminate messages interface and program
507 intf_Msg( "intf: program terminated" );
511 * Stop threads system
518 /*****************************************************************************
519 * main_GetIntVariable: get the int value of an environment variable
520 *****************************************************************************
521 * This function is used to read some default parameters in modules.
522 *****************************************************************************/
523 int main_GetIntVariable( char *psz_name, int i_default )
525 char * psz_env; /* environment value */
526 char * psz_end; /* end of parsing index */
527 long int i_value; /* value */
529 psz_env = getenv( psz_name );
532 i_value = strtol( psz_env, &psz_end, 0 );
533 if( (*psz_env != '\0') && (*psz_end == '\0') )
541 /*****************************************************************************
542 * main_GetPszVariable: get the string value of an environment variable
543 *****************************************************************************
544 * This function is used to read some default parameters in modules.
545 *****************************************************************************/
546 char * main_GetPszVariable( char *psz_name, char *psz_default )
550 psz_env = getenv( psz_name );
555 return( psz_default );
558 /*****************************************************************************
559 * main_PutPszVariable: set the string value of an environment variable
560 *****************************************************************************
561 * This function is used to set some default parameters in modules. The use of
562 * this function will cause some memory leak: since some systems use the pointer
563 * passed to putenv to store the environment string, it can't be freed.
564 *****************************************************************************/
565 void main_PutPszVariable( char *psz_name, char *psz_value )
569 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
570 if( psz_env == NULL )
572 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
577 sprintf( psz_env, "%s=%s", psz_name, psz_value );
578 if( putenv( psz_env ) )
580 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
585 /*****************************************************************************
586 * main_PutIntVariable: set the integer value of an environment variable
587 *****************************************************************************
588 * This function is used to set some default parameters in modules. The use of
589 * this function will cause some memory leak: since some systems use the pointer
590 * passed to putenv to store the environment string, it can't be freed.
591 *****************************************************************************/
592 void main_PutIntVariable( char *psz_name, int i_value )
594 char psz_value[ 256 ]; /* buffer for value */
596 sprintf( psz_value, "%d", i_value );
597 main_PutPszVariable( psz_name, psz_value );
600 /* following functions are local */
602 /*****************************************************************************
603 * GetConfiguration: parse command line
604 *****************************************************************************
605 * Parse command line and configuration file for configuration. If the inline
606 * help is requested, the function Usage() is called and the function returns
607 * -1 (causing main() to exit). The messages interface is initialized at this
608 * stage, but most structures are not allocated, so only environment should
610 *****************************************************************************/
611 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
616 /* Set default configuration and copy arguments */
617 p_main->i_argc = *pi_argc;
618 p_main->ppsz_argv = ppsz_argv;
619 p_main->ppsz_env = ppsz_env;
624 p_main->i_warning_level = 0;
626 p_main->i_desync = 0; /* No desynchronization by default */
628 p_main->p_channel = NULL;
630 /* Get the executable name (similar to the basename command) */
631 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
636 p_main->psz_arg0 = ++p_tmp;
645 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
646 * is the PSN - process serial number (a unique PID-ish thingie)
647 * still ok for real Darwin & when run from command line */
648 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
649 /* for example -psn_0_9306113 */
651 /* GDMF!... I can't do this or else the MacOSX window server will
652 * not pick up the PSN and not register the app and we crash...
653 * hence the following kludge otherwise we'll get confused w/ argv[1]
654 * being an input file name */
656 ppsz_argv[ 1 ] = NULL;
658 *pi_argc = *pi_argc - 1;
664 /* Parse command line options */
666 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
667 psz_shortopts, longopts, 0 ) ) != EOF )
671 /* General/common options */
672 case 'h': /* -h, --help */
676 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
677 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
678 INTF_STDOUT_DEFAULT ) ) == 0 )
680 /* No stdout redirection has been asked for */
681 intf_MsgImm( "\nPress the RETURN key to continue..." );
687 case 'H': /* -H, --longhelp */
691 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
692 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
693 INTF_STDOUT_DEFAULT ) ) == 0 )
695 /* No stdout redirection has been asked for */
696 intf_MsgImm( "\nPress the RETURN key to continue..." );
702 case OPT_VERSION: /* --version */
706 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
707 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
708 INTF_STDOUT_DEFAULT ) ) == 0 )
710 /* No stdout redirection has been asked for */
711 intf_MsgImm( "\nPress the RETURN key to continue..." );
717 case 'v': /* -v, --verbose */
718 p_main->i_warning_level++;
721 /* Interface warning messages level */
722 case 'I': /* -I, --intf */
723 main_PutPszVariable( INTF_METHOD_VAR, optarg );
725 case OPT_WARNING: /* --warning */
726 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
727 p_main->i_warning_level = atoi(optarg);
730 case OPT_STDOUT: /* --stdout */
731 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
739 case OPT_NOAUDIO: /* --noaudio */
742 case 'A': /* -A, --aout */
743 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
745 case OPT_STEREO: /* --stereo */
746 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
748 case OPT_MONO: /* --mono */
749 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
751 case OPT_SPDIF: /* --spdif */
752 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
754 case OPT_DOWNMIX: /* --downmix */
755 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
757 case OPT_IMDCT: /* --imdct */
758 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
760 case OPT_VOLUME: /* --volume */
761 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
763 case OPT_DESYNC: /* --desync */
764 p_main->i_desync = atoi(optarg);
768 case OPT_NOVIDEO: /* --novideo */
771 case 'V': /* -V, --vout */
772 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
774 case OPT_DISPLAY: /* --display */
775 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
777 case OPT_WIDTH: /* --width */
778 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
780 case OPT_HEIGHT: /* --height */
781 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
783 case 'g': /* -g, --grayscale */
784 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
786 case OPT_COLOR: /* --color */
787 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
789 case OPT_FULLSCREEN: /* --fullscreen */
790 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
792 case OPT_OVERLAY: /* --overlay */
793 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
795 case OPT_XVADAPTOR: /* --xvadaptor */
796 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
798 case OPT_MOTION: /* --motion */
799 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
801 case OPT_IDCT: /* --idct */
802 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
804 case OPT_YUV: /* --yuv */
805 main_PutPszVariable( YUV_METHOD_VAR, optarg );
807 case OPT_SMP: /* --smp */
808 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
810 case OPT_FILTER: /* --filter */
811 main_PutPszVariable( VOUT_FILTER_VAR, optarg );
815 case 't': /* --dvdtitle */
816 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
818 case 'T': /* --dvdchapter */
819 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
821 case 'u': /* --dvdangle */
822 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
824 case 'a': /* --dvdaudio */
825 if ( ! strcmp(optarg, "ac3") )
826 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
827 else if ( ! strcmp(optarg, "lpcm") )
828 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
829 else if ( ! strcmp(optarg, "mpeg") )
830 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
832 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
834 case 'c': /* --dvdchannel */
835 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
837 case 's': /* --dvdsubtitle */
838 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
840 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
841 main_PutPszVariable( "DVDCSS_METHOD", optarg );
843 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
844 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
848 case OPT_INPUT: /* --input */
849 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
851 case OPT_CHANNELS: /* --channels */
852 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
854 case OPT_CHANNELSERVER: /* --channelserver */
855 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
860 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
863 main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
866 /* Decoder options */
868 main_PutPszVariable( ADEC_MPEG_VAR, optarg );
871 /* Internal error: unknown option */
876 intf_ErrMsg( "intf error: unknown option `%s'",
879 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
880 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
881 INTF_STDOUT_DEFAULT ) ) == 0 )
883 /* No stdout redirection has been asked for */
884 intf_MsgImm( "\nPress the RETURN key to continue..." );
893 if( p_main->i_warning_level < 0 )
895 p_main->i_warning_level = 0;
901 /*****************************************************************************
902 * GetFilenames: parse command line options which are not flags
903 *****************************************************************************
904 * Parse command line for input files.
905 *****************************************************************************/
906 static int GetFilenames( int i_argc, char *ppsz_argv[] )
910 /* We assume that the remaining parameters are filenames */
911 for( i_opt = optind; i_opt < i_argc; i_opt++ )
913 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
914 ppsz_argv[ i_opt ] );
920 /*****************************************************************************
921 * Usage: print program usage
922 *****************************************************************************
923 * Print a short inline help. Message interface is initialized at this stage.
924 *****************************************************************************/
925 static void Usage( int i_fashion )
928 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
931 if( i_fashion == USAGE )
933 intf_MsgImm( "Try `%s --help' for more information.",
939 intf_MsgImm( "\nOptions:"
940 "\n -I, --intf <module> \tinterface method"
941 "\n -v, --verbose \tverbose mode (cumulative)"
942 "\n --stdout <filename> \tredirect console stdout"
943 "\n --memcpy <module> \tmemcpy method"
945 "\n --noaudio \tdisable audio"
946 "\n -A, --aout <module> \taudio output method"
947 "\n --stereo, --mono \tstereo/mono audio"
948 "\n --spdif \tAC3 pass-through mode"
949 "\n --downmix <module> \tAC3 downmix method"
950 "\n --imdct <module> \tAC3 IMDCT method"
951 "\n --volume [0..1024] \tVLC output volume"
952 "\n --desync <time in ms> \tCompensate desynchronization of the audio"
954 "\n --novideo \tdisable video"
955 "\n -V, --vout <module> \tvideo output method"
956 "\n --display <display> \tdisplay string"
957 "\n --width <w>, --height <h> \tdisplay dimensions"
958 "\n -g, --grayscale \tgrayscale output"
959 "\n --fullscreen \tfullscreen output"
960 "\n --overlay \taccelerated display"
961 "\n --xvadaptor <adaptor> \tXVideo adaptor"
962 "\n --color \tcolor output"
963 "\n --motion <module> \tmotion compensation method"
964 "\n --idct <module> \tIDCT method"
965 "\n --yuv <module> \tYUV method"
966 "\n --synchro <type> \tforce synchro algorithm"
967 "\n --smp <number of threads> \tuse several processors"
968 "\n --filter <module> \tvideo filter module"
970 "\n -t, --dvdtitle <num> \tchoose DVD title"
971 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
972 "\n -u, --dvdangle <num> \tchoose DVD angle"
973 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
974 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
975 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
976 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
977 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
979 "\n --input \tinput method"
980 "\n --channels \tenable channels"
981 "\n --channelserver <host> \tchannel server address"
983 "\n --mpeg_adec <builtin|mad> \tchoose audio decoder"
985 "\n -h, --help \tprint help and exit"
986 "\n -H, --longhelp \tprint long help and exit"
987 "\n --version \toutput version information and exit"
988 "\n\nPlaylist items :"
989 "\n *.mpg, *.vob \tPlain MPEG-1/2 files"
990 "\n dvd:<device>[@<raw device>] \tDVD device"
991 "\n vcd:<device> \tVCD device"
992 "\n udpstream:[<server>[:<server port>]][@[<bind address>][:<bind port>]]"
993 "\n \tUDP stream sent by VLS"
994 "\n vlc:loop \tLoop execution of the playlist"
995 "\n vlc:pause \tPause execution of playlist items"
996 "\n vlc:quit \tQuit VLC");
998 if( i_fashion == SHORT_HELP )
1001 /* Interface parameters */
1002 intf_MsgImm( "\nInterface parameters:"
1003 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
1004 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
1005 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
1006 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout"
1007 "\n " MEMCPY_METHOD_VAR "=<method name> \tmemcpy method" );
1009 /* Audio parameters */
1010 intf_MsgImm( "\nAudio parameters:"
1011 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
1012 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
1013 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
1014 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
1015 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
1016 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
1017 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
1018 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
1020 /* Video parameters */
1021 intf_MsgImm( "\nVideo parameters:"
1022 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
1023 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
1024 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
1025 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
1026 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
1027 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
1028 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
1029 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
1030 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
1031 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
1032 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
1033 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
1034 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
1035 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors"
1036 "\n " VOUT_FILTER_VAR "=<method name> \tvideo filter method" );
1038 /* DVD parameters */
1039 intf_MsgImm( "\nDVD parameters:"
1040 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
1041 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
1042 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
1043 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
1044 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
1045 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
1046 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
1048 /* Input parameters */
1049 intf_MsgImm( "\nInput parameters:"
1050 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
1051 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
1052 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
1054 /* Decoder parameters */
1055 intf_MsgImm( "\nDecoder parameters:"
1056 "\n " ADEC_MPEG_VAR "=<builtin|mad> \taudio decoder" );
1059 /*****************************************************************************
1060 * Version: print complete program version
1061 *****************************************************************************
1062 * Print complete program version and build number.
1063 *****************************************************************************/
1064 static void Version( void )
1066 intf_MsgImm( VERSION_MESSAGE
1067 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1068 "You may redistribute it under the terms of the GNU General Public License;\n"
1069 "see the file named COPYING for details.\n"
1070 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1073 /*****************************************************************************
1074 * InitSignalHandler: system signal handler initialization
1075 *****************************************************************************
1076 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1077 * at least a method to kill the program when all other methods failed, and
1078 * when we don't want to use SIGKILL.
1079 *****************************************************************************/
1080 static void InitSignalHandler( void )
1082 /* Termination signals */
1084 signal( SIGINT, FatalSignalHandler );
1085 signal( SIGHUP, FatalSignalHandler );
1086 signal( SIGQUIT, FatalSignalHandler );
1089 signal( SIGALRM, SimpleSignalHandler );
1090 signal( SIGPIPE, SimpleSignalHandler );
1095 /*****************************************************************************
1096 * SimpleSignalHandler: system signal handler
1097 *****************************************************************************
1098 * This function is called when a non fatal signal is received by the program.
1099 *****************************************************************************/
1100 static void SimpleSignalHandler( int i_signal )
1102 /* Acknowledge the signal received */
1103 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1107 /*****************************************************************************
1108 * FatalSignalHandler: system signal handler
1109 *****************************************************************************
1110 * This function is called when a fatal signal is received by the program.
1111 * It tries to end the program in a clean way.
1112 *****************************************************************************/
1113 static void FatalSignalHandler( int i_signal )
1115 /* Once a signal has been trapped, the termination sequence will be
1116 * armed and following signals will be ignored to avoid sending messages
1117 * to an interface having been destroyed */
1119 signal( SIGINT, SIG_IGN );
1120 signal( SIGHUP, SIG_IGN );
1121 signal( SIGQUIT, SIG_IGN );
1124 /* Acknowledge the signal received */
1125 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1127 /* Try to terminate everything - this is done by requesting the end of the
1128 * interface thread */
1129 p_main->p_intf->b_die = 1;
1132 /*****************************************************************************
1133 * InstructionSignalHandler: system signal handler
1134 *****************************************************************************
1135 * This function is called when a illegal instruction signal is received by
1137 * We use this function to test OS and CPU_Capabilities
1138 *****************************************************************************/
1139 static void InstructionSignalHandler( int i_signal )
1141 /* Once a signal has been trapped, the termination sequence will be
1142 * armed and following signals will be ignored to avoid sending messages
1143 * to an interface having been destroyed */
1145 /* Acknowledge the signal received */
1148 #ifdef HAVE_SIGRELSE
1149 sigrelse( i_signal );
1154 /*****************************************************************************
1155 * CPUCapabilities: list the processors MMX support and other capabilities
1156 *****************************************************************************
1157 * This function is called to list extensions the CPU may have.
1158 *****************************************************************************/
1159 static int CPUCapabilities( void )
1161 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1163 #if defined( SYS_BEOS )
1164 i_capabilities |= CPU_CAPABILITY_FPU
1165 | CPU_CAPABILITY_486
1166 | CPU_CAPABILITY_586
1167 | CPU_CAPABILITY_MMX;
1169 return( i_capabilities );
1171 #elif defined( SYS_DARWIN )
1172 struct host_basic_info hi;
1174 host_name_port_t host;
1177 char *psz_name, *psz_subname;
1179 i_capabilities |= CPU_CAPABILITY_FPU;
1181 /* Should 'never' fail? */
1182 host = mach_host_self();
1184 i_size = sizeof( hi ) / sizeof( int );
1185 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1187 if( ret != KERN_SUCCESS )
1189 fprintf( stderr, "error: couldn't get CPU information\n" );
1190 return( i_capabilities );
1193 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1194 /* FIXME: need better way to detect newer proccessors.
1195 * could do strncmp(a,b,5), but that's real ugly */
1196 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1198 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1201 return( i_capabilities );
1203 #elif defined( __i386__ )
1204 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1205 volatile boolean_t b_amd;
1207 i_capabilities |= CPU_CAPABILITY_FPU;
1209 signal( SIGILL, InstructionSignalHandler );
1211 /* test for a 486 CPU */
1212 asm volatile ( "pushfl\n\t"
1214 "movl %%eax, %%ebx\n\t"
1215 "xorl $0x200000, %%eax\n\t"
1225 if( i_eax == i_ebx )
1227 signal( SIGILL, NULL );
1228 return( i_capabilities );
1231 i_capabilities |= CPU_CAPABILITY_486;
1233 /* the CPU supports the CPUID instruction - get its level */
1234 cpuid( 0x00000000 );
1238 signal( SIGILL, NULL );
1239 return( i_capabilities );
1242 /* FIXME: this isn't correct, since some 486s have cpuid */
1243 i_capabilities |= CPU_CAPABILITY_586;
1245 /* borrowed from mpeg2dec */
1246 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1247 && ( i_edx == 0x69746e65 );
1249 /* test for the MMX flag */
1250 cpuid( 0x00000001 );
1252 if( ! (i_edx & 0x00800000) )
1254 signal( SIGILL, NULL );
1255 return( i_capabilities );
1258 i_capabilities |= CPU_CAPABILITY_MMX;
1260 if( i_edx & 0x02000000 )
1262 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1264 #ifdef CAN_COMPILE_SSE
1265 /* We test if OS support the SSE instructions */
1267 if( setjmp( env ) == 0 )
1269 /* Test a SSE instruction */
1270 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1273 if( i_illegal == 0 )
1275 i_capabilities |= CPU_CAPABILITY_SSE;
1279 fprintf( stderr, "warning: your OS doesn't have support for "
1280 "SSE instructions, "
1281 "some optimizations\nwill be disabled\n" );
1283 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1289 /* test for additional capabilities */
1290 cpuid( 0x80000000 );
1292 if( i_eax < 0x80000001 )
1294 signal( SIGILL, NULL );
1295 return( i_capabilities );
1298 /* list these additional capabilities */
1299 cpuid( 0x80000001 );
1301 #ifdef CAN_COMPILE_3DNOW
1302 if( i_edx & 0x80000000 )
1305 if( setjmp( env ) == 0 )
1307 /* Test a 3D Now! instruction */
1308 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1311 if( i_illegal == 0 )
1313 i_capabilities |= CPU_CAPABILITY_3DNOW;
1318 if( b_amd && ( i_edx & 0x00400000 ) )
1320 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1323 signal( SIGILL, NULL );
1324 return( i_capabilities );
1326 #elif defined( __powerpc__ )
1328 i_capabilities |= CPU_CAPABILITY_FPU;
1330 /* Test for Altivec */
1331 signal( SIGILL, InstructionSignalHandler );
1333 #ifdef CAN_COMPILE_ALTIVEC
1335 if( setjmp( env ) == 0 )
1337 asm volatile ("mtspr 256, %0\n\t"
1338 "vand %%v0, %%v0, %%v0"
1343 if( i_illegal == 0 )
1345 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1349 signal( SIGILL, NULL );
1350 return( i_capabilities );
1353 /* default behaviour */
1354 return( i_capabilities );
1359 /*****************************************************************************
1360 * RedirectSTDOUT: redirect stdout and stderr to a file
1361 *****************************************************************************
1362 * This function will redirect stdout and stderr to a file if the user has
1364 *****************************************************************************/
1365 static int RedirectSTDOUT( void )
1370 psz_filename = main_GetPszVariable( INTF_STDOUT_VAR, INTF_STDOUT_DEFAULT );
1375 i_fd = open( psz_filename, O_CREAT | O_TRUNC | O_RDWR,
1376 S_IREAD | S_IWRITE );
1377 if( dup2( i_fd, fileno(stdout) ) == -1 )
1379 intf_ErrMsg( "warning: unable to redirect stdout" );
1382 if( dup2( i_fd, fileno(stderr) ) == -1 )
1384 intf_ErrMsg( "warning: unable to redirect stderr" );
1391 /* No stdout redirection has been asked so open a console */
1392 if( p_main->i_warning_level )
1402 /*****************************************************************************
1403 * ShowConsole: On Win32, create an output console for debug messages
1404 *****************************************************************************
1405 * This function is usefull only on Win32.
1406 *****************************************************************************/
1407 static void ShowConsole( void )
1411 freopen( "CONOUT$", "w", stdout );
1412 freopen( "CONOUT$", "w", stderr );
1413 freopen( "CONIN$", "r", stdin );