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.129 2001/12/04 13:47:46 massiot 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
148 #define OPT_MPEG_ADEC 200
155 /* Needed for x86 CPU capabilities detection */
157 asm volatile ( "cpuid" \
166 static const struct option longopts[] =
168 /* name, has_arg, flag, val */
170 /* General/common options */
171 { "help", 0, 0, 'h' },
172 { "longhelp", 0, 0, 'H' },
173 { "version", 0, 0, OPT_VERSION },
175 /* Interface options */
176 { "intf", 1, 0, 'I' },
177 { "warning", 1, 0, OPT_WARNING },
178 { "stdout", 1, 0, OPT_STDOUT },
179 { "stats", 0, 0, OPT_STATS },
182 { "noaudio", 0, 0, OPT_NOAUDIO },
183 { "aout", 1, 0, 'A' },
184 { "stereo", 0, 0, OPT_STEREO },
185 { "mono", 0, 0, OPT_MONO },
186 { "spdif", 0, 0, OPT_SPDIF },
187 { "downmix", 1, 0, OPT_DOWNMIX },
188 { "imdct", 1, 0, OPT_IMDCT },
189 { "volume", 1, 0, OPT_VOLUME },
192 { "novideo", 0, 0, OPT_NOVIDEO },
193 { "vout", 1, 0, 'V' },
194 { "display", 1, 0, OPT_DISPLAY },
195 { "width", 1, 0, OPT_WIDTH },
196 { "height", 1, 0, OPT_HEIGHT },
197 { "grayscale", 0, 0, 'g' },
198 { "color", 0, 0, OPT_COLOR },
199 { "motion", 1, 0, OPT_MOTION },
200 { "idct", 1, 0, OPT_IDCT },
201 { "yuv", 1, 0, OPT_YUV },
202 { "fullscreen", 0, 0, OPT_FULLSCREEN },
203 { "overlay", 0, 0, OPT_OVERLAY },
204 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
205 { "smp", 1, 0, OPT_SMP },
208 { "dvdtitle", 1, 0, 't' },
209 { "dvdchapter", 1, 0, 'T' },
210 { "dvdangle", 1, 0, 'u' },
211 { "dvdaudio", 1, 0, 'a' },
212 { "dvdchannel", 1, 0, 'c' },
213 { "dvdsubtitle", 1, 0, 's' },
214 { "dvdcss-method", 1, 0, OPT_DVDCSS_METHOD },
215 { "dvdcss-verbose", 1, 0, OPT_DVDCSS_VERBOSE },
218 { "input", 1, 0, OPT_INPUT },
219 { "channels", 0, 0, OPT_CHANNELS },
220 { "channelserver", 1, 0, OPT_CHANNELSERVER },
223 { "synchro", 1, 0, OPT_SYNCHRO },
224 { "memcpy", 1, 0, OPT_MEMCPY },
226 /* Decoder options */
227 { "mpeg_adec", 1, 0, OPT_MPEG_ADEC },
233 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
235 /*****************************************************************************
236 * Global variables - these are the only ones, see main.h and modules.h
237 *****************************************************************************/
239 module_bank_t *p_module_bank;
240 aout_bank_t *p_aout_bank;
241 vout_bank_t *p_vout_bank;
242 void* ( *pf_fast_memcpy ) ( void *, const void *, size_t );
244 /*****************************************************************************
246 *****************************************************************************/
247 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
249 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
250 static void Usage ( int i_fashion );
251 static void Version ( void );
253 static void InitSignalHandler ( void );
254 static void SimpleSignalHandler ( int i_signal );
255 static void FatalSignalHandler ( int i_signal );
256 static void InstructionSignalHandler( int i_signal );
257 static int CPUCapabilities ( void );
259 static int RedirectSTDOUT ( void );
260 static void ShowConsole ( void );
263 static int i_illegal;
265 /*****************************************************************************
266 * main: parse command line, start interface and spawn threads
267 *****************************************************************************
268 * Steps during program execution are:
269 * -configuration parsing and messages interface initialization
270 * -opening of audio output device and some global modules
271 * -execution of interface, which exit on error or on user request
272 * -closing of audio output device and some global modules
273 * On error, the spawned threads are canceled, and the open devices closed.
274 *****************************************************************************/
275 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
277 main_t main_data; /* root of all data - see main.h */
278 module_bank_t module_bank;
279 aout_bank_t aout_bank;
280 vout_bank_t vout_bank;
282 p_main = &main_data; /* set up the global variables */
283 p_module_bank = &module_bank;
284 p_aout_bank = &aout_bank;
285 p_vout_bank = &vout_bank;
288 * Initialize threads system
293 * Test if our code is likely to run on this CPU
295 p_main->i_cpu_capabilities = CPUCapabilities();
298 * System specific initialization code
300 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
301 system_Init( &i_argc, ppsz_argv, ppsz_env );
303 #elif defined( SYS_LINUX )
305 /* Activate malloc checking routines to detect heap corruptions. */
306 main_PutIntVariable( "MALLOC_CHECK_", 2 );
311 * Initialize messages interface
313 p_main->p_msg = intf_MsgCreate();
314 if( !p_main->p_msg ) /* start messages interface */
316 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
321 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
326 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
333 * Redirect the standard output if required by the user, and on Win32 we
334 * also open a console to display the debug messages.
338 if( p_main->b_stats )
340 char p_capabilities[200];
341 p_capabilities[0] = '\0';
343 #define PRINT_CAPABILITY( capability, string ) \
344 if( p_main->i_cpu_capabilities & capability ) \
346 strncat( p_capabilities, string " ", \
347 sizeof(p_capabilities) - strlen(p_capabilities) ); \
348 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
351 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
352 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
353 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
354 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
355 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
356 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
357 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
358 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
359 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
363 * Initialize playlist and get commandline files
365 p_main->p_playlist = intf_PlaylistCreate();
366 if( !p_main->p_playlist )
368 intf_ErrMsg( "playlist error: playlist initialization failed" );
372 intf_PlaylistInit( p_main->p_playlist );
375 * Get input filenames given as commandline arguments
377 GetFilenames( i_argc, ppsz_argv );
380 * Initialize module, aout and vout banks
387 * Choose the best memcpy module
389 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL );
391 if( p_main->p_memcpy_module == NULL )
393 intf_ErrMsg( "intf error: no suitable memcpy module, "
394 "using libc default" );
395 pf_fast_memcpy = memcpy;
399 #define f p_main->p_memcpy_module->p_functions->memcpy.functions.memcpy
400 pf_fast_memcpy = f.pf_fast_memcpy;
405 * Initialize shared resources and libraries
407 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
408 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
409 network_ChannelCreate() )
411 /* On error during Channels initialization, switch off channels */
412 intf_Msg( "Channels initialization failed : "
413 "Channel management is deactivated" );
414 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
418 * Try to run the interface
420 p_main->p_intf = intf_Create();
421 if( p_main->p_intf == NULL )
423 intf_ErrMsg( "intf error: interface initialization failed" );
428 * Set signal handling policy for all threads
433 * This is the main loop
435 p_main->p_intf->pf_run( p_main->p_intf );
438 * Finished, destroy the interface
440 intf_Destroy( p_main->p_intf );
443 * Go back into channel 0 which is the network
445 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
446 INPUT_NETWORK_CHANNEL_DEFAULT ) )
448 network_ChannelJoin( COMMON_CHANNEL );
455 module_Unneed( p_main->p_memcpy_module );
458 * Free module, aout and vout banks
467 intf_PlaylistDestroy( p_main->p_playlist );
470 * System specific cleaning code
472 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
478 * Terminate messages interface and program
480 intf_Msg( "intf: program terminated" );
484 * Stop threads system
491 /*****************************************************************************
492 * main_GetIntVariable: get the int value of an environment variable
493 *****************************************************************************
494 * This function is used to read some default parameters in modules.
495 *****************************************************************************/
496 int main_GetIntVariable( char *psz_name, int i_default )
498 char * psz_env; /* environment value */
499 char * psz_end; /* end of parsing index */
500 long int i_value; /* value */
502 psz_env = getenv( psz_name );
505 i_value = strtol( psz_env, &psz_end, 0 );
506 if( (*psz_env != '\0') && (*psz_end == '\0') )
514 /*****************************************************************************
515 * main_GetPszVariable: get the string value of an environment variable
516 *****************************************************************************
517 * This function is used to read some default parameters in modules.
518 *****************************************************************************/
519 char * main_GetPszVariable( char *psz_name, char *psz_default )
523 psz_env = getenv( psz_name );
528 return( psz_default );
531 /*****************************************************************************
532 * main_PutPszVariable: set the string value of an environment variable
533 *****************************************************************************
534 * This function is used to set some default parameters in modules. The use of
535 * this function will cause some memory leak: since some systems use the pointer
536 * passed to putenv to store the environment string, it can't be freed.
537 *****************************************************************************/
538 void main_PutPszVariable( char *psz_name, char *psz_value )
542 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
543 if( psz_env == NULL )
545 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
550 sprintf( psz_env, "%s=%s", psz_name, psz_value );
551 if( putenv( psz_env ) )
553 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
558 /*****************************************************************************
559 * main_PutIntVariable: set the integer 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_PutIntVariable( char *psz_name, int i_value )
567 char psz_value[ 256 ]; /* buffer for value */
569 sprintf( psz_value, "%d", i_value );
570 main_PutPszVariable( psz_name, psz_value );
573 /* following functions are local */
575 /*****************************************************************************
576 * GetConfiguration: parse command line
577 *****************************************************************************
578 * Parse command line and configuration file for configuration. If the inline
579 * help is requested, the function Usage() is called and the function returns
580 * -1 (causing main() to exit). The messages interface is initialized at this
581 * stage, but most structures are not allocated, so only environment should
583 *****************************************************************************/
584 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
589 /* Set default configuration and copy arguments */
590 p_main->i_argc = *pi_argc;
591 p_main->ppsz_argv = ppsz_argv;
592 p_main->ppsz_env = ppsz_env;
597 p_main->i_warning_level = 0;
600 p_main->p_channel = NULL;
602 /* Get the executable name (similar to the basename command) */
603 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
608 p_main->psz_arg0 = ++p_tmp;
617 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
618 * is the PSN - process serial number (a unique PID-ish thingie)
619 * still ok for real Darwin & when run from command line */
620 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
621 /* for example -psn_0_9306113 */
623 /* GDMF!... I can't do this or else the MacOSX window server will
624 * not pick up the PSN and not register the app and we crash...
625 * hence the following kludge otherwise we'll get confused w/ argv[1]
626 * being an input file name */
628 ppsz_argv[ 1 ] = NULL;
630 *pi_argc = *pi_argc - 1;
636 /* Parse command line options */
638 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
639 psz_shortopts, longopts, 0 ) ) != EOF )
643 /* General/common options */
644 case 'h': /* -h, --help */
648 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
649 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
650 INTF_STDOUT_DEFAULT ) ) == 0 )
652 /* No stdout redirection has been asked for */
653 intf_MsgImm( "\nPress the RETURN key to continue..." );
659 case 'H': /* -H, --longhelp */
663 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
664 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
665 INTF_STDOUT_DEFAULT ) ) == 0 )
667 /* No stdout redirection has been asked for */
668 intf_MsgImm( "\nPress the RETURN key to continue..." );
674 case OPT_VERSION: /* --version */
678 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
679 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
680 INTF_STDOUT_DEFAULT ) ) == 0 )
682 /* No stdout redirection has been asked for */
683 intf_MsgImm( "\nPress the RETURN key to continue..." );
689 case 'v': /* -v, --verbose */
690 p_main->i_warning_level++;
693 /* Interface warning messages level */
694 case 'I': /* -I, --intf */
695 main_PutPszVariable( INTF_METHOD_VAR, optarg );
697 case OPT_WARNING: /* --warning */
698 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
699 p_main->i_warning_level = atoi(optarg);
702 case OPT_STDOUT: /* --stdout */
703 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
711 case OPT_NOAUDIO: /* --noaudio */
714 case 'A': /* -A, --aout */
715 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
717 case OPT_STEREO: /* --stereo */
718 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
720 case OPT_MONO: /* --mono */
721 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
723 case OPT_SPDIF: /* --spdif */
724 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
726 case OPT_DOWNMIX: /* --downmix */
727 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
729 case OPT_IMDCT: /* --imdct */
730 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
732 case OPT_VOLUME: /* --volume */
733 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
737 case OPT_NOVIDEO: /* --novideo */
740 case 'V': /* -V, --vout */
741 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
743 case OPT_DISPLAY: /* --display */
744 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
746 case OPT_WIDTH: /* --width */
747 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
749 case OPT_HEIGHT: /* --height */
750 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
752 case 'g': /* -g, --grayscale */
753 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
755 case OPT_COLOR: /* --color */
756 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
758 case OPT_FULLSCREEN: /* --fullscreen */
759 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
761 case OPT_OVERLAY: /* --overlay */
762 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
764 case OPT_XVADAPTOR: /* --xvadaptor */
765 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
767 case OPT_MOTION: /* --motion */
768 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
770 case OPT_IDCT: /* --idct */
771 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
773 case OPT_YUV: /* --yuv */
774 main_PutPszVariable( YUV_METHOD_VAR, optarg );
776 case OPT_SMP: /* --smp */
777 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
781 case 't': /* --dvdtitle */
782 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
784 case 'T': /* --dvdchapter */
785 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
787 case 'u': /* --dvdangle */
788 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
790 case 'a': /* --dvdaudio */
791 if ( ! strcmp(optarg, "ac3") )
792 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
793 else if ( ! strcmp(optarg, "lpcm") )
794 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
795 else if ( ! strcmp(optarg, "mpeg") )
796 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
798 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
800 case 'c': /* --dvdchannel */
801 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
803 case 's': /* --dvdsubtitle */
804 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
806 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
807 main_PutPszVariable( "DVDCSS_METHOD", optarg );
809 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
810 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
814 case OPT_INPUT: /* --input */
815 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
817 case OPT_CHANNELS: /* --channels */
818 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
820 case OPT_CHANNELSERVER: /* --channelserver */
821 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
826 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
829 main_PutPszVariable( MEMCPY_METHOD_VAR, optarg );
832 /* Decoder options */
834 main_PutPszVariable( ADEC_MPEG_VAR, optarg );
837 /* Internal error: unknown option */
842 intf_ErrMsg( "intf error: unknown option `%s'",
845 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
846 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
847 INTF_STDOUT_DEFAULT ) ) == 0 )
849 /* No stdout redirection has been asked for */
850 intf_MsgImm( "\nPress the RETURN key to continue..." );
859 if( p_main->i_warning_level < 0 )
861 p_main->i_warning_level = 0;
867 /*****************************************************************************
868 * GetFilenames: parse command line options which are not flags
869 *****************************************************************************
870 * Parse command line for input files.
871 *****************************************************************************/
872 static int GetFilenames( int i_argc, char *ppsz_argv[] )
876 /* We assume that the remaining parameters are filenames */
877 for( i_opt = optind; i_opt < i_argc; i_opt++ )
879 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
880 ppsz_argv[ i_opt ] );
886 /*****************************************************************************
887 * Usage: print program usage
888 *****************************************************************************
889 * Print a short inline help. Message interface is initialized at this stage.
890 *****************************************************************************/
891 static void Usage( int i_fashion )
894 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
897 if( i_fashion == USAGE )
899 intf_MsgImm( "Try `%s --help' for more information.",
905 intf_MsgImm( "\nOptions:"
906 "\n -I, --intf <module> \tinterface method"
907 "\n -v, --verbose \tverbose mode (cumulative)"
908 "\n --stdout <filename> \tredirect console stdout"
909 "\n --memcpy <module> \tmemcpy method"
911 "\n --noaudio \tdisable audio"
912 "\n -A, --aout <module> \taudio output method"
913 "\n --stereo, --mono \tstereo/mono audio"
914 "\n --spdif \tAC3 pass-through mode"
915 "\n --downmix <module> \tAC3 downmix method"
916 "\n --imdct <module> \tAC3 IMDCT method"
917 "\n --volume [0..1024] \tVLC output volume"
919 "\n --novideo \tdisable video"
920 "\n -V, --vout <module> \tvideo output method"
921 "\n --display <display> \tdisplay string"
922 "\n --width <w>, --height <h> \tdisplay dimensions"
923 "\n -g, --grayscale \tgrayscale output"
924 "\n --fullscreen \tfullscreen output"
925 "\n --overlay \taccelerated display"
926 "\n --xvadaptor <adaptor> \tXVideo adaptor"
927 "\n --color \tcolor output"
928 "\n --motion <module> \tmotion compensation method"
929 "\n --idct <module> \tIDCT method"
930 "\n --yuv <module> \tYUV method"
931 "\n --synchro <type> \tforce synchro algorithm"
932 "\n --smp <number of threads> \tuse several processors"
934 "\n -t, --dvdtitle <num> \tchoose DVD title"
935 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
936 "\n -u, --dvdangle <num> \tchoose DVD angle"
937 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
938 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
939 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
940 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
941 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
943 "\n --input \tinput method"
944 "\n --channels \tenable channels"
945 "\n --channelserver <host> \tchannel server address"
947 "\n --mpeg_adec <builtin|mad> \tchoose audio decoder"
949 "\n -h, --help \tprint help and exit"
950 "\n -H, --longhelp \tprint long help and exit"
951 "\n --version \toutput version information and exit" );
953 if( i_fashion == SHORT_HELP )
956 /* Interface parameters */
957 intf_MsgImm( "\nInterface parameters:"
958 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
959 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
960 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
961 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout"
962 "\n " MEMCPY_METHOD_VAR "=<method name> \tmemcpy method" );
964 /* Audio parameters */
965 intf_MsgImm( "\nAudio parameters:"
966 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
967 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
968 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
969 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
970 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
971 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
972 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
973 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
975 /* Video parameters */
976 intf_MsgImm( "\nVideo parameters:"
977 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
978 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
979 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
980 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
981 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
982 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
983 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
984 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
985 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
986 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
987 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
988 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
989 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
990 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
993 intf_MsgImm( "\nDVD parameters:"
994 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
995 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
996 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
997 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
998 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
999 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
1000 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
1002 /* Input parameters */
1003 intf_MsgImm( "\nInput parameters:"
1004 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
1005 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
1006 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
1008 /* Decoder parameters */
1009 intf_MsgImm( "\nDecoder parameters:"
1010 "\n " ADEC_MPEG_VAR "=<builtin|mad> \taudio decoder" );
1013 /*****************************************************************************
1014 * Version: print complete program version
1015 *****************************************************************************
1016 * Print complete program version and build number.
1017 *****************************************************************************/
1018 static void Version( void )
1020 intf_MsgImm( VERSION_MESSAGE
1021 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
1022 "You may redistribute it under the terms of the GNU General Public License;\n"
1023 "see the file named COPYING for details.\n"
1024 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1027 /*****************************************************************************
1028 * InitSignalHandler: system signal handler initialization
1029 *****************************************************************************
1030 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1031 * at least a method to kill the program when all other methods failed, and
1032 * when we don't want to use SIGKILL.
1033 *****************************************************************************/
1034 static void InitSignalHandler( void )
1036 /* Termination signals */
1038 signal( SIGINT, FatalSignalHandler );
1039 signal( SIGHUP, FatalSignalHandler );
1040 signal( SIGQUIT, FatalSignalHandler );
1043 signal( SIGALRM, SimpleSignalHandler );
1044 signal( SIGPIPE, SimpleSignalHandler );
1049 /*****************************************************************************
1050 * SimpleSignalHandler: system signal handler
1051 *****************************************************************************
1052 * This function is called when a non fatal signal is received by the program.
1053 *****************************************************************************/
1054 static void SimpleSignalHandler( int i_signal )
1056 /* Acknowledge the signal received */
1057 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1061 /*****************************************************************************
1062 * FatalSignalHandler: system signal handler
1063 *****************************************************************************
1064 * This function is called when a fatal signal is received by the program.
1065 * It tries to end the program in a clean way.
1066 *****************************************************************************/
1067 static void FatalSignalHandler( int i_signal )
1069 /* Once a signal has been trapped, the termination sequence will be
1070 * armed and following signals will be ignored to avoid sending messages
1071 * to an interface having been destroyed */
1073 signal( SIGINT, SIG_IGN );
1074 signal( SIGHUP, SIG_IGN );
1075 signal( SIGQUIT, SIG_IGN );
1078 /* Acknowledge the signal received */
1079 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1081 /* Try to terminate everything - this is done by requesting the end of the
1082 * interface thread */
1083 p_main->p_intf->b_die = 1;
1086 /*****************************************************************************
1087 * InstructionSignalHandler: system signal handler
1088 *****************************************************************************
1089 * This function is called when a illegal instruction signal is received by
1091 * We use this function to test OS and CPU_Capabilities
1092 *****************************************************************************/
1093 static void InstructionSignalHandler( int i_signal )
1095 /* Once a signal has been trapped, the termination sequence will be
1096 * armed and following signals will be ignored to avoid sending messages
1097 * to an interface having been destroyed */
1099 /* Acknowledge the signal received */
1102 #ifdef HAVE_SIGRELSE
1103 sigrelse( i_signal );
1108 /*****************************************************************************
1109 * CPUCapabilities: list the processors MMX support and other capabilities
1110 *****************************************************************************
1111 * This function is called to list extensions the CPU may have.
1112 *****************************************************************************/
1113 static int CPUCapabilities( void )
1115 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1117 #if defined( SYS_BEOS )
1118 i_capabilities |= CPU_CAPABILITY_FPU
1119 | CPU_CAPABILITY_486
1120 | CPU_CAPABILITY_586
1121 | CPU_CAPABILITY_MMX;
1123 return( i_capabilities );
1125 #elif defined( SYS_DARWIN )
1126 struct host_basic_info hi;
1128 host_name_port_t host;
1131 char *psz_name, *psz_subname;
1133 i_capabilities |= CPU_CAPABILITY_FPU;
1135 /* Should 'never' fail? */
1136 host = mach_host_self();
1138 i_size = sizeof( hi ) / sizeof( int );
1139 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1141 if( ret != KERN_SUCCESS )
1143 fprintf( stderr, "error: couldn't get CPU information\n" );
1144 return( i_capabilities );
1147 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1148 /* FIXME: need better way to detect newer proccessors.
1149 * could do strncmp(a,b,5), but that's real ugly */
1150 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1152 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1155 return( i_capabilities );
1157 #elif defined( __i386__ )
1158 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1159 volatile boolean_t b_amd;
1161 i_capabilities |= CPU_CAPABILITY_FPU;
1163 signal( SIGILL, InstructionSignalHandler );
1165 /* test for a 486 CPU */
1166 asm volatile ( "pushfl\n\t"
1168 "movl %%eax, %%ebx\n\t"
1169 "xorl $0x200000, %%eax\n\t"
1179 if( i_eax == i_ebx )
1181 signal( SIGILL, NULL );
1182 return( i_capabilities );
1185 i_capabilities |= CPU_CAPABILITY_486;
1187 /* the CPU supports the CPUID instruction - get its level */
1188 cpuid( 0x00000000 );
1192 signal( SIGILL, NULL );
1193 return( i_capabilities );
1196 /* FIXME: this isn't correct, since some 486s have cpuid */
1197 i_capabilities |= CPU_CAPABILITY_586;
1199 /* borrowed from mpeg2dec */
1200 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1201 && ( i_edx == 0x69746e65 );
1203 /* test for the MMX flag */
1204 cpuid( 0x00000001 );
1206 if( ! (i_edx & 0x00800000) )
1208 signal( SIGILL, NULL );
1209 return( i_capabilities );
1212 i_capabilities |= CPU_CAPABILITY_MMX;
1214 if( i_edx & 0x02000000 )
1216 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1218 #ifdef CAN_COMPILE_SSE
1219 /* We test if OS support the SSE instructions */
1221 if( setjmp( env ) == 0 )
1223 /* Test a SSE instruction */
1224 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1227 if( i_illegal == 0 )
1229 i_capabilities |= CPU_CAPABILITY_SSE;
1233 fprintf( stderr, "warning: your OS doesn't have support for "
1234 "SSE instructions, "
1235 "some optimizations\nwill be disabled\n" );
1237 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1243 /* test for additional capabilities */
1244 cpuid( 0x80000000 );
1246 if( i_eax < 0x80000001 )
1248 signal( SIGILL, NULL );
1249 return( i_capabilities );
1252 /* list these additional capabilities */
1253 cpuid( 0x80000001 );
1255 #ifdef CAN_COMPILE_3DNOW
1256 if( i_edx & 0x80000000 )
1259 if( setjmp( env ) == 0 )
1261 /* Test a 3D Now! instruction */
1262 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1265 if( i_illegal == 0 )
1267 i_capabilities |= CPU_CAPABILITY_3DNOW;
1272 if( b_amd && ( i_edx & 0x00400000 ) )
1274 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1277 signal( SIGILL, NULL );
1278 return( i_capabilities );
1280 #elif defined( __powerpc__ )
1282 i_capabilities |= CPU_CAPABILITY_FPU;
1284 /* Test for Altivec */
1285 signal( SIGILL, InstructionSignalHandler );
1287 #ifdef CAN_COMPILE_ALTIVEC
1289 if( setjmp( env ) == 0 )
1291 asm volatile ("mtspr 256,%0" : : "r" (-1));
1294 if( i_illegal == 0 )
1296 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1300 signal( SIGILL, NULL );
1301 return( i_capabilities );
1304 /* default behaviour */
1305 return( i_capabilities );
1310 /*****************************************************************************
1311 * RedirectSTDOUT: redirect stdout and stderr to a file
1312 *****************************************************************************
1313 * This function will redirect stdout and stderr to a file if the user has
1315 *****************************************************************************/
1316 static int RedirectSTDOUT( void )
1318 int i_stdout_filedesc;
1319 char *psz_stdout_filename;
1321 psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1322 INTF_STDOUT_DEFAULT );
1323 if( strcmp( "", psz_stdout_filename ) != 0 )
1326 i_stdout_filedesc = open( psz_stdout_filename,
1327 O_CREAT | O_TRUNC | O_RDWR,
1328 S_IREAD | S_IWRITE );
1330 if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1332 intf_ErrMsg( "warning: unable to redirect stdout" );
1335 if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1337 intf_ErrMsg( "warning: unable to redirect stderr" );
1340 close( i_stdout_filedesc );
1344 /* No stdout redirection has been asked so open a console */
1345 if( p_main->i_warning_level )
1355 /*****************************************************************************
1356 * ShowConsole: On Win32, create an output console for debug messages
1357 *****************************************************************************
1358 * This function is usefull only on Win32.
1359 *****************************************************************************/
1360 static void ShowConsole( void )
1364 freopen( "CONOUT$", "w", stdout );
1365 freopen( "CONOUT$", "w", stderr );
1366 freopen( "CONIN$", "r", stdin );