Remove global vo_flags
[mplayer.git] / mplayer.c
blobd2bee07ce4ffb78b7b37d25f3dc80296cbff2cfb
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
8 #include "talloc.h"
10 #ifdef WIN32
11 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
12 #include <windows.h>
13 #endif
14 #include <string.h>
15 #include <unistd.h>
17 // #include <sys/mman.h>
18 #include <sys/types.h>
19 #ifndef __MINGW32__
20 #include <sys/ioctl.h>
21 #include <sys/wait.h>
22 #else
23 #define SIGHUP 1 /* hangup */
24 #define SIGQUIT 3 /* quit */
25 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
26 #define SIGBUS 10 /* bus error */
27 #define SIGPIPE 13 /* broken pipe */
28 #endif
30 #include <sys/time.h>
31 #include <sys/stat.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include <errno.h>
40 #include "version.h"
42 #include "mp_msg.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "m_property.h"
51 #include "cfg-mplayer-def.h"
53 #include "libavutil/intreadwrite.h"
55 #include "subreader.h"
57 #include "mp_osd.h"
58 #include "libvo/video_out.h"
60 #include "libvo/font_load.h"
61 #include "libvo/sub.h"
63 #ifdef HAVE_X11
64 #include "libvo/x11_common.h"
65 #endif
67 #include "libao2/audio_out.h"
69 #include "codec-cfg.h"
71 #include "edl.h"
73 #include "spudec.h"
74 #include "vobsub.h"
76 #include "osdep/getch2.h"
77 #include "osdep/timer.h"
79 #include "cpudetect.h"
81 #ifdef HAVE_NEW_GUI
82 #include "gui/interface.h"
83 #endif
85 #include "input/input.h"
87 int slave_mode=0;
88 int player_idle_mode=0;
89 int quiet=0;
90 int enable_mouse_movements=0;
92 #ifdef WIN32
93 char * proc_priority=NULL;
94 #endif
96 char *heartbeat_cmd;
98 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
100 #ifdef HAVE_RTC
101 #ifdef __linux__
102 #include <linux/rtc.h>
103 #else
104 #include <rtc.h>
105 #define RTC_IRQP_SET RTCIO_IRQP_SET
106 #define RTC_PIE_ON RTCIO_PIE_ON
107 #endif /* __linux__ */
108 #endif /* HAVE_RTC */
110 #ifdef USE_TV
111 #include "stream/tv.h"
112 #endif
113 #ifdef USE_RADIO
114 #include "stream/stream_radio.h"
115 #endif
117 #ifdef HAS_DVBIN_SUPPORT
118 #include "stream/dvbin.h"
119 #include "stream/cache2.h"
120 #endif
122 //**************************************************************************//
123 // Playtree
124 //**************************************************************************//
125 #include "playtree.h"
126 #include "playtreeparser.h"
128 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
129 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
131 //**************************************************************************//
132 // Config
133 //**************************************************************************//
134 #include "parser-cfg.h"
135 #include "parser-mpcmd.h"
137 m_config_t* mconfig;
139 //**************************************************************************//
140 // Config file
141 //**************************************************************************//
143 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
145 static int cfg_include(m_option_t *conf, char *filename){
146 return m_config_parse_config_file(mconfig, filename);
149 #include "get_path.h"
151 //**************************************************************************//
152 // XScreensaver
153 //**************************************************************************//
155 void xscreensaver_heartbeat(struct vo_x11_state *);
157 //**************************************************************************//
158 //**************************************************************************//
159 // Input media streaming & demultiplexer:
160 //**************************************************************************//
162 static int max_framesize=0;
164 #include "stream/stream.h"
165 #include "libmpdemux/demuxer.h"
166 #include "libmpdemux/stheader.h"
168 #ifdef USE_DVDREAD
169 #include "stream/stream_dvd.h"
170 #endif
172 #ifdef USE_DVDNAV
173 #include "stream/stream_dvdnav.h"
174 #endif
176 #include "libmpcodecs/dec_audio.h"
177 #include "libmpcodecs/dec_video.h"
178 #include "libmpcodecs/mp_image.h"
179 #include "libmpcodecs/vf.h"
180 #include "libmpcodecs/vd.h"
182 #include "mixer.h"
184 #include "mp_core.h"
185 #include "options.h"
186 #include "defaultopts.h"
188 //**************************************************************************//
189 //**************************************************************************//
191 // Common FIFO functions, and keyboard/event FIFO code
192 #include "mp_fifo.h"
193 int noconsolecontrols=0;
194 //**************************************************************************//
196 // benchmark:
197 double video_time_usage=0;
198 double vout_time_usage=0;
199 static double audio_time_usage=0;
200 static int total_time_usage_start=0;
201 static int total_frame_cnt=0;
202 static int drop_frame_cnt=0; // total number of dropped frames
203 int benchmark=0;
205 // options:
206 int auto_quality=0;
207 static int output_quality=0;
209 int use_gui=0;
211 #ifdef HAVE_NEW_GUI
212 int enqueue=0;
213 #endif
215 static int list_properties = 0;
217 int osd_level=1;
218 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
219 unsigned int osd_visible;
220 int osd_duration = 1000;
222 int term_osd = 1;
223 static char* term_osd_esc = "\x1b[A\r\x1b[K";
224 static char* playing_msg = NULL;
225 // seek:
226 static double seek_to_sec;
227 static off_t seek_to_byte=0;
228 static off_t step_sec=0;
229 static int loop_seek=0;
231 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
233 // A/V sync:
234 int autosync=0; // 30 might be a good default value.
236 // codecs:
237 char **audio_codec_list=NULL; // override audio codec
238 char **video_codec_list=NULL; // override video codec
239 char **audio_fm_list=NULL; // override audio codec family
240 char **video_fm_list=NULL; // override video codec family
242 // demuxer:
243 extern char *demuxer_name; // override demuxer
244 extern char *audio_demuxer_name; // override audio demuxer
245 extern char *sub_demuxer_name; // override sub demuxer
247 int vobsub_id=-1;
248 char* audio_lang=NULL;
249 char* dvdsub_lang=NULL;
250 static char* spudec_ifo=NULL;
251 char* filename=NULL; //"MI2-Trailer.avi";
252 int forced_subs_only=0;
253 int file_filter=1;
255 // cache2:
256 int stream_cache_size=-1;
257 #ifdef USE_STREAM_CACHE
258 extern int cache_fill_status;
260 float stream_cache_min_percent=20.0;
261 float stream_cache_seek_min_percent=50.0;
262 #else
263 #define cache_fill_status 0
264 #endif
266 // dump:
267 static char *stream_dump_name="stream.dump";
268 int stream_dump_type=0;
270 // A-V sync:
271 static float default_max_pts_correction=-1;//0.01f;
272 static float max_pts_correction=0;//default_max_pts_correction;
273 static float c_total=0;
274 float audio_delay=0;
275 static int ignore_start=0;
277 static int softsleep=0;
279 double force_fps=0;
280 static int force_srate=0;
281 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
282 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
283 static int play_n_frames=-1;
284 static int play_n_frames_mf=-1;
286 // sub:
287 char *font_name=NULL;
288 char *sub_font_name=NULL;
289 extern int font_fontconfig;
290 float font_factor=0.75;
291 char **sub_name=NULL;
292 float sub_delay=0;
293 float sub_fps=0;
294 int sub_auto = 1;
295 char *vobsub_name=NULL;
296 /*DSP!!char *dsp=NULL;*/
297 int subcc_enabled=0;
298 int suboverlap_enabled = 1;
300 #ifdef USE_ASS
301 #include "libass/ass.h"
302 #include "libass/ass_mp.h"
303 #endif
305 char* current_module=NULL; // for debugging
308 // ---
310 #ifdef HAVE_MENU
311 #include "m_struct.h"
312 #include "libmenu/menu.h"
313 extern void vf_menu_pause_update(struct vf_instance* vf);
314 extern vf_info_t vf_info_menu;
315 static vf_info_t* libmenu_vfs[] = {
316 &vf_info_menu,
317 NULL
319 static vf_instance_t* vf_menu = NULL;
320 int use_menu = 0;
321 static char* menu_cfg = NULL;
322 static char* menu_root = "main";
323 #endif
326 #ifdef HAVE_RTC
327 static int nortc = 1;
328 static char* rtc_device;
329 #endif
331 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
332 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
333 short edl_decision = 0; ///< 1 when an EDL operation has been made.
334 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
335 int use_filedir_conf;
337 static unsigned int initialized_flags=0;
338 #include "mpcommon.h"
339 #include "command.h"
341 #include "metadata.h"
343 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
345 const void *mpctx_get_video_out(MPContext *mpctx)
347 return mpctx->video_out;
350 void *mpctx_get_audio_out(MPContext *mpctx)
352 return mpctx->audio_out;
355 void *mpctx_get_demuxer(MPContext *mpctx)
357 return mpctx->demuxer;
360 void *mpctx_get_playtree_iter(MPContext *mpctx)
362 return mpctx->playtree_iter;
365 void *mpctx_get_mixer(MPContext *mpctx)
367 return &mpctx->mixer;
370 int mpctx_get_global_sub_size(MPContext *mpctx)
372 return mpctx->global_sub_size;
375 int mpctx_get_osd_function(MPContext *mpctx)
377 return mpctx->osd_function;
380 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
381 switch (type)
383 /* check for valid video stream */
384 case META_VIDEO_CODEC:
385 case META_VIDEO_BITRATE:
386 case META_VIDEO_RESOLUTION:
388 if (!mpctx->sh_video)
389 return 0;
390 break;
393 /* check for valid audio stream */
394 case META_AUDIO_CODEC:
395 case META_AUDIO_BITRATE:
396 case META_AUDIO_SAMPLES:
398 if (!mpctx->sh_audio)
399 return 0;
400 break;
403 /* check for valid demuxer */
404 case META_INFO_TITLE:
405 case META_INFO_ARTIST:
406 case META_INFO_ALBUM:
407 case META_INFO_YEAR:
408 case META_INFO_COMMENT:
409 case META_INFO_TRACK:
410 case META_INFO_GENRE:
412 if (!mpctx->demuxer)
413 return 0;
414 break;
417 default:
418 break;
421 return 1;
424 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
425 char **info = mpctx->demuxer->info;
426 int n;
428 if (!info || !tag)
429 return NULL;
431 for (n = 0; info[2*n] != NULL ; n++)
432 if (!strcmp (info[2*n], tag))
433 break;
435 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
438 char *get_metadata(struct MPContext *mpctx, metadata_t type)
440 char *meta = NULL;
441 sh_audio_t * const sh_audio = mpctx->sh_audio;
442 sh_video_t * const sh_video = mpctx->sh_video;
444 if (!is_valid_metadata_type(mpctx, type))
445 return NULL;
447 switch (type)
449 case META_NAME:
451 return strdup (mp_basename2 (filename));
454 case META_VIDEO_CODEC:
456 if (sh_video->format == 0x10000001)
457 meta = strdup ("mpeg1");
458 else if (sh_video->format == 0x10000002)
459 meta = strdup ("mpeg2");
460 else if (sh_video->format == 0x10000004)
461 meta = strdup ("mpeg4");
462 else if (sh_video->format == 0x10000005)
463 meta = strdup ("h264");
464 else if (sh_video->format >= 0x20202020)
466 meta = malloc (8);
467 sprintf (meta, "%.4s", (char *) &sh_video->format);
469 else
471 meta = malloc (8);
472 sprintf (meta, "0x%08X", sh_video->format);
474 return meta;
477 case META_VIDEO_BITRATE:
479 meta = malloc (16);
480 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
481 return meta;
484 case META_VIDEO_RESOLUTION:
486 meta = malloc (16);
487 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
488 return meta;
491 case META_AUDIO_CODEC:
493 if (sh_audio->codec && sh_audio->codec->name)
494 meta = strdup (sh_audio->codec->name);
495 return meta;
498 case META_AUDIO_BITRATE:
500 meta = malloc (16);
501 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
502 return meta;
505 case META_AUDIO_SAMPLES:
507 meta = malloc (16);
508 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
509 return meta;
512 /* check for valid demuxer */
513 case META_INFO_TITLE:
514 return get_demuxer_info(mpctx, "Title");
516 case META_INFO_ARTIST:
517 return get_demuxer_info(mpctx, "Artist");
519 case META_INFO_ALBUM:
520 return get_demuxer_info(mpctx, "Album");
522 case META_INFO_YEAR:
523 return get_demuxer_info(mpctx, "Year");
525 case META_INFO_COMMENT:
526 return get_demuxer_info(mpctx, "Comment");
528 case META_INFO_TRACK:
529 return get_demuxer_info(mpctx, "Track");
531 case META_INFO_GENRE:
532 return get_demuxer_info(mpctx, "Genre");
534 default:
535 break;
538 return meta;
541 /// step size of mixer changes
542 int volstep = 3;
544 #ifdef USE_DVDNAV
545 static void mp_dvdnav_context_free(MPContext *ctx){
546 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
547 ctx->nav_smpi = NULL;
548 if (ctx->nav_buffer) free(ctx->nav_buffer);
549 ctx->nav_buffer = NULL;
550 ctx->nav_start = NULL;
551 ctx->nav_in_size = 0;
553 #endif
555 void uninit_player(struct MPContext *mpctx, unsigned int mask){
556 mask=initialized_flags&mask;
558 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
560 if(mask&INITIALIZED_ACODEC){
561 initialized_flags&=~INITIALIZED_ACODEC;
562 current_module="uninit_acodec";
563 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
564 #ifdef HAVE_NEW_GUI
565 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
566 #endif
567 mpctx->sh_audio=NULL;
568 mpctx->mixer.afilter = NULL;
571 if(mask&INITIALIZED_VCODEC){
572 initialized_flags&=~INITIALIZED_VCODEC;
573 current_module="uninit_vcodec";
574 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
575 mpctx->sh_video=NULL;
576 #ifdef HAVE_MENU
577 vf_menu=NULL;
578 #endif
581 if(mask&INITIALIZED_DEMUXER){
582 initialized_flags&=~INITIALIZED_DEMUXER;
583 current_module="free_demuxer";
584 if(mpctx->demuxer){
585 mpctx->stream=mpctx->demuxer->stream;
586 free_demuxer(mpctx->demuxer);
588 mpctx->demuxer=NULL;
591 // kill the cache process:
592 if(mask&INITIALIZED_STREAM){
593 initialized_flags&=~INITIALIZED_STREAM;
594 current_module="uninit_stream";
595 if(mpctx->stream) free_stream(mpctx->stream);
596 mpctx->stream=NULL;
599 if(mask&INITIALIZED_VO){
600 initialized_flags&=~INITIALIZED_VO;
601 current_module="uninit_vo";
602 vo_destroy(mpctx->video_out);
603 mpctx->video_out=NULL;
604 #ifdef USE_DVDNAV
605 mp_dvdnav_context_free(mpctx);
606 #endif
609 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
610 if(mask&INITIALIZED_GETCH2){
611 initialized_flags&=~INITIALIZED_GETCH2;
612 current_module="uninit_getch2";
613 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
614 // restore terminal:
615 getch2_disable();
618 if(mask&INITIALIZED_VOBSUB){
619 initialized_flags&=~INITIALIZED_VOBSUB;
620 current_module="uninit_vobsub";
621 if(vo_vobsub) vobsub_close(vo_vobsub);
622 vo_vobsub=NULL;
625 if (mask&INITIALIZED_SPUDEC){
626 initialized_flags&=~INITIALIZED_SPUDEC;
627 current_module="uninit_spudec";
628 spudec_free(vo_spudec);
629 vo_spudec=NULL;
632 if(mask&INITIALIZED_AO){
633 initialized_flags&=~INITIALIZED_AO;
634 current_module="uninit_ao";
635 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
636 mpctx->audio_out->uninit(mpctx->eof?0:1); mpctx->audio_out=NULL;
639 #ifdef HAVE_NEW_GUI
640 if(mask&INITIALIZED_GUI){
641 initialized_flags&=~INITIALIZED_GUI;
642 current_module="uninit_gui";
643 guiDone();
645 #endif
647 if(mask&INITIALIZED_INPUT){
648 initialized_flags&=~INITIALIZED_INPUT;
649 current_module="uninit_input";
650 mp_input_uninit();
651 #ifdef HAVE_MENU
652 if (use_menu)
653 menu_uninit();
654 #endif
657 current_module=NULL;
660 void exit_player_with_rc(struct MPContext *mpctx, const char* how, int rc){
662 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
663 uninit_player(mpctx, INITIALIZED_ALL);
664 #ifdef HAVE_X11
665 #ifdef HAVE_NEW_GUI
666 if ( !use_gui )
667 #endif
668 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
669 #endif
671 #ifdef HAVE_FREETYPE
672 current_module="uninit_font";
673 if (sub_font && sub_font != vo_font) free_font_desc(sub_font);
674 sub_font = NULL;
675 if (vo_font) free_font_desc(vo_font);
676 vo_font = NULL;
677 done_freetype();
678 #endif
679 free_osd_list();
681 #ifdef USE_ASS
682 ass_library_done(ass_library);
683 #endif
685 current_module="exit_player";
687 // free mplayer config
688 if(mconfig)
689 m_config_free(mconfig);
691 if(mpctx->playtree)
692 play_tree_free(mpctx->playtree, 1);
695 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
696 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
697 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
699 exit(rc);
702 static void exit_player(struct MPContext *mpctx, const char* how)
704 exit_player_with_rc(mpctx, how, 1);
707 #ifndef __MINGW32__
708 static void child_sighandler(int x){
709 pid_t pid;
710 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
712 #endif
714 #ifdef CRASH_DEBUG
715 static char *prog_path;
716 static int crash_debug = 0;
717 #endif
719 static void exit_sighandler(int x){
720 static int sig_count=0;
721 #ifdef CRASH_DEBUG
722 if (!crash_debug || x != SIGTRAP)
723 #endif
724 ++sig_count;
725 if(initialized_flags==0 && sig_count>1) exit(1);
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 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 extern void mp_input_register_options(m_config_t* cfg);
792 #include "cfg-mplayer.h"
794 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
796 char *conffile;
797 int conffile_fd;
798 if (!disable_system_conf &&
799 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
800 exit_player(mpctx, NULL);
801 if ((conffile = get_path("")) == NULL) {
802 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
803 } else {
804 #ifdef __MINGW32__
805 mkdir(conffile);
806 #else
807 mkdir(conffile, 0777);
808 #endif
809 free(conffile);
810 if ((conffile = get_path("config")) == NULL) {
811 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
812 } else {
813 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
814 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
815 write(conffile_fd, default_config, strlen(default_config));
816 close(conffile_fd);
818 if (!disable_user_conf &&
819 m_config_parse_config_file(conf, conffile) < 0)
820 exit_player(mpctx, NULL);
821 free(conffile);
826 #define PROFILE_CFG_PROTOCOL "protocol."
828 static void load_per_protocol_config (m_config_t* conf, const char *const file)
830 char *str;
831 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
832 m_profile_t *p;
834 /* does filename actually uses a protocol ? */
835 str = strstr (file, "://");
836 if (!str)
837 return;
839 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
840 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
841 p = m_config_get_profile (conf, protocol);
842 if (p)
844 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
845 m_config_set_profile(conf,p);
849 #define PROFILE_CFG_EXTENSION "extension."
851 static void load_per_extension_config (m_config_t* conf, const char *const file)
853 char *str;
854 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
855 m_profile_t *p;
857 /* does filename actually have an extension ? */
858 str = strrchr (filename, '.');
859 if (!str)
860 return;
862 sprintf (extension, PROFILE_CFG_EXTENSION);
863 strncat (extension, ++str, 7);
864 p = m_config_get_profile (conf, extension);
865 if (p)
867 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
868 m_config_set_profile(conf,p);
872 #define PROFILE_CFG_VO "vo."
873 #define PROFILE_CFG_AO "ao."
875 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
877 char profile[strlen (cfg) + strlen (out) + 1];
878 m_profile_t *p;
880 sprintf (profile, "%s%s", cfg, out);
881 p = m_config_get_profile (conf, profile);
882 if (p)
884 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
885 m_config_set_profile(conf,p);
889 static void load_per_file_config (m_config_t* conf, const char *const file)
891 char *confpath;
892 char cfg[strlen(file)+10];
893 struct stat st;
894 char *name;
896 sprintf (cfg, "%s.conf", file);
898 if (use_filedir_conf && !stat (cfg, &st))
900 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
901 m_config_parse_config_file (conf, cfg);
902 return;
905 if ((name = strrchr (cfg, '/')) == NULL)
906 name = cfg;
907 else
908 name++;
910 if ((confpath = get_path (name)) != NULL)
912 if (!stat (confpath, &st))
914 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
915 m_config_parse_config_file (conf, confpath);
918 free (confpath);
922 /* When libmpdemux performs a blocking operation (network connection or
923 * cache filling) if the operation fails we use this function to check
924 * if it was interrupted by the user.
925 * The function returns a new value for eof. */
926 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int eof)
928 mp_cmd_t* cmd;
929 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
930 switch(cmd->id) {
931 case MP_CMD_QUIT:
932 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
933 case MP_CMD_PLAY_TREE_STEP: {
934 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
935 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
936 } break;
937 case MP_CMD_PLAY_TREE_UP_STEP: {
938 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
939 } break;
940 case MP_CMD_PLAY_ALT_SRC_STEP: {
941 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
942 } break;
944 mp_cmd_free(cmd);
946 return eof;
949 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
951 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
953 play_tree_add_bpf(entry,filename);
955 #ifdef HAVE_NEW_GUI
956 if (use_gui) {
957 if (entry) {
958 import_playtree_playlist_into_gui(entry, mconfig);
959 play_tree_free_list(entry,1);
961 } else
962 #endif
964 if(!entry) {
965 entry = mpctx->playtree_iter->tree;
966 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
967 return PT_NEXT_ENTRY;
969 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
970 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
971 return PT_NEXT_ENTRY;
974 play_tree_remove(entry,1,1);
975 return PT_NEXT_SRC;
977 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
978 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
979 entry = mpctx->playtree_iter->tree;
980 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
981 return PT_NEXT_ENTRY;
983 play_tree_remove(entry,1,1);
985 return PT_NEXT_SRC;
988 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
990 sub_data *subd;
991 #ifdef USE_ASS
992 ass_track_t *asst = 0;
993 #endif
995 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
996 return;
999 subd = sub_read_file(filename, fps);
1000 #ifdef USE_ASS
1001 if (ass_enabled)
1002 #ifdef USE_ICONV
1003 asst = ass_read_file(ass_library, filename, sub_cp);
1004 #else
1005 asst = ass_read_file(ass_library, filename, 0);
1006 #endif
1007 if (ass_enabled && subd && !asst)
1008 asst = ass_read_subdata(ass_library, subd, fps);
1010 if (!asst && !subd)
1011 #else
1012 if(!subd)
1013 #endif
1014 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1015 filename_recode(filename));
1017 #ifdef USE_ASS
1018 if (!asst && !subd) return;
1019 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1020 #else
1021 if (!subd) return;
1022 #endif
1023 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1024 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1025 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1026 filename_recode(filename));
1027 ++mpctx->set_of_sub_size;
1028 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1029 filename_recode(filename));
1032 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1033 void update_set_of_subtitles(struct MPContext *mpctx)
1034 // subdata was changed, set_of_sub... have to be updated.
1036 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1037 int i;
1038 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1039 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1040 set_of_subtitles[i-1] = set_of_subtitles[i];
1041 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1042 --mpctx->set_of_sub_size;
1043 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1045 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1046 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1048 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1049 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1050 ++mpctx->set_of_sub_size;
1054 void init_vo_spudec(struct MPContext *mpctx)
1056 if (vo_spudec)
1057 spudec_free(vo_spudec);
1058 initialized_flags &= ~INITIALIZED_SPUDEC;
1059 vo_spudec = NULL;
1060 if (spudec_ifo) {
1061 unsigned int palette[16], width, height;
1062 current_module="spudec_init_vobsub";
1063 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1064 vo_spudec=spudec_new_scaled(palette, width, height);
1067 #ifdef USE_DVDREAD
1068 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1069 current_module="spudec_init_dvdread";
1070 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1071 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1073 #endif
1075 #ifdef USE_DVDNAV
1076 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1077 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1078 current_module="spudec_init_dvdnav";
1079 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1081 #endif
1083 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1084 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1085 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1086 current_module = "spudec_init_matroska";
1087 vo_spudec =
1088 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1089 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1090 mkv_sh_sub->height);
1091 forced_subs_only = mkv_sh_sub->forced_subs_only;
1094 if (vo_spudec==NULL) {
1095 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1096 unsigned int *palette = NULL;
1097 if (sh && !sh->has_palette && sh->extradata_len == 16*4) {
1098 int i;
1099 for (i = 0; i < 16; i++)
1100 sh->palette[i] = AV_RB32(sh->extradata + i*4);
1101 sh->has_palette = 1;
1103 if (sh && sh->has_palette)
1104 palette = sh->palette;
1105 current_module="spudec_init_normal";
1106 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1107 spudec_set_font_factor(vo_spudec,font_factor);
1110 if (vo_spudec!=NULL)
1111 initialized_flags|=INITIALIZED_SPUDEC;
1115 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1116 * make it all work is to use the builtin SDL-bootstrap code, which
1117 * will be done automatically by replacing our main() if we include SDL.h.
1119 #if defined(__APPLE__) && defined(HAVE_SDL)
1120 #include <SDL.h>
1121 #endif
1124 * \brief append a formatted string
1125 * \param buf buffer to print into
1126 * \param pos position of terminating 0 in buf
1127 * \param len maximum number of characters in buf, not including terminating 0
1128 * \param format printf format string
1130 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1132 va_list va;
1133 va_start(va, format);
1134 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1135 va_end(va);
1136 if (*pos >= len ) {
1137 buf[len] = 0;
1138 *pos = len;
1143 * \brief append time in the hh:mm:ss.f format
1144 * \param buf buffer to print into
1145 * \param pos position of terminating 0 in buf
1146 * \param len maximum number of characters in buf, not including terminating 0
1147 * \param time time value to convert/append
1149 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1150 long tenths = 10 * time;
1151 int f1 = tenths % 10;
1152 int ss = (tenths / 10) % 60;
1153 int mm = (tenths / 600) % 60;
1154 int hh = tenths / 36000;
1155 if (time <= 0) {
1156 saddf(buf, pos, len, "unknown");
1157 return;
1159 if (hh > 0)
1160 saddf(buf, pos, len, "%2d:", hh);
1161 if (hh > 0 || mm > 0)
1162 saddf(buf, pos, len, "%02d:", mm);
1163 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1167 * \brief print the status line
1168 * \param a_pos audio position
1169 * \param a_v A-V desynchronization
1170 * \param corr amount out A-V synchronization
1172 static void print_status(struct MPContext *mpctx, float a_pos, float a_v, float corr)
1174 struct MPOpts *opts = &mpctx->opts;
1175 sh_video_t * const sh_video = mpctx->sh_video;
1176 int width;
1177 char *line;
1178 unsigned pos = 0;
1179 get_screen_size();
1180 if (screen_width > 0)
1181 width = screen_width;
1182 else
1183 width = 80;
1184 #if defined(WIN32) || defined(__OS2__)
1185 /* Windows command line is broken (MinGW's rxvt works, but we
1186 * should not depend on that). */
1187 width--;
1188 #endif
1189 line = malloc(width + 1); // one additional char for the terminating null
1191 // Audio time
1192 if (mpctx->sh_audio) {
1193 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1194 if (!sh_video) {
1195 float len = demuxer_get_time_length(mpctx->demuxer);
1196 saddf(line, &pos, width, "(");
1197 sadd_hhmmssf(line, &pos, width, a_pos);
1198 saddf(line, &pos, width, ") of %.1f (", len);
1199 sadd_hhmmssf(line, &pos, width, len);
1200 saddf(line, &pos, width, ") ");
1204 // Video time
1205 if (sh_video)
1206 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1208 // A-V sync
1209 if (mpctx->sh_audio && sh_video)
1210 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1212 // Video stats
1213 if (sh_video)
1214 saddf(line, &pos, width, "%3d/%3d ",
1215 (int)sh_video->num_frames,
1216 (int)sh_video->num_frames_decoded);
1218 // CPU usage
1219 if (sh_video) {
1220 if (sh_video->timer > 0.5)
1221 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1222 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1223 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1224 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1225 else
1226 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1227 } else if (mpctx->sh_audio) {
1228 if (mpctx->delay > 0.5)
1229 saddf(line, &pos, width, "%4.1f%% ",
1230 100.0*audio_time_usage/(double)mpctx->delay);
1231 else
1232 saddf(line, &pos, width, "??,?%% ");
1235 // VO stats
1236 if (sh_video)
1237 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1239 #ifdef USE_STREAM_CACHE
1240 // cache stats
1241 if (stream_cache_size > 0)
1242 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1243 #endif
1245 // other
1246 if (opts->playback_speed != 1)
1247 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1249 // end
1250 if (erase_to_end_of_line) {
1251 line[pos] = 0;
1252 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1253 } else {
1254 memset(&line[pos], ' ', width - pos);
1255 line[width] = 0;
1256 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1258 free(line);
1262 * \brief build a chain of audio filters that converts the input format
1263 * to the ao's format, taking into account the current playback_speed.
1264 * \param sh_audio describes the requested input format of the chain.
1265 * \param ao_data describes the requested output format of the chain.
1267 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1269 struct MPOpts *opts = &mpctx->opts;
1270 int new_srate;
1271 int result;
1272 if (!sh_audio)
1274 #ifdef HAVE_NEW_GUI
1275 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1276 #endif
1277 mpctx->mixer.afilter = NULL;
1278 return 0;
1280 if(af_control_any_rev(sh_audio->afilter,
1281 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1282 &opts->playback_speed)) {
1283 new_srate = sh_audio->samplerate;
1284 } else {
1285 new_srate = sh_audio->samplerate * opts->playback_speed;
1286 if (new_srate != ao_data->samplerate) {
1287 // limits are taken from libaf/af_resample.c
1288 if (new_srate < 8000)
1289 new_srate = 8000;
1290 if (new_srate > 192000)
1291 new_srate = 192000;
1292 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1295 result = init_audio_filters(sh_audio, new_srate,
1296 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1297 mpctx->mixer.afilter = sh_audio->afilter;
1298 #ifdef HAVE_NEW_GUI
1299 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1300 #endif
1301 return result;
1305 typedef struct mp_osd_msg mp_osd_msg_t;
1306 struct mp_osd_msg {
1307 /// Previous message on the stack.
1308 mp_osd_msg_t* prev;
1309 /// Message text.
1310 char msg[64];
1311 int id,level,started;
1312 /// Display duration in ms.
1313 unsigned time;
1316 /// OSD message stack.
1317 static mp_osd_msg_t* osd_msg_stack = NULL;
1320 * \brief Add a message on the OSD message stack
1322 * If a message with the same id is already present in the stack
1323 * it is pulled on top of the stack, otherwise a new message is created.
1327 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1328 mp_osd_msg_t *msg,*last=NULL;
1329 va_list va;
1330 int r;
1332 // look if the id is already in the stack
1333 for(msg = osd_msg_stack ; msg && msg->id != id ;
1334 last = msg, msg = msg->prev);
1335 // not found: alloc it
1336 if(!msg) {
1337 msg = calloc(1,sizeof(mp_osd_msg_t));
1338 msg->prev = osd_msg_stack;
1339 osd_msg_stack = msg;
1340 } else if(last) { // found, but it's not on top of the stack
1341 last->prev = msg->prev;
1342 msg->prev = osd_msg_stack;
1343 osd_msg_stack = msg;
1345 // write the msg
1346 va_start(va,fmt);
1347 r = vsnprintf(msg->msg, 64, fmt, va);
1348 va_end(va);
1349 if(r >= 64) msg->msg[63] = 0;
1350 // set id and time
1351 msg->id = id;
1352 msg->level = level;
1353 msg->time = time;
1358 * \brief Remove a message from the OSD stack
1360 * This function can be used to get rid of a message right away.
1364 void rm_osd_msg(int id) {
1365 mp_osd_msg_t *msg,*last=NULL;
1367 // Search for the msg
1368 for(msg = osd_msg_stack ; msg && msg->id != id ;
1369 last = msg, msg = msg->prev);
1370 if(!msg) return;
1372 // Detach it from the stack and free it
1373 if(last)
1374 last->prev = msg->prev;
1375 else
1376 osd_msg_stack = msg->prev;
1377 free(msg);
1381 * \brief Remove all messages from the OSD stack
1385 static void clear_osd_msgs(void) {
1386 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1387 while(msg) {
1388 prev = msg->prev;
1389 free(msg);
1390 msg = prev;
1392 osd_msg_stack = NULL;
1396 * \brief Get the current message from the OSD stack.
1398 * This function decrements the message timer and destroys the old ones.
1399 * The message that should be displayed is returned (if any).
1403 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1405 mp_osd_msg_t *msg,*prev,*last = NULL;
1406 static unsigned last_update = 0;
1407 unsigned now = GetTimerMS();
1408 unsigned diff;
1409 char hidden_dec_done = 0;
1411 if (osd_visible) {
1412 // 36000000 means max timed visibility is 1 hour into the future, if
1413 // the difference is greater assume it's wrapped around from below 0
1414 if (osd_visible - now > 36000000) {
1415 osd_visible = 0;
1416 vo_osd_progbar_type = -1; // disable
1417 vo_osd_changed(OSDTYPE_PROGBAR);
1418 if (mpctx->osd_function != OSD_PAUSE)
1419 mpctx->osd_function = OSD_PLAY;
1423 if(!last_update) last_update = now;
1424 diff = now >= last_update ? now - last_update : 0;
1426 last_update = now;
1428 // Look for the first message in the stack with high enough level.
1429 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1430 prev = msg->prev;
1431 if(msg->level > osd_level && hidden_dec_done) continue;
1432 // The message has a high enough level or it is the first hidden one
1433 // in both cases we decrement the timer or kill it.
1434 if(!msg->started || msg->time > diff) {
1435 if(msg->started) msg->time -= diff;
1436 else msg->started = 1;
1437 // display it
1438 if(msg->level <= osd_level) return msg;
1439 hidden_dec_done = 1;
1440 continue;
1442 // kill the message
1443 free(msg);
1444 if(last) {
1445 last->prev = prev;
1446 msg = last;
1447 } else {
1448 osd_msg_stack = prev;
1449 msg = NULL;
1452 // Nothing found
1453 return NULL;
1457 * \brief Display the OSD bar.
1459 * Display the OSD bar or fall back on a simple message.
1463 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1465 if(osd_level < 1) return;
1467 if(mpctx->sh_video) {
1468 osd_visible = (GetTimerMS() + 1000) | 1;
1469 vo_osd_progbar_type = type;
1470 vo_osd_progbar_value = 256*(val-min)/(max-min);
1471 vo_osd_changed(OSDTYPE_PROGBAR);
1472 return;
1475 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1476 name,ROUND(100*(val-min)/(max-min)));
1481 * \brief Update the OSD message line.
1483 * This function displays the current message on the vo OSD or on the term.
1484 * If the stack is empty and the OSD level is high enough the timer
1485 * is displayed (only on the vo OSD).
1489 static void update_osd_msg(struct MPContext *mpctx)
1491 mp_osd_msg_t *msg;
1492 static char osd_text[64] = "";
1493 static char osd_text_timer[64];
1495 // we need some mem for vo_osd_text
1496 vo_osd_text = (unsigned char*)osd_text;
1498 // Look if we have a msg
1499 if((msg = get_osd_msg(mpctx))) {
1500 if(strcmp(osd_text,msg->msg)) {
1501 strncpy((char*)osd_text, msg->msg, 63);
1502 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1503 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1505 return;
1508 if(mpctx->sh_video) {
1509 // fallback on the timer
1510 if(osd_level>=2) {
1511 int len = demuxer_get_time_length(mpctx->demuxer);
1512 int percentage = -1;
1513 char percentage_text[10];
1514 int pts = demuxer_get_current_time(mpctx->demuxer);
1516 if (mpctx->osd_show_percentage)
1517 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1519 if (percentage >= 0)
1520 snprintf(percentage_text, 9, " (%d%%)", percentage);
1521 else
1522 percentage_text[0] = 0;
1524 if (osd_level == 3)
1525 snprintf(osd_text_timer, 63,
1526 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1527 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1528 len/3600,(len/60)%60,len%60,percentage_text);
1529 else
1530 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1531 mpctx->osd_function,pts/3600,(pts/60)%60,
1532 pts%60,percentage_text);
1533 } else
1534 osd_text_timer[0]=0;
1536 // always decrement the percentage timer
1537 if(mpctx->osd_show_percentage)
1538 mpctx->osd_show_percentage--;
1540 if(strcmp(osd_text,osd_text_timer)) {
1541 strncpy(osd_text, osd_text_timer, 63);
1542 vo_osd_changed(OSDTYPE_OSD);
1544 return;
1547 // Clear the term osd line
1548 if(term_osd && osd_text[0]) {
1549 osd_text[0] = 0;
1550 printf("%s\n",term_osd_esc);
1554 ///@}
1555 // OSDMsgStack
1558 void reinit_audio_chain(struct MPContext *mpctx)
1560 struct MPOpts *opts = &mpctx->opts;
1561 if(mpctx->sh_audio){
1562 current_module="init_audio_codec";
1563 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1564 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1565 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1566 mpctx->d_audio->id = -2;
1567 return;
1568 } else
1569 initialized_flags|=INITIALIZED_ACODEC;
1570 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1573 //const ao_info_t *info=audio_out->info;
1574 current_module="af_preinit";
1575 ao_data.samplerate=force_srate;
1576 ao_data.channels=0;
1577 ao_data.format=audio_output_format;
1578 #if 1
1579 // first init to detect best values
1580 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1581 // input:
1582 mpctx->sh_audio->samplerate,
1583 // output:
1584 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1585 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1586 exit_player(mpctx, MSGTR_Exit_error);
1588 #endif
1589 current_module="ao2_init";
1590 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1591 0, // plugin flag
1592 ao_data.samplerate,
1593 ao_data.channels,
1594 ao_data.format,0))){
1595 // FAILED:
1596 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1597 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1598 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1599 mpctx->d_audio->id = -2;
1600 return;
1601 } else {
1602 // SUCCESS:
1603 initialized_flags|=INITIALIZED_AO;
1604 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1605 mpctx->audio_out->info->short_name,
1606 ao_data.samplerate, ao_data.channels,
1607 af_fmt2str_short(ao_data.format),
1608 af_fmt2bits(ao_data.format)/8 );
1609 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1610 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1611 if(strlen(mpctx->audio_out->info->comment) > 0)
1612 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1613 // init audio filters:
1614 #if 1
1615 current_module="af_init";
1616 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1617 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1618 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1619 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1620 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1622 #endif
1624 mpctx->mixer.audio_out = mpctx->audio_out;
1625 mpctx->mixer.volstep = volstep;
1630 ///@}
1631 // Command2Property
1634 // Return pts value corresponding to the end point of audio written to the
1635 // ao so far.
1636 static double written_audio_pts(struct MPContext *mpctx)
1638 sh_audio_t *sh_audio = mpctx->sh_audio;
1639 demux_stream_t *d_audio = mpctx->d_audio;
1640 double buffered_output;
1641 // first calculate the end pts of audio that has been output by decoder
1642 double a_pts = sh_audio->pts;
1643 if (a_pts != MP_NOPTS_VALUE)
1644 // Good, decoder supports new way of calculating audio pts.
1645 // sh_audio->pts is the timestamp of the latest input packet with
1646 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1647 // the amount of bytes the decoder has written after that timestamp.
1648 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1649 else {
1650 // Decoder doesn't support new way of calculating pts (or we're
1651 // being called before it has decoded anything with known timestamp).
1652 // Use the old method of audio pts calculation: take the timestamp
1653 // of last packet with known pts the decoder has read data from,
1654 // and add amount of bytes read after the beginning of that packet
1655 // divided by input bps. This will be inaccurate if the input/output
1656 // ratio is not constant for every audio packet or if it is constant
1657 // but not accurately known in sh_audio->i_bps.
1659 a_pts = d_audio->pts;
1660 // ds_tell_pts returns bytes read after last timestamp from
1661 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1662 // it has read but not decoded
1663 if (sh_audio->i_bps)
1664 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1665 (double)sh_audio->i_bps;
1667 // Now a_pts hopefully holds the pts for end of audio from decoder.
1668 // Substract data in buffers between decoder and audio out.
1670 // Decoded but not filtered
1671 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1673 // Data buffered in audio filters, measured in bytes of "missing" output
1674 buffered_output = af_calc_delay(sh_audio->afilter);
1676 // Data that was ready for ao but was buffered because ao didn't fully
1677 // accept everything to internal buffers yet
1678 buffered_output += sh_audio->a_out_buffer_len;
1680 // Filters divide audio length by playback_speed, so multiply by it
1681 // to get the length in original units without speedup or slowdown
1682 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1684 return a_pts;
1687 // Return pts value corresponding to currently playing audio.
1688 double playing_audio_pts(struct MPContext *mpctx)
1690 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1691 mpctx->audio_out->get_delay();
1694 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1695 struct MPOpts *opts = &mpctx->opts;
1696 // check for frame-drop:
1697 current_module = "check_framedrop";
1698 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1699 static int dropped_frames;
1700 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1701 float d = delay-mpctx->delay;
1702 ++total_frame_cnt;
1703 // we should avoid dropping too many frames in sequence unless we
1704 // are too late. and we allow 100ms A-V delay here:
1705 if (d < -dropped_frames*frame_time-0.100 &&
1706 mpctx->osd_function != OSD_PAUSE) {
1707 ++drop_frame_cnt;
1708 ++dropped_frames;
1709 return frame_dropping;
1710 } else
1711 dropped_frames = 0;
1713 return 0;
1716 static int generate_video_frame(struct MPContext *mpctx)
1718 sh_video_t * const sh_video = mpctx->sh_video;
1719 demux_stream_t *d_video = mpctx->d_video;
1721 unsigned char *start;
1722 int in_size;
1723 int hit_eof=0;
1724 double pts;
1726 while (1) {
1727 int drop_frame = check_framedrop(mpctx, sh_video->frametime);
1728 void *decoded_frame;
1729 current_module = "decode video";
1730 // XXX Time used in this call is not counted in any performance
1731 // timer now, OSD is not updated correctly for filter-added frames
1732 if (vf_output_queued_frame(sh_video->vfilter))
1733 break;
1734 current_module = "video_read_frame";
1735 in_size = ds_get_packet_pts(d_video, &start, &pts);
1736 if (in_size < 0) {
1737 // try to extract last frames in case of decoder lag
1738 in_size = 0;
1739 pts = 1e300;
1740 hit_eof = 1;
1742 if (in_size > max_framesize)
1743 max_framesize = in_size;
1744 current_module = "decode video";
1745 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1746 if (decoded_frame) {
1747 update_subtitles(sh_video, mpctx->d_sub, 0);
1748 update_teletext(sh_video, mpctx->demuxer, 0);
1749 update_osd_msg(mpctx);
1750 current_module = "filter video";
1751 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1752 break;
1753 } else if (drop_frame)
1754 return -1;
1755 if (hit_eof)
1756 return 0;
1758 return 1;
1761 #ifdef HAVE_RTC
1762 int rtc_fd = -1;
1763 #endif
1765 static float timing_sleep(float time_frame)
1767 #ifdef HAVE_RTC
1768 if (rtc_fd >= 0){
1769 // -------- RTC -----------
1770 current_module="sleep_rtc";
1771 while (time_frame > 0.000) {
1772 unsigned long rtc_ts;
1773 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1774 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1775 time_frame -= GetRelativeTime();
1777 } else
1778 #endif
1780 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1781 // unnecessarily high CPU usage
1782 float margin = softsleep ? 0.011 : 0;
1783 current_module = "sleep_timer";
1784 while (time_frame > margin) {
1785 usec_sleep(1000000 * (time_frame - margin));
1786 time_frame -= GetRelativeTime();
1788 if (softsleep){
1789 current_module = "sleep_soft";
1790 if (time_frame < 0)
1791 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1792 while (time_frame > 0)
1793 time_frame-=GetRelativeTime(); // burn the CPU
1796 return time_frame;
1799 #ifdef USE_DVDNAV
1800 #ifndef FF_B_TYPE
1801 #define FF_B_TYPE 3
1802 #endif
1803 /// store decoded video image
1804 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1805 mp_image_t *from_mpi) {
1806 mp_image_t *mpi;
1808 /// Do not store B-frames
1809 if (from_mpi->pict_type == FF_B_TYPE)
1810 return to_mpi;
1812 if (to_mpi &&
1813 to_mpi->w == from_mpi->w &&
1814 to_mpi->h == from_mpi->h &&
1815 to_mpi->imgfmt == from_mpi->imgfmt)
1816 mpi = to_mpi;
1817 else {
1818 if (to_mpi)
1819 free_mp_image(to_mpi);
1820 if (from_mpi->w == 0 || from_mpi->h == 0)
1821 return NULL;
1822 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1825 copy_mpi(mpi,from_mpi);
1826 return mpi;
1829 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1830 if (ctx->sh_video) {
1831 /// clear video pts
1832 ctx->d_video->pts = 0.0f;
1833 ctx->sh_video->pts = 0.0f;
1834 ctx->sh_video->i_pts = 0.0f;
1835 ctx->sh_video->last_pts = 0.0f;
1836 ctx->sh_video->num_buffered_pts = 0;
1837 ctx->sh_video->num_frames = 0;
1838 ctx->sh_video->num_frames_decoded = 0;
1839 ctx->sh_video->timer = 0.0f;
1840 ctx->sh_video->stream_delay = 0.0f;
1841 ctx->sh_video->timer = 0;
1842 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1845 if (ctx->sh_audio) {
1846 /// free audio packets and reset
1847 ds_free_packs(ctx->d_audio);
1848 audio_delay -= ctx->sh_audio->stream_delay;
1849 ctx->delay =- audio_delay;
1850 ctx->audio_out->reset();
1851 resync_audio_stream(ctx->sh_audio);
1854 if (ctx->d_sub) opts->sub_id = -2;
1856 audio_delay = 0.0f;
1858 /// clear all EOF related flags
1859 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1862 /// Restore last decoded DVDNAV (still frame)
1863 static mp_image_t *mp_dvdnav_restore_smpi(int *in_size,
1864 unsigned char **start,
1865 mp_image_t *decoded_frame)
1867 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1868 return decoded_frame;
1870 /// a change occured in dvdnav stream
1871 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1872 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1873 mp_dvdnav_context_free(mpctx);
1874 mp_dvdnav_reset_stream(mpctx);
1875 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1876 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1879 if (*in_size < 0) {
1880 float len;
1882 /// Display still frame, if any
1883 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1884 decoded_frame = mpctx->nav_smpi;
1886 /// increment video frame : continue playing after still frame
1887 len = demuxer_get_time_length(mpctx->demuxer);
1888 if (mpctx->sh_video->pts >= len &&
1889 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1890 mp_dvdnav_skip_still(mpctx->stream);
1891 mp_dvdnav_skip_wait(mpctx->stream);
1893 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1895 if (mpctx->nav_buffer) {
1896 *start = mpctx->nav_start;
1897 *in_size = mpctx->nav_in_size;
1898 if (mpctx->nav_start)
1899 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1903 return decoded_frame;
1906 /// Save last decoded DVDNAV (still frame)
1907 static void mp_dvdnav_save_smpi(int in_size,
1908 unsigned char *start,
1909 mp_image_t *decoded_frame)
1911 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1912 return;
1914 if (mpctx->nav_buffer)
1915 free(mpctx->nav_buffer);
1917 mpctx->nav_buffer = malloc(in_size);
1918 mpctx->nav_start = start;
1919 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1920 if (mpctx->nav_buffer)
1921 memcpy(mpctx->nav_buffer,start,in_size);
1923 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1924 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1926 #endif /* USE_DVDNAV */
1928 static void adjust_sync_and_print_status(struct MPContext *mpctx,
1929 int between_frames,
1930 float timing_error)
1932 struct MPOpts *opts = &mpctx->opts;
1933 current_module="av_sync";
1935 if(mpctx->sh_audio){
1936 double a_pts, v_pts;
1938 if (autosync)
1940 * If autosync is enabled, the value for delay must be calculated
1941 * a bit differently. It is set only to the difference between
1942 * the audio and video timers. Any attempt to include the real
1943 * or corrected delay causes the pts_correction code below to
1944 * try to correct for the changes in delay which autosync is
1945 * trying to measure. This keeps the two from competing, but still
1946 * allows the code to correct for PTS drift *only*. (Using a delay
1947 * value here, even a "corrected" one, would be incompatible with
1948 * autosync mode.)
1950 a_pts = written_audio_pts(mpctx) - mpctx->delay;
1951 else
1952 a_pts = playing_audio_pts(mpctx);
1954 v_pts = mpctx->sh_video->pts;
1957 static int drop_message=0;
1958 double AV_delay = a_pts - audio_delay - v_pts;
1959 double x;
1960 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1961 ++drop_message;
1962 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1964 if (autosync)
1965 x = AV_delay*0.1f;
1966 else
1967 /* Do not correct target time for the next frame if this frame
1968 * was late not because of wrong target time but because the
1969 * target time could not be met */
1970 x = (AV_delay + timing_error * opts->playback_speed) * 0.1f;
1971 if (x < -max_pts_correction)
1972 x = -max_pts_correction;
1973 else if (x> max_pts_correction)
1974 x = max_pts_correction;
1975 if (default_max_pts_correction >= 0)
1976 max_pts_correction = default_max_pts_correction;
1977 else
1978 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1979 if (!between_frames) {
1980 mpctx->delay+=x;
1981 c_total+=x;
1983 if(!quiet)
1984 print_status(mpctx, a_pts - audio_delay, AV_delay, c_total);
1987 } else {
1988 // No audio:
1990 if (!quiet)
1991 print_status(mpctx, 0, 0, 0);
1995 static int fill_audio_out_buffers(struct MPContext *mpctx)
1997 struct MPOpts *opts = &mpctx->opts;
1998 unsigned int t;
1999 double tt;
2000 int playsize;
2001 int playflags=0;
2002 int audio_eof=0;
2003 int bytes_to_write;
2004 sh_audio_t * const sh_audio = mpctx->sh_audio;
2006 current_module="play_audio";
2008 while (1) {
2009 // all the current uses of ao_data.pts seem to be in aos that handle
2010 // sync completely wrong; there should be no need to use ao_data.pts
2011 // in get_space()
2012 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2013 bytes_to_write = mpctx->audio_out->get_space();
2014 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2015 break;
2017 // handle audio-only case:
2018 // this is where mplayer sleeps during audio-only playback
2019 // to avoid 100% CPU use
2020 usec_sleep(10000); // Wait a tick before retry
2023 while (bytes_to_write) {
2024 playsize = bytes_to_write;
2025 if (playsize > MAX_OUTBURST)
2026 playsize = MAX_OUTBURST;
2027 bytes_to_write -= playsize;
2029 // Fill buffer if needed:
2030 current_module="decode_audio";
2031 t = GetTimer();
2032 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2033 if (mpctx->d_audio->eof) {
2034 audio_eof = 1;
2035 if (sh_audio->a_out_buffer_len == 0)
2036 return 0;
2038 t = GetTimer() - t;
2039 tt = t*0.000001f; audio_time_usage+=tt;
2040 if (playsize > sh_audio->a_out_buffer_len) {
2041 playsize = sh_audio->a_out_buffer_len;
2042 if (audio_eof)
2043 playflags |= AOPLAY_FINAL_CHUNK;
2045 if (!playsize)
2046 break;
2048 // play audio:
2049 current_module="play_audio";
2051 // Is this pts value actually useful for the aos that access it?
2052 // They're obviously badly broken in the way they handle av sync;
2053 // would not having access to this make them more broken?
2054 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2055 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2057 if (playsize > 0) {
2058 sh_audio->a_out_buffer_len -= playsize;
2059 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2060 sh_audio->a_out_buffer_len);
2061 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2063 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2064 // Sanity check to avoid hanging in case current ao doesn't output
2065 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2066 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2067 sh_audio->a_out_buffer_len = 0;
2070 return 1;
2073 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2074 float *aq_sleep_time)
2076 struct MPOpts *opts = &mpctx->opts;
2077 int frame_time_remaining = 0;
2078 current_module="calc_sleep_time";
2080 *time_frame -= GetRelativeTime(); // reset timer
2082 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2083 float delay = mpctx->audio_out->get_delay();
2084 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2086 if (autosync) {
2088 * Adjust this raw delay value by calculating the expected
2089 * delay for this frame and generating a new value which is
2090 * weighted between the two. The higher autosync is, the
2091 * closer to the delay value gets to that which "-nosound"
2092 * would have used, and the longer it will take for A/V
2093 * sync to settle at the right value (but it eventually will.)
2094 * This settling time is very short for values below 100.
2096 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2097 float difference = delay - predicted;
2098 delay = predicted + difference / (float)autosync;
2101 *time_frame = delay - mpctx->delay / opts->playback_speed;
2103 // delay = amount of audio buffered in soundcard/driver
2104 if (delay > 0.25) delay=0.25; else
2105 if (delay < 0.10) delay=0.10;
2106 if (*time_frame > delay*0.6) {
2107 // sleep time too big - may cause audio drops (buffer underrun)
2108 frame_time_remaining = 1;
2109 *time_frame = delay*0.5;
2111 } else {
2112 // If we're lagging more than 200 ms behind the right playback rate,
2113 // don't try to "catch up".
2114 // If benchmark is set always output frames as fast as possible
2115 // without sleeping.
2116 if (*time_frame < -0.2 || benchmark)
2117 *time_frame = 0;
2120 *aq_sleep_time += *time_frame;
2123 //============================== SLEEP: ===================================
2125 // flag 256 means: libvo driver does its timing (dvb card)
2126 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2127 *time_frame = timing_sleep(*time_frame);
2128 return frame_time_remaining;
2131 int reinit_video_chain(struct MPContext *mpctx)
2133 struct MPOpts *opts = &mpctx->opts;
2134 sh_video_t * const sh_video = mpctx->sh_video;
2135 double ar=-1.0;
2136 //================== Init VIDEO (codec & libvo) ==========================
2137 if(opts->fixed_vo || !(initialized_flags&INITIALIZED_VO)){
2138 current_module="preinit_libvo";
2140 //shouldn't we set dvideo->id=-2 when we fail?
2141 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2142 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state))){
2143 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2144 goto err_out;
2146 initialized_flags|=INITIALIZED_VO;
2149 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2150 mpctx->sh_video->stream_aspect = ar;
2151 current_module="init_video_filters";
2153 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2154 sh_video->vfilter=(void*)vf_open_filter(opts, NULL,"vo",vf_arg);
2156 #ifdef HAVE_MENU
2157 if(use_menu) {
2158 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2159 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2160 if(!vf_menu) {
2161 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2162 use_menu = 0;
2165 if(vf_menu)
2166 sh_video->vfilter=(void*)vf_menu;
2167 #endif
2169 #ifdef USE_ASS
2170 if(ass_enabled) {
2171 int i;
2172 int insert = 1;
2173 if (vf_settings)
2174 for (i = 0; vf_settings[i].name; ++i)
2175 if (strcmp(vf_settings[i].name, "ass") == 0) {
2176 insert = 0;
2177 break;
2179 if (insert) {
2180 extern vf_info_t vf_info_ass;
2181 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2182 char* vf_arg[] = {"auto", "1", NULL};
2183 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2184 if (vf_ass)
2185 sh_video->vfilter=(void*)vf_ass;
2186 else
2187 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2190 #endif
2192 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
2194 #ifdef USE_ASS
2195 if (ass_enabled)
2196 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2197 #endif
2199 current_module="init_video_codec";
2201 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2202 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2203 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2205 if(!sh_video->initialized){
2206 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2207 goto err_out;
2210 initialized_flags|=INITIALIZED_VCODEC;
2212 if (sh_video->codec)
2213 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2215 sh_video->last_pts = MP_NOPTS_VALUE;
2216 sh_video->num_buffered_pts = 0;
2217 sh_video->next_frame_time = 0;
2219 if(auto_quality>0){
2220 // Auto quality option enabled
2221 output_quality=get_video_quality_max(sh_video);
2222 if(auto_quality>output_quality) auto_quality=output_quality;
2223 else output_quality=auto_quality;
2224 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2225 set_video_quality(sh_video,output_quality);
2228 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2230 current_module="init_vo";
2232 return 1;
2234 err_out:
2235 mpctx->sh_video = mpctx->d_video->sh = NULL;
2236 return 0;
2239 static double update_video(struct MPContext *mpctx, int *blit_frame)
2241 struct MPOpts *opts = &mpctx->opts;
2242 sh_video_t * const sh_video = mpctx->sh_video;
2243 //-------------------- Decode a frame: -----------------------
2244 double frame_time;
2245 *blit_frame = 0; // Don't blit if we hit EOF
2246 if (!opts->correct_pts) {
2247 unsigned char* start=NULL;
2248 void *decoded_frame = NULL;
2249 int drop_frame=0;
2250 int in_size;
2252 current_module = "video_read_frame";
2253 frame_time = sh_video->next_frame_time;
2254 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2255 &start, force_fps);
2256 #ifdef USE_DVDNAV
2257 /// wait, still frame or EOF
2258 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2259 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2260 if (mpctx->d_video) mpctx->d_video->eof = 0;
2261 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2262 mpctx->stream->eof = 0;
2263 } else
2264 #endif
2265 if (in_size < 0)
2266 return -1;
2267 if (in_size > max_framesize)
2268 max_framesize = in_size; // stats
2269 sh_video->timer += frame_time;
2270 if (mpctx->sh_audio)
2271 mpctx->delay -= frame_time;
2272 // video_read_frame can change fps (e.g. for ASF video)
2273 vo_fps = sh_video->fps;
2274 drop_frame = check_framedrop(mpctx, frame_time);
2275 update_subtitles(sh_video, mpctx->d_sub, 0);
2276 update_teletext(sh_video, mpctx->demuxer, 0);
2277 update_osd_msg(mpctx);
2278 current_module = "decode_video";
2279 #ifdef USE_DVDNAV
2280 decoded_frame = mp_dvdnav_restore_smpi(&in_size,&start,decoded_frame);
2281 /// still frame has been reached, no need to decode
2282 if (in_size > 0 && !decoded_frame)
2283 #endif
2284 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2285 sh_video->pts);
2286 #ifdef USE_DVDNAV
2287 /// save back last still frame for future display
2288 mp_dvdnav_save_smpi(in_size,start,decoded_frame);
2289 #endif
2290 current_module = "filter_video";
2291 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2292 sh_video->pts));
2294 else {
2295 int res = generate_video_frame(mpctx);
2296 if (!res)
2297 return -1;
2298 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2299 VFCTRL_GET_PTS, &sh_video->pts);
2300 if (sh_video->pts == MP_NOPTS_VALUE) {
2301 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2302 sh_video->pts = sh_video->last_pts;
2304 if (sh_video->last_pts == MP_NOPTS_VALUE)
2305 sh_video->last_pts= sh_video->pts;
2306 else if (sh_video->last_pts >= sh_video->pts) {
2307 sh_video->last_pts = sh_video->pts;
2308 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2310 frame_time = sh_video->pts - sh_video->last_pts;
2311 sh_video->last_pts = sh_video->pts;
2312 sh_video->timer += frame_time;
2313 if(mpctx->sh_audio)
2314 mpctx->delay -= frame_time;
2315 *blit_frame = res > 0;
2317 return frame_time;
2320 static void pause_loop(struct MPContext *mpctx)
2322 mp_cmd_t* cmd;
2323 if (!quiet) {
2324 // Small hack to display the pause message on the OSD line.
2325 // The pause string is: "\n == PAUSE == \r" so we need to
2326 // take the first and the last char out
2327 if (term_osd && !mpctx->sh_video) {
2328 char msg[128] = MSGTR_Paused;
2329 int mlen = strlen(msg);
2330 msg[mlen-1] = '\0';
2331 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2332 update_osd_msg(mpctx);
2333 } else
2334 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2335 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2337 #ifdef HAVE_NEW_GUI
2338 if (use_gui)
2339 guiGetEvent(guiCEvent, (char *)guiSetPause);
2340 #endif
2341 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2342 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2344 if (mpctx->audio_out && mpctx->sh_audio)
2345 mpctx->audio_out->pause(); // pause audio, keep data if possible
2347 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL
2348 || cmd->id == MP_CMD_SET_MOUSE_POS) {
2349 if (cmd) {
2350 cmd = mp_input_get_cmd(0,1,0);
2351 mp_cmd_free(cmd);
2352 continue;
2354 if (mpctx->sh_video && mpctx->video_out)
2355 vo_check_events(mpctx->video_out);
2356 #ifdef HAVE_NEW_GUI
2357 if (use_gui) {
2358 guiEventHandling();
2359 guiGetEvent(guiReDraw, NULL);
2360 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2361 break;
2363 #endif
2364 #ifdef HAVE_MENU
2365 if (vf_menu)
2366 vf_menu_pause_update(vf_menu);
2367 #endif
2368 usec_sleep(20000);
2370 if (cmd && cmd->id == MP_CMD_PAUSE) {
2371 cmd = mp_input_get_cmd(0,1,0);
2372 mp_cmd_free(cmd);
2374 mpctx->osd_function=OSD_PLAY;
2375 if (mpctx->audio_out && mpctx->sh_audio)
2376 mpctx->audio_out->resume(); // resume audio
2377 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2378 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2379 (void)GetRelativeTime(); // ignore time that passed during pause
2380 #ifdef HAVE_NEW_GUI
2381 if (use_gui) {
2382 if (guiIntfStruct.Playing == guiSetStop)
2383 mpctx->eof = 1;
2384 else
2385 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2387 #endif
2390 void print_version(void){
2391 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2393 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2394 GetCpuCaps(&gCpuCaps);
2395 #ifdef ARCH_X86
2396 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2397 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2398 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2399 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2400 #ifdef RUNTIME_CPUDETECT
2401 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2402 #else
2403 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2404 #ifdef HAVE_MMX
2405 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2406 #endif
2407 #ifdef HAVE_MMX2
2408 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2409 #endif
2410 #ifdef HAVE_3DNOW
2411 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2412 #endif
2413 #ifdef HAVE_3DNOWEX
2414 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2415 #endif
2416 #ifdef HAVE_SSE
2417 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2418 #endif
2419 #ifdef HAVE_SSE2
2420 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2421 #endif
2422 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2423 #endif /* RUNTIME_CPUDETECT */
2424 #endif /* ARCH_X86 */
2428 // Find the right mute status and record position for new file position
2429 static void edl_seek_reset(MPContext *mpctx)
2431 mpctx->edl_muted = 0;
2432 next_edl_record = edl_records;
2434 while (next_edl_record) {
2435 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2436 break;
2438 if (next_edl_record->action == EDL_MUTE)
2439 mpctx->edl_muted = !mpctx->edl_muted;
2440 next_edl_record = next_edl_record->next;
2442 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2443 mixer_mute(&mpctx->mixer);
2447 // Execute EDL command for the current position if one exists
2448 static void edl_update(MPContext *mpctx)
2450 if (!next_edl_record)
2451 return;
2453 if (!mpctx->sh_video) {
2454 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2455 free_edl(edl_records);
2456 next_edl_record = NULL;
2457 edl_records = NULL;
2458 return;
2461 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2462 if (next_edl_record->action == EDL_SKIP) {
2463 mpctx->osd_function = OSD_FFW;
2464 mpctx->abs_seek_pos = 0;
2465 mpctx->rel_seek_secs = next_edl_record->length_sec;
2466 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2467 "[%f], length [%f]\n", next_edl_record->start_sec,
2468 next_edl_record->stop_sec, next_edl_record->length_sec);
2469 edl_decision = 1;
2471 else if (next_edl_record->action == EDL_MUTE) {
2472 mpctx->edl_muted = !mpctx->edl_muted;
2473 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2474 mixer_mute(&mpctx->mixer);
2475 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2476 next_edl_record->start_sec );
2478 next_edl_record = next_edl_record->next;
2483 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2484 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2485 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2486 static int seek(MPContext *mpctx, double amount, int style)
2488 current_module = "seek";
2489 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2490 return -1;
2492 if (mpctx->sh_video) {
2493 current_module = "seek_video_reset";
2494 resync_video_stream(mpctx->sh_video);
2495 if (mpctx->video_out->config_ok)
2496 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2497 mpctx->sh_video->num_buffered_pts = 0;
2498 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2499 mpctx->num_buffered_frames = 0;
2500 mpctx->delay = 0;
2501 // Not all demuxers set d_video->pts during seek, so this value
2502 // (which is used by at least vobsub and edl code below) may
2503 // be completely wrong (probably 0).
2504 mpctx->sh_video->pts = mpctx->d_video->pts;
2505 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2506 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2509 if (mpctx->sh_audio) {
2510 current_module = "seek_audio_reset";
2511 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2512 mpctx->sh_audio->a_buffer_len = 0;
2513 mpctx->sh_audio->a_out_buffer_len = 0;
2516 if (vo_vobsub && mpctx->sh_video) {
2517 current_module = "seek_vobsub_reset";
2518 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2521 edl_seek_reset(mpctx);
2523 c_total = 0;
2524 max_pts_correction = 0.1;
2525 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2526 drop_frame_cnt = 0;
2528 current_module = NULL;
2529 return 0;
2533 static void read_keys(void *ctx)
2535 getch2();
2539 int main(int argc,char* argv[]){
2542 char * mem_ptr;
2544 // movie info:
2546 /* Flag indicating whether MPlayer should exit without playing anything. */
2547 int opt_exit = 0;
2549 //float a_frame=0; // Audio
2551 int i;
2553 int gui_no_filename=0;
2555 srand((int) time(NULL));
2557 struct MPContext *mpctx = &(struct MPContext){
2558 .osd_function = OSD_PLAY,
2559 .begin_skip = MP_NOPTS_VALUE,
2560 .play_tree_step = 1,
2561 .global_sub_pos = -1,
2562 .set_of_sub_pos = -1,
2563 .file_format = DEMUXER_TYPE_UNKNOWN,
2564 .last_dvb_step = 1,
2567 InitTimer();
2569 mp_msg_init();
2571 #ifdef HAVE_X11
2572 mpctx->x11_state = talloc_ptrtype(NULL, mpctx->x11_state);
2573 vo_x11_init_state(mpctx->x11_state);
2574 #endif
2575 struct MPOpts *opts = &mpctx->opts;
2576 set_default_mplayer_options(opts);
2577 // Create the config context and register the options
2578 mconfig = m_config_new(opts);
2579 m_config_register_options(mconfig,mplayer_opts);
2580 mp_input_register_options(mconfig);
2582 // Preparse the command line
2583 m_config_preparse_command_line(mconfig,argc,argv);
2585 print_version();
2586 #if defined(WIN32) && defined(USE_WIN32DLL)
2587 set_path_env();
2588 #endif /*WIN32 && USE_WIN32DLL*/
2590 #ifdef USE_TV
2591 stream_tv_defaults.immediate = 1;
2592 #endif
2594 if (argc > 1 && argv[1] &&
2595 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2596 use_gui = !strcmp(argv[1], "-gui");
2597 } else
2598 if ( argv[0] )
2600 char *base = strrchr(argv[0], '/');
2601 if (!base)
2602 base = strrchr(argv[0], '\\');
2603 if (!base)
2604 base = argv[0];
2605 if(strstr(base, "gmplayer"))
2606 use_gui=1;
2609 parse_cfgfiles(mpctx, mconfig);
2611 #ifdef HAVE_NEW_GUI
2612 if ( use_gui ) cfg_read();
2613 #endif
2615 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2616 if(mpctx->playtree == NULL)
2617 opt_exit = 1;
2618 else {
2619 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2620 if(mpctx->playtree) {
2621 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2622 if(mpctx->playtree_iter) {
2623 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2624 play_tree_iter_free(mpctx->playtree_iter);
2625 mpctx->playtree_iter = NULL;
2627 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2632 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2633 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2634 if(runningmplayer && filename && use_gui){
2635 COPYDATASTRUCT csData;
2636 char file[MAX_PATH];
2637 char *filepart = filename;
2638 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2639 csData.dwData = 0;
2640 csData.cbData = strlen(file)*2;
2641 csData.lpData = file;
2642 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2645 #endif
2647 #ifdef WIN32
2648 if(proc_priority){
2649 int i;
2650 for(i=0; priority_presets_defs[i].name; i++){
2651 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2652 break;
2654 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2655 priority_presets_defs[i].name);
2656 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2658 #endif
2659 #ifndef HAVE_NEW_GUI
2660 if(use_gui){
2661 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2662 use_gui=0;
2664 #else
2665 #ifndef WIN32
2666 if(use_gui && !vo_init()){
2667 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2668 use_gui=0;
2670 #endif
2671 if (use_gui && mpctx->playtree_iter){
2672 char cwd[PATH_MAX+2];
2673 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2674 play_tree_iter_free(mpctx->playtree_iter);
2675 mpctx->playtree_iter=NULL;
2677 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2679 strcat(cwd, "/");
2680 // Prefix relative paths with current working directory
2681 play_tree_add_bpf(mpctx->playtree, cwd);
2683 // Import initital playtree into GUI.
2684 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2686 #endif /* HAVE_NEW_GUI */
2688 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2689 list_video_out();
2690 opt_exit = 1;
2693 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2694 list_audio_out();
2695 opt_exit = 1;
2698 /* Check codecs.conf. */
2699 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2700 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2701 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2702 if(!parse_codec_cfg(NULL)){
2703 exit_player_with_rc(mpctx, NULL, 0);
2705 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2708 free( mem_ptr ); // release the buffer created by get_path()
2711 #if 0
2712 if(video_codec_list){
2713 int i;
2714 video_codec=video_codec_list[0];
2715 for(i=0;video_codec_list[i];i++)
2716 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2718 #endif
2719 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2720 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2721 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2722 list_codecs(1);
2723 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2724 opt_exit = 1;
2726 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2727 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2728 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2729 list_codecs(0);
2730 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2731 opt_exit = 1;
2733 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2734 vfm_help();
2735 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2736 opt_exit = 1;
2738 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2739 afm_help();
2740 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2741 opt_exit = 1;
2743 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2744 af_help();
2745 printf("\n");
2746 opt_exit = 1;
2748 #ifdef HAVE_X11
2749 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2750 fstype_help();
2751 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2752 opt_exit = 1;
2754 #endif
2755 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2756 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2757 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2758 demuxer_help();
2759 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2760 opt_exit = 1;
2762 if(list_properties) {
2763 property_print_help();
2764 opt_exit = 1;
2767 if(opt_exit)
2768 exit_player(mpctx, NULL);
2770 if (player_idle_mode && use_gui) {
2771 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2772 exit_player_with_rc(mpctx, NULL, 1);
2775 if(!filename && !player_idle_mode){
2776 if(!use_gui){
2777 // no file/vcd/dvd -> show HELP:
2778 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2779 exit_player_with_rc(mpctx, NULL, 0);
2780 } else gui_no_filename=1;
2783 /* Display what configure line was used */
2784 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2786 // Many users forget to include command line in bugreports...
2787 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2788 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2789 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2790 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2793 //------ load global data first ------
2795 // check font
2796 #ifdef HAVE_FREETYPE
2797 init_freetype();
2798 #endif
2799 #ifdef HAVE_FONTCONFIG
2800 if(font_fontconfig <= 0)
2802 #endif
2803 #ifdef HAVE_BITMAP_FONT
2804 if(font_name){
2805 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2806 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2807 filename_recode(font_name));
2808 } else {
2809 // try default:
2810 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2811 free(mem_ptr); // release the buffer created by get_path()
2812 if(!vo_font)
2813 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2815 if (sub_font_name)
2816 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2817 else
2818 sub_font = vo_font;
2819 #endif
2820 #ifdef HAVE_FONTCONFIG
2822 #endif
2824 vo_init_osd();
2826 #ifdef USE_ASS
2827 ass_library = ass_init();
2828 #endif
2830 #ifdef HAVE_RTC
2831 if(!nortc)
2833 // seteuid(0); /* Can't hurt to try to get root here */
2834 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2835 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2836 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2837 else {
2838 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2840 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2841 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2842 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2843 close (rtc_fd);
2844 rtc_fd = -1;
2845 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2846 /* variable only by the root */
2847 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2848 close (rtc_fd);
2849 rtc_fd = -1;
2850 } else
2851 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2854 #ifdef HAVE_NEW_GUI
2855 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2856 // and now ? -- Pontscho
2857 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2858 #endif
2859 if(rtc_fd<0)
2860 #endif /* HAVE_RTC */
2861 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2862 softsleep?"software":timer_name);
2864 #ifdef USE_TERMCAP
2865 if ( !use_gui ) load_termcap(NULL); // load key-codes
2866 #endif
2868 // ========== Init keyboard FIFO (connection to libvo) ============
2870 // Init input system
2871 current_module = "init_input";
2872 mp_input_init(use_gui);
2873 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2874 if(slave_mode)
2875 mp_input_add_cmd_fd(0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2876 else if(!noconsolecontrols)
2877 mp_input_add_event_fd(0, read_keys, NULL);
2878 // Set the libstream interrupt callback
2879 stream_set_interrupt_callback(mp_input_check_interrupt);
2881 #ifdef HAVE_MENU
2882 if(use_menu) {
2883 if(menu_cfg && menu_init(mpctx, menu_cfg))
2884 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2885 else {
2886 menu_cfg = get_path("menu.conf");
2887 if(menu_init(mpctx, menu_cfg))
2888 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2889 else {
2890 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2891 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2892 else {
2893 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2894 use_menu = 0;
2899 #endif
2901 initialized_flags|=INITIALIZED_INPUT;
2902 current_module = NULL;
2904 /// Catch signals
2905 #ifndef __MINGW32__
2906 signal(SIGCHLD,child_sighandler);
2907 #endif
2909 #ifdef CRASH_DEBUG
2910 prog_path = argv[0];
2911 #endif
2912 //========= Catch terminate signals: ================
2913 // terminate requests:
2914 signal(SIGTERM,exit_sighandler); // kill
2915 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2917 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2919 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2920 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2921 #ifdef ENABLE_SIGHANDLER
2922 // fatal errors:
2923 signal(SIGBUS,exit_sighandler); // bus error
2924 signal(SIGSEGV,exit_sighandler); // segfault
2925 signal(SIGILL,exit_sighandler); // illegal instruction
2926 signal(SIGFPE,exit_sighandler); // floating point exc.
2927 signal(SIGABRT,exit_sighandler); // abort()
2928 #ifdef CRASH_DEBUG
2929 if (crash_debug)
2930 signal(SIGTRAP,exit_sighandler);
2931 #endif
2932 #endif
2934 #ifdef HAVE_NEW_GUI
2935 if(use_gui){
2936 guiInit();
2937 guiGetEvent(guiSetContext, mpctx);
2938 initialized_flags|=INITIALIZED_GUI;
2939 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2941 #endif
2943 // ******************* Now, let's see the per-file stuff ********************
2945 play_next_file:
2947 // init global sub numbers
2948 mpctx->global_sub_size = 0;
2949 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2951 if (filename) {
2952 load_per_protocol_config (mconfig, filename);
2953 load_per_extension_config (mconfig, filename);
2954 load_per_file_config (mconfig, filename);
2957 if (opts->video_driver_list)
2958 load_per_output_config (mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2959 if (opts->audio_driver_list)
2960 load_per_output_config (mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2962 // We must enable getch2 here to be able to interrupt network connection
2963 // or cache filling
2964 if(!noconsolecontrols && !slave_mode){
2965 if(initialized_flags&INITIALIZED_GETCH2)
2966 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2967 else
2968 getch2_enable(); // prepare stdin for hotkeys...
2969 initialized_flags|=INITIALIZED_GETCH2;
2970 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2973 // =================== GUI idle loop (STOP state) ===========================
2974 #ifdef HAVE_NEW_GUI
2975 if ( use_gui ) {
2976 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2977 guiGetEvent( guiSetDefaults,0 );
2978 while ( guiIntfStruct.Playing != 1 )
2980 mp_cmd_t* cmd;
2981 usec_sleep(20000);
2982 guiEventHandling();
2983 guiGetEvent( guiReDraw,NULL );
2984 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2985 guiGetEvent(guiIEvent, (char *)cmd->id);
2986 mp_cmd_free(cmd);
2989 guiGetEvent( guiSetParameters,NULL );
2990 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2992 play_tree_t * entry = play_tree_new();
2993 play_tree_add_file( entry,guiIntfStruct.Filename );
2994 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2995 else mpctx->playtree=play_tree_new();
2996 play_tree_set_child( mpctx->playtree,entry );
2997 if(mpctx->playtree)
2999 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
3000 if(mpctx->playtree_iter)
3002 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3004 play_tree_iter_free(mpctx->playtree_iter);
3005 mpctx->playtree_iter = NULL;
3007 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3012 #endif /* HAVE_NEW_GUI */
3014 while (player_idle_mode && !filename) {
3015 play_tree_t * entry = NULL;
3016 mp_cmd_t * cmd;
3017 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
3018 if (mpctx->video_out)
3019 vo_check_events(mpctx->video_out);
3020 usec_sleep(20000);
3022 switch (cmd->id) {
3023 case MP_CMD_LOADFILE:
3024 // prepare a tree entry with the new filename
3025 entry = play_tree_new();
3026 play_tree_add_file(entry, cmd->args[0].v.s);
3027 // The entry is added to the main playtree after the switch().
3028 break;
3029 case MP_CMD_LOADLIST:
3030 entry = parse_playlist_file(cmd->args[0].v.s);
3031 break;
3032 case MP_CMD_QUIT:
3033 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3034 break;
3035 case MP_CMD_GET_PROPERTY:
3036 case MP_CMD_SET_PROPERTY:
3037 case MP_CMD_STEP_PROPERTY:
3038 run_command(mpctx, cmd);
3039 break;
3042 mp_cmd_free(cmd);
3044 if (entry) { // user entered a command that gave a valid entry
3045 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3046 play_tree_free_list(mpctx->playtree->child, 1);
3047 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3049 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3051 play_tree_set_child(mpctx->playtree, entry);
3053 /* Make iterator start at the top the of tree. */
3054 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
3055 if (!mpctx->playtree_iter) continue;
3057 // find the first real item in the tree
3058 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3059 // no items!
3060 play_tree_iter_free(mpctx->playtree_iter);
3061 mpctx->playtree_iter = NULL;
3062 continue; // wait for next command
3064 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3067 //---------------------------------------------------------------------------
3069 if(filename)
3070 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3071 filename_recode(filename));
3073 if (edl_filename) {
3074 if (edl_records) free_edl(edl_records);
3075 next_edl_record = edl_records = edl_parse_file();
3077 if (edl_output_filename) {
3078 if (edl_fd) fclose(edl_fd);
3079 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3081 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3082 filename_recode(edl_output_filename));
3086 //==================== Open VOB-Sub ============================
3088 current_module="vobsub";
3089 if (vobsub_name){
3090 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3091 if(vo_vobsub==NULL)
3092 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3093 filename_recode(vobsub_name));
3094 } else if (sub_auto && filename){
3095 /* try to autodetect vobsub from movie filename ::atmos */
3096 char *buf = strdup(filename), *psub;
3097 char *pdot = strrchr(buf, '.');
3098 char *pslash = strrchr(buf, '/');
3099 #ifdef WIN32
3100 if (!pslash) pslash = strrchr(buf, '\\');
3101 #endif
3102 if (pdot && (!pslash || pdot > pslash))
3103 *pdot = '\0';
3104 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3105 /* try from ~/.mplayer/sub */
3106 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3107 char *bname;
3108 int l;
3109 bname = strrchr(buf,'/');
3110 #ifdef WIN32
3111 if(!bname) bname = strrchr(buf,'\\');
3112 #endif
3113 if(bname) bname++;
3114 else bname = buf;
3115 l = strlen(psub) + strlen(bname) + 1;
3116 psub = realloc(psub,l);
3117 strcat(psub,bname);
3118 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3119 free(psub);
3121 free(buf);
3123 if(vo_vobsub){
3124 initialized_flags|=INITIALIZED_VOBSUB;
3125 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3126 // check if vobsub requested only to display forced subtitles
3127 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
3129 // setup global sub numbering
3130 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3131 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3134 //============ Open & Sync STREAM --- fork cache2 ====================
3136 mpctx->stream=NULL;
3137 mpctx->demuxer=NULL;
3138 if (mpctx->d_audio) {
3139 //free_demuxer_stream(mpctx->d_audio);
3140 mpctx->d_audio=NULL;
3142 if (mpctx->d_video) {
3143 //free_demuxer_stream(d_video);
3144 mpctx->d_video=NULL;
3146 mpctx->sh_audio=NULL;
3147 mpctx->sh_video=NULL;
3149 current_module="open_stream";
3150 mpctx->stream = open_stream(filename, opts, &mpctx->file_format);
3151 if(!mpctx->stream) { // error...
3152 mpctx->eof = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3153 goto goto_next_file;
3155 initialized_flags|=INITIALIZED_STREAM;
3157 #ifdef HAVE_NEW_GUI
3158 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3159 #endif
3161 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3162 play_tree_t* entry;
3163 // Handle playlist
3164 current_module="handle_playlist";
3165 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3166 filename_recode(filename));
3167 entry = parse_playtree(mpctx->stream,0);
3168 mpctx->eof=playtree_add_playlist(mpctx, entry);
3169 goto goto_next_file;
3171 mpctx->stream->start_pos+=seek_to_byte;
3173 if(stream_dump_type==5){
3174 unsigned char buf[4096];
3175 int len;
3176 FILE *f;
3177 current_module="dumpstream";
3178 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3179 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3180 exit_player(mpctx, MSGTR_Exit_error);
3182 stream_reset(mpctx->stream);
3183 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3184 f=fopen(stream_dump_name,"wb");
3185 if(!f){
3186 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3187 exit_player(mpctx, MSGTR_Exit_error);
3189 while(!mpctx->stream->eof){
3190 len=stream_read(mpctx->stream,buf,4096);
3191 if(len>0) {
3192 if(fwrite(buf,len,1,f) != 1) {
3193 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3194 exit_player(mpctx, MSGTR_Exit_error);
3198 if(fclose(f)) {
3199 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3200 exit_player(mpctx, MSGTR_Exit_error);
3202 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3203 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3206 #ifdef USE_DVDREAD
3207 if(mpctx->stream->type==STREAMTYPE_DVD){
3208 current_module="dvd lang->id";
3209 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3210 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3211 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3212 // setup global sub numbering
3213 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3214 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3215 current_module=NULL;
3217 #endif
3219 #ifdef USE_DVDNAV
3220 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3221 current_module="dvdnav lang->id";
3222 if(opts->audio_id==-1) opts->audio_id=dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3223 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3224 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3225 // setup global sub numbering
3226 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3227 mpctx->global_sub_size += dvdnav_number_of_subs(mpctx->stream);
3228 current_module=NULL;
3230 #endif
3232 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3233 goto_enable_cache:
3234 if(stream_cache_size>0){
3235 current_module="enable_cache";
3236 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3237 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3238 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3239 if((mpctx->eof = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3242 //============ Open DEMUXERS --- DETECT file type =======================
3243 current_module="demux_open";
3245 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,filename);
3247 // HACK to get MOV Reference Files working
3249 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3251 unsigned char* playlist_entry;
3252 play_tree_t *list = NULL, *entry = NULL;
3254 current_module="handle_demux_playlist";
3255 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3257 char *temp, *bname;
3259 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3260 filename_recode(playlist_entry));
3262 bname=mp_basename(playlist_entry);
3263 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3264 continue;
3266 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
3267 continue;
3269 entry = play_tree_new();
3271 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3273 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
3274 if (temp)
3276 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
3277 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
3278 strcat(temp, playlist_entry);
3279 play_tree_add_file(entry,temp);
3280 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3281 free(temp);
3284 else
3285 play_tree_add_file(entry,playlist_entry);
3287 if(!list)
3288 list = entry;
3289 else
3290 play_tree_append_entry(list,entry);
3292 free_demuxer(mpctx->demuxer);
3293 mpctx->demuxer = NULL;
3295 if (list)
3297 entry = play_tree_new();
3298 play_tree_set_child(entry,list);
3299 mpctx->eof=playtree_add_playlist(mpctx, entry);
3300 goto goto_next_file;
3304 if(!mpctx->demuxer)
3305 goto goto_next_file;
3306 if(dvd_chapter>1) {
3307 float pts;
3308 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3309 seek(mpctx, pts, SEEK_ABSOLUTE);
3312 initialized_flags|=INITIALIZED_DEMUXER;
3314 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3315 int i;
3316 int maxid = -1;
3317 // setup global sub numbering
3318 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3319 for (i = 0; i < MAX_S_STREAMS; i++)
3320 if (mpctx->demuxer->s_streams[i])
3321 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3322 mpctx->global_sub_size += maxid + 1;
3324 // Make opts->sub_id always selectable if set.
3325 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3326 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3328 #ifdef USE_ASS
3329 if (ass_enabled && ass_library) {
3330 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3331 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3332 if (extract_embedded_fonts &&
3333 att->name && att->type && att->data && att->data_size &&
3334 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3335 strcmp(att->type, "application/x-font") == 0))
3336 ass_add_font(ass_library, att->name, att->data, att->data_size);
3339 #endif
3341 current_module="demux_open2";
3343 //file_format=demuxer->file_format;
3345 mpctx->d_audio=mpctx->demuxer->audio;
3346 mpctx->d_video=mpctx->demuxer->video;
3347 mpctx->d_sub=mpctx->demuxer->sub;
3349 // select audio stream
3350 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3352 // DUMP STREAMS:
3353 if((stream_dump_type)&&(stream_dump_type<4)){
3354 FILE *f;
3355 demux_stream_t *ds=NULL;
3356 current_module="dump";
3357 // select stream to dump
3358 switch(stream_dump_type){
3359 case 1: ds=mpctx->d_audio;break;
3360 case 2: ds=mpctx->d_video;break;
3361 case 3: ds=mpctx->d_sub;break;
3363 if(!ds){
3364 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3365 exit_player(mpctx, MSGTR_Exit_error);
3367 // disable other streams:
3368 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3369 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3370 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3371 // let's dump it!
3372 f=fopen(stream_dump_name,"wb");
3373 if(!f){
3374 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3375 exit_player(mpctx, MSGTR_Exit_error);
3377 while(!ds->eof){
3378 unsigned char* start;
3379 int in_size=ds_get_packet(ds,&start);
3380 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3381 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3382 if(in_size>0) fwrite(start,in_size,1,f);
3384 fclose(f);
3385 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3386 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3389 mpctx->sh_audio=mpctx->d_audio->sh;
3390 mpctx->sh_video=mpctx->d_video->sh;
3392 if(mpctx->sh_video){
3394 current_module="video_read_properties";
3395 if(!video_read_properties(mpctx->sh_video)) {
3396 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3397 mpctx->sh_video=mpctx->d_video->sh=NULL;
3398 } else {
3399 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
3400 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3401 mpctx->sh_video->fps,mpctx->sh_video->frametime
3404 /* need to set fps here for output encoders to pick it up in their init */
3405 if(force_fps){
3406 mpctx->sh_video->fps=force_fps;
3407 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3409 vo_fps = mpctx->sh_video->fps;
3411 if(!mpctx->sh_video->fps && !force_fps){
3412 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3413 mpctx->sh_video=mpctx->d_video->sh=NULL;
3419 if(!mpctx->sh_video && !mpctx->sh_audio){
3420 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3421 #ifdef HAS_DVBIN_SUPPORT
3422 if(mpctx->stream->type == STREAMTYPE_DVB)
3424 int dir;
3425 int v = mpctx->last_dvb_step;
3426 if(v > 0)
3427 dir = DVB_CHANNEL_HIGHER;
3428 else
3429 dir = DVB_CHANNEL_LOWER;
3431 if(dvb_step_channel(mpctx->stream, dir))
3432 mpctx->eof = mpctx->dvbin_reopen = 1;
3434 #endif
3435 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3438 /* display clip info */
3439 demux_info_print(mpctx->demuxer);
3441 //================== Read SUBTITLES (DVD & TEXT) ==========================
3442 if(vo_spudec==NULL && mpctx->sh_video &&
3443 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3444 init_vo_spudec(mpctx);
3447 // Apply current settings for forced subs
3448 if (vo_spudec!=NULL)
3449 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3451 if(mpctx->sh_video) {
3452 // after reading video params we should load subtitles because
3453 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3454 // check .sub
3455 current_module="read_subtitles_file";
3456 if(sub_name){
3457 for (i = 0; sub_name[i] != NULL; ++i)
3458 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3460 if(sub_auto) { // auto load sub file ...
3461 char *psub = get_path( "sub/" );
3462 char **tmp = sub_filenames((psub ? psub : ""), filename);
3463 int i = 0;
3464 free(psub); // release the buffer created by get_path() above
3465 while (tmp[i]) {
3466 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3467 free(tmp[i++]);
3469 free(tmp);
3471 if (mpctx->set_of_sub_size > 0) {
3472 // setup global sub numbering
3473 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3474 mpctx->global_sub_size += mpctx->set_of_sub_size;
3478 if (mpctx->global_sub_size) {
3479 // find the best sub to use
3480 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3481 if (opts->sub_id < 0 && dvdsub_lang)
3482 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3483 if (opts->sub_id < 0)
3484 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3485 if (vobsub_index_id >= 0) {
3486 // if user asks for a vobsub id, use that first.
3487 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3488 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3489 // if user asks for a dvd sub id, use that next.
3490 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3491 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3492 // if there are text subs to use, use those. (autosubs come last here)
3493 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3495 } else if (mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3496 // if nothing else works, get subs from the demuxer.
3497 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX];
3499 } else {
3500 // nothing worth doing automatically.
3501 mpctx->global_sub_pos = -1;
3503 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3504 mpctx->global_sub_pos--;
3505 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3506 if(subdata)
3507 switch (stream_dump_type) {
3508 case 3: list_sub_file(subdata); break;
3509 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3510 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3511 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3512 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3513 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3517 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3518 filename_recode(filename));
3519 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3520 if (mpctx->sh_video) {
3521 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3522 if (mpctx->sh_video->format >= 0x20202020)
3523 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3524 else
3525 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3526 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3527 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3528 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3529 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3530 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3532 if (mpctx->sh_audio) {
3533 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3534 if (mpctx->sh_audio->format >= 0x20202020)
3535 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3536 else
3537 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3539 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3540 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3542 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3543 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3545 if(!mpctx->sh_video) goto main; // audio-only
3547 if(!reinit_video_chain(mpctx)) {
3548 if(!mpctx->sh_video){
3549 if(!mpctx->sh_audio) goto goto_next_file;
3550 goto main; // exit_player(MSGTR_Exit_error);
3554 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3555 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3557 #ifdef HAVE_FREETYPE
3558 force_load_font = 1;
3559 #endif
3561 //================== MAIN: ==========================
3562 main:
3563 current_module="main";
3565 if(playing_msg) {
3566 char* msg = property_expand_string(mpctx, playing_msg);
3567 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3568 free(msg);
3572 // Disable the term OSD in verbose mode
3573 if(verbose) term_osd = 0;
3576 //int frame_corr_num=0; //
3577 //float v_frame=0; // Video
3578 float time_frame=0; // Timer
3579 //float num_frames=0; // number of frames played
3581 int frame_time_remaining=0; // flag
3582 int blit_frame=0;
3583 mpctx->num_buffered_frames=0;
3585 // Make sure old OSD does not stay around,
3586 // e.g. with -fixed-vo and same-resolution files
3587 clear_osd_msgs();
3588 update_osd_msg(mpctx);
3590 //================ SETUP AUDIO ==========================
3592 if(mpctx->sh_audio){
3593 reinit_audio_chain(mpctx);
3594 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3595 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3598 current_module="av_init";
3600 if(mpctx->sh_video){
3601 mpctx->sh_video->timer=0;
3602 if (! ignore_start)
3603 audio_delay += mpctx->sh_video->stream_delay;
3605 if(mpctx->sh_audio){
3606 if (! ignore_start)
3607 audio_delay -= mpctx->sh_audio->stream_delay;
3608 mpctx->delay=-audio_delay;
3611 if(!mpctx->sh_audio){
3612 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3613 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3614 ds_free_packs(mpctx->d_audio); // free buffered chunks
3615 //mpctx->d_audio->id=-2; // do not read audio chunks
3616 //uninit_player(mpctx, INITIALIZED_AO); // close device
3618 if(!mpctx->sh_video){
3619 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3620 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3621 ds_free_packs(mpctx->d_video);
3622 mpctx->d_video->id=-2;
3623 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3626 if (!mpctx->sh_video && !mpctx->sh_audio)
3627 goto goto_next_file;
3629 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3630 if(force_fps && mpctx->sh_video){
3631 vo_fps = mpctx->sh_video->fps=force_fps;
3632 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3633 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3636 #ifdef HAVE_NEW_GUI
3637 if ( use_gui ) {
3638 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3639 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3640 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3641 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3642 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3644 #endif
3646 mp_input_set_section(NULL);
3647 //TODO: add desired (stream-based) sections here
3648 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3650 //==================== START PLAYING =======================
3652 if(opts->loop_times>1) opts->loop_times--; else
3653 if(opts->loop_times==1) opts->loop_times = -1;
3655 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3657 total_time_usage_start=GetTimer();
3658 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3659 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3660 play_n_frames=play_n_frames_mf;
3662 if(play_n_frames==0){
3663 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3666 if (seek_to_sec) {
3667 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3668 end_at.pos += seek_to_sec;
3671 if (end_at.type == END_AT_SIZE) {
3672 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3673 end_at.type = END_AT_NONE;
3676 #ifdef USE_DVDNAV
3677 mp_dvdnav_context_free(mpctx);
3678 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3679 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3680 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3682 #endif
3684 while(!mpctx->eof){
3685 float aq_sleep_time=0;
3687 if(dvd_last_chapter>0) {
3688 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3689 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3690 goto goto_next_file;
3693 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3694 mpctx->sh_audio = mpctx->d_audio->sh;
3695 mpctx->sh_audio->ds = mpctx->d_audio;
3696 reinit_audio_chain(mpctx);
3699 /*========================== PLAY AUDIO ============================*/
3701 if (mpctx->sh_audio)
3702 if (!fill_audio_out_buffers(mpctx))
3703 // at eof, all audio at least written to ao
3704 if (!mpctx->sh_video)
3705 mpctx->eof = PT_NEXT_ENTRY;
3708 if(!mpctx->sh_video) {
3709 // handle audio-only case:
3710 double a_pos=0;
3711 if(!quiet || end_at.type == END_AT_TIME )
3712 a_pos = playing_audio_pts(mpctx);
3714 if(!quiet)
3715 print_status(mpctx, a_pos, 0, 0);
3717 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3718 mpctx->eof = PT_NEXT_ENTRY;
3719 update_osd_msg(mpctx);
3721 } else {
3723 /*========================== PLAY VIDEO ============================*/
3725 vo_pts=mpctx->sh_video->timer*90000.0;
3726 vo_fps=mpctx->sh_video->fps;
3728 if (!mpctx->num_buffered_frames) {
3729 double frame_time = update_video(mpctx, &blit_frame);
3730 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3731 if (mpctx->sh_video->vf_initialized < 0) {
3732 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3733 mpctx->eof = 1; goto goto_next_file;
3735 if (frame_time < 0)
3736 mpctx->eof = 1;
3737 else {
3738 // might return with !eof && !blit_frame if !correct_pts
3739 mpctx->num_buffered_frames += blit_frame;
3740 time_frame += frame_time / opts->playback_speed; // for nosound
3744 // ==========================================================================
3746 // current_module="draw_osd";
3747 // if(vo_config_count) mpctx->video_out->draw_osd();
3749 #ifdef HAVE_NEW_GUI
3750 if(use_gui) guiEventHandling();
3751 #endif
3753 current_module="vo_check_events";
3754 vo_check_events(mpctx->video_out);
3756 #ifdef HAVE_X11
3757 if (stop_xscreensaver) {
3758 current_module = "stop_xscreensaver";
3759 xscreensaver_heartbeat(mpctx->x11_state);
3761 #endif
3762 if (heartbeat_cmd) {
3763 static unsigned last_heartbeat;
3764 unsigned now = GetTimerMS();
3765 if (now - last_heartbeat > 30000) {
3766 last_heartbeat = now;
3767 system(heartbeat_cmd);
3771 frame_time_remaining = sleep_until_update(mpctx, &time_frame, &aq_sleep_time);
3773 //====================== FLIP PAGE (VIDEO BLT): =========================
3775 current_module="flip_page";
3776 if (!frame_time_remaining && blit_frame) {
3777 unsigned int t2=GetTimer();
3779 vo_flip_page(mpctx->video_out);
3780 mpctx->num_buffered_frames--;
3782 vout_time_usage += (GetTimer() - t2) * 0.000001;
3784 //====================== A-V TIMESTAMP CORRECTION: =========================
3786 adjust_sync_and_print_status(mpctx, frame_time_remaining, time_frame);
3788 //============================ Auto QUALITY ============================
3790 /*Output quality adjustments:*/
3791 if(auto_quality>0){
3792 current_module="autoq";
3793 // float total=0.000001f * (GetTimer()-aq_total_time);
3794 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3795 if(output_quality<auto_quality && aq_sleep_time>0)
3796 ++output_quality;
3797 else
3798 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3799 if(output_quality>1 && aq_sleep_time<0)
3800 --output_quality;
3801 else
3802 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3803 output_quality=0;
3804 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3805 set_video_quality(mpctx->sh_video,output_quality);
3808 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3809 --play_n_frames;
3810 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3814 // FIXME: add size based support for -endpos
3815 if (end_at.type == END_AT_TIME &&
3816 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3817 mpctx->eof = PT_NEXT_ENTRY;
3819 } // end if(mpctx->sh_video)
3821 #ifdef USE_DVDNAV
3822 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3823 nav_highlight_t hl;
3824 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3825 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3826 vo_osd_changed (OSDTYPE_DVDNAV);
3828 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3829 double ar = -1.0;
3830 if (stream_control (mpctx->demuxer->stream,
3831 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3832 != STREAM_UNSUPPORTED)
3833 mpctx->sh_video->stream_aspect = ar;
3836 #endif
3838 //============================ Handle PAUSE ===============================
3840 current_module="pause";
3842 if (mpctx->osd_function == OSD_PAUSE) {
3843 pause_loop(mpctx);
3844 mpctx->was_paused = 1;
3847 // handle -sstep
3848 if(step_sec>0) {
3849 mpctx->osd_function=OSD_FFW;
3850 mpctx->rel_seek_secs+=step_sec;
3853 edl_update(mpctx);
3855 //================= Keyboard events, SEEKing ====================
3857 current_module="key_events";
3860 mp_cmd_t* cmd;
3861 int brk_cmd = 0;
3862 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3863 brk_cmd = run_command(mpctx, cmd);
3864 mp_cmd_free(cmd);
3865 if (brk_cmd == 2)
3866 goto goto_enable_cache;
3869 mpctx->was_paused = 0;
3871 /* Looping. */
3872 if(mpctx->eof==1 && opts->loop_times>=0) {
3873 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", opts->loop_times,mpctx->eof);
3875 if(opts->loop_times>1) opts->loop_times--; else
3876 if(opts->loop_times==1) opts->loop_times=-1;
3877 play_n_frames=play_n_frames_mf;
3878 mpctx->eof=0;
3879 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3880 loop_seek = 1;
3883 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3884 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3885 // Set OSD:
3886 if(!loop_seek){
3887 if( !edl_decision )
3888 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3892 mpctx->rel_seek_secs=0;
3893 mpctx->abs_seek_pos=0;
3894 loop_seek=0;
3895 edl_decision = 0;
3898 #ifdef HAVE_NEW_GUI
3899 if(use_gui){
3900 guiEventHandling();
3901 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3902 // get pos from frame number / total frames
3903 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3904 } else {
3905 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3907 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3908 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3909 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3910 guiGetEvent( guiReDraw,NULL );
3911 guiGetEvent( guiSetVolume,NULL );
3912 if(guiIntfStruct.Playing==0) break; // STOP
3913 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3914 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3915 #ifdef USE_DVDREAD
3916 if ( mpctx->stream->type == STREAMTYPE_DVD )
3918 dvd_priv_t * dvdp = mpctx->stream->priv;
3919 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3921 #endif
3923 #endif /* HAVE_NEW_GUI */
3925 } // while(!mpctx->eof)
3927 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3929 #ifdef HAS_DVBIN_SUPPORT
3930 if(mpctx->dvbin_reopen)
3932 mpctx->eof = 0;
3933 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_INPUT|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3934 cache_uninit(mpctx->stream);
3935 mpctx->dvbin_reopen = 0;
3936 goto goto_enable_cache;
3938 #endif
3941 goto_next_file: // don't jump here after ao/vo/getch initialization!
3943 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3945 if(benchmark){
3946 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3947 double total_time_usage;
3948 total_time_usage_start=GetTimer()-total_time_usage_start;
3949 total_time_usage = (float)total_time_usage_start*0.000001;
3950 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3951 video_time_usage,vout_time_usage,audio_time_usage,
3952 total_time_usage-tot,total_time_usage);
3953 if(total_time_usage>0.0)
3954 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3955 100.0*video_time_usage/total_time_usage,
3956 100.0*vout_time_usage/total_time_usage,
3957 100.0*audio_time_usage/total_time_usage,
3958 100.0*(total_time_usage-tot)/total_time_usage,
3959 100.0);
3960 if(total_frame_cnt && frame_dropping)
3961 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3962 total_frame_cnt-drop_frame_cnt,
3963 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3964 drop_frame_cnt,
3965 100*drop_frame_cnt/total_frame_cnt,
3966 total_frame_cnt,
3967 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3970 // time to uninit all, except global stuff:
3971 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+INITIALIZED_INPUT+(opts->fixed_vo?INITIALIZED_VO:0)));
3973 if(mpctx->set_of_sub_size > 0) {
3974 current_module="sub_free";
3975 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
3976 sub_free(mpctx->set_of_subtitles[i]);
3977 #ifdef USE_ASS
3978 if(mpctx->set_of_ass_tracks[i])
3979 ass_free_track( mpctx->set_of_ass_tracks[i] );
3980 #endif
3982 mpctx->set_of_sub_size = 0;
3984 vo_sub_last = vo_sub=NULL;
3985 subdata=NULL;
3986 #ifdef USE_ASS
3987 ass_track = NULL;
3988 if(ass_library)
3989 ass_clear_fonts(ass_library);
3990 #endif
3992 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
3993 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
3994 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
3995 mpctx->eof = 1;
3996 } else {
3997 play_tree_iter_free(mpctx->playtree_iter);
3998 mpctx->playtree_iter = NULL;
4000 mpctx->play_tree_step = 1;
4001 } else if(mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
4002 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
4003 if(mpctx->playtree_iter) {
4004 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
4005 mpctx->eof = 1;
4006 } else {
4007 play_tree_iter_free(mpctx->playtree_iter);
4008 mpctx->playtree_iter = NULL;
4011 } else { // NEXT PREV SRC
4012 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
4015 if(mpctx->eof == 0) mpctx->eof = 1;
4017 while(mpctx->playtree_iter != NULL) {
4018 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
4019 if(filename == NULL) {
4020 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
4021 play_tree_iter_free(mpctx->playtree_iter);
4022 mpctx->playtree_iter = NULL;
4024 } else
4025 break;
4028 #ifdef HAVE_NEW_GUI
4029 if(use_gui && !mpctx->playtree_iter) {
4030 #ifdef USE_DVDREAD
4031 if(!guiIntfStruct.DiskChanged)
4032 #endif
4033 mplEnd();
4035 #endif
4037 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4038 if(!mpctx->playtree_iter) filename = NULL;
4039 mpctx->eof = 0;
4040 goto play_next_file;
4044 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
4046 return 1;