10l initial patch by Oded Shimon <ods15 at ods15.dyndns.org>
[mplayer/greg.git] / mplayer.c
blobe7f5fc7d550a34dc9e42b60f647febd5c7a03526
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"
61 #include "libao2/audio_plugin.h"
63 #include "codec-cfg.h"
65 #ifdef USE_DVDNAV
66 #include <dvdnav.h>
67 #endif
69 #ifdef USE_EDL
70 #include "edl.h"
71 #endif
73 #include "spudec.h"
74 #include "vobsub.h"
76 #include "osdep/getch2.h"
77 #include "osdep/timer.h"
79 #include "cpudetect.h"
81 #ifdef HAVE_NEW_GUI
82 #include "Gui/interface.h"
83 #endif
85 #include "input/input.h"
87 int slave_mode=0;
88 int verbose=0;
89 int identify=0;
90 int quiet=0;
92 #define ABS(x) (((x)>=0)?(x):(-(x)))
93 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
95 #ifdef HAVE_RTC
96 #include <linux/rtc.h>
97 #endif
99 #ifdef USE_TV
100 #include "libmpdemux/tv.h"
101 #endif
103 #ifdef HAS_DVBIN_SUPPORT
104 #include "libmpdemux/dvbin.h"
105 static int last_dvb_step = 1;
106 #endif
108 //**************************************************************************//
109 // Playtree
110 //**************************************************************************//
111 #include "playtree.h"
112 #include "playtreeparser.h"
114 #ifdef HAVE_NEW_GUI
115 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
116 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
117 #endif
119 play_tree_t* playtree;
120 play_tree_iter_t* playtree_iter = NULL;
122 #define PT_NEXT_ENTRY 1
123 #define PT_PREV_ENTRY -1
124 #define PT_NEXT_SRC 2
125 #define PT_PREV_SRC -2
126 #define PT_UP_NEXT 3
127 #define PT_UP_PREV -3
129 //**************************************************************************//
130 // Config
131 //**************************************************************************//
132 m_config_t* mconfig;
134 extern play_tree_t*
135 m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv);
136 extern int
137 m_config_parse_config_file(m_config_t* config, char *conffile);
139 //**************************************************************************//
140 // Config file
141 //**************************************************************************//
143 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
145 static int cfg_include(m_option_t *conf, char *filename){
146 return m_config_parse_config_file(mconfig, filename);
149 #include "get_path.c"
151 //**************************************************************************//
152 // XScreensaver
153 //**************************************************************************//
155 #ifdef HAVE_X11
156 void xscreensaver_heartbeat(void);
157 #endif
159 //**************************************************************************//
160 //**************************************************************************//
161 // Input media streaming & demultiplexer:
162 //**************************************************************************//
164 static int max_framesize=0;
166 #include "libmpdemux/stream.h"
167 #include "libmpdemux/demuxer.h"
168 #include "libmpdemux/stheader.h"
169 //#include "parse_es.h"
170 #ifdef HAVE_MATROSKA
171 #include "libmpdemux/matroska.h"
172 #endif
174 #include "libmpcodecs/dec_audio.h"
175 #include "libmpcodecs/dec_video.h"
176 #include "libmpcodecs/mp_image.h"
177 #include "libmpcodecs/vf.h"
179 extern void vf_list_plugins();
181 //**************************************************************************//
182 //**************************************************************************//
184 // Common FIFO functions, and keyboard/event FIFO code
185 #include "fifo.c"
186 int noconsolecontrols=0;
187 //**************************************************************************//
189 vo_functions_t *video_out=NULL;
190 ao_functions_t *audio_out=NULL;
192 int fixed_vo=0;
193 int eof=0;
195 // benchmark:
196 double video_time_usage=0;
197 double vout_time_usage=0;
198 static double audio_time_usage=0;
199 static int total_time_usage_start=0;
200 static int total_frame_cnt=0;
201 static int drop_frame_cnt=0; // total number of dropped frames
202 int benchmark=0;
204 // options:
205 int auto_quality=0;
206 static int output_quality=0;
208 float playback_speed=1.0;
210 int use_gui=0;
212 #ifdef HAVE_NEW_GUI
213 int enqueue=0;
214 #endif
216 #define MAX_OSD_LEVEL 3
218 int osd_level=1;
219 int osd_level_saved=-1;
220 int osd_visible=100;
222 // seek:
223 static char *seek_to_sec=NULL;
224 static off_t seek_to_byte=0;
225 static off_t step_sec=0;
226 static int loop_times=-1;
227 static int loop_seek=0;
229 // A/V sync:
230 int autosync=0; // 30 might be a good default value.
232 // may be changed by GUI: (FIXME!)
233 float rel_seek_secs=0;
234 int abs_seek_pos=0;
236 // codecs:
237 char **audio_codec_list=NULL; // override audio codec
238 char **video_codec_list=NULL; // override video codec
239 char **audio_fm_list=NULL; // override audio codec family
240 char **video_fm_list=NULL; // override video codec family
242 // streaming:
243 int audio_id=-1;
244 int video_id=-1;
245 int dvdsub_id=-1;
246 int vobsub_id=-1;
247 char* audio_lang=NULL;
248 char* dvdsub_lang=NULL;
249 static char* spudec_ifo=NULL;
250 char* filename=NULL; //"MI2-Trailer.avi";
251 int forced_subs_only=0;
253 // cache2:
254 int stream_cache_size=-1;
255 #ifdef USE_STREAM_CACHE
256 extern int cache_fill_status;
258 float stream_cache_min_percent=20.0;
259 float stream_cache_prefill_percent=5.0;
260 #else
261 #define cache_fill_status 0
262 #endif
264 // dump:
265 static char *stream_dump_name="stream.dump";
266 int stream_dump_type=0;
268 // A-V sync:
269 static float default_max_pts_correction=-1;//0.01f;
270 static float max_pts_correction=0;//default_max_pts_correction;
271 static float c_total=0;
272 float audio_delay=0;
274 static int softsleep=0;
276 float force_fps=0;
277 static int force_srate=0;
278 static int audio_output_format=0;
279 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
280 static int play_n_frames=-1;
281 static int play_n_frames_mf=-1;
283 // screen info:
284 char** video_driver_list=NULL;
285 char** audio_driver_list=NULL;
287 extern char *vo_subdevice;
288 extern char *ao_subdevice;
290 // codec outfmt flags (defined in libmpcodecs/vd.c)
291 extern int vo_flags;
293 // sub:
294 char *font_name=NULL;
295 #ifdef HAVE_FONTCONFIG
296 extern int font_fontconfig;
297 #endif
298 float font_factor=0.75;
299 char **sub_name=NULL;
300 float sub_delay=0;
301 float sub_fps=0;
302 int sub_auto = 1;
303 char *vobsub_name=NULL;
304 /*DSP!!char *dsp=NULL;*/
305 int subcc_enabled=0;
306 int suboverlap_enabled = 1;
307 #ifdef USE_SUB
308 sub_data* set_of_subtitles[MAX_SUBTITLE_FILES];
309 int set_of_sub_size = 0;
310 int set_of_sub_pos = -1;
311 float sub_last_pts = -303;
312 #endif
313 int global_sub_size = 0; // this encompasses all subtitle sources
314 int global_sub_pos = -1; // this encompasses all subtitle sources
315 #define SUB_SOURCE_SUBS 0
316 #define SUB_SOURCE_VOBSUB 1
317 #define SUB_SOURCE_DEMUX 2
318 #define SUB_SOURCES 3
319 int global_sub_indices[SUB_SOURCES];
320 int global_sub_quiet_osd_hack = 0;
322 static stream_t* stream=NULL;
323 static demuxer_t *demuxer=NULL;
324 static sh_audio_t *sh_audio=NULL;
325 static sh_video_t *sh_video=NULL;
327 char* current_module=NULL; // for debugging
329 extern int vo_gamma_gamma;
330 extern int vo_gamma_brightness;
331 extern int vo_gamma_contrast;
332 extern int vo_gamma_saturation;
333 extern int vo_gamma_hue;
335 // ---
337 #ifdef HAVE_MENU
338 #include "m_struct.h"
339 #include "libmenu/menu.h"
340 extern void vf_menu_pause_update(struct vf_instance_s* vf);
341 extern vf_info_t vf_info_menu;
342 static vf_info_t* libmenu_vfs[] = {
343 &vf_info_menu,
344 NULL
346 static vf_instance_t* vf_menu = NULL;
347 static int use_menu = 0;
348 static char* menu_cfg = NULL;
349 static char* menu_root = "main";
350 #endif
353 #ifdef HAVE_RTC
354 static int nortc;
355 static char* rtc_device;
356 #endif
358 #ifdef USE_EDL
359 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
360 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
361 int edl_memory_slots = 0; ///< number of EDL entries (1 for skip + 2 for each mute)
362 int edl_operations = 0; ///< number of EDL operations, skip + mute
363 short user_muted = 0; ///< Stores whether User wanted muted mode.
364 short edl_muted = 0; ///< Stores whether EDL is currently in muted mode.
365 short edl_decision = 0; ///< 1 when an EDL operation has been made
366 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode
367 #endif
369 static unsigned int inited_flags=0;
370 #define INITED_VO 1
371 #define INITED_AO 2
372 #define INITED_GUI 4
373 #define INITED_GETCH2 8
374 #define INITED_SPUDEC 32
375 #define INITED_STREAM 64
376 #define INITED_INPUT 128
377 #define INITED_VOBSUB 256
378 #define INITED_DEMUXER 512
379 #define INITED_ACODEC 1024
380 #define INITED_VCODEC 2048
381 #define INITED_ALL 0xFFFF
383 static void uninit_player(unsigned int mask){
384 mask=inited_flags&mask;
386 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
388 if(mask&INITED_ACODEC){
389 inited_flags&=~INITED_ACODEC;
390 current_module="uninit_acodec";
391 if(sh_audio) uninit_audio(sh_audio);
392 sh_audio=NULL;
395 if(mask&INITED_VCODEC){
396 inited_flags&=~INITED_VCODEC;
397 current_module="uninit_vcodec";
398 if(sh_video) uninit_video(sh_video);
399 sh_video=NULL;
400 #ifdef HAVE_MENU
401 vf_menu=NULL;
402 #endif
405 if(mask&INITED_DEMUXER){
406 inited_flags&=~INITED_DEMUXER;
407 current_module="free_demuxer";
408 if(demuxer){
409 stream=demuxer->stream;
410 free_demuxer(demuxer);
412 demuxer=NULL;
415 // kill the cache process:
416 if(mask&INITED_STREAM){
417 inited_flags&=~INITED_STREAM;
418 current_module="uninit_stream";
419 if(stream) free_stream(stream);
420 stream=NULL;
423 if(mask&INITED_VO){
424 inited_flags&=~INITED_VO;
425 current_module="uninit_vo";
426 video_out->uninit();
427 video_out=NULL;
430 // must be after libvo uninit, as few vo drivers (svgalib) has tty code
431 if(mask&INITED_GETCH2){
432 inited_flags&=~INITED_GETCH2;
433 current_module="uninit_getch2";
434 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
435 // restore terminal:
436 getch2_disable();
439 if(mask&INITED_VOBSUB){
440 inited_flags&=~INITED_VOBSUB;
441 current_module="uninit_vobsub";
442 if(vo_vobsub) vobsub_close(vo_vobsub);
443 vo_vobsub=NULL;
446 if (mask&INITED_SPUDEC){
447 inited_flags&=~INITED_SPUDEC;
448 current_module="uninit_spudec";
449 spudec_free(vo_spudec);
450 vo_spudec=NULL;
453 if(mask&INITED_AO){
454 inited_flags&=~INITED_AO;
455 current_module="uninit_ao";
456 audio_out->uninit(eof?0:1); audio_out=NULL;
459 #ifdef HAVE_NEW_GUI
460 if(mask&INITED_GUI){
461 inited_flags&=~INITED_GUI;
462 current_module="uninit_gui";
463 guiDone();
465 #endif
467 if(mask&INITED_INPUT){
468 inited_flags&=~INITED_INPUT;
469 current_module="uninit_input";
470 mp_input_uninit();
473 current_module=NULL;
476 static void exit_player_with_rc(char* how, int rc){
478 uninit_player(INITED_ALL);
479 #ifdef HAVE_X11
480 #ifdef HAVE_NEW_GUI
481 if ( !use_gui )
482 #endif
483 vo_uninit(); // close the X11 connection (if any opened)
484 #endif
486 #ifdef HAVE_FREETYPE
487 current_module="uninit_font";
488 if (vo_font) free_font_desc(vo_font);
489 vo_font = NULL;
490 done_freetype();
491 #endif
493 current_module="exit_player";
495 // free mplayer config
496 free(mconfig);
498 #ifdef USE_EDL
499 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
500 #endif
501 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,mp_gettext(how));
502 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
504 exit(rc);
507 void exit_player(char* how){
508 exit_player_with_rc(how, 1);
511 #ifndef __MINGW32__
512 static void child_sighandler(int x){
513 pid_t pid;
514 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
516 #endif
518 #ifdef CRASH_DEBUG
519 static char *prog_path;
520 static int crash_debug = 0;
521 #endif
523 static void exit_sighandler(int x){
524 static int sig_count=0;
525 #ifdef CRASH_DEBUG
526 if (!crash_debug || x != SIGTRAP)
527 #endif
528 ++sig_count;
529 if(inited_flags==0 && sig_count>1) exit(1);
530 if(sig_count==5)
532 /* We're crashing bad and can't uninit cleanly :(
533 * by popular request, we make one last (dirty)
534 * effort to restore the user's
535 * terminal. */
536 getch2_disable();
537 exit(1);
539 if(sig_count==6) exit(1);
540 if(sig_count>6){
541 // can't stop :(
542 #ifndef __MINGW32__
543 kill(getpid(),SIGKILL);
544 #endif
546 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
547 current_module?current_module:mp_gettext("unknown")
549 if(sig_count<=1)
550 switch(x){
551 case SIGINT:
552 case SIGQUIT:
553 case SIGTERM:
554 case SIGKILL:
555 break; // killed from keyboard (^C) or killed [-9]
556 case SIGILL:
557 #ifdef RUNTIME_CPUDETECT
558 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
559 #else
560 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
561 #endif
562 case SIGFPE:
563 case SIGSEGV:
564 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
565 default:
566 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
567 #ifdef CRASH_DEBUG
568 if (crash_debug) {
569 int gdb_pid;
570 char spid[20];
571 snprintf(spid, 19, "%i", getpid());
572 spid[19] = 0;
573 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
574 gdb_pid = fork();
575 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
576 if (gdb_pid == 0) { // We are the child
577 if (execlp("gdb", "gdb", prog_path, spid, NULL) == -1)
578 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
579 } else if (gdb_pid < 0)
580 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
581 else {
582 waitpid(gdb_pid, NULL, 0);
584 if (x == SIGTRAP) return;
586 #endif
588 exit_player(NULL);
591 //extern void write_avi_header_1(FILE *f,int fcc,float fps,int width,int height);
593 extern void mp_input_register_options(m_config_t* cfg);
595 #include "mixer.h"
596 mixer_t mixer;
597 /// step size of mixer changes
598 int volstep = 3;
600 #include "cfg-mplayer.h"
602 void parse_cfgfiles( m_config_t* conf )
604 char *conffile;
605 int conffile_fd;
606 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
607 exit_player(NULL);
608 if ((conffile = get_path("")) == NULL) {
609 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
610 } else {
611 #ifdef __MINGW32__
612 mkdir(conffile);
613 #else
614 mkdir(conffile, 0777);
615 #endif
616 free(conffile);
617 if ((conffile = get_path("config")) == NULL) {
618 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
619 } else {
620 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
621 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
622 write(conffile_fd, default_config, strlen(default_config));
623 close(conffile_fd);
625 if (m_config_parse_config_file(conf, conffile) < 0)
626 exit_player(NULL);
627 free(conffile);
632 void load_per_file_config (m_config_t* conf, const char *const file)
634 char *confpath;
635 char cfg[strlen(file)+10];
636 struct stat st;
637 char *name;
639 sprintf (cfg, "%s.conf", file);
641 if (!stat (cfg, &st))
643 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
644 m_config_parse_config_file (conf, cfg);
645 return;
648 if ((name = strrchr (cfg, '/')) == NULL)
649 name = cfg;
650 else
651 name++;
653 if ((confpath = get_path (name)) != NULL)
655 if (!stat (confpath, &st))
657 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
658 m_config_parse_config_file (conf, confpath);
661 free (confpath);
665 // When libmpdemux perform a blocking operation (network connection or cache filling)
666 // if the operation fail we use this function to check if it was interrupted by the user.
667 // The function return a new value for eof.
668 static int libmpdemux_was_interrupted(int eof) {
669 mp_cmd_t* cmd;
670 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
671 switch(cmd->id) {
672 case MP_CMD_QUIT:
673 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
674 case MP_CMD_PLAY_TREE_STEP: {
675 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
676 } break;
677 case MP_CMD_PLAY_TREE_UP_STEP: {
678 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
679 } break;
680 case MP_CMD_PLAY_ALT_SRC_STEP: {
681 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
682 } break;
684 mp_cmd_free(cmd);
686 return eof;
689 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
690 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
692 int playtree_add_playlist(play_tree_t* entry)
694 play_tree_add_bpf(entry,filename);
696 #ifdef HAVE_NEW_GUI
697 if (use_gui) {
698 if (entry) {
699 import_playtree_playlist_into_gui(entry, mconfig);
700 play_tree_free_list(entry,1);
702 } else
703 #endif
705 if(!entry) {
706 entry = playtree_iter->tree;
707 if(play_tree_iter_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
708 return PT_NEXT_ENTRY;
710 if(playtree_iter->tree == entry ) { // Loop with a single file
711 if(play_tree_iter_up_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
712 return PT_NEXT_ENTRY;
715 play_tree_remove(entry,1,1);
716 return PT_NEXT_SRC;
718 play_tree_insert_entry(playtree_iter->tree,entry);
719 play_tree_set_params_from(entry,playtree_iter->tree);
720 entry = playtree_iter->tree;
721 if(play_tree_iter_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
722 return PT_NEXT_ENTRY;
724 play_tree_remove(entry,1,1);
726 return PT_NEXT_SRC;
729 static int play_tree_step = 1;
731 int sub_source()
733 int source = -1;
734 int top = -1;
735 int i;
736 for (i = 0; i < SUB_SOURCES; i++) {
737 int j = global_sub_indices[i];
738 if ((j >= 0) && (j > top) && (global_sub_pos >= j)) {
739 source = i;
740 top = j;
743 return source;
746 #ifdef USE_SUB
748 sub_data* subdata = NULL;
750 void add_subtitles(char *filename, float fps, int silent)
752 sub_data *subd;
754 if (filename == NULL) {
755 return;
758 subd = sub_read_file(filename, fps);
759 if(!subd && !silent)
760 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub, filename);
761 if (subd == NULL || set_of_sub_size >= MAX_SUBTITLE_FILES) return;
762 set_of_subtitles[set_of_sub_size] = subd;
763 ++set_of_sub_size;
764 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_AddedSubtitleFile, set_of_sub_size, filename);
767 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
768 void update_set_of_subtitles()
769 // subdata was changed, set_of_sub... have to be updated.
771 int i;
772 if (set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
773 for (i = set_of_sub_pos + 1; i < set_of_sub_size; ++i)
774 set_of_subtitles[i-1] = set_of_subtitles[i];
775 set_of_subtitles[set_of_sub_size-1] = NULL;
776 --set_of_sub_size;
777 if (set_of_sub_size > 0) subdata = set_of_subtitles[set_of_sub_pos=0];
779 else if (set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
780 set_of_subtitles[set_of_sub_pos] = subdata;
782 else if (set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
783 set_of_subtitles[set_of_sub_pos=set_of_sub_size] = subdata;
784 ++set_of_sub_size;
787 #endif
790 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
791 * make it all work is to use the builtin SDL-bootstrap code, which
792 * will be done automatically by replacing our main() if we include SDL.h.
794 #if defined(SYS_DARWIN) && defined(HAVE_SDL)
795 #include <SDL.h>
796 #endif
799 * \brief append a formatted string
800 * \param buf buffer to print into
801 * \param pos position of terminating 0 in buf
802 * \param len maximum number of characters in buf, not including terminating 0
803 * \param format printf format string
805 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
807 va_list va;
808 va_start(va, format);
809 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
810 va_end(va);
811 if (*pos >= len ) {
812 buf[len] = 0;
813 *pos = len;
818 * \brief print the status line
819 * \param a_pos audio position
820 * \param a_v A-V desynchronization
821 * \param corr amount out A-V synchronization
823 static void print_status(float a_pos, float a_v, float corr)
825 int width;
826 char *line;
827 unsigned pos = 0;
828 get_screen_size();
829 if (screen_width > 0)
830 width = screen_width;
831 else
832 width = 80;
833 #ifdef WIN32
834 // windows command line is broken (MinGW's rxvt works though, but we
835 // should not depend on that).
836 width--;
837 #endif
838 line = malloc(width + 1); // one additional for terminating null
840 // Audio time
841 if (sh_audio) {
842 saddf(line, &pos, width, "A:%6.1f ", a_pos);
843 if (!sh_video) {
844 // convert time to HH:MM:SS.F format
845 long tenths = 10 * a_pos;
846 int f1 = tenths % 10;
847 int ss = (tenths / 10) % 60;
848 int mm = (tenths / 600) % 60;
849 int hh = (tenths / 36000) % 100;
850 saddf(line, &pos, width, "(");
851 if (hh > 0)
852 saddf(line, &pos, width, "%2d:", hh);
853 if (hh > 0 || mm > 0)
854 saddf(line, &pos, width, "%02d:", mm);
855 saddf(line, &pos, width, "%02d.", ss);
856 saddf(line, &pos, width, "%1d", f1);
857 saddf(line, &pos, width, ") ");
861 // Video time
862 if (sh_video)
863 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
865 // A-V sync
866 if (sh_audio && sh_video)
867 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
869 // Video stats
870 if (sh_video)
871 saddf(line, &pos, width, "%3d/%3d ",
872 (int)sh_video->num_frames,
873 (int)sh_video->num_frames_decoded);
875 // CPU usage
876 if (sh_video) {
877 if (sh_video->timer > 0.5)
878 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
879 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
880 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
881 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
882 else
883 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
884 } else if (sh_audio) {
885 if (sh_audio->delay > 0.5)
886 saddf(line, &pos, width, "%4.1f%% ",
887 100.0*audio_time_usage/(double)sh_audio->delay);
888 else
889 saddf(line, &pos, width, "??,?%% ");
892 // VO stats
893 if (sh_video)
894 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
896 #ifdef USE_STREAM_CACHE
897 // cache stats
898 if (stream_cache_size > 0)
899 saddf(line, &pos, width, "%d%% ", cache_fill_status);
900 #endif
902 // other
903 if (playback_speed != 1)
904 saddf(line, &pos, width, "%4.2fx ", playback_speed);
906 // end
907 memset(&line[pos], ' ', width - pos);
908 line[width] = 0;
909 mp_msg(MSGT_AVSYNC, MSGL_STATUS, "%s\r", line);
910 free(line);
914 * \brief build a chain of audio filters that converts the input format
915 * to the ao's format, taking into account the current playback_speed.
916 * \param sh_audio describes the requested input format of the chain.
917 * \param ao_data describes the requested output format of the chain.
919 static int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
921 int new_srate = sh_audio->samplerate * playback_speed;
922 if (!sh_audio)
923 return 0;
924 if (new_srate != ao_data->samplerate) {
925 // limits are taken from libaf/af_resample.c
926 if (new_srate < 8000)
927 new_srate = 8000;
928 if (new_srate > 192000)
929 new_srate = 192000;
930 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
932 return init_audio_filters(sh_audio, new_srate,
933 sh_audio->channels, sh_audio->sample_format, sh_audio->samplesize,
934 ao_data->samplerate, ao_data->channels, ao_data->format,
935 audio_out_format_bits(ao_data->format) / 8, /* ao_data.bps, */
936 ao_data->outburst * 4, ao_data->buffersize);
939 int main(int argc,char* argv[]){
942 char * mem_ptr;
944 static demux_stream_t *d_audio=NULL;
945 static demux_stream_t *d_video=NULL;
946 static demux_stream_t *d_dvdsub=NULL;
948 int file_format=DEMUXER_TYPE_UNKNOWN;
950 int delay_corrected=1;
952 // movie info:
954 int osd_function=OSD_PLAY;
955 int osd_last_pts=-303;
956 int osd_show_av_delay = 0;
957 int osd_show_text = 0;
958 int osd_show_speed = 0;
959 int osd_show_sub_delay = 0;
960 int osd_show_sub_pos = 0;
961 int osd_show_sub_visibility = 0;
962 int osd_show_sub_alignment = 0;
963 int osd_show_vobsub_changed = 0;
964 int osd_show_sub_changed = 0;
965 int osd_show_percentage = 0;
966 int osd_show_tv_channel = 25;
967 int osd_show_ontop = 0;
968 int osd_show_rootwin = 0;
969 int osd_show_framedropping = 0;
971 int rtc_fd=-1;
973 //float a_frame=0; // Audio
975 int i;
976 char *tmp;
978 int gui_no_filename=0;
981 srand((int) time(NULL));
983 mp_msg_init();
984 mp_msg_set_level(MSGL_STATUS);
986 mp_msg(MSGT_CPLAYER,MSGL_INFO, "MPlayer " VERSION " (C) 2000-2004 MPlayer Team\n");
987 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
988 GetCpuCaps(&gCpuCaps);
989 #ifdef ARCH_X86
990 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
991 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
992 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
993 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
994 #ifdef RUNTIME_CPUDETECT
995 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
996 #else
997 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
998 #ifdef HAVE_MMX
999 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
1000 #endif
1001 #ifdef HAVE_MMX2
1002 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
1003 #endif
1004 #ifdef HAVE_3DNOW
1005 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
1006 #endif
1007 #ifdef HAVE_3DNOWEX
1008 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
1009 #endif
1010 #ifdef HAVE_SSE
1011 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
1012 #endif
1013 #ifdef HAVE_SSE2
1014 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
1015 #endif
1016 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
1017 #endif
1018 #endif
1020 #if defined(WIN32) && defined(USE_WIN32DLL)
1021 { /*make our codec dirs available for LoadLibraryA()*/
1022 char tmppath[MAX_PATH*2 + 1];
1023 char win32path[MAX_PATH];
1024 char realpath[MAX_PATH];
1025 #ifdef __CYGWIN__
1026 cygwin_conv_to_full_win32_path(WIN32_PATH,win32path);
1027 strcpy(tmppath,win32path);
1028 #ifdef USE_REALCODECS
1029 cygwin_conv_to_full_win32_path(REALCODEC_PATH,realpath);
1030 sprintf(tmppath,"%s;%s",win32path,realpath);
1031 #endif /*USE_REALCODECS*/
1032 #else
1033 if(!strstr(WIN32_PATH,":")){
1034 GetModuleFileNameA(NULL, win32path, MAX_PATH);
1035 strcpy(win32path + strlen(win32path) - strlen("mplayer.exe"), WIN32_PATH);
1037 else strcpy(win32path,WIN32_PATH);
1038 strcpy(tmppath,win32path);
1039 #ifdef USE_REALCODECS
1040 if(!strstr(REALCODEC_PATH,":")){
1041 GetModuleFileNameA(NULL, realpath, MAX_PATH);
1042 strcpy(realpath + strlen(realpath) - strlen("mplayer.exe"), REALCODEC_PATH);
1044 else strcpy(realpath,REALCODEC_PATH);
1045 sprintf(tmppath,"%s;%s",win32path,realpath);
1046 #endif /*USE_REALCODECS*/
1047 #endif /*__CYGWIN__*/
1048 SetEnvironmentVariableA("PATH", tmppath);
1050 #endif /*WIN32 && USE_WIN32DLL*/
1052 #ifdef USE_TV
1053 tv_param_immediate = 1;
1054 #endif
1056 if ( argv[0] )
1057 if(!strcmp(argv[0],"gmplayer") ||
1058 (strrchr(argv[0],'/') && !strcmp(strrchr(argv[0],'/'),"/gmplayer") ) )
1059 use_gui=1;
1061 mconfig = m_config_new();
1062 m_config_register_options(mconfig,mplayer_opts);
1063 // TODO : add something to let modules register their options
1064 mp_input_register_options(mconfig);
1065 parse_cfgfiles(mconfig);
1067 #ifdef HAVE_NEW_GUI
1068 if ( use_gui ) cfg_read();
1069 #endif
1071 playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
1072 if(playtree == NULL)
1073 exit_player(NULL);
1075 playtree = play_tree_cleanup(playtree);
1076 if(playtree) {
1077 playtree_iter = play_tree_iter_new(playtree,mconfig);
1078 if(playtree_iter) {
1079 if(play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
1080 play_tree_iter_free(playtree_iter);
1081 playtree_iter = NULL;
1083 filename = play_tree_iter_get_file(playtree_iter,1);
1087 #ifndef HAVE_NEW_GUI
1088 if(use_gui){
1089 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
1090 use_gui=0;
1092 #else
1093 if(use_gui && !vo_init()){
1094 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
1095 use_gui=0;
1097 if (use_gui && playtree_iter){
1098 char cwd[PATH_MAX+2];
1099 // Remove Playtree and Playtree-Iter from memory as its not used by gui
1100 play_tree_iter_free(playtree_iter);
1101 playtree_iter=NULL;
1103 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
1105 strcat(cwd, "/");
1106 // Prefix relative paths with current working directory
1107 play_tree_add_bpf(playtree, cwd);
1109 // Import initital playtree into gui
1110 import_initial_playtree_into_gui(playtree, mconfig, enqueue);
1112 #endif
1114 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
1115 list_video_out();
1116 exit_player_with_rc(NULL, 0);
1119 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
1120 list_audio_out();
1121 exit_player_with_rc(NULL, 0);
1124 // check codec.conf
1125 if(!codecs_file || !parse_codec_cfg(codecs_file)){
1126 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
1127 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
1128 if(!parse_codec_cfg(NULL)){
1129 mp_msg(MSGT_CPLAYER,MSGL_HINT,MSGTR_CopyCodecsConf);
1130 exit_player_with_rc(NULL, 0);
1132 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
1135 free( mem_ptr ); // release the buffer created by get_path()
1138 #if 0
1139 if(video_codec_list){
1140 int i;
1141 video_codec=video_codec_list[0];
1142 for(i=0;video_codec_list[i];i++)
1143 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
1145 #endif
1146 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
1147 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
1148 list_codecs(1);
1149 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1150 exit_player_with_rc(NULL, 0);
1152 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
1153 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
1154 list_codecs(0);
1155 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1156 exit_player_with_rc(NULL, 0);
1158 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
1159 vfm_help();
1160 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1161 exit_player_with_rc(NULL, 0);
1163 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
1164 afm_help();
1165 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1166 exit_player_with_rc(NULL, 0);
1168 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
1169 af_help();
1170 printf("\n");
1171 exit_player_with_rc(NULL, 0);
1173 #ifdef HAVE_X11
1174 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
1175 fstype_help();
1176 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1177 exit_player_with_rc(NULL, 0);
1179 #endif
1181 #ifdef USE_EDL
1182 if (edl_check_mode() == EDL_ERROR && edl_filename)
1184 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantUseBothModes);
1185 exit_player(NULL);
1186 } else if (edl_filename)
1188 edl_memory_slots = edl_count_entries();
1189 if (edl_memory_slots > 0)
1191 edl_records = calloc(edl_memory_slots, sizeof(struct edl_record));
1192 if (edl_records == NULL)
1194 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_EdlOutOfMem);
1195 exit_player(NULL);
1196 } else
1198 if ((edl_operations = edl_parse_file(edl_records)) > 0)
1200 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_EdlRecordsNo,
1201 edl_operations);
1202 } else
1205 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_EdlQueueEmpty);
1210 next_edl_record = edl_records;
1212 } else if (edl_output_filename)
1214 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
1216 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
1217 edl_output_filename);
1218 exit_player(NULL);
1221 #endif
1223 if(!filename){
1224 if(!use_gui){
1225 // no file/vcd/dvd -> show HELP:
1226 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
1227 exit_player_with_rc(NULL, 0);
1228 } else gui_no_filename=1;
1231 // Many users forget to include command line in bugreports...
1232 if(verbose>0){
1233 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
1234 for(i=1;i<argc;i++)mp_msg(MSGT_FIXME, MSGL_FIXME," '%s'",argv[i]);
1235 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
1238 mp_msg_set_level(verbose+MSGL_STATUS);
1240 //------ load global data first ------
1242 // check font
1243 #ifdef USE_OSD
1244 #ifdef HAVE_FREETYPE
1245 init_freetype();
1246 #endif
1247 #ifdef HAVE_FONTCONFIG
1248 if(!font_fontconfig)
1250 #endif
1251 if(font_name){
1252 vo_font=read_font_desc(font_name,font_factor,verbose>1);
1253 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
1254 } else {
1255 // try default:
1256 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
1257 free(mem_ptr); // release the buffer created by get_path()
1258 if(!vo_font)
1259 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
1261 #ifdef HAVE_FONTCONFIG
1263 #endif
1264 #endif
1265 vo_init_osd();
1267 #ifdef HAVE_RTC
1268 if(!nortc)
1270 // seteuid(0); /* Can't hurt to try to get root here */
1271 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
1272 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
1273 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
1274 else {
1275 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
1277 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
1278 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
1279 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
1280 close (rtc_fd);
1281 rtc_fd = -1;
1282 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1283 /* variable only by the root */
1284 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
1285 close (rtc_fd);
1286 rtc_fd = -1;
1287 } else
1288 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
1291 #ifdef HAVE_NEW_GUI
1292 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
1293 // and now ? -- Pontscho
1294 if(use_gui) setuid( getuid() ); // strongly test, please check this.
1295 #endif
1296 if(rtc_fd<0)
1297 #endif
1298 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
1299 softsleep?"software":timer_name);
1301 #ifdef USE_TERMCAP
1302 if ( !use_gui ) load_termcap(NULL); // load key-codes
1303 #endif
1305 // ========== Init keyboard FIFO (connection to libvo) ============
1307 // Init input system
1308 current_module = "init_input";
1309 mp_input_init();
1310 #ifndef HAVE_NO_POSIX_SELECT
1311 make_pipe(&keyb_fifo_get,&keyb_fifo_put);
1313 if(keyb_fifo_get > 0)
1314 mp_input_add_key_fd(keyb_fifo_get,1,NULL,NULL);
1315 #else
1316 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
1317 #endif
1318 if(slave_mode)
1319 #ifndef __MINGW32__
1320 mp_input_add_cmd_fd(0,1,NULL,NULL);
1321 #else
1322 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
1323 #endif
1324 else if(!noconsolecontrols)
1325 #ifndef HAVE_NO_POSIX_SELECT
1326 mp_input_add_key_fd(0,1,NULL,NULL);
1327 #else
1328 mp_input_add_key_fd(0,0,NULL,NULL);
1329 #endif
1331 inited_flags|=INITED_INPUT;
1332 current_module = NULL;
1334 #ifdef HAVE_MENU
1335 if(use_menu) {
1336 if(menu_cfg && menu_init(menu_cfg))
1337 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
1338 else {
1339 menu_cfg = get_path("menu.conf");
1340 if(menu_init(menu_cfg))
1341 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
1342 else {
1343 if(menu_init(MPLAYER_CONFDIR "/menu.conf"))
1344 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
1345 else {
1346 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
1347 use_menu = 0;
1352 #endif
1354 /// Catch signals
1355 #ifndef __MINGW32__
1356 signal(SIGCHLD,child_sighandler);
1357 #endif
1359 #ifdef CRASH_DEBUG
1360 prog_path = argv[0];
1361 #endif
1362 //========= Catch terminate signals: ================
1363 // terminate requests:
1364 signal(SIGTERM,exit_sighandler); // kill
1365 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
1367 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1369 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1370 #ifdef ENABLE_SIGHANDLER
1371 // fatal errors:
1372 signal(SIGBUS,exit_sighandler); // bus error
1373 signal(SIGSEGV,exit_sighandler); // segfault
1374 signal(SIGILL,exit_sighandler); // illegal instruction
1375 signal(SIGFPE,exit_sighandler); // floating point exc.
1376 signal(SIGABRT,exit_sighandler); // abort()
1377 #ifdef CRASH_DEBUG
1378 if (crash_debug)
1379 signal(SIGTRAP,exit_sighandler);
1380 #endif
1381 #endif
1383 #ifdef HAVE_NEW_GUI
1384 if(use_gui){
1385 guiInit();
1386 inited_flags|=INITED_GUI;
1387 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
1389 #endif
1391 // ******************* Now, let's see the per-file stuff ********************
1393 play_next_file:
1395 // init global sub numbers
1396 global_sub_size = 0;
1397 { int i; for (i = 0; i < SUB_SOURCES; i++) global_sub_indices[i] = -1; }
1398 global_sub_quiet_osd_hack = 1;
1400 if (filename) load_per_file_config (mconfig, filename);
1402 // We must enable getch2 here to be able to interrupt network connection
1403 // or cache filling
1404 if(!noconsolecontrols && !slave_mode){
1405 if(inited_flags&INITED_GETCH2)
1406 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
1407 else
1408 getch2_enable(); // prepare stdin for hotkeys...
1409 inited_flags|=INITED_GETCH2;
1410 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
1413 // =================== GUI idle loop (STOP state) ===========================
1414 #ifdef HAVE_NEW_GUI
1415 if ( use_gui ) {
1416 file_format=DEMUXER_TYPE_UNKNOWN;
1417 guiGetEvent( guiSetDefaults,0 );
1418 while ( guiIntfStruct.Playing != 1 )
1420 mp_cmd_t* cmd;
1421 usec_sleep(20000);
1422 guiEventHandling();
1423 guiGetEvent( guiReDraw,NULL );
1424 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) guiGetEvent( guiIEvent,(char *)cmd->id );
1426 guiGetEvent( guiSetParameters,NULL );
1427 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
1429 play_tree_t * entry = play_tree_new();
1430 play_tree_add_file( entry,guiIntfStruct.Filename );
1431 if ( playtree ) play_tree_free_list( playtree->child,1 );
1432 else playtree=play_tree_new();
1433 play_tree_set_child( playtree,entry );
1434 if(playtree)
1436 playtree_iter = play_tree_iter_new(playtree,mconfig);
1437 if(playtree_iter)
1439 if(play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
1441 play_tree_iter_free(playtree_iter);
1442 playtree_iter = NULL;
1444 filename = play_tree_iter_get_file(playtree_iter,1);
1449 #endif
1450 //---------------------------------------------------------------------------
1452 if(filename) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing, filename);
1454 //==================== Open VOB-Sub ============================
1456 current_module="vobsub";
1457 if (vobsub_name){
1458 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
1459 if(vo_vobsub==NULL)
1460 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,vobsub_name);
1461 }else if(sub_auto && filename && (strlen(filename)>=5)){
1462 /* try to autodetect vobsub from movie filename ::atmos */
1463 char *buf = malloc((strlen(filename)-3) * sizeof(char));
1464 memset(buf,0,strlen(filename)-3); // make sure string is terminated
1465 strncpy(buf, filename, strlen(filename)-4);
1466 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
1467 free(buf);
1469 if(vo_vobsub){
1470 inited_flags|=INITED_VOBSUB;
1471 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
1472 // check if vobsub requested only to display forced subtitles
1473 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
1475 // setup global sub numbering
1476 global_sub_indices[SUB_SOURCE_VOBSUB] = global_sub_size; // the global # of the first vobsub.
1477 global_sub_size += vobsub_get_indexes_count(vo_vobsub);
1480 //============ Open & Sync STREAM --- fork cache2 ====================
1482 stream=NULL;
1483 demuxer=NULL;
1484 if (d_audio) {
1485 //free_demuxer_stream(d_audio);
1486 d_audio=NULL;
1488 if (d_video) {
1489 //free_demuxer_stream(d_video);
1490 d_video=NULL;
1492 sh_audio=NULL;
1493 sh_video=NULL;
1495 current_module="open_stream";
1496 stream=open_stream(filename,0,&file_format);
1497 if(!stream) { // error...
1498 eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
1499 goto goto_next_file;
1501 inited_flags|=INITED_STREAM;
1503 #ifdef HAVE_NEW_GUI
1504 if ( use_gui ) guiGetEvent( guiSetStream,(char *)stream );
1505 #endif
1507 if(file_format == DEMUXER_TYPE_PLAYLIST) {
1508 play_tree_t* entry;
1509 // Handle playlist
1510 current_module="handle_playlist";
1511 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",filename);
1512 entry = parse_playtree(stream,0);
1513 eof=playtree_add_playlist(entry);
1514 goto goto_next_file;
1516 stream->start_pos+=seek_to_byte;
1518 if(stream_dump_type==5){
1519 unsigned char buf[4096];
1520 int len;
1521 FILE *f;
1522 current_module="dumpstream";
1523 if(stream->type==STREAMTYPE_STREAM && stream->fd<0){
1524 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
1525 exit_player(MSGTR_Exit_error);
1527 stream_reset(stream);
1528 stream_seek(stream,stream->start_pos);
1529 f=fopen(stream_dump_name,"wb");
1530 if(!f){
1531 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
1532 exit_player(MSGTR_Exit_error);
1534 while(!stream->eof){
1535 len=stream_read(stream,buf,4096);
1536 if(len>0) {
1537 if(fwrite(buf,len,1,f) != 1) {
1538 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
1539 exit_player(MSGTR_Exit_error);
1543 if(fclose(f)) {
1544 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
1545 exit_player(MSGTR_Exit_error);
1547 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
1548 exit_player_with_rc(MSGTR_Exit_eof, 0);
1551 #ifdef USE_DVDREAD
1552 if(stream->type==STREAMTYPE_DVD){
1553 current_module="dvd lang->id";
1554 if(audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
1555 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
1556 // setup global sub numbering
1557 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
1558 global_sub_size += dvd_number_of_subs(stream);
1559 current_module=NULL;
1561 #endif
1563 #ifdef USE_DVDNAV
1564 if (stream->type==STREAMTYPE_DVDNAV) stream_cache_size=0; // must disable caching...
1565 #endif
1567 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
1568 #ifdef HAS_DVBIN_SUPPORT
1569 goto_enable_cache:
1570 #endif
1571 if(stream_cache_size>0){
1572 current_module="enable_cache";
1573 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)))
1574 if((eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
1577 //============ Open DEMUXERS --- DETECT file type =======================
1578 current_module="demux_open";
1580 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
1582 // HACK to get MOV Reference Files working
1584 if (demuxer && demuxer->type==DEMUXER_TYPE_PLAYLIST)
1586 unsigned char* playlist_entry;
1587 play_tree_t *list = NULL, *entry = NULL;
1589 current_module="handle_demux_playlist";
1590 while (ds_get_packet(demuxer->video,&playlist_entry)>0)
1592 char *temp, *bname;
1594 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",playlist_entry);
1596 bname=mp_basename(playlist_entry);
1597 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
1598 continue;
1600 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
1601 continue;
1603 entry = play_tree_new();
1605 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
1607 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1)*sizeof(char));
1608 if (temp)
1610 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
1611 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
1612 strcat(temp, playlist_entry);
1613 play_tree_add_file(entry,temp);
1614 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
1615 free(temp);
1618 else
1619 play_tree_add_file(entry,playlist_entry);
1621 if(!list)
1622 list = entry;
1623 else
1624 play_tree_append_entry(list,entry);
1626 free_demuxer(demuxer);
1627 demuxer = NULL;
1629 if (list)
1631 entry = play_tree_new();
1632 play_tree_set_child(entry,list);
1633 eof=playtree_add_playlist(entry);
1634 goto goto_next_file;
1638 if(!demuxer)
1640 #if 0
1641 play_tree_t* entry;
1642 // Handle playlist
1643 current_module="handle_playlist";
1644 switch(stream->type){
1645 case STREAMTYPE_VCD:
1646 case STREAMTYPE_DVD:
1647 case STREAMTYPE_DVDNAV:
1648 case STREAMTYPE_CDDA:
1649 case STREAMTYPE_VCDBINCUE:
1650 // don't try to parse raw media as playlist, it's unlikely
1651 goto goto_next_file;
1653 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FallingBackOnPlaylist,filename);
1654 stream_reset(stream);
1655 stream_seek(stream,stream->start_pos);
1656 entry = parse_playtree(stream,0);
1657 if(!entry)
1658 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
1659 else
1660 eof=playtree_add_playlist(entry);
1661 #endif
1662 goto goto_next_file;
1664 inited_flags|=INITED_DEMUXER;
1666 #ifdef HAVE_MATROSKA
1667 if (demuxer->type==DEMUXER_TYPE_MATROSKA) {
1668 // setup global sub numbering
1669 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
1670 global_sub_size += demux_mkv_num_subs(demuxer);
1672 #endif
1673 #ifdef HAVE_OGGVORBIS
1674 if (demuxer->type==DEMUXER_TYPE_OGG) {
1675 // setup global sub numbering
1676 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
1677 global_sub_size += demux_ogg_num_subs(demuxer);
1679 #endif
1681 current_module="demux_open2";
1683 //file_format=demuxer->file_format;
1685 d_audio=demuxer->audio;
1686 d_video=demuxer->video;
1687 d_dvdsub=demuxer->sub;
1689 // DUMP STREAMS:
1690 if((stream_dump_type)&&(stream_dump_type<4)){
1691 FILE *f;
1692 demux_stream_t *ds=NULL;
1693 current_module="dump";
1694 // select stream to dump
1695 switch(stream_dump_type){
1696 case 1: ds=d_audio;break;
1697 case 2: ds=d_video;break;
1698 case 3: ds=d_dvdsub;break;
1700 if(!ds){
1701 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
1702 exit_player(MSGTR_Exit_error);
1704 // disable other streams:
1705 if(d_audio && d_audio!=ds) {ds_free_packs(d_audio); d_audio->id=-2; }
1706 if(d_video && d_video!=ds) {ds_free_packs(d_video); d_video->id=-2; }
1707 if(d_dvdsub && d_dvdsub!=ds) {ds_free_packs(d_dvdsub); d_dvdsub->id=-2; }
1708 // let's dump it!
1709 f=fopen(stream_dump_name,"wb");
1710 if(!f){
1711 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
1712 exit_player(MSGTR_Exit_error);
1714 while(!ds->eof){
1715 unsigned char* start;
1716 int in_size=ds_get_packet(ds,&start);
1717 if( (demuxer->file_format==DEMUXER_TYPE_AVI || demuxer->file_format==DEMUXER_TYPE_ASF || demuxer->file_format==DEMUXER_TYPE_MOV)
1718 && stream_dump_type==2) fwrite(&in_size,1,4,f);
1719 if(in_size>0) fwrite(start,in_size,1,f);
1721 fclose(f);
1722 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
1723 exit_player_with_rc(MSGTR_Exit_eof, 0);
1726 sh_audio=d_audio->sh;
1727 sh_video=d_video->sh;
1729 if(sh_video){
1731 current_module="video_read_properties";
1732 if(!video_read_properties(sh_video)) {
1733 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
1734 sh_video=d_video->sh=NULL;
1735 } else {
1736 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
1737 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
1738 sh_video->fps,sh_video->frametime
1741 /* need to set fps here for output encoders to pick it up in their init */
1742 if(force_fps){
1743 sh_video->fps=force_fps;
1744 sh_video->frametime=1.0f/sh_video->fps;
1746 vo_fps = sh_video->fps;
1747 #ifdef HAVE_X11
1748 vo_mouse_timer_const=(int)sh_video->fps;
1749 #endif
1751 if(!sh_video->fps && !force_fps){
1752 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
1753 sh_video=d_video->sh=NULL;
1759 fflush(stdout);
1761 if(!sh_video && !sh_audio){
1762 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
1763 #ifdef HAS_DVBIN_SUPPORT
1764 if((stream->type == STREAMTYPE_DVB) && stream->priv)
1766 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
1767 if(priv->is_on)
1769 int dir;
1770 int v = last_dvb_step;
1771 if(v > 0)
1772 dir = DVB_CHANNEL_HIGHER;
1773 else
1774 dir = DVB_CHANNEL_LOWER;
1776 if(dvb_step_channel(priv, dir))
1778 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
1779 cache_uninit(stream);
1780 goto goto_enable_cache;
1784 #endif
1785 goto goto_next_file; // exit_player(MSGTR_Exit_error);
1788 /* display clip info */
1789 demux_info_print(demuxer);
1791 //================== Read SUBTITLES (DVD & TEXT) ==========================
1792 if(vo_spudec==NULL && sh_video && (stream->type==STREAMTYPE_DVD || demuxer->type==DEMUXER_TYPE_MATROSKA)){
1794 if (spudec_ifo) {
1795 unsigned int palette[16], width, height;
1796 current_module="spudec_init_vobsub";
1797 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1798 vo_spudec=spudec_new_scaled(palette, width, height);
1801 #ifdef USE_DVDNAV
1802 if (vo_spudec==NULL && stream->type==STREAMTYPE_DVDNAV) {
1803 current_module="spudec_init_dvdnav";
1804 vo_spudec=spudec_new_scaled(dvdnav_stream_get_palette((dvdnav_priv_t*)(stream->priv)),
1805 sh_video->disp_w, sh_video->disp_h);
1807 #endif
1809 #ifdef USE_DVDREAD
1810 if (vo_spudec==NULL && stream->type==STREAMTYPE_DVD) {
1811 current_module="spudec_init_dvdread";
1812 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(stream->priv))->cur_pgc->palette,
1813 sh_video->disp_w, sh_video->disp_h);
1815 #endif
1817 #ifdef HAVE_MATROSKA
1818 if ((vo_spudec == NULL) && (demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1819 (d_dvdsub->sh != NULL) && (((mkv_sh_sub_t *)d_dvdsub->sh)->type == 'v')) {
1820 mkv_sh_sub_t *mkv_sh_sub = (mkv_sh_sub_t *)d_dvdsub->sh;
1821 current_module = "spudec_init_matroska";
1822 vo_spudec =
1823 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1824 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1825 mkv_sh_sub->height);
1826 forced_subs_only = mkv_sh_sub->forced_subs_only;
1828 #endif
1830 if (vo_spudec==NULL) {
1831 current_module="spudec_init_normal";
1832 vo_spudec=spudec_new_scaled(NULL, sh_video->disp_w, sh_video->disp_h);
1833 spudec_set_font_factor(vo_spudec,font_factor);
1836 if (vo_spudec!=NULL)
1837 inited_flags|=INITED_SPUDEC;
1841 // Apply current settings for forced subs
1842 if (vo_spudec!=NULL)
1843 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
1845 #ifdef USE_SUB
1846 if(sh_video) {
1847 // after reading video params we should load subtitles because
1848 // we know fps so now we can adjust subtitles time to ~6 seconds AST
1849 // check .sub
1850 current_module="read_subtitles_file";
1851 if(sub_name){
1852 for (i = 0; sub_name[i] != NULL; ++i)
1853 add_subtitles (sub_name[i], sh_video->fps, 0);
1855 if(sub_auto) { // auto load sub file ...
1856 char *psub = get_path( "sub/" );
1857 char **tmp = sub_filenames((psub ? psub : ""), filename);
1858 char **tmp2 = tmp;
1859 free(psub); // release the buffer created by get_path() above
1860 while (*tmp2)
1861 add_subtitles (*tmp2++, sh_video->fps, 0);
1862 free(tmp);
1863 if (set_of_sub_size == 0)
1865 add_subtitles (mem_ptr=get_path("default.sub"), sh_video->fps, 1);
1866 free(mem_ptr); // release the buffer created by get_path()
1868 if (set_of_sub_size > 0)
1869 add_subtitles (NULL, sh_video->fps, 1);
1871 if (set_of_sub_size > 0) {
1872 // setup global sub numbering
1873 global_sub_indices[SUB_SOURCE_SUBS] = global_sub_size; // the global # of the first sub.
1874 global_sub_size += set_of_sub_size;
1877 #endif
1879 if (global_sub_size) {
1880 // find the best sub to use
1881 if (vobsub_id >= 0) {
1882 // if user asks for a vobsub id, use that first.
1883 global_sub_pos = global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_id;
1884 } else if (dvdsub_id >= 0 && global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1885 // if user asks for a dvd sub id, use that next.
1886 global_sub_pos = global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
1887 } else if (global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1888 // if there are text subs to use, use those. (autosubs come last here)
1889 global_sub_pos = global_sub_indices[SUB_SOURCE_SUBS];
1891 } else if (global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1892 // if nothing else works, get subs from the demuxer.
1893 global_sub_pos = global_sub_indices[SUB_SOURCE_DEMUX];
1895 } else {
1896 // nothing worth doing automatically.
1897 global_sub_pos = -1;
1899 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1900 global_sub_pos--;
1901 mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
1904 //================== Init AUDIO (codec) ==========================
1905 if(sh_audio){
1906 // Go through the codec.conf and find the best codec...
1907 current_module="init_audio_codec";
1908 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1909 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
1910 sh_audio=d_audio->sh=NULL; // failed to init :(
1911 } else
1912 inited_flags|=INITED_ACODEC;
1913 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1916 if(identify) {
1917 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_FILENAME=%s\n", filename);
1918 if (sh_video) {
1919 /* Assume FOURCC if all bytes >= 0x20 (' ') */
1920 if (sh_video->format >= 0x20202020)
1921 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", &sh_video->format);
1922 else
1923 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", sh_video->format);
1924 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", sh_video->i_bps*8);
1925 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", sh_video->disp_w);
1926 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", sh_video->disp_h);
1927 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", sh_video->fps);
1928 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", sh_video->aspect);
1930 if (sh_audio) {
1931 if (sh_audio->codec)
1932 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", sh_audio->codec->name);
1933 /* Assume FOURCC if all bytes >= 0x20 (' ') */
1934 if (sh_audio->format >= 0x20202020)
1935 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", &sh_audio->format);
1936 else
1937 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", sh_audio->format);
1938 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", sh_audio->i_bps*8);
1939 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_RATE=%d\n", sh_audio->samplerate);
1940 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_AUDIO_NCH=%d\n", sh_audio->channels);
1942 mp_msg(MSGT_GLOBAL,MSGL_INFO,"ID_LENGTH=%ld\n", demuxer_get_time_length(demuxer));
1945 if(!sh_video) goto main; // audio-only
1947 //================== Init VIDEO (codec & libvo) ==========================
1948 if(!fixed_vo || !(inited_flags&INITED_VO)){
1949 current_module="preinit_libvo";
1951 vo_config_count=0;
1952 //if((video_out->preinit(vo_subdevice))!=0){
1953 if(!(video_out=init_best_video_out(video_driver_list))){
1954 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1955 goto goto_next_file; // exit_player(MSGTR_Exit_error);
1957 sh_video->video_out=video_out;
1958 inited_flags|=INITED_VO;
1961 current_module="init_video_filters";
1963 char* vf_arg[] = { "_oldargs_", (char*)video_out , NULL };
1964 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
1966 #ifdef HAVE_MENU
1967 if(use_menu) {
1968 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
1969 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
1970 if(!vf_menu) {
1971 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
1972 use_menu = 0;
1975 if(vf_menu)
1976 sh_video->vfilter=(void*)append_filters(vf_menu);
1977 else
1978 #endif
1979 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
1981 current_module="init_video_codec";
1983 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1984 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1985 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1987 if(!sh_video->inited){
1988 if(!fixed_vo) uninit_player(INITED_VO);
1989 if(!sh_audio) goto goto_next_file;
1990 sh_video = d_video->sh = NULL;
1991 goto main; // exit_player(MSGTR_Exit_error);
1994 inited_flags|=INITED_VCODEC;
1996 if(auto_quality>0){
1997 // Auto quality option enabled
1998 output_quality=get_video_quality_max(sh_video);
1999 if(auto_quality>output_quality) auto_quality=output_quality;
2000 else output_quality=auto_quality;
2001 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2002 set_video_quality(sh_video,output_quality);
2005 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2007 current_module="init_vo";
2009 if(vo_flags & 0x08 && vo_spudec)
2010 spudec_set_hw_spu(vo_spudec,video_out);
2012 #ifdef HAVE_FREETYPE
2013 force_load_font = 1;
2014 #endif
2016 //================== MAIN: ==========================
2017 main:
2018 current_module="main";
2020 // If there is no video OSD has to be disabled.
2021 // In case of playing a playtree we have to restore the
2022 // old OSD level after playing one or more audio-only files.
2023 if(!sh_video && osd_level >= 0) { // save OSD level only once
2024 osd_level_saved = osd_level;
2025 osd_level = 0;
2026 } else if (osd_level_saved > -1) { // if there is a saved OSD level, restore it
2027 osd_level = osd_level_saved;
2028 osd_level_saved = -1;
2031 fflush(stdout);
2033 #ifdef HAVE_NEW_GUI
2034 if ( use_gui )
2036 if ( sh_audio ) guiIntfStruct.AudioType=sh_audio->channels; else guiIntfStruct.AudioType=0;
2037 if ( !sh_video && sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
2038 guiGetEvent( guiSetFileFormat,(char *)demuxer->file_format );
2039 if ( guiGetEvent( guiSetValues,(char *)sh_video ) ) goto goto_next_file;
2040 guiGetEvent( guiSetDemuxer,(char *)demuxer );
2042 #endif
2045 //int frame_corr_num=0; //
2046 //float v_frame=0; // Video
2047 float time_frame=0; // Timer
2048 //float num_frames=0; // number of frames played
2049 int grab_frames=0;
2050 char osd_text_buffer[64];
2051 char osd_show_text_buffer[64];
2052 int drop_frame=0; // current dropping status
2053 int dropped_frames=0; // how many frames dropped since last non-dropped frame
2054 int too_slow_frame_cnt=0;
2055 int too_fast_frame_cnt=0;
2056 // for auto-quality:
2057 float AV_delay=0; // average of A-V timestamp differences
2058 double vdecode_time;
2059 unsigned int lastframeout_ts=0;
2060 /*float time_frame_corr_avg=0;*/ /* unused */
2062 float next_frame_time=0;
2063 int frame_time_remaining=0; // flag
2064 int blit_frame=0;
2066 osd_text_buffer[0]=0;
2068 //================ SETUP AUDIO ==========================
2070 if(sh_audio){
2071 //const ao_info_t *info=audio_out->info;
2072 current_module="af_preinit";
2073 ao_data.samplerate=force_srate?force_srate:sh_audio->samplerate*playback_speed;
2074 ao_data.channels=audio_output_channels?audio_output_channels:sh_audio->channels;
2075 ao_data.format=audio_output_format?audio_output_format:sh_audio->sample_format;
2076 #if 1
2077 if(!preinit_audio_filters(sh_audio,
2078 // input:
2079 (int)(sh_audio->samplerate*playback_speed),
2080 sh_audio->channels, sh_audio->sample_format, sh_audio->samplesize,
2081 // output:
2082 &ao_data.samplerate, &ao_data.channels, &ao_data.format,
2083 audio_out_format_bits(ao_data.format)/8)){
2084 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
2085 } else {
2086 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AF_pre: %dHz %dch %s\n",
2087 ao_data.samplerate, ao_data.channels,
2088 audio_out_format_name(ao_data.format));
2090 #endif
2091 current_module="ao2_init";
2092 if(!(audio_out=init_best_audio_out(audio_driver_list,
2093 (ao_plugin_cfg.plugin_list!=NULL), // plugin flag
2094 force_srate?force_srate:ao_data.samplerate,
2095 audio_output_channels?audio_output_channels:ao_data.channels,
2096 audio_output_format?audio_output_format:ao_data.format,0))){
2097 // FAILED:
2098 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
2099 uninit_player(INITED_ACODEC); // close codec
2100 sh_audio=d_audio->sh=NULL; // -> nosound
2101 } else {
2102 // SUCCESS:
2103 inited_flags|=INITED_AO;
2104 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bps)\n",
2105 audio_out->info->short_name,
2106 ao_data.samplerate, ao_data.channels,
2107 audio_out_format_name(ao_data.format),
2108 audio_out_format_bits(ao_data.format)/8 );
2109 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
2110 audio_out->info->name, audio_out->info->author);
2111 if(strlen(audio_out->info->comment) > 0)
2112 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", audio_out->info->comment);
2113 // init audio filters:
2114 #if 1
2115 current_module="af_init";
2116 if(!build_afilter_chain(sh_audio, &ao_data)) {
2117 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
2118 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
2119 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
2120 // sh_audio=d_audio->sh=NULL; // -> nosound
2122 #endif
2124 mixer.audio_out = audio_out;
2125 mixer.afilter = sh_audio ? sh_audio->afilter : NULL;
2126 mixer.volstep = volstep;
2129 current_module="av_init";
2131 if(sh_video) sh_video->timer=0;
2132 if(sh_audio) sh_audio->delay=-audio_delay;
2134 if(!sh_audio){
2135 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
2136 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",d_audio->packs);
2137 ds_free_packs(d_audio); // free buffered chunks
2138 d_audio->id=-2; // do not read audio chunks
2139 //uninit_player(INITED_AO); // close device
2141 if(!sh_video){
2142 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
2143 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",d_video->packs);
2144 ds_free_packs(d_video);
2145 d_video->id=-2;
2146 //if(!fixed_vo) uninit_player(INITED_VO);
2149 if (!sh_video && !sh_audio)
2150 goto goto_next_file;
2152 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
2153 if(force_fps && sh_video){
2154 vo_fps = sh_video->fps=force_fps;
2155 sh_video->frametime=1.0f/sh_video->fps;
2156 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,sh_video->fps,sh_video->frametime);
2159 //==================== START PLAYING =======================
2161 if(loop_times>1) loop_times--; else
2162 if(loop_times==1) loop_times = -1;
2164 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);fflush(stdout);
2166 InitTimer();
2168 #ifdef USE_DVDNAV
2169 if (stream->type==STREAMTYPE_DVDNAV) {
2170 dvdnav_stream_fullstart((dvdnav_priv_t *)stream->priv);
2172 #endif
2174 total_time_usage_start=GetTimer();
2175 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
2176 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
2177 play_n_frames=play_n_frames_mf;
2179 if(play_n_frames==0){
2180 eof=PT_NEXT_ENTRY; goto goto_next_file;
2183 while(!eof){
2184 float aq_sleep_time=0;
2186 if(play_n_frames>=0){
2187 --play_n_frames;
2188 if(play_n_frames<0) eof = PT_NEXT_ENTRY;
2191 /*========================== PLAY AUDIO ============================*/
2193 while(sh_audio){
2194 unsigned int t;
2195 double tt;
2196 int playsize;
2198 current_module="play_audio";
2200 ao_data.pts=((sh_video?sh_video->timer:0)+sh_audio->delay)*90000.0;
2201 playsize=audio_out->get_space();
2203 // handle audio-only case:
2204 if(playsize < ao_data.outburst &&
2205 !sh_video) { // buffer is full, do not block here!!!
2206 usec_sleep(10000); // Wait a tick before retry
2207 continue;
2210 if(playsize>MAX_OUTBURST) playsize=MAX_OUTBURST; // we shouldn't exceed it!
2212 // Fill buffer if needed:
2213 current_module="decode_audio"; // Enter AUDIO decoder module
2214 t=GetTimer();
2215 while(sh_audio->a_out_buffer_len<playsize && !d_audio->eof){
2216 int ret=decode_audio(sh_audio,&sh_audio->a_out_buffer[sh_audio->a_out_buffer_len],
2217 playsize-sh_audio->a_out_buffer_len,sh_audio->a_out_buffer_size-sh_audio->a_out_buffer_len);
2218 if(ret<=0) break; // EOF?
2219 sh_audio->a_out_buffer_len+=ret;
2221 t=GetTimer()-t;
2222 tt = t*0.000001f; audio_time_usage+=tt;
2223 if(playsize>sh_audio->a_out_buffer_len) playsize=sh_audio->a_out_buffer_len;
2225 // play audio:
2226 current_module="play_audio";
2227 playsize=audio_out->play(sh_audio->a_out_buffer,playsize,0);
2229 if(playsize>0){
2230 sh_audio->a_out_buffer_len-=playsize;
2231 memmove(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[playsize],sh_audio->a_out_buffer_len);
2232 sh_audio->delay+=playback_speed*playsize/((float)((ao_data.bps && sh_audio->afilter) ?
2233 ao_data.bps : sh_audio->o_bps));
2236 break;
2237 } // while(sh_audio)
2239 if(!sh_video) {
2240 // handle audio-only case:
2241 if(!quiet) {
2242 float a_pos = sh_audio->delay - audio_out->get_delay() * playback_speed;
2243 print_status(a_pos, 0, 0);
2245 if(d_audio->eof) eof = PT_NEXT_ENTRY;
2247 } else {
2249 /*========================== PLAY VIDEO ============================*/
2251 float frame_time=next_frame_time;
2253 vo_pts=sh_video->timer*90000.0;
2254 vo_fps=sh_video->fps;
2256 if(!frame_time_remaining){
2257 //-------------------- Decode a frame: -----------------------
2258 vdecode_time=video_time_usage;
2259 while(1)
2260 { unsigned char* start=NULL;
2261 int in_size;
2262 // get it!
2263 current_module="video_read_frame";
2264 in_size=video_read_frame(sh_video,&next_frame_time,&start,force_fps);
2265 if(in_size<0){ eof=1; break; }
2266 if(in_size>max_framesize) max_framesize=in_size; // stats
2267 sh_video->timer+=frame_time;
2268 if(sh_audio) sh_audio->delay-=frame_time;
2269 time_frame+=frame_time; // for nosound
2270 // check for frame-drop:
2271 current_module="check_framedrop";
2272 if(sh_audio && !d_audio->eof){
2273 float delay=playback_speed*audio_out->get_delay();
2274 float d=delay-sh_audio->delay;
2275 // we should avoid dropping to many frames in sequence unless we
2276 // are too late. and we allow 100ms A-V delay here:
2277 if(d<-dropped_frames*frame_time-0.100 && osd_function != OSD_PAUSE){
2278 drop_frame=frame_dropping;
2279 ++drop_frame_cnt;
2280 ++dropped_frames;
2281 } else {
2282 drop_frame=dropped_frames=0;
2284 ++total_frame_cnt;
2286 // decode:
2287 current_module="decode_video";
2288 // printf("Decode! %p %d \n",start,in_size);
2289 blit_frame=decode_video(sh_video,start,in_size,drop_frame);
2290 break;
2292 vdecode_time=video_time_usage-vdecode_time;
2293 //------------------------ frame decoded. --------------------
2295 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
2297 if(sh_video->vf_inited<0){
2298 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_NotInitializeVOPorVO);
2299 eof=1; goto goto_next_file;
2304 // ==========================================================================
2306 // current_module="draw_osd";
2307 // if(vo_config_count) video_out->draw_osd();
2309 #ifdef HAVE_NEW_GUI
2310 if(use_gui) guiEventHandling();
2311 #endif
2313 current_module="calc_sleep_time";
2315 #if 0
2316 { // debug frame dropping code
2317 float delay=audio_out->get_delay();
2318 mp_msg(MSGT_AVSYNC,MSGL_V,"\r[V] %5.3f [A] %5.3f => {%5.3f} (%5.3f) [%d] \n",
2319 sh_video->timer,sh_audio->timer-delay,
2320 sh_video->timer-(sh_audio->timer-delay),
2321 delay,drop_frame);
2323 #endif
2325 if(drop_frame && !frame_time_remaining && !autosync){
2327 * Note: time_frame should not be forced to 0 in autosync mode.
2328 * It is used as a cumulative counter to predict and correct the
2329 * delay measurements from the audio driver. time_frame is already
2330 * < 0, so the "time to sleep" code does not actually sleep. Also,
2331 * blit_frame is already 0 because drop_frame was true when
2332 * decode_video was called (which causes it to set blit_frame to 0.)
2333 * When autosync==0, the default behavior is still completely unchanged.
2336 time_frame=0; // don't sleep!
2337 blit_frame=0; // don't display!
2339 } else {
2341 // It's time to sleep...
2343 frame_time_remaining=0;
2344 time_frame-=GetRelativeTime(); // reset timer
2346 if(sh_audio && !d_audio->eof){
2347 float delay=playback_speed*audio_out->get_delay();
2348 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"delay=%f\n",delay);
2350 if (autosync){
2352 * Adjust this raw delay value by calculating the expected
2353 * delay for this frame and generating a new value which is
2354 * weighted between the two. The higher autosync is, the
2355 * closer to the delay value gets to that which "-nosound"
2356 * would have used, and the longer it will take for A/V
2357 * sync to settle at the right value (but it eventually will.)
2358 * This settling time is very short for values below 100.
2360 float predicted = sh_audio->delay+time_frame;
2361 float difference = delay - predicted;
2362 delay = predicted + difference / (float)autosync;
2365 time_frame=delay-sh_audio->delay;
2367 // delay = amount of audio buffered in soundcard/driver
2368 if(delay>0.25) delay=0.25; else
2369 if(delay<0.10) delay=0.10;
2370 if(time_frame>delay*0.6){
2371 // sleep time too big - may cause audio drops (buffer underrun)
2372 frame_time_remaining=1;
2373 time_frame=delay*0.5;
2376 } else {
2378 // NOSOUND:
2379 if( (time_frame<-3*frame_time || time_frame>3*frame_time) || benchmark)
2380 time_frame=0;
2384 // if(verbose>1)printf("sleep: %5.3f a:%6.3f v:%6.3f \n",time_frame,sh_audio->timer,sh_video->timer);
2386 aq_sleep_time+=time_frame;
2388 } // !drop_frame
2390 //============================== SLEEP: ===================================
2392 time_frame/=playback_speed;
2394 // flag 256 means: libvo driver does its timing (dvb card)
2395 if(time_frame>0.001 && !(vo_flags&256)){
2397 #ifdef HAVE_RTC
2398 if(rtc_fd>=0){
2399 // -------- RTC -----------
2400 current_module="sleep_rtc";
2401 while (time_frame > 0.000) {
2402 unsigned long rtc_ts;
2403 if (read (rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
2404 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
2405 time_frame-=GetRelativeTime();
2407 } else
2408 #endif
2410 // -------- TIMER + SOFTSLEEP -----------
2411 float min=softsleep?0.021:0.005;
2412 current_module="sleep_timer";
2413 while(time_frame>min){
2414 if(time_frame<=0.020)
2415 usec_sleep(0); // sleeps 1 clock tick (10ms)!
2416 else
2417 usec_sleep(1000000*(time_frame-0.020));
2418 time_frame-=GetRelativeTime();
2420 if(softsleep){
2421 current_module="sleep_soft";
2422 if(time_frame<0) mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
2423 while(time_frame>0) time_frame-=GetRelativeTime(); // burn the CPU
2429 //if(!frame_time_remaining){ // should we display the frame now?
2431 //====================== FLIP PAGE (VIDEO BLT): =========================
2433 current_module="vo_check_events";
2434 if(vo_config_count) video_out->check_events();
2436 current_module="flip_page";
2437 if (!frame_time_remaining) {
2438 if(blit_frame){
2439 unsigned int t2=GetTimer();
2440 double tt;
2441 float j;
2442 #define FRAME_LAG_WARN 0.2
2443 j = ((float)t2 - lastframeout_ts) / 1000000;
2444 lastframeout_ts = GetTimer();
2445 if (j < frame_time + frame_time * -FRAME_LAG_WARN)
2446 too_fast_frame_cnt++;
2447 /* printf ("PANIC: too fast frame (%.3f)!\n", j); */
2448 else if (j > frame_time + frame_time * FRAME_LAG_WARN)
2449 too_slow_frame_cnt++;
2450 /* printf ("PANIC: too slow frame (%.3f)!\n", j); */
2452 if(vo_config_count) video_out->flip_page();
2453 // usec_sleep(50000); // test only!
2454 t2=GetTimer()-t2;
2455 tt = t2*0.000001f;
2456 vout_time_usage+=tt;
2457 } else {
2459 Well, no blitting is needed, but some devices (such as yuv4mpeg) must output frame
2460 otherwise A/V desync will occur. -- Alvieboy
2462 if (vo_config_count)
2463 video_out->control(VOCTRL_DUPLICATE_FRAME, NULL);
2466 //====================== A-V TIMESTAMP CORRECTION: =========================
2468 current_module="av_sync";
2470 if(sh_audio){
2471 float a_pts=0;
2472 float v_pts=0;
2474 // unplayed bytes in our and soundcard/dma buffer:
2475 float delay=playback_speed*audio_out->get_delay()+(float)sh_audio->a_buffer_len/(float)sh_audio->o_bps;
2477 if (autosync){
2479 * If autosync is enabled, the value for delay must be calculated
2480 * a bit differently. It is set only to the difference between
2481 * the audio and video timers. Any attempt to include the real
2482 * or corrected delay causes the pts_correction code below to
2483 * try to correct for the changes in delay which autosync is
2484 * trying to measure. This keeps the two from competing, but still
2485 * allows the code to correct for PTS drift *only*. (Using a delay
2486 * value here, even a "corrected" one, would be incompatible with
2487 * autosync mode.)
2489 delay=sh_audio->delay;
2490 delay+=(float)sh_audio->a_buffer_len/(float)sh_audio->o_bps;
2493 #if 0
2494 if(pts_from_bps){
2495 // PTS = sample_no / samplerate
2496 unsigned int samples=
2497 // (sh_audio->audio.dwSampleSize)?
2498 // ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
2499 ds_tell_block(d_audio); // <- used for VBR audio
2500 samples+=sh_audio->audio.dwStart; // offset
2501 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
2502 delay_corrected=1;
2503 a_pts-=(sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
2504 } else
2505 #endif
2507 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
2508 a_pts=d_audio->pts;
2509 if(!delay_corrected) if(a_pts) delay_corrected=1;
2510 #if 0
2511 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n#X# pts=%5.3f ds_pts=%5.3f buff=%5.3f total=%5.3f\n",
2512 a_pts,
2513 ds_tell_pts(d_audio)/(float)sh_audio->i_bps,
2514 -sh_audio->a_in_buffer_len/(float)sh_audio->i_bps,
2515 a_pts+(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps);
2516 #endif
2517 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
2519 v_pts=sh_video ? sh_video->pts : d_video->pts;
2521 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);
2523 if(delay_corrected){
2524 static int drop_message=0;
2525 float x;
2526 AV_delay=(a_pts-delay-audio_delay)-v_pts;
2527 if(AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
2528 ++drop_message;
2529 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
2531 x=AV_delay*0.1f;
2532 if(x<-max_pts_correction) x=-max_pts_correction; else
2533 if(x> max_pts_correction) x= max_pts_correction;
2534 if(default_max_pts_correction>=0)
2535 max_pts_correction=default_max_pts_correction;
2536 else
2537 max_pts_correction=sh_video->frametime*0.10; // +-10% of time
2538 if(!frame_time_remaining){ sh_audio->delay+=x; c_total+=x;} // correction
2539 if(!quiet)
2540 print_status(a_pts - audio_delay - delay, AV_delay, c_total);
2543 } else {
2544 // No audio:
2546 if(!quiet)
2547 print_status(0, 0, 0);
2551 //============================ Auto QUALITY ============================
2553 /*Output quality adjustments:*/
2554 if(auto_quality>0){
2555 current_module="autoq";
2556 // float total=0.000001f * (GetTimer()-aq_total_time);
2557 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
2558 if(output_quality<auto_quality && aq_sleep_time>0)
2559 ++output_quality;
2560 else
2561 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
2562 if(output_quality>1 && aq_sleep_time<0)
2563 --output_quality;
2564 else
2565 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
2566 output_quality=0;
2567 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
2568 set_video_quality(sh_video,output_quality);
2571 } // end if(sh_video)
2573 //============================ Handle PAUSE ===============================
2575 current_module="pause";
2577 #ifdef USE_OSD
2578 if(osd_visible){
2579 if (!--osd_visible){
2580 vo_osd_progbar_type=-1; // disable
2581 vo_osd_changed(OSDTYPE_PROGBAR);
2582 if (osd_function != OSD_PAUSE)
2583 osd_function = OSD_PLAY;
2586 #endif
2588 if(osd_function==OSD_PAUSE){
2589 mp_cmd_t* cmd;
2590 if(!quiet) {
2591 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2592 fflush(stdout);
2594 #ifdef HAVE_NEW_GUI
2595 if(use_gui) guiGetEvent( guiCEvent,(char *)guiSetPause );
2596 #endif
2597 if (video_out && sh_video && vo_config_count)
2598 video_out->control(VOCTRL_PAUSE, NULL);
2600 if (audio_out && sh_audio)
2601 audio_out->pause(); // pause audio, keep data if possible
2603 while( (cmd = mp_input_get_cmd(20,1,1)) == NULL) {
2604 if(sh_video && video_out && vo_config_count) video_out->check_events();
2605 #ifdef HAVE_NEW_GUI
2606 if(use_gui){
2607 guiEventHandling();
2608 guiGetEvent( guiReDraw,NULL );
2609 if(guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos)) break;
2611 #endif
2612 #ifdef HAVE_MENU
2613 if(vf_menu)
2614 vf_menu_pause_update(vf_menu);
2615 #endif
2616 usec_sleep(20000);
2618 if (cmd && cmd->id == MP_CMD_PAUSE) {
2619 cmd = mp_input_get_cmd(0,1,0);
2620 mp_cmd_free(cmd);
2622 osd_function=OSD_PLAY;
2623 if (audio_out && sh_audio)
2624 audio_out->resume(); // resume audio
2625 if (video_out && sh_video && vo_config_count)
2626 video_out->control(VOCTRL_RESUME, NULL); // resume video
2627 (void)GetRelativeTime(); // keep TF around FT in next cycle
2628 #ifdef HAVE_NEW_GUI
2629 if (use_gui)
2631 if ( guiIntfStruct.Playing == guiSetStop ) goto goto_next_file;
2632 guiGetEvent( guiCEvent,(char *)guiSetPlay );
2634 #endif
2637 // handle -sstep
2638 if(step_sec>0) {
2639 osd_function=OSD_FFW;
2640 rel_seek_secs+=step_sec;
2643 #ifdef USE_DVDNAV
2644 if (stream->type==STREAMTYPE_DVDNAV && dvd_nav_still)
2645 dvdnav_stream_sleeping((dvdnav_priv_t*)stream->priv);
2646 #endif
2648 //================= EDL =========================================
2650 #ifdef USE_EDL
2651 if( next_edl_record ) { // Are we (still?) doing EDL?
2652 if ( !sh_video ) {
2653 mp_msg( MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video );
2654 free(edl_records);
2655 next_edl_record = NULL;
2656 edl_records = NULL;
2657 } else {
2658 if( sh_video->pts >= next_edl_record->start_sec ) {
2659 if( next_edl_record->action == EDL_SKIP ) {
2660 osd_function = OSD_FFW;
2661 abs_seek_pos = 0;
2662 rel_seek_secs = next_edl_record->length_sec;
2663 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 );
2664 edl_decision = 1;
2665 } else if( next_edl_record->action == EDL_MUTE ) {
2666 edl_muted = !edl_muted;
2667 if ((user_muted | edl_muted) != mixer.muted) mixer_mute(&mixer);
2668 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
2670 next_edl_record=next_edl_record->next;
2674 #endif
2676 //================= Keyboard events, SEEKing ====================
2678 current_module="key_events";
2681 mp_cmd_t* cmd;
2682 int brk_cmd = 0;
2683 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2684 switch(cmd->id) {
2685 case MP_CMD_SEEK : {
2686 int v,abs;
2687 osd_show_percentage = 25;
2688 v = cmd->args[0].v.i;
2689 abs = (cmd->nargs > 1) ? cmd->args[1].v.i : 0;
2690 if(abs==2) { /* Absolute seek to a specific timestamp in seconds */
2691 abs_seek_pos = 1;
2692 if(sh_video)
2693 osd_function= (v > sh_video->timer) ? OSD_FFW : OSD_REW;
2694 rel_seek_secs = v;
2696 else if(abs) { /* Absolute seek by percentage */
2697 abs_seek_pos = 3;
2698 if(sh_video)
2699 osd_function= (v > sh_video->timer) ? OSD_FFW : OSD_REW;
2700 rel_seek_secs = v/100.0;
2702 else {
2703 rel_seek_secs+= v;
2704 osd_function= (v > 0) ? OSD_FFW : OSD_REW;
2706 brk_cmd = 1;
2707 } break;
2708 #ifdef USE_EDL
2709 case MP_CMD_EDL_MARK:
2710 if( edl_fd ) {
2711 float v = sh_video->pts;
2712 fprintf( edl_fd, "%f %f %d\n", v-2, v, 0 );
2714 break;
2715 #endif
2716 case MP_CMD_SWITCH_RATIO : {
2717 if (cmd->nargs == 0)
2718 movie_aspect = (float) sh_video->disp_w / sh_video->disp_h;
2719 else
2720 movie_aspect = cmd->args[0].v.f;
2721 mpcodecs_config_vo (sh_video, sh_video->disp_w, sh_video->disp_h, 0);
2722 } break;
2723 case MP_CMD_AUDIO_DELAY : {
2724 float v = cmd->args[0].v.f;
2725 audio_delay += v;
2726 osd_show_av_delay = 9;
2727 if(sh_audio) sh_audio->delay+= v;
2728 } break;
2729 case MP_CMD_SPEED_INCR : {
2730 float v = cmd->args[0].v.f;
2731 playback_speed += v;
2732 if (sh_video)
2733 osd_show_speed = sh_video->fps;
2734 build_afilter_chain(sh_audio, &ao_data);
2735 } break;
2736 case MP_CMD_SPEED_MULT : {
2737 float v = cmd->args[0].v.f;
2738 playback_speed *= v;
2739 if (sh_video)
2740 osd_show_speed = sh_video->fps;
2741 build_afilter_chain(sh_audio, &ao_data);
2742 } break;
2743 case MP_CMD_SPEED_SET : {
2744 float v = cmd->args[0].v.f;
2745 playback_speed = v;
2746 if (sh_video)
2747 osd_show_speed = sh_video->fps;
2748 build_afilter_chain(sh_audio, &ao_data);
2749 } break;
2750 case MP_CMD_FRAME_STEP :
2751 case MP_CMD_PAUSE : {
2752 cmd->pausing = 1;
2753 brk_cmd = 1;
2754 } break;
2755 case MP_CMD_QUIT : {
2756 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2758 case MP_CMD_GRAB_FRAMES : {
2759 grab_frames=2;
2760 } break;
2761 case MP_CMD_PLAY_TREE_STEP : {
2762 int n = cmd->args[0].v.i == 0 ? 1 : cmd->args[0].v.i;
2763 int force = cmd->args[1].v.i;
2765 #ifdef HAVE_NEW_GUI
2766 if (use_gui) {
2767 int i=0;
2768 if (n>0)
2769 for (i=0;i<n;i++)
2770 mplNext();
2771 else
2772 for (i=0;i<-1*n;i++)
2773 mplPrev();
2774 } else
2775 #endif
2777 if(!force && playtree_iter) {
2778 play_tree_iter_t* i = play_tree_iter_new_copy(playtree_iter);
2780 if(play_tree_iter_step(i,n,0) == PLAY_TREE_ITER_ENTRY)
2781 eof = (n > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
2782 play_tree_iter_free(i);
2783 } else
2784 eof = (n > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
2785 if(eof)
2786 play_tree_step = n;
2787 brk_cmd = 1;
2789 } break;
2790 case MP_CMD_PLAY_TREE_UP_STEP : {
2791 int n = cmd->args[0].v.i > 0 ? 1 : -1;
2792 int force = cmd->args[1].v.i;
2794 if(!force && playtree_iter) {
2795 play_tree_iter_t* i = play_tree_iter_new_copy(playtree_iter);
2796 if(play_tree_iter_up_step(i,n,0) == PLAY_TREE_ITER_ENTRY)
2797 eof = (n > 0) ? PT_UP_NEXT : PT_UP_PREV;
2798 play_tree_iter_free(i);
2799 } else
2800 eof = (n > 0) ? PT_UP_NEXT : PT_UP_PREV;
2801 brk_cmd = 1;
2802 } break;
2803 case MP_CMD_PLAY_ALT_SRC_STEP : {
2804 if(playtree_iter && playtree_iter->num_files > 1) {
2805 int v = cmd->args[0].v.i;
2806 if(v > 0 && playtree_iter->file < playtree_iter->num_files)
2807 eof = PT_NEXT_SRC;
2808 else if(v < 0 && playtree_iter->file > 1)
2809 eof = PT_PREV_SRC;
2811 brk_cmd = 1;
2812 } break;
2813 case MP_CMD_SUB_DELAY : {
2814 #ifdef USE_SUB
2815 if (sh_video) {
2816 int abs= cmd->args[1].v.i;
2817 float v = cmd->args[0].v.f;
2818 if(abs)
2819 sub_delay = v;
2820 else
2821 sub_delay += v;
2822 osd_show_sub_delay = 9; // show the subdelay in OSD
2824 #endif
2825 } break;
2826 case MP_CMD_SUB_STEP : {
2827 #ifdef USE_SUB
2828 if (sh_video) {
2829 int movement = cmd->args[0].v.i;
2830 step_sub(subdata, sh_video->pts, movement);
2831 osd_show_sub_delay = 9; // show the subdelay in OSD
2833 #endif
2834 } break;
2835 case MP_CMD_OSD : {
2836 #ifdef USE_OSD
2837 if(sh_video) {
2838 int v = cmd->args[0].v.i;
2839 if(v < 0)
2840 osd_level=(osd_level+1)%(MAX_OSD_LEVEL+1);
2841 else
2842 osd_level= v > MAX_OSD_LEVEL ? MAX_OSD_LEVEL : v;
2844 #endif
2845 } break;
2846 case MP_CMD_OSD_SHOW_TEXT : {
2847 #ifdef USE_OSD
2848 if(osd_level && sh_video){
2849 osd_show_text=sh_video->fps; // 1 sec
2850 strncpy(osd_show_text_buffer, cmd->args[0].v.s, 64);
2852 #endif
2853 } break;
2854 case MP_CMD_VOLUME : {
2855 int v = cmd->args[0].v.i;
2857 // start change for absolute volume value
2858 int abs = (cmd->nargs > 1) ? cmd->args[1].v.i : 0;
2860 #ifdef USE_EDL
2861 if (edl_muted) break;
2862 user_muted = 0;
2863 #endif
2864 if( abs )
2866 mixer_setvolume(&mixer, (float)v, (float)v );
2867 } else {
2868 if(v > 0)
2869 mixer_incvolume(&mixer);
2870 else
2871 mixer_decvolume(&mixer);
2874 #ifdef USE_OSD
2875 if(osd_level && sh_video){
2876 float vol;
2877 osd_visible=sh_video->fps; // 1 sec
2878 vo_osd_progbar_type=OSD_VOLUME;
2879 mixer_getbothvolume(&mixer, &vol);
2880 vo_osd_progbar_value=(vol*256.0)/100.0;
2881 vo_osd_changed(OSDTYPE_PROGBAR);
2883 #endif
2884 } break;
2885 case MP_CMD_MUTE:
2886 #ifdef USE_EDL
2887 user_muted = !user_muted;
2888 if ((edl_muted | user_muted) != mixer.muted)
2889 #endif
2890 mixer_mute(&mixer);
2891 break;
2892 case MP_CMD_LOADFILE : {
2893 play_tree_t* e = play_tree_new();
2894 play_tree_add_file(e,cmd->args[0].v.s);
2896 // Go back to the start point
2897 while(play_tree_iter_up_step(playtree_iter,0,1) != PLAY_TREE_ITER_END)
2898 /* NOP */;
2899 play_tree_free_list(playtree->child,1);
2900 play_tree_set_child(playtree,e);
2901 play_tree_iter_step(playtree_iter,0,0);
2902 eof = PT_NEXT_SRC;
2903 brk_cmd = 1;
2904 } break;
2905 case MP_CMD_LOADLIST : {
2906 play_tree_t* e = parse_playlist_file(cmd->args[0].v.s);
2907 if(!e)
2908 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_PlaylistLoadUnable,cmd->args[0].v.s);
2909 else {
2910 // Go back to the start point
2911 while(play_tree_iter_up_step(playtree_iter,0,1) != PLAY_TREE_ITER_END)
2912 /* NOP */;
2913 play_tree_free_list(playtree->child,1);
2914 play_tree_set_child(playtree,e);
2915 play_tree_iter_step(playtree_iter,0,0);
2916 eof = PT_NEXT_SRC;
2918 brk_cmd = 1;
2919 } break;
2920 case MP_CMD_GAMMA : {
2921 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
2923 if (!sh_video)
2924 break;
2926 if (vo_gamma_gamma == 1000)
2928 vo_gamma_gamma = 0;
2929 get_video_colors (sh_video, "gamma", &vo_gamma_gamma);
2932 if (abs)
2933 vo_gamma_gamma = v;
2934 else
2935 vo_gamma_gamma += v;
2937 if (vo_gamma_gamma > 100)
2938 vo_gamma_gamma = 100;
2939 else if (vo_gamma_gamma < -100)
2940 vo_gamma_gamma = -100;
2941 set_video_colors(sh_video, "gamma", vo_gamma_gamma);
2942 #ifdef USE_OSD
2943 if(osd_level){
2944 osd_visible=sh_video->fps; // 1 sec
2945 vo_osd_progbar_type=OSD_BRIGHTNESS;
2946 vo_osd_progbar_value=(vo_gamma_gamma<<7)/100 + 128;
2947 vo_osd_changed(OSDTYPE_PROGBAR);
2949 #endif // USE_OSD
2950 } break;
2951 case MP_CMD_BRIGHTNESS : {
2952 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
2954 if (!sh_video)
2955 break;
2957 if (vo_gamma_brightness == 1000)
2959 vo_gamma_brightness = 0;
2960 get_video_colors(sh_video, "brightness", &vo_gamma_brightness);
2963 if (abs)
2964 vo_gamma_brightness = v;
2965 else
2966 vo_gamma_brightness += v;
2968 if (vo_gamma_brightness > 100)
2969 vo_gamma_brightness = 100;
2970 else if (vo_gamma_brightness < -100)
2971 vo_gamma_brightness = -100;
2972 if(set_video_colors(sh_video, "brightness", vo_gamma_brightness)){
2973 #ifdef USE_OSD
2974 if(osd_level){
2975 osd_visible=sh_video->fps; // 1 sec
2976 vo_osd_progbar_type=OSD_BRIGHTNESS;
2977 vo_osd_progbar_value=(vo_gamma_brightness<<7)/100 + 128;
2978 vo_osd_changed(OSDTYPE_PROGBAR);
2980 #endif // USE_OSD
2982 } break;
2983 case MP_CMD_CONTRAST : {
2984 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
2986 if (!sh_video)
2987 break;
2989 if (vo_gamma_contrast == 1000)
2991 vo_gamma_contrast = 0;
2992 get_video_colors(sh_video, "contrast", &vo_gamma_contrast);
2995 if (abs)
2996 vo_gamma_contrast = v;
2997 else
2998 vo_gamma_contrast += v;
3000 if (vo_gamma_contrast > 100)
3001 vo_gamma_contrast = 100;
3002 else if (vo_gamma_contrast < -100)
3003 vo_gamma_contrast = -100;
3004 if(set_video_colors(sh_video, "contrast", vo_gamma_contrast)){
3005 #ifdef USE_OSD
3006 if(osd_level){
3007 osd_visible=sh_video->fps; // 1 sec
3008 vo_osd_progbar_type=OSD_CONTRAST;
3009 vo_osd_progbar_value=(vo_gamma_contrast<<7)/100 + 128;
3010 vo_osd_changed(OSDTYPE_PROGBAR);
3012 #endif // USE_OSD
3014 } break;
3015 case MP_CMD_SATURATION : {
3016 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
3018 if (!sh_video)
3019 break;
3021 if (vo_gamma_saturation == 1000)
3023 vo_gamma_saturation = 0;
3024 get_video_colors(sh_video, "saturation", &vo_gamma_saturation);
3027 if (abs)
3028 vo_gamma_saturation = v;
3029 else
3030 vo_gamma_saturation += v;
3032 if (vo_gamma_saturation > 100)
3033 vo_gamma_saturation = 100;
3034 else if (vo_gamma_saturation < -100)
3035 vo_gamma_saturation = -100;
3036 if(set_video_colors(sh_video, "saturation", vo_gamma_saturation)){
3037 #ifdef USE_OSD
3038 if(osd_level){
3039 osd_visible=sh_video->fps; // 1 sec
3040 vo_osd_progbar_type=OSD_SATURATION;
3041 vo_osd_progbar_value=(vo_gamma_saturation<<7)/100 + 128;
3042 vo_osd_changed(OSDTYPE_PROGBAR);
3044 #endif // USE_OSD
3046 } break;
3047 case MP_CMD_HUE : {
3048 int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
3050 if (!sh_video)
3051 break;
3053 if (vo_gamma_hue == 1000)
3055 vo_gamma_hue = 0;
3056 get_video_colors(sh_video, "hue", &vo_gamma_hue);
3059 if (abs)
3060 vo_gamma_hue = v;
3061 else
3062 vo_gamma_hue += v;
3064 if (vo_gamma_hue > 100)
3065 vo_gamma_hue = 100;
3066 else if (vo_gamma_hue < -100)
3067 vo_gamma_hue = -100;
3068 if(set_video_colors(sh_video, "hue", vo_gamma_hue)){
3069 #ifdef USE_OSD
3070 if(osd_level){
3071 osd_visible=sh_video->fps; // 1 sec
3072 vo_osd_progbar_type=OSD_HUE;
3073 vo_osd_progbar_value=(vo_gamma_hue<<7)/100 + 128;
3074 vo_osd_changed(OSDTYPE_PROGBAR);
3076 #endif // USE_OSD
3078 } break;
3079 case MP_CMD_FRAMEDROPPING : {
3080 int v = cmd->args[0].v.i;
3081 if(v < 0){
3082 frame_dropping = (frame_dropping+1)%3;
3083 #ifdef USE_OSD
3084 osd_show_framedropping=10;
3085 vo_osd_changed(OSDTYPE_SUBTITLE);
3086 #endif
3088 else
3089 frame_dropping = v > 2 ? 2 : v;
3090 } break;
3091 #ifdef USE_TV
3092 case MP_CMD_TV_SET_FREQ : {
3093 if (file_format == DEMUXER_TYPE_TV)
3094 tv_set_freq((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.f * 16.0);
3095 } break;
3096 case MP_CMD_TV_SET_NORM : {
3097 if (file_format == DEMUXER_TYPE_TV)
3098 tv_set_norm((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.s);
3099 } break;
3100 case MP_CMD_TV_SET_BRIGHTNESS : {
3101 if (file_format == DEMUXER_TYPE_TV)
3102 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_BRIGHTNESS, cmd->args[0].v.i);
3103 } break;
3104 case MP_CMD_TV_SET_HUE : {
3105 if (file_format == DEMUXER_TYPE_TV)
3106 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_HUE, cmd->args[0].v.i);
3107 } break;
3108 case MP_CMD_TV_SET_SATURATION : {
3109 if (file_format == DEMUXER_TYPE_TV)
3110 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_SATURATION, cmd->args[0].v.i);
3111 } break;
3112 case MP_CMD_TV_SET_CONTRAST : {
3113 if (file_format == DEMUXER_TYPE_TV)
3114 tv_set_color_options((tvi_handle_t*)(demuxer->priv), TV_COLOR_CONTRAST, cmd->args[0].v.i);
3115 } break;
3116 case MP_CMD_TV_STEP_CHANNEL : {
3117 if (file_format == DEMUXER_TYPE_TV) {
3118 int v = cmd->args[0].v.i;
3119 if(v > 0){
3120 tv_step_channel((tvi_handle_t*)(demuxer->priv), TV_CHANNEL_HIGHER);
3121 #ifdef USE_OSD
3122 if (tv_channel_list) {
3123 osd_show_tv_channel = sh_video->fps;
3124 vo_osd_changed(OSDTYPE_SUBTITLE);
3126 #endif
3127 } else {
3128 tv_step_channel((tvi_handle_t*)(demuxer->priv), TV_CHANNEL_LOWER);
3129 #ifdef USE_OSD
3130 if (tv_channel_list) {
3131 osd_show_tv_channel = sh_video->fps;
3132 vo_osd_changed(OSDTYPE_SUBTITLE);
3134 #endif
3138 #ifdef HAS_DVBIN_SUPPORT
3139 if((stream->type == STREAMTYPE_DVB) && stream->priv)
3141 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
3142 if(priv->is_on)
3144 int dir;
3145 int v = cmd->args[0].v.i;
3147 last_dvb_step = v;
3148 if(v > 0)
3149 dir = DVB_CHANNEL_HIGHER;
3150 else
3151 dir = DVB_CHANNEL_LOWER;
3154 if(dvb_step_channel(priv, dir))
3156 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
3157 cache_uninit(stream);
3158 goto goto_enable_cache;
3162 #endif
3163 break;
3164 case MP_CMD_TV_SET_CHANNEL : {
3165 if (file_format == DEMUXER_TYPE_TV) {
3166 tv_set_channel((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.s);
3167 #ifdef USE_OSD
3168 if (tv_channel_list) {
3169 osd_show_tv_channel = sh_video->fps;
3170 vo_osd_changed(OSDTYPE_SUBTITLE);
3172 #endif
3174 } break;
3175 #ifdef HAS_DVBIN_SUPPORT
3176 case MP_CMD_DVB_SET_CHANNEL:
3178 if((stream->type == STREAMTYPE_DVB) && stream->priv)
3180 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
3181 if(priv->is_on)
3183 if(priv->list->current <= cmd->args[0].v.i)
3184 last_dvb_step = 1;
3185 else
3186 last_dvb_step = -1;
3188 if(dvb_set_channel(priv, cmd->args[1].v.i, cmd->args[0].v.i))
3190 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
3191 cache_uninit(stream);
3192 goto goto_enable_cache;
3197 #endif
3198 case MP_CMD_TV_LAST_CHANNEL : {
3199 if (file_format == DEMUXER_TYPE_TV) {
3200 tv_last_channel((tvi_handle_t*)(demuxer->priv));
3201 #ifdef USE_OSD
3202 if (tv_channel_list) {
3203 osd_show_tv_channel = sh_video->fps;
3204 vo_osd_changed(OSDTYPE_SUBTITLE);
3206 #endif
3208 } break;
3209 case MP_CMD_TV_STEP_NORM : {
3210 if (file_format == DEMUXER_TYPE_TV)
3211 tv_step_norm((tvi_handle_t*)(demuxer->priv));
3212 } break;
3213 case MP_CMD_TV_STEP_CHANNEL_LIST : {
3214 if (file_format == DEMUXER_TYPE_TV)
3215 tv_step_chanlist((tvi_handle_t*)(demuxer->priv));
3216 } break;
3217 #endif
3218 case MP_CMD_SWITCH_VSYNC: {
3219 vo_vsync = ( cmd->nargs > 0 )? cmd->args[0].v.i : !vo_vsync;
3220 } break;
3221 case MP_CMD_VO_FULLSCREEN:
3223 #ifdef HAVE_NEW_GUI
3224 if ( use_gui ) guiGetEvent( guiIEvent,(char *)MP_CMD_GUI_FULLSCREEN );
3225 else
3226 #endif
3227 if(video_out && vo_config_count) video_out->control(VOCTRL_FULLSCREEN, 0);
3228 } break;
3229 case MP_CMD_VO_ONTOP:
3231 if(video_out && vo_config_count) {
3232 video_out->control(VOCTRL_ONTOP, 0);
3233 #ifdef USE_OSD
3234 osd_show_ontop=10;
3235 vo_osd_changed(OSDTYPE_SUBTITLE);
3236 #endif
3239 } break;
3240 case MP_CMD_VO_ROOTWIN:
3242 if(video_out && vo_config_count) {
3243 video_out->control(VOCTRL_ROOTWIN, 0);
3244 #ifdef USE_OSD
3245 osd_show_rootwin=10;
3246 vo_osd_changed(OSDTYPE_SUBTITLE);
3247 #endif
3250 } break;
3251 case MP_CMD_PANSCAN : {
3252 if ( !video_out ) break;
3253 if ( video_out->control( VOCTRL_GET_PANSCAN,NULL ) == VO_TRUE )
3255 int abs= cmd->args[1].v.i;
3256 float v = cmd->args[0].v.f;
3257 float res;
3258 if(abs) res = v;
3259 else res = vo_panscan+v;
3260 vo_panscan = res > 1 ? 1 : res < 0 ? 0 : res;
3261 video_out->control( VOCTRL_SET_PANSCAN,NULL );
3262 #ifdef USE_OSD
3263 if(osd_level && sh_video){
3264 osd_visible=sh_video->fps; // 1 sec
3265 vo_osd_progbar_type=OSD_PANSCAN;
3266 vo_osd_progbar_value=vo_panscan*256;
3267 vo_osd_changed(OSDTYPE_PROGBAR);
3268 #ifdef HAVE_FREETYPE
3269 if (subtitle_autoscale == 2 || subtitle_autoscale == 3)
3270 // force scaling font to movie width or diagonal
3271 force_load_font = 1;
3272 #endif
3274 #endif
3276 } break;
3277 case MP_CMD_SUB_POS:
3279 #ifdef USE_SUB
3280 if (sh_video) {
3281 int v;
3282 v = cmd->args[0].v.i;
3284 sub_pos+=v;
3285 if(sub_pos >100) sub_pos=100;
3286 if(sub_pos <0) sub_pos=0;
3287 vo_osd_changed(OSDTYPE_SUBTITLE);
3288 osd_show_sub_pos = 9;
3290 #endif
3291 } break;
3292 case MP_CMD_SUB_ALIGNMENT:
3294 #ifdef USE_SUB
3295 if (sh_video) {
3296 if (cmd->nargs >= 1)
3297 sub_alignment = cmd->args[0].v.i;
3298 else
3299 sub_alignment = (sub_alignment+1) % 3;
3300 osd_show_sub_alignment = 9;
3301 vo_osd_changed(OSDTYPE_SUBTITLE);
3303 #endif
3304 } break;
3305 case MP_CMD_SUB_VISIBILITY:
3307 #ifdef USE_SUB
3308 if (sh_video) {
3309 sub_visibility=1-sub_visibility;
3310 osd_show_sub_visibility = 9; // show state of subtitle visibility in OSD
3311 vo_osd_changed(OSDTYPE_SUBTITLE);
3313 #endif
3314 } break;
3315 case MP_CMD_GET_SUB_VISIBILITY:
3317 #ifdef USE_SUB
3318 if (sh_video) {
3319 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsSubVisibility, sub_visibility);
3321 #endif
3322 } break;
3323 case MP_CMD_SUB_SELECT:
3324 if (global_sub_size) {
3325 int source = -1;
3327 global_sub_pos++;
3328 if (global_sub_pos >= global_sub_size)
3329 global_sub_pos = -1;
3330 if (global_sub_pos >= 0)
3331 source = sub_source();
3333 mp_msg(MSGT_CPLAYER, MSGL_DBG3, "subtitles: %d subs, (v@%d s@%d d@%d), @%d, source @%d\n",
3334 global_sub_size, global_sub_indices[SUB_SOURCE_VOBSUB],
3335 global_sub_indices[SUB_SOURCE_SUBS], global_sub_indices[SUB_SOURCE_DEMUX],
3336 global_sub_pos, source);
3338 #ifdef USE_SUB
3339 set_of_sub_pos = -1;
3340 subdata = NULL;
3341 vo_sub = NULL;
3342 #endif
3343 vobsub_id = -1;
3344 dvdsub_id = -1;
3345 if (d_dvdsub) d_dvdsub->id = -1;
3347 // be careful!
3348 // if sub_changed is till on but subdata's been reset, bad things happen.
3349 osd_show_vobsub_changed = 0;
3350 osd_show_sub_changed = 0;
3352 if (source == SUB_SOURCE_VOBSUB) {
3353 vobsub_id = global_sub_pos - global_sub_indices[SUB_SOURCE_VOBSUB];
3354 if (!global_sub_quiet_osd_hack) osd_show_vobsub_changed = sh_video->fps;
3355 #ifdef USE_SUB
3356 } else if (source == SUB_SOURCE_SUBS) {
3357 set_of_sub_pos = global_sub_pos - global_sub_indices[SUB_SOURCE_SUBS];
3358 subdata = set_of_subtitles[set_of_sub_pos];
3359 if (!global_sub_quiet_osd_hack) osd_show_sub_changed = sh_video->fps;
3360 vo_osd_changed(OSDTYPE_SUBTITLE);
3362 // FIXME: is this the correct place for these?
3363 if(stream_dump_type==3) list_sub_file(subdata);
3364 if(stream_dump_type==4) dump_mpsub(subdata, sh_video->fps);
3365 if(stream_dump_type==6) dump_srt(subdata, sh_video->fps);
3366 if(stream_dump_type==7) dump_microdvd(subdata, sh_video->fps);
3367 if(stream_dump_type==8) dump_jacosub(subdata, sh_video->fps);
3368 if(stream_dump_type==9) dump_sami(subdata, sh_video->fps);
3369 #endif
3370 } else if (source == SUB_SOURCE_DEMUX) {
3371 dvdsub_id = global_sub_pos - global_sub_indices[SUB_SOURCE_DEMUX];
3372 if (d_dvdsub) {
3373 #ifdef USE_DVDREAD
3374 if (vo_spudec && stream->type == STREAMTYPE_DVD) {
3375 d_dvdsub->id = dvdsub_id;
3376 spudec_reset(vo_spudec);
3378 #endif
3379 #ifdef HAVE_OGGVORBIS
3380 if (demuxer->type == DEMUXER_TYPE_OGG)
3381 d_dvdsub->id = demux_ogg_sub_id(demuxer, dvdsub_id);
3382 #endif
3383 #ifdef HAVE_MATROSKA
3384 if (demuxer->type == DEMUXER_TYPE_MATROSKA) {
3385 d_dvdsub->id = demux_mkv_change_subs(demuxer, dvdsub_id);
3386 if (d_dvdsub->id >= 0 && ((mkv_sh_sub_t *)d_dvdsub->sh)->type == 'v') {
3387 mkv_sh_sub_t *mkv_sh_sub = (mkv_sh_sub_t *)d_dvdsub->sh;
3388 if (vo_spudec != NULL)
3389 spudec_free(vo_spudec);
3390 vo_spudec =
3391 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
3392 mkv_sh_sub->custom_colors,
3393 mkv_sh_sub->width,
3394 mkv_sh_sub->height);
3395 if (!forced_subs_only)
3396 forced_subs_only = mkv_sh_sub->forced_subs_only;
3397 if (vo_spudec) {
3398 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
3399 inited_flags |= INITED_SPUDEC;
3403 #endif
3405 if (!global_sub_quiet_osd_hack) osd_show_vobsub_changed = sh_video->fps;
3406 } else { // off
3407 if (!global_sub_quiet_osd_hack) osd_show_vobsub_changed = sh_video->fps;
3408 #ifdef USE_SUB
3409 vo_osd_changed(OSDTYPE_SUBTITLE);
3410 #endif
3412 // it's annoying and dumb to show osd saying "off" at every subless file...
3413 global_sub_quiet_osd_hack = 0;
3415 break;
3416 case MP_CMD_SUB_FORCED_ONLY:
3417 if (vo_spudec) {
3418 forced_subs_only = forced_subs_only ? 0 : ~0; // toggle state
3419 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3421 break;
3422 case MP_CMD_SCREENSHOT :
3423 if(vo_config_count) video_out->control(VOCTRL_SCREENSHOT, NULL);
3424 break;
3425 case MP_CMD_VF_CHANGE_RECTANGLE:
3426 set_rectangle(sh_video, cmd->args[0].v.i, cmd->args[1].v.i);
3427 break;
3429 case MP_CMD_GET_TIME_LENGTH : {
3430 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsLength, demuxer_get_time_length(demuxer));
3431 } break;
3433 case MP_CMD_GET_VO_FULLSCREEN : {
3434 if(video_out && vo_config_count)
3435 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsVoFullscreen, vo_fs);
3436 } break;
3438 case MP_CMD_GET_PERCENT_POS : {
3439 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_AnsPercentPos, demuxer_get_percent_pos(demuxer));
3440 } break;
3441 #ifdef HAVE_MENU
3442 case MP_CMD_CRUN : {
3443 #ifndef __MINGW32__
3444 if(!fork()) {
3445 execl("/bin/sh","sh","-c",cmd->args[0].v.s,NULL);
3446 exit(0);
3448 #endif
3449 } break;
3450 #endif
3452 #ifdef USE_DVDNAV
3453 case MP_CMD_DVDNAV_EVENT: {
3454 dvdnav_priv_t * dvdnav_priv = (dvdnav_priv_t*)(stream->priv);
3455 dvdnav_event_t * dvdnav_event = (dvdnav_event_t *)(cmd->args[0].v.v);
3457 /* ignore these events if we're not in dvd_nav mode */
3458 if (stream->type != STREAMTYPE_DVDNAV) break;
3460 if (!dvdnav_event) {
3461 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNullEvent);
3462 break;
3465 //printf("mplayer: got event: %d\n",dvdnav_event->event);
3467 switch (dvdnav_event->event) {
3468 case DVDNAV_BLOCK_OK: {
3469 /* be silent about this one */
3470 break;
3472 case DVDNAV_HIGHLIGHT: {
3473 dvdnav_highlight_event_t *hevent = (dvdnav_highlight_event_t*)(dvdnav_event->details);
3474 if (!hevent) {
3475 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavHighlightEventBroken);
3476 break;
3479 if (hevent->display && hevent->buttonN>0)
3481 //dvdnav_priv->seen_root_menu=1; /* if we got a highlight, we're on a menu */
3482 sprintf( dvd_nav_text, "Highlight button %d (%u,%u)-(%u,%u) PTS %d (now is %5.2f)",
3483 hevent->buttonN,
3484 hevent->sx,hevent->sy,
3485 hevent->ex,hevent->ey,
3486 hevent->pts, d_video->pts);
3487 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavEvent,dvd_nav_text);
3488 //osd_show_dvd_nav_delay = 60;
3490 osd_show_dvd_nav_highlight=1;
3491 osd_show_dvd_nav_sx=hevent->sx;
3492 osd_show_dvd_nav_ex=hevent->ex;
3493 osd_show_dvd_nav_sy=hevent->sy;
3494 osd_show_dvd_nav_ey=hevent->ey;
3496 else {
3497 osd_show_dvd_nav_highlight=0;
3498 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavHighlightHide);
3500 break;
3502 case DVDNAV_STILL_FRAME: {
3503 dvdnav_still_event_t *still_event = (dvdnav_still_event_t*)(dvdnav_event->details);
3505 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavStillFrame, still_event->length );
3506 while (dvdnav_stream_sleeping(dvdnav_priv)) {
3507 usec_sleep(1000); /* 1ms */
3509 dvdnav_stream_sleep(dvdnav_priv,still_event->length);
3510 break;
3512 case DVDNAV_STOP: {
3513 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavStop );
3514 break;
3516 case DVDNAV_NOP: {
3517 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavNOP);
3518 break;
3520 case DVDNAV_SPU_STREAM_CHANGE: {
3521 #if DVDNAVVERSION > 012
3522 dvdnav_spu_stream_change_event_t *stream_change = (dvdnav_spu_stream_change_event_t*)(dvdnav_event->details);
3524 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSpuStreamChangeVerbose,
3525 stream_change->physical_wide,
3526 stream_change->physical_letterbox,
3527 stream_change->physical_pan_scan,
3528 stream_change->logical);
3530 if (vo_spudec && dvdsub_id!=stream_change->physical_wide) {
3531 mp_msg(MSGT_INPUT,MSGL_DBG2,"d_dvdsub->id change: was %d is now %d\n",
3532 d_dvdsub->id,stream_change->physical_wide);
3533 // FIXME: need a better way to change SPU id
3534 d_dvdsub->id=dvdsub_id=stream_change->physical_wide;
3535 if (vo_spudec) spudec_reset(vo_spudec);
3537 #else
3538 dvdnav_stream_change_event_t *stream_change = (dvdnav_stream_change_event_t*)(dvdnav_event->details);
3540 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSpuStreamChange,
3541 stream_change->physical,
3542 stream_change->logical);
3544 if (vo_spudec && dvdsub_id!=stream_change->physical) {
3545 mp_msg(MSGT_INPUT,MSGL_DBG2,"d_dvdsub->id change: was %d is now %d\n",
3546 d_dvdsub->id,stream_change->physical);
3547 // FIXME: need a better way to change SPU id
3548 d_dvdsub->id=dvdsub_id=stream_change->physical;
3549 if (vo_spudec) spudec_reset(vo_spudec);
3551 #endif
3552 break;
3554 case DVDNAV_AUDIO_STREAM_CHANGE: {
3555 int aid_temp;
3556 #if DVDNAVVERSION > 012
3557 dvdnav_audio_stream_change_event_t *stream_change = (dvdnav_audio_stream_change_event_t*)(dvdnav_event->details);
3558 #else
3559 dvdnav_stream_change_event_t *stream_change = (dvdnav_stream_change_event_t*)(dvdnav_event->details);
3560 #endif
3562 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavAudioStreamChange,
3563 stream_change->physical,
3564 stream_change->logical);
3566 aid_temp=stream_change->physical;
3567 if (aid_temp>=0) aid_temp+=128; // FIXME: is this sane?
3568 if (d_audio && audio_id!=aid_temp) {
3569 mp_msg(MSGT_INPUT,MSGL_DBG2,"d_audio->id change: was %d is now %d\n",
3570 d_audio->id,aid_temp);
3571 // FIXME: need a bettery way to change audio stream id
3572 d_audio->id=dvdsub_id=aid_temp;
3573 if(sh_audio) resync_audio_stream(sh_audio);
3576 break;
3578 case DVDNAV_VTS_CHANGE: {
3579 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavVTSChange);
3580 break;
3582 case DVDNAV_CELL_CHANGE: {
3583 dvdnav_cell_change_event_t *cell_change = (dvdnav_cell_change_event_t*)(dvdnav_event->details);
3584 cell_playback_t * cell_playback = cell_change->new_cell;
3586 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavCellChange);
3587 osd_show_dvd_nav_highlight=0; /* screen changed, disable menu */
3589 printf("new still time: %d\n",cell_playback->still_time);
3590 printf("new cell_cmd_nr: %d\n",cell_playback->cell_cmd_nr);
3591 printf("new playback_time: %02d:%02d:%02d.%02d\n",
3592 cell_playback->playback_time.hour,
3593 cell_playback->playback_time.minute,
3594 cell_playback->playback_time.second,
3595 cell_playback->playback_time.frame_u);
3598 //rel_seek_secs=1; // not really: we can't seek, but it'll reset the muxer
3599 //abs_seek_pos=0;
3600 break;
3602 case DVDNAV_NAV_PACKET: {
3603 // printf("DVDNAV Event: Nav Packet\n");
3604 break;
3606 case DVDNAV_SPU_CLUT_CHANGE: {
3607 uint32_t * new_clut = (uint32_t *)(dvdnav_event->details);
3609 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSpuClutChange);
3610 // send new palette to SPU decoder
3611 if (vo_spudec) spudec_update_palette(vo_spudec,new_clut);
3613 break;
3615 case DVDNAV_SEEK_DONE: {
3616 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_DvdnavNavSeekDone);
3617 break;
3621 // free the dvdnav event
3622 free(dvdnav_event->details);
3623 free(dvdnav_event);
3624 cmd->args[0].v.v=NULL;
3626 case MP_CMD_DVDNAV: {
3627 dvdnav_priv_t * dvdnav_priv=(dvdnav_priv_t*)stream->priv;
3629 /* ignore these events if we're not in dvd_nav mode */
3630 if (stream->type != STREAMTYPE_DVDNAV) break;
3632 switch (cmd->args[0].v.i) {
3633 case MP_CMD_DVDNAV_UP:
3634 dvdnav_upper_button_select(dvdnav_priv->dvdnav);
3635 break;
3636 case MP_CMD_DVDNAV_DOWN:
3637 dvdnav_lower_button_select(dvdnav_priv->dvdnav);
3638 break;
3639 case MP_CMD_DVDNAV_LEFT:
3640 dvdnav_left_button_select(dvdnav_priv->dvdnav);
3641 break;
3642 case MP_CMD_DVDNAV_RIGHT:
3643 dvdnav_right_button_select(dvdnav_priv->dvdnav);
3644 break;
3645 case MP_CMD_DVDNAV_MENU:
3646 mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_MenuCall);
3647 dvdnav_menu_call(dvdnav_priv->dvdnav,DVD_MENU_Root);
3648 break;
3649 case MP_CMD_DVDNAV_SELECT:
3650 dvdnav_button_activate(dvdnav_priv->dvdnav);
3651 break;
3652 default:
3653 mp_msg(MSGT_CPLAYER, MSGL_V, "Weird DVD Nav cmd %d\n",cmd->args[0].v.i);
3654 break;
3656 break;
3658 #endif
3659 default : {
3660 #ifdef HAVE_NEW_GUI
3661 if ( ( use_gui )&&( cmd->id > MP_CMD_GUI_EVENTS ) ) guiGetEvent( guiIEvent,(char *)cmd->id );
3662 else
3663 #endif
3664 mp_msg(MSGT_CPLAYER, MSGL_V, "Received unknown cmd %s\n",cmd->name);
3667 if (cmd->pausing)
3668 osd_function = OSD_PAUSE;
3669 mp_cmd_free(cmd);
3673 if (seek_to_sec) {
3674 int a,b; float d;
3676 if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3)
3677 rel_seek_secs += 3600*a +60*b +d ;
3678 else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2)
3679 rel_seek_secs += 60*a +d;
3680 else if (sscanf(seek_to_sec, "%f", &d)==1)
3681 rel_seek_secs += d;
3683 seek_to_sec = NULL;
3686 /* Looping. */
3687 if(eof==1 && loop_times>=0) {
3688 int l = loop_times;
3689 play_tree_iter_step(playtree_iter,0,0);
3690 loop_times = l;
3691 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", loop_times,eof);
3693 if(loop_times>1) loop_times--; else
3694 if(loop_times==1) loop_times=-1;
3695 play_n_frames=play_n_frames_mf;
3696 eof=0;
3697 abs_seek_pos=3; rel_seek_secs=0; // seek to start of movie (0%)
3698 loop_seek = 1;
3701 if(rel_seek_secs || abs_seek_pos){
3702 current_module="seek";
3703 if(demux_seek(demuxer,rel_seek_secs,abs_seek_pos)){
3704 // success:
3705 /* FIXME there should be real seeking for vobsub */
3706 if(sh_video) sh_video->pts=d_video->pts;
3707 if (vo_vobsub)
3708 //vobsub_reset(vo_vobsub);
3709 vobsub_seek(vo_vobsub,sh_video->pts);
3710 #if 0
3711 if(sh_video && d_video->packs == 0)
3712 ds_fill_buffer(d_video);
3713 if(sh_audio){
3714 if(d_audio->packs == 0)
3715 ds_fill_buffer(d_audio);
3716 if(verbose>0){
3717 float a_pts=d_audio->pts;
3718 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
3719 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);
3721 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);
3722 } else {
3723 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A: --- V:%6.1f \r",d_video->pts);
3725 #endif
3726 fflush(stdout);
3728 if(sh_video){
3729 current_module="seek_video_reset";
3730 resync_video_stream(sh_video);
3731 if(vo_config_count) video_out->control(VOCTRL_RESET,NULL);
3734 if(sh_audio){
3735 current_module="seek_audio_reset";
3736 audio_out->reset(); // stop audio, throwing away buffered data
3738 #ifdef USE_OSD
3739 // Set OSD:
3740 if(osd_level && !loop_seek){
3741 #ifdef USE_EDL
3742 if( !edl_decision ) {
3743 #else
3744 if( 1 ) { // Let the compiler optimize this out
3745 #endif
3746 int len=((demuxer->movi_end-demuxer->movi_start)>>8);
3747 if (len>0 && sh_video){
3748 osd_visible=sh_video->fps; // 1 sec
3749 vo_osd_progbar_type=0;
3750 vo_osd_progbar_value=(demuxer->filepos-demuxer->movi_start)/len;
3751 vo_osd_changed(OSDTYPE_PROGBAR);
3755 #endif
3756 if(sh_video) {
3757 c_total=0;
3758 max_pts_correction=0.1;
3759 osd_visible=sh_video->fps; // to rewert to PLAY pointer after 1 sec
3760 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3761 drop_frame_cnt=0;
3762 too_slow_frame_cnt=0;
3763 too_fast_frame_cnt=0;
3765 if(vo_spudec) spudec_reset(vo_spudec);
3768 #ifdef USE_EDL
3770 * We saw a seek, have to rewind the EDL operations stack
3771 * and find the next EDL action to take care of.
3774 edl_muted = 0;
3775 next_edl_record = edl_records;
3777 while (next_edl_record)
3779 /* Trying to remember if we need to mute/unmute first;
3780 * prior EDL implementation lacks this.
3783 if (next_edl_record->start_sec >= sh_video->pts)
3784 break;
3786 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted;
3787 next_edl_record = next_edl_record->next;
3790 if ((user_muted | edl_muted) != mixer.muted) mixer_mute(&mixer);
3791 #endif
3792 rel_seek_secs=0;
3793 abs_seek_pos=0;
3794 frame_time_remaining=0;
3795 current_module=NULL;
3796 loop_seek=0;
3799 #ifdef HAVE_NEW_GUI
3800 if(use_gui){
3801 guiEventHandling();
3802 if(demuxer->file_format==DEMUXER_TYPE_AVI && sh_video && sh_video->video.dwLength>2){
3803 // get pos from frame number / total frames
3804 guiIntfStruct.Position=(float)d_video->pack_no*100.0f/sh_video->video.dwLength;
3805 } else {
3806 off_t len = ( demuxer->movi_end - demuxer->movi_start );
3807 off_t pos = ( demuxer->file_format == DEMUXER_TYPE_AUDIO?stream->pos:demuxer->filepos );
3808 guiIntfStruct.Position=(len <= 0? 0.0f : ( pos - demuxer->movi_start ) * 100.0f / len );
3810 if ( sh_video ) guiIntfStruct.TimeSec=sh_video->pts;
3811 else if ( sh_audio ) guiIntfStruct.TimeSec=sh_audio->delay;
3812 guiIntfStruct.LengthInSec=demuxer_get_time_length(demuxer);
3813 guiGetEvent( guiReDraw,NULL );
3814 guiGetEvent( guiSetVolume,NULL );
3815 if(guiIntfStruct.Playing==0) break; // STOP
3816 if(guiIntfStruct.Playing==2) osd_function=OSD_PAUSE;
3817 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3818 #ifdef USE_DVDREAD
3819 if ( stream->type == STREAMTYPE_DVD )
3821 dvd_priv_t * dvdp = stream->priv;
3822 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3824 #endif
3826 #endif
3829 //================= Update OSD ====================
3830 #ifdef USE_OSD
3831 if(osd_level>=1 && sh_video){
3832 int pts=sh_video->pts;
3833 char osd_text_tmp[64];
3834 if(pts==osd_last_pts-1) ++pts; else osd_last_pts=pts;
3835 vo_osd_text=osd_text_buffer;
3836 #ifdef USE_DVDNAV
3837 if (osd_show_dvd_nav_delay) {
3838 snprintf(osd_text_tmp, 63, "DVDNAV: %s", dvd_nav_text);
3839 osd_show_dvd_nav_delay--;
3840 } else
3841 #endif
3842 #ifdef USE_TV
3843 if (osd_show_tv_channel && tv_channel_list) {
3844 snprintf(osd_text_tmp, 63, "Channel: %s", tv_channel_current->name);
3845 osd_show_tv_channel--;
3846 } else
3847 #endif
3848 if (osd_show_text) {
3849 snprintf(osd_text_tmp, 63, "%s", osd_show_text_buffer);
3850 osd_show_text--;
3851 } else
3852 if (osd_show_sub_visibility) {
3853 snprintf(osd_text_tmp, 63, "Subtitles: %sabled", sub_visibility?"en":"dis");
3854 osd_show_sub_visibility--;
3855 } else
3856 if (osd_show_vobsub_changed) {
3857 if (vo_vobsub && vobsub_id >= 0) {
3858 const char *language = "none";
3859 language = vobsub_get_id(vo_vobsub, (unsigned int) vobsub_id);
3860 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", vobsub_id, language ? language : "unknown");
3862 #ifdef HAVE_OGGVORBIS
3863 if (d_dvdsub && demuxer->type == DEMUXER_TYPE_OGG) {
3864 if (dvdsub_id < 0)
3865 snprintf(osd_text_tmp, 63, "Subtitles: (off)");
3866 else {
3867 char *lang = demux_ogg_sub_lang(demuxer, dvdsub_id);
3868 if (!lang) lang = "unknown";
3869 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
3872 #endif
3873 #ifdef USE_DVDREAD
3874 if (vo_spudec && (demuxer->type != DEMUXER_TYPE_MATROSKA)) {
3875 char lang[5] = "none";
3876 int code = 0;
3877 if (dvdsub_id >= 0) code = dvd_lang_from_sid(stream, dvdsub_id);
3878 if (code) {
3879 lang[0] = code >> 8;
3880 lang[1] = code;
3881 lang[2] = 0;
3883 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
3885 #endif
3886 #ifdef HAVE_MATROSKA
3887 if (demuxer->type == DEMUXER_TYPE_MATROSKA) {
3888 char lang[10] = "unknown";
3889 if (dvdsub_id >= 0) {
3890 demux_mkv_get_sub_lang(demuxer, dvdsub_id, lang, 9);
3891 lang[9] = 0;
3892 } else
3893 strcpy(lang, "off");
3894 snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
3896 #endif
3897 osd_show_vobsub_changed--;
3898 } else
3899 #ifdef USE_SUB
3900 if (osd_show_sub_changed) {
3901 char *tmp2;
3902 tmp = subdata->filename;
3903 if ((tmp2 = strrchr(tmp, '/'))) {
3904 tmp = tmp2+1;
3906 snprintf(osd_text_tmp, 63, "Sub: (%d) %s%s",
3907 set_of_sub_pos + 1,
3908 strlen(tmp) < 20 ? "" : "...",
3909 strlen(tmp) < 20 ? tmp : tmp+strlen(tmp)-19);
3910 osd_show_sub_changed--;
3911 } else
3912 #endif
3913 if (osd_show_sub_delay) {
3914 snprintf(osd_text_tmp, 63, "Sub delay: %d ms", ROUND(sub_delay*1000));
3915 osd_show_sub_delay--;
3916 } else
3917 if (osd_show_sub_pos) {
3918 snprintf(osd_text_tmp, 63, "Sub position: %d/100", sub_pos);
3919 osd_show_sub_pos--;
3920 } else
3921 if (osd_show_sub_alignment) {
3922 snprintf(osd_text_tmp, 63, "Sub alignment: %s",
3923 (sub_alignment == 2 ? "bottom" :
3924 (sub_alignment == 1 ? "center" : "top")));
3925 osd_show_sub_alignment--;
3926 } else
3927 if (osd_show_av_delay) {
3928 snprintf(osd_text_tmp, 63, "A-V delay: %d ms", ROUND(audio_delay*1000));
3929 osd_show_av_delay--;
3930 } else if (osd_show_speed) {
3931 snprintf(osd_text_tmp, 63, "Speed: x %6.2f", playback_speed);
3932 osd_show_speed--;
3933 } else if (osd_show_ontop) {
3934 snprintf(osd_text_tmp, 63, "Stay on top: %sabled", vo_ontop?"en":"dis");
3935 osd_show_ontop--;
3936 } else if (osd_show_rootwin) {
3937 snprintf(osd_text_tmp, 63, "Rootwin: %sabled", vo_rootwin?"en":"dis");
3938 osd_show_rootwin--;
3939 } else if (osd_show_framedropping) {
3940 snprintf(osd_text_tmp, 63, "Framedropping: %s",
3941 (frame_dropping == 1 ? "on" :
3942 (frame_dropping == 2 ? "hard" : "off")));
3943 osd_show_framedropping--;
3944 } else if(osd_level>=2) {
3945 int len = demuxer_get_time_length(demuxer);
3946 int percentage = -1;
3947 char percentage_text[10];
3948 if (osd_show_percentage) {
3949 percentage = demuxer_get_percent_pos(demuxer);
3950 osd_show_percentage--;
3952 if (percentage >= 0)
3953 snprintf(percentage_text, 9, " (%d%%)", percentage);
3954 else
3955 percentage_text[0] = 0;
3956 if (osd_level == 3)
3957 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);
3958 else
3959 snprintf(osd_text_tmp, 63, "%c %02d:%02d:%02d%s",osd_function,pts/3600,(pts/60)%60,pts%60,percentage_text);
3960 } else osd_text_tmp[0]=0;
3962 if(strcmp(vo_osd_text, osd_text_tmp)) {
3963 strncpy(vo_osd_text, osd_text_tmp, 63);
3964 vo_osd_changed(OSDTYPE_OSD);
3966 } else {
3967 if(vo_osd_text) {
3968 vo_osd_text=NULL;
3969 vo_osd_changed(OSDTYPE_OSD);
3972 // for(i=1;i<=11;i++) osd_text_buffer[10+i]=i;osd_text_buffer[10+i]=0;
3973 // vo_osd_text=osd_text_buffer;
3974 #endif
3976 #ifdef USE_SUB
3977 // find sub
3978 if(subdata && sh_video->pts>0){
3979 float pts=sh_video->pts;
3980 if(sub_fps==0) sub_fps=sh_video->fps;
3981 current_module="find_sub";
3982 if (pts > sub_last_pts || pts < sub_last_pts-1.0 ) {
3983 find_sub(subdata, (pts+sub_delay) *
3984 (subdata->sub_uses_time ? 100. : sub_fps));
3985 // FIXME! frame counter...
3986 sub_last_pts = pts;
3988 current_module=NULL;
3990 #endif
3992 #ifdef HAVE_X11
3993 if (stop_xscreensaver && sh_video) {
3994 current_module="stop_xscreensaver";
3995 xscreensaver_heartbeat();
3996 current_module=NULL;
3998 #endif
4000 // DVD sub:
4001 if(vo_config_count && vo_spudec) {
4002 unsigned char* packet=NULL;
4003 int len,timestamp;
4004 current_module="spudec";
4005 spudec_heartbeat(vo_spudec,90000*sh_video->timer);
4006 // Get a sub packet from the dvd or a vobsub and make a timestamp relative to sh_video->timer
4007 while(1) {
4008 // Vobsub
4009 len = 0;
4010 if(vo_vobsub) {
4011 if(sh_video->pts+sub_delay>=0) {
4012 // The + next_frame_time is there because we'll display the sub at the next frame
4013 len = vobsub_get_packet(vo_vobsub,sh_video->pts+sub_delay+next_frame_time,(void**)&packet,&timestamp);
4014 if(len > 0) {
4015 timestamp -= (sh_video->pts + sub_delay - sh_video->timer)*90000;
4016 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);
4019 } else {
4020 // DVD sub
4021 len = ds_get_packet_sub(d_dvdsub,(unsigned char**)&packet);
4022 if(len > 0) {
4023 float x = d_dvdsub->pts - sh_video->pts;
4024 if (x < -10 || x > 10) // prevent missing subs on pts reset
4025 timestamp = 90000*(sh_video->timer + d_dvdsub->pts + sub_delay - sh_video->pts);
4026 else timestamp = 90000*(sh_video->timer + sub_delay);
4027 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);
4030 if(len<=0 || !packet) break;
4031 if(timestamp < 0) timestamp = 0;
4032 else spudec_assemble(vo_spudec,packet,len,timestamp);
4035 /* detect wether the sub has changed or not */
4036 if(spudec_changed(vo_spudec))
4037 vo_osd_changed(OSDTYPE_SPU);
4038 current_module=NULL;
4041 } // while(!eof)
4043 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",eof);
4047 goto_next_file: // don't jump here after ao/vo/getch initialization!
4049 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4051 if(benchmark){
4052 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4053 double total_time_usage;
4054 total_time_usage_start=GetTimer()-total_time_usage_start;
4055 total_time_usage = (float)total_time_usage_start*0.000001;
4056 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4057 video_time_usage,vout_time_usage,audio_time_usage,
4058 total_time_usage-tot,total_time_usage);
4059 if(total_time_usage>0.0)
4060 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4061 100.0*video_time_usage/total_time_usage,
4062 100.0*vout_time_usage/total_time_usage,
4063 100.0*audio_time_usage/total_time_usage,
4064 100.0*(total_time_usage-tot)/total_time_usage,
4065 100.0);
4066 if(total_frame_cnt && frame_dropping)
4067 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4068 total_frame_cnt-drop_frame_cnt,
4069 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4070 drop_frame_cnt,
4071 100*drop_frame_cnt/total_frame_cnt,
4072 total_frame_cnt,
4073 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4077 // time to uninit all, except global stuff:
4078 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
4080 #ifdef USE_SUB
4081 if ( set_of_sub_size > 0 )
4083 current_module="sub_free";
4084 for (i = 0; i < set_of_sub_size; ++i)
4085 sub_free( set_of_subtitles[i] );
4086 set_of_sub_size = 0;
4087 vo_sub=NULL;
4088 subdata=NULL;
4090 #endif
4092 if(eof == PT_NEXT_ENTRY || eof == PT_PREV_ENTRY) {
4093 eof = eof == PT_NEXT_ENTRY ? 1 : -1;
4094 if(play_tree_iter_step(playtree_iter,play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
4095 eof = 1;
4096 } else {
4097 play_tree_iter_free(playtree_iter);
4098 playtree_iter = NULL;
4100 play_tree_step = 1;
4101 } else if (eof == PT_UP_NEXT || eof == PT_UP_PREV) {
4102 eof = eof == PT_UP_NEXT ? 1 : -1;
4103 if ( playtree_iter ) {
4104 if(play_tree_iter_up_step(playtree_iter,eof,0) == PLAY_TREE_ITER_ENTRY) {
4105 eof = 1;
4106 } else {
4107 play_tree_iter_free(playtree_iter);
4108 playtree_iter = NULL;
4111 } else { // NEXT PREV SRC
4112 eof = eof == PT_PREV_SRC ? -1 : 1;
4115 if(eof == 0) eof = 1;
4117 while(playtree_iter != NULL) {
4118 filename = play_tree_iter_get_file(playtree_iter,eof);
4119 if(filename == NULL) {
4120 if( play_tree_iter_step(playtree_iter,eof,0) != PLAY_TREE_ITER_ENTRY) {
4121 play_tree_iter_free(playtree_iter);
4122 playtree_iter = NULL;
4124 } else
4125 break;
4128 #ifdef HAVE_NEW_GUI
4129 if( use_gui && !playtree_iter )
4131 #ifdef USE_DVDREAD
4132 if ( !guiIntfStruct.DiskChanged )
4133 #endif
4134 mplEnd();
4136 #endif
4138 if(use_gui || playtree_iter != NULL){
4140 eof = 0;
4141 goto play_next_file;
4145 exit_player_with_rc(MSGTR_Exit_eof, 0);
4147 return 1;