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.151 2002/02/15 13:32:54 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 *****************************************************************************/
30 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
31 #include <stdio.h> /* sprintf() */
32 #include <setjmp.h> /* longjmp, setjmp */
34 #include <videolan/vlc.h>
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 */
71 #include "netutils.h" /* network_ChannelJoin */
73 #include "stream_control.h"
74 #include "input_ext-intf.h"
76 #include "intf_playlist.h"
77 #include "interface.h"
79 #include "audio_output.h"
82 #include "video_output.h"
86 /*****************************************************************************
87 * Command line options constants. If something is changed here, be sure that
88 * GetConfiguration and Usage are also changed.
89 *****************************************************************************/
91 /* Long options return values - note that values corresponding to short options
92 * chars, and in general any regular char, should be avoided */
93 #define OPT_NOAUDIO 150
94 #define OPT_STEREO 151
97 #define OPT_VOLUME 154
98 #define OPT_DESYNC 155
100 #define OPT_NOVIDEO 160
101 #define OPT_DISPLAY 161
102 #define OPT_WIDTH 162
103 #define OPT_HEIGHT 163
104 #define OPT_COLOR 164
105 #define OPT_FULLSCREEN 165
106 #define OPT_NOOVERLAY 166
107 #define OPT_XVADAPTOR 167
109 #define OPT_SPUMARGIN 169
111 #define OPT_CHANNELS 170
112 #define OPT_SERVER 171
114 #define OPT_BROADCAST 173
115 #define OPT_CHANNELSERVER 174
117 #define OPT_INPUT 180
118 #define OPT_MOTION 181
121 #define OPT_DOWNMIX 184
122 #define OPT_IMDCT 185
123 #define OPT_MEMCPY 186
124 #define OPT_FILTER 187
125 #define OPT_DVDCSS_METHOD 188
126 #define OPT_DVDCSS_VERBOSE 189
128 #define OPT_SYNCHRO 190
129 #define OPT_WARNING 191
130 #define OPT_VERSION 192
131 #define OPT_STDOUT 193
132 #define OPT_STATS 194
134 #define OPT_MPEG_ADEC 200
135 #define OPT_AC3_ADEC 201
137 #define OPT_NOMMX 210
138 #define OPT_NO3DNOW 211
139 #define OPT_NOMMXEXT 212
140 #define OPT_NOSSE 213
141 #define OPT_NOALTIVEC 214
149 static const struct option longopts[] =
151 /* name, has_arg, flag, val */
153 /* General/common options */
154 { "help", 0, 0, 'h' },
155 { "longhelp", 0, 0, 'H' },
156 { "version", 0, 0, OPT_VERSION },
158 /* Interface options */
159 { "intf", 1, 0, 'I' },
160 { "warning", 1, 0, OPT_WARNING },
161 { "stdout", 1, 0, OPT_STDOUT },
162 { "stats", 0, 0, OPT_STATS },
165 { "noaudio", 0, 0, OPT_NOAUDIO },
166 { "aout", 1, 0, 'A' },
167 { "stereo", 0, 0, OPT_STEREO },
168 { "mono", 0, 0, OPT_MONO },
169 { "spdif", 0, 0, OPT_SPDIF },
170 { "downmix", 1, 0, OPT_DOWNMIX },
171 { "imdct", 1, 0, OPT_IMDCT },
172 { "volume", 1, 0, OPT_VOLUME },
173 { "desync", 1, 0, OPT_DESYNC },
176 { "novideo", 0, 0, OPT_NOVIDEO },
177 { "vout", 1, 0, 'V' },
178 { "display", 1, 0, OPT_DISPLAY },
179 { "width", 1, 0, OPT_WIDTH },
180 { "height", 1, 0, OPT_HEIGHT },
181 { "grayscale", 0, 0, 'g' },
182 { "color", 0, 0, OPT_COLOR },
183 { "motion", 1, 0, OPT_MOTION },
184 { "idct", 1, 0, OPT_IDCT },
185 { "yuv", 1, 0, OPT_YUV },
186 { "fullscreen", 0, 0, OPT_FULLSCREEN },
187 { "nooverlay", 0, 0, OPT_NOOVERLAY },
188 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
189 { "smp", 1, 0, OPT_SMP },
190 { "spumargin", 1, 0, OPT_SPUMARGIN },
193 { "dvdtitle", 1, 0, 't' },
194 { "dvdchapter", 1, 0, 'T' },
195 { "dvdangle", 1, 0, 'u' },
196 { "dvdaudio", 1, 0, 'a' },
197 { "dvdchannel", 1, 0, 'c' },
198 { "dvdsubtitle", 1, 0, 's' },
199 { "dvdcss-method", 1, 0, OPT_DVDCSS_METHOD },
200 { "dvdcss-verbose", 1, 0, OPT_DVDCSS_VERBOSE },
203 { "input", 1, 0, OPT_INPUT },
204 { "channels", 0, 0, OPT_CHANNELS },
205 { "channelserver", 1, 0, OPT_CHANNELSERVER },
208 { "synchro", 1, 0, OPT_SYNCHRO },
209 { "memcpy", 1, 0, OPT_MEMCPY },
210 { "filter", 1, 0, OPT_FILTER },
212 /* Decoder options */
213 { "mpeg_adec", 1, 0, OPT_MPEG_ADEC },
214 { "ac3_adec", 1, 0, OPT_AC3_ADEC },
217 { "nommx", 0, 0, OPT_NOMMX },
218 { "no3dnow", 0, 0, OPT_NO3DNOW },
219 { "nommxext", 0, 0, OPT_NOMMXEXT },
220 { "nosse", 0, 0, OPT_NOSSE },
221 { "noaltivec", 0, 0, OPT_NOALTIVEC },
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 input_bank_t *p_input_bank;
235 aout_bank_t *p_aout_bank;
236 vout_bank_t *p_vout_bank;
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 IllegalSignalHandler ( int i_signal );
251 static u32 CPUCapabilities ( void );
253 static int RedirectSTDOUT ( void );
254 static void ShowConsole ( void );
257 static int i_illegal;
258 static char *psz_capability;
260 /*****************************************************************************
261 * main: parse command line, start interface and spawn threads
262 *****************************************************************************
263 * Steps during program execution are:
264 * -configuration parsing and messages interface initialization
265 * -opening of audio output device and some global modules
266 * -execution of interface, which exit on error or on user request
267 * -closing of audio output device and some global modules
268 * On error, the spawned threads are canceled, and the open devices closed.
269 *****************************************************************************/
270 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
272 main_t main_data; /* root of all data - see main.h */
273 module_bank_t module_bank;
274 input_bank_t input_bank;
275 aout_bank_t aout_bank;
276 vout_bank_t vout_bank;
278 p_main = &main_data; /* set up the global variables */
279 p_module_bank = &module_bank;
280 p_input_bank = &input_bank;
281 p_aout_bank = &aout_bank;
282 p_vout_bank = &vout_bank;
284 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
288 #if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
289 if( !setlocale( LC_MESSAGES, "" ) )
291 fprintf( stderr, "warning: unsupported locale.\n" );
295 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
297 fprintf( stderr, "warning: no domain %s in directory %s\n",
298 PACKAGE, LOCALEDIR );
301 textdomain( PACKAGE );
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 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
377 intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
381 * Initialize playlist and get commandline files
383 p_main->p_playlist = intf_PlaylistCreate();
384 if( !p_main->p_playlist )
386 intf_ErrMsg( "playlist error: playlist initialization failed" );
390 intf_PlaylistInit( p_main->p_playlist );
393 * Get input filenames given as commandline arguments
395 GetFilenames( i_argc, ppsz_argv );
398 * Initialize module, input, aout and vout banks
406 * Choose the best memcpy module
408 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL,
410 if( p_main->p_memcpy_module == NULL )
412 intf_ErrMsg( "intf error: no suitable memcpy module, "
413 "using libc default" );
414 p_main->pf_memcpy = memcpy;
418 p_main->pf_memcpy = p_main->p_memcpy_module->p_functions
419 ->memcpy.functions.memcpy.pf_memcpy;
423 * Initialize shared resources and libraries
425 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
426 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
427 network_ChannelCreate() )
429 /* On error during Channels initialization, switch off channels */
430 intf_ErrMsg( "intf error: channels initialization failed, "
431 "deactivating channels" );
432 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
436 * Try to run the interface
438 p_main->p_intf = intf_Create();
439 if( p_main->p_intf == NULL )
441 intf_ErrMsg( "intf error: interface initialization failed" );
446 * Set signal handling policy for all threads
451 * This is the main loop
453 p_main->p_intf->pf_run( p_main->p_intf );
456 * Finished, destroy the interface
458 intf_Destroy( p_main->p_intf );
461 * Go back into channel 0 which is the network
463 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
464 INPUT_NETWORK_CHANNEL_DEFAULT ) )
466 network_ChannelJoin( COMMON_CHANNEL );
471 * Free input, aout and vout banks
480 intf_PlaylistDestroy( p_main->p_playlist );
483 * Free memcpy module if it was allocated
485 if( p_main->p_memcpy_module != NULL )
487 module_Unneed( p_main->p_memcpy_module );
496 * System specific cleaning code
498 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
504 * Terminate messages interface and program
506 intf_WarnMsg( 1, "intf: program terminated" );
510 * Stop threads system
517 /*****************************************************************************
518 * main_GetIntVariable: get the int value of an environment variable
519 *****************************************************************************
520 * This function is used to read some default parameters in modules.
521 *****************************************************************************/
522 int main_GetIntVariable( char *psz_name, int i_default )
524 char * psz_env; /* environment value */
525 char * psz_end; /* end of parsing index */
526 long int i_value; /* value */
528 psz_env = getenv( psz_name );
531 i_value = strtol( psz_env, &psz_end, 0 );
532 if( (*psz_env != '\0') && (*psz_end == '\0') )
540 /*****************************************************************************
541 * main_GetPszVariable: get the string value of an environment variable
542 *****************************************************************************
543 * This function is used to read some default parameters in modules.
544 *****************************************************************************/
545 char * main_GetPszVariable( char *psz_name, char *psz_default )
549 psz_env = getenv( psz_name );
554 return( psz_default );
557 /*****************************************************************************
558 * main_PutPszVariable: set the string value of an environment variable
559 *****************************************************************************
560 * This function is used to set some default parameters in modules. The use of
561 * this function will cause some memory leak: since some systems use the pointer
562 * passed to putenv to store the environment string, it can't be freed.
563 *****************************************************************************/
564 void main_PutPszVariable( char *psz_name, char *psz_value )
568 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
569 if( psz_env == NULL )
571 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
576 sprintf( psz_env, "%s=%s", psz_name, psz_value );
577 if( putenv( psz_env ) )
579 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
584 /*****************************************************************************
585 * main_PutIntVariable: set the integer value of an environment variable
586 *****************************************************************************
587 * This function is used to set some default parameters in modules. The use of
588 * this function will cause some memory leak: since some systems use the pointer
589 * passed to putenv to store the environment string, it can't be freed.
590 *****************************************************************************/
591 void main_PutIntVariable( char *psz_name, int i_value )
593 char psz_value[ 256 ]; /* buffer for value */
595 sprintf( psz_value, "%d", i_value );
596 main_PutPszVariable( psz_name, psz_value );
599 /* following functions are local */
601 /*****************************************************************************
602 * GetConfiguration: parse command line
603 *****************************************************************************
604 * Parse command line and configuration file for configuration. If the inline
605 * help is requested, the function Usage() is called and the function returns
606 * -1 (causing main() to exit). The messages interface is initialized at this
607 * stage, but most structures are not allocated, so only environment should
609 *****************************************************************************/
610 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
615 /* Set default configuration and copy arguments */
616 p_main->i_argc = *pi_argc;
617 p_main->ppsz_argv = ppsz_argv;
618 p_main->ppsz_env = ppsz_env;
623 p_main->i_warning_level = 0;
625 p_main->i_desync = 0; /* No desynchronization by default */
627 p_main->p_channel = NULL;
629 /* Get the executable name (similar to the basename command) */
630 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
635 p_main->psz_arg0 = ++p_tmp;
644 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
645 * is the PSN - process serial number (a unique PID-ish thingie)
646 * still ok for real Darwin & when run from command line */
647 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
648 /* for example -psn_0_9306113 */
650 /* GDMF!... I can't do this or else the MacOSX window server will
651 * not pick up the PSN and not register the app and we crash...
652 * hence the following kludge otherwise we'll get confused w/ argv[1]
653 * being an input file name */
655 ppsz_argv[ 1 ] = NULL;
657 *pi_argc = *pi_argc - 1;
663 /* Parse command line options */
665 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
666 psz_shortopts, longopts, 0 ) ) != EOF )
670 /* General/common options */
671 case 'h': /* -h, --help */
675 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
676 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
677 INTF_STDOUT_DEFAULT ) ) == 0 )
679 /* No stdout redirection has been asked for */
680 intf_MsgImm( "\nPress the RETURN key to continue..." );
686 case 'H': /* -H, --longhelp */
690 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
691 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
692 INTF_STDOUT_DEFAULT ) ) == 0 )
694 /* No stdout redirection has been asked for */
695 intf_MsgImm( "\nPress the RETURN key to continue..." );
701 case OPT_VERSION: /* --version */
705 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
706 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
707 INTF_STDOUT_DEFAULT ) ) == 0 )
709 /* No stdout redirection has been asked for */
710 intf_MsgImm( "\nPress the RETURN key to continue..." );
716 case 'v': /* -v, --verbose */
717 p_main->i_warning_level++;
720 /* Interface warning messages level */
721 case 'I': /* -I, --intf */
722 main_PutPszVariable( INTF_METHOD_VAR, optarg );
724 case OPT_WARNING: /* --warning */
725 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
726 p_main->i_warning_level = atoi(optarg);
729 case OPT_STDOUT: /* --stdout */
730 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
738 case OPT_NOAUDIO: /* --noaudio */
741 case 'A': /* -A, --aout */
742 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
744 case OPT_STEREO: /* --stereo */
745 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
747 case OPT_MONO: /* --mono */
748 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
750 case OPT_SPDIF: /* --spdif */
751 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
753 case OPT_DOWNMIX: /* --downmix */
754 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
756 case OPT_IMDCT: /* --imdct */
757 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
759 case OPT_VOLUME: /* --volume */
760 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
762 case OPT_DESYNC: /* --desync */
763 p_main->i_desync = (mtime_t)atoi(optarg) * (mtime_t)1000;
767 case OPT_NOVIDEO: /* --novideo */
770 case 'V': /* -V, --vout */
771 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
773 case OPT_DISPLAY: /* --display */
774 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
776 case OPT_WIDTH: /* --width */
777 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
779 case OPT_HEIGHT: /* --height */
780 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
782 case 'g': /* -g, --grayscale */
783 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
785 case OPT_COLOR: /* --color */
786 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
788 case OPT_FULLSCREEN: /* --fullscreen */
789 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
791 case OPT_NOOVERLAY: /* --nooverlay */
792 main_PutIntVariable( VOUT_NOOVERLAY_VAR, 1 );
794 case OPT_XVADAPTOR: /* --xvadaptor */
795 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
797 case OPT_MOTION: /* --motion */
798 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
800 case OPT_IDCT: /* --idct */
801 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
803 case OPT_YUV: /* --yuv */
804 main_PutPszVariable( YUV_METHOD_VAR, optarg );
806 case OPT_SMP: /* --smp */
807 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
809 case OPT_SPUMARGIN: /* --spumargin */
810 main_PutIntVariable( VOUT_SPUMARGIN_VAR, atoi(optarg) );
814 case 't': /* --dvdtitle */
815 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
817 case 'T': /* --dvdchapter */
818 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
820 case 'u': /* --dvdangle */
821 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
823 case 'a': /* --dvdaudio */
824 if ( ! strcmp(optarg, "ac3") )
825 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
826 else if ( ! strcmp(optarg, "lpcm") )
827 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
828 else if ( ! strcmp(optarg, "mpeg") )
829 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
831 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
833 case 'c': /* --dvdchannel */
834 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
836 case 's': /* --dvdsubtitle */
837 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
839 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
840 main_PutPszVariable( "DVDCSS_METHOD", optarg );
842 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
843 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
847 case OPT_INPUT: /* --input */
848 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
850 case OPT_CHANNELS: /* --channels */
851 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
853 case OPT_CHANNELSERVER: /* --channelserver */
854 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
859 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
862 main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
864 case OPT_FILTER: /* --filter */
865 main_PutPszVariable( VOUT_FILTER_VAR, optarg );
868 /* Decoder options */
870 main_PutPszVariable( ADEC_MPEG_VAR, optarg );
874 main_PutPszVariable( ADEC_AC3_VAR, optarg );
879 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
883 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
887 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
891 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
895 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
898 /* Internal error: unknown option */
903 intf_ErrMsg( "intf error: unknown option `%s'",
906 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
907 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
908 INTF_STDOUT_DEFAULT ) ) == 0 )
910 /* No stdout redirection has been asked for */
911 intf_MsgImm( "\nPress the RETURN key to continue..." );
920 if( p_main->i_warning_level < 0 )
922 p_main->i_warning_level = 0;
928 /*****************************************************************************
929 * GetFilenames: parse command line options which are not flags
930 *****************************************************************************
931 * Parse command line for input files.
932 *****************************************************************************/
933 static int GetFilenames( int i_argc, char *ppsz_argv[] )
937 /* We assume that the remaining parameters are filenames */
938 for( i_opt = optind; i_opt < i_argc; i_opt++ )
940 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
941 ppsz_argv[ i_opt ] );
947 /*****************************************************************************
948 * Usage: print program usage
949 *****************************************************************************
950 * Print a short inline help. Message interface is initialized at this stage.
951 *****************************************************************************/
952 static void Usage( int i_fashion )
955 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
958 if( i_fashion == USAGE )
960 intf_MsgImm( "Try `%s --help' for more information.",
966 intf_MsgImm( "\nOptions:"
967 "\n -I, --intf <module> \tinterface method"
968 "\n -v, --verbose \tverbose mode (cumulative)"
969 "\n --stdout <filename> \tredirect console stdout"
970 "\n --memcpy <module> \tmemcpy method"
972 "\n --noaudio \tdisable audio"
973 "\n -A, --aout <module> \taudio output method"
974 "\n --stereo, --mono \tstereo/mono audio"
975 "\n --spdif \tAC3 pass-through mode"
976 "\n --downmix <module> \tAC3 downmix method"
977 "\n --imdct <module> \tAC3 IMDCT method"
978 "\n --volume [0..1024] \tVLC output volume"
979 "\n --desync <time in ms> \tCompensate desynchronization of the audio"
981 "\n --novideo \tdisable video"
982 "\n -V, --vout <module> \tvideo output method"
983 "\n --display <display> \tdisplay string"
984 "\n --width <w>, --height <h> \tdisplay dimensions"
985 "\n -g, --grayscale \tgrayscale output"
986 "\n --fullscreen \tfullscreen output"
987 "\n --nooverlay \tdisable accelerated display"
988 "\n --xvadaptor <adaptor> \tXVideo adaptor"
989 "\n --color \tcolor output"
990 "\n --motion <module> \tmotion compensation method"
991 "\n --idct <module> \tIDCT method"
992 "\n --yuv <module> \tYUV method"
993 "\n --synchro <type> \tforce synchro algorithm"
994 "\n --smp <number of threads> \tuse several processors"
995 "\n --spumargin <m> \tforce SPU position"
996 "\n --filter <module> \tvideo filter module"
998 "\n -t, --dvdtitle <num> \tchoose DVD title"
999 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
1000 "\n -u, --dvdangle <num> \tchoose DVD angle"
1001 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
1002 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
1003 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
1004 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
1005 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
1007 "\n --input \tinput method"
1008 "\n --channels \tenable channels"
1009 "\n --channelserver <host> \tchannel server address"
1011 "\n --mpeg_adec <builtin|mad> \tchoose MPEG audio decoder"
1012 "\n --ac3_adec <builtin|a52> \tchoose AC3 audio decoder"
1014 "\n --nommx \tdisable CPU's MMX support"
1015 "\n --no3dnow \tdisable CPU's 3D Now! support"
1016 "\n --nommxext \tdisable CPU's MMX EXT support"
1017 "\n --nosse \tdisable CPU's SSE support"
1018 "\n --noaltivec \tdisable CPU's AltiVec support"
1020 "\n -h, --help \tprint help and exit"
1021 "\n -H, --longhelp \tprint long help and exit"
1022 "\n --version \toutput version information and exit"
1023 "\n\nPlaylist items:"
1024 "\n *.mpg, *.vob \tPlain MPEG-1/2 files"
1025 "\n dvd:<device>[@<raw device>] \tDVD device"
1026 "\n vcd:<device> \tVCD device"
1027 "\n udpstream:[<server>[:<server port>]][@[<bind address>][:<bind port>]]"
1028 "\n \tUDP stream sent by VLS"
1029 "\n vlc:loop \tLoop execution of the playlist"
1030 "\n vlc:pause \tPause execution of playlist items"
1031 "\n vlc:quit \tQuit VLC");
1033 if( i_fashion == SHORT_HELP )
1036 /* Interface parameters */
1037 intf_MsgImm( "\nInterface parameters:"
1038 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
1039 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
1040 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
1041 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout"
1042 "\n " MEMCPY_METHOD_VAR "=<method name> \tmemcpy method" );
1044 /* Audio parameters */
1045 intf_MsgImm( "\nAudio parameters:"
1046 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
1047 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
1048 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
1049 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
1050 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
1051 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
1052 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
1053 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
1055 /* Video parameters */
1056 intf_MsgImm( "\nVideo parameters:"
1057 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
1058 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
1059 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
1060 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
1061 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
1062 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
1063 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
1064 "\n " VOUT_NOOVERLAY_VAR "={1|0} \tnooverlay"
1065 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
1066 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
1067 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
1068 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
1069 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
1070 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors"
1071 "\n " VOUT_FILTER_VAR "=<method name> \tvideo filter method"
1072 "\n " VOUT_SPUMARGIN_VAR "=<margin> \tforce SPU margin" );
1074 /* DVD parameters */
1075 intf_MsgImm( "\nDVD parameters:"
1076 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
1077 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
1078 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
1079 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
1080 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
1081 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
1082 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
1084 /* Input parameters */
1085 intf_MsgImm( "\nInput parameters:"
1086 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
1087 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
1088 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
1090 /* Decoder parameters */
1091 intf_MsgImm( "\nDecoder parameters:"
1092 "\n " ADEC_MPEG_VAR "=<builtin|mad> \tMPEG audio decoder"
1093 "\n " ADEC_AC3_VAR "=<builtin|a52> \tAC3 audio decoder" );
1096 /*****************************************************************************
1097 * Version: print complete program version
1098 *****************************************************************************
1099 * Print complete program version and build number.
1100 *****************************************************************************/
1101 static void Version( void )
1103 intf_MsgImm( VERSION_MESSAGE
1104 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1105 "You may redistribute it under the terms of the GNU General Public License;\n"
1106 "see the file named COPYING for details.\n"
1107 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1110 /*****************************************************************************
1111 * InitSignalHandler: system signal handler initialization
1112 *****************************************************************************
1113 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1114 * at least a method to kill the program when all other methods failed, and
1115 * when we don't want to use SIGKILL.
1116 *****************************************************************************/
1117 static void InitSignalHandler( void )
1119 /* Termination signals */
1121 signal( SIGINT, FatalSignalHandler );
1122 signal( SIGHUP, FatalSignalHandler );
1123 signal( SIGQUIT, FatalSignalHandler );
1126 signal( SIGALRM, SimpleSignalHandler );
1127 signal( SIGPIPE, SimpleSignalHandler );
1131 /*****************************************************************************
1132 * SimpleSignalHandler: system signal handler
1133 *****************************************************************************
1134 * This function is called when a non fatal signal is received by the program.
1135 *****************************************************************************/
1136 static void SimpleSignalHandler( int i_signal )
1138 /* Acknowledge the signal received */
1139 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1142 /*****************************************************************************
1143 * FatalSignalHandler: system signal handler
1144 *****************************************************************************
1145 * This function is called when a fatal signal is received by the program.
1146 * It tries to end the program in a clean way.
1147 *****************************************************************************/
1148 static void FatalSignalHandler( int i_signal )
1150 /* Once a signal has been trapped, the termination sequence will be
1151 * armed and following signals will be ignored to avoid sending messages
1152 * to an interface having been destroyed */
1154 signal( SIGINT, SIG_IGN );
1155 signal( SIGHUP, SIG_IGN );
1156 signal( SIGQUIT, SIG_IGN );
1159 /* Acknowledge the signal received */
1160 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1162 /* Try to terminate everything - this is done by requesting the end of the
1163 * interface thread */
1164 p_main->p_intf->b_die = 1;
1167 /*****************************************************************************
1168 * IllegalSignalHandler: system signal handler
1169 *****************************************************************************
1170 * This function is called when a illegal instruction signal is received by
1171 * the program. We use this function to test OS and CPU capabilities
1172 *****************************************************************************/
1173 static void IllegalSignalHandler( int i_signal )
1175 /* Acknowledge the signal received */
1178 #ifdef HAVE_SIGRELSE
1179 sigrelse( i_signal );
1182 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
1183 "operating system.\n", psz_capability );
1184 fprintf( stderr, " some optimizations will be disabled unless "
1185 "you upgrade your OS\n" );
1187 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)" );
1193 /*****************************************************************************
1194 * CPUCapabilities: list the processors MMX support and other capabilities
1195 *****************************************************************************
1196 * This function is called to list extensions the CPU may have.
1197 *****************************************************************************/
1198 static u32 CPUCapabilities( void )
1200 volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
1202 #if defined( SYS_DARWIN )
1203 struct host_basic_info hi;
1205 host_name_port_t host;
1208 char *psz_name, *psz_subname;
1210 i_capabilities |= CPU_CAPABILITY_FPU;
1212 /* Should 'never' fail? */
1213 host = mach_host_self();
1215 i_size = sizeof( hi ) / sizeof( int );
1216 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1218 if( ret != KERN_SUCCESS )
1220 fprintf( stderr, "error: couldn't get CPU information\n" );
1221 return( i_capabilities );
1224 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1225 /* FIXME: need better way to detect newer proccessors.
1226 * could do strncmp(a,b,5), but that's real ugly */
1227 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1229 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1232 return( i_capabilities );
1234 #elif defined( __i386__ )
1235 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1236 volatile boolean_t b_amd;
1238 /* Needed for x86 CPU capabilities detection */
1239 # define cpuid( a ) \
1240 asm volatile ( "pushl %%ebx\n\t" \
1242 "movl %%ebx,%1\n\t" \
1251 i_capabilities |= CPU_CAPABILITY_FPU;
1253 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1254 signal( SIGILL, IllegalSignalHandler );
1257 /* test for a 486 CPU */
1258 asm volatile ( "pushl %%ebx\n\t"
1261 "movl %%eax, %%ebx\n\t"
1262 "xorl $0x200000, %%eax\n\t"
1274 if( i_eax == i_ebx )
1276 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1277 signal( SIGILL, NULL );
1279 return( i_capabilities );
1282 i_capabilities |= CPU_CAPABILITY_486;
1284 /* the CPU supports the CPUID instruction - get its level */
1285 cpuid( 0x00000000 );
1289 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1290 signal( SIGILL, NULL );
1292 return( i_capabilities );
1295 /* FIXME: this isn't correct, since some 486s have cpuid */
1296 i_capabilities |= CPU_CAPABILITY_586;
1298 /* borrowed from mpeg2dec */
1299 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1300 && ( i_edx == 0x69746e65 );
1302 /* test for the MMX flag */
1303 cpuid( 0x00000001 );
1305 if( ! (i_edx & 0x00800000) )
1307 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1308 signal( SIGILL, NULL );
1310 return( i_capabilities );
1313 i_capabilities |= CPU_CAPABILITY_MMX;
1315 if( i_edx & 0x02000000 )
1317 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1319 # ifdef CAN_COMPILE_SSE
1320 /* We test if OS support the SSE instructions */
1321 psz_capability = "SSE";
1323 if( setjmp( env ) == 0 )
1325 /* Test a SSE instruction */
1326 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1329 if( i_illegal == 0 )
1331 i_capabilities |= CPU_CAPABILITY_SSE;
1336 /* test for additional capabilities */
1337 cpuid( 0x80000000 );
1339 if( i_eax < 0x80000001 )
1341 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1342 signal( SIGILL, NULL );
1344 return( i_capabilities );
1347 /* list these additional capabilities */
1348 cpuid( 0x80000001 );
1350 # ifdef CAN_COMPILE_3DNOW
1351 if( i_edx & 0x80000000 )
1353 psz_capability = "3D Now!";
1355 if( setjmp( env ) == 0 )
1357 /* Test a 3D Now! instruction */
1358 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1361 if( i_illegal == 0 )
1363 i_capabilities |= CPU_CAPABILITY_3DNOW;
1368 if( b_amd && ( i_edx & 0x00400000 ) )
1370 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1373 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1374 signal( SIGILL, NULL );
1376 return( i_capabilities );
1378 #elif defined( __powerpc__ )
1380 i_capabilities |= CPU_CAPABILITY_FPU;
1382 # ifdef CAN_COMPILE_ALTIVEC
1383 signal( SIGILL, IllegalSignalHandler );
1385 psz_capability = "AltiVec";
1387 if( setjmp( env ) == 0 )
1389 asm volatile ("mtspr 256, %0\n\t"
1390 "vand %%v0, %%v0, %%v0"
1395 if( i_illegal == 0 )
1397 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1400 signal( SIGILL, NULL );
1403 return( i_capabilities );
1406 /* default behaviour */
1407 return( i_capabilities );
1412 /*****************************************************************************
1413 * RedirectSTDOUT: redirect stdout and stderr to a file
1414 *****************************************************************************
1415 * This function will redirect stdout and stderr to a file if the user has
1417 *****************************************************************************/
1418 static int RedirectSTDOUT( void )
1423 psz_filename = main_GetPszVariable( INTF_STDOUT_VAR, INTF_STDOUT_DEFAULT );
1428 i_fd = open( psz_filename, O_CREAT | O_TRUNC | O_RDWR,
1429 S_IREAD | S_IWRITE );
1430 if( dup2( i_fd, fileno(stdout) ) == -1 )
1432 intf_ErrMsg( "warning: unable to redirect stdout" );
1435 if( dup2( i_fd, fileno(stderr) ) == -1 )
1437 intf_ErrMsg( "warning: unable to redirect stderr" );
1444 /* No stdout redirection has been asked so open a console */
1445 if( p_main->i_warning_level )
1455 /*****************************************************************************
1456 * ShowConsole: On Win32, create an output console for debug messages
1457 *****************************************************************************
1458 * This function is usefull only on Win32.
1459 *****************************************************************************/
1460 static void ShowConsole( void )
1464 freopen( "CONOUT$", "w", stdout );
1465 freopen( "CONOUT$", "w", stderr );
1466 freopen( "CONIN$", "r", stdin );