Support send mouse movements commands to mplayer.
[mplayer/greg.git] / mplayer.c
blob2134d2360ffa48140b81cb16b1136c30a7f8c100
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
9 #ifdef WIN32
10 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
11 #include <windows.h>
12 #endif
13 #include <string.h>
14 #include <unistd.h>
16 // #include <sys/mman.h>
17 #include <sys/types.h>
18 #ifndef __MINGW32__
19 #include <sys/ioctl.h>
20 #include <sys/wait.h>
21 #else
22 #define SIGHUP 1 /* hangup */
23 #define SIGQUIT 3 /* quit */
24 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
25 #define SIGBUS 10 /* bus error */
26 #define SIGPIPE 13 /* broken pipe */
27 #endif
29 #include <sys/time.h>
30 #include <sys/stat.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include <errno.h>
39 #include "version.h"
41 #include "mp_msg.h"
43 #define HELP_MP_DEFINE_STATIC
44 #include "help_mp.h"
46 #include "m_option.h"
47 #include "m_config.h"
48 #include "m_property.h"
50 #include "cfg-mplayer-def.h"
52 #include "subreader.h"
54 #include "libvo/video_out.h"
56 #include "libvo/font_load.h"
57 #include "libvo/sub.h"
59 #ifdef HAVE_X11
60 #include "libvo/x11_common.h"
61 #endif
63 #include "libao2/audio_out.h"
65 #include "codec-cfg.h"
67 #include "edl.h"
69 #include "spudec.h"
70 #include "vobsub.h"
72 #include "osdep/getch2.h"
73 #include "osdep/timer.h"
75 #include "cpudetect.h"
77 #ifdef HAVE_NEW_GUI
78 #include "gui/interface.h"
79 #endif
81 #include "input/input.h"
83 int slave_mode=0;
84 int player_idle_mode=0;
85 int quiet=0;
86 int enable_mouse_movements=0;
88 #ifdef WIN32
89 char * proc_priority=NULL;
90 #endif
92 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
94 #ifdef HAVE_RTC
95 #ifdef __linux__
96 #include <linux/rtc.h>
97 #else
98 #include <rtc.h>
99 #define RTC_IRQP_SET RTCIO_IRQP_SET
100 #define RTC_PIE_ON RTCIO_PIE_ON
101 #endif /* __linux__ */
102 #endif /* HAVE_RTC */
104 #ifdef USE_TV
105 #include "stream/tv.h"
106 #endif
107 #ifdef USE_RADIO
108 #include "stream/stream_radio.h"
109 #endif
111 #ifdef HAS_DVBIN_SUPPORT
112 #include "stream/dvbin.h"
113 #include "stream/cache2.h"
114 #endif
116 //**************************************************************************//
117 // Playtree
118 //**************************************************************************//
119 #include "playtree.h"
120 #include "playtreeparser.h"
122 #ifdef HAVE_NEW_GUI
123 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
124 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
125 #endif
127 //**************************************************************************//
128 // Config
129 //**************************************************************************//
130 #include "parser-cfg.h"
131 #include "parser-mpcmd.h"
133 m_config_t* mconfig;
135 //**************************************************************************//
136 // Config file
137 //**************************************************************************//
139 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
141 static int cfg_include(m_option_t *conf, char *filename){
142 return m_config_parse_config_file(mconfig, filename);
145 #include "get_path.h"
147 //**************************************************************************//
148 // XScreensaver
149 //**************************************************************************//
151 #ifdef HAVE_X11
152 void xscreensaver_heartbeat(void);
153 #endif
155 //**************************************************************************//
156 //**************************************************************************//
157 // Input media streaming & demultiplexer:
158 //**************************************************************************//
160 static int max_framesize=0;
162 #include "stream/stream.h"
163 #include "libmpdemux/demuxer.h"
164 #include "libmpdemux/stheader.h"
165 //#include "parse_es.h"
166 #include "libmpdemux/matroska.h"
168 #ifdef USE_DVDREAD
169 #include "stream/stream_dvd.h"
170 #endif
172 #ifdef USE_DVDNAV
173 #include "stream/stream_dvdnav.h"
174 #endif
176 #include "libmpcodecs/dec_audio.h"
177 #include "libmpcodecs/dec_video.h"
178 #include "libmpcodecs/mp_image.h"
179 #include "libmpcodecs/vf.h"
180 #include "libmpcodecs/vd.h"
182 #include "mixer.h"
184 #include "mp_core.h"
186 //**************************************************************************//
187 //**************************************************************************//
189 // Common FIFO functions, and keyboard/event FIFO code
190 #include "mp_fifo.h"
191 int noconsolecontrols=0;
192 //**************************************************************************//
194 // Not all functions in mplayer.c take the context as an argument yet
195 static MPContext mpctx_s = {
196 .osd_function = OSD_PLAY,
197 .begin_skip = MP_NOPTS_VALUE,
198 .play_tree_step = 1,
199 .global_sub_pos = -1,
200 .set_of_sub_pos = -1,
201 .file_format = DEMUXER_TYPE_UNKNOWN,
202 .loop_times = -1,
203 #ifdef HAS_DVBIN_SUPPORT
204 .last_dvb_step = 1,
205 #endif
208 static MPContext *mpctx = &mpctx_s;
210 int fixed_vo=0;
212 // benchmark:
213 double video_time_usage=0;
214 double vout_time_usage=0;
215 static double audio_time_usage=0;
216 static int total_time_usage_start=0;
217 static int total_frame_cnt=0;
218 static int drop_frame_cnt=0; // total number of dropped frames
219 int benchmark=0;
221 // options:
222 int auto_quality=0;
223 static int output_quality=0;
225 float playback_speed=1.0;
227 int use_gui=0;
229 #ifdef HAVE_NEW_GUI
230 int enqueue=0;
231 #endif
233 static int list_properties = 0;
235 int osd_level=1;
236 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
237 unsigned int osd_visible;
238 int osd_duration = 1000;
240 int term_osd = 1;
241 static char* term_osd_esc = "\x1b[A\r\x1b[K";
242 static char* playing_msg = NULL;
243 // seek:
244 static double seek_to_sec;
245 static off_t seek_to_byte=0;
246 static off_t step_sec=0;
247 static int loop_seek=0;
249 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
251 // A/V sync:
252 int autosync=0; // 30 might be a good default value.
254 // may be changed by GUI: (FIXME!)
255 float rel_seek_secs=0;
256 int abs_seek_pos=0;
258 // codecs:
259 char **audio_codec_list=NULL; // override audio codec
260 char **video_codec_list=NULL; // override video codec
261 char **audio_fm_list=NULL; // override audio codec family
262 char **video_fm_list=NULL; // override video codec family
264 // demuxer:
265 extern char *demuxer_name; // override demuxer
266 extern char *audio_demuxer_name; // override audio demuxer
267 extern char *sub_demuxer_name; // override sub demuxer
269 // streaming:
270 int audio_id=-1;
271 int video_id=-1;
272 int dvdsub_id=-2;
273 int vobsub_id=-1;
274 char* audio_lang=NULL;
275 char* dvdsub_lang=NULL;
276 static char* spudec_ifo=NULL;
277 char* filename=NULL; //"MI2-Trailer.avi";
278 int forced_subs_only=0;
279 int file_filter=1;
281 // cache2:
282 int stream_cache_size=-1;
283 #ifdef USE_STREAM_CACHE
284 extern int cache_fill_status;
286 float stream_cache_min_percent=20.0;
287 float stream_cache_seek_min_percent=50.0;
288 #else
289 #define cache_fill_status 0
290 #endif
292 // dump:
293 static char *stream_dump_name="stream.dump";
294 int stream_dump_type=0;
296 // A-V sync:
297 static float default_max_pts_correction=-1;//0.01f;
298 static float max_pts_correction=0;//default_max_pts_correction;
299 static float c_total=0;
300 float audio_delay=0;
301 static int ignore_start=0;
303 static int softsleep=0;
305 float force_fps=0;
306 static int force_srate=0;
307 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
308 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
309 static int play_n_frames=-1;
310 static int play_n_frames_mf=-1;
312 // screen info:
313 char** video_driver_list=NULL;
314 char** audio_driver_list=NULL;
316 // sub:
317 char *font_name=NULL;
318 char *sub_font_name=NULL;
319 #ifdef HAVE_FONTCONFIG
320 extern int font_fontconfig;
321 #endif
322 float font_factor=0.75;
323 char **sub_name=NULL;
324 float sub_delay=0;
325 float sub_fps=0;
326 int sub_auto = 1;
327 char *vobsub_name=NULL;
328 /*DSP!!char *dsp=NULL;*/
329 int subcc_enabled=0;
330 int suboverlap_enabled = 1;
332 #ifdef USE_ASS
333 #include "libass/ass.h"
334 #include "libass/ass_mp.h"
335 #endif
337 extern int mp_msg_levels[MSGT_MAX];
338 extern int mp_msg_level_all;
340 char* current_module=NULL; // for debugging
343 // ---
345 #ifdef HAVE_MENU
346 #include "m_struct.h"
347 #include "libmenu/menu.h"
348 extern void vf_menu_pause_update(struct vf_instance_s* vf);
349 extern vf_info_t vf_info_menu;
350 static vf_info_t* libmenu_vfs[] = {
351 &vf_info_menu,
352 NULL
354 static vf_instance_t* vf_menu = NULL;
355 static int use_menu = 0;
356 static char* menu_cfg = NULL;
357 static char* menu_root = "main";
358 #endif
361 #ifdef HAVE_RTC
362 static int nortc = 1;
363 static char* rtc_device;
364 #endif
366 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
367 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
368 short edl_decision = 0; ///< 1 when an EDL operation has been made.
369 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
370 int use_filedir_conf;
372 static unsigned int inited_flags=0;
373 #include "mpcommon.h"
374 #include "command.h"
376 #include "metadata.h"
378 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
380 void *mpctx_get_video_out(MPContext *mpctx)
382 return mpctx->video_out;
385 void *mpctx_get_audio_out(MPContext *mpctx)
387 return mpctx->audio_out;
390 void *mpctx_get_demuxer(MPContext *mpctx)
392 return mpctx->demuxer;
395 void *mpctx_get_playtree_iter(MPContext *mpctx)
397 return mpctx->playtree_iter;
400 void *mpctx_get_mixer(MPContext *mpctx)
402 return &mpctx->mixer;
405 int mpctx_get_global_sub_size(MPContext *mpctx)
407 return mpctx->global_sub_size;
410 int mpctx_get_osd_function(MPContext *mpctx)
412 return mpctx->osd_function;
415 static int is_valid_metadata_type (metadata_t type) {
416 switch (type)
418 /* check for valid video stream */
419 case META_VIDEO_CODEC:
420 case META_VIDEO_BITRATE:
421 case META_VIDEO_RESOLUTION:
423 if (!mpctx->sh_video)
424 return 0;
425 break;
428 /* check for valid audio stream */
429 case META_AUDIO_CODEC:
430 case META_AUDIO_BITRATE:
431 case META_AUDIO_SAMPLES:
433 if (!mpctx->sh_audio)
434 return 0;
435 break;
438 /* check for valid demuxer */
439 case META_INFO_TITLE:
440 case META_INFO_ARTIST:
441 case META_INFO_ALBUM:
442 case META_INFO_YEAR:
443 case META_INFO_COMMENT:
444 case META_INFO_TRACK:
445 case META_INFO_GENRE:
447 if (!mpctx->demuxer)
448 return 0;
449 break;
452 default:
453 break;
456 return 1;
459 static char *get_demuxer_info (char *tag) {
460 char **info = mpctx->demuxer->info;
461 int n;
463 if (!info || !tag)
464 return NULL;
466 for (n = 0; info[2*n] != NULL ; n++)
467 if (!strcmp (info[2*n], tag))
468 break;
470 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
473 char *get_metadata (metadata_t type) {
474 char *meta = NULL;
475 sh_audio_t * const sh_audio = mpctx->sh_audio;
476 sh_video_t * const sh_video = mpctx->sh_video;
478 if (!is_valid_metadata_type (type))
479 return NULL;
481 switch (type)
483 case META_NAME:
485 return strdup (mp_basename2 (filename));
488 case META_VIDEO_CODEC:
490 if (sh_video->format == 0x10000001)
491 meta = strdup ("mpeg1");
492 else if (sh_video->format == 0x10000002)
493 meta = strdup ("mpeg2");
494 else if (sh_video->format == 0x10000004)
495 meta = strdup ("mpeg4");
496 else if (sh_video->format == 0x10000005)
497 meta = strdup ("h264");
498 else if (sh_video->format >= 0x20202020)
500 meta = malloc (8);
501 sprintf (meta, "%.4s", (char *) &sh_video->format);
503 else
505 meta = malloc (8);
506 sprintf (meta, "0x%08X", sh_video->format);
508 return meta;
511 case META_VIDEO_BITRATE:
513 meta = malloc (16);
514 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
515 return meta;
518 case META_VIDEO_RESOLUTION:
520 meta = malloc (16);
521 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
522 return meta;
525 case META_AUDIO_CODEC:
527 if (sh_audio->codec && sh_audio->codec->name)
528 meta = strdup (sh_audio->codec->name);
529 return meta;
532 case META_AUDIO_BITRATE:
534 meta = malloc (16);
535 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
536 return meta;
539 case META_AUDIO_SAMPLES:
541 meta = malloc (16);
542 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
543 return meta;
546 /* check for valid demuxer */
547 case META_INFO_TITLE:
548 return get_demuxer_info ("Title");
550 case META_INFO_ARTIST:
551 return get_demuxer_info ("Artist");
553 case META_INFO_ALBUM:
554 return get_demuxer_info ("Album");
556 case META_INFO_YEAR:
557 return get_demuxer_info ("Year");
559 case META_INFO_COMMENT:
560 return get_demuxer_info ("Comment");
562 case META_INFO_TRACK:
563 return get_demuxer_info ("Track");
565 case META_INFO_GENRE:
566 return get_demuxer_info ("Genre");
568 default:
569 break;
572 return meta;
575 /// step size of mixer changes
576 int volstep = 3;
578 void uninit_player(unsigned int mask){
579 mask=inited_flags&mask;
581 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
583 if(mask&INITED_ACODEC){
584 inited_flags&=~INITED_ACODEC;
585 current_module="uninit_acodec";
586 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
587 #ifdef HAVE_NEW_GUI
588 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
589 #endif
590 mpctx->sh_audio=NULL;
591 mpctx->mixer.afilter = NULL;
594 if(mask&INITED_VCODEC){
595 inited_flags&=~INITED_VCODEC;
596 current_module="uninit_vcodec";
597 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
598 mpctx->sh_video=NULL;
599 #ifdef HAVE_MENU
600 vf_menu=NULL;
601 #endif
604 if(mask&INITED_DEMUXER){
605 inited_flags&=~INITED_DEMUXER;
606 current_module="free_demuxer";
607 if(mpctx->demuxer){
608 mpctx->stream=mpctx->demuxer->stream;
609 free_demuxer(mpctx->demuxer);
611 mpctx->demuxer=NULL;
614 // kill the cache process:
615 if(mask&INITED_STREAM){
616 inited_flags&=~INITED_STREAM;
617 current_module="uninit_stream";
618 if(mpctx->stream) free_stream(mpctx->stream);
619 mpctx->stream=NULL;
622 if(mask&INITED_VO){
623 inited_flags&=~INITED_VO;
624 current_module="uninit_vo";
625 mpctx->video_out->uninit();
626 mpctx->video_out=NULL;
629 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
630 if(mask&INITED_GETCH2){
631 inited_flags&=~INITED_GETCH2;
632 current_module="uninit_getch2";
633 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
634 // restore terminal:
635 getch2_disable();
638 if(mask&INITED_VOBSUB){
639 inited_flags&=~INITED_VOBSUB;
640 current_module="uninit_vobsub";
641 if(vo_vobsub) vobsub_close(vo_vobsub);
642 vo_vobsub=NULL;
645 if (mask&INITED_SPUDEC){
646 inited_flags&=~INITED_SPUDEC;
647 current_module="uninit_spudec";
648 spudec_free(vo_spudec);
649 vo_spudec=NULL;
652 if(mask&INITED_AO){
653 inited_flags&=~INITED_AO;
654 current_module="uninit_ao";
655 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
656 mpctx->audio_out->uninit(mpctx->eof?0:1); mpctx->audio_out=NULL;
659 #ifdef HAVE_NEW_GUI
660 if(mask&INITED_GUI){
661 inited_flags&=~INITED_GUI;
662 current_module="uninit_gui";
663 guiDone();
665 #endif
667 if(mask&INITED_INPUT){
668 inited_flags&=~INITED_INPUT;
669 current_module="uninit_input";
670 mp_input_uninit();
671 #ifdef HAVE_MENU
672 if (use_menu)
673 menu_uninit();
674 #endif
677 current_module=NULL;
680 void exit_player_with_rc(const char* how, int rc){
682 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
683 uninit_player(INITED_ALL);
684 #ifdef HAVE_X11
685 #ifdef HAVE_NEW_GUI
686 if ( !use_gui )
687 #endif
688 vo_uninit(); // Close the X11 connection (if any is open).
689 #endif
691 #ifdef HAVE_FREETYPE
692 current_module="uninit_font";
693 if (vo_font) free_font_desc(vo_font);
694 vo_font = NULL;
695 done_freetype();
696 #endif
697 free_osd_list();
699 #ifdef USE_ASS
700 ass_library_done(ass_library);
701 #endif
703 current_module="exit_player";
705 // free mplayer config
706 if(mconfig)
707 m_config_free(mconfig);
709 if(mpctx->playtree)
710 play_tree_free(mpctx->playtree, 1);
713 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
714 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
715 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
717 exit(rc);
720 void exit_player(const char* how){
721 exit_player_with_rc(how, 1);
724 #ifndef __MINGW32__
725 static void child_sighandler(int x){
726 pid_t pid;
727 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
729 #endif
731 #ifdef CRASH_DEBUG
732 static char *prog_path;
733 static int crash_debug = 0;
734 #endif
736 static void exit_sighandler(int x){
737 static int sig_count=0;
738 #ifdef CRASH_DEBUG
739 if (!crash_debug || x != SIGTRAP)
740 #endif
741 ++sig_count;
742 if(inited_flags==0 && sig_count>1) exit(1);
743 if(sig_count==5)
745 /* We're crashing bad and can't uninit cleanly :(
746 * by popular request, we make one last (dirty)
747 * effort to restore the user's
748 * terminal. */
749 getch2_disable();
750 exit(1);
752 if(sig_count==6) exit(1);
753 if(sig_count>6){
754 // can't stop :(
755 #ifndef __MINGW32__
756 kill(getpid(),SIGKILL);
757 #endif
759 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
760 current_module?current_module:"unknown"
762 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
763 if(sig_count<=1)
764 switch(x){
765 case SIGINT:
766 case SIGQUIT:
767 case SIGTERM:
768 case SIGKILL:
769 break; // killed from keyboard (^C) or killed [-9]
770 case SIGILL:
771 #ifdef RUNTIME_CPUDETECT
772 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
773 #else
774 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
775 #endif
776 case SIGFPE:
777 case SIGSEGV:
778 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
779 default:
780 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
781 #ifdef CRASH_DEBUG
782 if (crash_debug) {
783 int gdb_pid;
784 char spid[20];
785 snprintf(spid, 19, "%i", getpid());
786 spid[19] = 0;
787 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
788 gdb_pid = fork();
789 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
790 if (gdb_pid == 0) { // We are the child
791 getch2_disable(); // allow terminal to work properly with gdb
792 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
793 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
794 } else if (gdb_pid < 0)
795 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
796 else {
797 waitpid(gdb_pid, NULL, 0);
799 if (x == SIGTRAP) return;
801 #endif
803 exit_player(NULL);
806 extern void mp_input_register_options(m_config_t* cfg);
808 #include "cfg-mplayer.h"
810 static void parse_cfgfiles( m_config_t* conf )
812 char *conffile;
813 int conffile_fd;
814 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
815 exit_player(NULL);
816 if ((conffile = get_path("")) == NULL) {
817 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
818 } else {
819 #ifdef __MINGW32__
820 mkdir(conffile);
821 #else
822 mkdir(conffile, 0777);
823 #endif
824 free(conffile);
825 if ((conffile = get_path("config")) == NULL) {
826 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
827 } else {
828 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
829 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
830 write(conffile_fd, default_config, strlen(default_config));
831 close(conffile_fd);
833 if (m_config_parse_config_file(conf, conffile) < 0)
834 exit_player(NULL);
835 free(conffile);
840 void load_per_file_config (m_config_t* conf, const char *const file)
842 char *confpath;
843 char cfg[strlen(file)+10];
844 struct stat st;
845 char *name;
847 sprintf (cfg, "%s.conf", file);
849 if (use_filedir_conf && !stat (cfg, &st))
851 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
852 m_config_parse_config_file (conf, cfg);
853 return;
856 if ((name = strrchr (cfg, '/')) == NULL)
857 name = cfg;
858 else
859 name++;
861 if ((confpath = get_path (name)) != NULL)
863 if (!stat (confpath, &st))
865 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
866 m_config_parse_config_file (conf, confpath);
869 free (confpath);
873 /* When libmpdemux performs a blocking operation (network connection or
874 * cache filling) if the operation fails we use this function to check
875 * if it was interrupted by the user.
876 * The function returns a new value for eof. */
877 static int libmpdemux_was_interrupted(int eof) {
878 mp_cmd_t* cmd;
879 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
880 switch(cmd->id) {
881 case MP_CMD_QUIT:
882 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
883 case MP_CMD_PLAY_TREE_STEP: {
884 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
885 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
886 } break;
887 case MP_CMD_PLAY_TREE_UP_STEP: {
888 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
889 } break;
890 case MP_CMD_PLAY_ALT_SRC_STEP: {
891 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
892 } break;
894 mp_cmd_free(cmd);
896 return eof;
899 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
901 int playtree_add_playlist(play_tree_t* entry)
903 play_tree_add_bpf(entry,filename);
905 #ifdef HAVE_NEW_GUI
906 if (use_gui) {
907 if (entry) {
908 import_playtree_playlist_into_gui(entry, mconfig);
909 play_tree_free_list(entry,1);
911 } else
912 #endif
914 if(!entry) {
915 entry = mpctx->playtree_iter->tree;
916 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
917 return PT_NEXT_ENTRY;
919 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
920 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
921 return PT_NEXT_ENTRY;
924 play_tree_remove(entry,1,1);
925 return PT_NEXT_SRC;
927 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
928 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
929 entry = mpctx->playtree_iter->tree;
930 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
931 return PT_NEXT_ENTRY;
933 play_tree_remove(entry,1,1);
935 return PT_NEXT_SRC;
938 void add_subtitles(char *filename, float fps, int noerr)
940 sub_data *subd;
941 #ifdef USE_ASS
942 ass_track_t *asst = 0;
943 #endif
945 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
946 return;
949 subd = sub_read_file(filename, fps);
950 #ifdef USE_ASS
951 if (ass_enabled)
952 #ifdef USE_ICONV
953 asst = ass_read_file(ass_library, filename, sub_cp);
954 #else
955 asst = ass_read_file(ass_library, filename, 0);
956 #endif
957 if (ass_enabled && subd && !asst)
958 asst = ass_read_subdata(ass_library, subd, fps);
960 if (!asst && !subd)
961 #else
962 if(!subd)
963 #endif
964 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
965 filename_recode(filename));
967 #ifdef USE_ASS
968 if (!asst && !subd) return;
969 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
970 #else
971 if (!subd) return;
972 #endif
973 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
974 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
975 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
976 filename_recode(filename));
977 ++mpctx->set_of_sub_size;
978 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
979 filename_recode(filename));
982 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
983 void update_set_of_subtitles(void)
984 // subdata was changed, set_of_sub... have to be updated.
986 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
987 int i;
988 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
989 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
990 set_of_subtitles[i-1] = set_of_subtitles[i];
991 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
992 --mpctx->set_of_sub_size;
993 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
995 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
996 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
998 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
999 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1000 ++mpctx->set_of_sub_size;
1004 void init_vo_spudec(void) {
1005 if (vo_spudec)
1006 spudec_free(vo_spudec);
1007 inited_flags &= ~INITED_SPUDEC;
1008 vo_spudec = NULL;
1009 if (spudec_ifo) {
1010 unsigned int palette[16], width, height;
1011 current_module="spudec_init_vobsub";
1012 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1013 vo_spudec=spudec_new_scaled(palette, width, height);
1016 #ifdef USE_DVDREAD
1017 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1018 current_module="spudec_init_dvdread";
1019 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1020 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1022 #endif
1024 #ifdef USE_DVDNAV
1025 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1026 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1027 current_module="spudec_init_dvdnav";
1028 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1030 #endif
1032 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1033 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1034 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1035 current_module = "spudec_init_matroska";
1036 vo_spudec =
1037 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1038 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1039 mkv_sh_sub->height);
1040 forced_subs_only = mkv_sh_sub->forced_subs_only;
1043 if (vo_spudec==NULL) {
1044 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1045 unsigned int *palette = NULL;
1046 if (sh && sh->has_palette)
1047 palette = sh->palette;
1048 current_module="spudec_init_normal";
1049 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1050 spudec_set_font_factor(vo_spudec,font_factor);
1053 if (vo_spudec!=NULL)
1054 inited_flags|=INITED_SPUDEC;
1058 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1059 * make it all work is to use the builtin SDL-bootstrap code, which
1060 * will be done automatically by replacing our main() if we include SDL.h.
1062 #if defined(__APPLE__) && defined(HAVE_SDL)
1063 #include <SDL.h>
1064 #endif
1067 * \brief append a formatted string
1068 * \param buf buffer to print into
1069 * \param pos position of terminating 0 in buf
1070 * \param len maximum number of characters in buf, not including terminating 0
1071 * \param format printf format string
1073 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1075 va_list va;
1076 va_start(va, format);
1077 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1078 va_end(va);
1079 if (*pos >= len ) {
1080 buf[len] = 0;
1081 *pos = len;
1086 * \brief append time in the hh:mm:ss.f format
1087 * \param buf buffer to print into
1088 * \param pos position of terminating 0 in buf
1089 * \param len maximum number of characters in buf, not including terminating 0
1090 * \param time time value to convert/append
1092 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1093 long tenths = 10 * time;
1094 int f1 = tenths % 10;
1095 int ss = (tenths / 10) % 60;
1096 int mm = (tenths / 600) % 60;
1097 int hh = tenths / 36000;
1098 if (time <= 0) {
1099 saddf(buf, pos, len, "unknown");
1100 return;
1102 if (hh > 0)
1103 saddf(buf, pos, len, "%2d:", hh);
1104 if (hh > 0 || mm > 0)
1105 saddf(buf, pos, len, "%02d:", mm);
1106 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1110 * \brief print the status line
1111 * \param a_pos audio position
1112 * \param a_v A-V desynchronization
1113 * \param corr amount out A-V synchronization
1115 static void print_status(float a_pos, float a_v, float corr)
1117 sh_video_t * const sh_video = mpctx->sh_video;
1118 int width;
1119 char *line;
1120 unsigned pos = 0;
1121 get_screen_size();
1122 if (screen_width > 0)
1123 width = screen_width;
1124 else
1125 width = 80;
1126 #ifdef WIN32
1127 /* Windows command line is broken (MinGW's rxvt works, but we
1128 * should not depend on that). */
1129 width--;
1130 #endif
1131 line = malloc(width + 1); // one additional char for the terminating null
1133 // Audio time
1134 if (mpctx->sh_audio) {
1135 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1136 if (!sh_video) {
1137 float len = demuxer_get_time_length(mpctx->demuxer);
1138 saddf(line, &pos, width, "(");
1139 sadd_hhmmssf(line, &pos, width, a_pos);
1140 saddf(line, &pos, width, ") of %.1f (", len);
1141 sadd_hhmmssf(line, &pos, width, len);
1142 saddf(line, &pos, width, ") ");
1146 // Video time
1147 if (sh_video)
1148 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1150 // A-V sync
1151 if (mpctx->sh_audio && sh_video)
1152 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1154 // Video stats
1155 if (sh_video)
1156 saddf(line, &pos, width, "%3d/%3d ",
1157 (int)sh_video->num_frames,
1158 (int)sh_video->num_frames_decoded);
1160 // CPU usage
1161 if (sh_video) {
1162 if (sh_video->timer > 0.5)
1163 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1164 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1165 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1166 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1167 else
1168 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1169 } else if (mpctx->sh_audio) {
1170 if (mpctx->delay > 0.5)
1171 saddf(line, &pos, width, "%4.1f%% ",
1172 100.0*audio_time_usage/(double)mpctx->delay);
1173 else
1174 saddf(line, &pos, width, "??,?%% ");
1177 // VO stats
1178 if (sh_video)
1179 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1181 #ifdef USE_STREAM_CACHE
1182 // cache stats
1183 if (stream_cache_size > 0)
1184 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1185 #endif
1187 // other
1188 if (playback_speed != 1)
1189 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1191 // end
1192 if (erase_to_end_of_line) {
1193 line[pos] = 0;
1194 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1195 } else {
1196 memset(&line[pos], ' ', width - pos);
1197 line[width] = 0;
1198 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1200 free(line);
1204 * \brief build a chain of audio filters that converts the input format
1205 * to the ao's format, taking into account the current playback_speed.
1206 * \param sh_audio describes the requested input format of the chain.
1207 * \param ao_data describes the requested output format of the chain.
1209 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1211 int new_srate;
1212 int result;
1213 if (!sh_audio)
1215 #ifdef HAVE_NEW_GUI
1216 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1217 #endif
1218 mpctx->mixer.afilter = NULL;
1219 return 0;
1221 if(af_control_any_rev(sh_audio->afilter,
1222 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1223 &playback_speed)) {
1224 new_srate = sh_audio->samplerate;
1225 } else {
1226 new_srate = sh_audio->samplerate * playback_speed;
1227 if (new_srate != ao_data->samplerate) {
1228 // limits are taken from libaf/af_resample.c
1229 if (new_srate < 8000)
1230 new_srate = 8000;
1231 if (new_srate > 192000)
1232 new_srate = 192000;
1233 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1236 result = init_audio_filters(sh_audio, new_srate,
1237 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1238 mpctx->mixer.afilter = sh_audio->afilter;
1239 #ifdef HAVE_NEW_GUI
1240 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1241 #endif
1242 return result;
1246 typedef struct mp_osd_msg mp_osd_msg_t;
1247 struct mp_osd_msg {
1248 /// Previous message on the stack.
1249 mp_osd_msg_t* prev;
1250 /// Message text.
1251 char msg[64];
1252 int id,level,started;
1253 /// Display duration in ms.
1254 unsigned time;
1257 /// OSD message stack.
1258 static mp_osd_msg_t* osd_msg_stack = NULL;
1261 * \brief Add a message on the OSD message stack
1263 * If a message with the same id is already present in the stack
1264 * it is pulled on top of the stack, otherwise a new message is created.
1268 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1269 mp_osd_msg_t *msg,*last=NULL;
1270 va_list va;
1271 int r;
1273 // look if the id is already in the stack
1274 for(msg = osd_msg_stack ; msg && msg->id != id ;
1275 last = msg, msg = msg->prev);
1276 // not found: alloc it
1277 if(!msg) {
1278 msg = calloc(1,sizeof(mp_osd_msg_t));
1279 msg->prev = osd_msg_stack;
1280 osd_msg_stack = msg;
1281 } else if(last) { // found, but it's not on top of the stack
1282 last->prev = msg->prev;
1283 msg->prev = osd_msg_stack;
1284 osd_msg_stack = msg;
1286 // write the msg
1287 va_start(va,fmt);
1288 r = vsnprintf(msg->msg, 64, fmt, va);
1289 va_end(va);
1290 if(r >= 64) msg->msg[63] = 0;
1291 // set id and time
1292 msg->id = id;
1293 msg->level = level;
1294 msg->time = time;
1299 * \brief Remove a message from the OSD stack
1301 * This function can be used to get rid of a message right away.
1305 void rm_osd_msg(int id) {
1306 mp_osd_msg_t *msg,*last=NULL;
1308 // Search for the msg
1309 for(msg = osd_msg_stack ; msg && msg->id != id ;
1310 last = msg, msg = msg->prev);
1311 if(!msg) return;
1313 // Detach it from the stack and free it
1314 if(last)
1315 last->prev = msg->prev;
1316 else
1317 osd_msg_stack = msg->prev;
1318 free(msg);
1322 * \brief Remove all messages from the OSD stack
1326 static void clear_osd_msgs(void) {
1327 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1328 while(msg) {
1329 prev = msg->prev;
1330 free(msg);
1331 msg = prev;
1333 osd_msg_stack = NULL;
1337 * \brief Get the current message from the OSD stack.
1339 * This function decrements the message timer and destroys the old ones.
1340 * The message that should be displayed is returned (if any).
1344 static mp_osd_msg_t* get_osd_msg(void) {
1345 mp_osd_msg_t *msg,*prev,*last = NULL;
1346 static unsigned last_update = 0;
1347 unsigned now = GetTimerMS();
1348 unsigned diff;
1349 char hidden_dec_done = 0;
1351 if (osd_visible) {
1352 // 36000000 means max timed visibility is 1 hour into the future, if
1353 // the difference is greater assume it's wrapped around from below 0
1354 if (osd_visible - now > 36000000) {
1355 osd_visible = 0;
1356 vo_osd_progbar_type = -1; // disable
1357 vo_osd_changed(OSDTYPE_PROGBAR);
1358 if (mpctx->osd_function != OSD_PAUSE)
1359 mpctx->osd_function = OSD_PLAY;
1363 if(!last_update) last_update = now;
1364 diff = now >= last_update ? now - last_update : 0;
1366 last_update = now;
1368 // Look for the first message in the stack with high enough level.
1369 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1370 prev = msg->prev;
1371 if(msg->level > osd_level && hidden_dec_done) continue;
1372 // The message has a high enough level or it is the first hidden one
1373 // in both cases we decrement the timer or kill it.
1374 if(!msg->started || msg->time > diff) {
1375 if(msg->started) msg->time -= diff;
1376 else msg->started = 1;
1377 // display it
1378 if(msg->level <= osd_level) return msg;
1379 hidden_dec_done = 1;
1380 continue;
1382 // kill the message
1383 free(msg);
1384 if(last) {
1385 last->prev = prev;
1386 msg = last;
1387 } else {
1388 osd_msg_stack = prev;
1389 msg = NULL;
1392 // Nothing found
1393 return NULL;
1397 * \brief Display the OSD bar.
1399 * Display the OSD bar or fall back on a simple message.
1403 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1405 if(osd_level < 1) return;
1407 if(mpctx->sh_video) {
1408 osd_visible = (GetTimerMS() + 1000) | 1;
1409 vo_osd_progbar_type = type;
1410 vo_osd_progbar_value = 256*(val-min)/(max-min);
1411 vo_osd_changed(OSDTYPE_PROGBAR);
1412 return;
1415 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1416 name,ROUND(100*(val-min)/(max-min)));
1421 * \brief Update the OSD message line.
1423 * This function displays the current message on the vo OSD or on the term.
1424 * If the stack is empty and the OSD level is high enough the timer
1425 * is displayed (only on the vo OSD).
1429 static void update_osd_msg(void) {
1430 mp_osd_msg_t *msg;
1431 static char osd_text[64] = "";
1432 static char osd_text_timer[64];
1434 // we need some mem for vo_osd_text
1435 vo_osd_text = (unsigned char*)osd_text;
1437 // Look if we have a msg
1438 if((msg = get_osd_msg())) {
1439 if(strcmp(osd_text,msg->msg)) {
1440 strncpy((char*)osd_text, msg->msg, 63);
1441 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1442 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1444 return;
1447 if(mpctx->sh_video) {
1448 // fallback on the timer
1449 if(osd_level>=2) {
1450 int len = demuxer_get_time_length(mpctx->demuxer);
1451 int percentage = -1;
1452 char percentage_text[10];
1453 int pts = demuxer_get_current_time(mpctx->demuxer);
1455 if (mpctx->osd_show_percentage)
1456 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1458 if (percentage >= 0)
1459 snprintf(percentage_text, 9, " (%d%%)", percentage);
1460 else
1461 percentage_text[0] = 0;
1463 if (osd_level == 3)
1464 snprintf(osd_text_timer, 63,
1465 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1466 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1467 len/3600,(len/60)%60,len%60,percentage_text);
1468 else
1469 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1470 mpctx->osd_function,pts/3600,(pts/60)%60,
1471 pts%60,percentage_text);
1472 } else
1473 osd_text_timer[0]=0;
1475 // always decrement the percentage timer
1476 if(mpctx->osd_show_percentage)
1477 mpctx->osd_show_percentage--;
1479 if(strcmp(osd_text,osd_text_timer)) {
1480 strncpy(osd_text, osd_text_timer, 63);
1481 vo_osd_changed(OSDTYPE_OSD);
1483 return;
1486 // Clear the term osd line
1487 if(term_osd && osd_text[0]) {
1488 osd_text[0] = 0;
1489 printf("%s\n",term_osd_esc);
1493 ///@}
1494 // OSDMsgStack
1497 void reinit_audio_chain(void) {
1498 if(mpctx->sh_audio){
1499 current_module="init_audio_codec";
1500 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1501 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1502 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1503 mpctx->d_audio->id = -2;
1504 return;
1505 } else
1506 inited_flags|=INITED_ACODEC;
1507 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1510 //const ao_info_t *info=audio_out->info;
1511 current_module="af_preinit";
1512 ao_data.samplerate=force_srate;
1513 ao_data.channels=0;
1514 ao_data.format=audio_output_format;
1515 #if 1
1516 // first init to detect best values
1517 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1518 // input:
1519 mpctx->sh_audio->samplerate,
1520 // output:
1521 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1522 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1523 exit_player(MSGTR_Exit_error);
1525 #endif
1526 current_module="ao2_init";
1527 if(!(mpctx->audio_out=init_best_audio_out(audio_driver_list,
1528 0, // plugin flag
1529 ao_data.samplerate,
1530 ao_data.channels,
1531 ao_data.format,0))){
1532 // FAILED:
1533 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1534 uninit_player(INITED_ACODEC); // close codec
1535 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1536 mpctx->d_audio->id = -2;
1537 return;
1538 } else {
1539 // SUCCESS:
1540 inited_flags|=INITED_AO;
1541 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1542 mpctx->audio_out->info->short_name,
1543 ao_data.samplerate, ao_data.channels,
1544 af_fmt2str_short(ao_data.format),
1545 af_fmt2bits(ao_data.format)/8 );
1546 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1547 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1548 if(strlen(mpctx->audio_out->info->comment) > 0)
1549 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1550 // init audio filters:
1551 #if 1
1552 current_module="af_init";
1553 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1554 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1555 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1556 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
1557 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1559 #endif
1561 mpctx->mixer.audio_out = mpctx->audio_out;
1562 mpctx->mixer.volstep = volstep;
1567 ///@}
1568 // Command2Property
1571 // Return pts value corresponding to the end point of audio written to the
1572 // ao so far.
1573 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1575 double buffered_output;
1576 // first calculate the end pts of audio that has been output by decoder
1577 double a_pts = sh_audio->pts;
1578 if (a_pts != MP_NOPTS_VALUE)
1579 // Good, decoder supports new way of calculating audio pts.
1580 // sh_audio->pts is the timestamp of the latest input packet with
1581 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1582 // the amount of bytes the decoder has written after that timestamp.
1583 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1584 else {
1585 // Decoder doesn't support new way of calculating pts (or we're
1586 // being called before it has decoded anything with known timestamp).
1587 // Use the old method of audio pts calculation: take the timestamp
1588 // of last packet with known pts the decoder has read data from,
1589 // and add amount of bytes read after the beginning of that packet
1590 // divided by input bps. This will be inaccurate if the input/output
1591 // ratio is not constant for every audio packet or if it is constant
1592 // but not accurately known in sh_audio->i_bps.
1594 a_pts = d_audio->pts;
1595 // ds_tell_pts returns bytes read after last timestamp from
1596 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1597 // it has read but not decoded
1598 if (sh_audio->i_bps)
1599 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1600 (double)sh_audio->i_bps;
1602 // Now a_pts hopefully holds the pts for end of audio from decoder.
1603 // Substract data in buffers between decoder and audio out.
1605 // Decoded but not filtered
1606 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1608 // Data buffered in audio filters, measured in bytes of "missing" output
1609 buffered_output = af_calc_delay(sh_audio->afilter);
1611 // Data that was ready for ao but was buffered because ao didn't fully
1612 // accept everything to internal buffers yet
1613 buffered_output += sh_audio->a_out_buffer_len;
1615 // Filters divide audio length by playback_speed, so multiply by it
1616 // to get the length in original units without speedup or slowdown
1617 a_pts -= buffered_output * playback_speed / ao_data.bps;
1619 return a_pts;
1622 // Return pts value corresponding to currently playing audio.
1623 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1624 ao_functions_t *audio_out)
1626 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1627 audio_out->get_delay();
1630 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1632 unsigned char *start;
1633 int in_size;
1634 int hit_eof=0;
1635 double pts;
1637 while (1) {
1638 void *decoded_frame;
1639 current_module = "decode video";
1640 // XXX Time used in this call is not counted in any performance
1641 // timer now, OSD is not updated correctly for filter-added frames
1642 if (vf_output_queued_frame(sh_video->vfilter))
1643 break;
1644 current_module = "video_read_frame";
1645 in_size = ds_get_packet_pts(d_video, &start, &pts);
1646 if (in_size < 0) {
1647 // try to extract last frames in case of decoder lag
1648 in_size = 0;
1649 pts = 1e300;
1650 hit_eof = 1;
1652 if (in_size > max_framesize)
1653 max_framesize = in_size;
1654 current_module = "decode video";
1655 decoded_frame = decode_video(sh_video, start, in_size, 0, pts);
1656 if (decoded_frame) {
1657 update_subtitles(sh_video, mpctx->d_sub, 0);
1658 update_teletext(sh_video, mpctx->demuxer, 0);
1659 update_osd_msg();
1660 current_module = "filter video";
1661 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1662 break;
1664 if (hit_eof)
1665 return 0;
1667 return 1;
1670 #ifdef HAVE_RTC
1671 int rtc_fd = -1;
1672 #endif
1674 static float timing_sleep(float time_frame)
1676 #ifdef HAVE_RTC
1677 if (rtc_fd >= 0){
1678 // -------- RTC -----------
1679 current_module="sleep_rtc";
1680 while (time_frame > 0.000) {
1681 unsigned long rtc_ts;
1682 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1683 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1684 time_frame -= GetRelativeTime();
1686 } else
1687 #endif
1689 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1690 // unnecessarily high CPU usage
1691 float margin = softsleep ? 0.011 : 0;
1692 current_module = "sleep_timer";
1693 while (time_frame > margin) {
1694 usec_sleep(1000000 * (time_frame - margin));
1695 time_frame -= GetRelativeTime();
1697 if (softsleep){
1698 current_module = "sleep_soft";
1699 if (time_frame < 0)
1700 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1701 while (time_frame > 0)
1702 time_frame-=GetRelativeTime(); // burn the CPU
1705 return time_frame;
1708 static void adjust_sync_and_print_status(int between_frames, float timing_error)
1710 current_module="av_sync";
1712 if(mpctx->sh_audio){
1713 double a_pts, v_pts;
1715 if (autosync)
1717 * If autosync is enabled, the value for delay must be calculated
1718 * a bit differently. It is set only to the difference between
1719 * the audio and video timers. Any attempt to include the real
1720 * or corrected delay causes the pts_correction code below to
1721 * try to correct for the changes in delay which autosync is
1722 * trying to measure. This keeps the two from competing, but still
1723 * allows the code to correct for PTS drift *only*. (Using a delay
1724 * value here, even a "corrected" one, would be incompatible with
1725 * autosync mode.)
1727 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
1728 else
1729 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
1731 v_pts = mpctx->sh_video->pts;
1734 static int drop_message=0;
1735 double AV_delay = a_pts - audio_delay - v_pts;
1736 double x;
1737 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1738 ++drop_message;
1739 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1741 if (autosync)
1742 x = AV_delay*0.1f;
1743 else
1744 /* Do not correct target time for the next frame if this frame
1745 * was late not because of wrong target time but because the
1746 * target time could not be met */
1747 x = (AV_delay + timing_error * playback_speed) * 0.1f;
1748 if (x < -max_pts_correction)
1749 x = -max_pts_correction;
1750 else if (x> max_pts_correction)
1751 x = max_pts_correction;
1752 if (default_max_pts_correction >= 0)
1753 max_pts_correction = default_max_pts_correction;
1754 else
1755 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1756 if (!between_frames) {
1757 mpctx->delay+=x;
1758 c_total+=x;
1760 if(!quiet)
1761 print_status(a_pts - audio_delay, AV_delay, c_total);
1764 } else {
1765 // No audio:
1767 if (!quiet)
1768 print_status(0, 0, 0);
1772 static int fill_audio_out_buffers(void)
1774 unsigned int t;
1775 double tt;
1776 int playsize;
1777 int playflags=0;
1778 int audio_eof=0;
1779 int bytes_to_write;
1780 sh_audio_t * const sh_audio = mpctx->sh_audio;
1782 current_module="play_audio";
1784 while (1) {
1785 // all the current uses of ao_data.pts seem to be in aos that handle
1786 // sync completely wrong; there should be no need to use ao_data.pts
1787 // in get_space()
1788 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1789 bytes_to_write = mpctx->audio_out->get_space();
1790 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1791 break;
1793 // handle audio-only case:
1794 // this is where mplayer sleeps during audio-only playback
1795 // to avoid 100% CPU use
1796 usec_sleep(10000); // Wait a tick before retry
1799 while (bytes_to_write) {
1800 playsize = bytes_to_write;
1801 if (playsize > MAX_OUTBURST)
1802 playsize = MAX_OUTBURST;
1803 bytes_to_write -= playsize;
1805 // Fill buffer if needed:
1806 current_module="decode_audio";
1807 t = GetTimer();
1808 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1809 if (mpctx->d_audio->eof) {
1810 audio_eof = 1;
1811 if (sh_audio->a_out_buffer_len == 0)
1812 return 0;
1814 t = GetTimer() - t;
1815 tt = t*0.000001f; audio_time_usage+=tt;
1816 if (playsize > sh_audio->a_out_buffer_len) {
1817 playsize = sh_audio->a_out_buffer_len;
1818 if (audio_eof)
1819 playflags |= AOPLAY_FINAL_CHUNK;
1821 if (!playsize)
1822 break;
1824 // play audio:
1825 current_module="play_audio";
1827 // Is this pts value actually useful for the aos that access it?
1828 // They're obviously badly broken in the way they handle av sync;
1829 // would not having access to this make them more broken?
1830 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1831 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1833 if (playsize > 0) {
1834 sh_audio->a_out_buffer_len -= playsize;
1835 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1836 sh_audio->a_out_buffer_len);
1837 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
1839 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1840 // Sanity check to avoid hanging in case current ao doesn't output
1841 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1842 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1843 sh_audio->a_out_buffer_len = 0;
1846 return 1;
1849 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
1851 int frame_time_remaining = 0;
1852 current_module="calc_sleep_time";
1854 *time_frame -= GetRelativeTime(); // reset timer
1856 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1857 float delay = mpctx->audio_out->get_delay();
1858 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
1860 if (autosync) {
1862 * Adjust this raw delay value by calculating the expected
1863 * delay for this frame and generating a new value which is
1864 * weighted between the two. The higher autosync is, the
1865 * closer to the delay value gets to that which "-nosound"
1866 * would have used, and the longer it will take for A/V
1867 * sync to settle at the right value (but it eventually will.)
1868 * This settling time is very short for values below 100.
1870 float predicted = mpctx->delay / playback_speed + *time_frame;
1871 float difference = delay - predicted;
1872 delay = predicted + difference / (float)autosync;
1875 *time_frame = delay - mpctx->delay / playback_speed;
1877 // delay = amount of audio buffered in soundcard/driver
1878 if (delay > 0.25) delay=0.25; else
1879 if (delay < 0.10) delay=0.10;
1880 if (*time_frame > delay*0.6) {
1881 // sleep time too big - may cause audio drops (buffer underrun)
1882 frame_time_remaining = 1;
1883 *time_frame = delay*0.5;
1885 } else {
1886 // If we're lagging more than 200 ms behind the right playback rate,
1887 // don't try to "catch up".
1888 // If benchmark is set always output frames as fast as possible
1889 // without sleeping.
1890 if (*time_frame < -0.2 || benchmark)
1891 *time_frame = 0;
1894 *aq_sleep_time += *time_frame;
1897 //============================== SLEEP: ===================================
1899 // flag 256 means: libvo driver does its timing (dvb card)
1900 if (*time_frame > 0.001 && !(vo_flags&256))
1901 *time_frame = timing_sleep(*time_frame);
1902 return frame_time_remaining;
1905 int reinit_video_chain(void) {
1906 sh_video_t * const sh_video = mpctx->sh_video;
1907 double ar=-1.0;
1908 //================== Init VIDEO (codec & libvo) ==========================
1909 if(!fixed_vo || !(inited_flags&INITED_VO)){
1910 current_module="preinit_libvo";
1912 //shouldn't we set dvideo->id=-2 when we fail?
1913 vo_config_count=0;
1914 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
1915 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
1916 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1917 goto err_out;
1919 inited_flags|=INITED_VO;
1922 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
1923 mpctx->sh_video->stream_aspect = ar;
1924 current_module="init_video_filters";
1926 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
1927 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
1929 #ifdef HAVE_MENU
1930 if(use_menu) {
1931 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
1932 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
1933 if(!vf_menu) {
1934 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
1935 use_menu = 0;
1938 if(vf_menu)
1939 sh_video->vfilter=(void*)vf_menu;
1940 #endif
1942 #ifdef USE_ASS
1943 if(ass_enabled) {
1944 int i;
1945 int insert = 1;
1946 if (vf_settings)
1947 for (i = 0; vf_settings[i].name; ++i)
1948 if (strcmp(vf_settings[i].name, "ass") == 0) {
1949 insert = 0;
1950 break;
1952 if (insert) {
1953 extern vf_info_t vf_info_ass;
1954 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1955 char* vf_arg[] = {"auto", "1", NULL};
1956 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
1957 if (vf_ass)
1958 sh_video->vfilter=(void*)vf_ass;
1959 else
1960 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1963 #endif
1965 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
1967 #ifdef USE_ASS
1968 if (ass_enabled)
1969 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1970 #endif
1972 current_module="init_video_codec";
1974 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1975 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1976 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1978 if(!sh_video->inited){
1979 if(!fixed_vo) uninit_player(INITED_VO);
1980 goto err_out;
1983 inited_flags|=INITED_VCODEC;
1985 if (sh_video->codec)
1986 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
1988 sh_video->last_pts = MP_NOPTS_VALUE;
1989 sh_video->num_buffered_pts = 0;
1990 sh_video->next_frame_time = 0;
1992 if(auto_quality>0){
1993 // Auto quality option enabled
1994 output_quality=get_video_quality_max(sh_video);
1995 if(auto_quality>output_quality) auto_quality=output_quality;
1996 else output_quality=auto_quality;
1997 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
1998 set_video_quality(sh_video,output_quality);
2001 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2003 current_module="init_vo";
2005 return 1;
2007 err_out:
2008 mpctx->sh_video = mpctx->d_video->sh = NULL;
2009 return 0;
2012 static double update_video(int *blit_frame)
2014 sh_video_t * const sh_video = mpctx->sh_video;
2015 //-------------------- Decode a frame: -----------------------
2016 double frame_time;
2017 *blit_frame = 0; // Don't blit if we hit EOF
2018 if (!correct_pts) {
2019 unsigned char* start=NULL;
2020 void *decoded_frame;
2021 int drop_frame=0;
2022 int in_size;
2024 current_module = "video_read_frame";
2025 frame_time = sh_video->next_frame_time;
2026 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2027 &start, force_fps);
2028 if (in_size < 0)
2029 return -1;
2030 if (in_size > max_framesize)
2031 max_framesize = in_size; // stats
2032 sh_video->timer += frame_time;
2033 if (mpctx->sh_audio)
2034 mpctx->delay -= frame_time;
2035 // video_read_frame can change fps (e.g. for ASF video)
2036 vo_fps = sh_video->fps;
2037 // check for frame-drop:
2038 current_module = "check_framedrop";
2039 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2040 static int dropped_frames;
2041 float delay = playback_speed*mpctx->audio_out->get_delay();
2042 float d = delay-mpctx->delay;
2043 // we should avoid dropping too many frames in sequence unless we
2044 // are too late. and we allow 100ms A-V delay here:
2045 if (d < -dropped_frames*frame_time-0.100 &&
2046 mpctx->osd_function != OSD_PAUSE) {
2047 drop_frame = frame_dropping;
2048 ++drop_frame_cnt;
2049 ++dropped_frames;
2050 } else
2051 drop_frame = dropped_frames = 0;
2052 ++total_frame_cnt;
2054 update_subtitles(sh_video, mpctx->d_sub, 0);
2055 update_teletext(sh_video, mpctx->demuxer, 0);
2056 update_osd_msg();
2057 current_module = "decode_video";
2058 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2059 sh_video->pts);
2060 current_module = "filter_video";
2061 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2062 sh_video->pts));
2064 else {
2065 if (!generate_video_frame(sh_video, mpctx->d_video))
2066 return -1;
2067 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2068 VFCTRL_GET_PTS, &sh_video->pts);
2069 if (sh_video->pts == MP_NOPTS_VALUE) {
2070 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2071 sh_video->pts = sh_video->last_pts;
2073 if (sh_video->last_pts == MP_NOPTS_VALUE)
2074 sh_video->last_pts= sh_video->pts;
2075 else if (sh_video->last_pts >= sh_video->pts) {
2076 sh_video->last_pts = sh_video->pts;
2077 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2079 frame_time = sh_video->pts - sh_video->last_pts;
2080 sh_video->last_pts = sh_video->pts;
2081 sh_video->timer += frame_time;
2082 if(mpctx->sh_audio)
2083 mpctx->delay -= frame_time;
2084 *blit_frame = 1;
2086 return frame_time;
2089 void pause_loop(void)
2091 mp_cmd_t* cmd;
2092 if (!quiet) {
2093 // Small hack to display the pause message on the OSD line.
2094 // The pause string is: "\n == PAUSE == \r" so we need to
2095 // take the first and the last char out
2096 if (term_osd && !mpctx->sh_video) {
2097 char msg[128] = MSGTR_Paused;
2098 int mlen = strlen(msg);
2099 msg[mlen-1] = '\0';
2100 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2101 update_osd_msg();
2102 } else
2103 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2104 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2106 #ifdef HAVE_NEW_GUI
2107 if (use_gui)
2108 guiGetEvent(guiCEvent, (char *)guiSetPause);
2109 #endif
2110 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2111 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2113 if (mpctx->audio_out && mpctx->sh_audio)
2114 mpctx->audio_out->pause(); // pause audio, keep data if possible
2116 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL
2117 || cmd->id == MP_CMD_SET_MOUSE_POS) {
2118 if (cmd) {
2119 cmd = mp_input_get_cmd(0,1,0);
2120 mp_cmd_free(cmd);
2121 continue;
2123 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2124 mpctx->video_out->check_events();
2125 #ifdef HAVE_NEW_GUI
2126 if (use_gui) {
2127 guiEventHandling();
2128 guiGetEvent(guiReDraw, NULL);
2129 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2130 break;
2132 #endif
2133 #ifdef HAVE_MENU
2134 if (vf_menu)
2135 vf_menu_pause_update(vf_menu);
2136 #endif
2137 usec_sleep(20000);
2139 if (cmd && cmd->id == MP_CMD_PAUSE) {
2140 cmd = mp_input_get_cmd(0,1,0);
2141 mp_cmd_free(cmd);
2143 mpctx->osd_function=OSD_PLAY;
2144 if (mpctx->audio_out && mpctx->sh_audio)
2145 mpctx->audio_out->resume(); // resume audio
2146 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2147 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2148 (void)GetRelativeTime(); // ignore time that passed during pause
2149 #ifdef HAVE_NEW_GUI
2150 if (use_gui) {
2151 if (guiIntfStruct.Playing == guiSetStop)
2152 mpctx->eof = 1;
2153 else
2154 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2156 #endif
2159 void print_version(void){
2160 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2162 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2163 GetCpuCaps(&gCpuCaps);
2164 #ifdef ARCH_X86
2165 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2166 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2167 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2168 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2169 #ifdef RUNTIME_CPUDETECT
2170 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2171 #else
2172 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2173 #ifdef HAVE_MMX
2174 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2175 #endif
2176 #ifdef HAVE_MMX2
2177 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2178 #endif
2179 #ifdef HAVE_3DNOW
2180 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2181 #endif
2182 #ifdef HAVE_3DNOWEX
2183 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2184 #endif
2185 #ifdef HAVE_SSE
2186 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2187 #endif
2188 #ifdef HAVE_SSE2
2189 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2190 #endif
2191 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2192 #endif /* RUNTIME_CPUDETECT */
2193 #endif /* ARCH_X86 */
2197 // Find the right mute status and record position for new file position
2198 static void edl_seek_reset(MPContext *mpctx)
2200 mpctx->edl_muted = 0;
2201 next_edl_record = edl_records;
2203 while (next_edl_record) {
2204 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2205 break;
2207 if (next_edl_record->action == EDL_MUTE)
2208 mpctx->edl_muted = !mpctx->edl_muted;
2209 next_edl_record = next_edl_record->next;
2211 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2212 mixer_mute(&mpctx->mixer);
2216 // Execute EDL command for the current position if one exists
2217 static void edl_update(MPContext *mpctx)
2219 if (!next_edl_record)
2220 return;
2222 if (!mpctx->sh_video) {
2223 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2224 free_edl(edl_records);
2225 next_edl_record = NULL;
2226 edl_records = NULL;
2227 return;
2230 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2231 if (next_edl_record->action == EDL_SKIP) {
2232 mpctx->osd_function = OSD_FFW;
2233 abs_seek_pos = 0;
2234 rel_seek_secs = next_edl_record->length_sec;
2235 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2236 "[%f], length [%f]\n", next_edl_record->start_sec,
2237 next_edl_record->stop_sec, next_edl_record->length_sec);
2238 edl_decision = 1;
2240 else if (next_edl_record->action == EDL_MUTE) {
2241 mpctx->edl_muted = !mpctx->edl_muted;
2242 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2243 mixer_mute(&mpctx->mixer);
2244 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2245 next_edl_record->start_sec );
2247 next_edl_record = next_edl_record->next;
2252 // style & 1 == 0 means seek relative to current position, == 1 means absolute
2253 // style & 2 == 0 means amount in seconds, == 2 means fraction of file length
2254 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2255 static int seek(MPContext *mpctx, double amount, int style)
2257 current_module = "seek";
2258 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2259 return -1;
2261 if (mpctx->sh_video) {
2262 current_module = "seek_video_reset";
2263 resync_video_stream(mpctx->sh_video);
2264 if (vo_config_count)
2265 mpctx->video_out->control(VOCTRL_RESET, NULL);
2266 mpctx->sh_video->num_buffered_pts = 0;
2267 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2268 mpctx->num_buffered_frames = 0;
2269 mpctx->delay = 0;
2270 // Not all demuxers set d_video->pts during seek, so this value
2271 // (which is used by at least vobsub and edl code below) may
2272 // be completely wrong (probably 0).
2273 mpctx->sh_video->pts = mpctx->d_video->pts;
2274 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2275 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2278 if (mpctx->sh_audio) {
2279 current_module = "seek_audio_reset";
2280 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2281 mpctx->sh_audio->a_buffer_len = 0;
2282 mpctx->sh_audio->a_out_buffer_len = 0;
2285 if (vo_vobsub && mpctx->sh_video) {
2286 current_module = "seek_vobsub_reset";
2287 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2290 edl_seek_reset(mpctx);
2292 c_total = 0;
2293 max_pts_correction = 0.1;
2294 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2295 drop_frame_cnt = 0;
2297 current_module = NULL;
2298 return 0;
2301 int main(int argc,char* argv[]){
2304 char * mem_ptr;
2306 // movie info:
2308 /* Flag indicating whether MPlayer should exit without playing anything. */
2309 int opt_exit = 0;
2311 //float a_frame=0; // Audio
2313 int i;
2315 int gui_no_filename=0;
2317 srand((int) time(NULL));
2319 InitTimer();
2321 mp_msg_init();
2323 for(i=1; i<argc; i++)
2324 if(!strcmp(argv[i], "-really-quiet"))
2325 verbose= -10;
2327 print_version();
2328 #if defined(WIN32) && defined(USE_WIN32DLL)
2329 set_path_env();
2330 #endif /*WIN32 && USE_WIN32DLL*/
2332 #ifdef USE_TV
2333 stream_tv_defaults.immediate = 1;
2334 #endif
2336 if (argc > 1 && argv[1] &&
2337 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2338 use_gui = !strcmp(argv[1], "-gui");
2339 } else
2340 if ( argv[0] )
2342 char *base = strrchr(argv[0], '/');
2343 if (!base)
2344 base = strrchr(argv[0], '\\');
2345 if (!base)
2346 base = argv[0];
2347 if(strstr(base, "gmplayer"))
2348 use_gui=1;
2351 mconfig = m_config_new();
2352 m_config_register_options(mconfig,mplayer_opts);
2353 // TODO : add something to let modules register their options
2354 mp_input_register_options(mconfig);
2355 parse_cfgfiles(mconfig);
2357 #ifdef HAVE_NEW_GUI
2358 if ( use_gui ) cfg_read();
2359 #endif
2361 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2362 if(mpctx->playtree == NULL)
2363 opt_exit = 1;
2364 else {
2365 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2366 if(mpctx->playtree) {
2367 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2368 if(mpctx->playtree_iter) {
2369 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2370 play_tree_iter_free(mpctx->playtree_iter);
2371 mpctx->playtree_iter = NULL;
2373 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2378 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2379 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2380 if(runningmplayer && filename && use_gui){
2381 COPYDATASTRUCT csData;
2382 char file[MAX_PATH];
2383 char *filepart = filename;
2384 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2385 csData.dwData = 0;
2386 csData.cbData = strlen(file)*2;
2387 csData.lpData = file;
2388 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2391 #endif
2393 #ifdef WIN32
2394 if(proc_priority){
2395 int i;
2396 for(i=0; priority_presets_defs[i].name; i++){
2397 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2398 break;
2400 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2401 priority_presets_defs[i].name);
2402 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2404 #endif
2405 #ifndef HAVE_NEW_GUI
2406 if(use_gui){
2407 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2408 use_gui=0;
2410 #else
2411 #ifndef WIN32
2412 if(use_gui && !vo_init()){
2413 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2414 use_gui=0;
2416 #endif
2417 if (use_gui && mpctx->playtree_iter){
2418 char cwd[PATH_MAX+2];
2419 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2420 play_tree_iter_free(mpctx->playtree_iter);
2421 mpctx->playtree_iter=NULL;
2423 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2425 strcat(cwd, "/");
2426 // Prefix relative paths with current working directory
2427 play_tree_add_bpf(mpctx->playtree, cwd);
2429 // Import initital playtree into GUI.
2430 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2432 #endif /* HAVE_NEW_GUI */
2434 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2435 list_video_out();
2436 opt_exit = 1;
2439 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2440 list_audio_out();
2441 opt_exit = 1;
2444 /* Check codecs.conf. */
2445 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2446 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2447 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2448 if(!parse_codec_cfg(NULL)){
2449 exit_player_with_rc(NULL, 0);
2451 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2454 free( mem_ptr ); // release the buffer created by get_path()
2457 #if 0
2458 if(video_codec_list){
2459 int i;
2460 video_codec=video_codec_list[0];
2461 for(i=0;video_codec_list[i];i++)
2462 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2464 #endif
2465 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2466 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2467 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2468 list_codecs(1);
2469 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2470 opt_exit = 1;
2472 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2473 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2474 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2475 list_codecs(0);
2476 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2477 opt_exit = 1;
2479 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2480 vfm_help();
2481 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2482 opt_exit = 1;
2484 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2485 afm_help();
2486 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2487 opt_exit = 1;
2489 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2490 af_help();
2491 printf("\n");
2492 opt_exit = 1;
2494 #ifdef HAVE_X11
2495 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2496 fstype_help();
2497 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2498 opt_exit = 1;
2500 #endif
2501 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2502 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2503 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2504 demuxer_help();
2505 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2506 opt_exit = 1;
2508 if(list_properties) {
2509 property_print_help();
2510 opt_exit = 1;
2513 if(opt_exit)
2514 exit_player(NULL);
2516 if (player_idle_mode && use_gui) {
2517 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2518 exit_player_with_rc(NULL, 1);
2521 if(!filename && !player_idle_mode){
2522 if(!use_gui){
2523 // no file/vcd/dvd -> show HELP:
2524 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2525 exit_player_with_rc(NULL, 0);
2526 } else gui_no_filename=1;
2529 /* Display what configure line was used */
2530 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2532 // Many users forget to include command line in bugreports...
2533 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2534 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2535 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2536 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2539 //------ load global data first ------
2541 // check font
2542 #ifdef HAVE_FREETYPE
2543 init_freetype();
2544 #endif
2545 #ifdef HAVE_FONTCONFIG
2546 if(font_fontconfig <= 0)
2548 #endif
2549 #ifdef HAVE_BITMAP_FONT
2550 if(font_name){
2551 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2552 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2553 filename_recode(font_name));
2554 } else {
2555 // try default:
2556 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2557 free(mem_ptr); // release the buffer created by get_path()
2558 if(!vo_font)
2559 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2561 if (sub_font_name)
2562 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2563 else
2564 sub_font = vo_font;
2565 #endif
2566 #ifdef HAVE_FONTCONFIG
2568 #endif
2570 vo_init_osd();
2572 #ifdef USE_ASS
2573 ass_library = ass_init();
2574 #endif
2576 #ifdef HAVE_RTC
2577 if(!nortc)
2579 // seteuid(0); /* Can't hurt to try to get root here */
2580 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2581 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2582 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2583 else {
2584 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2586 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2587 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2588 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2589 close (rtc_fd);
2590 rtc_fd = -1;
2591 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2592 /* variable only by the root */
2593 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2594 close (rtc_fd);
2595 rtc_fd = -1;
2596 } else
2597 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2600 #ifdef HAVE_NEW_GUI
2601 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2602 // and now ? -- Pontscho
2603 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2604 #endif
2605 if(rtc_fd<0)
2606 #endif /* HAVE_RTC */
2607 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2608 softsleep?"software":timer_name);
2610 #ifdef USE_TERMCAP
2611 if ( !use_gui ) load_termcap(NULL); // load key-codes
2612 #endif
2614 // ========== Init keyboard FIFO (connection to libvo) ============
2616 // Init input system
2617 current_module = "init_input";
2618 mp_input_init(use_gui);
2619 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2620 if(slave_mode)
2621 #ifndef __MINGW32__
2622 mp_input_add_cmd_fd(0,1,NULL,NULL);
2623 #else
2624 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
2625 #endif
2626 else if(!noconsolecontrols)
2627 mp_input_add_event_fd(0, getch2);
2629 #ifdef HAVE_MENU
2630 if(use_menu) {
2631 if(menu_cfg && menu_init(mpctx, menu_cfg))
2632 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2633 else {
2634 menu_cfg = get_path("menu.conf");
2635 if(menu_init(mpctx, menu_cfg))
2636 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2637 else {
2638 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2639 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2640 else {
2641 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2642 use_menu = 0;
2647 #endif
2649 inited_flags|=INITED_INPUT;
2650 current_module = NULL;
2652 /// Catch signals
2653 #ifndef __MINGW32__
2654 signal(SIGCHLD,child_sighandler);
2655 #endif
2657 #ifdef CRASH_DEBUG
2658 prog_path = argv[0];
2659 #endif
2660 //========= Catch terminate signals: ================
2661 // terminate requests:
2662 signal(SIGTERM,exit_sighandler); // kill
2663 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2665 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2667 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2668 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2669 #ifdef ENABLE_SIGHANDLER
2670 // fatal errors:
2671 signal(SIGBUS,exit_sighandler); // bus error
2672 signal(SIGSEGV,exit_sighandler); // segfault
2673 signal(SIGILL,exit_sighandler); // illegal instruction
2674 signal(SIGFPE,exit_sighandler); // floating point exc.
2675 signal(SIGABRT,exit_sighandler); // abort()
2676 #ifdef CRASH_DEBUG
2677 if (crash_debug)
2678 signal(SIGTRAP,exit_sighandler);
2679 #endif
2680 #endif
2682 #ifdef HAVE_NEW_GUI
2683 if(use_gui){
2684 guiInit();
2685 guiGetEvent(guiSetContext, mpctx);
2686 inited_flags|=INITED_GUI;
2687 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2689 #endif
2691 // ******************* Now, let's see the per-file stuff ********************
2693 play_next_file:
2695 // init global sub numbers
2696 mpctx->global_sub_size = 0;
2697 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2699 if (filename) load_per_file_config (mconfig, filename);
2701 // We must enable getch2 here to be able to interrupt network connection
2702 // or cache filling
2703 if(!noconsolecontrols && !slave_mode){
2704 if(inited_flags&INITED_GETCH2)
2705 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2706 else
2707 getch2_enable(); // prepare stdin for hotkeys...
2708 inited_flags|=INITED_GETCH2;
2709 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2712 // =================== GUI idle loop (STOP state) ===========================
2713 #ifdef HAVE_NEW_GUI
2714 if ( use_gui ) {
2715 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2716 guiGetEvent( guiSetDefaults,0 );
2717 while ( guiIntfStruct.Playing != 1 )
2719 mp_cmd_t* cmd;
2720 usec_sleep(20000);
2721 guiEventHandling();
2722 guiGetEvent( guiReDraw,NULL );
2723 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2724 guiGetEvent(guiIEvent, (char *)cmd->id);
2725 mp_cmd_free(cmd);
2728 guiGetEvent( guiSetParameters,NULL );
2729 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2731 play_tree_t * entry = play_tree_new();
2732 play_tree_add_file( entry,guiIntfStruct.Filename );
2733 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2734 else mpctx->playtree=play_tree_new();
2735 play_tree_set_child( mpctx->playtree,entry );
2736 if(mpctx->playtree)
2738 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2739 if(mpctx->playtree_iter)
2741 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2743 play_tree_iter_free(mpctx->playtree_iter);
2744 mpctx->playtree_iter = NULL;
2746 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2751 #endif /* HAVE_NEW_GUI */
2753 while (player_idle_mode && !filename) {
2754 play_tree_t * entry = NULL;
2755 mp_cmd_t * cmd;
2756 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
2757 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
2758 usec_sleep(20000);
2760 switch (cmd->id) {
2761 case MP_CMD_LOADFILE:
2762 // prepare a tree entry with the new filename
2763 entry = play_tree_new();
2764 play_tree_add_file(entry, cmd->args[0].v.s);
2765 // The entry is added to the main playtree after the switch().
2766 break;
2767 case MP_CMD_LOADLIST:
2768 entry = parse_playlist_file(cmd->args[0].v.s);
2769 break;
2770 case MP_CMD_QUIT:
2771 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2772 break;
2775 mp_cmd_free(cmd);
2777 if (entry) { // user entered a command that gave a valid entry
2778 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
2779 play_tree_free_list(mpctx->playtree->child, 1);
2780 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
2782 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
2784 play_tree_set_child(mpctx->playtree, entry);
2786 /* Make iterator start at the top the of tree. */
2787 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
2788 if (!mpctx->playtree_iter) continue;
2790 // find the first real item in the tree
2791 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2792 // no items!
2793 play_tree_iter_free(mpctx->playtree_iter);
2794 mpctx->playtree_iter = NULL;
2795 continue; // wait for next command
2797 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
2800 //---------------------------------------------------------------------------
2802 if(filename)
2803 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
2804 filename_recode(filename));
2806 if (edl_filename) {
2807 if (edl_records) free_edl(edl_records);
2808 next_edl_record = edl_records = edl_parse_file();
2810 if (edl_output_filename) {
2811 if (edl_fd) fclose(edl_fd);
2812 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
2814 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
2815 filename_recode(edl_output_filename));
2819 //==================== Open VOB-Sub ============================
2821 current_module="vobsub";
2822 if (vobsub_name){
2823 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
2824 if(vo_vobsub==NULL)
2825 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
2826 filename_recode(vobsub_name));
2827 }else if(sub_auto && filename && (strlen(filename)>=5)){
2828 /* try to autodetect vobsub from movie filename ::atmos */
2829 char *buf = malloc((strlen(filename)-3)),*psub;
2830 memset(buf,0,strlen(filename)-3); // make sure string is terminated
2831 strncpy(buf, filename, strlen(filename)-4);
2832 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
2833 /* try from ~/.mplayer/sub */
2834 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
2835 char *bname;
2836 int l;
2837 bname = strrchr(buf,'/');
2838 #ifdef WIN32
2839 if(!bname) bname = strrchr(buf,'\\');
2840 #endif
2841 if(bname) bname++;
2842 else bname = buf;
2843 l = strlen(psub) + strlen(bname) + 1;
2844 psub = realloc(psub,l);
2845 strcat(psub,bname);
2846 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
2847 free(psub);
2849 free(buf);
2851 if(vo_vobsub){
2852 inited_flags|=INITED_VOBSUB;
2853 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
2854 // check if vobsub requested only to display forced subtitles
2855 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
2857 // setup global sub numbering
2858 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
2859 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
2862 //============ Open & Sync STREAM --- fork cache2 ====================
2864 mpctx->stream=NULL;
2865 mpctx->demuxer=NULL;
2866 if (mpctx->d_audio) {
2867 //free_demuxer_stream(mpctx->d_audio);
2868 mpctx->d_audio=NULL;
2870 if (mpctx->d_video) {
2871 //free_demuxer_stream(d_video);
2872 mpctx->d_video=NULL;
2874 mpctx->sh_audio=NULL;
2875 mpctx->sh_video=NULL;
2877 current_module="open_stream";
2878 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
2879 if(!mpctx->stream) { // error...
2880 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
2881 goto goto_next_file;
2883 inited_flags|=INITED_STREAM;
2885 #ifdef HAVE_NEW_GUI
2886 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
2887 #endif
2889 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
2890 play_tree_t* entry;
2891 // Handle playlist
2892 current_module="handle_playlist";
2893 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
2894 filename_recode(filename));
2895 entry = parse_playtree(mpctx->stream,0);
2896 mpctx->eof=playtree_add_playlist(entry);
2897 goto goto_next_file;
2899 mpctx->stream->start_pos+=seek_to_byte;
2901 if(stream_dump_type==5){
2902 unsigned char buf[4096];
2903 int len;
2904 FILE *f;
2905 current_module="dumpstream";
2906 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
2907 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
2908 exit_player(MSGTR_Exit_error);
2910 stream_reset(mpctx->stream);
2911 stream_seek(mpctx->stream,mpctx->stream->start_pos);
2912 f=fopen(stream_dump_name,"wb");
2913 if(!f){
2914 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
2915 exit_player(MSGTR_Exit_error);
2917 while(!mpctx->stream->eof){
2918 len=stream_read(mpctx->stream,buf,4096);
2919 if(len>0) {
2920 if(fwrite(buf,len,1,f) != 1) {
2921 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2922 exit_player(MSGTR_Exit_error);
2926 if(fclose(f)) {
2927 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2928 exit_player(MSGTR_Exit_error);
2930 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
2931 exit_player_with_rc(MSGTR_Exit_eof, 0);
2934 #ifdef USE_DVDREAD
2935 if(mpctx->stream->type==STREAMTYPE_DVD){
2936 current_module="dvd lang->id";
2937 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
2938 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2939 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
2940 // setup global sub numbering
2941 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2942 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
2943 current_module=NULL;
2945 #endif
2947 #ifdef USE_DVDNAV
2948 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
2949 current_module="dvdnav lang->id";
2950 if(audio_id==-1) audio_id=dvdnav_aid_from_lang(mpctx->stream,audio_lang);
2951 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2952 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
2953 // setup global sub numbering
2954 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2955 mpctx->global_sub_size += dvdnav_number_of_subs(mpctx->stream);
2956 current_module=NULL;
2958 #endif
2960 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
2961 goto_enable_cache:
2962 if(stream_cache_size>0){
2963 current_module="enable_cache";
2964 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
2965 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
2966 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
2967 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
2970 //============ Open DEMUXERS --- DETECT file type =======================
2971 current_module="demux_open";
2973 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
2975 // HACK to get MOV Reference Files working
2977 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
2979 unsigned char* playlist_entry;
2980 play_tree_t *list = NULL, *entry = NULL;
2982 current_module="handle_demux_playlist";
2983 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
2985 char *temp, *bname;
2987 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
2988 filename_recode(playlist_entry));
2990 bname=mp_basename(playlist_entry);
2991 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
2992 continue;
2994 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
2995 continue;
2997 entry = play_tree_new();
2999 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3001 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
3002 if (temp)
3004 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
3005 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
3006 strcat(temp, playlist_entry);
3007 play_tree_add_file(entry,temp);
3008 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3009 free(temp);
3012 else
3013 play_tree_add_file(entry,playlist_entry);
3015 if(!list)
3016 list = entry;
3017 else
3018 play_tree_append_entry(list,entry);
3020 free_demuxer(mpctx->demuxer);
3021 mpctx->demuxer = NULL;
3023 if (list)
3025 entry = play_tree_new();
3026 play_tree_set_child(entry,list);
3027 mpctx->eof=playtree_add_playlist(entry);
3028 goto goto_next_file;
3032 if(!mpctx->demuxer)
3033 goto goto_next_file;
3034 inited_flags|=INITED_DEMUXER;
3036 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3037 int i;
3038 int maxid = -1;
3039 // setup global sub numbering
3040 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3041 for (i = 0; i < MAX_S_STREAMS; i++)
3042 if (mpctx->demuxer->s_streams[i])
3043 maxid = FFMAX(maxid, ((sh_sub_t *)mpctx->demuxer->s_streams[i])->sid);
3044 mpctx->global_sub_size += maxid + 1;
3046 // Make dvdsub_id always selectable if set.
3047 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id)
3048 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id + 1;
3050 current_module="demux_open2";
3052 //file_format=demuxer->file_format;
3054 mpctx->d_audio=mpctx->demuxer->audio;
3055 mpctx->d_video=mpctx->demuxer->video;
3056 mpctx->d_sub=mpctx->demuxer->sub;
3058 // DUMP STREAMS:
3059 if((stream_dump_type)&&(stream_dump_type<4)){
3060 FILE *f;
3061 demux_stream_t *ds=NULL;
3062 current_module="dump";
3063 // select stream to dump
3064 switch(stream_dump_type){
3065 case 1: ds=mpctx->d_audio;break;
3066 case 2: ds=mpctx->d_video;break;
3067 case 3: ds=mpctx->d_sub;break;
3069 if(!ds){
3070 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3071 exit_player(MSGTR_Exit_error);
3073 // disable other streams:
3074 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3075 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3076 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3077 // let's dump it!
3078 f=fopen(stream_dump_name,"wb");
3079 if(!f){
3080 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3081 exit_player(MSGTR_Exit_error);
3083 while(!ds->eof){
3084 unsigned char* start;
3085 int in_size=ds_get_packet(ds,&start);
3086 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3087 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3088 if(in_size>0) fwrite(start,in_size,1,f);
3090 fclose(f);
3091 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3092 exit_player_with_rc(MSGTR_Exit_eof, 0);
3095 mpctx->sh_audio=mpctx->d_audio->sh;
3096 mpctx->sh_video=mpctx->d_video->sh;
3098 if(mpctx->sh_video){
3100 current_module="video_read_properties";
3101 if(!video_read_properties(mpctx->sh_video)) {
3102 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3103 mpctx->sh_video=mpctx->d_video->sh=NULL;
3104 } else {
3105 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
3106 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3107 mpctx->sh_video->fps,mpctx->sh_video->frametime
3110 /* need to set fps here for output encoders to pick it up in their init */
3111 if(force_fps){
3112 mpctx->sh_video->fps=force_fps;
3113 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3115 vo_fps = mpctx->sh_video->fps;
3117 if(!mpctx->sh_video->fps && !force_fps){
3118 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3119 mpctx->sh_video=mpctx->d_video->sh=NULL;
3125 if(!mpctx->sh_video && !mpctx->sh_audio){
3126 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3127 #ifdef HAS_DVBIN_SUPPORT
3128 if(mpctx->stream->type == STREAMTYPE_DVB)
3130 int dir;
3131 int v = mpctx->last_dvb_step;
3132 if(v > 0)
3133 dir = DVB_CHANNEL_HIGHER;
3134 else
3135 dir = DVB_CHANNEL_LOWER;
3137 if(dvb_step_channel(mpctx->stream, dir))
3138 mpctx->eof = mpctx->dvbin_reopen = 1;
3140 #endif
3141 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3144 /* display clip info */
3145 demux_info_print(mpctx->demuxer);
3147 //================== Read SUBTITLES (DVD & TEXT) ==========================
3148 if(vo_spudec==NULL && mpctx->sh_video &&
3149 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3150 init_vo_spudec();
3153 // Apply current settings for forced subs
3154 if (vo_spudec!=NULL)
3155 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3157 if(mpctx->sh_video) {
3158 // after reading video params we should load subtitles because
3159 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3160 // check .sub
3161 current_module="read_subtitles_file";
3162 if(sub_name){
3163 for (i = 0; sub_name[i] != NULL; ++i)
3164 add_subtitles (sub_name[i], mpctx->sh_video->fps, 0);
3166 if(sub_auto) { // auto load sub file ...
3167 char *psub = get_path( "sub/" );
3168 char **tmp = sub_filenames((psub ? psub : ""), filename);
3169 int i = 0;
3170 free(psub); // release the buffer created by get_path() above
3171 while (tmp[i]) {
3172 add_subtitles (tmp[i], mpctx->sh_video->fps, 1);
3173 free(tmp[i++]);
3175 free(tmp);
3177 if (mpctx->set_of_sub_size > 0) {
3178 // setup global sub numbering
3179 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3180 mpctx->global_sub_size += mpctx->set_of_sub_size;
3184 if (mpctx->global_sub_size) {
3185 // find the best sub to use
3186 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3187 if (vobsub_index_id >= 0) {
3188 // if user asks for a vobsub id, use that first.
3189 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3190 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3191 // if user asks for a dvd sub id, use that next.
3192 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
3193 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3194 // if there are text subs to use, use those. (autosubs come last here)
3195 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3197 } else if (mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3198 // if nothing else works, get subs from the demuxer.
3199 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX];
3201 } else {
3202 // nothing worth doing automatically.
3203 mpctx->global_sub_pos = -1;
3205 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3206 mpctx->global_sub_pos--;
3207 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3208 if(subdata)
3209 switch (stream_dump_type) {
3210 case 3: list_sub_file(subdata); break;
3211 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3212 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3213 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3214 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3215 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3219 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3220 filename_recode(filename));
3221 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3222 if (mpctx->sh_video) {
3223 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3224 if (mpctx->sh_video->format >= 0x20202020)
3225 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3226 else
3227 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3228 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3229 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3230 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3231 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3232 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3234 if (mpctx->sh_audio) {
3235 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3236 if (mpctx->sh_audio->format >= 0x20202020)
3237 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3238 else
3239 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3240 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3241 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3242 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3244 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3246 if(!mpctx->sh_video) goto main; // audio-only
3248 if(!reinit_video_chain()) {
3249 if(!mpctx->sh_video){
3250 if(!mpctx->sh_audio) goto goto_next_file;
3251 goto main; // exit_player(MSGTR_Exit_error);
3255 if(vo_flags & 0x08 && vo_spudec)
3256 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3258 #ifdef HAVE_FREETYPE
3259 force_load_font = 1;
3260 #endif
3262 //================== MAIN: ==========================
3263 main:
3264 current_module="main";
3266 if(playing_msg) {
3267 char* msg = property_expand_string(mpctx, playing_msg);
3268 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3269 free(msg);
3273 // Disable the term OSD in verbose mode
3274 if(verbose) term_osd = 0;
3277 //int frame_corr_num=0; //
3278 //float v_frame=0; // Video
3279 float time_frame=0; // Timer
3280 //float num_frames=0; // number of frames played
3282 int frame_time_remaining=0; // flag
3283 int blit_frame=0;
3284 mpctx->num_buffered_frames=0;
3286 // Make sure old OSD does not stay around,
3287 // e.g. with -fixed-vo and same-resolution files
3288 clear_osd_msgs();
3289 update_osd_msg();
3291 //================ SETUP AUDIO ==========================
3293 if(mpctx->sh_audio){
3294 reinit_audio_chain();
3295 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3296 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3299 current_module="av_init";
3301 if(mpctx->sh_video){
3302 mpctx->sh_video->timer=0;
3303 if (! ignore_start)
3304 audio_delay += mpctx->sh_video->stream_delay;
3306 if(mpctx->sh_audio){
3307 if (! ignore_start)
3308 audio_delay -= mpctx->sh_audio->stream_delay;
3309 mpctx->delay=-audio_delay;
3312 if(!mpctx->sh_audio){
3313 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3314 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3315 ds_free_packs(mpctx->d_audio); // free buffered chunks
3316 //mpctx->d_audio->id=-2; // do not read audio chunks
3317 //uninit_player(INITED_AO); // close device
3319 if(!mpctx->sh_video){
3320 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3321 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3322 ds_free_packs(mpctx->d_video);
3323 mpctx->d_video->id=-2;
3324 //if(!fixed_vo) uninit_player(INITED_VO);
3327 if (!mpctx->sh_video && !mpctx->sh_audio)
3328 goto goto_next_file;
3330 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3331 if(force_fps && mpctx->sh_video){
3332 vo_fps = mpctx->sh_video->fps=force_fps;
3333 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3334 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3337 #ifdef HAVE_NEW_GUI
3338 if ( use_gui ) {
3339 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3340 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3341 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3342 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3343 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3345 #endif
3347 mp_input_set_section(NULL);
3348 //TODO: add desired (stream-based) sections here
3349 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3351 //==================== START PLAYING =======================
3353 if(mpctx->loop_times>1) mpctx->loop_times--; else
3354 if(mpctx->loop_times==1) mpctx->loop_times = -1;
3356 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3358 total_time_usage_start=GetTimer();
3359 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3360 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3361 play_n_frames=play_n_frames_mf;
3363 if(play_n_frames==0){
3364 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3367 if (seek_to_sec) {
3368 seek(mpctx, seek_to_sec, 1);
3369 end_at.pos += seek_to_sec;
3372 if (end_at.type == END_AT_SIZE) {
3373 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3374 end_at.type = END_AT_NONE;
3378 while(!mpctx->eof){
3379 float aq_sleep_time=0;
3380 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3381 mpctx->sh_audio = mpctx->d_audio->sh;
3382 mpctx->sh_audio->ds = mpctx->d_audio;
3383 reinit_audio_chain();
3386 /*========================== PLAY AUDIO ============================*/
3388 if (mpctx->sh_audio)
3389 if (!fill_audio_out_buffers())
3390 // at eof, all audio at least written to ao
3391 if (!mpctx->sh_video)
3392 mpctx->eof = PT_NEXT_ENTRY;
3395 if(!mpctx->sh_video) {
3396 // handle audio-only case:
3397 double a_pos=0;
3398 if(!quiet || end_at.type == END_AT_TIME )
3399 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3401 if(!quiet)
3402 print_status(a_pos, 0, 0);
3404 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3405 mpctx->eof = PT_NEXT_ENTRY;
3406 update_osd_msg();
3408 } else {
3410 /*========================== PLAY VIDEO ============================*/
3412 vo_pts=mpctx->sh_video->timer*90000.0;
3413 vo_fps=mpctx->sh_video->fps;
3415 if (!mpctx->num_buffered_frames) {
3416 double frame_time = update_video(&blit_frame);
3417 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3418 if (mpctx->sh_video->vf_inited < 0) {
3419 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3420 mpctx->eof = 1; goto goto_next_file;
3422 if (frame_time < 0)
3423 mpctx->eof = 1;
3424 else {
3425 // might return with !eof && !blit_frame if !correct_pts
3426 mpctx->num_buffered_frames += blit_frame;
3427 time_frame += frame_time / playback_speed; // for nosound
3431 // ==========================================================================
3433 // current_module="draw_osd";
3434 // if(vo_config_count) mpctx->video_out->draw_osd();
3436 #ifdef HAVE_NEW_GUI
3437 if(use_gui) guiEventHandling();
3438 #endif
3440 current_module="vo_check_events";
3441 if (vo_config_count) mpctx->video_out->check_events();
3443 #ifdef HAVE_X11
3444 if (stop_xscreensaver) {
3445 current_module = "stop_xscreensaver";
3446 xscreensaver_heartbeat();
3448 #endif
3450 frame_time_remaining = sleep_until_update(&time_frame, &aq_sleep_time);
3452 //====================== FLIP PAGE (VIDEO BLT): =========================
3454 current_module="flip_page";
3455 if (!frame_time_remaining && blit_frame) {
3456 unsigned int t2=GetTimer();
3458 if(vo_config_count) mpctx->video_out->flip_page();
3459 mpctx->num_buffered_frames--;
3461 vout_time_usage += (GetTimer() - t2) * 0.000001;
3463 //====================== A-V TIMESTAMP CORRECTION: =========================
3465 adjust_sync_and_print_status(frame_time_remaining, time_frame);
3467 //============================ Auto QUALITY ============================
3469 /*Output quality adjustments:*/
3470 if(auto_quality>0){
3471 current_module="autoq";
3472 // float total=0.000001f * (GetTimer()-aq_total_time);
3473 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3474 if(output_quality<auto_quality && aq_sleep_time>0)
3475 ++output_quality;
3476 else
3477 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3478 if(output_quality>1 && aq_sleep_time<0)
3479 --output_quality;
3480 else
3481 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3482 output_quality=0;
3483 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3484 set_video_quality(mpctx->sh_video,output_quality);
3487 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3488 --play_n_frames;
3489 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3493 // FIXME: add size based support for -endpos
3494 if (end_at.type == END_AT_TIME &&
3495 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3496 mpctx->eof = PT_NEXT_ENTRY;
3498 } // end if(mpctx->sh_video)
3500 #ifdef USE_DVDNAV
3501 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3502 nav_highlight_t hl;
3503 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3504 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3505 vo_osd_changed (OSDTYPE_DVDNAV);
3507 #endif
3509 //============================ Handle PAUSE ===============================
3511 current_module="pause";
3513 if (mpctx->osd_function == OSD_PAUSE) {
3514 pause_loop();
3515 mpctx->was_paused = 1;
3518 // handle -sstep
3519 if(step_sec>0) {
3520 mpctx->osd_function=OSD_FFW;
3521 rel_seek_secs+=step_sec;
3524 edl_update(mpctx);
3526 //================= Keyboard events, SEEKing ====================
3528 current_module="key_events";
3531 mp_cmd_t* cmd;
3532 int brk_cmd = 0;
3533 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3534 brk_cmd = run_command(mpctx, cmd);
3535 mp_cmd_free(cmd);
3536 if (brk_cmd == 2)
3537 goto goto_enable_cache;
3540 mpctx->was_paused = 0;
3542 /* Looping. */
3543 if(mpctx->eof==1 && mpctx->loop_times>=0) {
3544 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", mpctx->loop_times,mpctx->eof);
3546 if(mpctx->loop_times>1) mpctx->loop_times--; else
3547 if(mpctx->loop_times==1) mpctx->loop_times=-1;
3548 play_n_frames=play_n_frames_mf;
3549 mpctx->eof=0;
3550 abs_seek_pos=1; rel_seek_secs=seek_to_sec;
3551 loop_seek = 1;
3554 if(rel_seek_secs || abs_seek_pos){
3555 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3556 // Set OSD:
3557 if(!loop_seek){
3558 if( !edl_decision )
3559 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3563 rel_seek_secs=0;
3564 abs_seek_pos=0;
3565 loop_seek=0;
3566 edl_decision = 0;
3569 #ifdef HAVE_NEW_GUI
3570 if(use_gui){
3571 guiEventHandling();
3572 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3573 // get pos from frame number / total frames
3574 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3575 } else {
3576 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3578 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3579 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3580 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3581 guiGetEvent( guiReDraw,NULL );
3582 guiGetEvent( guiSetVolume,NULL );
3583 if(guiIntfStruct.Playing==0) break; // STOP
3584 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3585 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3586 #ifdef USE_DVDREAD
3587 if ( mpctx->stream->type == STREAMTYPE_DVD )
3589 dvd_priv_t * dvdp = mpctx->stream->priv;
3590 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3592 #endif
3594 #endif /* HAVE_NEW_GUI */
3596 } // while(!mpctx->eof)
3598 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3600 #ifdef HAS_DVBIN_SUPPORT
3601 if(mpctx->dvbin_reopen)
3603 mpctx->eof = 0;
3604 uninit_player(INITED_ALL-(INITED_GUI|INITED_STREAM|INITED_INPUT|INITED_GETCH2|(fixed_vo?INITED_VO:0)));
3605 cache_uninit(mpctx->stream);
3606 mpctx->dvbin_reopen = 0;
3607 goto goto_enable_cache;
3609 #endif
3612 goto_next_file: // don't jump here after ao/vo/getch initialization!
3614 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3616 if(benchmark){
3617 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3618 double total_time_usage;
3619 total_time_usage_start=GetTimer()-total_time_usage_start;
3620 total_time_usage = (float)total_time_usage_start*0.000001;
3621 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3622 video_time_usage,vout_time_usage,audio_time_usage,
3623 total_time_usage-tot,total_time_usage);
3624 if(total_time_usage>0.0)
3625 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3626 100.0*video_time_usage/total_time_usage,
3627 100.0*vout_time_usage/total_time_usage,
3628 100.0*audio_time_usage/total_time_usage,
3629 100.0*(total_time_usage-tot)/total_time_usage,
3630 100.0);
3631 if(total_frame_cnt && frame_dropping)
3632 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3633 total_frame_cnt-drop_frame_cnt,
3634 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3635 drop_frame_cnt,
3636 100*drop_frame_cnt/total_frame_cnt,
3637 total_frame_cnt,
3638 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3642 // time to uninit all, except global stuff:
3643 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
3645 if ( mpctx->set_of_sub_size > 0 )
3647 current_module="sub_free";
3648 for (i = 0; i < mpctx->set_of_sub_size; ++i) {
3649 sub_free( mpctx->set_of_subtitles[i] );
3650 #ifdef USE_ASS
3651 if ( mpctx->set_of_ass_tracks[i] )
3652 ass_free_track( mpctx->set_of_ass_tracks[i] );
3653 #endif
3655 mpctx->set_of_sub_size = 0;
3657 vo_sub_last = vo_sub=NULL;
3658 subdata=NULL;
3659 #ifdef USE_ASS
3660 ass_track = NULL;
3661 #endif
3663 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
3664 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
3665 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
3666 mpctx->eof = 1;
3667 } else {
3668 play_tree_iter_free(mpctx->playtree_iter);
3669 mpctx->playtree_iter = NULL;
3671 mpctx->play_tree_step = 1;
3672 } else if (mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
3673 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
3674 if ( mpctx->playtree_iter ) {
3675 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
3676 mpctx->eof = 1;
3677 } else {
3678 play_tree_iter_free(mpctx->playtree_iter);
3679 mpctx->playtree_iter = NULL;
3682 } else { // NEXT PREV SRC
3683 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
3686 if(mpctx->eof == 0) mpctx->eof = 1;
3688 while(mpctx->playtree_iter != NULL) {
3689 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
3690 if(filename == NULL) {
3691 if( play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
3692 play_tree_iter_free(mpctx->playtree_iter);
3693 mpctx->playtree_iter = NULL;
3695 } else
3696 break;
3699 #ifdef HAVE_NEW_GUI
3700 if( use_gui && !mpctx->playtree_iter )
3702 #ifdef USE_DVDREAD
3703 if ( !guiIntfStruct.DiskChanged )
3704 #endif
3705 mplEnd();
3707 #endif
3709 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
3710 if (!mpctx->playtree_iter) filename = NULL;
3711 mpctx->eof = 0;
3712 goto play_next_file;
3716 exit_player_with_rc(MSGTR_Exit_eof, 0);
3718 return 1;