Merge svn changes up to 27824
[mplayer.git] / mplayer.c
blob320107d46029c186b87bd8e6c73c800749af720c
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
8 #include "talloc.h"
10 #if defined(__MINGW32__) || defined(__CYGWIN__)
11 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
12 #include <windows.h>
13 #endif
14 #include <string.h>
15 #include <unistd.h>
17 // #include <sys/mman.h>
18 #include <sys/types.h>
19 #ifndef __MINGW32__
20 #include <sys/ioctl.h>
21 #include <sys/wait.h>
22 #else
23 #define SIGHUP 1 /* hangup */
24 #define SIGQUIT 3 /* quit */
25 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
26 #define SIGBUS 10 /* bus error */
27 #define SIGPIPE 13 /* broken pipe */
28 #endif
30 #include <sys/time.h>
31 #include <sys/stat.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include <errno.h>
40 #include "version.h"
42 #include "mp_msg.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "mplayer.h"
50 #include "access_mpcontext.h"
51 #include "m_property.h"
53 #include "cfg-mplayer-def.h"
55 #include "libavutil/intreadwrite.h"
57 #include "subreader.h"
59 #include "mp_osd.h"
60 #include "libvo/video_out.h"
62 #include "libvo/font_load.h"
63 #include "libvo/sub.h"
65 #ifdef CONFIG_X11
66 #include "libvo/x11_common.h"
67 #endif
69 #include "libao2/audio_out.h"
71 #include "codec-cfg.h"
73 #include "edl.h"
75 #include "spudec.h"
76 #include "vobsub.h"
78 #include "osdep/getch2.h"
79 #include "osdep/timer.h"
81 #include "cpudetect.h"
83 #ifdef CONFIG_GUI
84 #include "gui/interface.h"
85 #endif
87 #include "input/input.h"
89 int slave_mode=0;
90 int player_idle_mode=0;
91 int quiet=0;
92 int enable_mouse_movements=0;
94 #if defined(__MINGW32__) || defined(__CYGWIN__)
95 char * proc_priority=NULL;
96 #endif
98 char *heartbeat_cmd;
100 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
102 #ifdef HAVE_RTC
103 #ifdef __linux__
104 #include <linux/rtc.h>
105 #else
106 #include <rtc.h>
107 #define RTC_IRQP_SET RTCIO_IRQP_SET
108 #define RTC_PIE_ON RTCIO_PIE_ON
109 #endif /* __linux__ */
110 #endif /* HAVE_RTC */
112 #ifdef CONFIG_TV
113 #include "stream/tv.h"
114 #endif
115 #ifdef CONFIG_RADIO
116 #include "stream/stream_radio.h"
117 #endif
119 #ifdef CONFIG_DVBIN
120 #include "stream/dvbin.h"
121 #include "stream/cache2.h"
122 #endif
124 //**************************************************************************//
125 // Playtree
126 //**************************************************************************//
127 #include "playtree.h"
128 #include "playtreeparser.h"
130 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
131 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
133 //**************************************************************************//
134 // Config
135 //**************************************************************************//
136 #include "parser-cfg.h"
137 #include "parser-mpcmd.h"
139 //**************************************************************************//
140 // Config file
141 //**************************************************************************//
143 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
145 #include "get_path.h"
147 //**************************************************************************//
148 //**************************************************************************//
149 // Input media streaming & demultiplexer:
150 //**************************************************************************//
152 static int max_framesize=0;
154 #include "stream/stream.h"
155 #include "libmpdemux/demuxer.h"
156 #include "libmpdemux/stheader.h"
158 #ifdef CONFIG_DVDREAD
159 #include "stream/stream_dvd.h"
160 #endif
162 #ifdef CONFIG_DVDNAV
163 #include "stream/stream_dvdnav.h"
164 #endif
166 #include "libmpcodecs/dec_audio.h"
167 #include "libmpcodecs/dec_video.h"
168 #include "libmpcodecs/mp_image.h"
169 #include "libmpcodecs/vf.h"
170 #include "libmpcodecs/vd.h"
172 #include "mixer.h"
174 #include "mp_core.h"
175 #include "options.h"
176 #include "defaultopts.h"
178 //**************************************************************************//
179 //**************************************************************************//
181 // Common FIFO functions, and keyboard/event FIFO code
182 #include "mp_fifo.h"
183 int noconsolecontrols=0;
184 //**************************************************************************//
186 // benchmark:
187 double video_time_usage=0;
188 double vout_time_usage=0;
189 static double audio_time_usage=0;
190 static int total_time_usage_start=0;
191 static int total_frame_cnt=0;
192 static int drop_frame_cnt=0; // total number of dropped frames
193 int benchmark=0;
195 // options:
196 int auto_quality=0;
197 static int output_quality=0;
199 int use_gui=0;
201 #ifdef CONFIG_GUI
202 int enqueue=0;
203 #endif
205 static int list_properties = 0;
207 int osd_level=1;
208 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
209 unsigned int osd_visible;
210 int osd_duration = 1000;
212 int term_osd = 1;
213 static char* term_osd_esc = "\x1b[A\r\x1b[K";
214 static char* playing_msg = NULL;
215 // seek:
216 static double seek_to_sec;
217 static off_t seek_to_byte=0;
218 static off_t step_sec=0;
219 static int loop_seek=0;
221 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
223 // A/V sync:
224 int autosync=0; // 30 might be a good default value.
226 // codecs:
227 char **audio_codec_list=NULL; // override audio codec
228 char **video_codec_list=NULL; // override video codec
229 char **audio_fm_list=NULL; // override audio codec family
230 char **video_fm_list=NULL; // override video codec family
232 // demuxer:
233 extern char *demuxer_name; // override demuxer
234 extern char *audio_demuxer_name; // override audio demuxer
235 extern char *sub_demuxer_name; // override sub demuxer
237 int vobsub_id=-1;
238 char* audio_lang=NULL;
239 char* dvdsub_lang=NULL;
240 static char* spudec_ifo=NULL;
241 int forced_subs_only=0;
242 int file_filter=1;
244 // cache2:
245 int stream_cache_size=-1;
246 #ifdef CONFIG_STREAM_CACHE
247 extern int cache_fill_status;
249 float stream_cache_min_percent=20.0;
250 float stream_cache_seek_min_percent=50.0;
251 #else
252 #define cache_fill_status 0
253 #endif
255 // dump:
256 static char *stream_dump_name="stream.dump";
257 int stream_dump_type=0;
259 // A-V sync:
260 static float default_max_pts_correction=-1;//0.01f;
261 static float max_pts_correction=0;//default_max_pts_correction;
262 static float c_total=0;
263 float audio_delay=0;
264 static int ignore_start=0;
266 static int softsleep=0;
268 double force_fps=0;
269 static int force_srate=0;
270 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
271 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
272 static int play_n_frames=-1;
273 static int play_n_frames_mf=-1;
275 // sub:
276 char *font_name=NULL;
277 char *sub_font_name=NULL;
278 extern int font_fontconfig;
279 float font_factor=0.75;
280 char **sub_name=NULL;
281 float sub_delay=0;
282 float sub_fps=0;
283 int sub_auto = 1;
284 char *vobsub_name=NULL;
285 /*DSP!!char *dsp=NULL;*/
286 int subcc_enabled=0;
287 int suboverlap_enabled = 1;
289 #ifdef CONFIG_ASS
290 #include "libass/ass.h"
291 #include "libass/ass_mp.h"
292 #endif
294 char* current_module=NULL; // for debugging
297 // ---
299 #ifdef CONFIG_MENU
300 #include "m_struct.h"
301 #include "libmenu/menu.h"
302 extern void vf_menu_pause_update(struct vf_instance* vf);
303 extern vf_info_t vf_info_menu;
304 static const vf_info_t* const libmenu_vfs[] = {
305 &vf_info_menu,
306 NULL
308 static vf_instance_t* vf_menu = NULL;
309 int use_menu = 0;
310 static char* menu_cfg = NULL;
311 static char* menu_root = "main";
312 #endif
315 #ifdef HAVE_RTC
316 static int nortc = 1;
317 static char* rtc_device;
318 #endif
320 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
321 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
322 short edl_decision = 0; ///< 1 when an EDL operation has been made.
323 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
324 int use_filedir_conf;
326 #include "mpcommon.h"
327 #include "command.h"
329 #include "metadata.h"
331 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
333 const void *mpctx_get_video_out(MPContext *mpctx)
335 return mpctx->video_out;
338 const void *mpctx_get_audio_out(MPContext *mpctx)
340 return mpctx->audio_out;
343 void *mpctx_get_demuxer(MPContext *mpctx)
345 return mpctx->demuxer;
348 void *mpctx_get_playtree_iter(MPContext *mpctx)
350 return mpctx->playtree_iter;
353 void *mpctx_get_mixer(MPContext *mpctx)
355 return &mpctx->mixer;
358 int mpctx_get_global_sub_size(MPContext *mpctx)
360 return mpctx->global_sub_size;
363 int mpctx_get_osd_function(MPContext *mpctx)
365 return mpctx->osd_function;
368 static float get_relative_time(struct MPContext *mpctx)
370 unsigned int new_time = GetTimer();
371 unsigned int delta = new_time - mpctx->last_time;
372 mpctx->last_time = new_time;
373 return delta * 0.000001;
376 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
377 switch (type)
379 /* check for valid video stream */
380 case META_VIDEO_CODEC:
381 case META_VIDEO_BITRATE:
382 case META_VIDEO_RESOLUTION:
384 if (!mpctx->sh_video)
385 return 0;
386 break;
389 /* check for valid audio stream */
390 case META_AUDIO_CODEC:
391 case META_AUDIO_BITRATE:
392 case META_AUDIO_SAMPLES:
394 if (!mpctx->sh_audio)
395 return 0;
396 break;
399 /* check for valid demuxer */
400 case META_INFO_TITLE:
401 case META_INFO_ARTIST:
402 case META_INFO_ALBUM:
403 case META_INFO_YEAR:
404 case META_INFO_COMMENT:
405 case META_INFO_TRACK:
406 case META_INFO_GENRE:
408 if (!mpctx->demuxer)
409 return 0;
410 break;
413 default:
414 break;
417 return 1;
420 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
421 char **info = mpctx->demuxer->info;
422 int n;
424 if (!info || !tag)
425 return NULL;
427 for (n = 0; info[2*n] != NULL ; n++)
428 if (!strcmp (info[2*n], tag))
429 break;
431 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
434 char *get_metadata(struct MPContext *mpctx, metadata_t type)
436 char *meta = NULL;
437 sh_audio_t * const sh_audio = mpctx->sh_audio;
438 sh_video_t * const sh_video = mpctx->sh_video;
440 if (!is_valid_metadata_type(mpctx, type))
441 return NULL;
443 switch (type)
445 case META_NAME:
447 return strdup (mp_basename2 (mpctx->filename));
450 case META_VIDEO_CODEC:
452 if (sh_video->format == 0x10000001)
453 meta = strdup ("mpeg1");
454 else if (sh_video->format == 0x10000002)
455 meta = strdup ("mpeg2");
456 else if (sh_video->format == 0x10000004)
457 meta = strdup ("mpeg4");
458 else if (sh_video->format == 0x10000005)
459 meta = strdup ("h264");
460 else if (sh_video->format >= 0x20202020)
462 meta = malloc (8);
463 sprintf (meta, "%.4s", (char *) &sh_video->format);
465 else
467 meta = malloc (8);
468 sprintf (meta, "0x%08X", sh_video->format);
470 return meta;
473 case META_VIDEO_BITRATE:
475 meta = malloc (16);
476 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
477 return meta;
480 case META_VIDEO_RESOLUTION:
482 meta = malloc (16);
483 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
484 return meta;
487 case META_AUDIO_CODEC:
489 if (sh_audio->codec && sh_audio->codec->name)
490 meta = strdup (sh_audio->codec->name);
491 return meta;
494 case META_AUDIO_BITRATE:
496 meta = malloc (16);
497 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
498 return meta;
501 case META_AUDIO_SAMPLES:
503 meta = malloc (16);
504 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
505 return meta;
508 /* check for valid demuxer */
509 case META_INFO_TITLE:
510 return get_demuxer_info(mpctx, "Title");
512 case META_INFO_ARTIST:
513 return get_demuxer_info(mpctx, "Artist");
515 case META_INFO_ALBUM:
516 return get_demuxer_info(mpctx, "Album");
518 case META_INFO_YEAR:
519 return get_demuxer_info(mpctx, "Year");
521 case META_INFO_COMMENT:
522 return get_demuxer_info(mpctx, "Comment");
524 case META_INFO_TRACK:
525 return get_demuxer_info(mpctx, "Track");
527 case META_INFO_GENRE:
528 return get_demuxer_info(mpctx, "Genre");
530 default:
531 break;
534 return meta;
537 /// step size of mixer changes
538 int volstep = 3;
540 #ifdef CONFIG_DVDNAV
541 static void mp_dvdnav_context_free(MPContext *ctx){
542 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
543 ctx->nav_smpi = NULL;
544 if (ctx->nav_buffer) free(ctx->nav_buffer);
545 ctx->nav_buffer = NULL;
546 ctx->nav_start = NULL;
547 ctx->nav_in_size = 0;
549 #endif
551 void uninit_player(struct MPContext *mpctx, unsigned int mask){
552 mask=mpctx->initialized_flags&mask;
554 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
556 if(mask&INITIALIZED_ACODEC){
557 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
558 current_module="uninit_acodec";
559 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
560 #ifdef CONFIG_GUI
561 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
562 #endif
563 mpctx->sh_audio=NULL;
564 mpctx->mixer.afilter = NULL;
567 if(mask&INITIALIZED_VCODEC){
568 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
569 current_module="uninit_vcodec";
570 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
571 mpctx->sh_video=NULL;
572 #ifdef CONFIG_MENU
573 vf_menu=NULL;
574 #endif
577 if(mask&INITIALIZED_DEMUXER){
578 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
579 current_module="free_demuxer";
580 if(mpctx->demuxer){
581 mpctx->stream=mpctx->demuxer->stream;
582 free_demuxer(mpctx->demuxer);
584 mpctx->demuxer=NULL;
587 // kill the cache process:
588 if(mask&INITIALIZED_STREAM){
589 mpctx->initialized_flags&=~INITIALIZED_STREAM;
590 current_module="uninit_stream";
591 if(mpctx->stream) free_stream(mpctx->stream);
592 mpctx->stream=NULL;
595 if(mask&INITIALIZED_VO){
596 mpctx->initialized_flags&=~INITIALIZED_VO;
597 current_module="uninit_vo";
598 vo_destroy(mpctx->video_out);
599 mpctx->video_out=NULL;
600 #ifdef CONFIG_DVDNAV
601 mp_dvdnav_context_free(mpctx);
602 #endif
605 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
606 if(mask&INITIALIZED_GETCH2){
607 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
608 current_module="uninit_getch2";
609 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
610 // restore terminal:
611 getch2_disable();
614 if(mask&INITIALIZED_VOBSUB){
615 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
616 current_module="uninit_vobsub";
617 if(vo_vobsub) vobsub_close(vo_vobsub);
618 vo_vobsub=NULL;
621 if (mask&INITIALIZED_SPUDEC){
622 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
623 current_module="uninit_spudec";
624 spudec_free(vo_spudec);
625 vo_spudec=NULL;
628 if(mask&INITIALIZED_AO){
629 mpctx->initialized_flags&=~INITIALIZED_AO;
630 current_module="uninit_ao";
631 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
632 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
633 mpctx->audio_out=NULL;
636 #ifdef CONFIG_GUI
637 if(mask&INITIALIZED_GUI){
638 mpctx->initialized_flags&=~INITIALIZED_GUI;
639 current_module="uninit_gui";
640 guiDone();
642 #endif
644 current_module=NULL;
647 void exit_player_with_rc(struct MPContext *mpctx, const char* how, int rc){
649 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
650 uninit_player(mpctx, INITIALIZED_ALL);
651 #if defined(__MINGW32__) || defined(__CYGWIN__)
652 timeEndPeriod(1);
653 #endif
654 #ifdef CONFIG_X11
655 #ifdef CONFIG_GUI
656 if ( !use_gui )
657 #endif
658 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
659 #endif
661 current_module="uninit_input";
662 mp_input_uninit(mpctx->input);
663 #ifdef CONFIG_MENU
664 if (use_menu)
665 menu_uninit();
666 #endif
668 #ifdef CONFIG_FREETYPE
669 current_module="uninit_font";
670 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
671 free_font_desc(mpctx->osd->sub_font);
672 free_font_desc(vo_font);
673 vo_font = NULL;
674 done_freetype();
675 #endif
676 osd_free(mpctx->osd);
678 #ifdef CONFIG_ASS
679 ass_library_done(ass_library);
680 #endif
682 current_module="exit_player";
684 // free mplayer config
685 if(mpctx->mconfig)
686 m_config_free(mpctx->mconfig);
688 if(mpctx->playtree)
689 play_tree_free(mpctx->playtree, 1);
691 talloc_free(mpctx->key_fifo);
693 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
694 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
695 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
697 exit(rc);
700 static void exit_player(struct MPContext *mpctx, const char* how)
702 exit_player_with_rc(mpctx, how, 1);
705 #ifndef __MINGW32__
706 static void child_sighandler(int x){
707 pid_t pid;
708 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
710 #endif
712 #ifdef CONFIG_CRASH_DEBUG
713 static char *prog_path;
714 static int crash_debug = 0;
715 #endif
717 static void exit_sighandler(int x){
718 static int sig_count=0;
719 #ifdef CONFIG_CRASH_DEBUG
720 if (!crash_debug || x != SIGTRAP)
721 #endif
722 ++sig_count;
723 if(sig_count==5)
725 /* We're crashing bad and can't uninit cleanly :(
726 * by popular request, we make one last (dirty)
727 * effort to restore the user's
728 * terminal. */
729 getch2_disable();
730 exit(1);
732 if(sig_count==6) exit(1);
733 if(sig_count>6){
734 // can't stop :(
735 #ifndef __MINGW32__
736 kill(getpid(),SIGKILL);
737 #endif
739 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
740 current_module?current_module:"unknown"
742 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
743 if(sig_count<=1)
744 switch(x){
745 case SIGINT:
746 case SIGQUIT:
747 case SIGTERM:
748 case SIGKILL:
749 async_quit_request = 1;
750 return; // killed from keyboard (^C) or killed [-9]
751 case SIGILL:
752 #ifdef RUNTIME_CPUDETECT
753 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
754 #else
755 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
756 #endif
757 case SIGFPE:
758 case SIGSEGV:
759 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
760 default:
761 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
762 #ifdef CONFIG_CRASH_DEBUG
763 if (crash_debug) {
764 int gdb_pid;
765 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
766 gdb_pid = fork();
767 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
768 if (gdb_pid == 0) { // We are the child
769 char spid[20];
770 snprintf(spid, sizeof(spid), "%i", getppid());
771 getch2_disable(); // allow terminal to work properly with gdb
772 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
773 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
774 } else if (gdb_pid < 0)
775 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
776 else {
777 waitpid(gdb_pid, NULL, 0);
779 if (x == SIGTRAP) return;
781 #endif
783 getch2_disable();
784 exit(1);
787 #include "cfg-mplayer.h"
789 static int cfg_include(m_option_t *conf, char *filename)
791 return m_config_parse_config_file(conf->priv, filename);
794 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
796 char *conffile;
797 int conffile_fd;
798 if (!disable_system_conf &&
799 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
800 exit_player(mpctx, NULL);
801 if ((conffile = get_path("")) == NULL) {
802 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
803 } else {
804 #ifdef __MINGW32__
805 mkdir(conffile);
806 #else
807 mkdir(conffile, 0777);
808 #endif
809 free(conffile);
810 if ((conffile = get_path("config")) == NULL) {
811 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
812 } else {
813 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
814 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
815 write(conffile_fd, default_config, strlen(default_config));
816 close(conffile_fd);
818 if (!disable_user_conf &&
819 m_config_parse_config_file(conf, conffile) < 0)
820 exit_player(mpctx, NULL);
821 free(conffile);
826 #define PROFILE_CFG_PROTOCOL "protocol."
828 static void load_per_protocol_config (m_config_t* conf, const char *const file)
830 char *str;
831 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
832 m_profile_t *p;
834 /* does filename actually uses a protocol ? */
835 str = strstr (file, "://");
836 if (!str)
837 return;
839 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
840 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
841 p = m_config_get_profile (conf, protocol);
842 if (p)
844 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
845 m_config_set_profile(conf,p);
849 #define PROFILE_CFG_EXTENSION "extension."
851 static void load_per_extension_config (m_config_t* conf, const char *const file)
853 char *str;
854 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
855 m_profile_t *p;
857 /* does filename actually have an extension ? */
858 str = strrchr (file, '.');
859 if (!str)
860 return;
862 sprintf (extension, PROFILE_CFG_EXTENSION);
863 strncat (extension, ++str, 7);
864 p = m_config_get_profile (conf, extension);
865 if (p)
867 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
868 m_config_set_profile(conf,p);
872 #define PROFILE_CFG_VO "vo."
873 #define PROFILE_CFG_AO "ao."
875 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
877 char profile[strlen (cfg) + strlen (out) + 1];
878 m_profile_t *p;
880 sprintf (profile, "%s%s", cfg, out);
881 p = m_config_get_profile (conf, profile);
882 if (p)
884 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
885 m_config_set_profile(conf,p);
889 static void load_per_file_config (m_config_t* conf, const char *const file)
891 char *confpath;
892 char cfg[strlen(file)+10];
893 struct stat st;
894 char *name;
896 sprintf (cfg, "%s.conf", file);
898 if (use_filedir_conf && !stat (cfg, &st))
900 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
901 m_config_parse_config_file (conf, cfg);
902 return;
905 if ((name = strrchr (cfg, '/')) == NULL)
906 name = cfg;
907 else
908 name++;
910 if ((confpath = get_path (name)) != NULL)
912 if (!stat (confpath, &st))
914 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
915 m_config_parse_config_file (conf, confpath);
918 free (confpath);
922 /* When libmpdemux performs a blocking operation (network connection or
923 * cache filling) if the operation fails we use this function to check
924 * if it was interrupted by the user.
925 * The function returns a new value for eof. */
926 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
928 mp_cmd_t* cmd;
929 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
930 switch(cmd->id) {
931 case MP_CMD_QUIT:
932 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
933 case MP_CMD_PLAY_TREE_STEP: {
934 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
935 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
936 } break;
937 case MP_CMD_PLAY_TREE_UP_STEP: {
938 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
939 } break;
940 case MP_CMD_PLAY_ALT_SRC_STEP: {
941 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
942 } break;
944 mp_cmd_free(cmd);
946 return stop_play;
949 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
951 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
953 play_tree_add_bpf(entry,mpctx->filename);
955 #ifdef CONFIG_GUI
956 if (use_gui) {
957 if (entry) {
958 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
959 play_tree_free_list(entry,1);
961 } else
962 #endif
964 if(!entry) {
965 entry = mpctx->playtree_iter->tree;
966 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
967 return PT_NEXT_ENTRY;
969 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
970 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
971 return PT_NEXT_ENTRY;
974 play_tree_remove(entry,1,1);
975 return PT_NEXT_SRC;
977 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
978 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
979 entry = mpctx->playtree_iter->tree;
980 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
981 return PT_NEXT_ENTRY;
983 play_tree_remove(entry,1,1);
985 return PT_NEXT_SRC;
988 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
990 sub_data *subd;
991 #ifdef CONFIG_ASS
992 ass_track_t *asst = 0;
993 #endif
995 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
996 return;
999 subd = sub_read_file(filename, fps);
1000 #ifdef CONFIG_ASS
1001 if (ass_enabled)
1002 #ifdef CONFIG_ICONV
1003 asst = ass_read_file(ass_library, filename, sub_cp);
1004 #else
1005 asst = ass_read_file(ass_library, filename, 0);
1006 #endif
1007 if (ass_enabled && subd && !asst)
1008 asst = ass_read_subdata(ass_library, subd, fps);
1010 if (!asst && !subd)
1011 #else
1012 if(!subd)
1013 #endif
1014 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1015 filename_recode(filename));
1017 #ifdef CONFIG_ASS
1018 if (!asst && !subd) return;
1019 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1020 #else
1021 if (!subd) return;
1022 #endif
1023 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1024 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1025 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1026 filename_recode(filename));
1027 ++mpctx->set_of_sub_size;
1028 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1029 filename_recode(filename));
1032 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1033 void update_set_of_subtitles(struct MPContext *mpctx)
1034 // subdata was changed, set_of_sub... have to be updated.
1036 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1037 int i;
1038 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1039 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1040 set_of_subtitles[i-1] = set_of_subtitles[i];
1041 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1042 --mpctx->set_of_sub_size;
1043 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1045 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1046 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1048 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1049 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1050 ++mpctx->set_of_sub_size;
1054 void init_vo_spudec(struct MPContext *mpctx)
1056 if (vo_spudec)
1057 spudec_free(vo_spudec);
1058 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1059 vo_spudec = NULL;
1060 if (spudec_ifo) {
1061 unsigned int palette[16], width, height;
1062 current_module="spudec_init_vobsub";
1063 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1064 vo_spudec=spudec_new_scaled(palette, width, height);
1067 #ifdef CONFIG_DVDREAD
1068 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1069 current_module="spudec_init_dvdread";
1070 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1071 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1073 #endif
1075 #ifdef CONFIG_DVDNAV
1076 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1077 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1078 current_module="spudec_init_dvdnav";
1079 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1081 #endif
1083 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1084 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1085 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1086 current_module = "spudec_init_matroska";
1087 vo_spudec =
1088 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1089 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1090 mkv_sh_sub->height);
1091 forced_subs_only = mkv_sh_sub->forced_subs_only;
1094 if (vo_spudec==NULL) {
1095 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1096 unsigned int *palette = NULL;
1097 if (sh && !sh->has_palette && sh->extradata_len == 16*4) {
1098 int i;
1099 for (i = 0; i < 16; i++)
1100 sh->palette[i] = AV_RB32(sh->extradata + i*4);
1101 sh->has_palette = 1;
1103 if (sh && sh->has_palette)
1104 palette = sh->palette;
1105 current_module="spudec_init_normal";
1106 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1107 spudec_set_font_factor(vo_spudec,font_factor);
1110 if (vo_spudec!=NULL)
1111 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1115 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1116 * make it all work is to use the builtin SDL-bootstrap code, which
1117 * will be done automatically by replacing our main() if we include SDL.h.
1119 #if defined(__APPLE__) && defined(CONFIG_SDL)
1120 #include <SDL.h>
1121 #endif
1124 * \brief append a formatted string
1125 * \param buf buffer to print into
1126 * \param pos position of terminating 0 in buf
1127 * \param len maximum number of characters in buf, not including terminating 0
1128 * \param format printf format string
1130 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1132 va_list va;
1133 va_start(va, format);
1134 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1135 va_end(va);
1136 if (*pos >= len ) {
1137 buf[len] = 0;
1138 *pos = len;
1143 * \brief append time in the hh:mm:ss.f format
1144 * \param buf buffer to print into
1145 * \param pos position of terminating 0 in buf
1146 * \param len maximum number of characters in buf, not including terminating 0
1147 * \param time time value to convert/append
1149 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1150 long tenths = 10 * time;
1151 int f1 = tenths % 10;
1152 int ss = (tenths / 10) % 60;
1153 int mm = (tenths / 600) % 60;
1154 int hh = tenths / 36000;
1155 if (time <= 0) {
1156 saddf(buf, pos, len, "unknown");
1157 return;
1159 if (hh > 0)
1160 saddf(buf, pos, len, "%2d:", hh);
1161 if (hh > 0 || mm > 0)
1162 saddf(buf, pos, len, "%02d:", mm);
1163 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1167 * \brief print the status line
1168 * \param a_pos audio position
1169 * \param a_v A-V desynchronization
1170 * \param corr amount out A-V synchronization
1172 static void print_status(struct MPContext *mpctx, float a_pos, float a_v, float corr)
1174 struct MPOpts *opts = &mpctx->opts;
1175 sh_video_t * const sh_video = mpctx->sh_video;
1176 int width;
1177 char *line;
1178 unsigned pos = 0;
1179 get_screen_size();
1180 if (screen_width > 0)
1181 width = screen_width;
1182 else
1183 width = 80;
1184 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1185 /* Windows command line is broken (MinGW's rxvt works, but we
1186 * should not depend on that). */
1187 width--;
1188 #endif
1189 line = malloc(width + 1); // one additional char for the terminating null
1191 // Audio time
1192 if (mpctx->sh_audio) {
1193 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1194 if (!sh_video) {
1195 float len = demuxer_get_time_length(mpctx->demuxer);
1196 saddf(line, &pos, width, "(");
1197 sadd_hhmmssf(line, &pos, width, a_pos);
1198 saddf(line, &pos, width, ") of %.1f (", len);
1199 sadd_hhmmssf(line, &pos, width, len);
1200 saddf(line, &pos, width, ") ");
1204 // Video time
1205 if (sh_video)
1206 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1208 // A-V sync
1209 if (mpctx->sh_audio && sh_video)
1210 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1212 // Video stats
1213 if (sh_video)
1214 saddf(line, &pos, width, "%3d/%3d ",
1215 (int)sh_video->num_frames,
1216 (int)sh_video->num_frames_decoded);
1218 // CPU usage
1219 if (sh_video) {
1220 if (sh_video->timer > 0.5)
1221 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1222 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1223 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1224 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1225 else
1226 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1227 } else if (mpctx->sh_audio) {
1228 if (mpctx->delay > 0.5)
1229 saddf(line, &pos, width, "%4.1f%% ",
1230 100.0*audio_time_usage/(double)mpctx->delay);
1231 else
1232 saddf(line, &pos, width, "??,?%% ");
1235 // VO stats
1236 if (sh_video)
1237 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1239 #ifdef CONFIG_STREAM_CACHE
1240 // cache stats
1241 if (stream_cache_size > 0)
1242 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1243 #endif
1245 // other
1246 if (opts->playback_speed != 1)
1247 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1249 // end
1250 if (erase_to_end_of_line) {
1251 line[pos] = 0;
1252 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1253 } else {
1254 memset(&line[pos], ' ', width - pos);
1255 line[width] = 0;
1256 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1258 free(line);
1262 * \brief build a chain of audio filters that converts the input format
1263 * to the ao's format, taking into account the current playback_speed.
1264 * \param sh_audio describes the requested input format of the chain.
1265 * \param ao_data describes the requested output format of the chain.
1267 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1269 struct MPOpts *opts = &mpctx->opts;
1270 int new_srate;
1271 int result;
1272 if (!sh_audio)
1274 #ifdef CONFIG_GUI
1275 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1276 #endif
1277 mpctx->mixer.afilter = NULL;
1278 return 0;
1280 if(af_control_any_rev(sh_audio->afilter,
1281 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1282 &opts->playback_speed)) {
1283 new_srate = sh_audio->samplerate;
1284 } else {
1285 new_srate = sh_audio->samplerate * opts->playback_speed;
1286 if (new_srate != ao_data->samplerate) {
1287 // limits are taken from libaf/af_resample.c
1288 if (new_srate < 8000)
1289 new_srate = 8000;
1290 if (new_srate > 192000)
1291 new_srate = 192000;
1292 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1295 result = init_audio_filters(sh_audio, new_srate,
1296 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1297 mpctx->mixer.afilter = sh_audio->afilter;
1298 #ifdef CONFIG_GUI
1299 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1300 #endif
1301 return result;
1305 typedef struct mp_osd_msg mp_osd_msg_t;
1306 struct mp_osd_msg {
1307 /// Previous message on the stack.
1308 mp_osd_msg_t* prev;
1309 /// Message text.
1310 char msg[64];
1311 int id,level,started;
1312 /// Display duration in ms.
1313 unsigned time;
1316 /// OSD message stack.
1317 static mp_osd_msg_t* osd_msg_stack = NULL;
1320 * \brief Add a message on the OSD message stack
1322 * If a message with the same id is already present in the stack
1323 * it is pulled on top of the stack, otherwise a new message is created.
1327 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1328 mp_osd_msg_t *msg,*last=NULL;
1329 va_list va;
1330 int r;
1332 // look if the id is already in the stack
1333 for(msg = osd_msg_stack ; msg && msg->id != id ;
1334 last = msg, msg = msg->prev);
1335 // not found: alloc it
1336 if(!msg) {
1337 msg = calloc(1,sizeof(mp_osd_msg_t));
1338 msg->prev = osd_msg_stack;
1339 osd_msg_stack = msg;
1340 } else if(last) { // found, but it's not on top of the stack
1341 last->prev = msg->prev;
1342 msg->prev = osd_msg_stack;
1343 osd_msg_stack = msg;
1345 // write the msg
1346 va_start(va,fmt);
1347 r = vsnprintf(msg->msg, 64, fmt, va);
1348 va_end(va);
1349 if(r >= 64) msg->msg[63] = 0;
1350 // set id and time
1351 msg->id = id;
1352 msg->level = level;
1353 msg->time = time;
1358 * \brief Remove a message from the OSD stack
1360 * This function can be used to get rid of a message right away.
1364 void rm_osd_msg(int id) {
1365 mp_osd_msg_t *msg,*last=NULL;
1367 // Search for the msg
1368 for(msg = osd_msg_stack ; msg && msg->id != id ;
1369 last = msg, msg = msg->prev);
1370 if(!msg) return;
1372 // Detach it from the stack and free it
1373 if(last)
1374 last->prev = msg->prev;
1375 else
1376 osd_msg_stack = msg->prev;
1377 free(msg);
1381 * \brief Remove all messages from the OSD stack
1385 static void clear_osd_msgs(void) {
1386 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1387 while(msg) {
1388 prev = msg->prev;
1389 free(msg);
1390 msg = prev;
1392 osd_msg_stack = NULL;
1396 * \brief Get the current message from the OSD stack.
1398 * This function decrements the message timer and destroys the old ones.
1399 * The message that should be displayed is returned (if any).
1403 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1405 mp_osd_msg_t *msg,*prev,*last = NULL;
1406 static unsigned last_update = 0;
1407 unsigned now = GetTimerMS();
1408 unsigned diff;
1409 char hidden_dec_done = 0;
1411 if (osd_visible) {
1412 // 36000000 means max timed visibility is 1 hour into the future, if
1413 // the difference is greater assume it's wrapped around from below 0
1414 if (osd_visible - now > 36000000) {
1415 osd_visible = 0;
1416 vo_osd_progbar_type = -1; // disable
1417 vo_osd_changed(OSDTYPE_PROGBAR);
1418 if (mpctx->osd_function != OSD_PAUSE)
1419 mpctx->osd_function = OSD_PLAY;
1423 if(!last_update) last_update = now;
1424 diff = now >= last_update ? now - last_update : 0;
1426 last_update = now;
1428 // Look for the first message in the stack with high enough level.
1429 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1430 prev = msg->prev;
1431 if(msg->level > osd_level && hidden_dec_done) continue;
1432 // The message has a high enough level or it is the first hidden one
1433 // in both cases we decrement the timer or kill it.
1434 if(!msg->started || msg->time > diff) {
1435 if(msg->started) msg->time -= diff;
1436 else msg->started = 1;
1437 // display it
1438 if(msg->level <= osd_level) return msg;
1439 hidden_dec_done = 1;
1440 continue;
1442 // kill the message
1443 free(msg);
1444 if(last) {
1445 last->prev = prev;
1446 msg = last;
1447 } else {
1448 osd_msg_stack = prev;
1449 msg = NULL;
1452 // Nothing found
1453 return NULL;
1457 * \brief Display the OSD bar.
1459 * Display the OSD bar or fall back on a simple message.
1463 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1465 if(osd_level < 1) return;
1467 if(mpctx->sh_video) {
1468 osd_visible = (GetTimerMS() + 1000) | 1;
1469 vo_osd_progbar_type = type;
1470 vo_osd_progbar_value = 256*(val-min)/(max-min);
1471 vo_osd_changed(OSDTYPE_PROGBAR);
1472 return;
1475 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1476 name,ROUND(100*(val-min)/(max-min)));
1481 * \brief Update the OSD message line.
1483 * This function displays the current message on the vo OSD or on the term.
1484 * If the stack is empty and the OSD level is high enough the timer
1485 * is displayed (only on the vo OSD).
1489 static void update_osd_msg(struct MPContext *mpctx)
1491 mp_osd_msg_t *msg;
1492 struct osd_state *osd = mpctx->osd;
1493 char osd_text_timer[64];
1495 // Look if we have a msg
1496 if((msg = get_osd_msg(mpctx))) {
1497 if (strcmp(osd->osd_text, msg->msg)) {
1498 strncpy(osd->osd_text, msg->msg, 63);
1499 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1500 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1502 return;
1505 if(mpctx->sh_video) {
1506 // fallback on the timer
1507 if(osd_level>=2) {
1508 int len = demuxer_get_time_length(mpctx->demuxer);
1509 int percentage = -1;
1510 char percentage_text[10];
1511 int pts = demuxer_get_current_time(mpctx->demuxer);
1513 if (mpctx->osd_show_percentage)
1514 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1516 if (percentage >= 0)
1517 snprintf(percentage_text, 9, " (%d%%)", percentage);
1518 else
1519 percentage_text[0] = 0;
1521 if (osd_level == 3)
1522 snprintf(osd_text_timer, 63,
1523 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1524 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1525 len/3600,(len/60)%60,len%60,percentage_text);
1526 else
1527 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1528 mpctx->osd_function,pts/3600,(pts/60)%60,
1529 pts%60,percentage_text);
1530 } else
1531 osd_text_timer[0]=0;
1533 // always decrement the percentage timer
1534 if(mpctx->osd_show_percentage)
1535 mpctx->osd_show_percentage--;
1537 if (strcmp(osd->osd_text, osd_text_timer)) {
1538 strncpy(osd->osd_text, osd_text_timer, 63);
1539 vo_osd_changed(OSDTYPE_OSD);
1541 return;
1544 // Clear the term osd line
1545 if (term_osd && osd->osd_text[0]) {
1546 osd->osd_text[0] = 0;
1547 printf("%s\n",term_osd_esc);
1551 ///@}
1552 // OSDMsgStack
1555 void reinit_audio_chain(struct MPContext *mpctx)
1557 struct MPOpts *opts = &mpctx->opts;
1558 if(mpctx->sh_audio){
1559 current_module="init_audio_codec";
1560 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1561 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1562 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1563 mpctx->d_audio->id = -2;
1564 return;
1565 } else
1566 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1567 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1570 //const ao_info_t *info=audio_out->info;
1571 current_module="af_preinit";
1572 ao_data.samplerate=force_srate;
1573 ao_data.channels=0;
1574 ao_data.format=audio_output_format;
1575 #if 1
1576 // first init to detect best values
1577 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1578 // input:
1579 mpctx->sh_audio->samplerate,
1580 // output:
1581 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1582 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1583 exit_player(mpctx, MSGTR_Exit_error);
1585 #endif
1586 current_module="ao2_init";
1587 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1588 0, // plugin flag
1589 ao_data.samplerate,
1590 ao_data.channels,
1591 ao_data.format,0))){
1592 // FAILED:
1593 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1594 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1595 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1596 mpctx->d_audio->id = -2;
1597 return;
1598 } else {
1599 // SUCCESS:
1600 mpctx->initialized_flags|=INITIALIZED_AO;
1601 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1602 mpctx->audio_out->info->short_name,
1603 ao_data.samplerate, ao_data.channels,
1604 af_fmt2str_short(ao_data.format),
1605 af_fmt2bits(ao_data.format)/8 );
1606 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1607 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1608 if(strlen(mpctx->audio_out->info->comment) > 0)
1609 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1610 // init audio filters:
1611 #if 1
1612 current_module="af_init";
1613 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1614 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1615 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1616 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1617 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1619 #endif
1621 mpctx->mixer.audio_out = mpctx->audio_out;
1622 mpctx->mixer.volstep = volstep;
1627 ///@}
1628 // Command2Property
1631 // Return pts value corresponding to the end point of audio written to the
1632 // ao so far.
1633 static double written_audio_pts(struct MPContext *mpctx)
1635 sh_audio_t *sh_audio = mpctx->sh_audio;
1636 demux_stream_t *d_audio = mpctx->d_audio;
1637 double buffered_output;
1638 // first calculate the end pts of audio that has been output by decoder
1639 double a_pts = sh_audio->pts;
1640 if (a_pts != MP_NOPTS_VALUE)
1641 // Good, decoder supports new way of calculating audio pts.
1642 // sh_audio->pts is the timestamp of the latest input packet with
1643 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1644 // the amount of bytes the decoder has written after that timestamp.
1645 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1646 else {
1647 // Decoder doesn't support new way of calculating pts (or we're
1648 // being called before it has decoded anything with known timestamp).
1649 // Use the old method of audio pts calculation: take the timestamp
1650 // of last packet with known pts the decoder has read data from,
1651 // and add amount of bytes read after the beginning of that packet
1652 // divided by input bps. This will be inaccurate if the input/output
1653 // ratio is not constant for every audio packet or if it is constant
1654 // but not accurately known in sh_audio->i_bps.
1656 a_pts = d_audio->pts;
1657 // ds_tell_pts returns bytes read after last timestamp from
1658 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1659 // it has read but not decoded
1660 if (sh_audio->i_bps)
1661 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1662 (double)sh_audio->i_bps;
1664 // Now a_pts hopefully holds the pts for end of audio from decoder.
1665 // Substract data in buffers between decoder and audio out.
1667 // Decoded but not filtered
1668 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1670 // Data buffered in audio filters, measured in bytes of "missing" output
1671 buffered_output = af_calc_delay(sh_audio->afilter);
1673 // Data that was ready for ao but was buffered because ao didn't fully
1674 // accept everything to internal buffers yet
1675 buffered_output += sh_audio->a_out_buffer_len;
1677 // Filters divide audio length by playback_speed, so multiply by it
1678 // to get the length in original units without speedup or slowdown
1679 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1681 return a_pts;
1684 // Return pts value corresponding to currently playing audio.
1685 double playing_audio_pts(struct MPContext *mpctx)
1687 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1688 mpctx->audio_out->get_delay();
1691 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1692 struct MPOpts *opts = &mpctx->opts;
1693 // check for frame-drop:
1694 current_module = "check_framedrop";
1695 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1696 static int dropped_frames;
1697 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1698 float d = delay-mpctx->delay;
1699 ++total_frame_cnt;
1700 // we should avoid dropping too many frames in sequence unless we
1701 // are too late. and we allow 100ms A-V delay here:
1702 if (d < -dropped_frames*frame_time-0.100 &&
1703 mpctx->osd_function != OSD_PAUSE) {
1704 ++drop_frame_cnt;
1705 ++dropped_frames;
1706 return frame_dropping;
1707 } else
1708 dropped_frames = 0;
1710 return 0;
1713 static int generate_video_frame(struct MPContext *mpctx)
1715 sh_video_t * const sh_video = mpctx->sh_video;
1716 demux_stream_t *d_video = mpctx->d_video;
1718 unsigned char *start;
1719 int in_size;
1720 int hit_eof=0;
1721 double pts;
1723 while (1) {
1724 int drop_frame = check_framedrop(mpctx, sh_video->frametime);
1725 void *decoded_frame;
1726 current_module = "decode video";
1727 // XXX Time used in this call is not counted in any performance
1728 // timer now, OSD is not updated correctly for filter-added frames
1729 if (vf_output_queued_frame(sh_video->vfilter))
1730 break;
1731 current_module = "video_read_frame";
1732 in_size = ds_get_packet_pts(d_video, &start, &pts);
1733 if (in_size < 0) {
1734 // try to extract last frames in case of decoder lag
1735 in_size = 0;
1736 pts = 1e300;
1737 hit_eof = 1;
1739 if (in_size > max_framesize)
1740 max_framesize = in_size;
1741 current_module = "decode video";
1742 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1743 if (decoded_frame) {
1744 update_subtitles(sh_video, mpctx->d_sub, 0);
1745 update_teletext(sh_video, mpctx->demuxer, 0);
1746 update_osd_msg(mpctx);
1747 current_module = "filter video";
1748 if (filter_video(sh_video, decoded_frame, sh_video->pts,
1749 mpctx->osd))
1750 break;
1751 } else if (drop_frame)
1752 return -1;
1753 if (hit_eof)
1754 return 0;
1756 return 1;
1759 #ifdef HAVE_RTC
1760 int rtc_fd = -1;
1761 #endif
1763 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1765 #ifdef HAVE_RTC
1766 if (rtc_fd >= 0){
1767 // -------- RTC -----------
1768 current_module="sleep_rtc";
1769 while (time_frame > 0.000) {
1770 unsigned long rtc_ts;
1771 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1772 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1773 time_frame -= get_relative_time(mpctx);
1775 } else
1776 #endif
1778 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1779 // unnecessarily high CPU usage
1780 float margin = softsleep ? 0.011 : 0;
1781 current_module = "sleep_timer";
1782 while (time_frame > margin) {
1783 usec_sleep(1000000 * (time_frame - margin));
1784 time_frame -= get_relative_time(mpctx);
1786 if (softsleep){
1787 current_module = "sleep_soft";
1788 if (time_frame < 0)
1789 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1790 while (time_frame > 0)
1791 time_frame -= get_relative_time(mpctx); // burn the CPU
1794 return time_frame;
1797 #ifdef CONFIG_DVDNAV
1798 #ifndef FF_B_TYPE
1799 #define FF_B_TYPE 3
1800 #endif
1801 /// store decoded video image
1802 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1803 mp_image_t *from_mpi) {
1804 mp_image_t *mpi;
1806 /// Do not store B-frames
1807 if (from_mpi->pict_type == FF_B_TYPE)
1808 return to_mpi;
1810 if (to_mpi &&
1811 to_mpi->w == from_mpi->w &&
1812 to_mpi->h == from_mpi->h &&
1813 to_mpi->imgfmt == from_mpi->imgfmt)
1814 mpi = to_mpi;
1815 else {
1816 if (to_mpi)
1817 free_mp_image(to_mpi);
1818 if (from_mpi->w == 0 || from_mpi->h == 0)
1819 return NULL;
1820 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1823 copy_mpi(mpi,from_mpi);
1824 return mpi;
1827 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1828 struct MPOpts *opts = &ctx->opts;
1829 if (ctx->sh_video) {
1830 /// clear video pts
1831 ctx->d_video->pts = 0.0f;
1832 ctx->sh_video->pts = 0.0f;
1833 ctx->sh_video->i_pts = 0.0f;
1834 ctx->sh_video->last_pts = 0.0f;
1835 ctx->sh_video->num_buffered_pts = 0;
1836 ctx->sh_video->num_frames = 0;
1837 ctx->sh_video->num_frames_decoded = 0;
1838 ctx->sh_video->timer = 0.0f;
1839 ctx->sh_video->stream_delay = 0.0f;
1840 ctx->sh_video->timer = 0;
1841 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1844 if (ctx->sh_audio) {
1845 /// free audio packets and reset
1846 ds_free_packs(ctx->d_audio);
1847 audio_delay -= ctx->sh_audio->stream_delay;
1848 ctx->delay =- audio_delay;
1849 ctx->audio_out->reset();
1850 resync_audio_stream(ctx->sh_audio);
1853 if (ctx->d_sub) opts->sub_id = -2;
1855 audio_delay = 0.0f;
1857 /// clear all EOF related flags
1858 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1861 /// Restore last decoded DVDNAV (still frame)
1862 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1863 int *in_size,
1864 unsigned char **start,
1865 mp_image_t *decoded_frame)
1867 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1868 return decoded_frame;
1870 /// a change occured in dvdnav stream
1871 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1872 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1873 mp_dvdnav_context_free(mpctx);
1874 mp_dvdnav_reset_stream(mpctx);
1875 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1876 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1879 if (*in_size < 0) {
1880 float len;
1882 /// Display still frame, if any
1883 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1884 decoded_frame = mpctx->nav_smpi;
1886 /// increment video frame : continue playing after still frame
1887 len = demuxer_get_time_length(mpctx->demuxer);
1888 if (mpctx->sh_video->pts >= len &&
1889 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1890 mp_dvdnav_skip_still(mpctx->stream);
1891 mp_dvdnav_skip_wait(mpctx->stream);
1893 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1895 if (mpctx->nav_buffer) {
1896 *start = mpctx->nav_start;
1897 *in_size = mpctx->nav_in_size;
1898 if (mpctx->nav_start)
1899 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1903 return decoded_frame;
1906 /// Save last decoded DVDNAV (still frame)
1907 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1908 unsigned char *start,
1909 mp_image_t *decoded_frame)
1911 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1912 return;
1914 if (mpctx->nav_buffer)
1915 free(mpctx->nav_buffer);
1917 mpctx->nav_buffer = malloc(in_size);
1918 mpctx->nav_start = start;
1919 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1920 if (mpctx->nav_buffer)
1921 memcpy(mpctx->nav_buffer,start,in_size);
1923 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1924 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1926 #endif /* CONFIG_DVDNAV */
1928 static void adjust_sync_and_print_status(struct MPContext *mpctx,
1929 int between_frames,
1930 float timing_error)
1932 struct MPOpts *opts = &mpctx->opts;
1933 current_module="av_sync";
1935 if(mpctx->sh_audio){
1936 double a_pts, v_pts;
1938 if (autosync)
1940 * If autosync is enabled, the value for delay must be calculated
1941 * a bit differently. It is set only to the difference between
1942 * the audio and video timers. Any attempt to include the real
1943 * or corrected delay causes the pts_correction code below to
1944 * try to correct for the changes in delay which autosync is
1945 * trying to measure. This keeps the two from competing, but still
1946 * allows the code to correct for PTS drift *only*. (Using a delay
1947 * value here, even a "corrected" one, would be incompatible with
1948 * autosync mode.)
1950 a_pts = written_audio_pts(mpctx) - mpctx->delay;
1951 else
1952 a_pts = playing_audio_pts(mpctx);
1954 v_pts = mpctx->sh_video->pts;
1957 static int drop_message=0;
1958 double AV_delay = a_pts - audio_delay - v_pts;
1959 double x;
1960 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1961 ++drop_message;
1962 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1964 if (autosync)
1965 x = AV_delay*0.1f;
1966 else
1967 /* Do not correct target time for the next frame if this frame
1968 * was late not because of wrong target time but because the
1969 * target time could not be met */
1970 x = (AV_delay + timing_error * opts->playback_speed) * 0.1f;
1971 if (x < -max_pts_correction)
1972 x = -max_pts_correction;
1973 else if (x> max_pts_correction)
1974 x = max_pts_correction;
1975 if (default_max_pts_correction >= 0)
1976 max_pts_correction = default_max_pts_correction;
1977 else
1978 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1979 if (!between_frames) {
1980 mpctx->delay+=x;
1981 c_total+=x;
1983 if(!quiet)
1984 print_status(mpctx, a_pts - audio_delay, AV_delay, c_total);
1987 } else {
1988 // No audio:
1990 if (!quiet)
1991 print_status(mpctx, 0, 0, 0);
1995 static int fill_audio_out_buffers(struct MPContext *mpctx)
1997 struct MPOpts *opts = &mpctx->opts;
1998 unsigned int t;
1999 double tt;
2000 int playsize;
2001 int playflags=0;
2002 int audio_eof=0;
2003 int bytes_to_write;
2004 sh_audio_t * const sh_audio = mpctx->sh_audio;
2006 current_module="play_audio";
2008 while (1) {
2009 // all the current uses of ao_data.pts seem to be in aos that handle
2010 // sync completely wrong; there should be no need to use ao_data.pts
2011 // in get_space()
2012 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2013 bytes_to_write = mpctx->audio_out->get_space();
2014 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2015 break;
2017 // handle audio-only case:
2018 // this is where mplayer sleeps during audio-only playback
2019 // to avoid 100% CPU use
2020 usec_sleep(10000); // Wait a tick before retry
2023 while (bytes_to_write) {
2024 playsize = bytes_to_write;
2025 if (playsize > MAX_OUTBURST)
2026 playsize = MAX_OUTBURST;
2027 bytes_to_write -= playsize;
2029 // Fill buffer if needed:
2030 current_module="decode_audio";
2031 t = GetTimer();
2032 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2033 if (mpctx->d_audio->eof) {
2034 audio_eof = 1;
2035 if (sh_audio->a_out_buffer_len == 0)
2036 return 0;
2038 t = GetTimer() - t;
2039 tt = t*0.000001f; audio_time_usage+=tt;
2040 if (playsize > sh_audio->a_out_buffer_len) {
2041 playsize = sh_audio->a_out_buffer_len;
2042 if (audio_eof)
2043 playflags |= AOPLAY_FINAL_CHUNK;
2045 if (!playsize)
2046 break;
2048 // play audio:
2049 current_module="play_audio";
2051 // Is this pts value actually useful for the aos that access it?
2052 // They're obviously badly broken in the way they handle av sync;
2053 // would not having access to this make them more broken?
2054 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2055 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2057 if (playsize > 0) {
2058 sh_audio->a_out_buffer_len -= playsize;
2059 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2060 sh_audio->a_out_buffer_len);
2061 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2063 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2064 // Sanity check to avoid hanging in case current ao doesn't output
2065 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2066 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2067 sh_audio->a_out_buffer_len = 0;
2070 return 1;
2073 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2074 float *aq_sleep_time)
2076 struct MPOpts *opts = &mpctx->opts;
2077 int frame_time_remaining = 0;
2078 current_module="calc_sleep_time";
2080 *time_frame -= get_relative_time(mpctx); // reset timer
2082 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2083 float delay = mpctx->audio_out->get_delay();
2084 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2086 if (autosync) {
2088 * Adjust this raw delay value by calculating the expected
2089 * delay for this frame and generating a new value which is
2090 * weighted between the two. The higher autosync is, the
2091 * closer to the delay value gets to that which "-nosound"
2092 * would have used, and the longer it will take for A/V
2093 * sync to settle at the right value (but it eventually will.)
2094 * This settling time is very short for values below 100.
2096 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2097 float difference = delay - predicted;
2098 delay = predicted + difference / (float)autosync;
2101 *time_frame = delay - mpctx->delay / opts->playback_speed;
2103 // delay = amount of audio buffered in soundcard/driver
2104 if (delay > 0.25) delay=0.25; else
2105 if (delay < 0.10) delay=0.10;
2106 if (*time_frame > delay*0.6) {
2107 // sleep time too big - may cause audio drops (buffer underrun)
2108 frame_time_remaining = 1;
2109 *time_frame = delay*0.5;
2111 } else {
2112 // If we're lagging more than 200 ms behind the right playback rate,
2113 // don't try to "catch up".
2114 // If benchmark is set always output frames as fast as possible
2115 // without sleeping.
2116 if (*time_frame < -0.2 || benchmark)
2117 *time_frame = 0;
2120 *aq_sleep_time += *time_frame;
2123 //============================== SLEEP: ===================================
2125 // flag 256 means: libvo driver does its timing (dvb card)
2126 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2127 *time_frame = timing_sleep(mpctx, *time_frame);
2128 return frame_time_remaining;
2131 int reinit_video_chain(struct MPContext *mpctx)
2133 struct MPOpts *opts = &mpctx->opts;
2134 sh_video_t * const sh_video = mpctx->sh_video;
2135 double ar=-1.0;
2136 //================== Init VIDEO (codec & libvo) ==========================
2137 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2138 current_module="preinit_libvo";
2140 //shouldn't we set dvideo->id=-2 when we fail?
2141 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2142 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2143 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2144 goto err_out;
2146 mpctx->initialized_flags|=INITIALIZED_VO;
2149 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2150 mpctx->sh_video->stream_aspect = ar;
2151 current_module="init_video_filters";
2153 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2154 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2156 #ifdef CONFIG_MENU
2157 if(use_menu) {
2158 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2159 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2160 if(!vf_menu) {
2161 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2162 use_menu = 0;
2165 if(vf_menu)
2166 sh_video->vfilter = vf_menu;
2167 #endif
2169 #ifdef CONFIG_ASS
2170 if(ass_enabled) {
2171 int i;
2172 int insert = 1;
2173 if (opts->vf_settings)
2174 for (i = 0; opts->vf_settings[i].name; ++i)
2175 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2176 insert = 0;
2177 break;
2179 if (insert) {
2180 extern vf_info_t vf_info_ass;
2181 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2182 char* vf_arg[] = {"auto", "1", NULL};
2183 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2184 if (vf_ass)
2185 sh_video->vfilter = vf_ass;
2186 else
2187 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2190 #endif
2192 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2194 #ifdef CONFIG_ASS
2195 if (ass_enabled)
2196 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2197 #endif
2199 current_module="init_video_codec";
2201 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2202 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2203 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2205 if(!sh_video->initialized){
2206 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2207 goto err_out;
2210 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2212 if (sh_video->codec)
2213 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2215 sh_video->last_pts = MP_NOPTS_VALUE;
2216 sh_video->num_buffered_pts = 0;
2217 sh_video->next_frame_time = 0;
2219 if(auto_quality>0){
2220 // Auto quality option enabled
2221 output_quality=get_video_quality_max(sh_video);
2222 if(auto_quality>output_quality) auto_quality=output_quality;
2223 else output_quality=auto_quality;
2224 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2225 set_video_quality(sh_video,output_quality);
2228 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2230 current_module="init_vo";
2232 return 1;
2234 err_out:
2235 mpctx->sh_video = mpctx->d_video->sh = NULL;
2236 return 0;
2239 static double update_video(struct MPContext *mpctx, int *blit_frame)
2241 struct MPOpts *opts = &mpctx->opts;
2242 sh_video_t * const sh_video = mpctx->sh_video;
2243 //-------------------- Decode a frame: -----------------------
2244 double frame_time;
2245 *blit_frame = 0; // Don't blit if we hit EOF
2246 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2247 mpctx->osd); // hack for vf_expand
2248 if (!opts->correct_pts) {
2249 unsigned char* start=NULL;
2250 void *decoded_frame = NULL;
2251 int drop_frame=0;
2252 int in_size;
2254 current_module = "video_read_frame";
2255 frame_time = sh_video->next_frame_time;
2256 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2257 &start, force_fps);
2258 #ifdef CONFIG_DVDNAV
2259 /// wait, still frame or EOF
2260 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2261 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2262 if (mpctx->d_video) mpctx->d_video->eof = 0;
2263 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2264 mpctx->stream->eof = 0;
2265 } else
2266 #endif
2267 if (in_size < 0)
2268 return -1;
2269 if (in_size > max_framesize)
2270 max_framesize = in_size; // stats
2271 sh_video->timer += frame_time;
2272 if (mpctx->sh_audio)
2273 mpctx->delay -= frame_time;
2274 // video_read_frame can change fps (e.g. for ASF video)
2275 vo_fps = sh_video->fps;
2276 drop_frame = check_framedrop(mpctx, frame_time);
2277 update_subtitles(sh_video, mpctx->d_sub, 0);
2278 update_teletext(sh_video, mpctx->demuxer, 0);
2279 update_osd_msg(mpctx);
2280 current_module = "decode_video";
2281 #ifdef CONFIG_DVDNAV
2282 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size,&start,
2283 decoded_frame);
2284 /// still frame has been reached, no need to decode
2285 if (in_size > 0 && !decoded_frame)
2286 #endif
2287 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2288 sh_video->pts);
2289 #ifdef CONFIG_DVDNAV
2290 /// save back last still frame for future display
2291 mp_dvdnav_save_smpi(mpctx, in_size, start, decoded_frame);
2292 #endif
2293 current_module = "filter_video";
2294 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2295 sh_video->pts,
2296 mpctx->osd));
2298 else {
2299 int res = generate_video_frame(mpctx);
2300 if (!res)
2301 return -1;
2302 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS,
2303 &sh_video->pts);
2304 if (sh_video->pts == MP_NOPTS_VALUE) {
2305 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2306 sh_video->pts = sh_video->last_pts;
2308 if (sh_video->last_pts == MP_NOPTS_VALUE)
2309 sh_video->last_pts= sh_video->pts;
2310 else if (sh_video->last_pts >= sh_video->pts) {
2311 sh_video->last_pts = sh_video->pts;
2312 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2314 frame_time = sh_video->pts - sh_video->last_pts;
2315 sh_video->last_pts = sh_video->pts;
2316 sh_video->timer += frame_time;
2317 if(mpctx->sh_audio)
2318 mpctx->delay -= frame_time;
2319 *blit_frame = res > 0;
2321 return frame_time;
2324 static void pause_loop(struct MPContext *mpctx)
2326 mp_cmd_t* cmd;
2327 if (!quiet) {
2328 // Small hack to display the pause message on the OSD line.
2329 // The pause string is: "\n == PAUSE == \r" so we need to
2330 // take the first and the last char out
2331 if (term_osd && !mpctx->sh_video) {
2332 char msg[128] = MSGTR_Paused;
2333 int mlen = strlen(msg);
2334 msg[mlen-1] = '\0';
2335 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2336 update_osd_msg(mpctx);
2337 } else
2338 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2339 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2341 #ifdef CONFIG_GUI
2342 if (use_gui)
2343 guiGetEvent(guiCEvent, (char *)guiSetPause);
2344 #endif
2345 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2346 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2348 if (mpctx->audio_out && mpctx->sh_audio)
2349 mpctx->audio_out->pause(); // pause audio, keep data if possible
2351 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2352 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2353 if (cmd) {
2354 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2355 run_command(mpctx, cmd);
2356 mp_cmd_free(cmd);
2357 continue;
2359 if (mpctx->sh_video && mpctx->video_out)
2360 vo_check_events(mpctx->video_out);
2361 #ifdef CONFIG_GUI
2362 if (use_gui) {
2363 guiEventHandling();
2364 guiGetEvent(guiReDraw, NULL);
2365 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2366 break;
2368 #endif
2369 #ifdef CONFIG_MENU
2370 if (vf_menu)
2371 vf_menu_pause_update(vf_menu);
2372 #endif
2373 usec_sleep(20000);
2375 if (cmd && cmd->id == MP_CMD_PAUSE) {
2376 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2377 mp_cmd_free(cmd);
2379 mpctx->osd_function=OSD_PLAY;
2380 if (mpctx->audio_out && mpctx->sh_audio)
2381 mpctx->audio_out->resume(); // resume audio
2382 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2383 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2384 (void)get_relative_time(mpctx); // ignore time that passed during pause
2385 #ifdef CONFIG_GUI
2386 if (use_gui) {
2387 if (guiIntfStruct.Playing == guiSetStop)
2388 mpctx->eof = 1;
2389 else
2390 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2392 #endif
2395 static void print_version(void){
2396 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2398 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2399 GetCpuCaps(&gCpuCaps);
2400 #ifdef ARCH_X86
2401 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2402 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2403 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2404 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2405 #ifdef RUNTIME_CPUDETECT
2406 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2407 #else
2408 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2409 #ifdef HAVE_MMX
2410 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2411 #endif
2412 #ifdef HAVE_MMX2
2413 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2414 #endif
2415 #ifdef HAVE_3DNOW
2416 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2417 #endif
2418 #ifdef HAVE_3DNOWEX
2419 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2420 #endif
2421 #ifdef HAVE_SSE
2422 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2423 #endif
2424 #ifdef HAVE_SSE2
2425 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2426 #endif
2427 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2428 #endif /* RUNTIME_CPUDETECT */
2429 #endif /* ARCH_X86 */
2433 // Find the right mute status and record position for new file position
2434 static void edl_seek_reset(MPContext *mpctx)
2436 mpctx->edl_muted = 0;
2437 next_edl_record = edl_records;
2439 while (next_edl_record) {
2440 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2441 break;
2443 if (next_edl_record->action == EDL_MUTE)
2444 mpctx->edl_muted = !mpctx->edl_muted;
2445 next_edl_record = next_edl_record->next;
2447 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2448 mixer_mute(&mpctx->mixer);
2452 // Execute EDL command for the current position if one exists
2453 static void edl_update(MPContext *mpctx)
2455 if (!next_edl_record)
2456 return;
2458 if (!mpctx->sh_video) {
2459 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2460 free_edl(edl_records);
2461 next_edl_record = NULL;
2462 edl_records = NULL;
2463 return;
2466 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2467 if (next_edl_record->action == EDL_SKIP) {
2468 mpctx->osd_function = OSD_FFW;
2469 mpctx->abs_seek_pos = 0;
2470 mpctx->rel_seek_secs = next_edl_record->length_sec;
2471 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2472 "[%f], length [%f]\n", next_edl_record->start_sec,
2473 next_edl_record->stop_sec, next_edl_record->length_sec);
2474 edl_decision = 1;
2476 else if (next_edl_record->action == EDL_MUTE) {
2477 mpctx->edl_muted = !mpctx->edl_muted;
2478 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2479 mixer_mute(&mpctx->mixer);
2480 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2481 next_edl_record->start_sec );
2483 next_edl_record = next_edl_record->next;
2488 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2489 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2490 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2491 static int seek(MPContext *mpctx, double amount, int style)
2493 current_module = "seek";
2494 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2495 return -1;
2497 if (mpctx->sh_video) {
2498 current_module = "seek_video_reset";
2499 resync_video_stream(mpctx->sh_video);
2500 if (mpctx->video_out->config_ok)
2501 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2502 mpctx->sh_video->num_buffered_pts = 0;
2503 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2504 mpctx->num_buffered_frames = 0;
2505 mpctx->delay = 0;
2506 // Not all demuxers set d_video->pts during seek, so this value
2507 // (which is used by at least vobsub and edl code below) may
2508 // be completely wrong (probably 0).
2509 mpctx->sh_video->pts = mpctx->d_video->pts;
2510 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2511 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2514 if (mpctx->sh_audio) {
2515 current_module = "seek_audio_reset";
2516 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2517 mpctx->sh_audio->a_buffer_len = 0;
2518 mpctx->sh_audio->a_out_buffer_len = 0;
2521 if (vo_vobsub && mpctx->sh_video) {
2522 current_module = "seek_vobsub_reset";
2523 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2526 edl_seek_reset(mpctx);
2528 c_total = 0;
2529 max_pts_correction = 0.1;
2530 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2531 drop_frame_cnt = 0;
2533 current_module = NULL;
2534 return 0;
2538 static int read_keys(void *ctx, int fd)
2540 getch2(ctx);
2541 return mplayer_get_key(ctx, 0);
2545 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2546 * file for some tools to link against. */
2547 #ifndef DISABLE_MAIN
2548 int main(int argc,char* argv[]){
2551 char * mem_ptr;
2553 // movie info:
2555 /* Flag indicating whether MPlayer should exit without playing anything. */
2556 int opt_exit = 0;
2558 //float a_frame=0; // Audio
2560 int i;
2562 int gui_no_filename=0;
2564 struct MPContext *mpctx = &(struct MPContext){
2565 .osd_function = OSD_PLAY,
2566 .begin_skip = MP_NOPTS_VALUE,
2567 .play_tree_step = 1,
2568 .global_sub_pos = -1,
2569 .set_of_sub_pos = -1,
2570 .file_format = DEMUXER_TYPE_UNKNOWN,
2571 .last_dvb_step = 1,
2574 InitTimer();
2575 srand(GetTimerMS());
2577 mp_msg_init();
2579 #ifdef CONFIG_X11
2580 mpctx->x11_state = vo_x11_init_state();
2581 #endif
2582 struct MPOpts *opts = &mpctx->opts;
2583 set_default_mplayer_options(opts);
2584 // Create the config context and register the options
2585 mpctx->mconfig = m_config_new(opts, cfg_include);
2586 m_config_register_options(mpctx->mconfig,mplayer_opts);
2587 mp_input_register_options(mpctx->mconfig);
2589 // Preparse the command line
2590 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2592 print_version();
2593 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2594 set_path_env();
2595 #endif
2597 #ifdef CONFIG_TV
2598 stream_tv_defaults.immediate = 1;
2599 #endif
2601 if (argc > 1 && argv[1] &&
2602 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2603 use_gui = !strcmp(argv[1], "-gui");
2604 } else
2605 if ( argv[0] )
2607 char *base = strrchr(argv[0], '/');
2608 if (!base)
2609 base = strrchr(argv[0], '\\');
2610 if (!base)
2611 base = argv[0];
2612 if(strstr(base, "gmplayer"))
2613 use_gui=1;
2616 parse_cfgfiles(mpctx, mpctx->mconfig);
2618 #ifdef CONFIG_GUI
2619 if ( use_gui ) cfg_read();
2620 #endif
2622 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2623 if(mpctx->playtree == NULL)
2624 opt_exit = 1;
2625 else {
2626 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2627 if(mpctx->playtree) {
2628 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2629 if(mpctx->playtree_iter) {
2630 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2631 play_tree_iter_free(mpctx->playtree_iter);
2632 mpctx->playtree_iter = NULL;
2634 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2638 mpctx->key_fifo = mp_fifo_create(opts);
2640 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2641 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2642 if(runningmplayer && mpctx->filename && use_gui){
2643 COPYDATASTRUCT csData;
2644 char file[MAX_PATH];
2645 char *filepart = mpctx->filename;
2646 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2647 csData.dwData = 0;
2648 csData.cbData = strlen(file)*2;
2649 csData.lpData = file;
2650 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2653 #endif
2655 #if defined(__MINGW32__) || defined(__CYGWIN__)
2656 // request 1ms timer resolution
2657 timeBeginPeriod(1);
2658 if(proc_priority){
2659 int i;
2660 for(i=0; priority_presets_defs[i].name; i++){
2661 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2662 break;
2664 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
2665 priority_presets_defs[i].name);
2666 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2668 #endif
2669 #ifndef CONFIG_GUI
2670 if(use_gui){
2671 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2672 use_gui=0;
2674 #else
2675 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2676 if(use_gui && !vo_init()){
2677 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2678 use_gui=0;
2680 #endif
2681 if (use_gui && mpctx->playtree_iter){
2682 char cwd[PATH_MAX+2];
2683 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2684 play_tree_iter_free(mpctx->playtree_iter);
2685 mpctx->playtree_iter=NULL;
2687 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2689 strcat(cwd, "/");
2690 // Prefix relative paths with current working directory
2691 play_tree_add_bpf(mpctx->playtree, cwd);
2693 // Import initital playtree into GUI.
2694 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2696 #endif /* CONFIG_GUI */
2698 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2699 list_video_out();
2700 opt_exit = 1;
2703 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2704 list_audio_out();
2705 opt_exit = 1;
2708 /* Check codecs.conf. */
2709 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2710 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2711 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2712 if(!parse_codec_cfg(NULL)){
2713 exit_player_with_rc(mpctx, NULL, 0);
2715 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2718 free( mem_ptr ); // release the buffer created by get_path()
2721 #if 0
2722 if(video_codec_list){
2723 int i;
2724 video_codec=video_codec_list[0];
2725 for(i=0;video_codec_list[i];i++)
2726 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2728 #endif
2729 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2730 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2731 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2732 list_codecs(1);
2733 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2734 opt_exit = 1;
2736 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2737 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2738 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2739 list_codecs(0);
2740 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2741 opt_exit = 1;
2743 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2744 vfm_help();
2745 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2746 opt_exit = 1;
2748 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2749 afm_help();
2750 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2751 opt_exit = 1;
2753 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2754 af_help();
2755 printf("\n");
2756 opt_exit = 1;
2758 #ifdef CONFIG_X11
2759 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2760 fstype_help();
2761 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2762 opt_exit = 1;
2764 #endif
2765 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2766 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2767 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2768 demuxer_help();
2769 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2770 opt_exit = 1;
2772 if(list_properties) {
2773 property_print_help();
2774 opt_exit = 1;
2777 if(opt_exit)
2778 exit_player(mpctx, NULL);
2780 if (player_idle_mode && use_gui) {
2781 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2782 exit_player_with_rc(mpctx, NULL, 1);
2785 if(!mpctx->filename && !player_idle_mode){
2786 if(!use_gui){
2787 // no file/vcd/dvd -> show HELP:
2788 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2789 exit_player_with_rc(mpctx, NULL, 0);
2790 } else gui_no_filename=1;
2793 /* Display what configure line was used */
2794 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2796 // Many users forget to include command line in bugreports...
2797 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2798 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2799 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2800 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2803 //------ load global data first ------
2805 mpctx->osd = osd_create();
2807 // check font
2808 #ifdef CONFIG_FREETYPE
2809 init_freetype();
2810 #endif
2811 #ifdef CONFIG_FONTCONFIG
2812 if(font_fontconfig <= 0)
2814 #endif
2815 #ifdef CONFIG_BITMAP_FONT
2816 if(font_name){
2817 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2818 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2819 filename_recode(font_name));
2820 } else {
2821 // try default:
2822 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2823 free(mem_ptr); // release the buffer created by get_path()
2824 if(!vo_font)
2825 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2827 if (sub_font_name)
2828 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2829 else
2830 mpctx->osd->sub_font = vo_font;
2831 #endif
2832 #ifdef CONFIG_FONTCONFIG
2834 #endif
2836 #ifdef CONFIG_ASS
2837 ass_library = ass_init();
2838 #endif
2840 #ifdef HAVE_RTC
2841 if(!nortc)
2843 // seteuid(0); /* Can't hurt to try to get root here */
2844 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2845 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2846 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2847 else {
2848 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2850 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2851 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2852 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2853 close (rtc_fd);
2854 rtc_fd = -1;
2855 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2856 /* variable only by the root */
2857 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2858 close (rtc_fd);
2859 rtc_fd = -1;
2860 } else
2861 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2864 #ifdef CONFIG_GUI
2865 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2866 // and now ? -- Pontscho
2867 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2868 #endif
2869 if(rtc_fd<0)
2870 #endif /* HAVE_RTC */
2871 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2872 softsleep?"software":timer_name);
2874 #ifdef HAVE_TERMCAP
2875 if ( !use_gui ) load_termcap(NULL); // load key-codes
2876 #endif
2878 // ========== Init keyboard FIFO (connection to libvo) ============
2880 // Init input system
2881 current_module = "init_input";
2882 mpctx->input = mp_input_init(&opts->input, use_gui);
2883 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2884 if(slave_mode)
2885 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2886 else if(!noconsolecontrols)
2887 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2888 // Set the libstream interrupt callback
2889 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2891 #ifdef CONFIG_MENU
2892 if(use_menu) {
2893 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2894 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2895 else {
2896 menu_cfg = get_path("menu.conf");
2897 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2898 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2899 else {
2900 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2901 MPLAYER_CONFDIR "/menu.conf"))
2902 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2903 else {
2904 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2905 use_menu = 0;
2910 #endif
2912 current_module = NULL;
2914 /// Catch signals
2915 #ifndef __MINGW32__
2916 signal(SIGCHLD,child_sighandler);
2917 #endif
2919 #ifdef CONFIG_CRASH_DEBUG
2920 prog_path = argv[0];
2921 #endif
2922 //========= Catch terminate signals: ================
2923 // terminate requests:
2924 signal(SIGTERM,exit_sighandler); // kill
2925 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2927 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2929 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2930 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2931 #ifdef CONFIG_SIGHANDLER
2932 // fatal errors:
2933 signal(SIGBUS,exit_sighandler); // bus error
2934 signal(SIGSEGV,exit_sighandler); // segfault
2935 signal(SIGILL,exit_sighandler); // illegal instruction
2936 signal(SIGFPE,exit_sighandler); // floating point exc.
2937 signal(SIGABRT,exit_sighandler); // abort()
2938 #ifdef CONFIG_CRASH_DEBUG
2939 if (crash_debug)
2940 signal(SIGTRAP,exit_sighandler);
2941 #endif
2942 #endif
2944 #ifdef CONFIG_GUI
2945 if(use_gui){
2946 guiInit();
2947 guiGetEvent(guiSetContext, mpctx);
2948 mpctx->initialized_flags|=INITIALIZED_GUI;
2949 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2951 #endif
2953 // ******************* Now, let's see the per-file stuff ********************
2955 play_next_file:
2957 // init global sub numbers
2958 mpctx->global_sub_size = 0;
2959 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2961 if (mpctx->filename) {
2962 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2963 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2964 load_per_file_config (mpctx->mconfig, mpctx->filename);
2967 if (opts->video_driver_list)
2968 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2969 if (opts->audio_driver_list)
2970 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2972 // We must enable getch2 here to be able to interrupt network connection
2973 // or cache filling
2974 if(!noconsolecontrols && !slave_mode){
2975 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2976 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2977 else
2978 getch2_enable(); // prepare stdin for hotkeys...
2979 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2980 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2983 // =================== GUI idle loop (STOP state) ===========================
2984 #ifdef CONFIG_GUI
2985 if ( use_gui ) {
2986 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2987 guiGetEvent( guiSetDefaults,0 );
2988 while ( guiIntfStruct.Playing != 1 )
2990 mp_cmd_t* cmd;
2991 usec_sleep(20000);
2992 guiEventHandling();
2993 guiGetEvent( guiReDraw,NULL );
2994 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
2995 guiGetEvent(guiIEvent, (char *)cmd->id);
2996 mp_cmd_free(cmd);
2999 guiGetEvent( guiSetParameters,NULL );
3000 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3002 play_tree_t * entry = play_tree_new();
3003 play_tree_add_file( entry,guiIntfStruct.Filename );
3004 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3005 else mpctx->playtree=play_tree_new();
3006 play_tree_set_child( mpctx->playtree,entry );
3007 if(mpctx->playtree)
3009 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3010 if(mpctx->playtree_iter)
3012 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3014 play_tree_iter_free(mpctx->playtree_iter);
3015 mpctx->playtree_iter = NULL;
3017 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3022 #endif /* CONFIG_GUI */
3024 while (player_idle_mode && !mpctx->filename) {
3025 play_tree_t * entry = NULL;
3026 mp_cmd_t * cmd;
3027 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3028 if (mpctx->video_out)
3029 vo_check_events(mpctx->video_out);
3030 usec_sleep(20000);
3032 switch (cmd->id) {
3033 case MP_CMD_LOADFILE:
3034 // prepare a tree entry with the new filename
3035 entry = play_tree_new();
3036 play_tree_add_file(entry, cmd->args[0].v.s);
3037 // The entry is added to the main playtree after the switch().
3038 break;
3039 case MP_CMD_LOADLIST:
3040 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3041 break;
3042 case MP_CMD_QUIT:
3043 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3044 break;
3045 case MP_CMD_GET_PROPERTY:
3046 case MP_CMD_SET_PROPERTY:
3047 case MP_CMD_STEP_PROPERTY:
3048 run_command(mpctx, cmd);
3049 break;
3052 mp_cmd_free(cmd);
3054 if (entry) { // user entered a command that gave a valid entry
3055 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3056 play_tree_free_list(mpctx->playtree->child, 1);
3057 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3059 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3061 play_tree_set_child(mpctx->playtree, entry);
3063 /* Make iterator start at the top the of tree. */
3064 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3065 if (!mpctx->playtree_iter) continue;
3067 // find the first real item in the tree
3068 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3069 // no items!
3070 play_tree_iter_free(mpctx->playtree_iter);
3071 mpctx->playtree_iter = NULL;
3072 continue; // wait for next command
3074 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3077 //---------------------------------------------------------------------------
3079 if(mpctx->filename)
3080 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3081 filename_recode(mpctx->filename));
3083 if (edl_filename) {
3084 if (edl_records) free_edl(edl_records);
3085 next_edl_record = edl_records = edl_parse_file();
3087 if (edl_output_filename) {
3088 if (edl_fd) fclose(edl_fd);
3089 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3091 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3092 filename_recode(edl_output_filename));
3096 //==================== Open VOB-Sub ============================
3098 current_module="vobsub";
3099 if (vobsub_name){
3100 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3101 if(vo_vobsub==NULL)
3102 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3103 filename_recode(vobsub_name));
3104 } else if (sub_auto && mpctx->filename){
3105 /* try to autodetect vobsub from movie filename ::atmos */
3106 char *buf = strdup(mpctx->filename), *psub;
3107 char *pdot = strrchr(buf, '.');
3108 char *pslash = strrchr(buf, '/');
3109 #if defined(__MINGW32__) || defined(__CYGWIN__)
3110 if (!pslash) pslash = strrchr(buf, '\\');
3111 #endif
3112 if (pdot && (!pslash || pdot > pslash))
3113 *pdot = '\0';
3114 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3115 /* try from ~/.mplayer/sub */
3116 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3117 char *bname;
3118 int l;
3119 bname = strrchr(buf,'/');
3120 #if defined(__MINGW32__) || defined(__CYGWIN__)
3121 if(!bname) bname = strrchr(buf,'\\');
3122 #endif
3123 if(bname) bname++;
3124 else bname = buf;
3125 l = strlen(psub) + strlen(bname) + 1;
3126 psub = realloc(psub,l);
3127 strcat(psub,bname);
3128 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3129 free(psub);
3131 free(buf);
3133 if(vo_vobsub){
3134 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3135 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3136 // check if vobsub requested only to display forced subtitles
3137 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
3139 // setup global sub numbering
3140 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3141 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3144 //============ Open & Sync STREAM --- fork cache2 ====================
3146 mpctx->stream=NULL;
3147 mpctx->demuxer=NULL;
3148 if (mpctx->d_audio) {
3149 //free_demuxer_stream(mpctx->d_audio);
3150 mpctx->d_audio=NULL;
3152 if (mpctx->d_video) {
3153 //free_demuxer_stream(d_video);
3154 mpctx->d_video=NULL;
3156 mpctx->sh_audio=NULL;
3157 mpctx->sh_video=NULL;
3159 current_module="open_stream";
3160 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3161 if(!mpctx->stream) { // error...
3162 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3163 goto goto_next_file;
3165 mpctx->initialized_flags|=INITIALIZED_STREAM;
3167 #ifdef CONFIG_GUI
3168 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3169 #endif
3171 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3172 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3173 "playlist support will not be used automatically.\n"
3174 "MPlayer's playlist code is unsafe and should only be used with "
3175 "trusted sources.\nPlayback will probably fail.\n\n");
3176 #if 0
3177 play_tree_t* entry;
3178 // Handle playlist
3179 current_module="handle_playlist";
3180 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3181 filename_recode(mpctx->filename));
3182 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3183 mpctx->eof=playtree_add_playlist(mpctx, entry);
3184 goto goto_next_file;
3185 #endif
3187 mpctx->stream->start_pos+=seek_to_byte;
3189 if(stream_dump_type==5){
3190 unsigned char buf[4096];
3191 int len;
3192 FILE *f;
3193 current_module="dumpstream";
3194 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3195 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3196 exit_player(mpctx, MSGTR_Exit_error);
3198 stream_reset(mpctx->stream);
3199 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3200 f=fopen(stream_dump_name,"wb");
3201 if(!f){
3202 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3203 exit_player(mpctx, MSGTR_Exit_error);
3205 if (dvd_chapter > 1) {
3206 int chapter = dvd_chapter - 1;
3207 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3209 while(!mpctx->stream->eof && !async_quit_request){
3210 len=stream_read(mpctx->stream,buf,4096);
3211 if(len>0) {
3212 if(fwrite(buf,len,1,f) != 1) {
3213 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3214 exit_player(mpctx, MSGTR_Exit_error);
3218 if(fclose(f)) {
3219 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3220 exit_player(mpctx, MSGTR_Exit_error);
3222 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3223 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3226 #ifdef CONFIG_DVDREAD
3227 if(mpctx->stream->type==STREAMTYPE_DVD){
3228 current_module="dvd lang->id";
3229 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3230 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3231 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3232 // setup global sub numbering
3233 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3234 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3235 current_module=NULL;
3237 #endif
3239 #ifdef CONFIG_DVDNAV
3240 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3241 current_module="dvdnav lang->id";
3242 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3243 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3244 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3245 // setup global sub numbering
3246 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3247 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3248 current_module=NULL;
3250 #endif
3252 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3253 goto_enable_cache:
3254 if(stream_cache_size>0){
3255 current_module="enable_cache";
3256 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3257 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3258 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3259 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3262 //============ Open DEMUXERS --- DETECT file type =======================
3263 current_module="demux_open";
3265 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3267 // HACK to get MOV Reference Files working
3269 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3271 unsigned char* playlist_entry;
3272 play_tree_t *list = NULL, *entry = NULL;
3274 current_module="handle_demux_playlist";
3275 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3277 char *temp, *bname;
3279 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3280 filename_recode(playlist_entry));
3282 bname=mp_basename(playlist_entry);
3283 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3284 continue;
3286 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3287 continue;
3289 entry = play_tree_new();
3291 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3293 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3294 if (temp)
3296 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3297 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3298 strcat(temp, playlist_entry);
3299 play_tree_add_file(entry,temp);
3300 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3301 free(temp);
3304 else
3305 play_tree_add_file(entry,playlist_entry);
3307 if(!list)
3308 list = entry;
3309 else
3310 play_tree_append_entry(list,entry);
3312 free_demuxer(mpctx->demuxer);
3313 mpctx->demuxer = NULL;
3315 if (list)
3317 entry = play_tree_new();
3318 play_tree_set_child(entry,list);
3319 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3320 goto goto_next_file;
3324 if(!mpctx->demuxer)
3325 goto goto_next_file;
3326 if(dvd_chapter>1) {
3327 float pts;
3328 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3329 seek(mpctx, pts, SEEK_ABSOLUTE);
3332 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3334 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3335 int i;
3336 int maxid = -1;
3337 // setup global sub numbering
3338 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3339 for (i = 0; i < MAX_S_STREAMS; i++)
3340 if (mpctx->demuxer->s_streams[i])
3341 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3342 mpctx->global_sub_size += maxid + 1;
3344 // Make opts->sub_id always selectable if set.
3345 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3346 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3348 #ifdef CONFIG_ASS
3349 if (ass_enabled && ass_library) {
3350 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3351 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3352 if (extract_embedded_fonts &&
3353 att->name && att->type && att->data && att->data_size &&
3354 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3355 strcmp(att->type, "application/x-font") == 0))
3356 ass_add_font(ass_library, att->name, att->data, att->data_size);
3359 #endif
3361 current_module="demux_open2";
3363 //file_format=demuxer->file_format;
3365 mpctx->d_audio=mpctx->demuxer->audio;
3366 mpctx->d_video=mpctx->demuxer->video;
3367 mpctx->d_sub=mpctx->demuxer->sub;
3369 // select audio stream
3370 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3372 // DUMP STREAMS:
3373 if((stream_dump_type)&&(stream_dump_type<4)){
3374 FILE *f;
3375 demux_stream_t *ds=NULL;
3376 current_module="dump";
3377 // select stream to dump
3378 switch(stream_dump_type){
3379 case 1: ds=mpctx->d_audio;break;
3380 case 2: ds=mpctx->d_video;break;
3381 case 3: ds=mpctx->d_sub;break;
3383 if(!ds){
3384 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3385 exit_player(mpctx, MSGTR_Exit_error);
3387 // disable other streams:
3388 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3389 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3390 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3391 // let's dump it!
3392 f=fopen(stream_dump_name,"wb");
3393 if(!f){
3394 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3395 exit_player(mpctx, MSGTR_Exit_error);
3397 while(!ds->eof){
3398 unsigned char* start;
3399 int in_size=ds_get_packet(ds,&start);
3400 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3401 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3402 if(in_size>0) fwrite(start,in_size,1,f);
3404 fclose(f);
3405 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3406 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3409 mpctx->sh_audio=mpctx->d_audio->sh;
3410 mpctx->sh_video=mpctx->d_video->sh;
3412 if(mpctx->sh_video){
3414 current_module="video_read_properties";
3415 if(!video_read_properties(mpctx->sh_video)) {
3416 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3417 mpctx->sh_video=mpctx->d_video->sh=NULL;
3418 } else {
3419 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3420 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3421 mpctx->sh_video->fps,mpctx->sh_video->frametime
3424 /* need to set fps here for output encoders to pick it up in their init */
3425 if(force_fps){
3426 mpctx->sh_video->fps=force_fps;
3427 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3429 vo_fps = mpctx->sh_video->fps;
3431 if(!mpctx->sh_video->fps && !force_fps){
3432 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3433 mpctx->sh_video=mpctx->d_video->sh=NULL;
3439 if(!mpctx->sh_video && !mpctx->sh_audio){
3440 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3441 #ifdef CONFIG_DVBIN
3442 if(mpctx->stream->type == STREAMTYPE_DVB)
3444 int dir;
3445 int v = mpctx->last_dvb_step;
3446 if(v > 0)
3447 dir = DVB_CHANNEL_HIGHER;
3448 else
3449 dir = DVB_CHANNEL_LOWER;
3451 if(dvb_step_channel(mpctx->stream, dir)) {
3452 mpctx->stop_play = PT_NEXT_ENTRY;
3453 mpctx->dvbin_reopen = 1;
3456 #endif
3457 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3460 /* display clip info */
3461 demux_info_print(mpctx->demuxer);
3463 //================== Read SUBTITLES (DVD & TEXT) ==========================
3464 if(vo_spudec==NULL && mpctx->sh_video &&
3465 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3466 init_vo_spudec(mpctx);
3469 // Apply current settings for forced subs
3470 if (vo_spudec!=NULL)
3471 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3473 if(mpctx->sh_video) {
3474 // after reading video params we should load subtitles because
3475 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3476 // check .sub
3477 current_module="read_subtitles_file";
3478 if(sub_name){
3479 for (i = 0; sub_name[i] != NULL; ++i)
3480 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3482 if(sub_auto) { // auto load sub file ...
3483 char *psub = get_path( "sub/" );
3484 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3485 int i = 0;
3486 free(psub); // release the buffer created by get_path() above
3487 while (tmp[i]) {
3488 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3489 free(tmp[i++]);
3491 free(tmp);
3493 if (mpctx->set_of_sub_size > 0) {
3494 // setup global sub numbering
3495 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3496 mpctx->global_sub_size += mpctx->set_of_sub_size;
3500 if (mpctx->global_sub_size) {
3501 // find the best sub to use
3502 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3503 mpctx->global_sub_pos = -1; // no subs by default
3504 if (vobsub_index_id >= 0) {
3505 // if user asks for a vobsub id, use that first.
3506 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3507 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3508 // if user asks for a dvd sub id, use that next.
3509 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3510 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3511 // if there are text subs to use, use those. (autosubs come last here)
3512 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3513 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3514 // finally select subs by language and container hints
3515 if (opts->sub_id < 0 && dvdsub_lang)
3516 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3517 if (opts->sub_id < 0)
3518 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3519 if (opts->sub_id >= 0)
3520 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3522 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3523 mpctx->global_sub_pos--;
3524 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3525 if(subdata)
3526 switch (stream_dump_type) {
3527 case 3: list_sub_file(subdata); break;
3528 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3529 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3530 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3531 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3532 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3537 filename_recode(mpctx->filename));
3538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3539 if (mpctx->sh_video) {
3540 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3541 if (mpctx->sh_video->format >= 0x20202020)
3542 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3543 else
3544 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3545 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3546 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3547 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3548 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3549 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3551 if (mpctx->sh_audio) {
3552 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3553 if (mpctx->sh_audio->format >= 0x20202020)
3554 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3555 else
3556 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3557 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3558 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3559 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3561 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3562 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3563 if (mpctx->demuxer) {
3564 if (mpctx->demuxer->num_chapters == 0)
3565 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3566 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3569 if(!mpctx->sh_video) goto main; // audio-only
3571 if(!reinit_video_chain(mpctx)) {
3572 if(!mpctx->sh_video){
3573 if(!mpctx->sh_audio) goto goto_next_file;
3574 goto main; // exit_player(MSGTR_Exit_error);
3578 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3579 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3581 #ifdef CONFIG_FREETYPE
3582 force_load_font = 1;
3583 #endif
3585 //================== MAIN: ==========================
3586 main:
3587 current_module="main";
3589 if(playing_msg) {
3590 char* msg = property_expand_string(mpctx, playing_msg);
3591 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3592 free(msg);
3596 // Disable the term OSD in verbose mode
3597 if(verbose) term_osd = 0;
3600 //int frame_corr_num=0; //
3601 //float v_frame=0; // Video
3602 float time_frame=0; // Timer
3603 //float num_frames=0; // number of frames played
3605 int frame_time_remaining=0; // flag
3606 int blit_frame=0;
3607 mpctx->num_buffered_frames=0;
3609 // Make sure old OSD does not stay around,
3610 // e.g. with -fixed-vo and same-resolution files
3611 clear_osd_msgs();
3612 update_osd_msg(mpctx);
3614 //================ SETUP AUDIO ==========================
3616 if(mpctx->sh_audio){
3617 reinit_audio_chain(mpctx);
3618 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3619 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3622 current_module="av_init";
3624 if(mpctx->sh_video){
3625 mpctx->sh_video->timer=0;
3626 if (! ignore_start)
3627 audio_delay += mpctx->sh_video->stream_delay;
3629 if(mpctx->sh_audio){
3630 if (! ignore_start)
3631 audio_delay -= mpctx->sh_audio->stream_delay;
3632 mpctx->delay=-audio_delay;
3635 if(!mpctx->sh_audio){
3636 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3637 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3638 ds_free_packs(mpctx->d_audio); // free buffered chunks
3639 //mpctx->d_audio->id=-2; // do not read audio chunks
3640 //uninit_player(mpctx, INITIALIZED_AO); // close device
3642 if(!mpctx->sh_video){
3643 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3644 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3645 ds_free_packs(mpctx->d_video);
3646 mpctx->d_video->id=-2;
3647 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3650 if (!mpctx->sh_video && !mpctx->sh_audio)
3651 goto goto_next_file;
3653 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3654 if(force_fps && mpctx->sh_video){
3655 vo_fps = mpctx->sh_video->fps=force_fps;
3656 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3657 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3660 #ifdef CONFIG_GUI
3661 if ( use_gui ) {
3662 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3663 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3664 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3665 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3666 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3668 #endif
3670 mp_input_set_section(mpctx->input, NULL);
3671 //TODO: add desired (stream-based) sections here
3672 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3673 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3675 //==================== START PLAYING =======================
3677 if(opts->loop_times>1) opts->loop_times--; else
3678 if(opts->loop_times==1) opts->loop_times = -1;
3680 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3682 total_time_usage_start=GetTimer();
3683 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3684 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3685 play_n_frames=play_n_frames_mf;
3687 if(play_n_frames==0){
3688 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3691 if (seek_to_sec) {
3692 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3693 end_at.pos += seek_to_sec;
3696 if (end_at.type == END_AT_SIZE) {
3697 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3698 end_at.type = END_AT_NONE;
3701 #ifdef CONFIG_DVDNAV
3702 mp_dvdnav_context_free(mpctx);
3703 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3704 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3705 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3707 #endif
3709 get_relative_time(mpctx); // reset current delta
3711 while(!mpctx->stop_play){
3712 float aq_sleep_time=0;
3714 if(dvd_last_chapter>0) {
3715 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3716 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3717 goto goto_next_file;
3720 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3721 mpctx->sh_audio = mpctx->d_audio->sh;
3722 mpctx->sh_audio->ds = mpctx->d_audio;
3723 reinit_audio_chain(mpctx);
3726 /*========================== PLAY AUDIO ============================*/
3728 if (mpctx->sh_audio)
3729 if (!fill_audio_out_buffers(mpctx))
3730 // at eof, all audio at least written to ao
3731 if (!mpctx->sh_video)
3732 mpctx->stop_play = AT_END_OF_FILE;
3735 if(!mpctx->sh_video) {
3736 // handle audio-only case:
3737 double a_pos=0;
3738 // sh_audio can be NULL due to video stream switching
3739 // TODO: handle this better
3740 if(!quiet || end_at.type == END_AT_TIME && mpctx->sh_audio)
3741 a_pos = playing_audio_pts(mpctx);
3743 if(!quiet)
3744 print_status(mpctx, a_pos, 0, 0);
3746 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3747 mpctx->stop_play = PT_NEXT_ENTRY;
3748 update_osd_msg(mpctx);
3750 } else {
3752 /*========================== PLAY VIDEO ============================*/
3754 vo_pts=mpctx->sh_video->timer*90000.0;
3755 vo_fps=mpctx->sh_video->fps;
3757 if (!mpctx->num_buffered_frames) {
3758 double frame_time = update_video(mpctx, &blit_frame);
3759 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3760 if (mpctx->sh_video->vf_initialized < 0) {
3761 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3762 mpctx->stop_play = PT_NEXT_ENTRY;
3763 goto goto_next_file;
3765 if (frame_time < 0)
3766 mpctx->stop_play = AT_END_OF_FILE;
3767 else {
3768 // might return with !eof && !blit_frame if !correct_pts
3769 mpctx->num_buffered_frames += blit_frame;
3770 time_frame += frame_time / opts->playback_speed; // for nosound
3774 // ==========================================================================
3776 // current_module="draw_osd";
3777 // if(vo_config_count) mpctx->video_out->draw_osd();
3779 #ifdef CONFIG_GUI
3780 if(use_gui) guiEventHandling();
3781 #endif
3783 current_module="vo_check_events";
3784 vo_check_events(mpctx->video_out);
3786 #ifdef CONFIG_X11
3787 if (stop_xscreensaver) {
3788 current_module = "stop_xscreensaver";
3789 xscreensaver_heartbeat(mpctx->x11_state);
3791 #endif
3792 if (heartbeat_cmd) {
3793 static unsigned last_heartbeat;
3794 unsigned now = GetTimerMS();
3795 if (now - last_heartbeat > 30000) {
3796 last_heartbeat = now;
3797 system(heartbeat_cmd);
3801 frame_time_remaining = sleep_until_update(mpctx, &time_frame, &aq_sleep_time);
3803 //====================== FLIP PAGE (VIDEO BLT): =========================
3805 current_module="flip_page";
3806 if (!frame_time_remaining && blit_frame) {
3807 unsigned int t2=GetTimer();
3809 vo_flip_page(mpctx->video_out);
3810 mpctx->num_buffered_frames--;
3812 vout_time_usage += (GetTimer() - t2) * 0.000001;
3814 //====================== A-V TIMESTAMP CORRECTION: =========================
3816 adjust_sync_and_print_status(mpctx, frame_time_remaining, time_frame);
3818 //============================ Auto QUALITY ============================
3820 /*Output quality adjustments:*/
3821 if(auto_quality>0){
3822 current_module="autoq";
3823 // float total=0.000001f * (GetTimer()-aq_total_time);
3824 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3825 if(output_quality<auto_quality && aq_sleep_time>0)
3826 ++output_quality;
3827 else
3828 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3829 if(output_quality>1 && aq_sleep_time<0)
3830 --output_quality;
3831 else
3832 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3833 output_quality=0;
3834 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3835 set_video_quality(mpctx->sh_video,output_quality);
3838 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3839 --play_n_frames;
3840 if (play_n_frames <= 0) mpctx->stop_play = PT_NEXT_ENTRY;
3844 // FIXME: add size based support for -endpos
3845 if (end_at.type == END_AT_TIME &&
3846 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3847 mpctx->stop_play = PT_NEXT_ENTRY;
3849 } // end if(mpctx->sh_video)
3851 #ifdef CONFIG_DVDNAV
3852 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3853 nav_highlight_t hl;
3854 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3855 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3856 vo_osd_changed (OSDTYPE_DVDNAV);
3858 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3859 double ar = -1.0;
3860 if (stream_control (mpctx->demuxer->stream,
3861 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3862 != STREAM_UNSUPPORTED)
3863 mpctx->sh_video->stream_aspect = ar;
3866 #endif
3868 //============================ Handle PAUSE ===============================
3870 current_module="pause";
3872 if (mpctx->osd_function == OSD_PAUSE) {
3873 mpctx->was_paused = 1;
3874 pause_loop(mpctx);
3877 // handle -sstep
3878 if(step_sec>0) {
3879 mpctx->osd_function=OSD_FFW;
3880 mpctx->rel_seek_secs+=step_sec;
3883 edl_update(mpctx);
3885 //================= Keyboard events, SEEKing ====================
3887 current_module="key_events";
3890 mp_cmd_t* cmd;
3891 int brk_cmd = 0;
3892 while( !brk_cmd && (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3893 brk_cmd = run_command(mpctx, cmd);
3894 mp_cmd_free(cmd);
3895 if (brk_cmd == 2)
3896 goto goto_enable_cache;
3899 mpctx->was_paused = 0;
3901 /* Looping. */
3902 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3903 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3905 if(opts->loop_times>1) opts->loop_times--; else
3906 if(opts->loop_times==1) opts->loop_times=-1;
3907 play_n_frames=play_n_frames_mf;
3908 mpctx->stop_play=0;
3909 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3910 loop_seek = 1;
3913 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3914 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3915 // Set OSD:
3916 if(!loop_seek){
3917 if( !edl_decision )
3918 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3922 mpctx->rel_seek_secs=0;
3923 mpctx->abs_seek_pos=0;
3924 loop_seek=0;
3925 edl_decision = 0;
3928 #ifdef CONFIG_GUI
3929 if(use_gui){
3930 guiEventHandling();
3931 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3932 // get pos from frame number / total frames
3933 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3934 } else {
3935 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3937 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3938 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3939 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3940 guiGetEvent( guiReDraw,NULL );
3941 guiGetEvent( guiSetVolume,NULL );
3942 if(guiIntfStruct.Playing==0) break; // STOP
3943 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3944 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3945 #ifdef CONFIG_DVDREAD
3946 if ( mpctx->stream->type == STREAMTYPE_DVD )
3948 dvd_priv_t * dvdp = mpctx->stream->priv;
3949 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3951 #endif
3953 #endif /* CONFIG_GUI */
3955 } // while(!mpctx->stop_play)
3957 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
3959 #ifdef CONFIG_DVBIN
3960 if(mpctx->dvbin_reopen)
3962 mpctx->stop_play = 0;
3963 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3964 cache_uninit(mpctx->stream);
3965 mpctx->dvbin_reopen = 0;
3966 goto goto_enable_cache;
3968 #endif
3971 goto_next_file: // don't jump here after ao/vo/getch initialization!
3973 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3975 if(benchmark){
3976 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3977 double total_time_usage;
3978 total_time_usage_start=GetTimer()-total_time_usage_start;
3979 total_time_usage = (float)total_time_usage_start*0.000001;
3980 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3981 video_time_usage,vout_time_usage,audio_time_usage,
3982 total_time_usage-tot,total_time_usage);
3983 if(total_time_usage>0.0)
3984 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3985 100.0*video_time_usage/total_time_usage,
3986 100.0*vout_time_usage/total_time_usage,
3987 100.0*audio_time_usage/total_time_usage,
3988 100.0*(total_time_usage-tot)/total_time_usage,
3989 100.0);
3990 if(total_frame_cnt && frame_dropping)
3991 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3992 total_frame_cnt-drop_frame_cnt,
3993 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3994 drop_frame_cnt,
3995 100*drop_frame_cnt/total_frame_cnt,
3996 total_frame_cnt,
3997 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4000 // time to uninit all, except global stuff:
4001 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4003 if(mpctx->set_of_sub_size > 0) {
4004 current_module="sub_free";
4005 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4006 sub_free(mpctx->set_of_subtitles[i]);
4007 #ifdef CONFIG_ASS
4008 if(mpctx->set_of_ass_tracks[i])
4009 ass_free_track( mpctx->set_of_ass_tracks[i] );
4010 #endif
4012 mpctx->set_of_sub_size = 0;
4014 vo_sub_last = vo_sub=NULL;
4015 subdata=NULL;
4016 #ifdef CONFIG_ASS
4017 ass_track = NULL;
4018 if(ass_library)
4019 ass_clear_fonts(ass_library);
4020 #endif
4022 if (!mpctx->stop_play) // In case some goto jumped here...
4023 mpctx->stop_play = PT_NEXT_ENTRY;
4025 int playtree_direction = 1;
4027 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4028 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4029 play_tree_iter_free(mpctx->playtree_iter);
4030 mpctx->playtree_iter = NULL;
4032 mpctx->play_tree_step = 1;
4033 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4034 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4035 if(mpctx->playtree_iter) {
4036 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4037 play_tree_iter_free(mpctx->playtree_iter);
4038 mpctx->playtree_iter = NULL;
4041 } else if (mpctx->stop_play == PT_STOP) {
4042 play_tree_iter_free(mpctx->playtree_iter);
4043 mpctx->playtree_iter = NULL;
4044 } else { // NEXT PREV SRC
4045 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4048 while(mpctx->playtree_iter != NULL) {
4049 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4050 if(mpctx->filename == NULL) {
4051 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4052 play_tree_iter_free(mpctx->playtree_iter);
4053 mpctx->playtree_iter = NULL;
4055 } else
4056 break;
4059 #ifdef CONFIG_GUI
4060 if(use_gui && !mpctx->playtree_iter) {
4061 #ifdef CONFIG_DVDREAD
4062 if(!guiIntfStruct.DiskChanged)
4063 #endif
4064 mplEnd();
4066 #endif
4068 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4069 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4070 mpctx->stop_play = 0;
4071 goto play_next_file;
4075 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
4077 return 1;
4079 #endif /* DISABLE_MAIN */