core: Clean up OSD seek info logic
[mplayer/glamo.git] / mplayer.c
blob50f1ec4f0aec1b8cadeffa49ff7e5963da86fd03
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include "config.h"
9 #include "talloc.h"
11 #if defined(__MINGW32__) || defined(__CYGWIN__)
12 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
13 #include <windows.h>
14 #endif
15 #include <string.h>
16 #include <unistd.h>
18 // #include <sys/mman.h>
19 #include <sys/types.h>
20 #ifndef __MINGW32__
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #else
24 #define SIGHUP 1 /* hangup */
25 #define SIGQUIT 3 /* quit */
26 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
27 #define SIGBUS 10 /* bus error */
28 #define SIGPIPE 13 /* broken pipe */
29 #endif
31 #include <sys/time.h>
32 #include <sys/stat.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <limits.h>
39 #include <errno.h>
41 #include "mp_msg.h"
43 #define HELP_MP_DEFINE_STATIC
44 #include "help_mp.h"
46 #include "m_option.h"
47 #include "m_config.h"
48 #include "mplayer.h"
49 #include "access_mpcontext.h"
50 #include "m_property.h"
52 #include "cfg-mplayer-def.h"
54 #include "libavutil/intreadwrite.h"
56 #include "subreader.h"
58 #include "mp_osd.h"
59 #include "libvo/video_out.h"
61 #include "libvo/font_load.h"
62 #include "libvo/sub.h"
64 #ifdef CONFIG_X11
65 #include "libvo/x11_common.h"
66 #endif
68 #include "libao2/audio_out.h"
70 #include "codec-cfg.h"
72 #include "edl.h"
74 #include "spudec.h"
75 #include "vobsub.h"
77 #include "osdep/getch2.h"
78 #include "osdep/timer.h"
80 #ifdef CONFIG_GUI
81 #include "gui/interface.h"
82 #endif
84 #include "input/input.h"
86 const int under_mencoder = 0;
87 int slave_mode=0;
88 int player_idle_mode=0;
89 int quiet=0;
90 int enable_mouse_movements=0;
91 float start_volume = -1;
93 #include "osdep/priority.h"
95 char *heartbeat_cmd;
97 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
99 #ifdef HAVE_RTC
100 #ifdef __linux__
101 #include <linux/rtc.h>
102 #else
103 #include <rtc.h>
104 #define RTC_IRQP_SET RTCIO_IRQP_SET
105 #define RTC_PIE_ON RTCIO_PIE_ON
106 #endif /* __linux__ */
107 #endif /* HAVE_RTC */
109 #ifdef CONFIG_TV
110 #include "stream/tv.h"
111 #endif
112 #ifdef CONFIG_RADIO
113 #include "stream/stream_radio.h"
114 #endif
116 #ifdef CONFIG_DVBIN
117 #include "stream/dvbin.h"
118 #include "stream/cache2.h"
119 #endif
121 //**************************************************************************//
122 // Playtree
123 //**************************************************************************//
124 #include "playtree.h"
125 #include "playtreeparser.h"
127 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
128 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
130 //**************************************************************************//
131 // Config
132 //**************************************************************************//
133 #include "parser-cfg.h"
134 #include "parser-mpcmd.h"
136 //**************************************************************************//
137 // Config file
138 //**************************************************************************//
140 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
142 #include "get_path.h"
144 //**************************************************************************//
145 //**************************************************************************//
146 // Input media streaming & demultiplexer:
147 //**************************************************************************//
149 static int max_framesize=0;
151 #include "stream/stream.h"
152 #include "libmpdemux/demuxer.h"
153 #include "libmpdemux/stheader.h"
155 #ifdef CONFIG_DVDREAD
156 #include "stream/stream_dvd.h"
157 #endif
159 #ifdef CONFIG_DVDNAV
160 #include "stream/stream_dvdnav.h"
161 #endif
163 #include "libmpcodecs/dec_audio.h"
164 #include "libmpcodecs/dec_video.h"
165 #include "libmpcodecs/mp_image.h"
166 #include "libmpcodecs/vf.h"
167 #include "libmpcodecs/vd.h"
169 #include "mixer.h"
171 #include "mp_core.h"
172 #include "options.h"
173 #include "defaultopts.h"
175 //**************************************************************************//
176 //**************************************************************************//
178 // Common FIFO functions, and keyboard/event FIFO code
179 #include "mp_fifo.h"
180 int noconsolecontrols=0;
181 //**************************************************************************//
183 // benchmark:
184 double video_time_usage=0;
185 double vout_time_usage=0;
186 static double audio_time_usage=0;
187 static int total_time_usage_start=0;
188 static int total_frame_cnt=0;
189 static int drop_frame_cnt=0; // total number of dropped frames
190 int benchmark=0;
192 // options:
193 int auto_quality=0;
194 static int output_quality=0;
196 int use_gui=0;
198 #ifdef CONFIG_GUI
199 int enqueue=0;
200 #endif
202 static int list_properties = 0;
204 int term_osd = 1;
205 static char* term_osd_esc = "\x1b[A\r\x1b[K";
206 static char* playing_msg = NULL;
207 // seek:
208 static double seek_to_sec;
209 static off_t seek_to_byte=0;
210 static off_t step_sec=0;
212 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
214 // A/V sync:
215 int autosync=0; // 30 might be a good default value.
217 // codecs:
218 char **audio_codec_list=NULL; // override audio codec
219 char **video_codec_list=NULL; // override video codec
220 char **audio_fm_list=NULL; // override audio codec family
221 char **video_fm_list=NULL; // override video codec family
223 // demuxer:
224 extern char *demuxer_name; // override demuxer
225 extern char *audio_demuxer_name; // override audio demuxer
226 extern char *sub_demuxer_name; // override sub demuxer
228 int vobsub_id=-1;
229 char* audio_lang=NULL;
230 char* dvdsub_lang=NULL;
231 static char* spudec_ifo=NULL;
232 int forced_subs_only=0;
233 int file_filter=1;
235 // cache2:
236 int stream_cache_size=-1;
237 #ifdef CONFIG_STREAM_CACHE
238 extern int cache_fill_status;
240 float stream_cache_min_percent=20.0;
241 float stream_cache_seek_min_percent=50.0;
242 #else
243 #define cache_fill_status 0
244 #endif
246 // dump:
247 static char *stream_dump_name="stream.dump";
248 int stream_dump_type=0;
250 // A-V sync:
251 static float default_max_pts_correction=-1;//0.01f;
252 float audio_delay=0;
253 static int ignore_start=0;
255 static int softsleep=0;
257 double force_fps=0;
258 static int force_srate=0;
259 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
260 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
261 static int play_n_frames=-1;
262 static int play_n_frames_mf=-1;
264 // sub:
265 char *font_name=NULL;
266 char *sub_font_name=NULL;
267 extern int font_fontconfig;
268 float font_factor=0.75;
269 char **sub_name=NULL;
270 float sub_delay=0;
271 float sub_fps=0;
272 int sub_auto = 1;
273 char *vobsub_name=NULL;
274 /*DSP!!char *dsp=NULL;*/
275 int subcc_enabled=0;
276 int suboverlap_enabled = 1;
278 #ifdef CONFIG_ASS
279 #include "libass/ass.h"
280 #include "libass/ass_mp.h"
281 #endif
283 char* current_module=NULL; // for debugging
286 // ---
288 #ifdef CONFIG_MENU
289 #include "m_struct.h"
290 #include "libmenu/menu.h"
291 void vf_menu_pause_update(struct vf_instance* vf);
292 extern vf_info_t vf_info_menu;
293 static const vf_info_t* const libmenu_vfs[] = {
294 &vf_info_menu,
295 NULL
297 static vf_instance_t* vf_menu = NULL;
298 int use_menu = 0;
299 static char* menu_cfg = NULL;
300 static char* menu_root = "main";
301 #endif
304 #ifdef HAVE_RTC
305 static int nortc = 1;
306 static char* rtc_device;
307 #endif
309 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
310 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
311 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
312 int use_filedir_conf;
314 #include "mpcommon.h"
315 #include "command.h"
317 #include "metadata.h"
319 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
321 const void *mpctx_get_video_out(MPContext *mpctx)
323 return mpctx->video_out;
326 const void *mpctx_get_audio_out(MPContext *mpctx)
328 return mpctx->audio_out;
331 void *mpctx_get_demuxer(MPContext *mpctx)
333 return mpctx->demuxer;
336 void *mpctx_get_playtree_iter(MPContext *mpctx)
338 return mpctx->playtree_iter;
341 void *mpctx_get_mixer(MPContext *mpctx)
343 return &mpctx->mixer;
346 int mpctx_get_global_sub_size(MPContext *mpctx)
348 return mpctx->global_sub_size;
351 int mpctx_get_osd_function(MPContext *mpctx)
353 return mpctx->osd_function;
356 static float get_relative_time(struct MPContext *mpctx)
358 unsigned int new_time = GetTimer();
359 unsigned int delta = new_time - mpctx->last_time;
360 mpctx->last_time = new_time;
361 return delta * 0.000001;
364 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
365 switch (type)
367 /* check for valid video stream */
368 case META_VIDEO_CODEC:
369 case META_VIDEO_BITRATE:
370 case META_VIDEO_RESOLUTION:
372 if (!mpctx->sh_video)
373 return 0;
374 break;
377 /* check for valid audio stream */
378 case META_AUDIO_CODEC:
379 case META_AUDIO_BITRATE:
380 case META_AUDIO_SAMPLES:
382 if (!mpctx->sh_audio)
383 return 0;
384 break;
387 /* check for valid demuxer */
388 case META_INFO_TITLE:
389 case META_INFO_ARTIST:
390 case META_INFO_ALBUM:
391 case META_INFO_YEAR:
392 case META_INFO_COMMENT:
393 case META_INFO_TRACK:
394 case META_INFO_GENRE:
396 if (!mpctx->demuxer)
397 return 0;
398 break;
401 default:
402 break;
405 return 1;
408 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
409 char **info = mpctx->demuxer->info;
410 int n;
412 if (!info || !tag)
413 return NULL;
415 for (n = 0; info[2*n] != NULL ; n++)
416 if (!strcmp (info[2*n], tag))
417 break;
419 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
422 char *get_metadata(struct MPContext *mpctx, metadata_t type)
424 char *meta = NULL;
425 sh_audio_t * const sh_audio = mpctx->sh_audio;
426 sh_video_t * const sh_video = mpctx->sh_video;
428 if (!is_valid_metadata_type(mpctx, type))
429 return NULL;
431 switch (type)
433 case META_NAME:
435 return strdup (mp_basename2 (mpctx->filename));
438 case META_VIDEO_CODEC:
440 if (sh_video->format == 0x10000001)
441 meta = strdup ("mpeg1");
442 else if (sh_video->format == 0x10000002)
443 meta = strdup ("mpeg2");
444 else if (sh_video->format == 0x10000004)
445 meta = strdup ("mpeg4");
446 else if (sh_video->format == 0x10000005)
447 meta = strdup ("h264");
448 else if (sh_video->format >= 0x20202020)
450 meta = malloc (8);
451 sprintf (meta, "%.4s", (char *) &sh_video->format);
453 else
455 meta = malloc (8);
456 sprintf (meta, "0x%08X", sh_video->format);
458 return meta;
461 case META_VIDEO_BITRATE:
463 meta = malloc (16);
464 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
465 return meta;
468 case META_VIDEO_RESOLUTION:
470 meta = malloc (16);
471 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
472 return meta;
475 case META_AUDIO_CODEC:
477 if (sh_audio->codec && sh_audio->codec->name)
478 meta = strdup (sh_audio->codec->name);
479 return meta;
482 case META_AUDIO_BITRATE:
484 meta = malloc (16);
485 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
486 return meta;
489 case META_AUDIO_SAMPLES:
491 meta = malloc (16);
492 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
493 return meta;
496 /* check for valid demuxer */
497 case META_INFO_TITLE:
498 return get_demuxer_info(mpctx, "Title");
500 case META_INFO_ARTIST:
501 return get_demuxer_info(mpctx, "Artist");
503 case META_INFO_ALBUM:
504 return get_demuxer_info(mpctx, "Album");
506 case META_INFO_YEAR:
507 return get_demuxer_info(mpctx, "Year");
509 case META_INFO_COMMENT:
510 return get_demuxer_info(mpctx, "Comment");
512 case META_INFO_TRACK:
513 return get_demuxer_info(mpctx, "Track");
515 case META_INFO_GENRE:
516 return get_demuxer_info(mpctx, "Genre");
518 default:
519 break;
522 return meta;
525 /// step size of mixer changes
526 int volstep = 3;
528 #ifdef CONFIG_DVDNAV
529 static void mp_dvdnav_context_free(MPContext *ctx){
530 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
531 ctx->nav_smpi = NULL;
532 if (ctx->nav_buffer) free(ctx->nav_buffer);
533 ctx->nav_buffer = NULL;
534 ctx->nav_start = NULL;
535 ctx->nav_in_size = 0;
537 #endif
539 void uninit_player(struct MPContext *mpctx, unsigned int mask){
540 mask=mpctx->initialized_flags&mask;
542 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
544 if(mask&INITIALIZED_ACODEC){
545 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
546 current_module="uninit_acodec";
547 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
548 #ifdef CONFIG_GUI
549 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
550 #endif
551 mpctx->sh_audio=NULL;
552 mpctx->mixer.afilter = NULL;
555 if(mask&INITIALIZED_VCODEC){
556 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
557 current_module="uninit_vcodec";
558 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
559 mpctx->sh_video=NULL;
560 #ifdef CONFIG_MENU
561 vf_menu=NULL;
562 #endif
565 if(mask&INITIALIZED_DEMUXER){
566 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
567 current_module="free_demuxer";
568 if(mpctx->demuxer){
569 mpctx->stream=mpctx->demuxer->stream;
570 free_demuxer(mpctx->demuxer);
572 mpctx->demuxer=NULL;
575 // kill the cache process:
576 if(mask&INITIALIZED_STREAM){
577 mpctx->initialized_flags&=~INITIALIZED_STREAM;
578 current_module="uninit_stream";
579 if(mpctx->stream) free_stream(mpctx->stream);
580 mpctx->stream=NULL;
583 if(mask&INITIALIZED_VO){
584 mpctx->initialized_flags&=~INITIALIZED_VO;
585 current_module="uninit_vo";
586 vo_destroy(mpctx->video_out);
587 mpctx->video_out=NULL;
588 #ifdef CONFIG_DVDNAV
589 mp_dvdnav_context_free(mpctx);
590 #endif
593 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
594 if(mask&INITIALIZED_GETCH2){
595 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
596 current_module="uninit_getch2";
597 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
598 // restore terminal:
599 getch2_disable();
602 if(mask&INITIALIZED_VOBSUB){
603 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
604 current_module="uninit_vobsub";
605 if(vo_vobsub) vobsub_close(vo_vobsub);
606 vo_vobsub=NULL;
609 if (mask&INITIALIZED_SPUDEC){
610 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
611 current_module="uninit_spudec";
612 spudec_free(vo_spudec);
613 vo_spudec=NULL;
616 if(mask&INITIALIZED_AO){
617 mpctx->initialized_flags&=~INITIALIZED_AO;
618 current_module="uninit_ao";
619 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
620 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
621 mpctx->audio_out=NULL;
624 #ifdef CONFIG_GUI
625 if(mask&INITIALIZED_GUI){
626 mpctx->initialized_flags&=~INITIALIZED_GUI;
627 current_module="uninit_gui";
628 guiDone();
630 #endif
632 current_module=NULL;
635 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
637 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
638 uninit_player(mpctx, INITIALIZED_ALL);
639 #if defined(__MINGW32__) || defined(__CYGWIN__)
640 timeEndPeriod(1);
641 #endif
642 #ifdef CONFIG_X11
643 #ifdef CONFIG_GUI
644 if ( !use_gui )
645 #endif
646 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
647 #endif
649 current_module="uninit_input";
650 mp_input_uninit(mpctx->input);
651 #ifdef CONFIG_MENU
652 if (use_menu)
653 menu_uninit();
654 #endif
656 #ifdef CONFIG_FREETYPE
657 current_module="uninit_font";
658 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
659 free_font_desc(mpctx->osd->sub_font);
660 free_font_desc(vo_font);
661 vo_font = NULL;
662 done_freetype();
663 #endif
664 osd_free(mpctx->osd);
666 #ifdef CONFIG_ASS
667 ass_library_done(ass_library);
668 #endif
670 current_module="exit_player";
672 // free mplayer config
673 if(mpctx->mconfig)
674 m_config_free(mpctx->mconfig);
676 if(mpctx->playtree)
677 play_tree_free(mpctx->playtree, 1);
679 talloc_free(mpctx->key_fifo);
681 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
682 switch(how) {
683 case EXIT_QUIT:
684 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
685 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
686 break;
687 case EXIT_EOF:
688 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
689 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
690 break;
691 case EXIT_ERROR:
692 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
693 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
694 break;
695 default:
696 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
698 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
700 exit(rc);
703 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
705 exit_player_with_rc(mpctx, how, 1);
708 #ifndef __MINGW32__
709 static void child_sighandler(int x){
710 pid_t pid;
711 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
713 #endif
715 #ifdef CONFIG_CRASH_DEBUG
716 static char *prog_path;
717 static int crash_debug = 0;
718 #endif
720 static void exit_sighandler(int x){
721 static int sig_count=0;
722 #ifdef CONFIG_CRASH_DEBUG
723 if (!crash_debug || x != SIGTRAP)
724 #endif
725 ++sig_count;
726 if(sig_count==5)
728 /* We're crashing bad and can't uninit cleanly :(
729 * by popular request, we make one last (dirty)
730 * effort to restore the user's
731 * terminal. */
732 getch2_disable();
733 exit(1);
735 if(sig_count==6) exit(1);
736 if(sig_count>6){
737 // can't stop :(
738 #ifndef __MINGW32__
739 kill(getpid(),SIGKILL);
740 #endif
742 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
743 current_module?current_module:"unknown"
745 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
746 if(sig_count<=1)
747 switch(x){
748 case SIGINT:
749 case SIGQUIT:
750 case SIGTERM:
751 case SIGKILL:
752 async_quit_request = 1;
753 return; // killed from keyboard (^C) or killed [-9]
754 case SIGILL:
755 #ifdef RUNTIME_CPUDETECT
756 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
757 #else
758 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
759 #endif
760 case SIGFPE:
761 case SIGSEGV:
762 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
763 default:
764 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
765 #ifdef CONFIG_CRASH_DEBUG
766 if (crash_debug) {
767 int gdb_pid;
768 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
769 gdb_pid = fork();
770 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
771 if (gdb_pid == 0) { // We are the child
772 char spid[20];
773 snprintf(spid, sizeof(spid), "%i", getppid());
774 getch2_disable(); // allow terminal to work properly with gdb
775 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
776 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
777 } else if (gdb_pid < 0)
778 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
779 else {
780 waitpid(gdb_pid, NULL, 0);
782 if (x == SIGTRAP) return;
784 #endif
786 getch2_disable();
787 exit(1);
790 #include "cfg-mplayer.h"
792 static int cfg_include(m_option_t *conf, char *filename)
794 return m_config_parse_config_file(conf->priv, filename);
797 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
799 char *conffile;
800 int conffile_fd;
801 if (!disable_system_conf &&
802 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
803 exit_player(mpctx, EXIT_NONE);
804 if ((conffile = get_path("")) == NULL) {
805 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
806 } else {
807 #ifdef __MINGW32__
808 mkdir(conffile);
809 #else
810 mkdir(conffile, 0777);
811 #endif
812 free(conffile);
813 if ((conffile = get_path("config")) == NULL) {
814 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
815 } else {
816 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
817 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
818 write(conffile_fd, default_config, strlen(default_config));
819 close(conffile_fd);
821 if (!disable_user_conf &&
822 m_config_parse_config_file(conf, conffile) < 0)
823 exit_player(mpctx, EXIT_NONE);
824 free(conffile);
829 #define PROFILE_CFG_PROTOCOL "protocol."
831 static void load_per_protocol_config (m_config_t* conf, const char *const file)
833 char *str;
834 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
835 m_profile_t *p;
837 /* does filename actually uses a protocol ? */
838 str = strstr (file, "://");
839 if (!str)
840 return;
842 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
843 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
844 p = m_config_get_profile (conf, protocol);
845 if (p)
847 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
848 m_config_set_profile(conf,p);
852 #define PROFILE_CFG_EXTENSION "extension."
854 static void load_per_extension_config (m_config_t* conf, const char *const file)
856 char *str;
857 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
858 m_profile_t *p;
860 /* does filename actually have an extension ? */
861 str = strrchr (file, '.');
862 if (!str)
863 return;
865 sprintf (extension, PROFILE_CFG_EXTENSION);
866 strncat (extension, ++str, 7);
867 p = m_config_get_profile (conf, extension);
868 if (p)
870 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
871 m_config_set_profile(conf,p);
875 #define PROFILE_CFG_VO "vo."
876 #define PROFILE_CFG_AO "ao."
878 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
880 char profile[strlen (cfg) + strlen (out) + 1];
881 m_profile_t *p;
883 sprintf (profile, "%s%s", cfg, out);
884 p = m_config_get_profile (conf, profile);
885 if (p)
887 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
888 m_config_set_profile(conf,p);
892 static void load_per_file_config (m_config_t* conf, const char *const file)
894 char *confpath;
895 char cfg[strlen(file)+10];
896 struct stat st;
897 char *name;
899 sprintf (cfg, "%s.conf", file);
901 if (use_filedir_conf && !stat (cfg, &st))
903 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
904 m_config_parse_config_file (conf, cfg);
905 return;
908 if ((name = strrchr (cfg, '/')) == NULL)
909 name = cfg;
910 else
911 name++;
913 if ((confpath = get_path (name)) != NULL)
915 if (!stat (confpath, &st))
917 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
918 m_config_parse_config_file (conf, confpath);
921 free (confpath);
925 /* When libmpdemux performs a blocking operation (network connection or
926 * cache filling) if the operation fails we use this function to check
927 * if it was interrupted by the user.
928 * The function returns a new value for eof. */
929 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
931 mp_cmd_t* cmd;
932 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
933 switch(cmd->id) {
934 case MP_CMD_QUIT:
935 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
936 case MP_CMD_PLAY_TREE_STEP: {
937 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
938 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
939 } break;
940 case MP_CMD_PLAY_TREE_UP_STEP: {
941 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
942 } break;
943 case MP_CMD_PLAY_ALT_SRC_STEP: {
944 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
945 } break;
947 mp_cmd_free(cmd);
949 return stop_play;
952 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
954 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
956 play_tree_add_bpf(entry,mpctx->filename);
958 #ifdef CONFIG_GUI
959 if (use_gui) {
960 if (entry) {
961 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
962 play_tree_free_list(entry,1);
964 } else
965 #endif
967 if(!entry) {
968 entry = mpctx->playtree_iter->tree;
969 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
970 return PT_NEXT_ENTRY;
972 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
973 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
974 return PT_NEXT_ENTRY;
977 play_tree_remove(entry,1,1);
978 return PT_NEXT_SRC;
980 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
981 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
982 entry = mpctx->playtree_iter->tree;
983 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
984 return PT_NEXT_ENTRY;
986 play_tree_remove(entry,1,1);
988 return PT_NEXT_SRC;
991 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
993 sub_data *subd;
994 #ifdef CONFIG_ASS
995 ass_track_t *asst = 0;
996 #endif
998 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
999 return;
1002 subd = sub_read_file(filename, fps);
1003 #ifdef CONFIG_ASS
1004 if (ass_enabled)
1005 #ifdef CONFIG_ICONV
1006 asst = ass_read_file(ass_library, filename, sub_cp);
1007 #else
1008 asst = ass_read_file(ass_library, filename, 0);
1009 #endif
1010 if (ass_enabled && subd && !asst)
1011 asst = ass_read_subdata(ass_library, subd, fps);
1013 if (!asst && !subd)
1014 #else
1015 if(!subd)
1016 #endif
1017 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1018 filename_recode(filename));
1020 #ifdef CONFIG_ASS
1021 if (!asst && !subd) return;
1022 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1023 #else
1024 if (!subd) return;
1025 #endif
1026 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1027 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1028 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1029 filename_recode(filename));
1030 ++mpctx->set_of_sub_size;
1031 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1032 filename_recode(filename));
1035 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1036 void update_set_of_subtitles(struct MPContext *mpctx)
1037 // subdata was changed, set_of_sub... have to be updated.
1039 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1040 int i;
1041 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1042 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1043 set_of_subtitles[i-1] = set_of_subtitles[i];
1044 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1045 --mpctx->set_of_sub_size;
1046 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1048 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1049 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1051 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1052 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1053 ++mpctx->set_of_sub_size;
1057 void init_vo_spudec(struct MPContext *mpctx)
1059 if (vo_spudec)
1060 spudec_free(vo_spudec);
1061 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1062 vo_spudec = NULL;
1063 if (spudec_ifo) {
1064 unsigned int palette[16], width, height;
1065 current_module="spudec_init_vobsub";
1066 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1067 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1070 #ifdef CONFIG_DVDREAD
1071 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1072 current_module="spudec_init_dvdread";
1073 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1074 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1075 NULL, 0);
1077 #endif
1079 #ifdef CONFIG_DVDNAV
1080 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1081 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1082 current_module="spudec_init_dvdnav";
1083 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1085 #endif
1087 if (vo_spudec==NULL) {
1088 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1089 current_module="spudec_init_normal";
1090 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1091 spudec_set_font_factor(vo_spudec,font_factor);
1094 if (vo_spudec!=NULL)
1095 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1099 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1100 * make it all work is to use the builtin SDL-bootstrap code, which
1101 * will be done automatically by replacing our main() if we include SDL.h.
1103 #if defined(__APPLE__) && defined(CONFIG_SDL)
1104 #include <SDL.h>
1105 #endif
1108 * \brief append a formatted string
1109 * \param buf buffer to print into
1110 * \param pos position of terminating 0 in buf
1111 * \param len maximum number of characters in buf, not including terminating 0
1112 * \param format printf format string
1114 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1116 va_list va;
1117 va_start(va, format);
1118 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1119 va_end(va);
1120 if (*pos >= len ) {
1121 buf[len] = 0;
1122 *pos = len;
1127 * \brief append time in the hh:mm:ss.f format
1128 * \param buf buffer to print into
1129 * \param pos position of terminating 0 in buf
1130 * \param len maximum number of characters in buf, not including terminating 0
1131 * \param time time value to convert/append
1133 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1134 long tenths = 10 * time;
1135 int f1 = tenths % 10;
1136 int ss = (tenths / 10) % 60;
1137 int mm = (tenths / 600) % 60;
1138 int hh = tenths / 36000;
1139 if (time <= 0) {
1140 saddf(buf, pos, len, "unknown");
1141 return;
1143 if (hh > 0)
1144 saddf(buf, pos, len, "%2d:", hh);
1145 if (hh > 0 || mm > 0)
1146 saddf(buf, pos, len, "%02d:", mm);
1147 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1150 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1152 struct MPOpts *opts = &mpctx->opts;
1153 sh_video_t * const sh_video = mpctx->sh_video;
1155 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1156 a_pos = playing_audio_pts(mpctx);
1157 if (mpctx->sh_audio && sh_video && at_frame) {
1158 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1159 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1160 && !mpctx->drop_message_shown) {
1161 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1162 mpctx->drop_message_shown = true;
1165 if (quiet)
1166 return;
1169 int width;
1170 char *line;
1171 unsigned pos = 0;
1172 get_screen_size();
1173 if (screen_width > 0)
1174 width = screen_width;
1175 else
1176 width = 80;
1177 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1178 /* Windows command line is broken (MinGW's rxvt works, but we
1179 * should not depend on that). */
1180 width--;
1181 #endif
1182 line = malloc(width + 1); // one additional char for the terminating null
1184 // Audio time
1185 if (mpctx->sh_audio) {
1186 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1187 if (!sh_video) {
1188 float len = demuxer_get_time_length(mpctx->demuxer);
1189 saddf(line, &pos, width, "(");
1190 sadd_hhmmssf(line, &pos, width, a_pos);
1191 saddf(line, &pos, width, ") of %.1f (", len);
1192 sadd_hhmmssf(line, &pos, width, len);
1193 saddf(line, &pos, width, ") ");
1197 // Video time
1198 if (sh_video)
1199 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1201 // A-V sync
1202 if (mpctx->sh_audio && sh_video)
1203 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1204 mpctx->last_av_difference, mpctx->total_avsync_change);
1206 // Video stats
1207 if (sh_video)
1208 saddf(line, &pos, width, "%3d/%3d ",
1209 (int)sh_video->num_frames,
1210 (int)sh_video->num_frames_decoded);
1212 // CPU usage
1213 if (sh_video) {
1214 if (sh_video->timer > 0.5)
1215 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1216 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1217 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1218 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1219 else
1220 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1221 } else if (mpctx->sh_audio) {
1222 if (mpctx->delay > 0.5)
1223 saddf(line, &pos, width, "%4.1f%% ",
1224 100.0*audio_time_usage/(double)mpctx->delay);
1225 else
1226 saddf(line, &pos, width, "??,?%% ");
1229 // VO stats
1230 if (sh_video)
1231 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1233 #ifdef CONFIG_STREAM_CACHE
1234 // cache stats
1235 if (stream_cache_size > 0)
1236 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1237 #endif
1239 // other
1240 if (opts->playback_speed != 1)
1241 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1243 // end
1244 if (erase_to_end_of_line) {
1245 line[pos] = 0;
1246 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1247 } else {
1248 memset(&line[pos], ' ', width - pos);
1249 line[width] = 0;
1250 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1252 free(line);
1256 * \brief build a chain of audio filters that converts the input format
1257 * to the ao's format, taking into account the current playback_speed.
1258 * \param sh_audio describes the requested input format of the chain.
1259 * \param ao_data describes the requested output format of the chain.
1261 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1263 struct MPOpts *opts = &mpctx->opts;
1264 int new_srate;
1265 int result;
1266 if (!sh_audio)
1268 #ifdef CONFIG_GUI
1269 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1270 #endif
1271 mpctx->mixer.afilter = NULL;
1272 return 0;
1274 if(af_control_any_rev(sh_audio->afilter,
1275 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1276 &opts->playback_speed)) {
1277 new_srate = sh_audio->samplerate;
1278 } else {
1279 new_srate = sh_audio->samplerate * opts->playback_speed;
1280 if (new_srate != ao_data->samplerate) {
1281 // limits are taken from libaf/af_resample.c
1282 if (new_srate < 8000)
1283 new_srate = 8000;
1284 if (new_srate > 192000)
1285 new_srate = 192000;
1286 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1289 result = init_audio_filters(sh_audio, new_srate,
1290 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1291 mpctx->mixer.afilter = sh_audio->afilter;
1292 #ifdef CONFIG_GUI
1293 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1294 #endif
1295 return result;
1299 typedef struct mp_osd_msg mp_osd_msg_t;
1300 struct mp_osd_msg {
1301 /// Previous message on the stack.
1302 mp_osd_msg_t* prev;
1303 /// Message text.
1304 char msg[128];
1305 int id,level,started;
1306 /// Display duration in ms.
1307 unsigned time;
1310 /// OSD message stack.
1311 static mp_osd_msg_t* osd_msg_stack = NULL;
1314 * \brief Add a message on the OSD message stack
1316 * If a message with the same id is already present in the stack
1317 * it is pulled on top of the stack, otherwise a new message is created.
1321 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1322 mp_osd_msg_t *msg,*last=NULL;
1323 va_list va;
1324 int r;
1326 // look if the id is already in the stack
1327 for(msg = osd_msg_stack ; msg && msg->id != id ;
1328 last = msg, msg = msg->prev);
1329 // not found: alloc it
1330 if(!msg) {
1331 msg = calloc(1,sizeof(mp_osd_msg_t));
1332 msg->prev = osd_msg_stack;
1333 osd_msg_stack = msg;
1334 } else if(last) { // found, but it's not on top of the stack
1335 last->prev = msg->prev;
1336 msg->prev = osd_msg_stack;
1337 osd_msg_stack = msg;
1339 // write the msg
1340 va_start(va,fmt);
1341 r = vsnprintf(msg->msg, 128, fmt, va);
1342 va_end(va);
1343 if(r >= 128) msg->msg[127] = 0;
1344 // set id and time
1345 msg->id = id;
1346 msg->level = level;
1347 msg->time = time;
1352 * \brief Remove a message from the OSD stack
1354 * This function can be used to get rid of a message right away.
1358 void rm_osd_msg(int id) {
1359 mp_osd_msg_t *msg,*last=NULL;
1361 // Search for the msg
1362 for(msg = osd_msg_stack ; msg && msg->id != id ;
1363 last = msg, msg = msg->prev);
1364 if(!msg) return;
1366 // Detach it from the stack and free it
1367 if(last)
1368 last->prev = msg->prev;
1369 else
1370 osd_msg_stack = msg->prev;
1371 free(msg);
1375 * \brief Remove all messages from the OSD stack
1379 static void clear_osd_msgs(void) {
1380 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1381 while(msg) {
1382 prev = msg->prev;
1383 free(msg);
1384 msg = prev;
1386 osd_msg_stack = NULL;
1390 * \brief Get the current message from the OSD stack.
1392 * This function decrements the message timer and destroys the old ones.
1393 * The message that should be displayed is returned (if any).
1397 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1399 struct MPOpts *opts = &mpctx->opts;
1400 mp_osd_msg_t *msg,*prev,*last = NULL;
1401 static unsigned last_update = 0;
1402 unsigned now = GetTimerMS();
1403 unsigned diff;
1404 char hidden_dec_done = 0;
1406 if (mpctx->osd_visible) {
1407 // 36000000 means max timed visibility is 1 hour into the future, if
1408 // the difference is greater assume it's wrapped around from below 0
1409 if (mpctx->osd_visible - now > 36000000) {
1410 mpctx->osd_visible = 0;
1411 vo_osd_progbar_type = -1; // disable
1412 vo_osd_changed(OSDTYPE_PROGBAR);
1413 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1416 if (mpctx->osd_show_percentage_until - now > 36000000)
1417 mpctx->osd_show_percentage_until = 0;
1419 if(!last_update) last_update = now;
1420 diff = now >= last_update ? now - last_update : 0;
1422 last_update = now;
1424 // Look for the first message in the stack with high enough level.
1425 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1426 prev = msg->prev;
1427 if (msg->level > opts->osd_level && hidden_dec_done)
1428 continue;
1429 // The message has a high enough level or it is the first hidden one
1430 // in both cases we decrement the timer or kill it.
1431 if(!msg->started || msg->time > diff) {
1432 if(msg->started) msg->time -= diff;
1433 else msg->started = 1;
1434 // display it
1435 if (msg->level <= opts->osd_level)
1436 return msg;
1437 hidden_dec_done = 1;
1438 continue;
1440 // kill the message
1441 free(msg);
1442 if(last) {
1443 last->prev = prev;
1444 msg = last;
1445 } else {
1446 osd_msg_stack = prev;
1447 msg = NULL;
1450 // Nothing found
1451 return NULL;
1455 * \brief Display the OSD bar.
1457 * Display the OSD bar or fall back on a simple message.
1461 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1462 struct MPOpts *opts = &mpctx->opts;
1463 if (opts->osd_level < 1)
1464 return;
1466 if(mpctx->sh_video) {
1467 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1468 vo_osd_progbar_type = type;
1469 vo_osd_progbar_value = 256*(val-min)/(max-min);
1470 vo_osd_changed(OSDTYPE_PROGBAR);
1471 return;
1474 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1475 name, ROUND(100*(val-min)/(max-min)));
1480 * \brief Update the OSD message line.
1482 * This function displays the current message on the vo OSD or on the term.
1483 * If the stack is empty and the OSD level is high enough the timer
1484 * is displayed (only on the vo OSD).
1488 static void update_osd_msg(struct MPContext *mpctx)
1490 struct MPOpts *opts = &mpctx->opts;
1491 mp_osd_msg_t *msg;
1492 struct osd_state *osd = mpctx->osd;
1493 char osd_text_timer[128];
1495 if (mpctx->add_osd_seek_info) {
1496 set_osd_bar(mpctx, 0, "Position", 0, 100,
1497 demuxer_get_percent_pos(mpctx->demuxer));
1498 if (mpctx->sh_video)
1499 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1500 mpctx->add_osd_seek_info = false;
1503 // Look if we have a msg
1504 if((msg = get_osd_msg(mpctx))) {
1505 if (strcmp(osd->osd_text, msg->msg)) {
1506 strncpy(osd->osd_text, msg->msg, 127);
1507 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1508 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1510 return;
1513 if(mpctx->sh_video) {
1514 // fallback on the timer
1515 if (opts->osd_level >= 2) {
1516 int len = demuxer_get_time_length(mpctx->demuxer);
1517 int percentage = -1;
1518 char percentage_text[10];
1519 int pts = demuxer_get_current_time(mpctx->demuxer);
1521 if (mpctx->osd_show_percentage_until)
1522 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1524 if (percentage >= 0)
1525 snprintf(percentage_text, 9, " (%d%%)", percentage);
1526 else
1527 percentage_text[0] = 0;
1529 if (opts->osd_level == 3)
1530 snprintf(osd_text_timer, 63,
1531 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1532 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1533 len/3600,(len/60)%60,len%60,percentage_text);
1534 else
1535 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1536 mpctx->osd_function,pts/3600,(pts/60)%60,
1537 pts%60,percentage_text);
1538 } else
1539 osd_text_timer[0]=0;
1541 if (strcmp(osd->osd_text, osd_text_timer)) {
1542 strncpy(osd->osd_text, osd_text_timer, 63);
1543 vo_osd_changed(OSDTYPE_OSD);
1545 return;
1548 // Clear the term osd line
1549 if (term_osd && osd->osd_text[0]) {
1550 osd->osd_text[0] = 0;
1551 printf("%s\n",term_osd_esc);
1555 ///@}
1556 // OSDMsgStack
1559 void reinit_audio_chain(struct MPContext *mpctx)
1561 struct MPOpts *opts = &mpctx->opts;
1562 if(mpctx->sh_audio){
1563 current_module="init_audio_codec";
1564 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1565 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1566 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1567 mpctx->d_audio->id = -2;
1568 return;
1569 } else
1570 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1571 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1574 //const ao_info_t *info=audio_out->info;
1575 current_module="af_preinit";
1576 ao_data.samplerate=force_srate;
1577 ao_data.channels=0;
1578 ao_data.format=audio_output_format;
1579 #if 1
1580 // first init to detect best values
1581 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1582 // input:
1583 mpctx->sh_audio->samplerate,
1584 // output:
1585 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1586 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1587 exit_player(mpctx, EXIT_ERROR);
1589 #endif
1590 current_module="ao2_init";
1591 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1592 0, // plugin flag
1593 ao_data.samplerate,
1594 ao_data.channels,
1595 ao_data.format,0))){
1596 // FAILED:
1597 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1598 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1599 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1600 mpctx->d_audio->id = -2;
1601 return;
1602 } else {
1603 // SUCCESS:
1604 mpctx->initialized_flags|=INITIALIZED_AO;
1605 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1606 mpctx->audio_out->info->short_name,
1607 ao_data.samplerate, ao_data.channels,
1608 af_fmt2str_short(ao_data.format),
1609 af_fmt2bits(ao_data.format)/8 );
1610 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1611 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1612 if(strlen(mpctx->audio_out->info->comment) > 0)
1613 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1614 // init audio filters:
1615 #if 1
1616 current_module="af_init";
1617 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1618 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1619 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1620 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1621 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1623 #endif
1625 mpctx->mixer.audio_out = mpctx->audio_out;
1626 mpctx->mixer.volstep = volstep;
1631 ///@}
1632 // Command2Property
1635 // Return pts value corresponding to the end point of audio written to the
1636 // ao so far.
1637 static double written_audio_pts(struct MPContext *mpctx)
1639 sh_audio_t *sh_audio = mpctx->sh_audio;
1640 demux_stream_t *d_audio = mpctx->d_audio;
1641 double buffered_output;
1642 // first calculate the end pts of audio that has been output by decoder
1643 double a_pts = sh_audio->pts;
1644 if (a_pts != MP_NOPTS_VALUE)
1645 // Good, decoder supports new way of calculating audio pts.
1646 // sh_audio->pts is the timestamp of the latest input packet with
1647 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1648 // the amount of bytes the decoder has written after that timestamp.
1649 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1650 else {
1651 // Decoder doesn't support new way of calculating pts (or we're
1652 // being called before it has decoded anything with known timestamp).
1653 // Use the old method of audio pts calculation: take the timestamp
1654 // of last packet with known pts the decoder has read data from,
1655 // and add amount of bytes read after the beginning of that packet
1656 // divided by input bps. This will be inaccurate if the input/output
1657 // ratio is not constant for every audio packet or if it is constant
1658 // but not accurately known in sh_audio->i_bps.
1660 a_pts = d_audio->pts;
1661 // ds_tell_pts returns bytes read after last timestamp from
1662 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1663 // it has read but not decoded
1664 if (sh_audio->i_bps)
1665 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1666 (double)sh_audio->i_bps;
1668 // Now a_pts hopefully holds the pts for end of audio from decoder.
1669 // Substract data in buffers between decoder and audio out.
1671 // Decoded but not filtered
1672 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1674 // Data buffered in audio filters, measured in bytes of "missing" output
1675 buffered_output = af_calc_delay(sh_audio->afilter);
1677 // Data that was ready for ao but was buffered because ao didn't fully
1678 // accept everything to internal buffers yet
1679 buffered_output += sh_audio->a_out_buffer_len;
1681 // Filters divide audio length by playback_speed, so multiply by it
1682 // to get the length in original units without speedup or slowdown
1683 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1685 return a_pts;
1688 // Return pts value corresponding to currently playing audio.
1689 double playing_audio_pts(struct MPContext *mpctx)
1691 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1692 mpctx->audio_out->get_delay();
1695 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1696 struct MPOpts *opts = &mpctx->opts;
1697 // check for frame-drop:
1698 current_module = "check_framedrop";
1699 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1700 static int dropped_frames;
1701 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1702 float d = delay-mpctx->delay;
1703 ++total_frame_cnt;
1704 // we should avoid dropping too many frames in sequence unless we
1705 // are too late. and we allow 100ms A-V delay here:
1706 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1707 && !mpctx->update_video_immediately) {
1708 ++drop_frame_cnt;
1709 ++dropped_frames;
1710 return frame_dropping;
1711 } else
1712 dropped_frames = 0;
1714 return 0;
1718 #ifdef HAVE_RTC
1719 int rtc_fd = -1;
1720 #endif
1722 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1724 #ifdef HAVE_RTC
1725 if (rtc_fd >= 0){
1726 // -------- RTC -----------
1727 current_module="sleep_rtc";
1728 while (time_frame > 0.000) {
1729 unsigned long rtc_ts;
1730 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1731 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1732 time_frame -= get_relative_time(mpctx);
1734 } else
1735 #endif
1737 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1738 // unnecessarily high CPU usage
1739 float margin = softsleep ? 0.011 : 0;
1740 current_module = "sleep_timer";
1741 while (time_frame > margin) {
1742 usec_sleep(1000000 * (time_frame - margin));
1743 time_frame -= get_relative_time(mpctx);
1745 if (softsleep){
1746 current_module = "sleep_soft";
1747 if (time_frame < 0)
1748 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1749 while (time_frame > 0)
1750 time_frame -= get_relative_time(mpctx); // burn the CPU
1753 return time_frame;
1756 #ifdef CONFIG_DVDNAV
1757 #ifndef FF_B_TYPE
1758 #define FF_B_TYPE 3
1759 #endif
1760 /// store decoded video image
1761 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1762 mp_image_t *from_mpi) {
1763 mp_image_t *mpi;
1765 /// Do not store B-frames
1766 if (from_mpi->pict_type == FF_B_TYPE)
1767 return to_mpi;
1769 if (to_mpi &&
1770 to_mpi->w == from_mpi->w &&
1771 to_mpi->h == from_mpi->h &&
1772 to_mpi->imgfmt == from_mpi->imgfmt)
1773 mpi = to_mpi;
1774 else {
1775 if (to_mpi)
1776 free_mp_image(to_mpi);
1777 if (from_mpi->w == 0 || from_mpi->h == 0)
1778 return NULL;
1779 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1782 copy_mpi(mpi,from_mpi);
1783 return mpi;
1786 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1787 struct MPOpts *opts = &ctx->opts;
1788 if (ctx->sh_video) {
1789 /// clear video pts
1790 ctx->d_video->pts = 0.0f;
1791 ctx->sh_video->pts = 0.0f;
1792 ctx->sh_video->i_pts = 0.0f;
1793 ctx->sh_video->last_pts = 0.0f;
1794 ctx->sh_video->num_buffered_pts = 0;
1795 ctx->sh_video->num_frames = 0;
1796 ctx->sh_video->num_frames_decoded = 0;
1797 ctx->sh_video->timer = 0.0f;
1798 ctx->sh_video->stream_delay = 0.0f;
1799 ctx->sh_video->timer = 0;
1800 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1803 if (ctx->sh_audio) {
1804 /// free audio packets and reset
1805 ds_free_packs(ctx->d_audio);
1806 audio_delay -= ctx->sh_audio->stream_delay;
1807 ctx->delay =- audio_delay;
1808 ctx->audio_out->reset();
1809 resync_audio_stream(ctx->sh_audio);
1812 if (ctx->d_sub) opts->sub_id = -2;
1814 audio_delay = 0.0f;
1816 /// clear all EOF related flags
1817 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1820 /// Restore last decoded DVDNAV (still frame)
1821 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1822 int *in_size,
1823 unsigned char **start,
1824 mp_image_t *decoded_frame)
1826 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1827 return decoded_frame;
1829 /// a change occured in dvdnav stream
1830 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1831 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1832 mp_dvdnav_context_free(mpctx);
1833 mp_dvdnav_reset_stream(mpctx);
1834 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1835 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1838 if (*in_size < 0) {
1839 float len;
1841 /// Display still frame, if any
1842 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1843 decoded_frame = mpctx->nav_smpi;
1845 /// increment video frame : continue playing after still frame
1846 len = demuxer_get_time_length(mpctx->demuxer);
1847 if (mpctx->sh_video->pts >= len &&
1848 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1849 mp_dvdnav_skip_still(mpctx->stream);
1850 mp_dvdnav_skip_wait(mpctx->stream);
1852 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1854 if (mpctx->nav_buffer) {
1855 *start = mpctx->nav_start;
1856 *in_size = mpctx->nav_in_size;
1857 if (mpctx->nav_start)
1858 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1862 return decoded_frame;
1865 /// Save last decoded DVDNAV (still frame)
1866 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1867 unsigned char *start,
1868 mp_image_t *decoded_frame)
1870 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1871 return;
1873 if (mpctx->nav_buffer)
1874 free(mpctx->nav_buffer);
1876 mpctx->nav_buffer = malloc(in_size);
1877 mpctx->nav_start = start;
1878 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1879 if (mpctx->nav_buffer)
1880 memcpy(mpctx->nav_buffer,start,in_size);
1882 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1883 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1885 #endif /* CONFIG_DVDNAV */
1887 /* Modify video timing to match the audio timeline. There are two main
1888 * reasons this is needed. First, video and audio can start from different
1889 * positions at beginning of file or after a seek (MPlayer starts both
1890 * immediately even if they have different pts). Second, the file can have
1891 * audio timestamps that are inconsistent with the duration of the audio
1892 * packets, for example two consecutive timestamp values differing by
1893 * one second but only a packet with enough samples for half a second
1894 * of playback between them.
1896 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1898 struct MPOpts *opts = &mpctx->opts;
1899 current_module = "av_sync";
1901 if (!mpctx->sh_audio)
1902 return;
1904 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1905 double v_pts = mpctx->sh_video->pts;
1906 double av_delay = a_pts - v_pts;
1907 // Try to sync vo_flip() so it will *finish* at given time
1908 av_delay += mpctx->last_vo_flip_duration;
1909 av_delay -= audio_delay; // This much pts difference is desired
1911 double change = av_delay * 0.1;
1912 double max_change = default_max_pts_correction >= 0 ?
1913 default_max_pts_correction : frame_time * 0.1;
1914 if (change < -max_change)
1915 change = -max_change;
1916 else if (change > max_change)
1917 change = max_change;
1918 mpctx->delay += change;
1919 mpctx->total_avsync_change += change;
1922 static int fill_audio_out_buffers(struct MPContext *mpctx)
1924 struct MPOpts *opts = &mpctx->opts;
1925 unsigned int t;
1926 double tt;
1927 int playsize;
1928 int playflags=0;
1929 int audio_eof=0;
1930 int bytes_to_write;
1931 sh_audio_t * const sh_audio = mpctx->sh_audio;
1933 current_module="play_audio";
1935 while (1) {
1936 int sleep_time;
1937 // all the current uses of ao_data.pts seem to be in aos that handle
1938 // sync completely wrong; there should be no need to use ao_data.pts
1939 // in get_space()
1940 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1941 bytes_to_write = mpctx->audio_out->get_space();
1942 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1943 break;
1945 // handle audio-only case:
1946 // this is where mplayer sleeps during audio-only playback
1947 // to avoid 100% CPU use
1948 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
1949 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
1950 usec_sleep(sleep_time * 1000);
1953 while (bytes_to_write) {
1954 playsize = bytes_to_write;
1955 if (playsize > MAX_OUTBURST)
1956 playsize = MAX_OUTBURST;
1957 bytes_to_write -= playsize;
1959 // Fill buffer if needed:
1960 current_module="decode_audio";
1961 t = GetTimer();
1962 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1963 if (mpctx->d_audio->eof) {
1964 audio_eof = 1;
1965 if (sh_audio->a_out_buffer_len == 0)
1966 return 0;
1968 t = GetTimer() - t;
1969 tt = t*0.000001f; audio_time_usage+=tt;
1970 if (playsize > sh_audio->a_out_buffer_len) {
1971 playsize = sh_audio->a_out_buffer_len;
1972 if (audio_eof)
1973 playflags |= AOPLAY_FINAL_CHUNK;
1975 if (!playsize)
1976 break;
1978 // play audio:
1979 current_module="play_audio";
1981 // Is this pts value actually useful for the aos that access it?
1982 // They're obviously badly broken in the way they handle av sync;
1983 // would not having access to this make them more broken?
1984 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1985 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1987 if (playsize > 0) {
1988 sh_audio->a_out_buffer_len -= playsize;
1989 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1990 sh_audio->a_out_buffer_len);
1991 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
1993 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1994 // Sanity check to avoid hanging in case current ao doesn't output
1995 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1996 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1997 sh_audio->a_out_buffer_len = 0;
2000 return 1;
2003 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2004 float *aq_sleep_time)
2006 struct MPOpts *opts = &mpctx->opts;
2007 int frame_time_remaining = 0;
2008 current_module="calc_sleep_time";
2010 *time_frame -= get_relative_time(mpctx); // reset timer
2012 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2013 float delay = mpctx->audio_out->get_delay();
2014 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2016 if (autosync) {
2018 * Adjust this raw delay value by calculating the expected
2019 * delay for this frame and generating a new value which is
2020 * weighted between the two. The higher autosync is, the
2021 * closer to the delay value gets to that which "-nosound"
2022 * would have used, and the longer it will take for A/V
2023 * sync to settle at the right value (but it eventually will.)
2024 * This settling time is very short for values below 100.
2026 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2027 float difference = delay - predicted;
2028 delay = predicted + difference / (float)autosync;
2031 *time_frame = delay - mpctx->delay / opts->playback_speed;
2033 // delay = amount of audio buffered in soundcard/driver
2034 if (delay > 0.25) delay=0.25; else
2035 if (delay < 0.10) delay=0.10;
2036 if (*time_frame > delay*0.6) {
2037 // sleep time too big - may cause audio drops (buffer underrun)
2038 frame_time_remaining = 1;
2039 *time_frame = delay*0.5;
2041 } else {
2042 // If we're lagging more than 200 ms behind the right playback rate,
2043 // don't try to "catch up".
2044 // If benchmark is set always output frames as fast as possible
2045 // without sleeping.
2046 if (*time_frame < -0.2 || benchmark)
2047 *time_frame = 0;
2050 *aq_sleep_time += *time_frame;
2053 //============================== SLEEP: ===================================
2055 // flag 256 means: libvo driver does its timing (dvb card)
2056 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2057 *time_frame = timing_sleep(mpctx, *time_frame);
2058 return frame_time_remaining;
2061 int reinit_video_chain(struct MPContext *mpctx)
2063 struct MPOpts *opts = &mpctx->opts;
2064 sh_video_t * const sh_video = mpctx->sh_video;
2065 double ar=-1.0;
2066 //================== Init VIDEO (codec & libvo) ==========================
2067 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2068 current_module="preinit_libvo";
2070 //shouldn't we set dvideo->id=-2 when we fail?
2071 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2072 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2073 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2074 goto err_out;
2076 mpctx->initialized_flags|=INITIALIZED_VO;
2079 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2080 mpctx->sh_video->stream_aspect = ar;
2081 current_module="init_video_filters";
2083 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2084 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2086 #ifdef CONFIG_MENU
2087 if(use_menu) {
2088 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2089 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2090 if(!vf_menu) {
2091 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2092 use_menu = 0;
2095 if(vf_menu)
2096 sh_video->vfilter = vf_menu;
2097 #endif
2099 #ifdef CONFIG_ASS
2100 if(ass_enabled) {
2101 int i;
2102 int insert = 1;
2103 if (opts->vf_settings)
2104 for (i = 0; opts->vf_settings[i].name; ++i)
2105 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2106 insert = 0;
2107 break;
2109 if (insert) {
2110 extern vf_info_t vf_info_ass;
2111 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2112 char* vf_arg[] = {"auto", "1", NULL};
2113 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2114 if (vf_ass)
2115 sh_video->vfilter = vf_ass;
2116 else
2117 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2120 #endif
2122 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2124 #ifdef CONFIG_ASS
2125 if (ass_enabled)
2126 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2127 #endif
2129 current_module="init_video_codec";
2131 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2132 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2133 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2135 if(!sh_video->initialized){
2136 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2137 goto err_out;
2140 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2142 if (sh_video->codec)
2143 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2145 sh_video->last_pts = MP_NOPTS_VALUE;
2146 sh_video->num_buffered_pts = 0;
2147 sh_video->next_frame_time = 0;
2149 if(auto_quality>0){
2150 // Auto quality option enabled
2151 output_quality=get_video_quality_max(sh_video);
2152 if(auto_quality>output_quality) auto_quality=output_quality;
2153 else output_quality=auto_quality;
2154 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2155 set_video_quality(sh_video,output_quality);
2158 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2160 current_module="init_vo";
2162 return 1;
2164 err_out:
2165 mpctx->sh_video = mpctx->d_video->sh = NULL;
2166 return 0;
2169 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2170 int *blit_frame)
2172 struct sh_video *sh_video = mpctx->sh_video;
2173 *blit_frame = 0;
2174 double frame_time = 0;
2175 while (1) {
2176 current_module = "filter_video";
2177 // In nocorrect-pts mode there is no way to properly time these frames
2178 if (vf_output_queued_frame(sh_video->vfilter))
2179 break;
2180 unsigned char *packet = NULL;
2181 frame_time = sh_video->next_frame_time;
2182 if (mpctx->update_video_immediately)
2183 frame_time = 0;
2184 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2185 &packet, force_fps);
2186 if (in_size < 0) {
2187 #ifdef CONFIG_DVDNAV
2188 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2189 if (mp_dvdnav_is_eof(mpctx->stream))
2190 return -1;
2191 if (mpctx->d_video)
2192 mpctx->d_video->eof = 0;
2193 if (mpctx->d_audio)
2194 mpctx->d_audio->eof = 0;
2195 mpctx->stream->eof = 0;
2196 } else
2197 #endif
2198 return -1;
2200 if (in_size > max_framesize)
2201 max_framesize = in_size;
2202 sh_video->timer += frame_time;
2203 if (mpctx->sh_audio)
2204 mpctx->delay -= frame_time;
2205 // video_read_frame can change fps (e.g. for ASF video)
2206 vo_fps = sh_video->fps;
2207 int framedrop_type = check_framedrop(mpctx, frame_time);
2208 current_module = "decode video";
2210 void *decoded_frame;
2211 #ifdef CONFIG_DVDNAV
2212 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2213 if (in_size >= 0 && !decoded_frame)
2214 #endif
2215 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2216 sh_video->pts);
2217 #ifdef CONFIG_DVDNAV
2218 // Save last still frame for future display
2219 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2220 #endif
2221 if (decoded_frame) {
2222 // These updates are done here for vf_expand OSD/subtitles
2223 update_subtitles(sh_video, mpctx->d_sub, 0);
2224 update_teletext(sh_video, mpctx->demuxer, 0);
2225 update_osd_msg(mpctx);
2226 current_module = "filter video";
2227 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2228 break;
2231 *blit_frame = 1;
2232 return frame_time;
2235 static double update_video(struct MPContext *mpctx, int *blit_frame)
2237 struct sh_video *sh_video = mpctx->sh_video;
2238 *blit_frame = 0;
2239 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2240 mpctx->osd); // hack for vf_expand
2241 if (!mpctx->opts.correct_pts)
2242 return update_video_nocorrect_pts(mpctx, blit_frame);
2244 double pts;
2246 while (1) {
2247 current_module = "filter_video";
2248 // XXX Time used in this call is not counted in any performance
2249 // timer now, OSD time is not updated correctly for filter-added frames
2250 if (vf_output_queued_frame(sh_video->vfilter))
2251 break;
2252 unsigned char *packet = NULL;
2253 bool hit_eof = false;
2254 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2255 if (in_size < 0) {
2256 // try to extract last frames in case of decoder lag
2257 in_size = 0;
2258 pts = 1e300;
2259 hit_eof = true;
2261 if (in_size > max_framesize)
2262 max_framesize = in_size;
2263 current_module = "decode video";
2264 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2265 void *decoded_frame = decode_video(sh_video, packet, in_size,
2266 framedrop_type, pts);
2267 if (decoded_frame) {
2268 // These updates are done here for vf_expand OSD/subtitles
2269 update_subtitles(sh_video, mpctx->d_sub, 0);
2270 update_teletext(sh_video, mpctx->demuxer, 0);
2271 update_osd_msg(mpctx);
2272 current_module = "filter video";
2273 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2274 break;
2275 } else if (hit_eof)
2276 return -1;
2279 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2280 if (pts == MP_NOPTS_VALUE) {
2281 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2282 // Try to use decoder pts from before filters
2283 pts = sh_video->pts;
2285 sh_video->pts = pts;
2286 if (sh_video->last_pts == MP_NOPTS_VALUE)
2287 sh_video->last_pts = sh_video->pts;
2288 else if (sh_video->last_pts > sh_video->pts) {
2289 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2290 sh_video->pts, sh_video->last_pts);
2291 /* If the difference in pts is small treat it as jitter around the
2292 * right value (possibly caused by incorrect timestamp ordering) and
2293 * just show this frame immediately after the last one.
2294 * Treat bigger differences as timestamp resets and start counting
2295 * timing of later frames from the position of this one. */
2296 if (sh_video->last_pts - sh_video->pts > 0.5)
2297 sh_video->last_pts = sh_video->pts;
2298 else
2299 sh_video->pts = sh_video->last_pts;
2301 double frame_time = sh_video->pts - sh_video->last_pts;
2302 sh_video->last_pts = sh_video->pts;
2303 sh_video->timer += frame_time;
2304 if (mpctx->sh_audio)
2305 mpctx->delay -= frame_time;
2306 *blit_frame = 1;
2307 return frame_time;
2310 void pause_player(struct MPContext *mpctx)
2312 if (mpctx->paused)
2313 return;
2314 mpctx->paused = 1;
2315 mpctx->step_frames = 0;
2316 mpctx->time_frame -= get_relative_time(mpctx);
2318 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2319 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2321 if (mpctx->audio_out && mpctx->sh_audio)
2322 mpctx->audio_out->pause(); // pause audio, keep data if possible
2325 void unpause_player(struct MPContext *mpctx)
2327 if (!mpctx->paused)
2328 return;
2329 mpctx->paused = 0;
2331 if (mpctx->audio_out && mpctx->sh_audio)
2332 mpctx->audio_out->resume(); // resume audio
2333 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2334 && !mpctx->step_frames)
2335 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2336 (void)get_relative_time(mpctx); // ignore time that passed during pause
2339 void add_step_frame(struct MPContext *mpctx)
2341 mpctx->step_frames++;
2342 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2343 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2344 unpause_player(mpctx);
2347 static void pause_loop(struct MPContext *mpctx)
2349 mp_cmd_t* cmd;
2350 if (!quiet) {
2351 // Small hack to display the pause message on the OSD line.
2352 // The pause string is: "\n == PAUSE == \r" so we need to
2353 // take the first and the last char out
2354 if (term_osd && !mpctx->sh_video) {
2355 char msg[128] = MSGTR_Paused;
2356 int mlen = strlen(msg);
2357 msg[mlen-1] = '\0';
2358 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2359 update_osd_msg(mpctx);
2360 } else
2361 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2362 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2364 #ifdef CONFIG_GUI
2365 if (use_gui)
2366 guiGetEvent(guiCEvent, (char *)guiSetPause);
2367 #endif
2369 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2370 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2371 if (cmd) {
2372 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2373 run_command(mpctx, cmd);
2374 mp_cmd_free(cmd);
2375 continue;
2377 if (mpctx->sh_video && mpctx->video_out)
2378 vo_check_events(mpctx->video_out);
2379 #ifdef CONFIG_GUI
2380 if (use_gui) {
2381 guiEventHandling();
2382 guiGetEvent(guiReDraw, NULL);
2383 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2384 break;
2386 #endif
2387 #ifdef CONFIG_MENU
2388 if (vf_menu)
2389 vf_menu_pause_update(vf_menu);
2390 #endif
2391 usec_sleep(20000);
2392 update_osd_msg(mpctx);
2393 int hack = vo_osd_changed(0);
2394 vo_osd_changed(hack);
2395 if (hack)
2396 break;
2398 #ifdef CONFIG_GUI
2399 if (use_gui) {
2400 if (guiIntfStruct.Playing == guiSetStop)
2401 mpctx->eof = 1;
2402 else
2403 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2405 #endif
2409 // Find the right mute status and record position for new file position
2410 static void edl_seek_reset(MPContext *mpctx)
2412 mpctx->edl_muted = 0;
2413 next_edl_record = edl_records;
2415 while (next_edl_record) {
2416 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2417 break;
2419 if (next_edl_record->action == EDL_MUTE)
2420 mpctx->edl_muted = !mpctx->edl_muted;
2421 next_edl_record = next_edl_record->next;
2423 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2424 mixer_mute(&mpctx->mixer);
2428 // Execute EDL command for the current position if one exists
2429 static void edl_update(MPContext *mpctx)
2431 if (!next_edl_record)
2432 return;
2434 if (!mpctx->sh_video) {
2435 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2436 free_edl(edl_records);
2437 next_edl_record = NULL;
2438 edl_records = NULL;
2439 return;
2442 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2443 if (next_edl_record->action == EDL_SKIP) {
2444 mpctx->osd_function = OSD_FFW;
2445 mpctx->abs_seek_pos = 0;
2446 mpctx->rel_seek_secs = next_edl_record->length_sec;
2447 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2448 "[%f], length [%f]\n", next_edl_record->start_sec,
2449 next_edl_record->stop_sec, next_edl_record->length_sec);
2451 else if (next_edl_record->action == EDL_MUTE) {
2452 mpctx->edl_muted = !mpctx->edl_muted;
2453 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2454 mixer_mute(&mpctx->mixer);
2455 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2456 next_edl_record->start_sec );
2458 next_edl_record = next_edl_record->next;
2463 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2464 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2465 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2466 static int seek(MPContext *mpctx, double amount, int style)
2468 current_module = "seek";
2469 if (mpctx->demuxer->accurate_seek && mpctx->sh_video
2470 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2471 style |= SEEK_ABSOLUTE;
2472 if (amount > 0)
2473 style |= SEEK_FORWARD;
2474 else
2475 style |= SEEK_BACKWARD;
2476 amount += mpctx->sh_video->pts;
2479 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2480 return -1;
2482 if (mpctx->sh_video) {
2483 current_module = "seek_video_reset";
2484 resync_video_stream(mpctx->sh_video);
2485 if (mpctx->video_out->config_ok)
2486 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2487 mpctx->sh_video->num_buffered_pts = 0;
2488 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2489 mpctx->num_buffered_frames = 0;
2490 mpctx->delay = 0;
2491 mpctx->time_frame = 0;
2492 mpctx->update_video_immediately = true;
2493 // Not all demuxers set d_video->pts during seek, so this value
2494 // (which is used by at least vobsub and edl code below) may
2495 // be completely wrong (probably 0).
2496 mpctx->sh_video->pts = mpctx->d_video->pts;
2497 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2498 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2501 if (mpctx->sh_audio) {
2502 current_module = "seek_audio_reset";
2503 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2504 mpctx->sh_audio->a_buffer_len = 0;
2505 mpctx->sh_audio->a_out_buffer_len = 0;
2508 if (vo_vobsub && mpctx->sh_video) {
2509 current_module = "seek_vobsub_reset";
2510 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2513 edl_seek_reset(mpctx);
2515 mpctx->total_avsync_change = 0;
2516 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2517 drop_frame_cnt = 0;
2519 current_module = NULL;
2520 return 0;
2524 static int read_keys(void *ctx, int fd)
2526 getch2(ctx);
2527 return mplayer_get_key(ctx, 0);
2531 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2532 * file for some tools to link against. */
2533 #ifndef DISABLE_MAIN
2534 int main(int argc,char* argv[]){
2537 char * mem_ptr;
2539 // movie info:
2541 /* Flag indicating whether MPlayer should exit without playing anything. */
2542 int opt_exit = 0;
2544 //float a_frame=0; // Audio
2546 int i;
2548 int gui_no_filename=0;
2550 struct MPContext *mpctx = &(struct MPContext){
2551 .osd_function = OSD_PLAY,
2552 .begin_skip = MP_NOPTS_VALUE,
2553 .play_tree_step = 1,
2554 .global_sub_pos = -1,
2555 .set_of_sub_pos = -1,
2556 .file_format = DEMUXER_TYPE_UNKNOWN,
2557 .last_dvb_step = 1,
2560 InitTimer();
2561 srand(GetTimerMS());
2563 mp_msg_init();
2565 #ifdef CONFIG_X11
2566 mpctx->x11_state = vo_x11_init_state();
2567 #endif
2568 struct MPOpts *opts = &mpctx->opts;
2569 set_default_mplayer_options(opts);
2570 // Create the config context and register the options
2571 mpctx->mconfig = m_config_new(opts, cfg_include);
2572 m_config_register_options(mpctx->mconfig,mplayer_opts);
2573 mp_input_register_options(mpctx->mconfig);
2575 // Preparse the command line
2576 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2578 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2579 set_path_env();
2580 #endif
2582 #ifdef CONFIG_TV
2583 stream_tv_defaults.immediate = 1;
2584 #endif
2586 if (argc > 1 && argv[1] &&
2587 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2588 use_gui = !strcmp(argv[1], "-gui");
2589 } else
2590 if ( argv[0] )
2592 char *base = strrchr(argv[0], '/');
2593 if (!base)
2594 base = strrchr(argv[0], '\\');
2595 if (!base)
2596 base = argv[0];
2597 if(strstr(base, "gmplayer"))
2598 use_gui=1;
2601 parse_cfgfiles(mpctx, mpctx->mconfig);
2603 #ifdef CONFIG_GUI
2604 if ( use_gui ) cfg_read();
2605 #endif
2607 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2608 if(mpctx->playtree == NULL)
2609 opt_exit = 1;
2610 else {
2611 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2612 if(mpctx->playtree) {
2613 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2614 if(mpctx->playtree_iter) {
2615 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2616 play_tree_iter_free(mpctx->playtree_iter);
2617 mpctx->playtree_iter = NULL;
2619 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2623 mpctx->key_fifo = mp_fifo_create(opts);
2625 print_version("MPlayer");
2627 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2628 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2629 if(runningmplayer && mpctx->filename && use_gui){
2630 COPYDATASTRUCT csData;
2631 char file[MAX_PATH];
2632 char *filepart = mpctx->filename;
2633 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2634 csData.dwData = 0;
2635 csData.cbData = strlen(file)*2;
2636 csData.lpData = file;
2637 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2640 #endif
2642 #if defined(__MINGW32__) || defined(__CYGWIN__)
2643 // stop Windows from showing all kinds of annoying error dialogs
2644 SetErrorMode(0x8003);
2645 // request 1ms timer resolution
2646 timeBeginPeriod(1);
2647 #endif
2649 #ifdef CONFIG_PRIORITY
2650 set_priority();
2651 #endif
2653 #ifndef CONFIG_GUI
2654 if(use_gui){
2655 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2656 use_gui=0;
2658 #else
2659 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2660 if(use_gui && !vo_init()){
2661 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2662 use_gui=0;
2664 #endif
2665 if (use_gui && mpctx->playtree_iter){
2666 char cwd[PATH_MAX+2];
2667 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2668 play_tree_iter_free(mpctx->playtree_iter);
2669 mpctx->playtree_iter=NULL;
2671 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2673 strcat(cwd, "/");
2674 // Prefix relative paths with current working directory
2675 play_tree_add_bpf(mpctx->playtree, cwd);
2677 // Import initital playtree into GUI.
2678 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2680 #endif /* CONFIG_GUI */
2682 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2683 list_video_out();
2684 opt_exit = 1;
2687 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2688 list_audio_out();
2689 opt_exit = 1;
2692 /* Check codecs.conf. */
2693 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2694 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2695 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2696 if(!parse_codec_cfg(NULL)){
2697 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2699 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2702 free( mem_ptr ); // release the buffer created by get_path()
2705 #if 0
2706 if(video_codec_list){
2707 int i;
2708 video_codec=video_codec_list[0];
2709 for(i=0;video_codec_list[i];i++)
2710 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2712 #endif
2713 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2714 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2715 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2716 list_codecs(1);
2717 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2718 opt_exit = 1;
2720 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2721 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2722 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2723 list_codecs(0);
2724 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2725 opt_exit = 1;
2727 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2728 vfm_help();
2729 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2730 opt_exit = 1;
2732 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2733 afm_help();
2734 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2735 opt_exit = 1;
2737 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2738 af_help();
2739 printf("\n");
2740 opt_exit = 1;
2742 #ifdef CONFIG_X11
2743 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2744 fstype_help();
2745 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2746 opt_exit = 1;
2748 #endif
2749 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2750 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2751 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2752 demuxer_help();
2753 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2754 opt_exit = 1;
2756 if(list_properties) {
2757 property_print_help();
2758 opt_exit = 1;
2761 if(opt_exit)
2762 exit_player(mpctx, EXIT_NONE);
2764 if (player_idle_mode && use_gui) {
2765 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2766 exit_player_with_rc(mpctx, EXIT_NONE, 1);
2769 if(!mpctx->filename && !player_idle_mode){
2770 if(!use_gui){
2771 // no file/vcd/dvd -> show HELP:
2772 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2773 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2774 } else gui_no_filename=1;
2777 /* Display what configure line was used */
2778 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2780 // Many users forget to include command line in bugreports...
2781 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2782 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2783 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2784 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2787 //------ load global data first ------
2789 mpctx->osd = osd_create();
2791 // check font
2792 #ifdef CONFIG_FREETYPE
2793 init_freetype();
2794 #endif
2795 #ifdef CONFIG_FONTCONFIG
2796 if(font_fontconfig <= 0)
2798 #endif
2799 #ifdef CONFIG_BITMAP_FONT
2800 if(font_name){
2801 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2802 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2803 filename_recode(font_name));
2804 } else {
2805 // try default:
2806 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2807 free(mem_ptr); // release the buffer created by get_path()
2808 if(!vo_font)
2809 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2811 if (sub_font_name)
2812 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2813 else
2814 mpctx->osd->sub_font = vo_font;
2815 #endif
2816 #ifdef CONFIG_FONTCONFIG
2818 #endif
2820 #ifdef CONFIG_ASS
2821 ass_library = ass_init();
2822 #endif
2824 #ifdef HAVE_RTC
2825 if(!nortc)
2827 // seteuid(0); /* Can't hurt to try to get root here */
2828 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2829 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2830 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2831 else {
2832 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2834 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2835 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2836 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2837 close (rtc_fd);
2838 rtc_fd = -1;
2839 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2840 /* variable only by the root */
2841 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2842 close (rtc_fd);
2843 rtc_fd = -1;
2844 } else
2845 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2848 #ifdef CONFIG_GUI
2849 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2850 // and now ? -- Pontscho
2851 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2852 #endif
2853 if(rtc_fd<0)
2854 #endif /* HAVE_RTC */
2855 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2856 softsleep?"software":timer_name);
2858 #ifdef HAVE_TERMCAP
2859 if ( !use_gui ) load_termcap(NULL); // load key-codes
2860 #endif
2862 // ========== Init keyboard FIFO (connection to libvo) ============
2864 // Init input system
2865 current_module = "init_input";
2866 mpctx->input = mp_input_init(&opts->input, use_gui);
2867 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2868 if(slave_mode)
2869 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2870 else if(!noconsolecontrols)
2871 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2872 // Set the libstream interrupt callback
2873 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2875 #ifdef CONFIG_MENU
2876 if(use_menu) {
2877 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2878 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2879 else {
2880 menu_cfg = get_path("menu.conf");
2881 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2882 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2883 else {
2884 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2885 MPLAYER_CONFDIR "/menu.conf"))
2886 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2887 else {
2888 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2889 use_menu = 0;
2894 #endif
2896 current_module = NULL;
2898 /// Catch signals
2899 #ifndef __MINGW32__
2900 signal(SIGCHLD,child_sighandler);
2901 #endif
2903 #ifdef CONFIG_CRASH_DEBUG
2904 prog_path = argv[0];
2905 #endif
2906 //========= Catch terminate signals: ================
2907 // terminate requests:
2908 signal(SIGTERM,exit_sighandler); // kill
2909 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2911 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2913 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2914 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2915 #ifdef CONFIG_SIGHANDLER
2916 // fatal errors:
2917 signal(SIGBUS,exit_sighandler); // bus error
2918 signal(SIGSEGV,exit_sighandler); // segfault
2919 signal(SIGILL,exit_sighandler); // illegal instruction
2920 signal(SIGFPE,exit_sighandler); // floating point exc.
2921 signal(SIGABRT,exit_sighandler); // abort()
2922 #ifdef CONFIG_CRASH_DEBUG
2923 if (crash_debug)
2924 signal(SIGTRAP,exit_sighandler);
2925 #endif
2926 #endif
2928 #ifdef CONFIG_GUI
2929 if(use_gui){
2930 guiInit();
2931 guiGetEvent(guiSetContext, mpctx);
2932 mpctx->initialized_flags|=INITIALIZED_GUI;
2933 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2935 #endif
2937 // ******************* Now, let's see the per-file stuff ********************
2939 play_next_file:
2941 // init global sub numbers
2942 mpctx->global_sub_size = 0;
2943 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2945 if (mpctx->filename) {
2946 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2947 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2948 load_per_file_config (mpctx->mconfig, mpctx->filename);
2951 if (opts->video_driver_list)
2952 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2953 if (opts->audio_driver_list)
2954 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2956 // We must enable getch2 here to be able to interrupt network connection
2957 // or cache filling
2958 if(!noconsolecontrols && !slave_mode){
2959 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2960 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2961 else
2962 getch2_enable(); // prepare stdin for hotkeys...
2963 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2964 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2967 // =================== GUI idle loop (STOP state) ===========================
2968 #ifdef CONFIG_GUI
2969 if ( use_gui ) {
2970 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2971 guiGetEvent( guiSetDefaults,0 );
2972 while ( guiIntfStruct.Playing != 1 )
2974 mp_cmd_t* cmd;
2975 usec_sleep(20000);
2976 guiEventHandling();
2977 guiGetEvent( guiReDraw,NULL );
2978 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
2979 guiGetEvent(guiIEvent, (char *)cmd->id);
2980 mp_cmd_free(cmd);
2983 guiGetEvent( guiSetParameters,NULL );
2984 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2986 play_tree_t * entry = play_tree_new();
2987 play_tree_add_file( entry,guiIntfStruct.Filename );
2988 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2989 else mpctx->playtree=play_tree_new();
2990 play_tree_set_child( mpctx->playtree,entry );
2991 if(mpctx->playtree)
2993 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2994 if(mpctx->playtree_iter)
2996 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2998 play_tree_iter_free(mpctx->playtree_iter);
2999 mpctx->playtree_iter = NULL;
3001 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3006 #endif /* CONFIG_GUI */
3008 while (player_idle_mode && !mpctx->filename) {
3009 play_tree_t * entry = NULL;
3010 mp_cmd_t * cmd;
3011 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3012 if (mpctx->video_out)
3013 vo_check_events(mpctx->video_out);
3014 usec_sleep(20000);
3016 switch (cmd->id) {
3017 case MP_CMD_LOADFILE:
3018 // prepare a tree entry with the new filename
3019 entry = play_tree_new();
3020 play_tree_add_file(entry, cmd->args[0].v.s);
3021 // The entry is added to the main playtree after the switch().
3022 break;
3023 case MP_CMD_LOADLIST:
3024 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3025 break;
3026 case MP_CMD_QUIT:
3027 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3028 break;
3029 case MP_CMD_GET_PROPERTY:
3030 case MP_CMD_SET_PROPERTY:
3031 case MP_CMD_STEP_PROPERTY:
3032 run_command(mpctx, cmd);
3033 break;
3036 mp_cmd_free(cmd);
3038 if (entry) { // user entered a command that gave a valid entry
3039 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3040 play_tree_free_list(mpctx->playtree->child, 1);
3041 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3043 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3045 play_tree_set_child(mpctx->playtree, entry);
3047 /* Make iterator start at the top the of tree. */
3048 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3049 if (!mpctx->playtree_iter) continue;
3051 // find the first real item in the tree
3052 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3053 // no items!
3054 play_tree_iter_free(mpctx->playtree_iter);
3055 mpctx->playtree_iter = NULL;
3056 continue; // wait for next command
3058 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3061 //---------------------------------------------------------------------------
3063 if(mpctx->filename)
3064 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3065 filename_recode(mpctx->filename));
3067 if (edl_filename) {
3068 if (edl_records) free_edl(edl_records);
3069 next_edl_record = edl_records = edl_parse_file();
3071 if (edl_output_filename) {
3072 if (edl_fd) fclose(edl_fd);
3073 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3075 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3076 filename_recode(edl_output_filename));
3080 //==================== Open VOB-Sub ============================
3082 current_module="vobsub";
3083 if (vobsub_name){
3084 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3085 if(vo_vobsub==NULL)
3086 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3087 filename_recode(vobsub_name));
3088 } else if (sub_auto && mpctx->filename){
3089 /* try to autodetect vobsub from movie filename ::atmos */
3090 char *buf = strdup(mpctx->filename), *psub;
3091 char *pdot = strrchr(buf, '.');
3092 char *pslash = strrchr(buf, '/');
3093 #if defined(__MINGW32__) || defined(__CYGWIN__)
3094 if (!pslash) pslash = strrchr(buf, '\\');
3095 #endif
3096 if (pdot && (!pslash || pdot > pslash))
3097 *pdot = '\0';
3098 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3099 /* try from ~/.mplayer/sub */
3100 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3101 char *bname;
3102 int l;
3103 bname = strrchr(buf,'/');
3104 #if defined(__MINGW32__) || defined(__CYGWIN__)
3105 if(!bname) bname = strrchr(buf,'\\');
3106 #endif
3107 if(bname) bname++;
3108 else bname = buf;
3109 l = strlen(psub) + strlen(bname) + 1;
3110 psub = realloc(psub,l);
3111 strcat(psub,bname);
3112 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3113 free(psub);
3115 free(buf);
3117 if(vo_vobsub){
3118 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3119 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3121 // setup global sub numbering
3122 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3123 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3126 //============ Open & Sync STREAM --- fork cache2 ====================
3128 mpctx->stream=NULL;
3129 mpctx->demuxer=NULL;
3130 if (mpctx->d_audio) {
3131 //free_demuxer_stream(mpctx->d_audio);
3132 mpctx->d_audio=NULL;
3134 if (mpctx->d_video) {
3135 //free_demuxer_stream(d_video);
3136 mpctx->d_video=NULL;
3138 mpctx->sh_audio=NULL;
3139 mpctx->sh_video=NULL;
3141 current_module="open_stream";
3142 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3143 if(!mpctx->stream) { // error...
3144 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3145 goto goto_next_file;
3147 mpctx->initialized_flags|=INITIALIZED_STREAM;
3149 #ifdef CONFIG_GUI
3150 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3151 #endif
3153 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3154 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3155 "playlist support will not be used automatically.\n"
3156 "MPlayer's playlist code is unsafe and should only be used with "
3157 "trusted sources.\nPlayback will probably fail.\n\n");
3158 #if 0
3159 play_tree_t* entry;
3160 // Handle playlist
3161 current_module="handle_playlist";
3162 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3163 filename_recode(mpctx->filename));
3164 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3165 mpctx->eof=playtree_add_playlist(mpctx, entry);
3166 goto goto_next_file;
3167 #endif
3169 mpctx->stream->start_pos+=seek_to_byte;
3171 if(stream_dump_type==5){
3172 unsigned char buf[4096];
3173 int len;
3174 FILE *f;
3175 current_module="dumpstream";
3176 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3177 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3178 exit_player(mpctx, EXIT_ERROR);
3180 stream_reset(mpctx->stream);
3181 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3182 f=fopen(stream_dump_name,"wb");
3183 if(!f){
3184 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3185 exit_player(mpctx, EXIT_ERROR);
3187 if (dvd_chapter > 1) {
3188 int chapter = dvd_chapter - 1;
3189 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3191 while(!mpctx->stream->eof && !async_quit_request){
3192 len=stream_read(mpctx->stream,buf,4096);
3193 if(len>0) {
3194 if(fwrite(buf,len,1,f) != 1) {
3195 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3196 exit_player(mpctx, EXIT_ERROR);
3199 if(dvd_last_chapter > 0) {
3200 int chapter = -1;
3201 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3202 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3203 break;
3206 if(fclose(f)) {
3207 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3208 exit_player(mpctx, EXIT_ERROR);
3210 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3211 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3214 #ifdef CONFIG_DVDREAD
3215 if(mpctx->stream->type==STREAMTYPE_DVD){
3216 current_module="dvd lang->id";
3217 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3218 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3219 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3220 // setup global sub numbering
3221 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3222 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3223 current_module=NULL;
3225 #endif
3227 #ifdef CONFIG_DVDNAV
3228 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3229 current_module="dvdnav lang->id";
3230 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3231 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3232 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3233 // setup global sub numbering
3234 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3235 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3236 current_module=NULL;
3238 #endif
3240 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3241 goto_enable_cache:
3242 if(stream_cache_size>0){
3243 current_module="enable_cache";
3244 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3245 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3246 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3247 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3250 //============ Open DEMUXERS --- DETECT file type =======================
3251 current_module="demux_open";
3253 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3255 // HACK to get MOV Reference Files working
3257 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3259 unsigned char* playlist_entry;
3260 play_tree_t *list = NULL, *entry = NULL;
3262 current_module="handle_demux_playlist";
3263 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3265 char *temp, *bname;
3267 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3268 filename_recode(playlist_entry));
3270 bname=mp_basename(playlist_entry);
3271 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3272 continue;
3274 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3275 continue;
3277 entry = play_tree_new();
3279 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3281 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3282 if (temp)
3284 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3285 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3286 strcat(temp, playlist_entry);
3287 play_tree_add_file(entry,temp);
3288 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3289 free(temp);
3292 else
3293 play_tree_add_file(entry,playlist_entry);
3295 if(!list)
3296 list = entry;
3297 else
3298 play_tree_append_entry(list,entry);
3300 free_demuxer(mpctx->demuxer);
3301 mpctx->demuxer = NULL;
3303 if (list)
3305 entry = play_tree_new();
3306 play_tree_set_child(entry,list);
3307 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3308 goto goto_next_file;
3312 if(!mpctx->demuxer)
3313 goto goto_next_file;
3314 if(dvd_chapter>1) {
3315 float pts;
3316 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3317 seek(mpctx, pts, SEEK_ABSOLUTE);
3320 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3322 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3323 int i;
3324 int maxid = -1;
3325 // setup global sub numbering
3326 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3327 for (i = 0; i < MAX_S_STREAMS; i++)
3328 if (mpctx->demuxer->s_streams[i])
3329 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3330 mpctx->global_sub_size += maxid + 1;
3332 // Make opts->sub_id always selectable if set.
3333 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3334 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3336 #ifdef CONFIG_ASS
3337 if (ass_enabled && ass_library) {
3338 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3339 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3340 if (extract_embedded_fonts &&
3341 att->name && att->type && att->data && att->data_size &&
3342 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3343 strcmp(att->type, "application/x-font") == 0))
3344 ass_add_font(ass_library, att->name, att->data, att->data_size);
3347 #endif
3349 current_module="demux_open2";
3351 //file_format=demuxer->file_format;
3353 mpctx->d_audio=mpctx->demuxer->audio;
3354 mpctx->d_video=mpctx->demuxer->video;
3355 mpctx->d_sub=mpctx->demuxer->sub;
3357 // select audio stream
3358 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3360 // DUMP STREAMS:
3361 if((stream_dump_type)&&(stream_dump_type<4)){
3362 FILE *f;
3363 demux_stream_t *ds=NULL;
3364 current_module="dump";
3365 // select stream to dump
3366 switch(stream_dump_type){
3367 case 1: ds=mpctx->d_audio;break;
3368 case 2: ds=mpctx->d_video;break;
3369 case 3: ds=mpctx->d_sub;break;
3371 if(!ds){
3372 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3373 exit_player(mpctx, EXIT_ERROR);
3375 // disable other streams:
3376 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3377 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3378 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3379 // let's dump it!
3380 f=fopen(stream_dump_name,"wb");
3381 if(!f){
3382 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3383 exit_player(mpctx, EXIT_ERROR);
3385 while(!ds->eof){
3386 unsigned char* start;
3387 int in_size=ds_get_packet(ds,&start);
3388 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3389 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3390 if(in_size>0) fwrite(start,in_size,1,f);
3391 if(dvd_last_chapter>0) {
3392 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3393 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3394 break;
3397 fclose(f);
3398 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3399 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3402 mpctx->sh_audio=mpctx->d_audio->sh;
3403 mpctx->sh_video=mpctx->d_video->sh;
3405 if(mpctx->sh_video){
3407 current_module="video_read_properties";
3408 if(!video_read_properties(mpctx->sh_video)) {
3409 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3410 mpctx->sh_video=mpctx->d_video->sh=NULL;
3411 } else {
3412 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3413 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3414 mpctx->sh_video->fps,mpctx->sh_video->frametime
3417 /* need to set fps here for output encoders to pick it up in their init */
3418 if(force_fps){
3419 mpctx->sh_video->fps=force_fps;
3420 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3422 vo_fps = mpctx->sh_video->fps;
3424 if(!mpctx->sh_video->fps && !force_fps){
3425 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3426 mpctx->sh_video=mpctx->d_video->sh=NULL;
3432 if(!mpctx->sh_video && !mpctx->sh_audio){
3433 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3434 #ifdef CONFIG_DVBIN
3435 if(mpctx->stream->type == STREAMTYPE_DVB)
3437 int dir;
3438 int v = mpctx->last_dvb_step;
3439 if(v > 0)
3440 dir = DVB_CHANNEL_HIGHER;
3441 else
3442 dir = DVB_CHANNEL_LOWER;
3444 if(dvb_step_channel(mpctx->stream, dir)) {
3445 mpctx->stop_play = PT_NEXT_ENTRY;
3446 mpctx->dvbin_reopen = 1;
3449 #endif
3450 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3453 /* display clip info */
3454 demux_info_print(mpctx->demuxer);
3456 //================== Read SUBTITLES (DVD & TEXT) ==========================
3457 if(vo_spudec==NULL && mpctx->sh_video &&
3458 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3459 init_vo_spudec(mpctx);
3462 if(mpctx->sh_video) {
3463 // after reading video params we should load subtitles because
3464 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3465 // check .sub
3466 current_module="read_subtitles_file";
3467 if(sub_name){
3468 for (i = 0; sub_name[i] != NULL; ++i)
3469 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3471 if(sub_auto) { // auto load sub file ...
3472 char *psub = get_path( "sub/" );
3473 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3474 int i = 0;
3475 free(psub); // release the buffer created by get_path() above
3476 while (tmp[i]) {
3477 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3478 free(tmp[i++]);
3480 free(tmp);
3482 if (mpctx->set_of_sub_size > 0) {
3483 // setup global sub numbering
3484 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3485 mpctx->global_sub_size += mpctx->set_of_sub_size;
3489 if (mpctx->global_sub_size) {
3490 // find the best sub to use
3491 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3492 mpctx->global_sub_pos = -1; // no subs by default
3493 if (vobsub_index_id >= 0) {
3494 // if user asks for a vobsub id, use that first.
3495 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3496 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3497 // if user asks for a dvd sub id, use that next.
3498 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3499 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3500 // if there are text subs to use, use those. (autosubs come last here)
3501 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3502 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3503 // finally select subs by language and container hints
3504 if (opts->sub_id < 0 && dvdsub_lang)
3505 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3506 if (opts->sub_id < 0)
3507 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3508 if (opts->sub_id >= 0)
3509 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3511 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3512 mpctx->global_sub_pos--;
3513 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3514 if(subdata)
3515 switch (stream_dump_type) {
3516 case 3: list_sub_file(subdata); break;
3517 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3518 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3519 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3520 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3521 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3525 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3526 filename_recode(mpctx->filename));
3527 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3528 if (mpctx->sh_video) {
3529 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3530 if (mpctx->sh_video->format >= 0x20202020)
3531 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3532 else
3533 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3534 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3535 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3537 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3540 if (mpctx->sh_audio) {
3541 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3542 if (mpctx->sh_audio->format >= 0x20202020)
3543 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3544 else
3545 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3546 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3547 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3548 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3550 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3551 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3552 if (mpctx->demuxer) {
3553 if (mpctx->demuxer->num_chapters == 0)
3554 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3555 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3558 if(!mpctx->sh_video) goto main; // audio-only
3560 if(!reinit_video_chain(mpctx)) {
3561 if(!mpctx->sh_video){
3562 if(!mpctx->sh_audio) goto goto_next_file;
3563 goto main; // exit_player(MSGTR_Exit_error);
3567 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3568 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3570 #ifdef CONFIG_FREETYPE
3571 force_load_font = 1;
3572 #endif
3574 //================== MAIN: ==========================
3575 main:
3576 current_module="main";
3578 if(playing_msg) {
3579 char* msg = property_expand_string(mpctx, playing_msg);
3580 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3581 free(msg);
3585 // Disable the term OSD in verbose mode
3586 if(verbose) term_osd = 0;
3589 //int frame_corr_num=0; //
3590 //float v_frame=0; // Video
3591 //float num_frames=0; // number of frames played
3593 int frame_time_remaining=0; // flag
3594 int blit_frame=0;
3595 mpctx->num_buffered_frames=0;
3597 // Make sure old OSD does not stay around,
3598 // e.g. with -fixed-vo and same-resolution files
3599 clear_osd_msgs();
3600 update_osd_msg(mpctx);
3602 //================ SETUP AUDIO ==========================
3604 if(mpctx->sh_audio){
3605 reinit_audio_chain(mpctx);
3606 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3607 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3610 current_module="av_init";
3612 if(mpctx->sh_video){
3613 mpctx->sh_video->timer=0;
3614 if (! ignore_start)
3615 audio_delay += mpctx->sh_video->stream_delay;
3617 if(mpctx->sh_audio){
3618 if (start_volume >= 0)
3619 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3620 if (! ignore_start)
3621 audio_delay -= mpctx->sh_audio->stream_delay;
3622 mpctx->delay=-audio_delay;
3625 if(!mpctx->sh_audio){
3626 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3627 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3628 ds_free_packs(mpctx->d_audio); // free buffered chunks
3629 //mpctx->d_audio->id=-2; // do not read audio chunks
3630 //uninit_player(mpctx, INITIALIZED_AO); // close device
3632 if(!mpctx->sh_video){
3633 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3634 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3635 ds_free_packs(mpctx->d_video);
3636 mpctx->d_video->id=-2;
3637 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3640 if (!mpctx->sh_video && !mpctx->sh_audio)
3641 goto goto_next_file;
3643 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3644 if(force_fps && mpctx->sh_video){
3645 vo_fps = mpctx->sh_video->fps=force_fps;
3646 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3647 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3650 #ifdef CONFIG_GUI
3651 if ( use_gui ) {
3652 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3653 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3654 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3655 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3656 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3658 #endif
3660 mp_input_set_section(mpctx->input, NULL);
3661 //TODO: add desired (stream-based) sections here
3662 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3663 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3665 //==================== START PLAYING =======================
3667 if(opts->loop_times>1) opts->loop_times--; else
3668 if(opts->loop_times==1) opts->loop_times = -1;
3670 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3672 total_time_usage_start=GetTimer();
3673 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3674 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3675 play_n_frames=play_n_frames_mf;
3677 if(play_n_frames==0){
3678 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3681 if (seek_to_sec) {
3682 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3683 end_at.pos += seek_to_sec;
3686 if (end_at.type == END_AT_SIZE) {
3687 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3688 end_at.type = END_AT_NONE;
3691 #ifdef CONFIG_DVDNAV
3692 mp_dvdnav_context_free(mpctx);
3693 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3694 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3695 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3697 #endif
3699 get_relative_time(mpctx); // reset current delta
3700 mpctx->time_frame = 0;
3701 mpctx->drop_message_shown = 0;
3702 mpctx->update_video_immediately = true;
3703 mpctx->total_avsync_change = 0;
3705 while(!mpctx->stop_play){
3706 float aq_sleep_time=0;
3708 if(dvd_last_chapter>0) {
3709 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3710 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3711 goto goto_next_file;
3714 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3715 mpctx->sh_audio = mpctx->d_audio->sh;
3716 mpctx->sh_audio->ds = mpctx->d_audio;
3717 reinit_audio_chain(mpctx);
3720 /*========================== PLAY AUDIO ============================*/
3722 if (mpctx->sh_audio && !mpctx->paused)
3723 if (!fill_audio_out_buffers(mpctx))
3724 // at eof, all audio at least written to ao
3725 if (!mpctx->sh_video)
3726 mpctx->stop_play = AT_END_OF_FILE;
3729 if(!mpctx->sh_video) {
3730 // handle audio-only case:
3731 double a_pos=0;
3732 // sh_audio can be NULL due to video stream switching
3733 // TODO: handle this better
3734 if (mpctx->sh_audio)
3735 a_pos = playing_audio_pts(mpctx);
3737 print_status(mpctx, a_pos, false);
3739 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3740 mpctx->stop_play = PT_NEXT_ENTRY;
3741 update_osd_msg(mpctx);
3743 } else {
3745 /*========================== PLAY VIDEO ============================*/
3747 vo_pts=mpctx->sh_video->timer*90000.0;
3748 vo_fps=mpctx->sh_video->fps;
3750 if (!mpctx->num_buffered_frames) {
3751 double frame_time = update_video(mpctx, &blit_frame);
3752 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3753 if (mpctx->sh_video->vf_initialized < 0) {
3754 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3755 mpctx->stop_play = PT_NEXT_ENTRY;
3756 goto goto_next_file;
3758 if (blit_frame) {
3759 struct vf_instance *vf = mpctx->sh_video->vfilter;
3760 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
3761 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
3762 vo_osd_changed(0);
3764 if (frame_time < 0)
3765 mpctx->stop_play = AT_END_OF_FILE;
3766 else {
3767 // might return with !eof && !blit_frame if !correct_pts
3768 mpctx->num_buffered_frames += blit_frame;
3769 if (mpctx->update_video_immediately) {
3770 // Show this frame immediately, rest normally
3771 mpctx->update_video_immediately = false;
3772 } else {
3773 mpctx->time_frame += frame_time / opts->playback_speed;
3774 adjust_sync(mpctx, frame_time);
3779 // ==========================================================================
3781 // current_module="draw_osd";
3782 // if(vo_config_count) mpctx->video_out->draw_osd();
3784 #ifdef CONFIG_GUI
3785 if(use_gui) guiEventHandling();
3786 #endif
3788 current_module="vo_check_events";
3789 vo_check_events(mpctx->video_out);
3791 #ifdef CONFIG_X11
3792 if (stop_xscreensaver) {
3793 current_module = "stop_xscreensaver";
3794 xscreensaver_heartbeat(mpctx->x11_state);
3796 #endif
3797 if (heartbeat_cmd) {
3798 static unsigned last_heartbeat;
3799 unsigned now = GetTimerMS();
3800 if (now - last_heartbeat > 30000) {
3801 last_heartbeat = now;
3802 system(heartbeat_cmd);
3806 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
3808 //====================== FLIP PAGE (VIDEO BLT): =========================
3810 current_module="flip_page";
3811 if (!frame_time_remaining && blit_frame) {
3812 unsigned int t2=GetTimer();
3814 vo_flip_page(mpctx->video_out);
3815 mpctx->num_buffered_frames--;
3817 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
3818 vout_time_usage += mpctx->last_vo_flip_duration;
3819 print_status(mpctx, MP_NOPTS_VALUE, true);
3821 else
3822 print_status(mpctx, MP_NOPTS_VALUE, false);
3824 //============================ Auto QUALITY ============================
3826 /*Output quality adjustments:*/
3827 if(auto_quality>0){
3828 current_module="autoq";
3829 // float total=0.000001f * (GetTimer()-aq_total_time);
3830 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3831 if(output_quality<auto_quality && aq_sleep_time>0)
3832 ++output_quality;
3833 else
3834 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3835 if(output_quality>1 && aq_sleep_time<0)
3836 --output_quality;
3837 else
3838 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3839 output_quality=0;
3840 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3841 set_video_quality(mpctx->sh_video,output_quality);
3844 if (!frame_time_remaining && blit_frame) {
3845 if (play_n_frames >= 0) {
3846 --play_n_frames;
3847 if (play_n_frames <= 0)
3848 mpctx->stop_play = PT_NEXT_ENTRY;
3850 if (mpctx->step_frames > 0) {
3851 mpctx->step_frames--;
3852 if (mpctx->step_frames == 0)
3853 pause_player(mpctx);
3858 // FIXME: add size based support for -endpos
3859 if (end_at.type == END_AT_TIME &&
3860 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3861 mpctx->stop_play = PT_NEXT_ENTRY;
3863 } // end if(mpctx->sh_video)
3865 #ifdef CONFIG_DVDNAV
3866 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3867 nav_highlight_t hl;
3868 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3869 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3870 vo_osd_changed (OSDTYPE_DVDNAV);
3872 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3873 double ar = -1.0;
3874 if (stream_control (mpctx->demuxer->stream,
3875 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3876 != STREAM_UNSUPPORTED)
3877 mpctx->sh_video->stream_aspect = ar;
3880 #endif
3882 //================= Keyboard events, SEEKing ====================
3884 current_module="key_events";
3887 while (1) {
3888 mp_cmd_t* cmd;
3889 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3890 run_command(mpctx, cmd);
3891 mp_cmd_free(cmd);
3892 if (mpctx->stop_play)
3893 break;
3895 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
3896 || mpctx->abs_seek_pos)
3897 break;
3898 if (mpctx->sh_video) {
3899 update_osd_msg(mpctx);
3900 int hack = vo_osd_changed(0);
3901 vo_osd_changed(hack);
3902 if (hack)
3903 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
3904 add_step_frame(mpctx);
3905 break;
3907 else
3908 vo_osd_changed(0);
3910 pause_loop(mpctx);
3914 // handle -sstep
3915 if (step_sec > 0 && !mpctx->paused) {
3916 mpctx->osd_function=OSD_FFW;
3917 mpctx->rel_seek_secs+=step_sec;
3920 edl_update(mpctx);
3922 /* Looping. */
3923 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3924 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3926 if(opts->loop_times>1) opts->loop_times--; else
3927 if(opts->loop_times==1) opts->loop_times=-1;
3928 play_n_frames=play_n_frames_mf;
3929 mpctx->stop_play=0;
3930 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3933 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3934 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
3936 mpctx->rel_seek_secs=0;
3937 mpctx->abs_seek_pos=0;
3940 #ifdef CONFIG_GUI
3941 if(use_gui){
3942 guiEventHandling();
3943 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3944 // get pos from frame number / total frames
3945 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3946 } else {
3947 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3949 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3950 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3951 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3952 guiGetEvent( guiReDraw,NULL );
3953 guiGetEvent( guiSetVolume,NULL );
3954 if(guiIntfStruct.Playing==0) break; // STOP
3955 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3956 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3957 #ifdef CONFIG_DVDREAD
3958 if ( mpctx->stream->type == STREAMTYPE_DVD )
3960 dvd_priv_t * dvdp = mpctx->stream->priv;
3961 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3963 #endif
3965 #endif /* CONFIG_GUI */
3967 } // while(!mpctx->stop_play)
3969 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
3971 #ifdef CONFIG_DVBIN
3972 if(mpctx->dvbin_reopen)
3974 mpctx->stop_play = 0;
3975 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3976 cache_uninit(mpctx->stream);
3977 mpctx->dvbin_reopen = 0;
3978 goto goto_enable_cache;
3980 #endif
3983 goto_next_file: // don't jump here after ao/vo/getch initialization!
3985 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3987 if(benchmark){
3988 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3989 double total_time_usage;
3990 total_time_usage_start=GetTimer()-total_time_usage_start;
3991 total_time_usage = (float)total_time_usage_start*0.000001;
3992 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3993 video_time_usage,vout_time_usage,audio_time_usage,
3994 total_time_usage-tot,total_time_usage);
3995 if(total_time_usage>0.0)
3996 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3997 100.0*video_time_usage/total_time_usage,
3998 100.0*vout_time_usage/total_time_usage,
3999 100.0*audio_time_usage/total_time_usage,
4000 100.0*(total_time_usage-tot)/total_time_usage,
4001 100.0);
4002 if(total_frame_cnt && frame_dropping)
4003 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4004 total_frame_cnt-drop_frame_cnt,
4005 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4006 drop_frame_cnt,
4007 100*drop_frame_cnt/total_frame_cnt,
4008 total_frame_cnt,
4009 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4012 // time to uninit all, except global stuff:
4013 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4015 if(mpctx->set_of_sub_size > 0) {
4016 current_module="sub_free";
4017 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4018 sub_free(mpctx->set_of_subtitles[i]);
4019 #ifdef CONFIG_ASS
4020 if(mpctx->set_of_ass_tracks[i])
4021 ass_free_track( mpctx->set_of_ass_tracks[i] );
4022 #endif
4024 mpctx->set_of_sub_size = 0;
4026 vo_sub_last = vo_sub=NULL;
4027 subdata=NULL;
4028 #ifdef CONFIG_ASS
4029 ass_track = NULL;
4030 if(ass_library)
4031 ass_clear_fonts(ass_library);
4032 #endif
4034 if (!mpctx->stop_play) // In case some goto jumped here...
4035 mpctx->stop_play = PT_NEXT_ENTRY;
4037 int playtree_direction = 1;
4039 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4040 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4041 play_tree_iter_free(mpctx->playtree_iter);
4042 mpctx->playtree_iter = NULL;
4044 mpctx->play_tree_step = 1;
4045 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4046 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4047 if(mpctx->playtree_iter) {
4048 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4049 play_tree_iter_free(mpctx->playtree_iter);
4050 mpctx->playtree_iter = NULL;
4053 } else if (mpctx->stop_play == PT_STOP) {
4054 play_tree_iter_free(mpctx->playtree_iter);
4055 mpctx->playtree_iter = NULL;
4056 } else { // NEXT PREV SRC
4057 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4060 while(mpctx->playtree_iter != NULL) {
4061 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4062 if(mpctx->filename == NULL) {
4063 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4064 play_tree_iter_free(mpctx->playtree_iter);
4065 mpctx->playtree_iter = NULL;
4067 } else
4068 break;
4071 #ifdef CONFIG_GUI
4072 if(use_gui && !mpctx->playtree_iter) {
4073 #ifdef CONFIG_DVDREAD
4074 if(!guiIntfStruct.DiskChanged)
4075 #endif
4076 mplEnd();
4078 #endif
4080 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4081 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4082 mpctx->stop_play = 0;
4083 goto play_next_file;
4087 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4089 return 1;
4091 #endif /* DISABLE_MAIN */