missing #include (for malloc and free)
[mplayer/glamo.git] / mplayer.c
blob19cdd0daa890d14078ca4bdc6a114593aefc9790
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "config.h"
5 #ifdef WIN32
6 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
7 #include <windows.h>
8 #endif
9 #include <string.h>
10 #include <unistd.h>
12 // #include <sys/mman.h>
13 #include <sys/types.h>
14 #ifndef __MINGW32__
15 #include <sys/ioctl.h>
16 #include <sys/wait.h>
17 #else
18 #define SIGHUP 1 /* hangup */
19 #define SIGQUIT 3 /* quit */
20 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
21 #define SIGBUS 10 /* bus error */
22 extern int mp_input_win32_slave_cmd_func(int fd,char* dest,int size);
23 #endif
25 #include <sys/time.h>
26 #include <sys/stat.h>
28 #include <signal.h>
29 #include <time.h>
30 #include <fcntl.h>
31 #include <limits.h>
33 #include <errno.h>
35 #include "version.h"
37 #include "mp_msg.h"
39 #define HELP_MP_DEFINE_STATIC
40 #include "help_mp.h"
42 #include "m_option.h"
43 #include "m_config.h"
45 #include "cfg-mplayer-def.h"
47 #ifdef USE_SUB
48 #include "subreader.h"
49 #endif
51 #include "libvo/video_out.h"
53 #include "libvo/font_load.h"
54 #include "libvo/sub.h"
56 #ifdef HAVE_X11
57 #include "libvo/x11_common.h"
58 #endif
60 #include "libao2/audio_out.h"
62 #include "codec-cfg.h"
64 #ifdef USE_DVDNAV
65 #include <dvdnav.h>
66 #endif
68 #ifdef USE_EDL
69 #include "edl.h"
70 #endif
72 #include "spudec.h"
73 #include "vobsub.h"
75 #include "osdep/getch2.h"
76 #include "osdep/timer.h"
78 #include "cpudetect.h"
80 #ifdef HAVE_NEW_GUI
81 #include "Gui/interface.h"
82 #endif
84 #include "input/input.h"
86 int slave_mode=0;
87 int verbose=0;
88 int identify=0;
89 int quiet=0;
91 #define ABS(x) (((x)>=0)?(x):(-(x)))
92 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
94 #ifdef HAVE_RTC
95 #ifdef __linux__
96 #include <linux/rtc.h>
97 #else
98 #include <rtc.h>
99 #define RTC_IRQP_SET RTCIO_IRQP_SET
100 #define RTC_PIE_ON RTCIO_PIE_ON
101 #endif
102 #endif
104 #ifdef USE_TV
105 #include "libmpdemux/tv.h"
106 #endif
108 #ifdef HAS_DVBIN_SUPPORT
109 #include "libmpdemux/dvbin.h"
110 static int last_dvb_step = 1;
111 #endif
113 //**************************************************************************//
114 // Playtree
115 //**************************************************************************//
116 #include "playtree.h"
117 #include "playtreeparser.h"
119 #ifdef HAVE_NEW_GUI
120 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
121 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
122 #endif
124 play_tree_t* playtree;
125 play_tree_iter_t* playtree_iter = NULL;
127 #define PT_NEXT_ENTRY 1
128 #define PT_PREV_ENTRY -1
129 #define PT_NEXT_SRC 2
130 #define PT_PREV_SRC -2
131 #define PT_UP_NEXT 3
132 #define PT_UP_PREV -3
134 //**************************************************************************//
135 // Config
136 //**************************************************************************//
137 m_config_t* mconfig;
139 extern play_tree_t*
140 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv);
141 extern int
142 m_config_parse_config_file(m_config_t* config, char *conffile);
144 //**************************************************************************//
145 // Config file
146 //**************************************************************************//
148 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
150 static int cfg_include(m_option_t *conf, char *filename){
151 return m_config_parse_config_file(mconfig, filename);
154 #include "get_path.c"
156 //**************************************************************************//
157 // XScreensaver
158 //**************************************************************************//
160 #ifdef HAVE_X11
161 void xscreensaver_heartbeat(void);
162 #endif
164 //**************************************************************************//
165 //**************************************************************************//
166 // Input media streaming & demultiplexer:
167 //**************************************************************************//
169 static int max_framesize=0;
171 #include "libmpdemux/stream.h"
172 #include "libmpdemux/demuxer.h"
173 #include "libmpdemux/stheader.h"
174 //#include "parse_es.h"
175 #ifdef HAVE_MATROSKA
176 #include "libmpdemux/matroska.h"
177 #endif
179 #include "libmpcodecs/dec_audio.h"
180 #include "libmpcodecs/dec_video.h"
181 #include "libmpcodecs/mp_image.h"
182 #include "libmpcodecs/vf.h"
184 extern void vf_list_plugins();
186 //**************************************************************************//
187 //**************************************************************************//
189 // Common FIFO functions, and keyboard/event FIFO code
190 #include "fifo.c"
191 int noconsolecontrols=0;
192 //**************************************************************************//
194 vo_functions_t *video_out=NULL;
195 ao_functions_t *audio_out=NULL;
197 int fixed_vo=0;
198 int eof=0;
200 // benchmark:
201 double video_time_usage=0;
202 double vout_time_usage=0;
203 static double audio_time_usage=0;
204 static int total_time_usage_start=0;
205 static int total_frame_cnt=0;
206 static int drop_frame_cnt=0; // total number of dropped frames
207 int benchmark=0;
209 // options:
210 int auto_quality=0;
211 static int output_quality=0;
213 float playback_speed=1.0;
215 int use_gui=0;
217 #ifdef HAVE_NEW_GUI
218 int enqueue=0;
219 #endif
221 #define MAX_OSD_LEVEL 3
223 int osd_level=1;
224 int osd_level_saved=-1;
225 int osd_visible=100;
227 // seek:
228 static char *seek_to_sec=NULL;
229 static off_t seek_to_byte=0;
230 static off_t step_sec=0;
231 static int loop_times=-1;
232 static int loop_seek=0;
234 // A/V sync:
235 int autosync=0; // 30 might be a good default value.
237 // may be changed by GUI: (FIXME!)
238 float rel_seek_secs=0;
239 int abs_seek_pos=0;
241 // codecs:
242 char **audio_codec_list=NULL; // override audio codec
243 char **video_codec_list=NULL; // override video codec
244 char **audio_fm_list=NULL; // override audio codec family
245 char **video_fm_list=NULL; // override video codec family
247 // streaming:
248 int audio_id=-1;
249 int video_id=-1;
250 int dvdsub_id=-1;
251 int vobsub_id=-1;
252 char* audio_lang=NULL;
253 char* dvdsub_lang=NULL;
254 static char* spudec_ifo=NULL;
255 char* filename=NULL; //"MI2-Trailer.avi";
256 int forced_subs_only=0;
258 // cache2:
259 int stream_cache_size=-1;
260 #ifdef USE_STREAM_CACHE
261 extern int cache_fill_status;
263 float stream_cache_min_percent=20.0;
264 float stream_cache_prefill_percent=5.0;
265 #else
266 #define cache_fill_status 0
267 #endif
269 // dump:
270 static char *stream_dump_name="stream.dump";
271 int stream_dump_type=0;
273 // A-V sync:
274 static float default_max_pts_correction=-1;//0.01f;
275 static float max_pts_correction=0;//default_max_pts_correction;
276 static float c_total=0;
277 float audio_delay=0;
279 static int softsleep=0;
281 float force_fps=0;
282 static int force_srate=0;
283 static int audio_output_format=0;
284 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
285 static int play_n_frames=-1;
286 static int play_n_frames_mf=-1;
288 // screen info:
289 char** video_driver_list=NULL;
290 char** audio_driver_list=NULL;
292 extern char *vo_subdevice;
293 extern char *ao_subdevice;
295 // codec outfmt flags (defined in libmpcodecs/vd.c)
296 extern int vo_flags;
298 // sub:
299 char *font_name=NULL;
300 #ifdef HAVE_FONTCONFIG
301 extern int font_fontconfig;
302 #endif
303 float font_factor=0.75;
304 char **sub_name=NULL;
305 float sub_delay=0;
306 float sub_fps=0;
307 int sub_auto = 1;
308 char *vobsub_name=NULL;
309 /*DSP!!char *dsp=NULL;*/
310 int subcc_enabled=0;
311 int suboverlap_enabled = 1;
312 #ifdef USE_SUB
313 sub_data* set_of_subtitles[MAX_SUBTITLE_FILES];
314 int set_of_sub_size = 0;
315 int set_of_sub_pos = -1;
316 float sub_last_pts = -303;
317 #endif
318 int global_sub_size = 0; // this encompasses all subtitle sources
319 int global_sub_pos = -1; // this encompasses all subtitle sources
320 #define SUB_SOURCE_SUBS 0
321 #define SUB_SOURCE_VOBSUB 1
322 #define SUB_SOURCE_DEMUX 2
323 #define SUB_SOURCES 3
324 int global_sub_indices[SUB_SOURCES];
325 int global_sub_quiet_osd_hack = 0;
327 static stream_t* stream=NULL;
328 static demuxer_t *demuxer=NULL;
329 static sh_audio_t *sh_audio=NULL;
330 static sh_video_t *sh_video=NULL;
332 char* current_module=NULL; // for debugging
334 extern int vo_gamma_gamma;
335 extern int vo_gamma_brightness;
336 extern int vo_gamma_contrast;
337 extern int vo_gamma_saturation;
338 extern int vo_gamma_hue;
340 // ---
342 #ifdef HAVE_MENU
343 #include "m_struct.h"
344 #include "libmenu/menu.h"
345 extern void vf_menu_pause_update(struct vf_instance_s* vf);
346 extern vf_info_t vf_info_menu;
347 static vf_info_t* libmenu_vfs[] = {
348 &vf_info_menu,
349 NULL
351 static vf_instance_t* vf_menu = NULL;
352 static int use_menu = 0;
353 static char* menu_cfg = NULL;
354 static char* menu_root = "main";
355 #endif
358 #ifdef HAVE_RTC
359 static int nortc;
360 static char* rtc_device;
361 #endif
363 #ifdef USE_EDL
364 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
365 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
366 short user_muted = 0; ///< Stores whether User wanted muted mode.
367 short edl_muted = 0; ///< Stores whether EDL is currently in muted mode.
368 short edl_decision = 0; ///< 1 when an EDL operation has been made
369 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode
370 #endif
372 static unsigned int inited_flags=0;
373 #define INITED_VO 1
374 #define INITED_AO 2
375 #define INITED_GUI 4
376 #define INITED_GETCH2 8
377 #define INITED_SPUDEC 32
378 #define INITED_STREAM 64
379 #define INITED_INPUT 128
380 #define INITED_VOBSUB 256
381 #define INITED_DEMUXER 512
382 #define INITED_ACODEC 1024
383 #define INITED_VCODEC 2048
384 #define INITED_ALL 0xFFFF
386 static void uninit_player(unsigned int mask){
387 mask=inited_flags&mask;
389 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
391 if(mask&INITED_ACODEC){
392 inited_flags&=~INITED_ACODEC;
393 current_module="uninit_acodec";
394 if(sh_audio) uninit_audio(sh_audio);
395 #ifdef HAVE_NEW_GUI
396 guiGetEvent(guiSetAfilter, (char *)NULL);
397 #endif
398 sh_audio=NULL;
401 if(mask&INITED_VCODEC){
402 inited_flags&=~INITED_VCODEC;
403 current_module="uninit_vcodec";
404 if(sh_video) uninit_video(sh_video);
405 sh_video=NULL;
406 #ifdef HAVE_MENU
407 vf_menu=NULL;
408 #endif
411 if(mask&INITED_DEMUXER){
412 inited_flags&=~INITED_DEMUXER;
413 current_module="free_demuxer";
414 if(demuxer){
415 stream=demuxer->stream;
416 free_demuxer(demuxer);
418 demuxer=NULL;
421 // kill the cache process:
422 if(mask&INITED_STREAM){
423 inited_flags&=~INITED_STREAM;
424 current_module="uninit_stream";
425 if(stream) free_stream(stream);
426 stream=NULL;
429 if(mask&INITED_VO){
430 inited_flags&=~INITED_VO;
431 current_module="uninit_vo";
432 video_out->uninit();
433 video_out=NULL;
436 // must be after libvo uninit, as few vo drivers (svgalib) has tty code
437 if(mask&INITED_GETCH2){
438 inited_flags&=~INITED_GETCH2;
439 current_module="uninit_getch2";
440 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
441 // restore terminal:
442 getch2_disable();
445 if(mask&INITED_VOBSUB){
446 inited_flags&=~INITED_VOBSUB;
447 current_module="uninit_vobsub";
448 if(vo_vobsub) vobsub_close(vo_vobsub);
449 vo_vobsub=NULL;
452 if (mask&INITED_SPUDEC){
453 inited_flags&=~INITED_SPUDEC;
454 current_module="uninit_spudec";
455 spudec_free(vo_spudec);
456 vo_spudec=NULL;
459 if(mask&INITED_AO){
460 inited_flags&=~INITED_AO;
461 current_module="uninit_ao";
462 audio_out->uninit(eof?0:1); audio_out=NULL;
465 #ifdef HAVE_NEW_GUI
466 if(mask&INITED_GUI){
467 inited_flags&=~INITED_GUI;
468 current_module="uninit_gui";
469 guiDone();
471 #endif
473 if(mask&INITED_INPUT){
474 inited_flags&=~INITED_INPUT;
475 current_module="uninit_input";
476 mp_input_uninit();
479 current_module=NULL;
482 static void exit_player_with_rc(char* how, int rc){
484 uninit_player(INITED_ALL);
485 #ifdef HAVE_X11
486 #ifdef HAVE_NEW_GUI
487 if ( !use_gui )
488 #endif
489 vo_uninit(); // close the X11 connection (if any opened)
490 #endif
492 #ifdef HAVE_FREETYPE
493 current_module="uninit_font";
494 if (vo_font) free_font_desc(vo_font);
495 vo_font = NULL;
496 done_freetype();
497 #endif
499 current_module="exit_player";
501 // free mplayer config
502 free(mconfig);
504 #ifdef USE_EDL
505 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
506 #endif
507 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,mp_gettext(how));
508 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
510 exit(rc);
513 void exit_player(char* how){
514 exit_player_with_rc(how, 1);
517 #ifndef __MINGW32__
518 static void child_sighandler(int x){
519 pid_t pid;
520 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
522 #endif
524 #ifdef CRASH_DEBUG
525 static char *prog_path;
526 static int crash_debug = 0;
527 #endif
529 static void exit_sighandler(int x){
530 static int sig_count=0;
531 #ifdef CRASH_DEBUG
532 if (!crash_debug || x != SIGTRAP)
533 #endif
534 ++sig_count;
535 if(inited_flags==0 && sig_count>1) exit(1);
536 if(sig_count==5)
538 /* We're crashing bad and can't uninit cleanly :(
539 * by popular request, we make one last (dirty)
540 * effort to restore the user's
541 * terminal. */
542 getch2_disable();
543 exit(1);
545 if(sig_count==6) exit(1);
546 if(sig_count>6){
547 // can't stop :(
548 #ifndef __MINGW32__
549 kill(getpid(),SIGKILL);
550 #endif
552 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
553 current_module?current_module:mp_gettext("unknown")
555 if(sig_count<=1)
556 switch(x){
557 case SIGINT:
558 case SIGQUIT:
559 case SIGTERM:
560 case SIGKILL:
561 break; // killed from keyboard (^C) or killed [-9]
562 case SIGILL:
563 #ifdef RUNTIME_CPUDETECT
564 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
565 #else
566 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
567 #endif
568 case SIGFPE:
569 case SIGSEGV:
570 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
571 default:
572 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
573 #ifdef CRASH_DEBUG
574 if (crash_debug) {
575 int gdb_pid;
576 char spid[20];
577 snprintf(spid, 19, "%i", getpid());
578 spid[19] = 0;
579 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
580 gdb_pid = fork();
581 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
582 if (gdb_pid == 0) { // We are the child
583 if (execlp("gdb", "gdb", prog_path, spid, NULL) == -1)
584 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
585 } else if (gdb_pid < 0)
586 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
587 else {
588 waitpid(gdb_pid, NULL, 0);
590 if (x == SIGTRAP) return;
592 #endif
594 exit_player(NULL);
597 //extern void write_avi_header_1(FILE *f,int fcc,float fps,int width,int height);
599 extern void mp_input_register_options(m_config_t* cfg);
601 #include "mixer.h"
602 mixer_t mixer;
603 /// step size of mixer changes
604 int volstep = 3;
606 #include "cfg-mplayer.h"
608 void parse_cfgfiles( m_config_t* conf )
610 char *conffile;
611 int conffile_fd;
612 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
613 exit_player(NULL);
614 if ((conffile = get_path("")) == NULL) {
615 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
616 } else {
617 #ifdef __MINGW32__
618 mkdir(conffile);
619 #else
620 mkdir(conffile, 0777);
621 #endif
622 free(conffile);
623 if ((conffile = get_path("config")) == NULL) {
624 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
625 } else {
626 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
627 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
628 write(conffile_fd, default_config, strlen(default_config));
629 close(conffile_fd);
631 if (m_config_parse_config_file(conf, conffile) < 0)
632 exit_player(NULL);
633 free(conffile);
638 void load_per_file_config (m_config_t* conf, const char *const file)
640 char *confpath;
641 char cfg[strlen(file)+10];
642 struct stat st;
643 char *name;
645 sprintf (cfg, "%s.conf", file);
647 if (!stat (cfg, &st))
649 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
650 m_config_parse_config_file (conf, cfg);
651 return;
654 if ((name = strrchr (cfg, '/')) == NULL)
655 name = cfg;
656 else
657 name++;
659 if ((confpath = get_path (name)) != NULL)
661 if (!stat (confpath, &st))
663 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
664 m_config_parse_config_file (conf, confpath);
667 free (confpath);
671 // When libmpdemux perform a blocking operation (network connection or cache filling)
672 // if the operation fail we use this function to check if it was interrupted by the user.
673 // The function return a new value for eof.
674 static int libmpdemux_was_interrupted(int eof) {
675 mp_cmd_t* cmd;
676 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
677 switch(cmd->id) {
678 case MP_CMD_QUIT:
679 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
680 case MP_CMD_PLAY_TREE_STEP: {
681 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
682 } break;
683 case MP_CMD_PLAY_TREE_UP_STEP: {
684 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
685 } break;
686 case MP_CMD_PLAY_ALT_SRC_STEP: {
687 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
688 } break;
690 mp_cmd_free(cmd);
692 return eof;
695 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
696 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
698 int playtree_add_playlist(play_tree_t* entry)
700 play_tree_add_bpf(entry,filename);
702 #ifdef HAVE_NEW_GUI
703 if (use_gui) {
704 if (entry) {
705 import_playtree_playlist_into_gui(entry, mconfig);
706 play_tree_free_list(entry,1);
708 } else
709 #endif
711 if(!entry) {
712 entry = playtree_iter->tree;
713 if(play_tree_iter_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
714 return PT_NEXT_ENTRY;
716 if(playtree_iter->tree == entry ) { // Loop with a single file
717 if(play_tree_iter_up_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
718 return PT_NEXT_ENTRY;
721 play_tree_remove(entry,1,1);
722 return PT_NEXT_SRC;
724 play_tree_insert_entry(playtree_iter->tree,entry);
725 play_tree_set_params_from(entry,playtree_iter->tree);
726 entry = playtree_iter->tree;
727 if(play_tree_iter_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
728 return PT_NEXT_ENTRY;
730 play_tree_remove(entry,1,1);
732 return PT_NEXT_SRC;
735 static int play_tree_step = 1;
737 int sub_source()
739 int source = -1;
740 int top = -1;
741 int i;
742 for (i = 0; i < SUB_SOURCES; i++) {
743 int j = global_sub_indices[i];
744 if ((j >= 0) && (j > top) && (global_sub_pos >= j)) {
745 source = i;
746 top = j;
749 return source;
752 #ifdef USE_SUB
754 sub_data* subdata = NULL;
755 static subtitle* vo_sub_last = NULL;
757 void add_subtitles(char *filename, float fps, int silent)
759 sub_data *subd;
761 if (filename == NULL) {
762 return;
765 subd = sub_read_file(filename, fps);
766 if(!subd && !silent)
767 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub, filename);
768 if (subd == NULL || set_of_sub_size >= MAX_SUBTITLE_FILES) return;
769 set_of_subtitles[set_of_sub_size] = subd;
770 if (identify)
772 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", set_of_sub_size);
773 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n", filename);
775 ++set_of_sub_size;
776 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, set_of_sub_size, filename);
779 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
780 void update_set_of_subtitles()
781 // subdata was changed, set_of_sub... have to be updated.
783 int i;
784 if (set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
785 for (i = set_of_sub_pos + 1; i < set_of_sub_size; ++i)
786 set_of_subtitles[i-1] = set_of_subtitles[i];
787 set_of_subtitles[set_of_sub_size-1] = NULL;
788 --set_of_sub_size;
789 if (set_of_sub_size > 0) subdata = set_of_subtitles[set_of_sub_pos=0];
791 else if (set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
792 set_of_subtitles[set_of_sub_pos] = subdata;
794 else if (set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
795 set_of_subtitles[set_of_sub_pos=set_of_sub_size] = subdata;
796 ++set_of_sub_size;
799 #endif
802 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
803 * make it all work is to use the builtin SDL-bootstrap code, which
804 * will be done automatically by replacing our main() if we include SDL.h.
806 #if defined(SYS_DARWIN) && defined(HAVE_SDL)
807 #include <SDL.h>
808 #endif
811 * \brief append a formatted string
812 * \param buf buffer to print into
813 * \param pos position of terminating 0 in buf
814 * \param len maximum number of characters in buf, not including terminating 0
815 * \param format printf format string
817 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
819 va_list va;
820 va_start(va, format);
821 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
822 va_end(va);
823 if (*pos >= len ) {
824 buf[len] = 0;
825 *pos = len;
830 * \brief print the status line
831 * \param a_pos audio position
832 * \param a_v A-V desynchronization
833 * \param corr amount out A-V synchronization
835 static void print_status(float a_pos, float a_v, float corr)
837 int width;
838 char *line;
839 unsigned pos = 0;
840 get_screen_size();
841 if (screen_width > 0)
842 width = screen_width;
843 else
844 width = 80;
845 #ifdef WIN32
846 // windows command line is broken (MinGW's rxvt works though, but we
847 // should not depend on that).
848 width--;
849 #endif
850 line = malloc(width + 1); // one additional for terminating null
852 // Audio time
853 if (sh_audio) {
854 saddf(line, &pos, width, "A:%6.1f ", a_pos);
855 if (!sh_video) {
856 // convert time to HH:MM:SS.F format
857 long tenths = 10 * a_pos;
858 int f1 = tenths % 10;
859 int ss = (tenths / 10) % 60;
860 int mm = (tenths / 600) % 60;
861 int hh = (tenths / 36000) % 100;
862 saddf(line, &pos, width, "(");
863 if (hh > 0)
864 saddf(line, &pos, width, "%2d:", hh);
865 if (hh > 0 || mm > 0)
866 saddf(line, &pos, width, "%02d:", mm);
867 saddf(line, &pos, width, "%02d.", ss);
868 saddf(line, &pos, width, "%1d", f1);
869 saddf(line, &pos, width, ") ");
873 // Video time
874 if (sh_video)
875 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
877 // A-V sync
878 if (sh_audio && sh_video)
879 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
881 // Video stats
882 if (sh_video)
883 saddf(line, &pos, width, "%3d/%3d ",
884 (int)sh_video->num_frames,
885 (int)sh_video->num_frames_decoded);
887 // CPU usage
888 if (sh_video) {
889 if (sh_video->timer > 0.5)
890 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
891 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
892 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
893 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
894 else
895 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
896 } else if (sh_audio) {
897 if (sh_audio->delay > 0.5)
898 saddf(line, &pos, width, "%4.1f%% ",
899 100.0*audio_time_usage/(double)sh_audio->delay);
900 else
901 saddf(line, &pos, width, "??,?%% ");
904 // VO stats
905 if (sh_video)
906 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
908 #ifdef USE_STREAM_CACHE
909 // cache stats
910 if (stream_cache_size > 0)
911 saddf(line, &pos, width, "%d%% ", cache_fill_status);
912 #endif
914 // other
915 if (playback_speed != 1)
916 saddf(line, &pos, width, "%4.2fx ", playback_speed);
918 // end
919 memset(&line[pos], ' ', width - pos);
920 line[width] = 0;
921 mp_msg(MSGT_AVSYNC, MSGL_STATUS, "%s\r", line);
922 free(line);
926 * \brief build a chain of audio filters that converts the input format
927 * to the ao's format, taking into account the current playback_speed.
928 * \param sh_audio describes the requested input format of the chain.
929 * \param ao_data describes the requested output format of the chain.
931 static int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
933 int new_srate;
934 int result;
935 if (!sh_audio)
937 #ifdef HAVE_NEW_GUI
938 guiGetEvent(guiSetAfilter, (char *)NULL);
939 #endif
940 mixer.afilter = NULL;
941 return 0;
943 new_srate = sh_audio->samplerate * playback_speed;
944 if (new_srate != ao_data->samplerate) {
945 // limits are taken from libaf/af_resample.c
946 if (new_srate < 8000)
947 new_srate = 8000;
948 if (new_srate > 192000)
949 new_srate = 192000;
950 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
952 result = init_audio_filters(sh_audio, new_srate,
953 sh_audio->channels, sh_audio->sample_format,
954 ao_data->samplerate, ao_data->channels, ao_data->format,
955 ao_data->outburst * 4, ao_data->buffersize);
956 mixer.afilter = sh_audio->afilter;
957 #ifdef HAVE_NEW_GUI
958 guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
959 #endif
960 return result;
963 #ifdef USE_SUB
965 * \brief Log the currently displayed subtitle to a file
967 * Logs the current or last displayed subtitle together with filename
968 * and time information to ~/.mplayer/subtitle_log
970 * Intended purpose is to allow convenient marking of bogus subtitles
971 * which need to be fixed while watching the movie.
974 static void log_sub(){
975 char *fname;
976 FILE *f;
977 int i;
979 if (subdata == NULL || vo_sub_last == NULL) return;
980 fname = get_path("subtitle_log");
981 f = fopen(fname, "a");
982 if (!f) return;
983 fprintf(f, "----------------------------------------------------------\n");
984 if (subdata->sub_uses_time) {
985 fprintf(f, "N: %s S: %02d:%02d:%02d.%02d E: %02d:%02d:%02d.%02d\n", filename,
986 vo_sub_last->start/360000, (vo_sub_last->start/6000)%60,
987 (vo_sub_last->start/100)%60, vo_sub_last->start%100,
988 vo_sub_last->end/360000, (vo_sub_last->end/6000)%60,
989 (vo_sub_last->end/100)%60, vo_sub_last->end%100);
990 } else {
991 fprintf(f, "N: %s S: %d E: %d\n", filename, vo_sub_last->start, vo_sub_last->end);
993 for (i = 0; i < vo_sub_last->lines; i++) {
994 fprintf(f, "%s\n", vo_sub_last->text[i]);
996 fclose(f);
998 #endif
1000 int main(int argc,char* argv[]){
1003 char * mem_ptr;
1005 static demux_stream_t *d_audio=NULL;
1006 static demux_stream_t *d_video=NULL;
1007 static demux_stream_t *d_dvdsub=NULL;
1009 int file_format=DEMUXER_TYPE_UNKNOWN;
1011 int delay_corrected=1;
1013 // movie info:
1015 int osd_function=OSD_PLAY;
1016 int osd_last_pts=-303;
1017 int osd_show_av_delay = 0;
1018 int osd_show_text = 0;
1019 int osd_show_speed = 0;
1020 int osd_show_sub_delay = 0;
1021 int osd_show_sub_pos = 0;
1022 int osd_show_sub_visibility = 0;
1023 int osd_show_sub_alignment = 0;
1024 int osd_show_vobsub_changed = 0;
1025 int osd_show_sub_changed = 0;
1026 int osd_show_percentage = 0;
1027 int osd_show_tv_channel = 25;
1028 int osd_show_ontop = 0;
1029 int osd_show_rootwin = 0;
1030 int osd_show_framedropping = 0;
1032 int rtc_fd=-1;
1034 //float a_frame=0; // Audio
1036 int i;
1037 char *tmp;
1039 int gui_no_filename=0;
1042 srand((int) time(NULL));
1044 mp_msg_init();
1045 mp_msg_set_level(MSGL_STATUS);
1047 mp_msg(MSGT_CPLAYER,MSGL_INFO, "MPlayer " VERSION " (C) 2000-2005 MPlayer Team\n");
1048 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
1049 GetCpuCaps(&gCpuCaps);
1050 #if defined(ARCH_X86) || defined(ARCH_X86_64)
1051 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
1052 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
1053 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
1054 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
1055 #ifdef RUNTIME_CPUDETECT
1056 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
1057 #else
1058 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
1059 #ifdef HAVE_MMX
1060 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
1061 #endif
1062 #ifdef HAVE_MMX2
1063 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
1064 #endif
1065 #ifdef HAVE_3DNOW
1066 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
1067 #endif
1068 #ifdef HAVE_3DNOWEX
1069 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
1070 #endif
1071 #ifdef HAVE_SSE
1072 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
1073 #endif
1074 #ifdef HAVE_SSE2
1075 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
1076 #endif
1077 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
1078 #endif
1079 #endif
1081 #if defined(WIN32) && defined(USE_WIN32DLL)
1082 { /*make our codec dirs available for LoadLibraryA()*/
1083 char tmppath[MAX_PATH*2 + 1];
1084 char win32path[MAX_PATH];
1085 char realpath[MAX_PATH];
1086 #ifdef __CYGWIN__
1087 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
1088 strcpy(tmppath,win32path);
1089 #ifdef USE_REALCODECS
1090 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
1091 sprintf(tmppath,"%s;%s",win32path,realpath);
1092 #endif /*USE_REALCODECS*/
1093 #else
1094 if(!strstr(WIN32_PATH,":")){
1095 GetModuleFileNameA(NULL, win32path, MAX_PATH);
1096 strcpy(win32path + strlen(win32path) - strlen("mplayer.exe"), WIN32_PATH);
1098 else strcpy(win32path,WIN32_PATH);
1099 strcpy(tmppath,win32path);
1100 #ifdef USE_REALCODECS
1101 if(!strstr(REALCODEC_PATH,":")){
1102 GetModuleFileNameA(NULL, realpath, MAX_PATH);
1103 strcpy(realpath + strlen(realpath) - strlen("mplayer.exe"), REALCODEC_PATH);
1105 else strcpy(realpath,REALCODEC_PATH);
1106 sprintf(tmppath,"%s;%s",win32path,realpath);
1107 #endif /*USE_REALCODECS*/
1108 #endif /*__CYGWIN__*/
1109 SetEnvironmentVariableA("PATH", tmppath);
1111 #endif /*WIN32 && USE_WIN32DLL*/
1113 #ifdef USE_TV
1114 tv_param_immediate = 1;
1115 #endif
1117 if ( argv[0] )
1118 if(!strcmp(argv[0],"gmplayer") ||
1119 (strrchr(argv[0],'/') && !strcmp(strrchr(argv[0],'/'),"/gmplayer") ) )
1120 use_gui=1;
1122 mconfig = m_config_new();
1123 m_config_register_options(mconfig,mplayer_opts);
1124 // TODO : add something to let modules register their options
1125 mp_input_register_options(mconfig);
1126 parse_cfgfiles(mconfig);
1128 #ifdef HAVE_NEW_GUI
1129 if ( use_gui ) cfg_read();
1130 #endif
1132 playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
1133 if(playtree == NULL)
1134 exit_player(NULL);
1136 playtree = play_tree_cleanup(playtree);
1137 if(playtree) {
1138 playtree_iter = play_tree_iter_new(playtree,mconfig);
1139 if(playtree_iter) {
1140 if(play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
1141 play_tree_iter_free(playtree_iter);
1142 playtree_iter = NULL;
1144 filename = play_tree_iter_get_file(playtree_iter,1);
1148 #ifndef HAVE_NEW_GUI
1149 if(use_gui){
1150 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
1151 use_gui=0;
1153 #else
1154 if(use_gui && !vo_init()){
1155 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
1156 use_gui=0;
1158 if (use_gui && playtree_iter){
1159 char cwd[PATH_MAX+2];
1160 // Remove Playtree and Playtree-Iter from memory as its not used by gui
1161 play_tree_iter_free(playtree_iter);
1162 playtree_iter=NULL;
1164 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
1166 strcat(cwd, "/");
1167 // Prefix relative paths with current working directory
1168 play_tree_add_bpf(playtree, cwd);
1170 // Import initital playtree into gui
1171 import_initial_playtree_into_gui(playtree, mconfig, enqueue);
1173 #endif
1175 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
1176 list_video_out();
1177 exit_player_with_rc(NULL, 0);
1180 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
1181 list_audio_out();
1182 exit_player_with_rc(NULL, 0);
1185 // check codec.conf
1186 if(!codecs_file || !parse_codec_cfg(codecs_file)){
1187 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
1188 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
1189 if(!parse_codec_cfg(NULL)){
1190 mp_msg(MSGT_CPLAYER,MSGL_HINT,MSGTR_CopyCodecsConf);
1191 exit_player_with_rc(NULL, 0);
1193 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
1196 free( mem_ptr ); // release the buffer created by get_path()
1199 #if 0
1200 if(video_codec_list){
1201 int i;
1202 video_codec=video_codec_list[0];
1203 for(i=0;video_codec_list[i];i++)
1204 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
1206 #endif
1207 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
1208 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
1209 list_codecs(1);
1210 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1211 exit_player_with_rc(NULL, 0);
1213 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
1214 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
1215 list_codecs(0);
1216 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1217 exit_player_with_rc(NULL, 0);
1219 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
1220 vfm_help();
1221 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1222 exit_player_with_rc(NULL, 0);
1224 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
1225 afm_help();
1226 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1227 exit_player_with_rc(NULL, 0);
1229 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
1230 af_help();
1231 printf("\n");
1232 exit_player_with_rc(NULL, 0);
1234 #ifdef HAVE_X11
1235 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
1236 fstype_help();
1237 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1238 exit_player_with_rc(NULL, 0);
1240 #endif
1242 #ifdef USE_EDL
1243 if (edl_check_mode() == EDL_ERROR && edl_filename)
1245 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantUseBothModes);
1246 exit_player(NULL);
1247 } else if (edl_filename)
1249 if (edl_records) free_edl(edl_records);
1250 next_edl_record = edl_records = edl_parse_file();
1251 } else if (edl_output_filename)
1253 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
1255 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
1256 edl_output_filename);
1257 exit_player(NULL);
1260 #endif
1262 if(!filename){
1263 if(!use_gui){
1264 // no file/vcd/dvd -> show HELP:
1265 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
1266 exit_player_with_rc(NULL, 0);
1267 } else gui_no_filename=1;
1270 // Many users forget to include command line in bugreports...
1271 if(verbose>0){
1272 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
1273 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
1274 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
1277 mp_msg_set_level(verbose+MSGL_STATUS);
1279 //------ load global data first ------
1281 // check font
1282 #ifdef USE_OSD
1283 #ifdef HAVE_FREETYPE
1284 init_freetype();
1285 #endif
1286 #ifdef HAVE_FONTCONFIG
1287 if(!font_fontconfig)
1289 #endif
1290 if(font_name){
1291 vo_font=read_font_desc(font_name,font_factor,verbose>1);
1292 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
1293 } else {
1294 // try default:
1295 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
1296 free(mem_ptr); // release the buffer created by get_path()
1297 if(!vo_font)
1298 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
1300 #ifdef HAVE_FONTCONFIG
1302 #endif
1303 #endif
1304 vo_init_osd();
1306 #ifdef HAVE_RTC
1307 if(!nortc)
1309 // seteuid(0); /* Can't hurt to try to get root here */
1310 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
1311 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
1312 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
1313 else {
1314 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
1316 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
1317 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
1318 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
1319 close (rtc_fd);
1320 rtc_fd = -1;
1321 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1322 /* variable only by the root */
1323 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
1324 close (rtc_fd);
1325 rtc_fd = -1;
1326 } else
1327 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
1330 #ifdef HAVE_NEW_GUI
1331 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
1332 // and now ? -- Pontscho
1333 if(use_gui) setuid( getuid() ); // strongly test, please check this.
1334 #endif
1335 if(rtc_fd<0)
1336 #endif
1337 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
1338 softsleep?"software":timer_name);
1340 #ifdef USE_TERMCAP
1341 if ( !use_gui ) load_termcap(NULL); // load key-codes
1342 #endif
1344 // ========== Init keyboard FIFO (connection to libvo) ============
1346 // Init input system
1347 current_module = "init_input";
1348 mp_input_init();
1349 #if 0
1350 make_pipe(&keyb_fifo_get,&keyb_fifo_put);
1352 if(keyb_fifo_get > 0)
1353 mp_input_add_key_fd(keyb_fifo_get,1,NULL,NULL);
1354 #else
1355 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
1356 #endif
1357 if(slave_mode)
1358 #ifndef __MINGW32__
1359 mp_input_add_cmd_fd(0,1,NULL,NULL);
1360 #else
1361 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
1362 #endif
1363 else if(!noconsolecontrols)
1364 #ifndef HAVE_NO_POSIX_SELECT
1365 mp_input_add_key_fd(0,1,NULL,NULL);
1366 #else
1367 mp_input_add_key_fd(0,0,NULL,NULL);
1368 #endif
1370 inited_flags|=INITED_INPUT;
1371 current_module = NULL;
1373 #ifdef HAVE_MENU
1374 if(use_menu) {
1375 if(menu_cfg && menu_init(menu_cfg))
1376 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
1377 else {
1378 menu_cfg = get_path("menu.conf");
1379 if(menu_init(menu_cfg))
1380 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
1381 else {
1382 if(menu_init(MPLAYER_CONFDIR "/menu.conf"))
1383 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
1384 else {
1385 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
1386 use_menu = 0;
1391 #endif
1393 /// Catch signals
1394 #ifndef __MINGW32__
1395 signal(SIGCHLD,child_sighandler);
1396 #endif
1398 #ifdef CRASH_DEBUG
1399 prog_path = argv[0];
1400 #endif
1401 //========= Catch terminate signals: ================
1402 // terminate requests:
1403 signal(SIGTERM,exit_sighandler); // kill
1404 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
1406 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1408 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1409 #ifdef ENABLE_SIGHANDLER
1410 // fatal errors:
1411 signal(SIGBUS,exit_sighandler); // bus error
1412 signal(SIGSEGV,exit_sighandler); // segfault
1413 signal(SIGILL,exit_sighandler); // illegal instruction
1414 signal(SIGFPE,exit_sighandler); // floating point exc.
1415 signal(SIGABRT,exit_sighandler); // abort()
1416 #ifdef CRASH_DEBUG
1417 if (crash_debug)
1418 signal(SIGTRAP,exit_sighandler);
1419 #endif
1420 #endif
1422 #ifdef HAVE_NEW_GUI
1423 if(use_gui){
1424 guiInit();
1425 inited_flags|=INITED_GUI;
1426 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
1428 #endif
1430 // ******************* Now, let's see the per-file stuff ********************
1432 play_next_file:
1434 // init global sub numbers
1435 global_sub_size = 0;
1436 { int i; for (i = 0; i < SUB_SOURCES; i++) global_sub_indices[i] = -1; }
1437 global_sub_quiet_osd_hack = 1;
1439 if (filename) load_per_file_config (mconfig, filename);
1441 // We must enable getch2 here to be able to interrupt network connection
1442 // or cache filling
1443 if(!noconsolecontrols && !slave_mode){
1444 if(inited_flags&INITED_GETCH2)
1445 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
1446 else
1447 getch2_enable(); // prepare stdin for hotkeys...
1448 inited_flags|=INITED_GETCH2;
1449 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
1452 // =================== GUI idle loop (STOP state) ===========================
1453 #ifdef HAVE_NEW_GUI
1454 if ( use_gui ) {
1455 file_format=DEMUXER_TYPE_UNKNOWN;
1456 guiGetEvent( guiSetDefaults,0 );
1457 while ( guiIntfStruct.Playing != 1 )
1459 mp_cmd_t* cmd;
1460 usec_sleep(20000);
1461 guiEventHandling();
1462 guiGetEvent( guiReDraw,NULL );
1463 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) guiGetEvent( guiIEvent,(char *)cmd->id );
1465 guiGetEvent( guiSetParameters,NULL );
1466 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
1468 play_tree_t * entry = play_tree_new();
1469 play_tree_add_file( entry,guiIntfStruct.Filename );
1470 if ( playtree ) play_tree_free_list( playtree->child,1 );
1471 else playtree=play_tree_new();
1472 play_tree_set_child( playtree,entry );
1473 if(playtree)
1475 playtree_iter = play_tree_iter_new(playtree,mconfig);
1476 if(playtree_iter)
1478 if(play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
1480 play_tree_iter_free(playtree_iter);
1481 playtree_iter = NULL;
1483 filename = play_tree_iter_get_file(playtree_iter,1);
1488 #endif
1489 //---------------------------------------------------------------------------
1491 if(filename) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing, filename);
1493 //==================== Open VOB-Sub ============================
1495 current_module="vobsub";
1496 if (vobsub_name){
1497 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
1498 if(vo_vobsub==NULL)
1499 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,vobsub_name);
1500 }else if(sub_auto && filename && (strlen(filename)>=5)){
1501 /* try to autodetect vobsub from movie filename ::atmos */
1502 char *buf = malloc((strlen(filename)-3) * sizeof(char));
1503 memset(buf,0,strlen(filename)-3); // make sure string is terminated
1504 strncpy(buf, filename, strlen(filename)-4);
1505 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
1506 free(buf);
1508 if(vo_vobsub){
1509 inited_flags|=INITED_VOBSUB;
1510 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
1511 // check if vobsub requested only to display forced subtitles
1512 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
1514 // setup global sub numbering
1515 global_sub_indices[SUB_SOURCE_VOBSUB] = global_sub_size; // the global # of the first vobsub.
1516 global_sub_size += vobsub_get_indexes_count(vo_vobsub);
1519 //============ Open & Sync STREAM --- fork cache2 ====================
1521 stream=NULL;
1522 demuxer=NULL;
1523 if (d_audio) {
1524 //free_demuxer_stream(d_audio);
1525 d_audio=NULL;
1527 if (d_video) {
1528 //free_demuxer_stream(d_video);
1529 d_video=NULL;
1531 sh_audio=NULL;
1532 sh_video=NULL;
1534 current_module="open_stream";
1535 stream=open_stream(filename,0,&file_format);
1536 if(!stream) { // error...
1537 eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
1538 goto goto_next_file;
1540 inited_flags|=INITED_STREAM;
1542 #ifdef HAVE_NEW_GUI
1543 if ( use_gui ) guiGetEvent( guiSetStream,(char *)stream );
1544 #endif
1546 if(file_format == DEMUXER_TYPE_PLAYLIST) {
1547 play_tree_t* entry;
1548 // Handle playlist
1549 current_module="handle_playlist";
1550 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",filename);
1551 entry = parse_playtree(stream,0);
1552 eof=playtree_add_playlist(entry);
1553 goto goto_next_file;
1555 stream->start_pos+=seek_to_byte;
1557 if(stream_dump_type==5){
1558 unsigned char buf[4096];
1559 int len;
1560 FILE *f;
1561 current_module="dumpstream";
1562 if(stream->type==STREAMTYPE_STREAM && stream->fd<0){
1563 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
1564 exit_player(MSGTR_Exit_error);
1566 stream_reset(stream);
1567 stream_seek(stream,stream->start_pos);
1568 f=fopen(stream_dump_name,"wb");
1569 if(!f){
1570 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
1571 exit_player(MSGTR_Exit_error);
1573 while(!stream->eof){
1574 len=stream_read(stream,buf,4096);
1575 if(len>0) {
1576 if(fwrite(buf,len,1,f) != 1) {
1577 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
1578 exit_player(MSGTR_Exit_error);
1582 if(fclose(f)) {
1583 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
1584 exit_player(MSGTR_Exit_error);
1586 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
1587 exit_player_with_rc(MSGTR_Exit_eof, 0);
1590 #ifdef USE_DVDREAD
1591 if(stream->type==STREAMTYPE_DVD){
1592 current_module="dvd lang->id";
1593 if(audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
1594 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
1595 // setup global sub numbering
1596 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
1597 global_sub_size += dvd_number_of_subs(stream);
1598 current_module=NULL;
1600 #endif
1602 #ifdef USE_DVDNAV
1603 if (stream->type==STREAMTYPE_DVDNAV) stream_cache_size=0; // must disable caching...
1604 #endif
1606 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
1607 #ifdef HAS_DVBIN_SUPPORT
1608 goto_enable_cache:
1609 #endif
1610 if(stream_cache_size>0){
1611 current_module="enable_cache";
1612 if(!stream_enable_cache(stream,stream_cache_size*1024,stream_cache_size*1024*(stream_cache_min_percent / 100.0),stream_cache_size*1024*(stream_cache_prefill_percent / 100.0)))
1613 if((eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
1616 //============ Open DEMUXERS --- DETECT file type =======================
1617 current_module="demux_open";
1619 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
1621 // HACK to get MOV Reference Files working
1623 if (demuxer && demuxer->type==DEMUXER_TYPE_PLAYLIST)
1625 unsigned char* playlist_entry;
1626 play_tree_t *list = NULL, *entry = NULL;
1628 current_module="handle_demux_playlist";
1629 while (ds_get_packet(demuxer->video,&playlist_entry)>0)
1631 char *temp, *bname;
1633 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",playlist_entry);
1635 bname=mp_basename(playlist_entry);
1636 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
1637 continue;
1639 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
1640 continue;
1642 entry = play_tree_new();
1644 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
1646 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1)*sizeof(char));
1647 if (temp)
1649 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
1650 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
1651 strcat(temp, playlist_entry);
1652 play_tree_add_file(entry,temp);
1653 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
1654 free(temp);
1657 else
1658 play_tree_add_file(entry,playlist_entry);
1660 if(!list)
1661 list = entry;
1662 else
1663 play_tree_append_entry(list,entry);
1665 free_demuxer(demuxer);
1666 demuxer = NULL;
1668 if (list)
1670 entry = play_tree_new();
1671 play_tree_set_child(entry,list);
1672 eof=playtree_add_playlist(entry);
1673 goto goto_next_file;
1677 if(!demuxer)
1679 #if 0
1680 play_tree_t* entry;
1681 // Handle playlist
1682 current_module="handle_playlist";
1683 switch(stream->type){
1684 case STREAMTYPE_VCD:
1685 case STREAMTYPE_DVD:
1686 case STREAMTYPE_DVDNAV:
1687 case STREAMTYPE_CDDA:
1688 case STREAMTYPE_VCDBINCUE:
1689 // don't try to parse raw media as playlist, it's unlikely
1690 goto goto_next_file;
1692 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FallingBackOnPlaylist,filename);
1693 stream_reset(stream);
1694 stream_seek(stream,stream->start_pos);
1695 entry = parse_playtree(stream,0);
1696 if(!entry)
1697 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
1698 else
1699 eof=playtree_add_playlist(entry);
1700 #endif
1701 goto goto_next_file;
1703 inited_flags|=INITED_DEMUXER;
1705 #ifdef HAVE_MATROSKA
1706 if (demuxer->type==DEMUXER_TYPE_MATROSKA) {
1707 // setup global sub numbering
1708 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
1709 global_sub_size += demux_mkv_num_subs(demuxer);
1711 #endif
1712 #ifdef HAVE_OGGVORBIS
1713 if (demuxer->type==DEMUXER_TYPE_OGG) {
1714 // setup global sub numbering
1715 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
1716 global_sub_size += demux_ogg_num_subs(demuxer);
1718 #endif
1720 current_module="demux_open2";
1722 //file_format=demuxer->file_format;
1724 d_audio=demuxer->audio;
1725 d_video=demuxer->video;
1726 d_dvdsub=demuxer->sub;
1728 // DUMP STREAMS:
1729 if((stream_dump_type)&&(stream_dump_type<4)){
1730 FILE *f;
1731 demux_stream_t *ds=NULL;
1732 current_module="dump";
1733 // select stream to dump
1734 switch(stream_dump_type){
1735 case 1: ds=d_audio;break;
1736 case 2: ds=d_video;break;
1737 case 3: ds=d_dvdsub;break;
1739 if(!ds){
1740 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
1741 exit_player(MSGTR_Exit_error);
1743 // disable other streams:
1744 if(d_audio && d_audio!=ds) {ds_free_packs(d_audio); d_audio->id=-2; }
1745 if(d_video && d_video!=ds) {ds_free_packs(d_video); d_video->id=-2; }
1746 if(d_dvdsub && d_dvdsub!=ds) {ds_free_packs(d_dvdsub); d_dvdsub->id=-2; }
1747 // let's dump it!
1748 f=fopen(stream_dump_name,"wb");
1749 if(!f){
1750 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
1751 exit_player(MSGTR_Exit_error);
1753 while(!ds->eof){
1754 unsigned char* start;
1755 int in_size=ds_get_packet(ds,&start);
1756 if( (demuxer->file_format==DEMUXER_TYPE_AVI || demuxer->file_format==DEMUXER_TYPE_ASF || demuxer->file_format==DEMUXER_TYPE_MOV)
1757 && stream_dump_type==2) fwrite(&in_size,1,4,f);
1758 if(in_size>0) fwrite(start,in_size,1,f);
1760 fclose(f);
1761 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
1762 exit_player_with_rc(MSGTR_Exit_eof, 0);
1765 sh_audio=d_audio->sh;
1766 sh_video=d_video->sh;
1768 if(sh_video){
1770 current_module="video_read_properties";
1771 if(!video_read_properties(sh_video)) {
1772 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
1773 sh_video=d_video->sh=NULL;
1774 } else {
1775 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
1776 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
1777 sh_video->fps,sh_video->frametime
1780 /* need to set fps here for output encoders to pick it up in their init */
1781 if(force_fps){
1782 sh_video->fps=force_fps;
1783 sh_video->frametime=1.0f/sh_video->fps;
1785 vo_fps = sh_video->fps;
1786 #ifdef HAVE_X11
1787 vo_mouse_timer_const=(int)sh_video->fps;
1788 #endif
1790 if(!sh_video->fps && !force_fps){
1791 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
1792 sh_video=d_video->sh=NULL;
1798 fflush(stdout);
1800 if(!sh_video && !sh_audio){
1801 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
1802 #ifdef HAS_DVBIN_SUPPORT
1803 if((stream->type == STREAMTYPE_DVB) && stream->priv)
1805 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
1806 if(priv->is_on)
1808 int dir;
1809 int v = last_dvb_step;
1810 if(v > 0)
1811 dir = DVB_CHANNEL_HIGHER;
1812 else
1813 dir = DVB_CHANNEL_LOWER;
1815 if(dvb_step_channel(priv, dir))
1817 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
1818 cache_uninit(stream);
1819 goto goto_enable_cache;
1823 #endif
1824 goto goto_next_file; // exit_player(MSGTR_Exit_error);
1827 /* display clip info */
1828 demux_info_print(demuxer);
1830 //================== Read SUBTITLES (DVD & TEXT) ==========================
1831 if(vo_spudec==NULL && sh_video &&
1832 (stream->type==STREAMTYPE_DVD || demuxer->type==DEMUXER_TYPE_MATROSKA ||
1833 d_dvdsub->id >= 0)){
1835 if (spudec_ifo) {
1836 unsigned int palette[16], width, height;
1837 current_module="spudec_init_vobsub";
1838 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1839 vo_spudec=spudec_new_scaled(palette, width, height);
1842 #ifdef USE_DVDNAV
1843 if (vo_spudec==NULL && stream->type==STREAMTYPE_DVDNAV) {
1844 current_module="spudec_init_dvdnav";
1845 vo_spudec=spudec_new_scaled(dvdnav_stream_get_palette((dvdnav_priv_t*)(stream->priv)),
1846 sh_video->disp_w, sh_video->disp_h);
1848 #endif
1850 #ifdef USE_DVDREAD
1851 if (vo_spudec==NULL && stream->type==STREAMTYPE_DVD) {
1852 current_module="spudec_init_dvdread";
1853 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(stream->priv))->cur_pgc->palette,
1854 sh_video->disp_w, sh_video->disp_h);
1856 #endif
1858 #ifdef HAVE_MATROSKA
1859 if ((vo_spudec == NULL) && (demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1860 (d_dvdsub->sh != NULL) && (((mkv_sh_sub_t *)d_dvdsub->sh)->type == 'v')) {
1861 mkv_sh_sub_t *mkv_sh_sub = (mkv_sh_sub_t *)d_dvdsub->sh;
1862 current_module = "spudec_init_matroska";
1863 vo_spudec =
1864 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1865 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1866 mkv_sh_sub->height);
1867 forced_subs_only = mkv_sh_sub->forced_subs_only;
1869 #endif
1871 if (vo_spudec==NULL) {
1872 current_module="spudec_init_normal";
1873 vo_spudec=spudec_new_scaled(NULL, sh_video->disp_w, sh_video->disp_h);
1874 spudec_set_font_factor(vo_spudec,font_factor);
1877 if (vo_spudec!=NULL)
1878 inited_flags|=INITED_SPUDEC;
1882 // Apply current settings for forced subs
1883 if (vo_spudec!=NULL)
1884 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
1886 #ifdef USE_SUB
1887 if(sh_video) {
1888 // after reading video params we should load subtitles because
1889 // we know fps so now we can adjust subtitles time to ~6 seconds AST
1890 // check .sub
1891 current_module="read_subtitles_file";
1892 if(sub_name){
1893 for (i = 0; sub_name[i] != NULL; ++i)
1894 add_subtitles (sub_name[i], sh_video->fps, 0);
1896 if(sub_auto) { // auto load sub file ...
1897 char *psub = get_path( "sub/" );
1898 char **tmp = sub_filenames((psub ? psub : ""), filename);
1899 char **tmp2 = tmp;
1900 free(psub); // release the buffer created by get_path() above
1901 while (*tmp2)
1902 add_subtitles (*tmp2++, sh_video->fps, 0);
1903 free(tmp);
1904 if (set_of_sub_size == 0)
1906 add_subtitles (mem_ptr=get_path("default.sub"), sh_video->fps, 1);
1907 free(mem_ptr); // release the buffer created by get_path()
1909 if (set_of_sub_size > 0)
1910 add_subtitles (NULL, sh_video->fps, 1);
1912 if (set_of_sub_size > 0) {
1913 // setup global sub numbering
1914 global_sub_indices[SUB_SOURCE_SUBS] = global_sub_size; // the global # of the first sub.
1915 global_sub_size += set_of_sub_size;
1918 #endif
1920 if (global_sub_size) {
1921 // find the best sub to use
1922 if (vobsub_id >= 0) {
1923 // if user asks for a vobsub id, use that first.
1924 global_sub_pos = global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_id;
1925 } else if (dvdsub_id >= 0 && global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1926 // if user asks for a dvd sub id, use that next.
1927 global_sub_pos = global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
1928 } else if (global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1929 // if there are text subs to use, use those. (autosubs come last here)
1930 global_sub_pos = global_sub_indices[SUB_SOURCE_SUBS];
1932 } else if (global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1933 // if nothing else works, get subs from the demuxer.
1934 global_sub_pos = global_sub_indices[SUB_SOURCE_DEMUX];
1936 } else {
1937 // nothing worth doing automatically.
1938 global_sub_pos = -1;
1940 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1941 global_sub_pos--;
1942 mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
1945 //================== Init AUDIO (codec) ==========================
1946 if(sh_audio){
1947 // Go through the codec.conf and find the best codec...
1948 current_module="init_audio_codec";
1949 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1950 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
1951 sh_audio=d_audio->sh=NULL; // failed to init :(
1952 } else
1953 inited_flags|=INITED_ACODEC;
1954 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1957 if(identify) {
1958 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_FILENAME=%s\n", filename);
1959 if (sh_video) {
1960 /* Assume FOURCC if all bytes >= 0x20 (' ') */
1961 if (sh_video->format >= 0x20202020)
1962 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", &sh_video->format);
1963 else
1964 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", sh_video->format);
1965 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", sh_video->i_bps*8);
1966 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", sh_video->disp_w);
1967 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", sh_video->disp_h);
1968 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", sh_video->fps);
1969 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", sh_video->aspect);
1971 if (sh_audio) {
1972 if (sh_audio->codec)
1973 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", sh_audio->codec->name);
1974 /* Assume FOURCC if all bytes >= 0x20 (' ') */
1975 if (sh_audio->format >= 0x20202020)
1976 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", &sh_audio->format);
1977 else
1978 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", sh_audio->format);
1979 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", sh_audio->i_bps*8);
1980 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_RATE=%d\n", sh_audio->samplerate);
1981 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_NCH=%d\n", sh_audio->channels);
1983 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_LENGTH=%ld\n", demuxer_get_time_length(demuxer));
1986 if(!sh_video) goto main; // audio-only
1988 //================== Init VIDEO (codec & libvo) ==========================
1989 if(!fixed_vo || !(inited_flags&INITED_VO)){
1990 current_module="preinit_libvo";
1992 vo_config_count=0;
1993 //if((video_out->preinit(vo_subdevice))!=0){
1994 if(!(video_out=init_best_video_out(video_driver_list))){
1995 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1996 goto goto_next_file; // exit_player(MSGTR_Exit_error);
1998 sh_video->video_out=video_out;
1999 inited_flags|=INITED_VO;
2002 current_module="init_video_filters";
2004 char* vf_arg[] = { "_oldargs_", (char*)video_out , NULL };
2005 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
2007 #ifdef HAVE_MENU
2008 if(use_menu) {
2009 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2010 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2011 if(!vf_menu) {
2012 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2013 use_menu = 0;
2016 if(vf_menu)
2017 sh_video->vfilter=(void*)append_filters(vf_menu);
2018 else
2019 #endif
2020 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
2022 current_module="init_video_codec";
2024 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2025 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2026 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2028 if(!sh_video->inited){
2029 if(!fixed_vo) uninit_player(INITED_VO);
2030 if(!sh_audio) goto goto_next_file;
2031 sh_video = d_video->sh = NULL;
2032 goto main; // exit_player(MSGTR_Exit_error);
2035 inited_flags|=INITED_VCODEC;
2037 if(auto_quality>0){
2038 // Auto quality option enabled
2039 output_quality=get_video_quality_max(sh_video);
2040 if(auto_quality>output_quality) auto_quality=output_quality;
2041 else output_quality=auto_quality;
2042 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2043 set_video_quality(sh_video,output_quality);
2046 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2048 current_module="init_vo";
2050 if(vo_flags & 0x08 && vo_spudec)
2051 spudec_set_hw_spu(vo_spudec,video_out);
2053 #ifdef HAVE_FREETYPE
2054 force_load_font = 1;
2055 #endif
2057 //================== MAIN: ==========================
2058 main:
2059 current_module="main";
2061 // If there is no video OSD has to be disabled.
2062 // In case of playing a playtree we have to restore the
2063 // old OSD level after playing one or more audio-only files.
2064 if(!sh_video && osd_level >= 0) { // save OSD level only once
2065 osd_level_saved = osd_level;
2066 osd_level = 0;
2067 } else if (osd_level_saved > -1) { // if there is a saved OSD level, restore it
2068 osd_level = osd_level_saved;
2069 osd_level_saved = -1;
2072 fflush(stdout);
2074 #ifdef HAVE_NEW_GUI
2075 if ( use_gui )
2077 if ( sh_audio ) guiIntfStruct.AudioType=sh_audio->channels; else guiIntfStruct.AudioType=0;
2078 if ( !sh_video && sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
2079 guiGetEvent( guiSetFileFormat,(char *)demuxer->file_format );
2080 if ( guiGetEvent( guiSetValues,(char *)sh_video ) ) goto goto_next_file;
2081 guiGetEvent( guiSetDemuxer,(char *)demuxer );
2083 #endif
2086 //int frame_corr_num=0; //
2087 //float v_frame=0; // Video
2088 float time_frame=0; // Timer
2089 //float num_frames=0; // number of frames played
2090 int grab_frames=0;
2091 char osd_text_buffer[64];
2092 char osd_show_text_buffer[64];
2093 int drop_frame=0; // current dropping status
2094 int dropped_frames=0; // how many frames dropped since last non-dropped frame
2095 int too_slow_frame_cnt=0;
2096 int too_fast_frame_cnt=0;
2097 // for auto-quality:
2098 float AV_delay=0; // average of A-V timestamp differences
2099 double vdecode_time;
2100 unsigned int lastframeout_ts=0;
2101 /*float time_frame_corr_avg=0;*/ /* unused */
2103 float next_frame_time=0;
2104 int frame_time_remaining=0; // flag
2105 int blit_frame=0;
2107 osd_text_buffer[0]=0;
2109 //================ SETUP AUDIO ==========================
2111 if(sh_audio){
2112 //const ao_info_t *info=audio_out->info;
2113 current_module="af_preinit";
2114 ao_data.samplerate=force_srate?force_srate:sh_audio->samplerate*playback_speed;
2115 ao_data.channels=audio_output_channels?audio_output_channels:sh_audio->channels;
2116 ao_data.format=audio_output_format?audio_output_format:sh_audio->sample_format;
2117 #if 1
2118 if(!preinit_audio_filters(sh_audio,
2119 // input:
2120 (int)(sh_audio->samplerate*playback_speed),
2121 sh_audio->channels, sh_audio->sample_format,
2122 // output:
2123 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
2124 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
2126 #endif
2127 current_module="ao2_init";
2128 if(!(audio_out=init_best_audio_out(audio_driver_list,
2129 0, // plugin flag
2130 force_srate?force_srate:ao_data.samplerate,
2131 audio_output_channels?audio_output_channels:ao_data.channels,
2132 audio_output_format?audio_output_format:ao_data.format,0))){
2133 // FAILED:
2134 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
2135 uninit_player(INITED_ACODEC); // close codec
2136 sh_audio=d_audio->sh=NULL; // -> nosound
2137 } else {
2138 // SUCCESS:
2139 inited_flags|=INITED_AO;
2140 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bps)\n",
2141 audio_out->info->short_name,
2142 ao_data.samplerate, ao_data.channels,
2143 af_fmt2str_short(ao_data.format),
2144 af_fmt2bits(ao_data.format)/8 );
2145 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
2146 audio_out->info->name, audio_out->info->author);
2147 if(strlen(audio_out->info->comment) > 0)
2148 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", audio_out->info->comment);
2149 // init audio filters:
2150 #if 1
2151 current_module="af_init";
2152 if(!build_afilter_chain(sh_audio, &ao_data)) {
2153 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
2154 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
2155 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
2156 // sh_audio=d_audio->sh=NULL; // -> nosound
2158 #endif
2160 mixer.audio_out = audio_out;
2161 mixer.volstep = volstep;
2164 current_module="av_init";
2166 if(sh_video) sh_video->timer=0;
2167 if(sh_audio) sh_audio->delay=-audio_delay;
2169 if(!sh_audio){
2170 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
2171 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",d_audio->packs);
2172 ds_free_packs(d_audio); // free buffered chunks
2173 d_audio->id=-2; // do not read audio chunks
2174 //uninit_player(INITED_AO); // close device
2176 if(!sh_video){
2177 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
2178 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",d_video->packs);
2179 ds_free_packs(d_video);
2180 d_video->id=-2;
2181 //if(!fixed_vo) uninit_player(INITED_VO);
2184 if (!sh_video && !sh_audio)
2185 goto goto_next_file;
2187 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
2188 if(force_fps && sh_video){
2189 vo_fps = sh_video->fps=force_fps;
2190 sh_video->frametime=1.0f/sh_video->fps;
2191 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,sh_video->fps,sh_video->frametime);
2194 //==================== START PLAYING =======================
2196 if(loop_times>1) loop_times--; else
2197 if(loop_times==1) loop_times = -1;
2199 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);fflush(stdout);
2201 InitTimer();
2203 #ifdef USE_DVDNAV
2204 if (stream->type==STREAMTYPE_DVDNAV) {
2205 dvdnav_stream_fullstart((dvdnav_priv_t *)stream->priv);
2207 #endif
2209 total_time_usage_start=GetTimer();
2210 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
2211 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
2212 play_n_frames=play_n_frames_mf;
2214 if(play_n_frames==0){
2215 eof=PT_NEXT_ENTRY; goto goto_next_file;
2218 while(!eof){
2219 float aq_sleep_time=0;
2221 if(play_n_frames>=0){
2222 --play_n_frames;
2223 if(play_n_frames<0) eof = PT_NEXT_ENTRY;
2226 /*========================== PLAY AUDIO ============================*/
2228 while(sh_audio){
2229 unsigned int t;
2230 double tt;
2231 int playsize;
2233 current_module="play_audio";
2235 ao_data.pts=((sh_video?sh_video->timer:0)+sh_audio->delay)*90000.0;
2236 playsize=audio_out->get_space();
2238 // handle audio-only case:
2239 if(playsize < ao_data.outburst &&
2240 !sh_video) { // buffer is full, do not block here!!!
2241 usec_sleep(10000); // Wait a tick before retry
2242 continue;
2245 if(playsize>MAX_OUTBURST) playsize=MAX_OUTBURST; // we shouldn't exceed it!
2247 // Fill buffer if needed:
2248 current_module="decode_audio"; // Enter AUDIO decoder module
2249 t=GetTimer();
2250 while(sh_audio->a_out_buffer_len<playsize && !d_audio->eof){
2251 int ret=decode_audio(sh_audio,&sh_audio->a_out_buffer[sh_audio->a_out_buffer_len],
2252 playsize-sh_audio->a_out_buffer_len,sh_audio->a_out_buffer_size-sh_audio->a_out_buffer_len);
2253 if(ret<=0) break; // EOF?
2254 sh_audio->a_out_buffer_len+=ret;
2256 t=GetTimer()-t;
2257 tt = t*0.000001f; audio_time_usage+=tt;
2258 if(playsize>sh_audio->a_out_buffer_len) playsize=sh_audio->a_out_buffer_len;
2260 // play audio:
2261 current_module="play_audio";
2262 playsize=audio_out->play(sh_audio->a_out_buffer,playsize,0);
2264 if(playsize>0){
2265 sh_audio->a_out_buffer_len-=playsize;
2266 memmove(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[playsize],sh_audio->a_out_buffer_len);
2267 sh_audio->delay+=playback_speed*playsize/((float)((ao_data.bps && sh_audio->afilter) ?
2268 ao_data.bps : sh_audio->o_bps));
2271 break;
2272 } // while(sh_audio)
2274 if(!sh_video) {
2275 // handle audio-only case:
2276 if(!quiet) {
2277 float a_pos = sh_audio->delay - audio_out->get_delay() * playback_speed;
2278 print_status(a_pos, 0, 0);
2280 if(d_audio->eof) eof = PT_NEXT_ENTRY;
2282 } else {
2284 /*========================== PLAY VIDEO ============================*/
2286 float frame_time=next_frame_time;
2288 vo_pts=sh_video->timer*90000.0;
2289 vo_fps=sh_video->fps;
2291 if(!frame_time_remaining){
2292 //-------------------- Decode a frame: -----------------------
2293 vdecode_time=video_time_usage;
2294 while(1)
2295 { unsigned char* start=NULL;
2296 int in_size;
2297 // get it!
2298 current_module="video_read_frame";
2299 in_size=video_read_frame(sh_video,&next_frame_time,&start,force_fps);
2300 if(in_size<0){ eof=1; break; }
2301 if(in_size>max_framesize) max_framesize=in_size; // stats
2302 sh_video->timer+=frame_time;
2303 if(sh_audio) sh_audio->delay-=frame_time;
2304 time_frame+=frame_time; // for nosound
2305 // check for frame-drop:
2306 current_module="check_framedrop";
2307 if(sh_audio && !d_audio->eof){
2308 float delay=playback_speed*audio_out->get_delay();
2309 float d=delay-sh_audio->delay;
2310 // we should avoid dropping to many frames in sequence unless we
2311 // are too late. and we allow 100ms A-V delay here:
2312 if(d<-dropped_frames*frame_time-0.100 && osd_function != OSD_PAUSE){
2313 drop_frame=frame_dropping;
2314 ++drop_frame_cnt;
2315 ++dropped_frames;
2316 } else {
2317 drop_frame=dropped_frames=0;
2319 ++total_frame_cnt;
2321 // decode:
2322 current_module="decode_video";
2323 // printf("Decode! %p %d \n",start,in_size);
2324 blit_frame=decode_video(sh_video,start,in_size,drop_frame);
2325 break;
2327 vdecode_time=video_time_usage-vdecode_time;
2328 //------------------------ frame decoded. --------------------
2330 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
2332 if(sh_video->vf_inited<0){
2333 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_NotInitializeVOPorVO);
2334 eof=1; goto goto_next_file;
2339 // ==========================================================================
2341 // current_module="draw_osd";
2342 // if(vo_config_count) video_out->draw_osd();
2344 #ifdef HAVE_NEW_GUI
2345 if(use_gui) guiEventHandling();
2346 #endif
2348 current_module="calc_sleep_time";
2350 #if 0
2351 { // debug frame dropping code
2352 float delay=audio_out->get_delay();
2353 mp_msg(MSGT_AVSYNC,MSGL_V,"\r[V] %5.3f [A] %5.3f => {%5.3f} (%5.3f) [%d] \n",
2354 sh_video->timer,sh_audio->timer-delay,
2355 sh_video->timer-(sh_audio->timer-delay),
2356 delay,drop_frame);
2358 #endif
2360 if(drop_frame && !frame_time_remaining && !autosync){
2362 * Note: time_frame should not be forced to 0 in autosync mode.
2363 * It is used as a cumulative counter to predict and correct the
2364 * delay measurements from the audio driver. time_frame is already
2365 * < 0, so the "time to sleep" code does not actually sleep. Also,
2366 * blit_frame is already 0 because drop_frame was true when
2367 * decode_video was called (which causes it to set blit_frame to 0.)
2368 * When autosync==0, the default behavior is still completely unchanged.
2371 time_frame=0; // don't sleep!
2372 blit_frame=0; // don't display!
2374 } else {
2376 // It's time to sleep...
2378 frame_time_remaining=0;
2379 time_frame-=GetRelativeTime(); // reset timer
2381 if(sh_audio && !d_audio->eof){
2382 float delay=playback_speed*audio_out->get_delay();
2383 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"delay=%f\n",delay);
2385 if (autosync){
2387 * Adjust this raw delay value by calculating the expected
2388 * delay for this frame and generating a new value which is
2389 * weighted between the two. The higher autosync is, the
2390 * closer to the delay value gets to that which "-nosound"
2391 * would have used, and the longer it will take for A/V
2392 * sync to settle at the right value (but it eventually will.)
2393 * This settling time is very short for values below 100.
2395 float predicted = sh_audio->delay+time_frame;
2396 float difference = delay - predicted;
2397 delay = predicted + difference / (float)autosync;
2400 time_frame=delay-sh_audio->delay;
2402 // delay = amount of audio buffered in soundcard/driver
2403 if(delay>0.25) delay=0.25; else
2404 if(delay<0.10) delay=0.10;
2405 if(time_frame>delay*0.6){
2406 // sleep time too big - may cause audio drops (buffer underrun)
2407 frame_time_remaining=1;
2408 time_frame=delay*0.5;
2411 } else {
2413 // NOSOUND:
2414 if( (time_frame<-3*frame_time || time_frame>3*frame_time) || benchmark)
2415 time_frame=0;
2419 // if(verbose>1)printf("sleep: %5.3f a:%6.3f v:%6.3f \n",time_frame,sh_audio->timer,sh_video->timer);
2421 aq_sleep_time+=time_frame;
2423 } // !drop_frame
2425 //============================== SLEEP: ===================================
2427 time_frame/=playback_speed;
2429 // flag 256 means: libvo driver does its timing (dvb card)
2430 if(time_frame>0.001 && !(vo_flags&256)){
2432 #ifdef HAVE_RTC
2433 if(rtc_fd>=0){
2434 // -------- RTC -----------
2435 current_module="sleep_rtc";
2436 while (time_frame > 0.000) {
2437 unsigned long rtc_ts;
2438 if (read (rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
2439 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
2440 time_frame-=GetRelativeTime();
2442 } else
2443 #endif
2445 // -------- TIMER + SOFTSLEEP -----------
2446 float min=softsleep?0.021:0.005;
2447 current_module="sleep_timer";
2448 while(time_frame>min){
2449 if(time_frame<=0.020)
2450 usec_sleep(0); // sleeps 1 clock tick (10ms)!
2451 else
2452 usec_sleep(1000000*(time_frame-0.020));
2453 time_frame-=GetRelativeTime();
2455 if(softsleep){
2456 current_module="sleep_soft";
2457 if(time_frame<0) mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
2458 while(time_frame>0) time_frame-=GetRelativeTime(); // burn the CPU
2464 //if(!frame_time_remaining){ // should we display the frame now?
2466 //====================== FLIP PAGE (VIDEO BLT): =========================
2468 current_module="vo_check_events";
2469 if(vo_config_count) video_out->check_events();
2471 current_module="flip_page";
2472 if (!frame_time_remaining) {
2473 if(blit_frame){
2474 unsigned int t2=GetTimer();
2475 double tt;
2476 float j;
2477 #define FRAME_LAG_WARN 0.2
2478 j = ((float)t2 - lastframeout_ts) / 1000000;
2479 lastframeout_ts = GetTimer();
2480 if (j < frame_time + frame_time * -FRAME_LAG_WARN)
2481 too_fast_frame_cnt++;
2482 /* printf ("PANIC: too fast frame (%.3f)!\n", j); */
2483 else if (j > frame_time + frame_time * FRAME_LAG_WARN)
2484 too_slow_frame_cnt++;
2485 /* printf ("PANIC: too slow frame (%.3f)!\n", j); */
2487 if(vo_config_count) video_out->flip_page();
2488 // usec_sleep(50000); // test only!
2489 t2=GetTimer()-t2;
2490 tt = t2*0.000001f;
2491 vout_time_usage+=tt;
2492 } else {
2494 Well, no blitting is needed, but some devices (such as yuv4mpeg) must output frame
2495 otherwise A/V desync will occur. -- Alvieboy
2497 if (vo_config_count)
2498 video_out->control(VOCTRL_DUPLICATE_FRAME, NULL);
2501 //====================== A-V TIMESTAMP CORRECTION: =========================
2503 current_module="av_sync";
2505 if(sh_audio){
2506 float a_pts=0;
2507 float v_pts=0;
2509 // unplayed bytes in our and soundcard/dma buffer:
2510 float delay=playback_speed*audio_out->get_delay()+(float)sh_audio->a_buffer_len/(float)sh_audio->o_bps;
2512 if (autosync){
2514 * If autosync is enabled, the value for delay must be calculated
2515 * a bit differently. It is set only to the difference between
2516 * the audio and video timers. Any attempt to include the real
2517 * or corrected delay causes the pts_correction code below to
2518 * try to correct for the changes in delay which autosync is
2519 * trying to measure. This keeps the two from competing, but still
2520 * allows the code to correct for PTS drift *only*. (Using a delay
2521 * value here, even a "corrected" one, would be incompatible with
2522 * autosync mode.)
2524 delay=sh_audio->delay;
2525 delay+=(float)sh_audio->a_buffer_len/(float)sh_audio->o_bps;
2528 #if 0
2529 if(pts_from_bps){
2530 // PTS = sample_no / samplerate
2531 unsigned int samples=
2532 // (sh_audio->audio.dwSampleSize)?
2533 // ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
2534 ds_tell_block(d_audio); // <- used for VBR audio
2535 samples+=sh_audio->audio.dwStart; // offset
2536 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
2537 delay_corrected=1;
2538 a_pts-=(sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
2539 } else
2540 #endif
2542 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
2543 a_pts=d_audio->pts;
2544 if(!delay_corrected) if(a_pts) delay_corrected=1;
2545 #if 0
2546 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n#X# pts=%5.3f ds_pts=%5.3f buff=%5.3f total=%5.3f\n",
2547 a_pts,
2548 ds_tell_pts(d_audio)/(float)sh_audio->i_bps,
2549 -sh_audio->a_in_buffer_len/(float)sh_audio->i_bps,
2550 a_pts+(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps);
2551 #endif
2552 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
2554 v_pts=sh_video ? sh_video->pts : d_video->pts;
2556 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"### A:%8.3f (%8.3f) V:%8.3f A-V:%7.4f \n",a_pts,a_pts-audio_delay-delay,v_pts,(a_pts-delay-audio_delay)-v_pts);
2558 if(delay_corrected){
2559 static int drop_message=0;
2560 float x;
2561 AV_delay=(a_pts-delay-audio_delay)-v_pts;
2562 if(AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
2563 ++drop_message;
2564 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
2566 x=AV_delay*0.1f;
2567 if(x<-max_pts_correction) x=-max_pts_correction; else
2568 if(x> max_pts_correction) x= max_pts_correction;
2569 if(default_max_pts_correction>=0)
2570 max_pts_correction=default_max_pts_correction;
2571 else
2572 max_pts_correction=sh_video->frametime*0.10; // +-10% of time
2573 if(!frame_time_remaining){ sh_audio->delay+=x; c_total+=x;} // correction
2574 if(!quiet)
2575 print_status(a_pts - audio_delay - delay, AV_delay, c_total);
2578 } else {
2579 // No audio:
2581 if(!quiet)
2582 print_status(0, 0, 0);
2586 //============================ Auto QUALITY ============================
2588 /*Output quality adjustments:*/
2589 if(auto_quality>0){
2590 current_module="autoq";
2591 // float total=0.000001f * (GetTimer()-aq_total_time);
2592 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
2593 if(output_quality<auto_quality && aq_sleep_time>0)
2594 ++output_quality;
2595 else
2596 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
2597 if(output_quality>1 && aq_sleep_time<0)
2598 --output_quality;
2599 else
2600 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
2601 output_quality=0;
2602 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
2603 set_video_quality(sh_video,output_quality);
2606 } // end if(sh_video)
2608 //============================ Handle PAUSE ===============================
2610 current_module="pause";
2612 #ifdef USE_OSD
2613 if(osd_visible){
2614 if (!--osd_visible){
2615 vo_osd_progbar_type=-1; // disable
2616 vo_osd_changed(OSDTYPE_PROGBAR);
2617 if (osd_function != OSD_PAUSE)
2618 osd_function = OSD_PLAY;
2621 #endif
2623 if(osd_function==OSD_PAUSE){
2624 mp_cmd_t* cmd;
2625 if(!quiet) {
2626 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2627 fflush(stdout);
2629 #ifdef HAVE_NEW_GUI
2630 if(use_gui) guiGetEvent( guiCEvent,(char *)guiSetPause );
2631 #endif
2632 if (video_out && sh_video && vo_config_count)
2633 video_out->control(VOCTRL_PAUSE, NULL);
2635 if (audio_out && sh_audio)
2636 audio_out->pause(); // pause audio, keep data if possible
2638 while( (cmd = mp_input_get_cmd(20,1,1)) == NULL) {
2639 if(sh_video && video_out && vo_config_count) video_out->check_events();
2640 #ifdef HAVE_NEW_GUI
2641 if(use_gui){
2642 guiEventHandling();
2643 guiGetEvent( guiReDraw,NULL );
2644 if(guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos)) break;
2646 #endif
2647 #ifdef HAVE_MENU
2648 if(vf_menu)
2649 vf_menu_pause_update(vf_menu);
2650 #endif
2651 usec_sleep(20000);
2653 if (cmd && cmd->id == MP_CMD_PAUSE) {
2654 cmd = mp_input_get_cmd(0,1,0);
2655 mp_cmd_free(cmd);
2657 osd_function=OSD_PLAY;
2658 if (audio_out && sh_audio)
2659 audio_out->resume(); // resume audio
2660 if (video_out && sh_video && vo_config_count)
2661 video_out->control(VOCTRL_RESUME, NULL); // resume video
2662 (void)GetRelativeTime(); // keep TF around FT in next cycle
2663 #ifdef HAVE_NEW_GUI
2664 if (use_gui)
2666 if ( guiIntfStruct.Playing == guiSetStop ) goto goto_next_file;
2667 guiGetEvent( guiCEvent,(char *)guiSetPlay );
2669 #endif
2672 // handle -sstep
2673 if(step_sec>0) {
2674 osd_function=OSD_FFW;
2675 rel_seek_secs+=step_sec;
2678 #ifdef USE_DVDNAV
2679 if (stream->type==STREAMTYPE_DVDNAV && dvd_nav_still)
2680 dvdnav_stream_sleeping((dvdnav_priv_t*)stream->priv);
2681 #endif
2683 //================= EDL =========================================
2685 #ifdef USE_EDL
2686 if( next_edl_record ) { // Are we (still?) doing EDL?
2687 if ( !sh_video ) {
2688 mp_msg( MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video );
2689 free_edl(edl_records);
2690 next_edl_record = NULL;
2691 edl_records = NULL;
2692 } else {
2693 if( sh_video->pts >= next_edl_record->start_sec ) {
2694 if( next_edl_record->action == EDL_SKIP ) {
2695 osd_function = OSD_FFW;
2696 abs_seek_pos = 0;
2697 rel_seek_secs = next_edl_record->length_sec;
2698 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n", next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec );
2699 edl_decision = 1;
2700 } else if( next_edl_record->action == EDL_MUTE ) {
2701 edl_muted = !edl_muted;
2702 if ((user_muted | edl_muted) != mixer.muted) mixer_mute(&mixer);
2703 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
2705 next_edl_record=next_edl_record->next;
2709 #endif
2711 //================= Keyboard events, SEEKing ====================
2713 current_module="key_events";
2716 mp_cmd_t* cmd;
2717 int brk_cmd = 0;
2718 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2719 switch(cmd->id) {
2720 case MP_CMD_SEEK : {
2721 float v;
2722 int abs;
2723 osd_show_percentage = 25;
2724 v = cmd->args[0].v.f;
2725 abs = (cmd->nargs > 1) ? cmd->args[1].v.i : 0;
2726 if(abs==2) { /* Absolute seek to a specific timestamp in seconds */
2727 abs_seek_pos = 1;
2728 if(sh_video)
2729 osd_function= (v > sh_video->timer) ? OSD_FFW : OSD_REW;
2730 rel_seek_secs = v;
2732 else if(abs) { /* Absolute seek by percentage */
2733 abs_seek_pos = 3;
2734 if(sh_video)
2735 osd_function= (v > sh_video->timer) ? OSD_FFW : OSD_REW;
2736 rel_seek_secs = v/100.0;
2738 else {
2739 rel_seek_secs+= v;
2740 osd_function= (v > 0) ? OSD_FFW : OSD_REW;
2742 brk_cmd = 1;
2743 } break;
2744 #ifdef USE_EDL
2745 case MP_CMD_EDL_MARK:
2746 if( edl_fd ) {
2747 float v = sh_video->pts;
2748 fprintf( edl_fd, "%f %f %d\n", v-2, v, 0 );
2750 break;
2751 #endif
2752 case MP_CMD_SWITCH_RATIO : {
2753 if (cmd->nargs == 0)
2754 movie_aspect = (float) sh_video->disp_w / sh_video->disp_h;
2755 else
2756 movie_aspect = cmd->args[0].v.f;
2757 mpcodecs_config_vo (sh_video, sh_video->disp_w, sh_video->disp_h, 0);
2758 } break;
2759 case MP_CMD_AUDIO_DELAY : {
2760 float v = cmd->args[0].v.f;
2761 audio_delay += v;
2762 osd_show_av_delay = 9;
2763 if(sh_audio) sh_audio->delay+= v;
2764 } break;
2765 case MP_CMD_SPEED_INCR : {
2766 float v = cmd->args[0].v.f;
2767 playback_speed += v;
2768 if (sh_video)
2769 osd_show_speed = sh_video->fps;
2770 build_afilter_chain(sh_audio, &ao_data);
2771 } break;
2772 case MP_CMD_SPEED_MULT : {
2773 float v = cmd->args[0].v.f;
2774 playback_speed *= v;
2775 if (sh_video)
2776 osd_show_speed = sh_video->fps;
2777 build_afilter_chain(sh_audio, &ao_data);
2778 } break;
2779 case MP_CMD_SPEED_SET : {
2780 float v = cmd->args[0].v.f;
2781 playback_speed = v;
2782 if (sh_video)
2783 osd_show_speed = sh_video->fps;
2784 build_afilter_chain(sh_audio, &ao_data);
2785 } break;
2786 case MP_CMD_FRAME_STEP :
2787 case MP_CMD_PAUSE : {
2788 cmd->pausing = 1;
2789 brk_cmd = 1;
2790 } break;
2791 case MP_CMD_QUIT : {
2792 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2794 case MP_CMD_GRAB_FRAMES : {
2795 grab_frames=2;
2796 } break;
2797 case MP_CMD_PLAY_TREE_STEP : {
2798 int n = cmd->args[0].v.i == 0 ? 1 : cmd->args[0].v.i;
2799 int force = cmd->args[1].v.i;
2801 #ifdef HAVE_NEW_GUI
2802 if (use_gui) {
2803 int i=0;
2804 if (n>0)
2805 for (i=0;i<n;i++)
2806 mplNext();
2807 else
2808 for (i=0;i<-1*n;i++)
2809 mplPrev();
2810 } else
2811 #endif
2813 if(!force && playtree_iter) {
2814 play_tree_iter_t* i = play_tree_iter_new_copy(playtree_iter);
2816 if(play_tree_iter_step(i,n,0) == PLAY_TREE_ITER_ENTRY)
2817 eof = (n > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
2818 play_tree_iter_free(i);
2819 } else
2820 eof = (n > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
2821 if(eof)
2822 play_tree_step = n;
2823 brk_cmd = 1;
2825 } break;
2826 case MP_CMD_PLAY_TREE_UP_STEP : {
2827 int n = cmd->args[0].v.i > 0 ? 1 : -1;
2828 int force = cmd->args[1].v.i;
2830 if(!force && playtree_iter) {
2831 play_tree_iter_t* i = play_tree_iter_new_copy(playtree_iter);
2832 if(play_tree_iter_up_step(i,n,0) == PLAY_TREE_ITER_ENTRY)
2833 eof = (n > 0) ? PT_UP_NEXT : PT_UP_PREV;
2834 play_tree_iter_free(i);
2835 } else
2836 eof = (n > 0) ? PT_UP_NEXT : PT_UP_PREV;
2837 brk_cmd = 1;
2838 } break;
2839 case MP_CMD_PLAY_ALT_SRC_STEP : {
2840 if(playtree_iter && playtree_iter->num_files > 1) {
2841 int v = cmd->args[0].v.i;
2842 if(v > 0 && playtree_iter->file < playtree_iter->num_files)
2843 eof = PT_NEXT_SRC;
2844 else if(v < 0 && playtree_iter->file > 1)
2845 eof = PT_PREV_SRC;
2847 brk_cmd = 1;
2848 } break;
2849 case MP_CMD_SUB_DELAY : {
2850 #ifdef USE_SUB
2851 if (sh_video) {
2852 int abs= cmd->args[1].v.i;
2853 float v = cmd->args[0].v.f;
2854 if(abs)
2855 sub_delay = v;
2856 else
2857 sub_delay += v;
2858 osd_show_sub_delay = 9; // show the subdelay in OSD
2860 #endif
2861 } break;
2862 case MP_CMD_SUB_STEP : {
2863 #ifdef USE_SUB
2864 if (sh_video) {
2865 int movement = cmd->args[0].v.i;
2866 step_sub(subdata, sh_video->pts, movement);
2867 osd_show_sub_delay = 9; // show the subdelay in OSD
2869 #endif
2870 } break;
2871 case MP_CMD_SUB_LOG : {
2872 #ifdef USE_SUB
2873 log_sub();
2874 #endif
2875 } break;
2876 case MP_CMD_OSD : {
2877 #ifdef USE_OSD
2878 if(sh_video) {
2879 int v = cmd->args[0].v.i;
2880 if(v < 0)
2881 osd_level=(osd_level+1)%(MAX_OSD_LEVEL+1);
2882 else
2883 osd_level= v > MAX_OSD_LEVEL ? MAX_OSD_LEVEL : v;
2885 #endif
2886 } break;
2887 case MP_CMD_OSD_SHOW_TEXT : {
2888 #ifdef USE_OSD
2889 if(osd_level && sh_video){
2890 osd_show_text=sh_video->fps; // 1 sec
2891 strncpy(osd_show_text_buffer, cmd->args[0].v.s, 64);
2893 #endif
2894 } break;
2895 case MP_CMD_VOLUME : {
2896 int v = cmd->args[0].v.i;
2898 // start change for absolute volume value
2899 int abs = (cmd->nargs > 1) ? cmd->args[1].v.i : 0;
2901 #ifdef USE_EDL
2902 if (edl_muted) break;
2903 user_muted = 0;
2904 #endif
2905 if( abs )
2907 mixer_setvolume(&mixer, (float)v, (float)v );
2908 } else {
2909 if(v > 0)
2910 mixer_incvolume(&mixer);
2911 else
2912 mixer_decvolume(&mixer);
2915 #ifdef USE_OSD
2916 if(osd_level && sh_video){
2917 float vol;
2918 osd_visible=sh_video->fps; // 1 sec
2919 vo_osd_progbar_type=OSD_VOLUME;
2920 mixer_getbothvolume(&mixer, &vol);
2921 vo_osd_progbar_value=(vol*256.0)/100.0;
2922 vo_osd_changed(OSDTYPE_PROGBAR);
2924 #endif
2925 } break;
2926 case MP_CMD_MUTE:
2927 #ifdef USE_EDL
2928 user_muted = !user_muted;
2929 if ((edl_muted | user_muted) != mixer.muted)
2930 #endif
2931 mixer_mute(&mixer);
2932 break;
2933 case MP_CMD_LOADFILE : {
2934 play_tree_t* e = play_tree_new();
2935 play_tree_add_file(e,cmd->args[0].v.s);
2937 // Go back to the start point
2938 while(play_tree_iter_up_step(playtree_iter,0,1) != PLAY_TREE_ITER_END)
2939 /* NOP */;
2940 play_tree_free_list(playtree->child,1);
2941 play_tree_set_child(playtree,e);
2942 play_tree_iter_step(playtree_iter,0,0);
2943 eof = PT_NEXT_SRC;
2944 brk_cmd = 1;
2945 } break;
2946 case MP_CMD_LOADLIST : {
2947 play_tree_t* e = parse_playlist_file(cmd->args[0].v.s);
2948 if(!e)
2949 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_PlaylistLoadUnable,cmd->args[0].v.s);
2950 else {
2951 // Go back to the start point
2952 while(play_tree_iter_up_step(playtree_iter,0,1) != PLAY_TREE_ITER_END)
2953 /* NOP */;
2954 play_tree_free_list(playtree->child,1);
2955 play_tree_set_child(playtree,e);
2956 play_tree_iter_step(playtree_iter,0,0);
2957 eof = PT_NEXT_SRC;
2959 brk_cmd = 1;
2960 } break;
2961 case MP_CMD_GAMMA : {
2962 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
2964 if (!sh_video)
2965 break;
2967 if (vo_gamma_gamma == 1000)
2969 vo_gamma_gamma = 0;
2970 get_video_colors (sh_video, "gamma", &vo_gamma_gamma);
2973 if (abs)
2974 vo_gamma_gamma = v;
2975 else
2976 vo_gamma_gamma += v;
2978 if (vo_gamma_gamma > 100)
2979 vo_gamma_gamma = 100;
2980 else if (vo_gamma_gamma < -100)
2981 vo_gamma_gamma = -100;
2982 set_video_colors(sh_video, "gamma", vo_gamma_gamma);
2983 #ifdef USE_OSD
2984 if(osd_level){
2985 osd_visible=sh_video->fps; // 1 sec
2986 vo_osd_progbar_type=OSD_BRIGHTNESS;
2987 vo_osd_progbar_value=(vo_gamma_gamma<<7)/100 + 128;
2988 vo_osd_changed(OSDTYPE_PROGBAR);
2990 #endif // USE_OSD
2991 } break;
2992 case MP_CMD_BRIGHTNESS : {
2993 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
2995 if (!sh_video)
2996 break;
2998 if (vo_gamma_brightness == 1000)
3000 vo_gamma_brightness = 0;
3001 get_video_colors(sh_video, "brightness", &vo_gamma_brightness);
3004 if (abs)
3005 vo_gamma_brightness = v;
3006 else
3007 vo_gamma_brightness += v;
3009 if (vo_gamma_brightness > 100)
3010 vo_gamma_brightness = 100;
3011 else if (vo_gamma_brightness < -100)
3012 vo_gamma_brightness = -100;
3013 if(set_video_colors(sh_video, "brightness", vo_gamma_brightness)){
3014 #ifdef USE_OSD
3015 if(osd_level){
3016 osd_visible=sh_video->fps; // 1 sec
3017 vo_osd_progbar_type=OSD_BRIGHTNESS;
3018 vo_osd_progbar_value=(vo_gamma_brightness<<7)/100 + 128;
3019 vo_osd_changed(OSDTYPE_PROGBAR);
3021 #endif // USE_OSD
3023 } break;
3024 case MP_CMD_CONTRAST : {
3025 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
3027 if (!sh_video)
3028 break;
3030 if (vo_gamma_contrast == 1000)
3032 vo_gamma_contrast = 0;
3033 get_video_colors(sh_video, "contrast", &vo_gamma_contrast);
3036 if (abs)
3037 vo_gamma_contrast = v;
3038 else
3039 vo_gamma_contrast += v;
3041 if (vo_gamma_contrast > 100)
3042 vo_gamma_contrast = 100;
3043 else if (vo_gamma_contrast < -100)
3044 vo_gamma_contrast = -100;
3045 if(set_video_colors(sh_video, "contrast", vo_gamma_contrast)){
3046 #ifdef USE_OSD
3047 if(osd_level){
3048 osd_visible=sh_video->fps; // 1 sec
3049 vo_osd_progbar_type=OSD_CONTRAST;
3050 vo_osd_progbar_value=(vo_gamma_contrast<<7)/100 + 128;
3051 vo_osd_changed(OSDTYPE_PROGBAR);
3053 #endif // USE_OSD
3055 } break;
3056 case MP_CMD_SATURATION : {
3057 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
3059 if (!sh_video)
3060 break;
3062 if (vo_gamma_saturation == 1000)
3064 vo_gamma_saturation = 0;
3065 get_video_colors(sh_video, "saturation", &vo_gamma_saturation);
3068 if (abs)
3069 vo_gamma_saturation = v;
3070 else
3071 vo_gamma_saturation += v;
3073 if (vo_gamma_saturation > 100)
3074 vo_gamma_saturation = 100;
3075 else if (vo_gamma_saturation < -100)
3076 vo_gamma_saturation = -100;
3077 if(set_video_colors(sh_video, "saturation", vo_gamma_saturation)){
3078 #ifdef USE_OSD
3079 if(osd_level){
3080 osd_visible=sh_video->fps; // 1 sec
3081 vo_osd_progbar_type=OSD_SATURATION;
3082 vo_osd_progbar_value=(vo_gamma_saturation<<7)/100 + 128;
3083 vo_osd_changed(OSDTYPE_PROGBAR);
3085 #endif // USE_OSD
3087 } break;
3088 case MP_CMD_HUE : {
3089 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
3091 if (!sh_video)
3092 break;
3094 if (vo_gamma_hue == 1000)
3096 vo_gamma_hue = 0;
3097 get_video_colors(sh_video, "hue", &vo_gamma_hue);
3100 if (abs)
3101 vo_gamma_hue = v;
3102 else
3103 vo_gamma_hue += v;
3105 if (vo_gamma_hue > 100)
3106 vo_gamma_hue = 100;
3107 else if (vo_gamma_hue < -100)
3108 vo_gamma_hue = -100;
3109 if(set_video_colors(sh_video, "hue", vo_gamma_hue)){
3110 #ifdef USE_OSD
3111 if(osd_level){
3112 osd_visible=sh_video->fps; // 1 sec
3113 vo_osd_progbar_type=OSD_HUE;
3114 vo_osd_progbar_value=(vo_gamma_hue<<7)/100 + 128;
3115 vo_osd_changed(OSDTYPE_PROGBAR);
3117 #endif // USE_OSD
3119 } break;
3120 case MP_CMD_FRAMEDROPPING : {
3121 int v = cmd->args[0].v.i;
3122 if(v < 0){
3123 frame_dropping = (frame_dropping+1)%3;
3124 #ifdef USE_OSD
3125 osd_show_framedropping=10;
3126 vo_osd_changed(OSDTYPE_SUBTITLE);
3127 #endif
3129 else
3130 frame_dropping = v > 2 ? 2 : v;
3131 } break;
3132 #ifdef USE_TV
3133 case MP_CMD_TV_SET_FREQ : {
3134 if (file_format == DEMUXER_TYPE_TV)
3135 tv_set_freq((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.f * 16.0);
3136 } break;
3137 case MP_CMD_TV_SET_NORM : {
3138 if (file_format == DEMUXER_TYPE_TV)
3139 tv_set_norm((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.s);
3140 } break;
3141 case MP_CMD_TV_SET_BRIGHTNESS : {
3142 if (file_format == DEMUXER_TYPE_TV)
3143 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_BRIGHTNESS, cmd->args[0].v.i);
3144 } break;
3145 case MP_CMD_TV_SET_HUE : {
3146 if (file_format == DEMUXER_TYPE_TV)
3147 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_HUE, cmd->args[0].v.i);
3148 } break;
3149 case MP_CMD_TV_SET_SATURATION : {
3150 if (file_format == DEMUXER_TYPE_TV)
3151 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_SATURATION, cmd->args[0].v.i);
3152 } break;
3153 case MP_CMD_TV_SET_CONTRAST : {
3154 if (file_format == DEMUXER_TYPE_TV)
3155 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_CONTRAST, cmd->args[0].v.i);
3156 } break;
3157 case MP_CMD_TV_STEP_CHANNEL : {
3158 if (file_format == DEMUXER_TYPE_TV) {
3159 int v = cmd->args[0].v.i;
3160 if(v > 0){
3161 tv_step_channel((tvi_handle_t*)(demuxer->priv), TV_CHANNEL_HIGHER);
3162 #ifdef USE_OSD
3163 if (tv_channel_list) {
3164 osd_show_tv_channel = sh_video->fps;
3165 vo_osd_changed(OSDTYPE_SUBTITLE);
3167 #endif
3168 } else {
3169 tv_step_channel((tvi_handle_t*)(demuxer->priv), TV_CHANNEL_LOWER);
3170 #ifdef USE_OSD
3171 if (tv_channel_list) {
3172 osd_show_tv_channel = sh_video->fps;
3173 vo_osd_changed(OSDTYPE_SUBTITLE);
3175 #endif
3179 #ifdef HAS_DVBIN_SUPPORT
3180 if((stream->type == STREAMTYPE_DVB) && stream->priv)
3182 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
3183 if(priv->is_on)
3185 int dir;
3186 int v = cmd->args[0].v.i;
3188 last_dvb_step = v;
3189 if(v > 0)
3190 dir = DVB_CHANNEL_HIGHER;
3191 else
3192 dir = DVB_CHANNEL_LOWER;
3195 if(dvb_step_channel(priv, dir))
3197 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
3198 cache_uninit(stream);
3199 goto goto_enable_cache;
3203 #endif
3204 break;
3205 case MP_CMD_TV_SET_CHANNEL : {
3206 if (file_format == DEMUXER_TYPE_TV) {
3207 tv_set_channel((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.s);
3208 #ifdef USE_OSD
3209 if (tv_channel_list) {
3210 osd_show_tv_channel = sh_video->fps;
3211 vo_osd_changed(OSDTYPE_SUBTITLE);
3213 #endif
3215 } break;
3216 #ifdef HAS_DVBIN_SUPPORT
3217 case MP_CMD_DVB_SET_CHANNEL:
3219 if((stream->type == STREAMTYPE_DVB) && stream->priv)
3221 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
3222 if(priv->is_on)
3224 if(priv->list->current <= cmd->args[0].v.i)
3225 last_dvb_step = 1;
3226 else
3227 last_dvb_step = -1;
3229 if(dvb_set_channel(priv, cmd->args[1].v.i, cmd->args[0].v.i))
3231 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
3232 cache_uninit(stream);
3233 goto goto_enable_cache;
3238 #endif
3239 case MP_CMD_TV_LAST_CHANNEL : {
3240 if (file_format == DEMUXER_TYPE_TV) {
3241 tv_last_channel((tvi_handle_t*)(demuxer->priv));
3242 #ifdef USE_OSD
3243 if (tv_channel_list) {
3244 osd_show_tv_channel = sh_video->fps;
3245 vo_osd_changed(OSDTYPE_SUBTITLE);
3247 #endif
3249 } break;
3250 case MP_CMD_TV_STEP_NORM : {
3251 if (file_format == DEMUXER_TYPE_TV)
3252 tv_step_norm((tvi_handle_t*)(demuxer->priv));
3253 } break;
3254 case MP_CMD_TV_STEP_CHANNEL_LIST : {
3255 if (file_format == DEMUXER_TYPE_TV)
3256 tv_step_chanlist((tvi_handle_t*)(demuxer->priv));
3257 } break;
3258 #endif
3259 case MP_CMD_SWITCH_VSYNC: {
3260 vo_vsync = ( cmd->nargs > 0 )? cmd->args[0].v.i : !vo_vsync;
3261 } break;
3262 case MP_CMD_VO_FULLSCREEN:
3264 #ifdef HAVE_NEW_GUI
3265 if ( use_gui ) guiGetEvent( guiIEvent,(char *)MP_CMD_GUI_FULLSCREEN );
3266 else
3267 #endif
3268 if(video_out && vo_config_count) video_out->control(VOCTRL_FULLSCREEN, 0);
3269 } break;
3270 case MP_CMD_VO_ONTOP:
3272 if(video_out && vo_config_count) {
3273 video_out->control(VOCTRL_ONTOP, 0);
3274 #ifdef USE_OSD
3275 osd_show_ontop=10;
3276 vo_osd_changed(OSDTYPE_SUBTITLE);
3277 #endif
3280 } break;
3281 case MP_CMD_VO_ROOTWIN:
3283 if(video_out && vo_config_count) {
3284 video_out->control(VOCTRL_ROOTWIN, 0);
3285 #ifdef USE_OSD
3286 osd_show_rootwin=10;
3287 vo_osd_changed(OSDTYPE_SUBTITLE);
3288 #endif
3291 } break;
3292 case MP_CMD_PANSCAN : {
3293 if ( !video_out ) break;
3294 if ( video_out->control( VOCTRL_GET_PANSCAN,NULL ) == VO_TRUE )
3296 int abs= cmd->args[1].v.i;
3297 float v = cmd->args[0].v.f;
3298 float res;
3299 if(abs) res = v;
3300 else res = vo_panscan+v;
3301 vo_panscan = res > 1 ? 1 : res < 0 ? 0 : res;
3302 video_out->control( VOCTRL_SET_PANSCAN,NULL );
3303 #ifdef USE_OSD
3304 if(osd_level && sh_video){
3305 osd_visible=sh_video->fps; // 1 sec
3306 vo_osd_progbar_type=OSD_PANSCAN;
3307 vo_osd_progbar_value=vo_panscan*256;
3308 vo_osd_changed(OSDTYPE_PROGBAR);
3310 #ifdef HAVE_FREETYPE
3311 if (sh_video && (subtitle_autoscale == 2 || subtitle_autoscale == 3))
3312 // force scaling font to movie width or diagonal
3313 force_load_font = 1;
3314 #endif
3315 #endif
3317 } break;
3318 case MP_CMD_SUB_POS:
3320 #ifdef USE_SUB
3321 if (sh_video) {
3322 int v;
3323 v = cmd->args[0].v.i;
3325 sub_pos+=v;
3326 if(sub_pos >100) sub_pos=100;
3327 if(sub_pos <0) sub_pos=0;
3328 vo_osd_changed(OSDTYPE_SUBTITLE);
3329 osd_show_sub_pos = 9;
3331 #endif
3332 } break;
3333 case MP_CMD_SUB_ALIGNMENT:
3335 #ifdef USE_SUB
3336 if (sh_video) {
3337 if (cmd->nargs >= 1)
3338 sub_alignment = cmd->args[0].v.i;
3339 else
3340 sub_alignment = (sub_alignment+1) % 3;
3341 osd_show_sub_alignment = 9;
3342 vo_osd_changed(OSDTYPE_SUBTITLE);
3344 #endif
3345 } break;
3346 case MP_CMD_SUB_VISIBILITY:
3348 #ifdef USE_SUB
3349 if (sh_video) {
3350 sub_visibility=1-sub_visibility;
3351 osd_show_sub_visibility = 9; // show state of subtitle visibility in OSD
3352 vo_osd_changed(OSDTYPE_SUBTITLE);
3354 #endif
3355 } break;
3356 case MP_CMD_GET_SUB_VISIBILITY:
3358 #ifdef USE_SUB
3359 if (sh_video) {
3360 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsSubVisibility, sub_visibility);
3362 #endif
3363 } break;
3364 case MP_CMD_SUB_SELECT:
3365 if (global_sub_size) {
3366 int source = -1;
3368 global_sub_pos++;
3369 if (global_sub_pos >= global_sub_size)
3370 global_sub_pos = -1;
3371 if (global_sub_pos >= 0)
3372 source = sub_source();
3374 mp_msg(MSGT_CPLAYER, MSGL_DBG3, "subtitles: %d subs, (v@%d s@%d d@%d), @%d, source @%d\n",
3375 global_sub_size, global_sub_indices[SUB_SOURCE_VOBSUB],
3376 global_sub_indices[SUB_SOURCE_SUBS], global_sub_indices[SUB_SOURCE_DEMUX],
3377 global_sub_pos, source);
3379 #ifdef USE_SUB
3380 set_of_sub_pos = -1;
3381 subdata = NULL;
3382 vo_sub_last = vo_sub = NULL;
3383 #endif
3384 vobsub_id = -1;
3385 dvdsub_id = -1;
3386 if (d_dvdsub) d_dvdsub->id = -1;
3388 // be careful!
3389 // if sub_changed is till on but subdata's been reset, bad things happen.
3390 osd_show_vobsub_changed = 0;
3391 osd_show_sub_changed = 0;
3393 if (source == SUB_SOURCE_VOBSUB) {
3394 vobsub_id = global_sub_pos - global_sub_indices[SUB_SOURCE_VOBSUB];
3395 if (!global_sub_quiet_osd_hack) osd_show_vobsub_changed = sh_video->fps;
3396 #ifdef USE_SUB
3397 } else if (source == SUB_SOURCE_SUBS) {
3398 set_of_sub_pos = global_sub_pos - global_sub_indices[SUB_SOURCE_SUBS];
3399 subdata = set_of_subtitles[set_of_sub_pos];
3400 if (!global_sub_quiet_osd_hack) osd_show_sub_changed = sh_video->fps;
3401 vo_osd_changed(OSDTYPE_SUBTITLE);
3403 // FIXME: is this the correct place for these?
3404 if(stream_dump_type==3) list_sub_file(subdata);
3405 if(stream_dump_type==4) dump_mpsub(subdata, sh_video->fps);
3406 if(stream_dump_type==6) dump_srt(subdata, sh_video->fps);
3407 if(stream_dump_type==7) dump_microdvd(subdata, sh_video->fps);
3408 if(stream_dump_type==8) dump_jacosub(subdata, sh_video->fps);
3409 if(stream_dump_type==9) dump_sami(subdata, sh_video->fps);
3410 #endif
3411 } else if (source == SUB_SOURCE_DEMUX) {
3412 dvdsub_id = global_sub_pos - global_sub_indices[SUB_SOURCE_DEMUX];
3413 if (d_dvdsub) {
3414 #ifdef USE_DVDREAD
3415 if (vo_spudec && stream->type == STREAMTYPE_DVD) {
3416 d_dvdsub->id = dvdsub_id;
3417 spudec_reset(vo_spudec);
3419 #endif
3420 #ifdef HAVE_OGGVORBIS
3421 if (demuxer->type == DEMUXER_TYPE_OGG)
3422 d_dvdsub->id = demux_ogg_sub_id(demuxer, dvdsub_id);
3423 #endif
3424 #ifdef HAVE_MATROSKA
3425 if (demuxer->type == DEMUXER_TYPE_MATROSKA) {
3426 d_dvdsub->id = demux_mkv_change_subs(demuxer, dvdsub_id);
3427 if (d_dvdsub->id >= 0 && ((mkv_sh_sub_t *)d_dvdsub->sh)->type == 'v') {
3428 mkv_sh_sub_t *mkv_sh_sub = (mkv_sh_sub_t *)d_dvdsub->sh;
3429 if (vo_spudec != NULL)
3430 spudec_free(vo_spudec);
3431 vo_spudec =
3432 spudec_new_scaled_vobsub(mkv_sh_sub->has_palette ? mkv_sh_sub->palette : NULL, mkv_sh_sub->colors,
3433 mkv_sh_sub->custom_colors,
3434 mkv_sh_sub->width,
3435 mkv_sh_sub->height);
3436 if (!forced_subs_only)
3437 forced_subs_only = mkv_sh_sub->forced_subs_only;
3438 if (vo_spudec) {
3439 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
3440 inited_flags |= INITED_SPUDEC;
3444 #endif
3446 if (!global_sub_quiet_osd_hack) osd_show_vobsub_changed = sh_video->fps;
3447 } else { // off
3448 if (!global_sub_quiet_osd_hack) osd_show_vobsub_changed = sh_video->fps;
3449 #ifdef USE_SUB
3450 vo_osd_changed(OSDTYPE_SUBTITLE);
3451 #endif
3453 // it's annoying and dumb to show osd saying "off" at every subless file...
3454 global_sub_quiet_osd_hack = 0;
3456 break;
3457 case MP_CMD_SUB_FORCED_ONLY:
3458 if (vo_spudec) {
3459 forced_subs_only = forced_subs_only ? 0 : ~0; // toggle state
3460 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3462 break;
3463 case MP_CMD_SCREENSHOT :
3464 if(vo_config_count) video_out->control(VOCTRL_SCREENSHOT, NULL);
3465 break;
3466 case MP_CMD_VF_CHANGE_RECTANGLE:
3467 set_rectangle(sh_video, cmd->args[0].v.i, cmd->args[1].v.i);
3468 break;
3470 case MP_CMD_GET_TIME_LENGTH : {
3471 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsLength, demuxer_get_time_length(demuxer));
3472 } break;
3474 case MP_CMD_GET_VO_FULLSCREEN : {
3475 if(video_out && vo_config_count)
3476 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsVoFullscreen, vo_fs);
3477 } break;
3479 case MP_CMD_GET_PERCENT_POS : {
3480 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsPercentPos, demuxer_get_percent_pos(demuxer));
3481 } break;
3482 case MP_CMD_RUN : {
3483 #ifndef __MINGW32__
3484 if(!fork()) {
3485 execl("/bin/sh","sh","-c",cmd->args[0].v.s,NULL);
3486 exit(0);
3488 #endif
3489 } break;
3491 #ifdef USE_DVDNAV
3492 case MP_CMD_DVDNAV_EVENT: {
3493 dvdnav_priv_t * dvdnav_priv = (dvdnav_priv_t*)(stream->priv);
3494 dvdnav_event_t * dvdnav_event = (dvdnav_event_t *)(cmd->args[0].v.v);
3496 /* ignore these events if we're not in dvd_nav mode */
3497 if (stream->type != STREAMTYPE_DVDNAV) break;
3499 if (!dvdnav_event) {
3500 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNullEvent);
3501 break;
3504 //printf("mplayer: got event: %d\n",dvdnav_event->event);
3506 switch (dvdnav_event->event) {
3507 case DVDNAV_BLOCK_OK: {
3508 /* be silent about this one */
3509 break;
3511 case DVDNAV_HIGHLIGHT: {
3512 dvdnav_highlight_event_t *hevent = (dvdnav_highlight_event_t*)(dvdnav_event->details);
3513 if (!hevent) {
3514 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavHighlightEventBroken);
3515 break;
3518 if (hevent->display && hevent->buttonN>0)
3520 //dvdnav_priv->seen_root_menu=1; /* if we got a highlight, we're on a menu */
3521 sprintf( dvd_nav_text, "Highlight button %d (%u,%u)-(%u,%u) PTS %d (now is %5.2f)",
3522 hevent->buttonN,
3523 hevent->sx,hevent->sy,
3524 hevent->ex,hevent->ey,
3525 hevent->pts, d_video->pts);
3526 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavEvent,dvd_nav_text);
3527 //osd_show_dvd_nav_delay = 60;
3529 osd_show_dvd_nav_highlight=1;
3530 osd_show_dvd_nav_sx=hevent->sx;
3531 osd_show_dvd_nav_ex=hevent->ex;
3532 osd_show_dvd_nav_sy=hevent->sy;
3533 osd_show_dvd_nav_ey=hevent->ey;
3535 else {
3536 osd_show_dvd_nav_highlight=0;
3537 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavHighlightHide);
3539 break;
3541 case DVDNAV_STILL_FRAME: {
3542 dvdnav_still_event_t *still_event = (dvdnav_still_event_t*)(dvdnav_event->details);
3544 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavStillFrame, still_event->length );
3545 while (dvdnav_stream_sleeping(dvdnav_priv)) {
3546 usec_sleep(1000); /* 1ms */
3548 dvdnav_stream_sleep(dvdnav_priv,still_event->length);
3549 break;
3551 case DVDNAV_STOP: {
3552 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavStop );
3553 break;
3555 case DVDNAV_NOP: {
3556 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavNOP);
3557 break;
3559 case DVDNAV_SPU_STREAM_CHANGE: {
3560 #if DVDNAVVERSION > 012
3561 dvdnav_spu_stream_change_event_t *stream_change = (dvdnav_spu_stream_change_event_t*)(dvdnav_event->details);
3563 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSpuStreamChangeVerbose,
3564 stream_change->physical_wide,
3565 stream_change->physical_letterbox,
3566 stream_change->physical_pan_scan,
3567 stream_change->logical);
3569 if (vo_spudec && dvdsub_id!=stream_change->physical_wide) {
3570 mp_msg(MSGT_INPUT,MSGL_DBG2,"d_dvdsub->id change: was %d is now %d\n",
3571 d_dvdsub->id,stream_change->physical_wide);
3572 // FIXME: need a better way to change SPU id
3573 d_dvdsub->id=dvdsub_id=stream_change->physical_wide;
3574 if (vo_spudec) spudec_reset(vo_spudec);
3576 #else
3577 dvdnav_stream_change_event_t *stream_change = (dvdnav_stream_change_event_t*)(dvdnav_event->details);
3579 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSpuStreamChange,
3580 stream_change->physical,
3581 stream_change->logical);
3583 if (vo_spudec && dvdsub_id!=stream_change->physical) {
3584 mp_msg(MSGT_INPUT,MSGL_DBG2,"d_dvdsub->id change: was %d is now %d\n",
3585 d_dvdsub->id,stream_change->physical);
3586 // FIXME: need a better way to change SPU id
3587 d_dvdsub->id=dvdsub_id=stream_change->physical;
3588 if (vo_spudec) spudec_reset(vo_spudec);
3590 #endif
3591 break;
3593 case DVDNAV_AUDIO_STREAM_CHANGE: {
3594 int aid_temp;
3595 #if DVDNAVVERSION > 012
3596 dvdnav_audio_stream_change_event_t *stream_change = (dvdnav_audio_stream_change_event_t*)(dvdnav_event->details);
3597 #else
3598 dvdnav_stream_change_event_t *stream_change = (dvdnav_stream_change_event_t*)(dvdnav_event->details);
3599 #endif
3601 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavAudioStreamChange,
3602 stream_change->physical,
3603 stream_change->logical);
3605 aid_temp=stream_change->physical;
3606 if (aid_temp>=0) aid_temp+=128; // FIXME: is this sane?
3607 if (d_audio && audio_id!=aid_temp) {
3608 mp_msg(MSGT_INPUT,MSGL_DBG2,"d_audio->id change: was %d is now %d\n",
3609 d_audio->id,aid_temp);
3610 // FIXME: need a bettery way to change audio stream id
3611 d_audio->id=dvdsub_id=aid_temp;
3612 if(sh_audio) resync_audio_stream(sh_audio);
3615 break;
3617 case DVDNAV_VTS_CHANGE: {
3618 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavVTSChange);
3619 break;
3621 case DVDNAV_CELL_CHANGE: {
3622 dvdnav_cell_change_event_t *cell_change = (dvdnav_cell_change_event_t*)(dvdnav_event->details);
3623 cell_playback_t * cell_playback = cell_change->new_cell;
3625 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavCellChange);
3626 osd_show_dvd_nav_highlight=0; /* screen changed, disable menu */
3628 printf("new still time: %d\n",cell_playback->still_time);
3629 printf("new cell_cmd_nr: %d\n",cell_playback->cell_cmd_nr);
3630 printf("new playback_time: %02d:%02d:%02d.%02d\n",
3631 cell_playback->playback_time.hour,
3632 cell_playback->playback_time.minute,
3633 cell_playback->playback_time.second,
3634 cell_playback->playback_time.frame_u);
3637 //rel_seek_secs=1; // not really: we can't seek, but it'll reset the muxer
3638 //abs_seek_pos=0;
3639 break;
3641 case DVDNAV_NAV_PACKET: {
3642 // printf("DVDNAV Event: Nav Packet\n");
3643 break;
3645 case DVDNAV_SPU_CLUT_CHANGE: {
3646 uint32_t * new_clut = (uint32_t *)(dvdnav_event->details);
3648 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSpuClutChange);
3649 // send new palette to SPU decoder
3650 if (vo_spudec) spudec_update_palette(vo_spudec,new_clut);
3652 break;
3654 case DVDNAV_SEEK_DONE: {
3655 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSeekDone);
3656 break;
3660 // free the dvdnav event
3661 free(dvdnav_event->details);
3662 free(dvdnav_event);
3663 cmd->args[0].v.v=NULL;
3665 case MP_CMD_DVDNAV: {
3666 dvdnav_priv_t * dvdnav_priv=(dvdnav_priv_t*)stream->priv;
3668 /* ignore these events if we're not in dvd_nav mode */
3669 if (stream->type != STREAMTYPE_DVDNAV) break;
3671 switch (cmd->args[0].v.i) {
3672 case MP_CMD_DVDNAV_UP:
3673 dvdnav_upper_button_select(dvdnav_priv->dvdnav);
3674 break;
3675 case MP_CMD_DVDNAV_DOWN:
3676 dvdnav_lower_button_select(dvdnav_priv->dvdnav);
3677 break;
3678 case MP_CMD_DVDNAV_LEFT:
3679 dvdnav_left_button_select(dvdnav_priv->dvdnav);
3680 break;
3681 case MP_CMD_DVDNAV_RIGHT:
3682 dvdnav_right_button_select(dvdnav_priv->dvdnav);
3683 break;
3684 case MP_CMD_DVDNAV_MENU:
3685 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_MenuCall);
3686 dvdnav_menu_call(dvdnav_priv->dvdnav,DVD_MENU_Root);
3687 break;
3688 case MP_CMD_DVDNAV_SELECT:
3689 dvdnav_button_activate(dvdnav_priv->dvdnav);
3690 break;
3691 default:
3692 mp_msg(MSGT_CPLAYER, MSGL_V, "Weird DVD Nav cmd %d\n",cmd->args[0].v.i);
3693 break;
3695 break;
3697 #endif
3698 default : {
3699 #ifdef HAVE_NEW_GUI
3700 if ( ( use_gui )&&( cmd->id > MP_CMD_GUI_EVENTS ) ) guiGetEvent( guiIEvent,(char *)cmd->id );
3701 else
3702 #endif
3703 mp_msg(MSGT_CPLAYER, MSGL_V, "Received unknown cmd %s\n",cmd->name);
3706 if (cmd->pausing)
3707 osd_function = OSD_PAUSE;
3708 mp_cmd_free(cmd);
3712 if (seek_to_sec) {
3713 int a,b; float d;
3715 if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3)
3716 rel_seek_secs += 3600*a +60*b +d ;
3717 else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2)
3718 rel_seek_secs += 60*a +d;
3719 else if (sscanf(seek_to_sec, "%f", &d)==1)
3720 rel_seek_secs += d;
3722 seek_to_sec = NULL;
3725 /* Looping. */
3726 if(eof==1 && loop_times>=0) {
3727 int l = loop_times;
3728 play_tree_iter_step(playtree_iter,0,0);
3729 loop_times = l;
3730 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", loop_times,eof);
3732 if(loop_times>1) loop_times--; else
3733 if(loop_times==1) loop_times=-1;
3734 play_n_frames=play_n_frames_mf;
3735 eof=0;
3736 abs_seek_pos=3; rel_seek_secs=0; // seek to start of movie (0%)
3737 loop_seek = 1;
3740 if(rel_seek_secs || abs_seek_pos){
3741 current_module="seek";
3742 if(demux_seek(demuxer,rel_seek_secs,abs_seek_pos)){
3743 // success:
3744 /* FIXME there should be real seeking for vobsub */
3745 if(sh_video) sh_video->pts=d_video->pts;
3746 if (vo_vobsub)
3747 //vobsub_reset(vo_vobsub);
3748 vobsub_seek(vo_vobsub,sh_video->pts);
3749 #if 0
3750 if(sh_video && d_video->packs == 0)
3751 ds_fill_buffer(d_video);
3752 if(sh_audio){
3753 if(d_audio->packs == 0)
3754 ds_fill_buffer(d_audio);
3755 if(verbose>0){
3756 float a_pts=d_audio->pts;
3757 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3758 mp_msg(MSGT_AVSYNC,MSGL_V,"SEEK: A: %5.3f V: %5.3f A-V: %5.3f \n",a_pts,d_video->pts,a_pts-d_video->pts);
3760 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A:%6.1f V:%6.1f A-V:%7.3f ct: ? \r",d_audio->pts,d_video->pts,0.0f);
3761 } else {
3762 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A: --- V:%6.1f \r",d_video->pts);
3764 #endif
3765 fflush(stdout);
3767 if(sh_video){
3768 current_module="seek_video_reset";
3769 resync_video_stream(sh_video);
3770 if(vo_config_count) video_out->control(VOCTRL_RESET,NULL);
3773 if(sh_audio){
3774 current_module="seek_audio_reset";
3775 audio_out->reset(); // stop audio, throwing away buffered data
3777 #ifdef USE_OSD
3778 // Set OSD:
3779 if(osd_level && !loop_seek){
3780 #ifdef USE_EDL
3781 if( !edl_decision ) {
3782 #else
3783 if( 1 ) { // Let the compiler optimize this out
3784 #endif
3785 if (sh_video) {
3786 osd_visible=sh_video->fps; // 1 sec
3787 vo_osd_progbar_type=0;
3788 vo_osd_progbar_value=demuxer_get_percent_pos(demuxer) * 256 / 100;
3789 vo_osd_changed(OSDTYPE_PROGBAR);
3793 #endif
3794 if(sh_video) {
3795 c_total=0;
3796 max_pts_correction=0.1;
3797 osd_visible=sh_video->fps; // to rewert to PLAY pointer after 1 sec
3798 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3799 drop_frame_cnt=0;
3800 too_slow_frame_cnt=0;
3801 too_fast_frame_cnt=0;
3803 if(vo_spudec) spudec_reset(vo_spudec);
3806 #ifdef USE_EDL
3808 * We saw a seek, have to rewind the EDL operations stack
3809 * and find the next EDL action to take care of.
3812 edl_muted = 0;
3813 next_edl_record = edl_records;
3815 while (next_edl_record)
3817 /* Trying to remember if we need to mute/unmute first;
3818 * prior EDL implementation lacks this.
3821 if (next_edl_record->start_sec >= sh_video->pts)
3822 break;
3824 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted;
3825 next_edl_record = next_edl_record->next;
3828 if ((user_muted | edl_muted) != mixer.muted) mixer_mute(&mixer);
3829 #endif
3830 rel_seek_secs=0;
3831 abs_seek_pos=0;
3832 frame_time_remaining=0;
3833 current_module=NULL;
3834 loop_seek=0;
3837 #ifdef HAVE_NEW_GUI
3838 if(use_gui){
3839 guiEventHandling();
3840 if(demuxer->file_format==DEMUXER_TYPE_AVI && sh_video && sh_video->video.dwLength>2){
3841 // get pos from frame number / total frames
3842 guiIntfStruct.Position=(float)d_video->pack_no*100.0f/sh_video->video.dwLength;
3843 } else {
3844 off_t len = ( demuxer->movi_end - demuxer->movi_start );
3845 off_t pos = ( demuxer->file_format == DEMUXER_TYPE_AUDIO?stream->pos:demuxer->filepos );
3846 guiIntfStruct.Position=(len <= 0? 0.0f : ( pos - demuxer->movi_start ) * 100.0f / len );
3848 if ( sh_video ) guiIntfStruct.TimeSec=sh_video->pts;
3849 else if ( sh_audio ) guiIntfStruct.TimeSec=sh_audio->delay;
3850 guiIntfStruct.LengthInSec=demuxer_get_time_length(demuxer);
3851 guiGetEvent( guiReDraw,NULL );
3852 guiGetEvent( guiSetVolume,NULL );
3853 if(guiIntfStruct.Playing==0) break; // STOP
3854 if(guiIntfStruct.Playing==2) osd_function=OSD_PAUSE;
3855 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3856 #ifdef USE_DVDREAD
3857 if ( stream->type == STREAMTYPE_DVD )
3859 dvd_priv_t * dvdp = stream->priv;
3860 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3862 #endif
3864 #endif
3867 //================= Update OSD ====================
3868 #ifdef USE_OSD
3869 if(osd_level>=1 && sh_video){
3870 int pts=sh_video->pts;
3871 char osd_text_tmp[64];
3872 if(pts==osd_last_pts-1) ++pts; else osd_last_pts=pts;
3873 vo_osd_text=osd_text_buffer;
3874 #ifdef USE_DVDNAV
3875 if (osd_show_dvd_nav_delay) {
3876 snprintf(osd_text_tmp, 63, "DVDNAV: %s", dvd_nav_text);
3877 osd_show_dvd_nav_delay--;
3878 } else
3879 #endif
3880 #ifdef USE_TV
3881 if (osd_show_tv_channel && tv_channel_list) {
3882 snprintf(osd_text_tmp, 63, "Channel: %s", tv_channel_current->name);
3883 osd_show_tv_channel--;
3884 } else
3885 #endif
3886 if (osd_show_text) {
3887 snprintf(osd_text_tmp, 63, "%s", osd_show_text_buffer);
3888 osd_show_text--;
3889 } else
3890 if (osd_show_sub_visibility) {
3891 snprintf(osd_text_tmp, 63, "Subtitles: %sabled", sub_visibility?"en":"dis");
3892 osd_show_sub_visibility--;
3893 } else
3894 if (osd_show_vobsub_changed) {
3895 if (vo_vobsub && vobsub_id >= 0) {
3896 const char *language = "none";
3897 language = vobsub_get_id(vo_vobsub, (unsigned int) vobsub_id);
3898 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", vobsub_id, language ? language : "unknown");
3900 #ifdef HAVE_OGGVORBIS
3901 if (d_dvdsub && demuxer->type == DEMUXER_TYPE_OGG) {
3902 if (dvdsub_id < 0)
3903 snprintf(osd_text_tmp, 63, "Subtitles: (off)");
3904 else {
3905 char *lang = demux_ogg_sub_lang(demuxer, dvdsub_id);
3906 if (!lang) lang = "unknown";
3907 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
3910 #endif
3911 #ifdef USE_DVDREAD
3912 if (vo_spudec && (demuxer->type != DEMUXER_TYPE_MATROSKA)) {
3913 char lang[5] = "none";
3914 int code = 0;
3915 if (dvdsub_id >= 0) code = dvd_lang_from_sid(stream, dvdsub_id);
3916 if (code) {
3917 lang[0] = code >> 8;
3918 lang[1] = code;
3919 lang[2] = 0;
3921 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
3923 #endif
3924 #ifdef HAVE_MATROSKA
3925 if (demuxer->type == DEMUXER_TYPE_MATROSKA) {
3926 char lang[10] = "unknown";
3927 if (dvdsub_id >= 0) {
3928 demux_mkv_get_sub_lang(demuxer, dvdsub_id, lang, 9);
3929 lang[9] = 0;
3930 } else
3931 strcpy(lang, "off");
3932 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
3934 #endif
3935 osd_show_vobsub_changed--;
3936 } else
3937 #ifdef USE_SUB
3938 if (osd_show_sub_changed) {
3939 char *tmp2;
3940 tmp = subdata->filename;
3941 if ((tmp2 = strrchr(tmp, '/'))) {
3942 tmp = tmp2+1;
3944 snprintf(osd_text_tmp, 63, "Sub: (%d) %s%s",
3945 set_of_sub_pos + 1,
3946 strlen(tmp) < 20 ? "" : "...",
3947 strlen(tmp) < 20 ? tmp : tmp+strlen(tmp)-19);
3948 osd_show_sub_changed--;
3949 } else
3950 #endif
3951 if (osd_show_sub_delay) {
3952 snprintf(osd_text_tmp, 63, "Sub delay: %d ms", ROUND(sub_delay*1000));
3953 osd_show_sub_delay--;
3954 } else
3955 if (osd_show_sub_pos) {
3956 snprintf(osd_text_tmp, 63, "Sub position: %d/100", sub_pos);
3957 osd_show_sub_pos--;
3958 } else
3959 if (osd_show_sub_alignment) {
3960 snprintf(osd_text_tmp, 63, "Sub alignment: %s",
3961 (sub_alignment == 2 ? "bottom" :
3962 (sub_alignment == 1 ? "center" : "top")));
3963 osd_show_sub_alignment--;
3964 } else
3965 if (osd_show_av_delay) {
3966 snprintf(osd_text_tmp, 63, "A-V delay: %d ms", ROUND(audio_delay*1000));
3967 osd_show_av_delay--;
3968 } else if (osd_show_speed) {
3969 snprintf(osd_text_tmp, 63, "Speed: x %6.2f", playback_speed);
3970 osd_show_speed--;
3971 } else if (osd_show_ontop) {
3972 snprintf(osd_text_tmp, 63, "Stay on top: %sabled", vo_ontop?"en":"dis");
3973 osd_show_ontop--;
3974 } else if (osd_show_rootwin) {
3975 snprintf(osd_text_tmp, 63, "Rootwin: %sabled", vo_rootwin?"en":"dis");
3976 osd_show_rootwin--;
3977 } else if (osd_show_framedropping) {
3978 snprintf(osd_text_tmp, 63, "Framedropping: %s",
3979 (frame_dropping == 1 ? "on" :
3980 (frame_dropping == 2 ? "hard" : "off")));
3981 osd_show_framedropping--;
3982 } else if(osd_level>=2) {
3983 int len = demuxer_get_time_length(demuxer);
3984 int percentage = -1;
3985 char percentage_text[10];
3986 if (osd_show_percentage) {
3987 percentage = demuxer_get_percent_pos(demuxer);
3988 osd_show_percentage--;
3990 if (percentage >= 0)
3991 snprintf(percentage_text, 9, " (%d%%)", percentage);
3992 else
3993 percentage_text[0] = 0;
3994 if (osd_level == 3)
3995 snprintf(osd_text_tmp, 63, "%c %02d:%02d:%02d / %02d:%02d:%02d%s",osd_function,pts/3600,(pts/60)%60,pts%60,len/3600,(len/60)%60,len%60,percentage_text);
3996 else
3997 snprintf(osd_text_tmp, 63, "%c %02d:%02d:%02d%s",osd_function,pts/3600,(pts/60)%60,pts%60,percentage_text);
3998 } else osd_text_tmp[0]=0;
4000 if(strcmp(vo_osd_text, osd_text_tmp)) {
4001 strncpy(vo_osd_text, osd_text_tmp, 63);
4002 vo_osd_changed(OSDTYPE_OSD);
4004 } else {
4005 if(vo_osd_text) {
4006 vo_osd_text=NULL;
4007 vo_osd_changed(OSDTYPE_OSD);
4010 // for(i=1;i<=11;i++) osd_text_buffer[10+i]=i;osd_text_buffer[10+i]=0;
4011 // vo_osd_text=osd_text_buffer;
4012 #endif
4014 #ifdef USE_SUB
4015 // find sub
4016 if(subdata && sh_video && sh_video->pts>0){
4017 float pts=sh_video->pts;
4018 if(sub_fps==0) sub_fps=sh_video->fps;
4019 current_module="find_sub";
4020 if (pts > sub_last_pts || pts < sub_last_pts-1.0 ) {
4021 find_sub(subdata, (pts+sub_delay) *
4022 (subdata->sub_uses_time ? 100. : sub_fps));
4023 if (vo_sub) vo_sub_last = vo_sub;
4024 // FIXME! frame counter...
4025 sub_last_pts = pts;
4027 current_module=NULL;
4029 #endif
4031 #ifdef HAVE_X11
4032 if (stop_xscreensaver && sh_video) {
4033 current_module="stop_xscreensaver";
4034 xscreensaver_heartbeat();
4035 current_module=NULL;
4037 #endif
4039 // DVD sub:
4040 if(vo_config_count && vo_spudec) {
4041 unsigned char* packet=NULL;
4042 int len,timestamp;
4043 current_module="spudec";
4044 spudec_heartbeat(vo_spudec,90000*sh_video->timer);
4045 // Get a sub packet from the dvd or a vobsub and make a timestamp relative to sh_video->timer
4046 while(1) {
4047 // Vobsub
4048 len = 0;
4049 if(vo_vobsub) {
4050 if(sh_video->pts+sub_delay>=0) {
4051 // The + next_frame_time is there because we'll display the sub at the next frame
4052 len = vobsub_get_packet(vo_vobsub,sh_video->pts+sub_delay+next_frame_time,(void**)&packet,&timestamp);
4053 if(len > 0) {
4054 timestamp -= (sh_video->pts + sub_delay - sh_video->timer)*90000;
4055 mp_dbg(MSGT_CPLAYER,MSGL_V,"\rVOB sub: len=%d v_pts=%5.3f v_timer=%5.3f sub=%5.3f ts=%d \n",len,sh_video->pts,sh_video->timer,timestamp / 90000.0,timestamp);
4058 } else {
4059 // DVD sub
4060 len = ds_get_packet_sub(d_dvdsub,(unsigned char**)&packet);
4061 if(len > 0) {
4062 float x = d_dvdsub->pts - sh_video->pts;
4063 if (x < -10 || x > 10) // prevent missing subs on pts reset
4064 timestamp = 90000*(sh_video->timer + d_dvdsub->pts + sub_delay - sh_video->pts);
4065 else timestamp = 90000*(sh_video->timer + sub_delay);
4066 mp_dbg(MSGT_CPLAYER,MSGL_V,"\rDVD sub: len=%d v_pts=%5.3f s_pts=%5.3f ts=%d \n",len,sh_video->pts,d_dvdsub->pts,timestamp);
4069 if(len<=0 || !packet) break;
4070 if(timestamp < 0) timestamp = 0;
4071 else spudec_assemble(vo_spudec,packet,len,timestamp);
4074 /* detect wether the sub has changed or not */
4075 if(spudec_changed(vo_spudec))
4076 vo_osd_changed(OSDTYPE_SPU);
4077 current_module=NULL;
4080 } // while(!eof)
4082 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",eof);
4086 goto_next_file: // don't jump here after ao/vo/getch initialization!
4088 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4090 if(benchmark){
4091 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4092 double total_time_usage;
4093 total_time_usage_start=GetTimer()-total_time_usage_start;
4094 total_time_usage = (float)total_time_usage_start*0.000001;
4095 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4096 video_time_usage,vout_time_usage,audio_time_usage,
4097 total_time_usage-tot,total_time_usage);
4098 if(total_time_usage>0.0)
4099 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4100 100.0*video_time_usage/total_time_usage,
4101 100.0*vout_time_usage/total_time_usage,
4102 100.0*audio_time_usage/total_time_usage,
4103 100.0*(total_time_usage-tot)/total_time_usage,
4104 100.0);
4105 if(total_frame_cnt && frame_dropping)
4106 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4107 total_frame_cnt-drop_frame_cnt,
4108 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4109 drop_frame_cnt,
4110 100*drop_frame_cnt/total_frame_cnt,
4111 total_frame_cnt,
4112 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4116 // time to uninit all, except global stuff:
4117 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
4119 #ifdef USE_SUB
4120 if ( set_of_sub_size > 0 )
4122 current_module="sub_free";
4123 for (i = 0; i < set_of_sub_size; ++i)
4124 sub_free( set_of_subtitles[i] );
4125 set_of_sub_size = 0;
4126 vo_sub_last = vo_sub=NULL;
4127 subdata=NULL;
4129 #endif
4131 if(eof == PT_NEXT_ENTRY || eof == PT_PREV_ENTRY) {
4132 eof = eof == PT_NEXT_ENTRY ? 1 : -1;
4133 if(play_tree_iter_step(playtree_iter,play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
4134 eof = 1;
4135 } else {
4136 play_tree_iter_free(playtree_iter);
4137 playtree_iter = NULL;
4139 play_tree_step = 1;
4140 } else if (eof == PT_UP_NEXT || eof == PT_UP_PREV) {
4141 eof = eof == PT_UP_NEXT ? 1 : -1;
4142 if ( playtree_iter ) {
4143 if(play_tree_iter_up_step(playtree_iter,eof,0) == PLAY_TREE_ITER_ENTRY) {
4144 eof = 1;
4145 } else {
4146 play_tree_iter_free(playtree_iter);
4147 playtree_iter = NULL;
4150 } else { // NEXT PREV SRC
4151 eof = eof == PT_PREV_SRC ? -1 : 1;
4154 if(eof == 0) eof = 1;
4156 while(playtree_iter != NULL) {
4157 filename = play_tree_iter_get_file(playtree_iter,eof);
4158 if(filename == NULL) {
4159 if( play_tree_iter_step(playtree_iter,eof,0) != PLAY_TREE_ITER_ENTRY) {
4160 play_tree_iter_free(playtree_iter);
4161 playtree_iter = NULL;
4163 } else
4164 break;
4167 #ifdef HAVE_NEW_GUI
4168 if( use_gui && !playtree_iter )
4170 #ifdef USE_DVDREAD
4171 if ( !guiIntfStruct.DiskChanged )
4172 #endif
4173 mplEnd();
4175 #endif
4177 if(use_gui || playtree_iter != NULL){
4179 eof = 0;
4180 goto play_next_file;
4184 exit_player_with_rc(MSGTR_Exit_eof, 0);
4186 return 1;