Use recorded last path only when stat it ok.
[mplayer/greg.git] / mplayer.c
blob6ad430b0d575a3535f19b9474fa1087f47439029
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_playtree_iter(MPContext *mpctx)
392 return mpctx->playtree_iter;
395 void *mpctx_get_mixer(MPContext *mpctx)
397 return &mpctx->mixer;
400 int mpctx_get_global_sub_size(MPContext *mpctx)
402 return mpctx->global_sub_size;
405 int mpctx_get_osd_function(MPContext *mpctx)
407 return mpctx->osd_function;
410 static int is_valid_metadata_type (metadata_t type) {
411 switch (type)
413 /* check for valid video stream */
414 case META_VIDEO_CODEC:
415 case META_VIDEO_BITRATE:
416 case META_VIDEO_RESOLUTION:
418 if (!mpctx->sh_video)
419 return 0;
420 break;
423 /* check for valid audio stream */
424 case META_AUDIO_CODEC:
425 case META_AUDIO_BITRATE:
426 case META_AUDIO_SAMPLES:
428 if (!mpctx->sh_audio)
429 return 0;
430 break;
433 /* check for valid demuxer */
434 case META_INFO_TITLE:
435 case META_INFO_ARTIST:
436 case META_INFO_ALBUM:
437 case META_INFO_YEAR:
438 case META_INFO_COMMENT:
439 case META_INFO_TRACK:
440 case META_INFO_GENRE:
442 if (!mpctx->demuxer)
443 return 0;
444 break;
447 default:
448 break;
451 return 1;
454 static char *get_demuxer_info (char *tag) {
455 char **info = mpctx->demuxer->info;
456 int n;
458 if (!info || !tag)
459 return NULL;
461 for (n = 0; info[2*n] != NULL ; n++)
462 if (!strcmp (info[2*n], tag))
463 break;
465 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
468 char *get_metadata (metadata_t type) {
469 char *meta = NULL;
470 sh_audio_t * const sh_audio = mpctx->sh_audio;
471 sh_video_t * const sh_video = mpctx->sh_video;
473 if (!is_valid_metadata_type (type))
474 return NULL;
476 switch (type)
478 case META_NAME:
480 return strdup (mp_basename2 (filename));
483 case META_VIDEO_CODEC:
485 if (sh_video->format == 0x10000001)
486 meta = strdup ("mpeg1");
487 else if (sh_video->format == 0x10000002)
488 meta = strdup ("mpeg2");
489 else if (sh_video->format == 0x10000004)
490 meta = strdup ("mpeg4");
491 else if (sh_video->format == 0x10000005)
492 meta = strdup ("h264");
493 else if (sh_video->format >= 0x20202020)
495 meta = malloc (8);
496 sprintf (meta, "%.4s", (char *) &sh_video->format);
498 else
500 meta = malloc (8);
501 sprintf (meta, "0x%08X", sh_video->format);
503 return meta;
506 case META_VIDEO_BITRATE:
508 meta = malloc (16);
509 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
510 return meta;
513 case META_VIDEO_RESOLUTION:
515 meta = malloc (16);
516 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
517 return meta;
520 case META_AUDIO_CODEC:
522 if (sh_audio->codec && sh_audio->codec->name)
523 meta = strdup (sh_audio->codec->name);
524 return meta;
527 case META_AUDIO_BITRATE:
529 meta = malloc (16);
530 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
531 return meta;
534 case META_AUDIO_SAMPLES:
536 meta = malloc (16);
537 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
538 return meta;
541 /* check for valid demuxer */
542 case META_INFO_TITLE:
543 return get_demuxer_info ("Title");
545 case META_INFO_ARTIST:
546 return get_demuxer_info ("Artist");
548 case META_INFO_ALBUM:
549 return get_demuxer_info ("Album");
551 case META_INFO_YEAR:
552 return get_demuxer_info ("Year");
554 case META_INFO_COMMENT:
555 return get_demuxer_info ("Comment");
557 case META_INFO_TRACK:
558 return get_demuxer_info ("Track");
560 case META_INFO_GENRE:
561 return get_demuxer_info ("Genre");
563 default:
564 break;
567 return meta;
570 /// step size of mixer changes
571 int volstep = 3;
573 void uninit_player(unsigned int mask){
574 mask=inited_flags&mask;
576 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
578 if(mask&INITED_ACODEC){
579 inited_flags&=~INITED_ACODEC;
580 current_module="uninit_acodec";
581 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
582 #ifdef HAVE_NEW_GUI
583 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
584 #endif
585 mpctx->sh_audio=NULL;
586 mpctx->mixer.afilter = NULL;
589 if(mask&INITED_VCODEC){
590 inited_flags&=~INITED_VCODEC;
591 current_module="uninit_vcodec";
592 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
593 mpctx->sh_video=NULL;
594 #ifdef HAVE_MENU
595 vf_menu=NULL;
596 #endif
599 if(mask&INITED_DEMUXER){
600 inited_flags&=~INITED_DEMUXER;
601 current_module="free_demuxer";
602 if(mpctx->demuxer){
603 mpctx->stream=mpctx->demuxer->stream;
604 free_demuxer(mpctx->demuxer);
606 mpctx->demuxer=NULL;
609 // kill the cache process:
610 if(mask&INITED_STREAM){
611 inited_flags&=~INITED_STREAM;
612 current_module="uninit_stream";
613 if(mpctx->stream) free_stream(mpctx->stream);
614 mpctx->stream=NULL;
617 if(mask&INITED_VO){
618 inited_flags&=~INITED_VO;
619 current_module="uninit_vo";
620 mpctx->video_out->uninit();
621 mpctx->video_out=NULL;
624 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
625 if(mask&INITED_GETCH2){
626 inited_flags&=~INITED_GETCH2;
627 current_module="uninit_getch2";
628 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
629 // restore terminal:
630 getch2_disable();
633 if(mask&INITED_VOBSUB){
634 inited_flags&=~INITED_VOBSUB;
635 current_module="uninit_vobsub";
636 if(vo_vobsub) vobsub_close(vo_vobsub);
637 vo_vobsub=NULL;
640 if (mask&INITED_SPUDEC){
641 inited_flags&=~INITED_SPUDEC;
642 current_module="uninit_spudec";
643 spudec_free(vo_spudec);
644 vo_spudec=NULL;
647 if(mask&INITED_AO){
648 inited_flags&=~INITED_AO;
649 current_module="uninit_ao";
650 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
651 mpctx->audio_out->uninit(mpctx->eof?0:1); mpctx->audio_out=NULL;
654 #ifdef HAVE_NEW_GUI
655 if(mask&INITED_GUI){
656 inited_flags&=~INITED_GUI;
657 current_module="uninit_gui";
658 guiDone();
660 #endif
662 if(mask&INITED_INPUT){
663 inited_flags&=~INITED_INPUT;
664 current_module="uninit_input";
665 mp_input_uninit();
668 current_module=NULL;
671 void exit_player_with_rc(const char* how, int rc){
673 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
674 uninit_player(INITED_ALL);
675 #ifdef HAVE_X11
676 #ifdef HAVE_NEW_GUI
677 if ( !use_gui )
678 #endif
679 vo_uninit(); // Close the X11 connection (if any is open).
680 #endif
682 #ifdef HAVE_FREETYPE
683 current_module="uninit_font";
684 if (vo_font) free_font_desc(vo_font);
685 vo_font = NULL;
686 done_freetype();
687 #endif
688 free_osd_list();
690 #ifdef USE_ASS
691 ass_library_done(ass_library);
692 #endif
694 current_module="exit_player";
696 // free mplayer config
697 if(mconfig)
698 m_config_free(mconfig);
700 if(mpctx->playtree)
701 play_tree_free(mpctx->playtree, 1);
704 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
705 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
706 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
708 exit(rc);
711 void exit_player(const char* how){
712 exit_player_with_rc(how, 1);
715 #ifndef __MINGW32__
716 static void child_sighandler(int x){
717 pid_t pid;
718 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
720 #endif
722 #ifdef CRASH_DEBUG
723 static char *prog_path;
724 static int crash_debug = 0;
725 #endif
727 static void exit_sighandler(int x){
728 static int sig_count=0;
729 #ifdef CRASH_DEBUG
730 if (!crash_debug || x != SIGTRAP)
731 #endif
732 ++sig_count;
733 if(inited_flags==0 && sig_count>1) exit(1);
734 if(sig_count==5)
736 /* We're crashing bad and can't uninit cleanly :(
737 * by popular request, we make one last (dirty)
738 * effort to restore the user's
739 * terminal. */
740 getch2_disable();
741 exit(1);
743 if(sig_count==6) exit(1);
744 if(sig_count>6){
745 // can't stop :(
746 #ifndef __MINGW32__
747 kill(getpid(),SIGKILL);
748 #endif
750 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
751 current_module?current_module:"unknown"
753 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
754 if(sig_count<=1)
755 switch(x){
756 case SIGINT:
757 case SIGQUIT:
758 case SIGTERM:
759 case SIGKILL:
760 break; // killed from keyboard (^C) or killed [-9]
761 case SIGILL:
762 #ifdef RUNTIME_CPUDETECT
763 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
764 #else
765 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
766 #endif
767 case SIGFPE:
768 case SIGSEGV:
769 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
770 default:
771 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
772 #ifdef CRASH_DEBUG
773 if (crash_debug) {
774 int gdb_pid;
775 char spid[20];
776 snprintf(spid, 19, "%i", getpid());
777 spid[19] = 0;
778 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
779 gdb_pid = fork();
780 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
781 if (gdb_pid == 0) { // We are the child
782 getch2_disable(); // allow terminal to work properly with gdb
783 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
784 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
785 } else if (gdb_pid < 0)
786 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
787 else {
788 waitpid(gdb_pid, NULL, 0);
790 if (x == SIGTRAP) return;
792 #endif
794 exit_player(NULL);
797 extern void mp_input_register_options(m_config_t* cfg);
799 #include "cfg-mplayer.h"
801 static void parse_cfgfiles( m_config_t* conf )
803 char *conffile;
804 int conffile_fd;
805 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
806 exit_player(NULL);
807 if ((conffile = get_path("")) == NULL) {
808 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
809 } else {
810 #ifdef __MINGW32__
811 mkdir(conffile);
812 #else
813 mkdir(conffile, 0777);
814 #endif
815 free(conffile);
816 if ((conffile = get_path("config")) == NULL) {
817 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
818 } else {
819 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
820 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
821 write(conffile_fd, default_config, strlen(default_config));
822 close(conffile_fd);
824 if (m_config_parse_config_file(conf, conffile) < 0)
825 exit_player(NULL);
826 free(conffile);
831 void load_per_file_config (m_config_t* conf, const char *const file)
833 char *confpath;
834 char cfg[strlen(file)+10];
835 struct stat st;
836 char *name;
838 sprintf (cfg, "%s.conf", file);
840 if (use_filedir_conf && !stat (cfg, &st))
842 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
843 m_config_parse_config_file (conf, cfg);
844 return;
847 if ((name = strrchr (cfg, '/')) == NULL)
848 name = cfg;
849 else
850 name++;
852 if ((confpath = get_path (name)) != NULL)
854 if (!stat (confpath, &st))
856 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
857 m_config_parse_config_file (conf, confpath);
860 free (confpath);
864 /* When libmpdemux performs a blocking operation (network connection or
865 * cache filling) if the operation fails we use this function to check
866 * if it was interrupted by the user.
867 * The function returns a new value for eof. */
868 static int libmpdemux_was_interrupted(int eof) {
869 mp_cmd_t* cmd;
870 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
871 switch(cmd->id) {
872 case MP_CMD_QUIT:
873 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
874 case MP_CMD_PLAY_TREE_STEP: {
875 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
876 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
877 } break;
878 case MP_CMD_PLAY_TREE_UP_STEP: {
879 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
880 } break;
881 case MP_CMD_PLAY_ALT_SRC_STEP: {
882 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
883 } break;
885 mp_cmd_free(cmd);
887 return eof;
890 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
892 int playtree_add_playlist(play_tree_t* entry)
894 play_tree_add_bpf(entry,filename);
896 #ifdef HAVE_NEW_GUI
897 if (use_gui) {
898 if (entry) {
899 import_playtree_playlist_into_gui(entry, mconfig);
900 play_tree_free_list(entry,1);
902 } else
903 #endif
905 if(!entry) {
906 entry = mpctx->playtree_iter->tree;
907 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
908 return PT_NEXT_ENTRY;
910 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
911 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
912 return PT_NEXT_ENTRY;
915 play_tree_remove(entry,1,1);
916 return PT_NEXT_SRC;
918 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
919 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
920 entry = mpctx->playtree_iter->tree;
921 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
922 return PT_NEXT_ENTRY;
924 play_tree_remove(entry,1,1);
926 return PT_NEXT_SRC;
929 void add_subtitles(char *filename, float fps, int noerr)
931 sub_data *subd;
932 #ifdef USE_ASS
933 ass_track_t *asst = 0;
934 #endif
936 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
937 return;
940 subd = sub_read_file(filename, fps);
941 #ifdef USE_ASS
942 if (ass_enabled)
943 #ifdef USE_ICONV
944 asst = ass_read_file(ass_library, filename, sub_cp);
945 #else
946 asst = ass_read_file(ass_library, filename, 0);
947 #endif
948 if (ass_enabled && subd && !asst)
949 asst = ass_read_subdata(ass_library, subd, fps);
951 if (!asst && !subd)
952 #else
953 if(!subd)
954 #endif
955 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
956 filename_recode(filename));
958 #ifdef USE_ASS
959 if (!asst && !subd) return;
960 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
961 #else
962 if (!subd) return;
963 #endif
964 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
965 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
966 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
967 filename_recode(filename));
968 ++mpctx->set_of_sub_size;
969 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
970 filename_recode(filename));
973 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
974 void update_set_of_subtitles(void)
975 // subdata was changed, set_of_sub... have to be updated.
977 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
978 int i;
979 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
980 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
981 set_of_subtitles[i-1] = set_of_subtitles[i];
982 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
983 --mpctx->set_of_sub_size;
984 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
986 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
987 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
989 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
990 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
991 ++mpctx->set_of_sub_size;
995 void init_vo_spudec(void) {
996 if (vo_spudec)
997 spudec_free(vo_spudec);
998 inited_flags &= ~INITED_SPUDEC;
999 vo_spudec = NULL;
1000 if (spudec_ifo) {
1001 unsigned int palette[16], width, height;
1002 current_module="spudec_init_vobsub";
1003 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1004 vo_spudec=spudec_new_scaled(palette, width, height);
1007 #ifdef USE_DVDREAD
1008 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1009 current_module="spudec_init_dvdread";
1010 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1011 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1013 #endif
1015 #ifdef USE_DVDNAV
1016 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1017 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1018 current_module="spudec_init_dvdnav";
1019 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1021 #endif
1023 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1024 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1025 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1026 current_module = "spudec_init_matroska";
1027 vo_spudec =
1028 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1029 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1030 mkv_sh_sub->height);
1031 forced_subs_only = mkv_sh_sub->forced_subs_only;
1034 if (vo_spudec==NULL) {
1035 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1036 unsigned int *palette = NULL;
1037 if (sh && sh->has_palette)
1038 palette = sh->palette;
1039 current_module="spudec_init_normal";
1040 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1041 spudec_set_font_factor(vo_spudec,font_factor);
1044 if (vo_spudec!=NULL)
1045 inited_flags|=INITED_SPUDEC;
1049 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1050 * make it all work is to use the builtin SDL-bootstrap code, which
1051 * will be done automatically by replacing our main() if we include SDL.h.
1053 #if defined(SYS_DARWIN) && defined(HAVE_SDL)
1054 #include <SDL.h>
1055 #endif
1058 * \brief append a formatted string
1059 * \param buf buffer to print into
1060 * \param pos position of terminating 0 in buf
1061 * \param len maximum number of characters in buf, not including terminating 0
1062 * \param format printf format string
1064 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1066 va_list va;
1067 va_start(va, format);
1068 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1069 va_end(va);
1070 if (*pos >= len ) {
1071 buf[len] = 0;
1072 *pos = len;
1077 * \brief append time in the hh:mm:ss.f format
1078 * \param buf buffer to print into
1079 * \param pos position of terminating 0 in buf
1080 * \param len maximum number of characters in buf, not including terminating 0
1081 * \param time time value to convert/append
1083 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1084 long tenths = 10 * time;
1085 int f1 = tenths % 10;
1086 int ss = (tenths / 10) % 60;
1087 int mm = (tenths / 600) % 60;
1088 int hh = tenths / 36000;
1089 if (time <= 0) {
1090 saddf(buf, pos, len, "unknown");
1091 return;
1093 if (hh > 0)
1094 saddf(buf, pos, len, "%2d:", hh);
1095 if (hh > 0 || mm > 0)
1096 saddf(buf, pos, len, "%02d:", mm);
1097 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1101 * \brief print the status line
1102 * \param a_pos audio position
1103 * \param a_v A-V desynchronization
1104 * \param corr amount out A-V synchronization
1106 static void print_status(float a_pos, float a_v, float corr)
1108 sh_video_t * const sh_video = mpctx->sh_video;
1109 int width;
1110 char *line;
1111 unsigned pos = 0;
1112 get_screen_size();
1113 if (screen_width > 0)
1114 width = screen_width;
1115 else
1116 width = 80;
1117 #ifdef WIN32
1118 /* Windows command line is broken (MinGW's rxvt works, but we
1119 * should not depend on that). */
1120 width--;
1121 #endif
1122 line = malloc(width + 1); // one additional char for the terminating null
1124 // Audio time
1125 if (mpctx->sh_audio) {
1126 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1127 if (!sh_video) {
1128 float len = demuxer_get_time_length(mpctx->demuxer);
1129 saddf(line, &pos, width, "(");
1130 sadd_hhmmssf(line, &pos, width, a_pos);
1131 saddf(line, &pos, width, ") of %.1f (", len);
1132 sadd_hhmmssf(line, &pos, width, len);
1133 saddf(line, &pos, width, ") ");
1137 // Video time
1138 if (sh_video)
1139 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1141 // A-V sync
1142 if (mpctx->sh_audio && sh_video)
1143 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1145 // Video stats
1146 if (sh_video)
1147 saddf(line, &pos, width, "%3d/%3d ",
1148 (int)sh_video->num_frames,
1149 (int)sh_video->num_frames_decoded);
1151 // CPU usage
1152 if (sh_video) {
1153 if (sh_video->timer > 0.5)
1154 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1155 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1156 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1157 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1158 else
1159 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1160 } else if (mpctx->sh_audio) {
1161 if (mpctx->delay > 0.5)
1162 saddf(line, &pos, width, "%4.1f%% ",
1163 100.0*audio_time_usage/(double)mpctx->delay);
1164 else
1165 saddf(line, &pos, width, "??,?%% ");
1168 // VO stats
1169 if (sh_video)
1170 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1172 #ifdef USE_STREAM_CACHE
1173 // cache stats
1174 if (stream_cache_size > 0)
1175 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1176 #endif
1178 // other
1179 if (playback_speed != 1)
1180 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1182 // end
1183 if (erase_to_end_of_line) {
1184 line[pos] = 0;
1185 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1186 } else {
1187 memset(&line[pos], ' ', width - pos);
1188 line[width] = 0;
1189 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1191 free(line);
1195 * \brief build a chain of audio filters that converts the input format
1196 * to the ao's format, taking into account the current playback_speed.
1197 * \param sh_audio describes the requested input format of the chain.
1198 * \param ao_data describes the requested output format of the chain.
1200 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1202 int new_srate;
1203 int result;
1204 if (!sh_audio)
1206 #ifdef HAVE_NEW_GUI
1207 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1208 #endif
1209 mpctx->mixer.afilter = NULL;
1210 return 0;
1212 if(af_control_any_rev(sh_audio->afilter,
1213 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1214 &playback_speed)) {
1215 new_srate = sh_audio->samplerate;
1216 } else {
1217 new_srate = sh_audio->samplerate * playback_speed;
1218 if (new_srate != ao_data->samplerate) {
1219 // limits are taken from libaf/af_resample.c
1220 if (new_srate < 8000)
1221 new_srate = 8000;
1222 if (new_srate > 192000)
1223 new_srate = 192000;
1224 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1227 result = init_audio_filters(sh_audio, new_srate,
1228 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1229 mpctx->mixer.afilter = sh_audio->afilter;
1230 #ifdef HAVE_NEW_GUI
1231 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1232 #endif
1233 return result;
1237 typedef struct mp_osd_msg mp_osd_msg_t;
1238 struct mp_osd_msg {
1239 /// Previous message on the stack.
1240 mp_osd_msg_t* prev;
1241 /// Message text.
1242 char msg[64];
1243 int id,level,started;
1244 /// Display duration in ms.
1245 unsigned time;
1248 /// OSD message stack.
1249 static mp_osd_msg_t* osd_msg_stack = NULL;
1252 * \brief Add a message on the OSD message stack
1254 * If a message with the same id is already present in the stack
1255 * it is pulled on top of the stack, otherwise a new message is created.
1259 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1260 mp_osd_msg_t *msg,*last=NULL;
1261 va_list va;
1262 int r;
1264 // look if the id is already in the stack
1265 for(msg = osd_msg_stack ; msg && msg->id != id ;
1266 last = msg, msg = msg->prev);
1267 // not found: alloc it
1268 if(!msg) {
1269 msg = calloc(1,sizeof(mp_osd_msg_t));
1270 msg->prev = osd_msg_stack;
1271 osd_msg_stack = msg;
1272 } else if(last) { // found, but it's not on top of the stack
1273 last->prev = msg->prev;
1274 msg->prev = osd_msg_stack;
1275 osd_msg_stack = msg;
1277 // write the msg
1278 va_start(va,fmt);
1279 r = vsnprintf(msg->msg, 64, fmt, va);
1280 va_end(va);
1281 if(r >= 64) msg->msg[63] = 0;
1282 // set id and time
1283 msg->id = id;
1284 msg->level = level;
1285 msg->time = time;
1290 * \brief Remove a message from the OSD stack
1292 * This function can be used to get rid of a message right away.
1296 void rm_osd_msg(int id) {
1297 mp_osd_msg_t *msg,*last=NULL;
1299 // Search for the msg
1300 for(msg = osd_msg_stack ; msg && msg->id != id ;
1301 last = msg, msg = msg->prev);
1302 if(!msg) return;
1304 // Detach it from the stack and free it
1305 if(last)
1306 last->prev = msg->prev;
1307 else
1308 osd_msg_stack = msg->prev;
1309 free(msg);
1313 * \brief Remove all messages from the OSD stack
1317 static void clear_osd_msgs(void) {
1318 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1319 while(msg) {
1320 prev = msg->prev;
1321 free(msg);
1322 msg = prev;
1324 osd_msg_stack = NULL;
1328 * \brief Get the current message from the OSD stack.
1330 * This function decrements the message timer and destroys the old ones.
1331 * The message that should be displayed is returned (if any).
1335 static mp_osd_msg_t* get_osd_msg(void) {
1336 mp_osd_msg_t *msg,*prev,*last = NULL;
1337 static unsigned last_update = 0;
1338 unsigned now = GetTimerMS();
1339 unsigned diff;
1340 char hidden_dec_done = 0;
1342 if (osd_visible) {
1343 // 36000000 means max timed visibility is 1 hour into the future, if
1344 // the difference is greater assume it's wrapped around from below 0
1345 if (osd_visible - now > 36000000) {
1346 osd_visible = 0;
1347 vo_osd_progbar_type = -1; // disable
1348 vo_osd_changed(OSDTYPE_PROGBAR);
1349 if (mpctx->osd_function != OSD_PAUSE)
1350 mpctx->osd_function = OSD_PLAY;
1354 if(!last_update) last_update = now;
1355 diff = now >= last_update ? now - last_update : 0;
1357 last_update = now;
1359 // Look for the first message in the stack with high enough level.
1360 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1361 prev = msg->prev;
1362 if(msg->level > osd_level && hidden_dec_done) continue;
1363 // The message has a high enough level or it is the first hidden one
1364 // in both cases we decrement the timer or kill it.
1365 if(!msg->started || msg->time > diff) {
1366 if(msg->started) msg->time -= diff;
1367 else msg->started = 1;
1368 // display it
1369 if(msg->level <= osd_level) return msg;
1370 hidden_dec_done = 1;
1371 continue;
1373 // kill the message
1374 free(msg);
1375 if(last) {
1376 last->prev = prev;
1377 msg = last;
1378 } else {
1379 osd_msg_stack = prev;
1380 msg = NULL;
1383 // Nothing found
1384 return NULL;
1388 * \brief Display the OSD bar.
1390 * Display the OSD bar or fall back on a simple message.
1394 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1396 if(osd_level < 1) return;
1398 if(mpctx->sh_video) {
1399 osd_visible = (GetTimerMS() + 1000) | 1;
1400 vo_osd_progbar_type = type;
1401 vo_osd_progbar_value = 256*(val-min)/(max-min);
1402 vo_osd_changed(OSDTYPE_PROGBAR);
1403 return;
1406 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1407 name,ROUND(100*(val-min)/(max-min)));
1412 * \brief Update the OSD message line.
1414 * This function displays the current message on the vo OSD or on the term.
1415 * If the stack is empty and the OSD level is high enough the timer
1416 * is displayed (only on the vo OSD).
1420 static void update_osd_msg(void) {
1421 mp_osd_msg_t *msg;
1422 static char osd_text[64] = "";
1423 static char osd_text_timer[64];
1425 // we need some mem for vo_osd_text
1426 vo_osd_text = (unsigned char*)osd_text;
1428 // Look if we have a msg
1429 if((msg = get_osd_msg())) {
1430 if(strcmp(osd_text,msg->msg)) {
1431 strncpy((char*)osd_text, msg->msg, 63);
1432 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1433 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1435 return;
1438 if(mpctx->sh_video) {
1439 // fallback on the timer
1440 if(osd_level>=2) {
1441 int len = demuxer_get_time_length(mpctx->demuxer);
1442 int percentage = -1;
1443 char percentage_text[10];
1444 int pts = demuxer_get_current_time(mpctx->demuxer);
1446 if (mpctx->osd_show_percentage)
1447 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1449 if (percentage >= 0)
1450 snprintf(percentage_text, 9, " (%d%%)", percentage);
1451 else
1452 percentage_text[0] = 0;
1454 if (osd_level == 3)
1455 snprintf(osd_text_timer, 63,
1456 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1457 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1458 len/3600,(len/60)%60,len%60,percentage_text);
1459 else
1460 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1461 mpctx->osd_function,pts/3600,(pts/60)%60,
1462 pts%60,percentage_text);
1463 } else
1464 osd_text_timer[0]=0;
1466 // always decrement the percentage timer
1467 if(mpctx->osd_show_percentage)
1468 mpctx->osd_show_percentage--;
1470 if(strcmp(osd_text,osd_text_timer)) {
1471 strncpy(osd_text, osd_text_timer, 63);
1472 vo_osd_changed(OSDTYPE_OSD);
1474 return;
1477 // Clear the term osd line
1478 if(term_osd && osd_text[0]) {
1479 osd_text[0] = 0;
1480 printf("%s\n",term_osd_esc);
1484 ///@}
1485 // OSDMsgStack
1488 void reinit_audio_chain(void) {
1489 if(mpctx->sh_audio){
1490 current_module="init_audio_codec";
1491 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1492 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1493 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1494 mpctx->d_audio->id = -2;
1495 return;
1496 } else
1497 inited_flags|=INITED_ACODEC;
1498 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1501 //const ao_info_t *info=audio_out->info;
1502 current_module="af_preinit";
1503 ao_data.samplerate=force_srate;
1504 ao_data.channels=0;
1505 ao_data.format=audio_output_format;
1506 #if 1
1507 // first init to detect best values
1508 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1509 // input:
1510 mpctx->sh_audio->samplerate,
1511 // output:
1512 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1513 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1514 exit_player(MSGTR_Exit_error);
1516 #endif
1517 current_module="ao2_init";
1518 if(!(mpctx->audio_out=init_best_audio_out(audio_driver_list,
1519 0, // plugin flag
1520 ao_data.samplerate,
1521 ao_data.channels,
1522 ao_data.format,0))){
1523 // FAILED:
1524 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1525 uninit_player(INITED_ACODEC); // close codec
1526 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1527 mpctx->d_audio->id = -2;
1528 return;
1529 } else {
1530 // SUCCESS:
1531 inited_flags|=INITED_AO;
1532 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1533 mpctx->audio_out->info->short_name,
1534 ao_data.samplerate, ao_data.channels,
1535 af_fmt2str_short(ao_data.format),
1536 af_fmt2bits(ao_data.format)/8 );
1537 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1538 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1539 if(strlen(mpctx->audio_out->info->comment) > 0)
1540 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1541 // init audio filters:
1542 #if 1
1543 current_module="af_init";
1544 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1545 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1546 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1547 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
1548 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1550 #endif
1552 mpctx->mixer.audio_out = mpctx->audio_out;
1553 mpctx->mixer.volstep = volstep;
1558 ///@}
1559 // Command2Property
1562 // Return pts value corresponding to the end point of audio written to the
1563 // ao so far.
1564 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1566 double buffered_output;
1567 // first calculate the end pts of audio that has been output by decoder
1568 double a_pts = sh_audio->pts;
1569 if (a_pts != MP_NOPTS_VALUE)
1570 // Good, decoder supports new way of calculating audio pts.
1571 // sh_audio->pts is the timestamp of the latest input packet with
1572 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1573 // the amount of bytes the decoder has written after that timestamp.
1574 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1575 else {
1576 // Decoder doesn't support new way of calculating pts (or we're
1577 // being called before it has decoded anything with known timestamp).
1578 // Use the old method of audio pts calculation: take the timestamp
1579 // of last packet with known pts the decoder has read data from,
1580 // and add amount of bytes read after the beginning of that packet
1581 // divided by input bps. This will be inaccurate if the input/output
1582 // ratio is not constant for every audio packet or if it is constant
1583 // but not accurately known in sh_audio->i_bps.
1585 a_pts = d_audio->pts;
1586 // ds_tell_pts returns bytes read after last timestamp from
1587 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1588 // it has read but not decoded
1589 if (sh_audio->i_bps)
1590 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1591 (double)sh_audio->i_bps;
1593 // Now a_pts hopefully holds the pts for end of audio from decoder.
1594 // Substract data in buffers between decoder and audio out.
1596 // Decoded but not filtered
1597 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1599 // Data buffered in audio filters, measured in bytes of "missing" output
1600 buffered_output = af_calc_delay(sh_audio->afilter);
1602 // Data that was ready for ao but was buffered because ao didn't fully
1603 // accept everything to internal buffers yet
1604 buffered_output += sh_audio->a_out_buffer_len;
1606 // Filters divide audio length by playback_speed, so multiply by it
1607 // to get the length in original units without speedup or slowdown
1608 a_pts -= buffered_output * playback_speed / ao_data.bps;
1610 return a_pts;
1613 // Return pts value corresponding to currently playing audio.
1614 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1615 ao_functions_t *audio_out)
1617 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1618 audio_out->get_delay();
1621 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1623 unsigned char *start;
1624 int in_size;
1625 int hit_eof=0;
1626 double pts;
1628 while (1) {
1629 void *decoded_frame;
1630 current_module = "decode video";
1631 // XXX Time used in this call is not counted in any performance
1632 // timer now, OSD is not updated correctly for filter-added frames
1633 if (vf_output_queued_frame(sh_video->vfilter))
1634 break;
1635 current_module = "video_read_frame";
1636 in_size = ds_get_packet_pts(d_video, &start, &pts);
1637 if (in_size < 0) {
1638 // try to extract last frames in case of decoder lag
1639 in_size = 0;
1640 pts = 1e300;
1641 hit_eof = 1;
1643 if (in_size > max_framesize)
1644 max_framesize = in_size;
1645 current_module = "decode video";
1646 decoded_frame = decode_video(sh_video, start, in_size, 0, pts);
1647 if (decoded_frame) {
1648 update_subtitles(sh_video, mpctx->d_sub, 0);
1649 update_teletext(sh_video, mpctx->demuxer, 0);
1650 update_osd_msg();
1651 current_module = "filter video";
1652 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1653 break;
1655 if (hit_eof)
1656 return 0;
1658 return 1;
1661 #ifdef HAVE_RTC
1662 int rtc_fd = -1;
1663 #endif
1665 static float timing_sleep(float time_frame)
1667 #ifdef HAVE_RTC
1668 if (rtc_fd >= 0){
1669 // -------- RTC -----------
1670 current_module="sleep_rtc";
1671 while (time_frame > 0.000) {
1672 unsigned long rtc_ts;
1673 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1674 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1675 time_frame -= GetRelativeTime();
1677 } else
1678 #endif
1680 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1681 // unnecessarily high CPU usage
1682 float margin = softsleep ? 0.011 : 0;
1683 current_module = "sleep_timer";
1684 while (time_frame > margin) {
1685 usec_sleep(1000000 * (time_frame - margin));
1686 time_frame -= GetRelativeTime();
1688 if (softsleep){
1689 current_module = "sleep_soft";
1690 if (time_frame < 0)
1691 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1692 while (time_frame > 0)
1693 time_frame-=GetRelativeTime(); // burn the CPU
1696 return time_frame;
1699 static void adjust_sync_and_print_status(int between_frames, float timing_error)
1701 current_module="av_sync";
1703 if(mpctx->sh_audio){
1704 double a_pts, v_pts;
1706 if (autosync)
1708 * If autosync is enabled, the value for delay must be calculated
1709 * a bit differently. It is set only to the difference between
1710 * the audio and video timers. Any attempt to include the real
1711 * or corrected delay causes the pts_correction code below to
1712 * try to correct for the changes in delay which autosync is
1713 * trying to measure. This keeps the two from competing, but still
1714 * allows the code to correct for PTS drift *only*. (Using a delay
1715 * value here, even a "corrected" one, would be incompatible with
1716 * autosync mode.)
1718 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
1719 else
1720 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
1722 v_pts = mpctx->sh_video->pts;
1725 static int drop_message=0;
1726 double AV_delay = a_pts - audio_delay - v_pts;
1727 double x;
1728 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1729 ++drop_message;
1730 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1732 if (autosync)
1733 x = AV_delay*0.1f;
1734 else
1735 /* Do not correct target time for the next frame if this frame
1736 * was late not because of wrong target time but because the
1737 * target time could not be met */
1738 x = (AV_delay + timing_error * playback_speed) * 0.1f;
1739 if (x < -max_pts_correction)
1740 x = -max_pts_correction;
1741 else if (x> max_pts_correction)
1742 x = max_pts_correction;
1743 if (default_max_pts_correction >= 0)
1744 max_pts_correction = default_max_pts_correction;
1745 else
1746 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1747 if (!between_frames) {
1748 mpctx->delay+=x;
1749 c_total+=x;
1751 if(!quiet)
1752 print_status(a_pts - audio_delay, AV_delay, c_total);
1755 } else {
1756 // No audio:
1758 if (!quiet)
1759 print_status(0, 0, 0);
1763 static int fill_audio_out_buffers(void)
1765 unsigned int t;
1766 double tt;
1767 int playsize;
1768 int playflags=0;
1769 int audio_eof=0;
1770 int bytes_to_write;
1771 sh_audio_t * const sh_audio = mpctx->sh_audio;
1773 current_module="play_audio";
1775 while (1) {
1776 // all the current uses of ao_data.pts seem to be in aos that handle
1777 // sync completely wrong; there should be no need to use ao_data.pts
1778 // in get_space()
1779 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1780 bytes_to_write = mpctx->audio_out->get_space();
1781 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1782 break;
1784 // handle audio-only case:
1785 // this is where mplayer sleeps during audio-only playback
1786 // to avoid 100% CPU use
1787 usec_sleep(10000); // Wait a tick before retry
1790 while (bytes_to_write) {
1791 playsize = bytes_to_write;
1792 if (playsize > MAX_OUTBURST)
1793 playsize = MAX_OUTBURST;
1794 bytes_to_write -= playsize;
1796 // Fill buffer if needed:
1797 current_module="decode_audio";
1798 t = GetTimer();
1799 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1800 if (mpctx->d_audio->eof) {
1801 audio_eof = 1;
1802 if (sh_audio->a_out_buffer_len == 0)
1803 return 0;
1805 t = GetTimer() - t;
1806 tt = t*0.000001f; audio_time_usage+=tt;
1807 if (playsize > sh_audio->a_out_buffer_len) {
1808 playsize = sh_audio->a_out_buffer_len;
1809 if (audio_eof)
1810 playflags |= AOPLAY_FINAL_CHUNK;
1812 if (!playsize)
1813 break;
1815 // play audio:
1816 current_module="play_audio";
1818 // Is this pts value actually useful for the aos that access it?
1819 // They're obviously badly broken in the way they handle av sync;
1820 // would not having access to this make them more broken?
1821 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1822 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1824 if (playsize > 0) {
1825 sh_audio->a_out_buffer_len -= playsize;
1826 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1827 sh_audio->a_out_buffer_len);
1828 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
1830 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1831 // Sanity check to avoid hanging in case current ao doesn't output
1832 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1833 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1834 sh_audio->a_out_buffer_len = 0;
1837 return 1;
1840 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
1842 int frame_time_remaining = 0;
1843 current_module="calc_sleep_time";
1845 *time_frame -= GetRelativeTime(); // reset timer
1847 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1848 float delay = mpctx->audio_out->get_delay();
1849 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
1851 if (autosync) {
1853 * Adjust this raw delay value by calculating the expected
1854 * delay for this frame and generating a new value which is
1855 * weighted between the two. The higher autosync is, the
1856 * closer to the delay value gets to that which "-nosound"
1857 * would have used, and the longer it will take for A/V
1858 * sync to settle at the right value (but it eventually will.)
1859 * This settling time is very short for values below 100.
1861 float predicted = mpctx->delay / playback_speed + *time_frame;
1862 float difference = delay - predicted;
1863 delay = predicted + difference / (float)autosync;
1866 *time_frame = delay - mpctx->delay / playback_speed;
1868 // delay = amount of audio buffered in soundcard/driver
1869 if (delay > 0.25) delay=0.25; else
1870 if (delay < 0.10) delay=0.10;
1871 if (*time_frame > delay*0.6) {
1872 // sleep time too big - may cause audio drops (buffer underrun)
1873 frame_time_remaining = 1;
1874 *time_frame = delay*0.5;
1876 } else {
1877 // If we're lagging more than 200 ms behind the right playback rate,
1878 // don't try to "catch up".
1879 // If benchmark is set always output frames as fast as possible
1880 // without sleeping.
1881 if (*time_frame < -0.2 || benchmark)
1882 *time_frame = 0;
1885 *aq_sleep_time += *time_frame;
1888 //============================== SLEEP: ===================================
1890 // flag 256 means: libvo driver does its timing (dvb card)
1891 if (*time_frame > 0.001 && !(vo_flags&256))
1892 *time_frame = timing_sleep(*time_frame);
1893 return frame_time_remaining;
1896 int reinit_video_chain(void) {
1897 sh_video_t * const sh_video = mpctx->sh_video;
1898 double ar=-1.0;
1899 //================== Init VIDEO (codec & libvo) ==========================
1900 if(!fixed_vo || !(inited_flags&INITED_VO)){
1901 current_module="preinit_libvo";
1903 //shouldn't we set dvideo->id=-2 when we fail?
1904 vo_config_count=0;
1905 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
1906 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
1907 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1908 goto err_out;
1910 inited_flags|=INITED_VO;
1913 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
1914 mpctx->sh_video->stream_aspect = ar;
1915 current_module="init_video_filters";
1917 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
1918 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
1920 #ifdef HAVE_MENU
1921 if(use_menu) {
1922 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
1923 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
1924 if(!vf_menu) {
1925 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
1926 use_menu = 0;
1929 if(vf_menu)
1930 sh_video->vfilter=(void*)vf_menu;
1931 #endif
1933 #ifdef USE_ASS
1934 if(ass_enabled) {
1935 int i;
1936 int insert = 1;
1937 if (vf_settings)
1938 for (i = 0; vf_settings[i].name; ++i)
1939 if (strcmp(vf_settings[i].name, "ass") == 0) {
1940 insert = 0;
1941 break;
1943 if (insert) {
1944 extern vf_info_t vf_info_ass;
1945 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1946 char* vf_arg[] = {"auto", "1", NULL};
1947 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
1948 if (vf_ass)
1949 sh_video->vfilter=(void*)vf_ass;
1950 else
1951 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1954 #endif
1956 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
1958 #ifdef USE_ASS
1959 if (ass_enabled)
1960 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1961 #endif
1963 current_module="init_video_codec";
1965 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1966 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1967 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1969 if(!sh_video->inited){
1970 if(!fixed_vo) uninit_player(INITED_VO);
1971 goto err_out;
1974 inited_flags|=INITED_VCODEC;
1976 if (sh_video->codec)
1977 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
1979 sh_video->last_pts = MP_NOPTS_VALUE;
1980 sh_video->num_buffered_pts = 0;
1981 sh_video->next_frame_time = 0;
1983 if(auto_quality>0){
1984 // Auto quality option enabled
1985 output_quality=get_video_quality_max(sh_video);
1986 if(auto_quality>output_quality) auto_quality=output_quality;
1987 else output_quality=auto_quality;
1988 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
1989 set_video_quality(sh_video,output_quality);
1992 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
1994 current_module="init_vo";
1996 return 1;
1998 err_out:
1999 mpctx->sh_video = mpctx->d_video->sh = NULL;
2000 return 0;
2003 static double update_video(int *blit_frame)
2005 sh_video_t * const sh_video = mpctx->sh_video;
2006 //-------------------- Decode a frame: -----------------------
2007 double frame_time;
2008 *blit_frame = 0; // Don't blit if we hit EOF
2009 if (!correct_pts) {
2010 unsigned char* start=NULL;
2011 void *decoded_frame;
2012 int drop_frame=0;
2013 int in_size;
2015 current_module = "video_read_frame";
2016 frame_time = sh_video->next_frame_time;
2017 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2018 &start, force_fps);
2019 if (in_size < 0)
2020 return -1;
2021 if (in_size > max_framesize)
2022 max_framesize = in_size; // stats
2023 sh_video->timer += frame_time;
2024 if (mpctx->sh_audio)
2025 mpctx->delay -= frame_time;
2026 // video_read_frame can change fps (e.g. for ASF video)
2027 vo_fps = sh_video->fps;
2028 // check for frame-drop:
2029 current_module = "check_framedrop";
2030 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2031 static int dropped_frames;
2032 float delay = playback_speed*mpctx->audio_out->get_delay();
2033 float d = delay-mpctx->delay;
2034 // we should avoid dropping too many frames in sequence unless we
2035 // are too late. and we allow 100ms A-V delay here:
2036 if (d < -dropped_frames*frame_time-0.100 &&
2037 mpctx->osd_function != OSD_PAUSE) {
2038 drop_frame = frame_dropping;
2039 ++drop_frame_cnt;
2040 ++dropped_frames;
2041 } else
2042 drop_frame = dropped_frames = 0;
2043 ++total_frame_cnt;
2045 update_subtitles(sh_video, mpctx->d_sub, 0);
2046 update_teletext(sh_video, mpctx->demuxer, 0);
2047 update_osd_msg();
2048 current_module = "decode_video";
2049 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2050 sh_video->pts);
2051 current_module = "filter_video";
2052 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2053 sh_video->pts));
2055 else {
2056 if (!generate_video_frame(sh_video, mpctx->d_video))
2057 return -1;
2058 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2059 VFCTRL_GET_PTS, &sh_video->pts);
2060 if (sh_video->pts == MP_NOPTS_VALUE) {
2061 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2062 sh_video->pts = sh_video->last_pts;
2064 if (sh_video->last_pts == MP_NOPTS_VALUE)
2065 sh_video->last_pts= sh_video->pts;
2066 else if (sh_video->last_pts >= sh_video->pts) {
2067 sh_video->last_pts = sh_video->pts;
2068 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2070 frame_time = sh_video->pts - sh_video->last_pts;
2071 sh_video->last_pts = sh_video->pts;
2072 sh_video->timer += frame_time;
2073 if(mpctx->sh_audio)
2074 mpctx->delay -= frame_time;
2075 *blit_frame = 1;
2077 return frame_time;
2080 void pause_loop(void)
2082 mp_cmd_t* cmd;
2083 if (!quiet) {
2084 // Small hack to display the pause message on the OSD line.
2085 // The pause string is: "\n == PAUSE == \r" so we need to
2086 // take the first and the last char out
2087 if (term_osd && !mpctx->sh_video) {
2088 char msg[128] = MSGTR_Paused;
2089 int mlen = strlen(msg);
2090 msg[mlen-1] = '\0';
2091 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2092 update_osd_msg();
2093 } else
2094 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2095 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2097 #ifdef HAVE_NEW_GUI
2098 if (use_gui)
2099 guiGetEvent(guiCEvent, (char *)guiSetPause);
2100 #endif
2101 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2102 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2104 if (mpctx->audio_out && mpctx->sh_audio)
2105 mpctx->audio_out->pause(); // pause audio, keep data if possible
2107 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL) {
2108 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2109 mpctx->video_out->check_events();
2110 #ifdef HAVE_NEW_GUI
2111 if (use_gui) {
2112 guiEventHandling();
2113 guiGetEvent(guiReDraw, NULL);
2114 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2115 break;
2117 #endif
2118 #ifdef HAVE_MENU
2119 if (vf_menu)
2120 vf_menu_pause_update(vf_menu);
2121 #endif
2122 usec_sleep(20000);
2124 if (cmd && cmd->id == MP_CMD_PAUSE) {
2125 cmd = mp_input_get_cmd(0,1,0);
2126 mp_cmd_free(cmd);
2128 mpctx->osd_function=OSD_PLAY;
2129 if (mpctx->audio_out && mpctx->sh_audio)
2130 mpctx->audio_out->resume(); // resume audio
2131 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2132 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2133 (void)GetRelativeTime(); // ignore time that passed during pause
2134 #ifdef HAVE_NEW_GUI
2135 if (use_gui) {
2136 if (guiIntfStruct.Playing == guiSetStop)
2137 mpctx->eof = 1;
2138 else
2139 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2141 #endif
2144 void print_version(void){
2145 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2147 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2148 GetCpuCaps(&gCpuCaps);
2149 #ifdef ARCH_X86
2150 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2151 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2152 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2153 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2154 #ifdef RUNTIME_CPUDETECT
2155 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2156 #else
2157 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2158 #ifdef HAVE_MMX
2159 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2160 #endif
2161 #ifdef HAVE_MMX2
2162 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2163 #endif
2164 #ifdef HAVE_3DNOW
2165 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2166 #endif
2167 #ifdef HAVE_3DNOWEX
2168 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2169 #endif
2170 #ifdef HAVE_SSE
2171 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2172 #endif
2173 #ifdef HAVE_SSE2
2174 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2175 #endif
2176 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2177 #endif /* RUNTIME_CPUDETECT */
2178 #endif /* ARCH_X86 */
2182 // Find the right mute status and record position for new file position
2183 static void edl_seek_reset(MPContext *mpctx)
2185 mpctx->edl_muted = 0;
2186 next_edl_record = edl_records;
2188 while (next_edl_record) {
2189 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2190 break;
2192 if (next_edl_record->action == EDL_MUTE)
2193 mpctx->edl_muted = !mpctx->edl_muted;
2194 next_edl_record = next_edl_record->next;
2196 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2197 mixer_mute(&mpctx->mixer);
2201 // Execute EDL command for the current position if one exists
2202 static void edl_update(MPContext *mpctx)
2204 if (!next_edl_record)
2205 return;
2207 if (!mpctx->sh_video) {
2208 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2209 free_edl(edl_records);
2210 next_edl_record = NULL;
2211 edl_records = NULL;
2212 return;
2215 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2216 if (next_edl_record->action == EDL_SKIP) {
2217 mpctx->osd_function = OSD_FFW;
2218 abs_seek_pos = 0;
2219 rel_seek_secs = next_edl_record->length_sec;
2220 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2221 "[%f], length [%f]\n", next_edl_record->start_sec,
2222 next_edl_record->stop_sec, next_edl_record->length_sec);
2223 edl_decision = 1;
2225 else if (next_edl_record->action == EDL_MUTE) {
2226 mpctx->edl_muted = !mpctx->edl_muted;
2227 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2228 mixer_mute(&mpctx->mixer);
2229 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2230 next_edl_record->start_sec );
2232 next_edl_record = next_edl_record->next;
2237 // style & 1 == 0 means seek relative to current position, == 1 means absolute
2238 // style & 2 == 0 means amount in seconds, == 2 means fraction of file length
2239 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2240 static int seek(MPContext *mpctx, double amount, int style)
2242 current_module = "seek";
2243 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2244 return -1;
2246 if (mpctx->sh_video) {
2247 current_module = "seek_video_reset";
2248 resync_video_stream(mpctx->sh_video);
2249 if (vo_config_count)
2250 mpctx->video_out->control(VOCTRL_RESET, NULL);
2251 mpctx->sh_video->num_buffered_pts = 0;
2252 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2253 mpctx->num_buffered_frames = 0;
2254 mpctx->delay = 0;
2255 // Not all demuxers set d_video->pts during seek, so this value
2256 // (which is used by at least vobsub and edl code below) may
2257 // be completely wrong (probably 0).
2258 mpctx->sh_video->pts = mpctx->d_video->pts;
2259 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2260 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2263 if (mpctx->sh_audio) {
2264 current_module = "seek_audio_reset";
2265 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2266 mpctx->sh_audio->a_buffer_len = 0;
2267 mpctx->sh_audio->a_out_buffer_len = 0;
2270 if (vo_vobsub) {
2271 current_module = "seek_vobsub_reset";
2272 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2275 edl_seek_reset(mpctx);
2277 c_total = 0;
2278 max_pts_correction = 0.1;
2279 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2280 drop_frame_cnt = 0;
2282 current_module = NULL;
2283 return 0;
2286 int main(int argc,char* argv[]){
2289 char * mem_ptr;
2291 // movie info:
2293 /* Flag indicating whether MPlayer should exit without playing anything. */
2294 int opt_exit = 0;
2296 //float a_frame=0; // Audio
2298 int i;
2300 int gui_no_filename=0;
2302 srand((int) time(NULL));
2304 InitTimer();
2306 mp_msg_init();
2308 for(i=1; i<argc; i++)
2309 if(!strcmp(argv[i], "-really-quiet"))
2310 verbose= -10;
2312 print_version();
2313 #if defined(WIN32) && defined(USE_WIN32DLL)
2314 set_path_env();
2315 #endif /*WIN32 && USE_WIN32DLL*/
2317 #ifdef USE_TV
2318 stream_tv_defaults.immediate = 1;
2319 #endif
2321 if (argc > 1 && argv[1] &&
2322 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2323 use_gui = !strcmp(argv[1], "-gui");
2324 } else
2325 if ( argv[0] )
2327 char *base = strrchr(argv[0], '/');
2328 if (!base)
2329 base = strrchr(argv[0], '\\');
2330 if (!base)
2331 base = argv[0];
2332 if(strstr(base, "gmplayer"))
2333 use_gui=1;
2336 mconfig = m_config_new();
2337 m_config_register_options(mconfig,mplayer_opts);
2338 // TODO : add something to let modules register their options
2339 mp_input_register_options(mconfig);
2340 parse_cfgfiles(mconfig);
2342 #ifdef HAVE_NEW_GUI
2343 if ( use_gui ) cfg_read();
2344 #endif
2346 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2347 if(mpctx->playtree == NULL)
2348 opt_exit = 1;
2349 else {
2350 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2351 if(mpctx->playtree) {
2352 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2353 if(mpctx->playtree_iter) {
2354 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2355 play_tree_iter_free(mpctx->playtree_iter);
2356 mpctx->playtree_iter = NULL;
2358 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2363 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2364 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2365 if(runningmplayer && filename && use_gui){
2366 COPYDATASTRUCT csData;
2367 char file[MAX_PATH];
2368 char *filepart = filename;
2369 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2370 csData.dwData = 0;
2371 csData.cbData = strlen(file)*2;
2372 csData.lpData = file;
2373 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2376 #endif
2378 #ifdef WIN32
2379 if(proc_priority){
2380 int i;
2381 for(i=0; priority_presets_defs[i].name; i++){
2382 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2383 break;
2385 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2386 priority_presets_defs[i].name);
2387 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2389 #endif
2390 #ifndef HAVE_NEW_GUI
2391 if(use_gui){
2392 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2393 use_gui=0;
2395 #else
2396 #ifndef WIN32
2397 if(use_gui && !vo_init()){
2398 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2399 use_gui=0;
2401 #endif
2402 if (use_gui && mpctx->playtree_iter){
2403 char cwd[PATH_MAX+2];
2404 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2405 play_tree_iter_free(mpctx->playtree_iter);
2406 mpctx->playtree_iter=NULL;
2408 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2410 strcat(cwd, "/");
2411 // Prefix relative paths with current working directory
2412 play_tree_add_bpf(mpctx->playtree, cwd);
2414 // Import initital playtree into GUI.
2415 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2417 #endif /* HAVE_NEW_GUI */
2419 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2420 list_video_out();
2421 opt_exit = 1;
2424 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2425 list_audio_out();
2426 opt_exit = 1;
2429 /* Check codecs.conf. */
2430 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2431 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2432 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2433 if(!parse_codec_cfg(NULL)){
2434 exit_player_with_rc(NULL, 0);
2436 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2439 free( mem_ptr ); // release the buffer created by get_path()
2442 #if 0
2443 if(video_codec_list){
2444 int i;
2445 video_codec=video_codec_list[0];
2446 for(i=0;video_codec_list[i];i++)
2447 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2449 #endif
2450 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2451 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2452 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2453 list_codecs(1);
2454 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2455 opt_exit = 1;
2457 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2458 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2459 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2460 list_codecs(0);
2461 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2462 opt_exit = 1;
2464 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2465 vfm_help();
2466 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2467 opt_exit = 1;
2469 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2470 afm_help();
2471 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2472 opt_exit = 1;
2474 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2475 af_help();
2476 printf("\n");
2477 opt_exit = 1;
2479 #ifdef HAVE_X11
2480 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2481 fstype_help();
2482 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2483 opt_exit = 1;
2485 #endif
2486 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2487 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2488 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2489 demuxer_help();
2490 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2491 opt_exit = 1;
2493 if(list_properties) {
2494 property_print_help();
2495 opt_exit = 1;
2498 if(opt_exit)
2499 exit_player(NULL);
2501 if (player_idle_mode && use_gui) {
2502 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2503 exit_player_with_rc(NULL, 1);
2506 if(!filename && !player_idle_mode){
2507 if(!use_gui){
2508 // no file/vcd/dvd -> show HELP:
2509 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2510 exit_player_with_rc(NULL, 0);
2511 } else gui_no_filename=1;
2514 /* Display what configure line was used */
2515 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2517 // Many users forget to include command line in bugreports...
2518 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2519 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2520 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2521 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2524 //------ load global data first ------
2526 // check font
2527 #ifdef HAVE_FREETYPE
2528 init_freetype();
2529 #endif
2530 #ifdef HAVE_FONTCONFIG
2531 if(!font_fontconfig)
2533 #endif
2534 #ifdef HAVE_BITMAP_FONT
2535 if(font_name){
2536 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2537 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2538 filename_recode(font_name));
2539 } else {
2540 // try default:
2541 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2542 free(mem_ptr); // release the buffer created by get_path()
2543 if(!vo_font)
2544 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2546 if (sub_font_name)
2547 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2548 else
2549 sub_font = vo_font;
2550 #endif
2551 #ifdef HAVE_FONTCONFIG
2553 #endif
2555 vo_init_osd();
2557 #ifdef USE_ASS
2558 ass_library = ass_init();
2559 #endif
2561 #ifdef HAVE_RTC
2562 if(!nortc)
2564 // seteuid(0); /* Can't hurt to try to get root here */
2565 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2566 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2567 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2568 else {
2569 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2571 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2572 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2573 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2574 close (rtc_fd);
2575 rtc_fd = -1;
2576 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2577 /* variable only by the root */
2578 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2579 close (rtc_fd);
2580 rtc_fd = -1;
2581 } else
2582 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2585 #ifdef HAVE_NEW_GUI
2586 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2587 // and now ? -- Pontscho
2588 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2589 #endif
2590 if(rtc_fd<0)
2591 #endif /* HAVE_RTC */
2592 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2593 softsleep?"software":timer_name);
2595 #ifdef USE_TERMCAP
2596 if ( !use_gui ) load_termcap(NULL); // load key-codes
2597 #endif
2599 // ========== Init keyboard FIFO (connection to libvo) ============
2601 // Init input system
2602 current_module = "init_input";
2603 mp_input_init(use_gui);
2604 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2605 if(slave_mode)
2606 #ifndef __MINGW32__
2607 mp_input_add_cmd_fd(0,1,NULL,NULL);
2608 #else
2609 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
2610 #endif
2611 else if(!noconsolecontrols)
2612 mp_input_add_event_fd(0, getch2);
2614 inited_flags|=INITED_INPUT;
2615 current_module = NULL;
2617 #ifdef HAVE_MENU
2618 if(use_menu) {
2619 if(menu_cfg && menu_init(mpctx, menu_cfg))
2620 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2621 else {
2622 menu_cfg = get_path("menu.conf");
2623 if(menu_init(mpctx, menu_cfg))
2624 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2625 else {
2626 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2627 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2628 else {
2629 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2630 use_menu = 0;
2635 #endif
2637 /// Catch signals
2638 #ifndef __MINGW32__
2639 signal(SIGCHLD,child_sighandler);
2640 #endif
2642 #ifdef CRASH_DEBUG
2643 prog_path = argv[0];
2644 #endif
2645 //========= Catch terminate signals: ================
2646 // terminate requests:
2647 signal(SIGTERM,exit_sighandler); // kill
2648 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2650 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2652 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2653 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2654 #ifdef ENABLE_SIGHANDLER
2655 // fatal errors:
2656 signal(SIGBUS,exit_sighandler); // bus error
2657 signal(SIGSEGV,exit_sighandler); // segfault
2658 signal(SIGILL,exit_sighandler); // illegal instruction
2659 signal(SIGFPE,exit_sighandler); // floating point exc.
2660 signal(SIGABRT,exit_sighandler); // abort()
2661 #ifdef CRASH_DEBUG
2662 if (crash_debug)
2663 signal(SIGTRAP,exit_sighandler);
2664 #endif
2665 #endif
2667 #ifdef HAVE_NEW_GUI
2668 if(use_gui){
2669 guiInit();
2670 guiGetEvent(guiSetContext, mpctx);
2671 inited_flags|=INITED_GUI;
2672 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2674 #endif
2676 // ******************* Now, let's see the per-file stuff ********************
2678 play_next_file:
2680 // init global sub numbers
2681 mpctx->global_sub_size = 0;
2682 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2684 if (filename) load_per_file_config (mconfig, filename);
2686 // We must enable getch2 here to be able to interrupt network connection
2687 // or cache filling
2688 if(!noconsolecontrols && !slave_mode){
2689 if(inited_flags&INITED_GETCH2)
2690 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2691 else
2692 getch2_enable(); // prepare stdin for hotkeys...
2693 inited_flags|=INITED_GETCH2;
2694 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2697 // =================== GUI idle loop (STOP state) ===========================
2698 #ifdef HAVE_NEW_GUI
2699 if ( use_gui ) {
2700 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2701 guiGetEvent( guiSetDefaults,0 );
2702 while ( guiIntfStruct.Playing != 1 )
2704 mp_cmd_t* cmd;
2705 usec_sleep(20000);
2706 guiEventHandling();
2707 guiGetEvent( guiReDraw,NULL );
2708 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2709 guiGetEvent(guiIEvent, (char *)cmd->id);
2710 mp_cmd_free(cmd);
2713 guiGetEvent( guiSetParameters,NULL );
2714 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2716 play_tree_t * entry = play_tree_new();
2717 play_tree_add_file( entry,guiIntfStruct.Filename );
2718 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2719 else mpctx->playtree=play_tree_new();
2720 play_tree_set_child( mpctx->playtree,entry );
2721 if(mpctx->playtree)
2723 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2724 if(mpctx->playtree_iter)
2726 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2728 play_tree_iter_free(mpctx->playtree_iter);
2729 mpctx->playtree_iter = NULL;
2731 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2736 #endif /* HAVE_NEW_GUI */
2738 while (player_idle_mode && !filename) {
2739 play_tree_t * entry = NULL;
2740 mp_cmd_t * cmd;
2741 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
2742 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
2743 usec_sleep(20000);
2745 switch (cmd->id) {
2746 case MP_CMD_LOADFILE:
2747 // prepare a tree entry with the new filename
2748 entry = play_tree_new();
2749 play_tree_add_file(entry, cmd->args[0].v.s);
2750 // The entry is added to the main playtree after the switch().
2751 break;
2752 case MP_CMD_LOADLIST:
2753 entry = parse_playlist_file(cmd->args[0].v.s);
2754 break;
2755 case MP_CMD_QUIT:
2756 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2757 break;
2760 mp_cmd_free(cmd);
2762 if (entry) { // user entered a command that gave a valid entry
2763 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
2764 play_tree_free_list(mpctx->playtree->child, 1);
2765 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
2767 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
2769 play_tree_set_child(mpctx->playtree, entry);
2771 /* Make iterator start at the top the of tree. */
2772 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
2773 if (!mpctx->playtree_iter) continue;
2775 // find the first real item in the tree
2776 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2777 // no items!
2778 play_tree_iter_free(mpctx->playtree_iter);
2779 mpctx->playtree_iter = NULL;
2780 continue; // wait for next command
2782 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
2785 //---------------------------------------------------------------------------
2787 if(filename)
2788 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
2789 filename_recode(filename));
2791 if (edl_filename) {
2792 if (edl_records) free_edl(edl_records);
2793 next_edl_record = edl_records = edl_parse_file();
2795 if (edl_output_filename) {
2796 if (edl_fd) fclose(edl_fd);
2797 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
2799 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
2800 filename_recode(edl_output_filename));
2804 //==================== Open VOB-Sub ============================
2806 current_module="vobsub";
2807 if (vobsub_name){
2808 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
2809 if(vo_vobsub==NULL)
2810 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
2811 filename_recode(vobsub_name));
2812 }else if(sub_auto && filename && (strlen(filename)>=5)){
2813 /* try to autodetect vobsub from movie filename ::atmos */
2814 char *buf = malloc((strlen(filename)-3)),*psub;
2815 memset(buf,0,strlen(filename)-3); // make sure string is terminated
2816 strncpy(buf, filename, strlen(filename)-4);
2817 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
2818 /* try from ~/.mplayer/sub */
2819 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
2820 char *bname;
2821 int l;
2822 bname = strrchr(buf,'/');
2823 #ifdef WIN32
2824 if(!bname) bname = strrchr(buf,'\\');
2825 #endif
2826 if(bname) bname++;
2827 else bname = buf;
2828 l = strlen(psub) + strlen(bname) + 1;
2829 psub = realloc(psub,l);
2830 strcat(psub,bname);
2831 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
2832 free(psub);
2834 free(buf);
2836 if(vo_vobsub){
2837 inited_flags|=INITED_VOBSUB;
2838 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
2839 // check if vobsub requested only to display forced subtitles
2840 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
2842 // setup global sub numbering
2843 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
2844 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
2847 //============ Open & Sync STREAM --- fork cache2 ====================
2849 mpctx->stream=NULL;
2850 mpctx->demuxer=NULL;
2851 if (mpctx->d_audio) {
2852 //free_demuxer_stream(mpctx->d_audio);
2853 mpctx->d_audio=NULL;
2855 if (mpctx->d_video) {
2856 //free_demuxer_stream(d_video);
2857 mpctx->d_video=NULL;
2859 mpctx->sh_audio=NULL;
2860 mpctx->sh_video=NULL;
2862 current_module="open_stream";
2863 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
2864 if(!mpctx->stream) { // error...
2865 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
2866 goto goto_next_file;
2868 inited_flags|=INITED_STREAM;
2870 #ifdef HAVE_NEW_GUI
2871 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
2872 #endif
2874 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
2875 play_tree_t* entry;
2876 // Handle playlist
2877 current_module="handle_playlist";
2878 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
2879 filename_recode(filename));
2880 entry = parse_playtree(mpctx->stream,0);
2881 mpctx->eof=playtree_add_playlist(entry);
2882 goto goto_next_file;
2884 mpctx->stream->start_pos+=seek_to_byte;
2886 if(stream_dump_type==5){
2887 unsigned char buf[4096];
2888 int len;
2889 FILE *f;
2890 current_module="dumpstream";
2891 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
2892 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
2893 exit_player(MSGTR_Exit_error);
2895 stream_reset(mpctx->stream);
2896 stream_seek(mpctx->stream,mpctx->stream->start_pos);
2897 f=fopen(stream_dump_name,"wb");
2898 if(!f){
2899 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
2900 exit_player(MSGTR_Exit_error);
2902 while(!mpctx->stream->eof){
2903 len=stream_read(mpctx->stream,buf,4096);
2904 if(len>0) {
2905 if(fwrite(buf,len,1,f) != 1) {
2906 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2907 exit_player(MSGTR_Exit_error);
2911 if(fclose(f)) {
2912 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2913 exit_player(MSGTR_Exit_error);
2915 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
2916 exit_player_with_rc(MSGTR_Exit_eof, 0);
2919 #ifdef USE_DVDREAD
2920 if(mpctx->stream->type==STREAMTYPE_DVD){
2921 current_module="dvd lang->id";
2922 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
2923 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2924 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
2925 // setup global sub numbering
2926 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2927 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
2928 current_module=NULL;
2930 #endif
2932 #ifdef USE_DVDNAV
2933 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
2934 current_module="dvdnav lang->id";
2935 if(audio_id==-1) audio_id=dvdnav_aid_from_lang(mpctx->stream,audio_lang);
2936 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2937 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
2938 // setup global sub numbering
2939 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2940 mpctx->global_sub_size += dvdnav_number_of_subs(mpctx->stream);
2941 current_module=NULL;
2943 #endif
2945 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
2946 goto_enable_cache:
2947 if(stream_cache_size>0){
2948 current_module="enable_cache";
2949 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
2950 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
2951 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
2952 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
2955 //============ Open DEMUXERS --- DETECT file type =======================
2956 current_module="demux_open";
2958 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
2960 // HACK to get MOV Reference Files working
2962 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
2964 unsigned char* playlist_entry;
2965 play_tree_t *list = NULL, *entry = NULL;
2967 current_module="handle_demux_playlist";
2968 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
2970 char *temp, *bname;
2972 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
2973 filename_recode(playlist_entry));
2975 bname=mp_basename(playlist_entry);
2976 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
2977 continue;
2979 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
2980 continue;
2982 entry = play_tree_new();
2984 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
2986 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
2987 if (temp)
2989 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
2990 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
2991 strcat(temp, playlist_entry);
2992 play_tree_add_file(entry,temp);
2993 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
2994 free(temp);
2997 else
2998 play_tree_add_file(entry,playlist_entry);
3000 if(!list)
3001 list = entry;
3002 else
3003 play_tree_append_entry(list,entry);
3005 free_demuxer(mpctx->demuxer);
3006 mpctx->demuxer = NULL;
3008 if (list)
3010 entry = play_tree_new();
3011 play_tree_set_child(entry,list);
3012 mpctx->eof=playtree_add_playlist(entry);
3013 goto goto_next_file;
3017 if(!mpctx->demuxer)
3018 goto goto_next_file;
3019 inited_flags|=INITED_DEMUXER;
3021 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3022 int i;
3023 int maxid = -1;
3024 // setup global sub numbering
3025 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3026 for (i = 0; i < MAX_S_STREAMS; i++)
3027 if (mpctx->demuxer->s_streams[i])
3028 maxid = FFMAX(maxid, ((sh_sub_t *)mpctx->demuxer->s_streams[i])->sid);
3029 mpctx->global_sub_size += maxid + 1;
3031 // Make dvdsub_id always selectable if set.
3032 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id)
3033 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id + 1;
3035 current_module="demux_open2";
3037 //file_format=demuxer->file_format;
3039 mpctx->d_audio=mpctx->demuxer->audio;
3040 mpctx->d_video=mpctx->demuxer->video;
3041 mpctx->d_sub=mpctx->demuxer->sub;
3043 // DUMP STREAMS:
3044 if((stream_dump_type)&&(stream_dump_type<4)){
3045 FILE *f;
3046 demux_stream_t *ds=NULL;
3047 current_module="dump";
3048 // select stream to dump
3049 switch(stream_dump_type){
3050 case 1: ds=mpctx->d_audio;break;
3051 case 2: ds=mpctx->d_video;break;
3052 case 3: ds=mpctx->d_sub;break;
3054 if(!ds){
3055 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3056 exit_player(MSGTR_Exit_error);
3058 // disable other streams:
3059 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3060 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3061 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3062 // let's dump it!
3063 f=fopen(stream_dump_name,"wb");
3064 if(!f){
3065 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3066 exit_player(MSGTR_Exit_error);
3068 while(!ds->eof){
3069 unsigned char* start;
3070 int in_size=ds_get_packet(ds,&start);
3071 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3072 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3073 if(in_size>0) fwrite(start,in_size,1,f);
3075 fclose(f);
3076 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3077 exit_player_with_rc(MSGTR_Exit_eof, 0);
3080 mpctx->sh_audio=mpctx->d_audio->sh;
3081 mpctx->sh_video=mpctx->d_video->sh;
3083 if(mpctx->sh_video){
3085 current_module="video_read_properties";
3086 if(!video_read_properties(mpctx->sh_video)) {
3087 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3088 mpctx->sh_video=mpctx->d_video->sh=NULL;
3089 } else {
3090 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
3091 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3092 mpctx->sh_video->fps,mpctx->sh_video->frametime
3095 /* need to set fps here for output encoders to pick it up in their init */
3096 if(force_fps){
3097 mpctx->sh_video->fps=force_fps;
3098 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3100 vo_fps = mpctx->sh_video->fps;
3102 if(!mpctx->sh_video->fps && !force_fps){
3103 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3104 mpctx->sh_video=mpctx->d_video->sh=NULL;
3110 if(!mpctx->sh_video && !mpctx->sh_audio){
3111 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3112 #ifdef HAS_DVBIN_SUPPORT
3113 if((mpctx->stream->type == STREAMTYPE_DVB) && mpctx->stream->priv)
3115 dvb_priv_t *priv = (dvb_priv_t*) mpctx->stream->priv;
3116 if(priv->is_on)
3118 int dir;
3119 int v = mpctx->last_dvb_step;
3120 if(v > 0)
3121 dir = DVB_CHANNEL_HIGHER;
3122 else
3123 dir = DVB_CHANNEL_LOWER;
3125 if(dvb_step_channel(priv, dir))
3126 mpctx->eof = mpctx->dvbin_reopen = 1;
3129 #endif
3130 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3133 /* display clip info */
3134 demux_info_print(mpctx->demuxer);
3136 //================== Read SUBTITLES (DVD & TEXT) ==========================
3137 if(vo_spudec==NULL && mpctx->sh_video &&
3138 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3139 init_vo_spudec();
3142 // Apply current settings for forced subs
3143 if (vo_spudec!=NULL)
3144 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3146 if(mpctx->sh_video) {
3147 // after reading video params we should load subtitles because
3148 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3149 // check .sub
3150 current_module="read_subtitles_file";
3151 if(sub_name){
3152 for (i = 0; sub_name[i] != NULL; ++i)
3153 add_subtitles (sub_name[i], mpctx->sh_video->fps, 0);
3155 if(sub_auto) { // auto load sub file ...
3156 char *psub = get_path( "sub/" );
3157 char **tmp = sub_filenames((psub ? psub : ""), filename);
3158 int i = 0;
3159 free(psub); // release the buffer created by get_path() above
3160 while (tmp[i]) {
3161 add_subtitles (tmp[i], mpctx->sh_video->fps, 1);
3162 free(tmp[i++]);
3164 free(tmp);
3166 if (mpctx->set_of_sub_size > 0) {
3167 // setup global sub numbering
3168 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3169 mpctx->global_sub_size += mpctx->set_of_sub_size;
3173 if (mpctx->global_sub_size) {
3174 // find the best sub to use
3175 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3176 if (vobsub_index_id >= 0) {
3177 // if user asks for a vobsub id, use that first.
3178 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3179 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3180 // if user asks for a dvd sub id, use that next.
3181 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
3182 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3183 // if there are text subs to use, use those. (autosubs come last here)
3184 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3186 } else if (mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3187 // if nothing else works, get subs from the demuxer.
3188 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX];
3190 } else {
3191 // nothing worth doing automatically.
3192 mpctx->global_sub_pos = -1;
3194 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3195 mpctx->global_sub_pos--;
3196 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3197 if(subdata)
3198 switch (stream_dump_type) {
3199 case 3: list_sub_file(subdata); break;
3200 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3201 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3202 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3203 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3204 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3208 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3209 filename_recode(filename));
3210 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3211 if (mpctx->sh_video) {
3212 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3213 if (mpctx->sh_video->format >= 0x20202020)
3214 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3215 else
3216 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3217 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3218 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3219 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3220 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3221 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3223 if (mpctx->sh_audio) {
3224 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3225 if (mpctx->sh_audio->format >= 0x20202020)
3226 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3227 else
3228 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3229 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3230 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3231 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3233 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3235 if(!mpctx->sh_video) goto main; // audio-only
3237 if(!reinit_video_chain()) {
3238 if(!mpctx->sh_video){
3239 if(!mpctx->sh_audio) goto goto_next_file;
3240 goto main; // exit_player(MSGTR_Exit_error);
3244 if(vo_flags & 0x08 && vo_spudec)
3245 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3247 #ifdef HAVE_FREETYPE
3248 force_load_font = 1;
3249 #endif
3251 //================== MAIN: ==========================
3252 main:
3253 current_module="main";
3255 if(playing_msg) {
3256 char* msg = property_expand_string(mpctx, playing_msg);
3257 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3258 free(msg);
3262 // Disable the term OSD in verbose mode
3263 if(verbose) term_osd = 0;
3266 //int frame_corr_num=0; //
3267 //float v_frame=0; // Video
3268 float time_frame=0; // Timer
3269 //float num_frames=0; // number of frames played
3271 int frame_time_remaining=0; // flag
3272 int blit_frame=0;
3273 mpctx->num_buffered_frames=0;
3275 // Make sure old OSD does not stay around,
3276 // e.g. with -fixed-vo and same-resolution files
3277 clear_osd_msgs();
3278 update_osd_msg();
3280 //================ SETUP AUDIO ==========================
3282 if(mpctx->sh_audio){
3283 reinit_audio_chain();
3284 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3285 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3288 current_module="av_init";
3290 if(mpctx->sh_video){
3291 mpctx->sh_video->timer=0;
3292 if (! ignore_start)
3293 audio_delay += mpctx->sh_video->stream_delay;
3295 if(mpctx->sh_audio){
3296 if (! ignore_start)
3297 audio_delay -= mpctx->sh_audio->stream_delay;
3298 mpctx->delay=-audio_delay;
3301 if(!mpctx->sh_audio){
3302 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3303 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3304 ds_free_packs(mpctx->d_audio); // free buffered chunks
3305 //mpctx->d_audio->id=-2; // do not read audio chunks
3306 //uninit_player(INITED_AO); // close device
3308 if(!mpctx->sh_video){
3309 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3310 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3311 ds_free_packs(mpctx->d_video);
3312 mpctx->d_video->id=-2;
3313 //if(!fixed_vo) uninit_player(INITED_VO);
3316 if (!mpctx->sh_video && !mpctx->sh_audio)
3317 goto goto_next_file;
3319 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3320 if(force_fps && mpctx->sh_video){
3321 vo_fps = mpctx->sh_video->fps=force_fps;
3322 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3323 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3326 #ifdef HAVE_NEW_GUI
3327 if ( use_gui ) {
3328 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3329 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3330 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3331 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3332 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3334 #endif
3336 mp_input_set_section(NULL);
3337 //TODO: add desired (stream-based) sections here
3338 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3340 //==================== START PLAYING =======================
3342 if(mpctx->loop_times>1) mpctx->loop_times--; else
3343 if(mpctx->loop_times==1) mpctx->loop_times = -1;
3345 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3347 total_time_usage_start=GetTimer();
3348 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3349 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3350 play_n_frames=play_n_frames_mf;
3352 if(play_n_frames==0){
3353 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3356 if (seek_to_sec) {
3357 seek(mpctx, seek_to_sec, 1);
3358 end_at.pos += seek_to_sec;
3361 if (end_at.type == END_AT_SIZE) {
3362 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3363 end_at.type = END_AT_NONE;
3367 while(!mpctx->eof){
3368 float aq_sleep_time=0;
3369 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3370 mpctx->sh_audio = mpctx->d_audio->sh;
3371 mpctx->sh_audio->ds = mpctx->d_audio;
3372 reinit_audio_chain();
3375 /*========================== PLAY AUDIO ============================*/
3377 if (mpctx->sh_audio)
3378 if (!fill_audio_out_buffers())
3379 // at eof, all audio at least written to ao
3380 if (!mpctx->sh_video)
3381 mpctx->eof = PT_NEXT_ENTRY;
3384 if(!mpctx->sh_video) {
3385 // handle audio-only case:
3386 double a_pos=0;
3387 if(!quiet || end_at.type == END_AT_TIME )
3388 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3390 if(!quiet)
3391 print_status(a_pos, 0, 0);
3393 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3394 mpctx->eof = PT_NEXT_ENTRY;
3395 update_osd_msg();
3397 } else {
3399 /*========================== PLAY VIDEO ============================*/
3401 vo_pts=mpctx->sh_video->timer*90000.0;
3402 vo_fps=mpctx->sh_video->fps;
3404 if (!mpctx->num_buffered_frames) {
3405 double frame_time = update_video(&blit_frame);
3406 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3407 if (mpctx->sh_video->vf_inited < 0) {
3408 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3409 mpctx->eof = 1; goto goto_next_file;
3411 if (frame_time < 0)
3412 mpctx->eof = 1;
3413 else {
3414 // might return with !eof && !blit_frame if !correct_pts
3415 mpctx->num_buffered_frames += blit_frame;
3416 time_frame += frame_time / playback_speed; // for nosound
3420 // ==========================================================================
3422 // current_module="draw_osd";
3423 // if(vo_config_count) mpctx->video_out->draw_osd();
3425 #ifdef HAVE_NEW_GUI
3426 if(use_gui) guiEventHandling();
3427 #endif
3429 current_module="vo_check_events";
3430 if (vo_config_count) mpctx->video_out->check_events();
3432 #ifdef HAVE_X11
3433 if (stop_xscreensaver) {
3434 current_module = "stop_xscreensaver";
3435 xscreensaver_heartbeat();
3437 #endif
3439 frame_time_remaining = sleep_until_update(&time_frame, &aq_sleep_time);
3441 //====================== FLIP PAGE (VIDEO BLT): =========================
3443 current_module="flip_page";
3444 if (!frame_time_remaining && blit_frame) {
3445 unsigned int t2=GetTimer();
3447 if(vo_config_count) mpctx->video_out->flip_page();
3448 mpctx->num_buffered_frames--;
3450 vout_time_usage += (GetTimer() - t2) * 0.000001;
3452 //====================== A-V TIMESTAMP CORRECTION: =========================
3454 adjust_sync_and_print_status(frame_time_remaining, time_frame);
3456 //============================ Auto QUALITY ============================
3458 /*Output quality adjustments:*/
3459 if(auto_quality>0){
3460 current_module="autoq";
3461 // float total=0.000001f * (GetTimer()-aq_total_time);
3462 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3463 if(output_quality<auto_quality && aq_sleep_time>0)
3464 ++output_quality;
3465 else
3466 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3467 if(output_quality>1 && aq_sleep_time<0)
3468 --output_quality;
3469 else
3470 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3471 output_quality=0;
3472 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3473 set_video_quality(mpctx->sh_video,output_quality);
3476 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3477 --play_n_frames;
3478 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3482 // FIXME: add size based support for -endpos
3483 if (end_at.type == END_AT_TIME &&
3484 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3485 mpctx->eof = PT_NEXT_ENTRY;
3487 } // end if(mpctx->sh_video)
3489 #ifdef USE_DVDNAV
3490 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3491 nav_highlight_t hl;
3492 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3493 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3494 vo_osd_changed (OSDTYPE_DVDNAV);
3496 #endif
3498 //============================ Handle PAUSE ===============================
3500 current_module="pause";
3502 if (mpctx->osd_function == OSD_PAUSE) {
3503 pause_loop();
3504 mpctx->was_paused = 1;
3507 // handle -sstep
3508 if(step_sec>0) {
3509 mpctx->osd_function=OSD_FFW;
3510 rel_seek_secs+=step_sec;
3513 edl_update(mpctx);
3515 //================= Keyboard events, SEEKing ====================
3517 current_module="key_events";
3520 mp_cmd_t* cmd;
3521 int brk_cmd = 0;
3522 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3523 brk_cmd = run_command(mpctx, cmd);
3524 mp_cmd_free(cmd);
3525 if (brk_cmd == 2)
3526 goto goto_enable_cache;
3529 mpctx->was_paused = 0;
3531 /* Looping. */
3532 if(mpctx->eof==1 && mpctx->loop_times>=0) {
3533 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", mpctx->loop_times,mpctx->eof);
3535 if(mpctx->loop_times>1) mpctx->loop_times--; else
3536 if(mpctx->loop_times==1) mpctx->loop_times=-1;
3537 play_n_frames=play_n_frames_mf;
3538 mpctx->eof=0;
3539 abs_seek_pos=1; rel_seek_secs=seek_to_sec;
3540 loop_seek = 1;
3543 if(rel_seek_secs || abs_seek_pos){
3544 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3545 // Set OSD:
3546 if(!loop_seek){
3547 if( !edl_decision )
3548 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3552 rel_seek_secs=0;
3553 abs_seek_pos=0;
3554 loop_seek=0;
3555 edl_decision = 0;
3558 #ifdef HAVE_NEW_GUI
3559 if(use_gui){
3560 guiEventHandling();
3561 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3562 // get pos from frame number / total frames
3563 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3564 } else {
3565 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3567 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3568 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3569 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3570 guiGetEvent( guiReDraw,NULL );
3571 guiGetEvent( guiSetVolume,NULL );
3572 if(guiIntfStruct.Playing==0) break; // STOP
3573 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3574 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3575 #ifdef USE_DVDREAD
3576 if ( mpctx->stream->type == STREAMTYPE_DVD )
3578 dvd_priv_t * dvdp = mpctx->stream->priv;
3579 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3581 #endif
3583 #endif /* HAVE_NEW_GUI */
3585 } // while(!mpctx->eof)
3587 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3589 #ifdef HAS_DVBIN_SUPPORT
3590 if(mpctx->dvbin_reopen)
3592 mpctx->eof = 0;
3593 uninit_player(INITED_ALL-(INITED_GUI|INITED_STREAM|INITED_INPUT|INITED_GETCH2|(fixed_vo?INITED_VO:0)));
3594 cache_uninit(mpctx->stream);
3595 mpctx->dvbin_reopen = 0;
3596 goto goto_enable_cache;
3598 #endif
3601 goto_next_file: // don't jump here after ao/vo/getch initialization!
3603 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3605 if(benchmark){
3606 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3607 double total_time_usage;
3608 total_time_usage_start=GetTimer()-total_time_usage_start;
3609 total_time_usage = (float)total_time_usage_start*0.000001;
3610 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3611 video_time_usage,vout_time_usage,audio_time_usage,
3612 total_time_usage-tot,total_time_usage);
3613 if(total_time_usage>0.0)
3614 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3615 100.0*video_time_usage/total_time_usage,
3616 100.0*vout_time_usage/total_time_usage,
3617 100.0*audio_time_usage/total_time_usage,
3618 100.0*(total_time_usage-tot)/total_time_usage,
3619 100.0);
3620 if(total_frame_cnt && frame_dropping)
3621 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3622 total_frame_cnt-drop_frame_cnt,
3623 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3624 drop_frame_cnt,
3625 100*drop_frame_cnt/total_frame_cnt,
3626 total_frame_cnt,
3627 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3631 // time to uninit all, except global stuff:
3632 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
3634 if ( mpctx->set_of_sub_size > 0 )
3636 current_module="sub_free";
3637 for (i = 0; i < mpctx->set_of_sub_size; ++i) {
3638 sub_free( mpctx->set_of_subtitles[i] );
3639 #ifdef USE_ASS
3640 if ( mpctx->set_of_ass_tracks[i] )
3641 ass_free_track( mpctx->set_of_ass_tracks[i] );
3642 #endif
3644 mpctx->set_of_sub_size = 0;
3646 vo_sub_last = vo_sub=NULL;
3647 subdata=NULL;
3648 #ifdef USE_ASS
3649 ass_track = NULL;
3650 #endif
3652 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
3653 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
3654 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
3655 mpctx->eof = 1;
3656 } else {
3657 play_tree_iter_free(mpctx->playtree_iter);
3658 mpctx->playtree_iter = NULL;
3660 mpctx->play_tree_step = 1;
3661 } else if (mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
3662 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
3663 if ( mpctx->playtree_iter ) {
3664 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
3665 mpctx->eof = 1;
3666 } else {
3667 play_tree_iter_free(mpctx->playtree_iter);
3668 mpctx->playtree_iter = NULL;
3671 } else { // NEXT PREV SRC
3672 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
3675 if(mpctx->eof == 0) mpctx->eof = 1;
3677 while(mpctx->playtree_iter != NULL) {
3678 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
3679 if(filename == NULL) {
3680 if( play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
3681 play_tree_iter_free(mpctx->playtree_iter);
3682 mpctx->playtree_iter = NULL;
3684 } else
3685 break;
3688 #ifdef HAVE_NEW_GUI
3689 if( use_gui && !mpctx->playtree_iter )
3691 #ifdef USE_DVDREAD
3692 if ( !guiIntfStruct.DiskChanged )
3693 #endif
3694 mplEnd();
3696 #endif
3698 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
3699 if (!mpctx->playtree_iter) filename = NULL;
3700 mpctx->eof = 0;
3701 goto play_next_file;
3705 exit_player_with_rc(MSGTR_Exit_eof, 0);
3707 return 1;