Fix crash during early exit
[mplayer.git] / mplayer.c
blobeb50e37ea2bc082bf8805a18db331c2bc3ed1126
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 //**************************************************************************//
138 // Config file
139 //**************************************************************************//
141 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
143 #include "get_path.h"
145 //**************************************************************************//
146 // XScreensaver
147 //**************************************************************************//
149 void xscreensaver_heartbeat(struct vo_x11_state *);
151 //**************************************************************************//
152 //**************************************************************************//
153 // Input media streaming & demultiplexer:
154 //**************************************************************************//
156 static int max_framesize=0;
158 #include "stream/stream.h"
159 #include "libmpdemux/demuxer.h"
160 #include "libmpdemux/stheader.h"
162 #ifdef USE_DVDREAD
163 #include "stream/stream_dvd.h"
164 #endif
166 #ifdef USE_DVDNAV
167 #include "stream/stream_dvdnav.h"
168 #endif
170 #include "libmpcodecs/dec_audio.h"
171 #include "libmpcodecs/dec_video.h"
172 #include "libmpcodecs/mp_image.h"
173 #include "libmpcodecs/vf.h"
174 #include "libmpcodecs/vd.h"
176 #include "mixer.h"
178 #include "mp_core.h"
179 #include "options.h"
180 #include "defaultopts.h"
182 //**************************************************************************//
183 //**************************************************************************//
185 // Common FIFO functions, and keyboard/event FIFO code
186 #include "mp_fifo.h"
187 int noconsolecontrols=0;
188 //**************************************************************************//
190 // benchmark:
191 double video_time_usage=0;
192 double vout_time_usage=0;
193 static double audio_time_usage=0;
194 static int total_time_usage_start=0;
195 static int total_frame_cnt=0;
196 static int drop_frame_cnt=0; // total number of dropped frames
197 int benchmark=0;
199 // options:
200 int auto_quality=0;
201 static int output_quality=0;
203 int use_gui=0;
205 #ifdef HAVE_NEW_GUI
206 int enqueue=0;
207 #endif
209 static int list_properties = 0;
211 int osd_level=1;
212 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
213 unsigned int osd_visible;
214 int osd_duration = 1000;
216 int term_osd = 1;
217 static char* term_osd_esc = "\x1b[A\r\x1b[K";
218 static char* playing_msg = NULL;
219 // seek:
220 static double seek_to_sec;
221 static off_t seek_to_byte=0;
222 static off_t step_sec=0;
223 static int loop_seek=0;
225 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
227 // A/V sync:
228 int autosync=0; // 30 might be a good default value.
230 // codecs:
231 char **audio_codec_list=NULL; // override audio codec
232 char **video_codec_list=NULL; // override video codec
233 char **audio_fm_list=NULL; // override audio codec family
234 char **video_fm_list=NULL; // override video codec family
236 // demuxer:
237 extern char *demuxer_name; // override demuxer
238 extern char *audio_demuxer_name; // override audio demuxer
239 extern char *sub_demuxer_name; // override sub demuxer
241 int vobsub_id=-1;
242 char* audio_lang=NULL;
243 char* dvdsub_lang=NULL;
244 static char* spudec_ifo=NULL;
245 int forced_subs_only=0;
246 int file_filter=1;
248 // cache2:
249 int stream_cache_size=-1;
250 #ifdef USE_STREAM_CACHE
251 extern int cache_fill_status;
253 float stream_cache_min_percent=20.0;
254 float stream_cache_seek_min_percent=50.0;
255 #else
256 #define cache_fill_status 0
257 #endif
259 // dump:
260 static char *stream_dump_name="stream.dump";
261 int stream_dump_type=0;
263 // A-V sync:
264 static float default_max_pts_correction=-1;//0.01f;
265 static float max_pts_correction=0;//default_max_pts_correction;
266 static float c_total=0;
267 float audio_delay=0;
268 static int ignore_start=0;
270 static int softsleep=0;
272 double force_fps=0;
273 static int force_srate=0;
274 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
275 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
276 static int play_n_frames=-1;
277 static int play_n_frames_mf=-1;
279 // sub:
280 char *font_name=NULL;
281 char *sub_font_name=NULL;
282 extern int font_fontconfig;
283 float font_factor=0.75;
284 char **sub_name=NULL;
285 float sub_delay=0;
286 float sub_fps=0;
287 int sub_auto = 1;
288 char *vobsub_name=NULL;
289 /*DSP!!char *dsp=NULL;*/
290 int subcc_enabled=0;
291 int suboverlap_enabled = 1;
293 #ifdef USE_ASS
294 #include "libass/ass.h"
295 #include "libass/ass_mp.h"
296 #endif
298 char* current_module=NULL; // for debugging
301 // ---
303 #ifdef HAVE_MENU
304 #include "m_struct.h"
305 #include "libmenu/menu.h"
306 extern void vf_menu_pause_update(struct vf_instance* vf);
307 extern vf_info_t vf_info_menu;
308 static const vf_info_t* const libmenu_vfs[] = {
309 &vf_info_menu,
310 NULL
312 static vf_instance_t* vf_menu = NULL;
313 int use_menu = 0;
314 static char* menu_cfg = NULL;
315 static char* menu_root = "main";
316 #endif
319 #ifdef HAVE_RTC
320 static int nortc = 1;
321 static char* rtc_device;
322 #endif
324 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
325 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
326 short edl_decision = 0; ///< 1 when an EDL operation has been made.
327 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
328 int use_filedir_conf;
330 #include "mpcommon.h"
331 #include "command.h"
333 #include "metadata.h"
335 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
337 const void *mpctx_get_video_out(MPContext *mpctx)
339 return mpctx->video_out;
342 const void *mpctx_get_audio_out(MPContext *mpctx)
344 return mpctx->audio_out;
347 void *mpctx_get_demuxer(MPContext *mpctx)
349 return mpctx->demuxer;
352 void *mpctx_get_playtree_iter(MPContext *mpctx)
354 return mpctx->playtree_iter;
357 void *mpctx_get_mixer(MPContext *mpctx)
359 return &mpctx->mixer;
362 int mpctx_get_global_sub_size(MPContext *mpctx)
364 return mpctx->global_sub_size;
367 int mpctx_get_osd_function(MPContext *mpctx)
369 return mpctx->osd_function;
372 static float get_relative_time(struct MPContext *mpctx)
374 unsigned int new_time = GetTimer();
375 unsigned int delta = new_time - mpctx->last_time;
376 mpctx->last_time = new_time;
377 return delta * 0.000001;
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 (mpctx->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=mpctx->initialized_flags&mask;
558 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
560 if(mask&INITIALIZED_ACODEC){
561 mpctx->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 mpctx->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 mpctx->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 mpctx->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 mpctx->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 mpctx->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 mpctx->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 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
627 current_module="uninit_spudec";
628 spudec_free(vo_spudec);
629 vo_spudec=NULL;
632 if(mask&INITIALIZED_AO){
633 mpctx->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 mpctx->initialized_flags&=~INITIALIZED_GUI;
642 current_module="uninit_gui";
643 guiDone();
645 #endif
647 current_module=NULL;
650 void exit_player_with_rc(struct MPContext *mpctx, const char* how, int rc){
652 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
653 uninit_player(mpctx, INITIALIZED_ALL);
654 #ifdef WIN32
655 timeEndPeriod(1);
656 #endif
657 #ifdef HAVE_X11
658 #ifdef HAVE_NEW_GUI
659 if ( !use_gui )
660 #endif
661 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
662 #endif
664 current_module="uninit_input";
665 mp_input_uninit(mpctx->input);
666 #ifdef HAVE_MENU
667 if (use_menu)
668 menu_uninit();
669 #endif
671 #ifdef HAVE_FREETYPE
672 current_module="uninit_font";
673 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
674 free_font_desc(mpctx->osd->sub_font);
675 free_font_desc(vo_font);
676 vo_font = NULL;
677 done_freetype();
678 #endif
679 osd_free(mpctx->osd);
681 #ifdef USE_ASS
682 ass_library_done(ass_library);
683 #endif
685 current_module="exit_player";
687 // free mplayer config
688 if(mpctx->mconfig)
689 m_config_free(mpctx->mconfig);
691 if(mpctx->playtree)
692 play_tree_free(mpctx->playtree, 1);
694 talloc_free(mpctx->key_fifo);
696 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
697 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
698 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
700 exit(rc);
703 static void exit_player(struct MPContext *mpctx, const char* how)
705 exit_player_with_rc(mpctx, how, 1);
708 #ifndef __MINGW32__
709 static void child_sighandler(int x){
710 pid_t pid;
711 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
713 #endif
715 #ifdef CRASH_DEBUG
716 static char *prog_path;
717 static int crash_debug = 0;
718 #endif
720 static void exit_sighandler(int x){
721 static int sig_count=0;
722 #ifdef CRASH_DEBUG
723 if (!crash_debug || x != SIGTRAP)
724 #endif
725 ++sig_count;
726 if(sig_count==5)
728 /* We're crashing bad and can't uninit cleanly :(
729 * by popular request, we make one last (dirty)
730 * effort to restore the user's
731 * terminal. */
732 getch2_disable();
733 exit(1);
735 if(sig_count==6) exit(1);
736 if(sig_count>6){
737 // can't stop :(
738 #ifndef __MINGW32__
739 kill(getpid(),SIGKILL);
740 #endif
742 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
743 current_module?current_module:"unknown"
745 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
746 if(sig_count<=1)
747 switch(x){
748 case SIGINT:
749 case SIGQUIT:
750 case SIGTERM:
751 case SIGKILL:
752 async_quit_request = 1;
753 return; // killed from keyboard (^C) or killed [-9]
754 case SIGILL:
755 #ifdef RUNTIME_CPUDETECT
756 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
757 #else
758 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
759 #endif
760 case SIGFPE:
761 case SIGSEGV:
762 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
763 default:
764 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
765 #ifdef 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 int cfg_include(m_option_t *conf, char *filename)
796 return m_config_parse_config_file(conf->priv, filename);
799 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
801 char *conffile;
802 int conffile_fd;
803 if (!disable_system_conf &&
804 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
805 exit_player(mpctx, NULL);
806 if ((conffile = get_path("")) == NULL) {
807 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
808 } else {
809 #ifdef __MINGW32__
810 mkdir(conffile);
811 #else
812 mkdir(conffile, 0777);
813 #endif
814 free(conffile);
815 if ((conffile = get_path("config")) == NULL) {
816 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
817 } else {
818 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
819 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
820 write(conffile_fd, default_config, strlen(default_config));
821 close(conffile_fd);
823 if (!disable_user_conf &&
824 m_config_parse_config_file(conf, conffile) < 0)
825 exit_player(mpctx, NULL);
826 free(conffile);
831 #define PROFILE_CFG_PROTOCOL "protocol."
833 static void load_per_protocol_config (m_config_t* conf, const char *const file)
835 char *str;
836 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
837 m_profile_t *p;
839 /* does filename actually uses a protocol ? */
840 str = strstr (file, "://");
841 if (!str)
842 return;
844 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
845 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
846 p = m_config_get_profile (conf, protocol);
847 if (p)
849 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
850 m_config_set_profile(conf,p);
854 #define PROFILE_CFG_EXTENSION "extension."
856 static void load_per_extension_config (m_config_t* conf, const char *const file)
858 char *str;
859 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
860 m_profile_t *p;
862 /* does filename actually have an extension ? */
863 str = strrchr (file, '.');
864 if (!str)
865 return;
867 sprintf (extension, PROFILE_CFG_EXTENSION);
868 strncat (extension, ++str, 7);
869 p = m_config_get_profile (conf, extension);
870 if (p)
872 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
873 m_config_set_profile(conf,p);
877 #define PROFILE_CFG_VO "vo."
878 #define PROFILE_CFG_AO "ao."
880 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
882 char profile[strlen (cfg) + strlen (out) + 1];
883 m_profile_t *p;
885 sprintf (profile, "%s%s", cfg, out);
886 p = m_config_get_profile (conf, profile);
887 if (p)
889 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
890 m_config_set_profile(conf,p);
894 static void load_per_file_config (m_config_t* conf, const char *const file)
896 char *confpath;
897 char cfg[strlen(file)+10];
898 struct stat st;
899 char *name;
901 sprintf (cfg, "%s.conf", file);
903 if (use_filedir_conf && !stat (cfg, &st))
905 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
906 m_config_parse_config_file (conf, cfg);
907 return;
910 if ((name = strrchr (cfg, '/')) == NULL)
911 name = cfg;
912 else
913 name++;
915 if ((confpath = get_path (name)) != NULL)
917 if (!stat (confpath, &st))
919 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
920 m_config_parse_config_file (conf, confpath);
923 free (confpath);
927 /* When libmpdemux performs a blocking operation (network connection or
928 * cache filling) if the operation fails we use this function to check
929 * if it was interrupted by the user.
930 * The function returns a new value for eof. */
931 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int eof)
933 mp_cmd_t* cmd;
934 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
935 switch(cmd->id) {
936 case MP_CMD_QUIT:
937 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
938 case MP_CMD_PLAY_TREE_STEP: {
939 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
940 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
941 } break;
942 case MP_CMD_PLAY_TREE_UP_STEP: {
943 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
944 } break;
945 case MP_CMD_PLAY_ALT_SRC_STEP: {
946 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
947 } break;
949 mp_cmd_free(cmd);
951 return eof;
954 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
956 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
958 play_tree_add_bpf(entry,mpctx->filename);
960 #ifdef HAVE_NEW_GUI
961 if (use_gui) {
962 if (entry) {
963 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
964 play_tree_free_list(entry,1);
966 } else
967 #endif
969 if(!entry) {
970 entry = mpctx->playtree_iter->tree;
971 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
972 return PT_NEXT_ENTRY;
974 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
975 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
976 return PT_NEXT_ENTRY;
979 play_tree_remove(entry,1,1);
980 return PT_NEXT_SRC;
982 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
983 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
984 entry = mpctx->playtree_iter->tree;
985 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
986 return PT_NEXT_ENTRY;
988 play_tree_remove(entry,1,1);
990 return PT_NEXT_SRC;
993 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
995 sub_data *subd;
996 #ifdef USE_ASS
997 ass_track_t *asst = 0;
998 #endif
1000 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1001 return;
1004 subd = sub_read_file(filename, fps);
1005 #ifdef USE_ASS
1006 if (ass_enabled)
1007 #ifdef USE_ICONV
1008 asst = ass_read_file(ass_library, filename, sub_cp);
1009 #else
1010 asst = ass_read_file(ass_library, filename, 0);
1011 #endif
1012 if (ass_enabled && subd && !asst)
1013 asst = ass_read_subdata(ass_library, subd, fps);
1015 if (!asst && !subd)
1016 #else
1017 if(!subd)
1018 #endif
1019 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1020 filename_recode(filename));
1022 #ifdef USE_ASS
1023 if (!asst && !subd) return;
1024 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1025 #else
1026 if (!subd) return;
1027 #endif
1028 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1029 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1030 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1031 filename_recode(filename));
1032 ++mpctx->set_of_sub_size;
1033 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1034 filename_recode(filename));
1037 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1038 void update_set_of_subtitles(struct MPContext *mpctx)
1039 // subdata was changed, set_of_sub... have to be updated.
1041 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1042 int i;
1043 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1044 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1045 set_of_subtitles[i-1] = set_of_subtitles[i];
1046 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1047 --mpctx->set_of_sub_size;
1048 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1050 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1051 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1053 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1054 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1055 ++mpctx->set_of_sub_size;
1059 void init_vo_spudec(struct MPContext *mpctx)
1061 if (vo_spudec)
1062 spudec_free(vo_spudec);
1063 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1064 vo_spudec = NULL;
1065 if (spudec_ifo) {
1066 unsigned int palette[16], width, height;
1067 current_module="spudec_init_vobsub";
1068 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1069 vo_spudec=spudec_new_scaled(palette, width, height);
1072 #ifdef USE_DVDREAD
1073 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1074 current_module="spudec_init_dvdread";
1075 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1076 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1078 #endif
1080 #ifdef USE_DVDNAV
1081 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1082 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1083 current_module="spudec_init_dvdnav";
1084 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1086 #endif
1088 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1089 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1090 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1091 current_module = "spudec_init_matroska";
1092 vo_spudec =
1093 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1094 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1095 mkv_sh_sub->height);
1096 forced_subs_only = mkv_sh_sub->forced_subs_only;
1099 if (vo_spudec==NULL) {
1100 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1101 unsigned int *palette = NULL;
1102 if (sh && !sh->has_palette && sh->extradata_len == 16*4) {
1103 int i;
1104 for (i = 0; i < 16; i++)
1105 sh->palette[i] = AV_RB32(sh->extradata + i*4);
1106 sh->has_palette = 1;
1108 if (sh && sh->has_palette)
1109 palette = sh->palette;
1110 current_module="spudec_init_normal";
1111 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1112 spudec_set_font_factor(vo_spudec,font_factor);
1115 if (vo_spudec!=NULL)
1116 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1120 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1121 * make it all work is to use the builtin SDL-bootstrap code, which
1122 * will be done automatically by replacing our main() if we include SDL.h.
1124 #if defined(__APPLE__) && defined(HAVE_SDL)
1125 #include <SDL.h>
1126 #endif
1129 * \brief append a formatted string
1130 * \param buf buffer to print into
1131 * \param pos position of terminating 0 in buf
1132 * \param len maximum number of characters in buf, not including terminating 0
1133 * \param format printf format string
1135 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1137 va_list va;
1138 va_start(va, format);
1139 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1140 va_end(va);
1141 if (*pos >= len ) {
1142 buf[len] = 0;
1143 *pos = len;
1148 * \brief append time in the hh:mm:ss.f format
1149 * \param buf buffer to print into
1150 * \param pos position of terminating 0 in buf
1151 * \param len maximum number of characters in buf, not including terminating 0
1152 * \param time time value to convert/append
1154 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1155 long tenths = 10 * time;
1156 int f1 = tenths % 10;
1157 int ss = (tenths / 10) % 60;
1158 int mm = (tenths / 600) % 60;
1159 int hh = tenths / 36000;
1160 if (time <= 0) {
1161 saddf(buf, pos, len, "unknown");
1162 return;
1164 if (hh > 0)
1165 saddf(buf, pos, len, "%2d:", hh);
1166 if (hh > 0 || mm > 0)
1167 saddf(buf, pos, len, "%02d:", mm);
1168 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1172 * \brief print the status line
1173 * \param a_pos audio position
1174 * \param a_v A-V desynchronization
1175 * \param corr amount out A-V synchronization
1177 static void print_status(struct MPContext *mpctx, float a_pos, float a_v, float corr)
1179 struct MPOpts *opts = &mpctx->opts;
1180 sh_video_t * const sh_video = mpctx->sh_video;
1181 int width;
1182 char *line;
1183 unsigned pos = 0;
1184 get_screen_size();
1185 if (screen_width > 0)
1186 width = screen_width;
1187 else
1188 width = 80;
1189 #if defined(WIN32) || defined(__OS2__)
1190 /* Windows command line is broken (MinGW's rxvt works, but we
1191 * should not depend on that). */
1192 width--;
1193 #endif
1194 line = malloc(width + 1); // one additional char for the terminating null
1196 // Audio time
1197 if (mpctx->sh_audio) {
1198 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1199 if (!sh_video) {
1200 float len = demuxer_get_time_length(mpctx->demuxer);
1201 saddf(line, &pos, width, "(");
1202 sadd_hhmmssf(line, &pos, width, a_pos);
1203 saddf(line, &pos, width, ") of %.1f (", len);
1204 sadd_hhmmssf(line, &pos, width, len);
1205 saddf(line, &pos, width, ") ");
1209 // Video time
1210 if (sh_video)
1211 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1213 // A-V sync
1214 if (mpctx->sh_audio && sh_video)
1215 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1217 // Video stats
1218 if (sh_video)
1219 saddf(line, &pos, width, "%3d/%3d ",
1220 (int)sh_video->num_frames,
1221 (int)sh_video->num_frames_decoded);
1223 // CPU usage
1224 if (sh_video) {
1225 if (sh_video->timer > 0.5)
1226 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1227 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1228 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1229 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1230 else
1231 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1232 } else if (mpctx->sh_audio) {
1233 if (mpctx->delay > 0.5)
1234 saddf(line, &pos, width, "%4.1f%% ",
1235 100.0*audio_time_usage/(double)mpctx->delay);
1236 else
1237 saddf(line, &pos, width, "??,?%% ");
1240 // VO stats
1241 if (sh_video)
1242 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1244 #ifdef USE_STREAM_CACHE
1245 // cache stats
1246 if (stream_cache_size > 0)
1247 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1248 #endif
1250 // other
1251 if (opts->playback_speed != 1)
1252 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1254 // end
1255 if (erase_to_end_of_line) {
1256 line[pos] = 0;
1257 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1258 } else {
1259 memset(&line[pos], ' ', width - pos);
1260 line[width] = 0;
1261 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1263 free(line);
1267 * \brief build a chain of audio filters that converts the input format
1268 * to the ao's format, taking into account the current playback_speed.
1269 * \param sh_audio describes the requested input format of the chain.
1270 * \param ao_data describes the requested output format of the chain.
1272 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1274 struct MPOpts *opts = &mpctx->opts;
1275 int new_srate;
1276 int result;
1277 if (!sh_audio)
1279 #ifdef HAVE_NEW_GUI
1280 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1281 #endif
1282 mpctx->mixer.afilter = NULL;
1283 return 0;
1285 if(af_control_any_rev(sh_audio->afilter,
1286 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1287 &opts->playback_speed)) {
1288 new_srate = sh_audio->samplerate;
1289 } else {
1290 new_srate = sh_audio->samplerate * opts->playback_speed;
1291 if (new_srate != ao_data->samplerate) {
1292 // limits are taken from libaf/af_resample.c
1293 if (new_srate < 8000)
1294 new_srate = 8000;
1295 if (new_srate > 192000)
1296 new_srate = 192000;
1297 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1300 result = init_audio_filters(sh_audio, new_srate,
1301 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1302 mpctx->mixer.afilter = sh_audio->afilter;
1303 #ifdef HAVE_NEW_GUI
1304 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1305 #endif
1306 return result;
1310 typedef struct mp_osd_msg mp_osd_msg_t;
1311 struct mp_osd_msg {
1312 /// Previous message on the stack.
1313 mp_osd_msg_t* prev;
1314 /// Message text.
1315 char msg[64];
1316 int id,level,started;
1317 /// Display duration in ms.
1318 unsigned time;
1321 /// OSD message stack.
1322 static mp_osd_msg_t* osd_msg_stack = NULL;
1325 * \brief Add a message on the OSD message stack
1327 * If a message with the same id is already present in the stack
1328 * it is pulled on top of the stack, otherwise a new message is created.
1332 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1333 mp_osd_msg_t *msg,*last=NULL;
1334 va_list va;
1335 int r;
1337 // look if the id is already in the stack
1338 for(msg = osd_msg_stack ; msg && msg->id != id ;
1339 last = msg, msg = msg->prev);
1340 // not found: alloc it
1341 if(!msg) {
1342 msg = calloc(1,sizeof(mp_osd_msg_t));
1343 msg->prev = osd_msg_stack;
1344 osd_msg_stack = msg;
1345 } else if(last) { // found, but it's not on top of the stack
1346 last->prev = msg->prev;
1347 msg->prev = osd_msg_stack;
1348 osd_msg_stack = msg;
1350 // write the msg
1351 va_start(va,fmt);
1352 r = vsnprintf(msg->msg, 64, fmt, va);
1353 va_end(va);
1354 if(r >= 64) msg->msg[63] = 0;
1355 // set id and time
1356 msg->id = id;
1357 msg->level = level;
1358 msg->time = time;
1363 * \brief Remove a message from the OSD stack
1365 * This function can be used to get rid of a message right away.
1369 void rm_osd_msg(int id) {
1370 mp_osd_msg_t *msg,*last=NULL;
1372 // Search for the msg
1373 for(msg = osd_msg_stack ; msg && msg->id != id ;
1374 last = msg, msg = msg->prev);
1375 if(!msg) return;
1377 // Detach it from the stack and free it
1378 if(last)
1379 last->prev = msg->prev;
1380 else
1381 osd_msg_stack = msg->prev;
1382 free(msg);
1386 * \brief Remove all messages from the OSD stack
1390 static void clear_osd_msgs(void) {
1391 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1392 while(msg) {
1393 prev = msg->prev;
1394 free(msg);
1395 msg = prev;
1397 osd_msg_stack = NULL;
1401 * \brief Get the current message from the OSD stack.
1403 * This function decrements the message timer and destroys the old ones.
1404 * The message that should be displayed is returned (if any).
1408 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1410 mp_osd_msg_t *msg,*prev,*last = NULL;
1411 static unsigned last_update = 0;
1412 unsigned now = GetTimerMS();
1413 unsigned diff;
1414 char hidden_dec_done = 0;
1416 if (osd_visible) {
1417 // 36000000 means max timed visibility is 1 hour into the future, if
1418 // the difference is greater assume it's wrapped around from below 0
1419 if (osd_visible - now > 36000000) {
1420 osd_visible = 0;
1421 vo_osd_progbar_type = -1; // disable
1422 vo_osd_changed(OSDTYPE_PROGBAR);
1423 if (mpctx->osd_function != OSD_PAUSE)
1424 mpctx->osd_function = OSD_PLAY;
1428 if(!last_update) last_update = now;
1429 diff = now >= last_update ? now - last_update : 0;
1431 last_update = now;
1433 // Look for the first message in the stack with high enough level.
1434 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1435 prev = msg->prev;
1436 if(msg->level > osd_level && hidden_dec_done) continue;
1437 // The message has a high enough level or it is the first hidden one
1438 // in both cases we decrement the timer or kill it.
1439 if(!msg->started || msg->time > diff) {
1440 if(msg->started) msg->time -= diff;
1441 else msg->started = 1;
1442 // display it
1443 if(msg->level <= osd_level) return msg;
1444 hidden_dec_done = 1;
1445 continue;
1447 // kill the message
1448 free(msg);
1449 if(last) {
1450 last->prev = prev;
1451 msg = last;
1452 } else {
1453 osd_msg_stack = prev;
1454 msg = NULL;
1457 // Nothing found
1458 return NULL;
1462 * \brief Display the OSD bar.
1464 * Display the OSD bar or fall back on a simple message.
1468 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1470 if(osd_level < 1) return;
1472 if(mpctx->sh_video) {
1473 osd_visible = (GetTimerMS() + 1000) | 1;
1474 vo_osd_progbar_type = type;
1475 vo_osd_progbar_value = 256*(val-min)/(max-min);
1476 vo_osd_changed(OSDTYPE_PROGBAR);
1477 return;
1480 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1481 name,ROUND(100*(val-min)/(max-min)));
1486 * \brief Update the OSD message line.
1488 * This function displays the current message on the vo OSD or on the term.
1489 * If the stack is empty and the OSD level is high enough the timer
1490 * is displayed (only on the vo OSD).
1494 static void update_osd_msg(struct MPContext *mpctx)
1496 mp_osd_msg_t *msg;
1497 struct osd_state *osd = mpctx->osd;
1498 char osd_text_timer[64];
1500 // Look if we have a msg
1501 if((msg = get_osd_msg(mpctx))) {
1502 if (strcmp(osd->osd_text, msg->msg)) {
1503 strncpy(osd->osd_text, msg->msg, 63);
1504 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1505 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1507 return;
1510 if(mpctx->sh_video) {
1511 // fallback on the timer
1512 if(osd_level>=2) {
1513 int len = demuxer_get_time_length(mpctx->demuxer);
1514 int percentage = -1;
1515 char percentage_text[10];
1516 int pts = demuxer_get_current_time(mpctx->demuxer);
1518 if (mpctx->osd_show_percentage)
1519 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1521 if (percentage >= 0)
1522 snprintf(percentage_text, 9, " (%d%%)", percentage);
1523 else
1524 percentage_text[0] = 0;
1526 if (osd_level == 3)
1527 snprintf(osd_text_timer, 63,
1528 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1529 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1530 len/3600,(len/60)%60,len%60,percentage_text);
1531 else
1532 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1533 mpctx->osd_function,pts/3600,(pts/60)%60,
1534 pts%60,percentage_text);
1535 } else
1536 osd_text_timer[0]=0;
1538 // always decrement the percentage timer
1539 if(mpctx->osd_show_percentage)
1540 mpctx->osd_show_percentage--;
1542 if (strcmp(osd->osd_text, osd_text_timer)) {
1543 strncpy(osd->osd_text, osd_text_timer, 63);
1544 vo_osd_changed(OSDTYPE_OSD);
1546 return;
1549 // Clear the term osd line
1550 if (term_osd && osd->osd_text[0]) {
1551 osd->osd_text[0] = 0;
1552 printf("%s\n",term_osd_esc);
1556 ///@}
1557 // OSDMsgStack
1560 void reinit_audio_chain(struct MPContext *mpctx)
1562 struct MPOpts *opts = &mpctx->opts;
1563 if(mpctx->sh_audio){
1564 current_module="init_audio_codec";
1565 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1566 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1567 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1568 mpctx->d_audio->id = -2;
1569 return;
1570 } else
1571 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1572 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1575 //const ao_info_t *info=audio_out->info;
1576 current_module="af_preinit";
1577 ao_data.samplerate=force_srate;
1578 ao_data.channels=0;
1579 ao_data.format=audio_output_format;
1580 #if 1
1581 // first init to detect best values
1582 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1583 // input:
1584 mpctx->sh_audio->samplerate,
1585 // output:
1586 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1587 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1588 exit_player(mpctx, MSGTR_Exit_error);
1590 #endif
1591 current_module="ao2_init";
1592 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1593 0, // plugin flag
1594 ao_data.samplerate,
1595 ao_data.channels,
1596 ao_data.format,0))){
1597 // FAILED:
1598 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1599 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1600 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1601 mpctx->d_audio->id = -2;
1602 return;
1603 } else {
1604 // SUCCESS:
1605 mpctx->initialized_flags|=INITIALIZED_AO;
1606 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1607 mpctx->audio_out->info->short_name,
1608 ao_data.samplerate, ao_data.channels,
1609 af_fmt2str_short(ao_data.format),
1610 af_fmt2bits(ao_data.format)/8 );
1611 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1612 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1613 if(strlen(mpctx->audio_out->info->comment) > 0)
1614 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1615 // init audio filters:
1616 #if 1
1617 current_module="af_init";
1618 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1619 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1620 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1621 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1622 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1624 #endif
1626 mpctx->mixer.audio_out = mpctx->audio_out;
1627 mpctx->mixer.volstep = volstep;
1632 ///@}
1633 // Command2Property
1636 // Return pts value corresponding to the end point of audio written to the
1637 // ao so far.
1638 static double written_audio_pts(struct MPContext *mpctx)
1640 sh_audio_t *sh_audio = mpctx->sh_audio;
1641 demux_stream_t *d_audio = mpctx->d_audio;
1642 double buffered_output;
1643 // first calculate the end pts of audio that has been output by decoder
1644 double a_pts = sh_audio->pts;
1645 if (a_pts != MP_NOPTS_VALUE)
1646 // Good, decoder supports new way of calculating audio pts.
1647 // sh_audio->pts is the timestamp of the latest input packet with
1648 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1649 // the amount of bytes the decoder has written after that timestamp.
1650 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1651 else {
1652 // Decoder doesn't support new way of calculating pts (or we're
1653 // being called before it has decoded anything with known timestamp).
1654 // Use the old method of audio pts calculation: take the timestamp
1655 // of last packet with known pts the decoder has read data from,
1656 // and add amount of bytes read after the beginning of that packet
1657 // divided by input bps. This will be inaccurate if the input/output
1658 // ratio is not constant for every audio packet or if it is constant
1659 // but not accurately known in sh_audio->i_bps.
1661 a_pts = d_audio->pts;
1662 // ds_tell_pts returns bytes read after last timestamp from
1663 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1664 // it has read but not decoded
1665 if (sh_audio->i_bps)
1666 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1667 (double)sh_audio->i_bps;
1669 // Now a_pts hopefully holds the pts for end of audio from decoder.
1670 // Substract data in buffers between decoder and audio out.
1672 // Decoded but not filtered
1673 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1675 // Data buffered in audio filters, measured in bytes of "missing" output
1676 buffered_output = af_calc_delay(sh_audio->afilter);
1678 // Data that was ready for ao but was buffered because ao didn't fully
1679 // accept everything to internal buffers yet
1680 buffered_output += sh_audio->a_out_buffer_len;
1682 // Filters divide audio length by playback_speed, so multiply by it
1683 // to get the length in original units without speedup or slowdown
1684 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1686 return a_pts;
1689 // Return pts value corresponding to currently playing audio.
1690 double playing_audio_pts(struct MPContext *mpctx)
1692 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1693 mpctx->audio_out->get_delay();
1696 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1697 struct MPOpts *opts = &mpctx->opts;
1698 // check for frame-drop:
1699 current_module = "check_framedrop";
1700 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1701 static int dropped_frames;
1702 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1703 float d = delay-mpctx->delay;
1704 ++total_frame_cnt;
1705 // we should avoid dropping too many frames in sequence unless we
1706 // are too late. and we allow 100ms A-V delay here:
1707 if (d < -dropped_frames*frame_time-0.100 &&
1708 mpctx->osd_function != OSD_PAUSE) {
1709 ++drop_frame_cnt;
1710 ++dropped_frames;
1711 return frame_dropping;
1712 } else
1713 dropped_frames = 0;
1715 return 0;
1718 static int generate_video_frame(struct MPContext *mpctx)
1720 sh_video_t * const sh_video = mpctx->sh_video;
1721 demux_stream_t *d_video = mpctx->d_video;
1723 unsigned char *start;
1724 int in_size;
1725 int hit_eof=0;
1726 double pts;
1728 while (1) {
1729 int drop_frame = check_framedrop(mpctx, sh_video->frametime);
1730 void *decoded_frame;
1731 current_module = "decode video";
1732 // XXX Time used in this call is not counted in any performance
1733 // timer now, OSD is not updated correctly for filter-added frames
1734 if (vf_output_queued_frame(sh_video->vfilter))
1735 break;
1736 current_module = "video_read_frame";
1737 in_size = ds_get_packet_pts(d_video, &start, &pts);
1738 if (in_size < 0) {
1739 // try to extract last frames in case of decoder lag
1740 in_size = 0;
1741 pts = 1e300;
1742 hit_eof = 1;
1744 if (in_size > max_framesize)
1745 max_framesize = in_size;
1746 current_module = "decode video";
1747 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1748 if (decoded_frame) {
1749 update_subtitles(sh_video, mpctx->d_sub, 0);
1750 update_teletext(sh_video, mpctx->demuxer, 0);
1751 update_osd_msg(mpctx);
1752 current_module = "filter video";
1753 if (filter_video(sh_video, decoded_frame, sh_video->pts,
1754 mpctx->osd))
1755 break;
1756 } else if (drop_frame)
1757 return -1;
1758 if (hit_eof)
1759 return 0;
1761 return 1;
1764 #ifdef HAVE_RTC
1765 int rtc_fd = -1;
1766 #endif
1768 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1770 #ifdef HAVE_RTC
1771 if (rtc_fd >= 0){
1772 // -------- RTC -----------
1773 current_module="sleep_rtc";
1774 while (time_frame > 0.000) {
1775 unsigned long rtc_ts;
1776 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1777 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1778 time_frame -= get_relative_time(mpctx);
1780 } else
1781 #endif
1783 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1784 // unnecessarily high CPU usage
1785 float margin = softsleep ? 0.011 : 0;
1786 current_module = "sleep_timer";
1787 while (time_frame > margin) {
1788 usec_sleep(1000000 * (time_frame - margin));
1789 time_frame -= get_relative_time(mpctx);
1791 if (softsleep){
1792 current_module = "sleep_soft";
1793 if (time_frame < 0)
1794 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1795 while (time_frame > 0)
1796 time_frame -= get_relative_time(mpctx); // burn the CPU
1799 return time_frame;
1802 #ifdef USE_DVDNAV
1803 #ifndef FF_B_TYPE
1804 #define FF_B_TYPE 3
1805 #endif
1806 /// store decoded video image
1807 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1808 mp_image_t *from_mpi) {
1809 mp_image_t *mpi;
1811 /// Do not store B-frames
1812 if (from_mpi->pict_type == FF_B_TYPE)
1813 return to_mpi;
1815 if (to_mpi &&
1816 to_mpi->w == from_mpi->w &&
1817 to_mpi->h == from_mpi->h &&
1818 to_mpi->imgfmt == from_mpi->imgfmt)
1819 mpi = to_mpi;
1820 else {
1821 if (to_mpi)
1822 free_mp_image(to_mpi);
1823 if (from_mpi->w == 0 || from_mpi->h == 0)
1824 return NULL;
1825 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1828 copy_mpi(mpi,from_mpi);
1829 return mpi;
1832 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1833 struct MPOpts *opts = &ctx->opts;
1834 if (ctx->sh_video) {
1835 /// clear video pts
1836 ctx->d_video->pts = 0.0f;
1837 ctx->sh_video->pts = 0.0f;
1838 ctx->sh_video->i_pts = 0.0f;
1839 ctx->sh_video->last_pts = 0.0f;
1840 ctx->sh_video->num_buffered_pts = 0;
1841 ctx->sh_video->num_frames = 0;
1842 ctx->sh_video->num_frames_decoded = 0;
1843 ctx->sh_video->timer = 0.0f;
1844 ctx->sh_video->stream_delay = 0.0f;
1845 ctx->sh_video->timer = 0;
1846 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1849 if (ctx->sh_audio) {
1850 /// free audio packets and reset
1851 ds_free_packs(ctx->d_audio);
1852 audio_delay -= ctx->sh_audio->stream_delay;
1853 ctx->delay =- audio_delay;
1854 ctx->audio_out->reset();
1855 resync_audio_stream(ctx->sh_audio);
1858 if (ctx->d_sub) opts->sub_id = -2;
1860 audio_delay = 0.0f;
1862 /// clear all EOF related flags
1863 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1866 /// Restore last decoded DVDNAV (still frame)
1867 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1868 int *in_size,
1869 unsigned char **start,
1870 mp_image_t *decoded_frame)
1872 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1873 return decoded_frame;
1875 /// a change occured in dvdnav stream
1876 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1877 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1878 mp_dvdnav_context_free(mpctx);
1879 mp_dvdnav_reset_stream(mpctx);
1880 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1881 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1884 if (*in_size < 0) {
1885 float len;
1887 /// Display still frame, if any
1888 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1889 decoded_frame = mpctx->nav_smpi;
1891 /// increment video frame : continue playing after still frame
1892 len = demuxer_get_time_length(mpctx->demuxer);
1893 if (mpctx->sh_video->pts >= len &&
1894 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1895 mp_dvdnav_skip_still(mpctx->stream);
1896 mp_dvdnav_skip_wait(mpctx->stream);
1898 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1900 if (mpctx->nav_buffer) {
1901 *start = mpctx->nav_start;
1902 *in_size = mpctx->nav_in_size;
1903 if (mpctx->nav_start)
1904 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1908 return decoded_frame;
1911 /// Save last decoded DVDNAV (still frame)
1912 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1913 unsigned char *start,
1914 mp_image_t *decoded_frame)
1916 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1917 return;
1919 if (mpctx->nav_buffer)
1920 free(mpctx->nav_buffer);
1922 mpctx->nav_buffer = malloc(in_size);
1923 mpctx->nav_start = start;
1924 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1925 if (mpctx->nav_buffer)
1926 memcpy(mpctx->nav_buffer,start,in_size);
1928 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1929 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1931 #endif /* USE_DVDNAV */
1933 static void adjust_sync_and_print_status(struct MPContext *mpctx,
1934 int between_frames,
1935 float timing_error)
1937 struct MPOpts *opts = &mpctx->opts;
1938 current_module="av_sync";
1940 if(mpctx->sh_audio){
1941 double a_pts, v_pts;
1943 if (autosync)
1945 * If autosync is enabled, the value for delay must be calculated
1946 * a bit differently. It is set only to the difference between
1947 * the audio and video timers. Any attempt to include the real
1948 * or corrected delay causes the pts_correction code below to
1949 * try to correct for the changes in delay which autosync is
1950 * trying to measure. This keeps the two from competing, but still
1951 * allows the code to correct for PTS drift *only*. (Using a delay
1952 * value here, even a "corrected" one, would be incompatible with
1953 * autosync mode.)
1955 a_pts = written_audio_pts(mpctx) - mpctx->delay;
1956 else
1957 a_pts = playing_audio_pts(mpctx);
1959 v_pts = mpctx->sh_video->pts;
1962 static int drop_message=0;
1963 double AV_delay = a_pts - audio_delay - v_pts;
1964 double x;
1965 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1966 ++drop_message;
1967 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1969 if (autosync)
1970 x = AV_delay*0.1f;
1971 else
1972 /* Do not correct target time for the next frame if this frame
1973 * was late not because of wrong target time but because the
1974 * target time could not be met */
1975 x = (AV_delay + timing_error * opts->playback_speed) * 0.1f;
1976 if (x < -max_pts_correction)
1977 x = -max_pts_correction;
1978 else if (x> max_pts_correction)
1979 x = max_pts_correction;
1980 if (default_max_pts_correction >= 0)
1981 max_pts_correction = default_max_pts_correction;
1982 else
1983 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1984 if (!between_frames) {
1985 mpctx->delay+=x;
1986 c_total+=x;
1988 if(!quiet)
1989 print_status(mpctx, a_pts - audio_delay, AV_delay, c_total);
1992 } else {
1993 // No audio:
1995 if (!quiet)
1996 print_status(mpctx, 0, 0, 0);
2000 static int fill_audio_out_buffers(struct MPContext *mpctx)
2002 struct MPOpts *opts = &mpctx->opts;
2003 unsigned int t;
2004 double tt;
2005 int playsize;
2006 int playflags=0;
2007 int audio_eof=0;
2008 int bytes_to_write;
2009 sh_audio_t * const sh_audio = mpctx->sh_audio;
2011 current_module="play_audio";
2013 while (1) {
2014 // all the current uses of ao_data.pts seem to be in aos that handle
2015 // sync completely wrong; there should be no need to use ao_data.pts
2016 // in get_space()
2017 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2018 bytes_to_write = mpctx->audio_out->get_space();
2019 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2020 break;
2022 // handle audio-only case:
2023 // this is where mplayer sleeps during audio-only playback
2024 // to avoid 100% CPU use
2025 usec_sleep(10000); // Wait a tick before retry
2028 while (bytes_to_write) {
2029 playsize = bytes_to_write;
2030 if (playsize > MAX_OUTBURST)
2031 playsize = MAX_OUTBURST;
2032 bytes_to_write -= playsize;
2034 // Fill buffer if needed:
2035 current_module="decode_audio";
2036 t = GetTimer();
2037 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2038 if (mpctx->d_audio->eof) {
2039 audio_eof = 1;
2040 if (sh_audio->a_out_buffer_len == 0)
2041 return 0;
2043 t = GetTimer() - t;
2044 tt = t*0.000001f; audio_time_usage+=tt;
2045 if (playsize > sh_audio->a_out_buffer_len) {
2046 playsize = sh_audio->a_out_buffer_len;
2047 if (audio_eof)
2048 playflags |= AOPLAY_FINAL_CHUNK;
2050 if (!playsize)
2051 break;
2053 // play audio:
2054 current_module="play_audio";
2056 // Is this pts value actually useful for the aos that access it?
2057 // They're obviously badly broken in the way they handle av sync;
2058 // would not having access to this make them more broken?
2059 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2060 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2062 if (playsize > 0) {
2063 sh_audio->a_out_buffer_len -= playsize;
2064 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2065 sh_audio->a_out_buffer_len);
2066 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2068 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2069 // Sanity check to avoid hanging in case current ao doesn't output
2070 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2071 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2072 sh_audio->a_out_buffer_len = 0;
2075 return 1;
2078 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2079 float *aq_sleep_time)
2081 struct MPOpts *opts = &mpctx->opts;
2082 int frame_time_remaining = 0;
2083 current_module="calc_sleep_time";
2085 *time_frame -= get_relative_time(mpctx); // reset timer
2087 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2088 float delay = mpctx->audio_out->get_delay();
2089 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2091 if (autosync) {
2093 * Adjust this raw delay value by calculating the expected
2094 * delay for this frame and generating a new value which is
2095 * weighted between the two. The higher autosync is, the
2096 * closer to the delay value gets to that which "-nosound"
2097 * would have used, and the longer it will take for A/V
2098 * sync to settle at the right value (but it eventually will.)
2099 * This settling time is very short for values below 100.
2101 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2102 float difference = delay - predicted;
2103 delay = predicted + difference / (float)autosync;
2106 *time_frame = delay - mpctx->delay / opts->playback_speed;
2108 // delay = amount of audio buffered in soundcard/driver
2109 if (delay > 0.25) delay=0.25; else
2110 if (delay < 0.10) delay=0.10;
2111 if (*time_frame > delay*0.6) {
2112 // sleep time too big - may cause audio drops (buffer underrun)
2113 frame_time_remaining = 1;
2114 *time_frame = delay*0.5;
2116 } else {
2117 // If we're lagging more than 200 ms behind the right playback rate,
2118 // don't try to "catch up".
2119 // If benchmark is set always output frames as fast as possible
2120 // without sleeping.
2121 if (*time_frame < -0.2 || benchmark)
2122 *time_frame = 0;
2125 *aq_sleep_time += *time_frame;
2128 //============================== SLEEP: ===================================
2130 // flag 256 means: libvo driver does its timing (dvb card)
2131 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2132 *time_frame = timing_sleep(mpctx, *time_frame);
2133 return frame_time_remaining;
2136 int reinit_video_chain(struct MPContext *mpctx)
2138 struct MPOpts *opts = &mpctx->opts;
2139 sh_video_t * const sh_video = mpctx->sh_video;
2140 double ar=-1.0;
2141 //================== Init VIDEO (codec & libvo) ==========================
2142 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2143 current_module="preinit_libvo";
2145 //shouldn't we set dvideo->id=-2 when we fail?
2146 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2147 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2148 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2149 goto err_out;
2151 mpctx->initialized_flags|=INITIALIZED_VO;
2154 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2155 mpctx->sh_video->stream_aspect = ar;
2156 current_module="init_video_filters";
2158 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2159 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2161 #ifdef HAVE_MENU
2162 if(use_menu) {
2163 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2164 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2165 if(!vf_menu) {
2166 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2167 use_menu = 0;
2170 if(vf_menu)
2171 sh_video->vfilter = vf_menu;
2172 #endif
2174 #ifdef USE_ASS
2175 if(ass_enabled) {
2176 int i;
2177 int insert = 1;
2178 if (opts->vf_settings)
2179 for (i = 0; opts->vf_settings[i].name; ++i)
2180 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2181 insert = 0;
2182 break;
2184 if (insert) {
2185 extern vf_info_t vf_info_ass;
2186 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2187 char* vf_arg[] = {"auto", "1", NULL};
2188 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2189 if (vf_ass)
2190 sh_video->vfilter = vf_ass;
2191 else
2192 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2195 #endif
2197 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2199 #ifdef USE_ASS
2200 if (ass_enabled)
2201 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2202 #endif
2204 current_module="init_video_codec";
2206 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2207 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2208 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2210 if(!sh_video->initialized){
2211 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2212 goto err_out;
2215 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2217 if (sh_video->codec)
2218 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2220 sh_video->last_pts = MP_NOPTS_VALUE;
2221 sh_video->num_buffered_pts = 0;
2222 sh_video->next_frame_time = 0;
2224 if(auto_quality>0){
2225 // Auto quality option enabled
2226 output_quality=get_video_quality_max(sh_video);
2227 if(auto_quality>output_quality) auto_quality=output_quality;
2228 else output_quality=auto_quality;
2229 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2230 set_video_quality(sh_video,output_quality);
2233 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2235 current_module="init_vo";
2237 return 1;
2239 err_out:
2240 mpctx->sh_video = mpctx->d_video->sh = NULL;
2241 return 0;
2244 static double update_video(struct MPContext *mpctx, int *blit_frame)
2246 struct MPOpts *opts = &mpctx->opts;
2247 sh_video_t * const sh_video = mpctx->sh_video;
2248 //-------------------- Decode a frame: -----------------------
2249 double frame_time;
2250 *blit_frame = 0; // Don't blit if we hit EOF
2251 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2252 mpctx->osd); // hack for vf_expand
2253 if (!opts->correct_pts) {
2254 unsigned char* start=NULL;
2255 void *decoded_frame = NULL;
2256 int drop_frame=0;
2257 int in_size;
2259 current_module = "video_read_frame";
2260 frame_time = sh_video->next_frame_time;
2261 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2262 &start, force_fps);
2263 #ifdef USE_DVDNAV
2264 /// wait, still frame or EOF
2265 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2266 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2267 if (mpctx->d_video) mpctx->d_video->eof = 0;
2268 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2269 mpctx->stream->eof = 0;
2270 } else
2271 #endif
2272 if (in_size < 0)
2273 return -1;
2274 if (in_size > max_framesize)
2275 max_framesize = in_size; // stats
2276 sh_video->timer += frame_time;
2277 if (mpctx->sh_audio)
2278 mpctx->delay -= frame_time;
2279 // video_read_frame can change fps (e.g. for ASF video)
2280 vo_fps = sh_video->fps;
2281 drop_frame = check_framedrop(mpctx, frame_time);
2282 update_subtitles(sh_video, mpctx->d_sub, 0);
2283 update_teletext(sh_video, mpctx->demuxer, 0);
2284 update_osd_msg(mpctx);
2285 current_module = "decode_video";
2286 #ifdef USE_DVDNAV
2287 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size,&start,
2288 decoded_frame);
2289 /// still frame has been reached, no need to decode
2290 if (in_size > 0 && !decoded_frame)
2291 #endif
2292 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2293 sh_video->pts);
2294 #ifdef USE_DVDNAV
2295 /// save back last still frame for future display
2296 mp_dvdnav_save_smpi(mpctx, in_size, start, decoded_frame);
2297 #endif
2298 current_module = "filter_video";
2299 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2300 sh_video->pts,
2301 mpctx->osd));
2303 else {
2304 int res = generate_video_frame(mpctx);
2305 if (!res)
2306 return -1;
2307 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS,
2308 &sh_video->pts);
2309 if (sh_video->pts == MP_NOPTS_VALUE) {
2310 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2311 sh_video->pts = sh_video->last_pts;
2313 if (sh_video->last_pts == MP_NOPTS_VALUE)
2314 sh_video->last_pts= sh_video->pts;
2315 else if (sh_video->last_pts >= sh_video->pts) {
2316 sh_video->last_pts = sh_video->pts;
2317 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2319 frame_time = sh_video->pts - sh_video->last_pts;
2320 sh_video->last_pts = sh_video->pts;
2321 sh_video->timer += frame_time;
2322 if(mpctx->sh_audio)
2323 mpctx->delay -= frame_time;
2324 *blit_frame = res > 0;
2326 return frame_time;
2329 static void pause_loop(struct MPContext *mpctx)
2331 mp_cmd_t* cmd;
2332 if (!quiet) {
2333 // Small hack to display the pause message on the OSD line.
2334 // The pause string is: "\n == PAUSE == \r" so we need to
2335 // take the first and the last char out
2336 if (term_osd && !mpctx->sh_video) {
2337 char msg[128] = MSGTR_Paused;
2338 int mlen = strlen(msg);
2339 msg[mlen-1] = '\0';
2340 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2341 update_osd_msg(mpctx);
2342 } else
2343 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2344 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2346 #ifdef HAVE_NEW_GUI
2347 if (use_gui)
2348 guiGetEvent(guiCEvent, (char *)guiSetPause);
2349 #endif
2350 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2351 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2353 if (mpctx->audio_out && mpctx->sh_audio)
2354 mpctx->audio_out->pause(); // pause audio, keep data if possible
2356 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2357 || cmd->id == MP_CMD_SET_MOUSE_POS) {
2358 if (cmd) {
2359 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2360 mp_cmd_free(cmd);
2361 continue;
2363 if (mpctx->sh_video && mpctx->video_out)
2364 vo_check_events(mpctx->video_out);
2365 #ifdef HAVE_NEW_GUI
2366 if (use_gui) {
2367 guiEventHandling();
2368 guiGetEvent(guiReDraw, NULL);
2369 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2370 break;
2372 #endif
2373 #ifdef HAVE_MENU
2374 if (vf_menu)
2375 vf_menu_pause_update(vf_menu);
2376 #endif
2377 usec_sleep(20000);
2379 if (cmd && cmd->id == MP_CMD_PAUSE) {
2380 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2381 mp_cmd_free(cmd);
2383 mpctx->osd_function=OSD_PLAY;
2384 if (mpctx->audio_out && mpctx->sh_audio)
2385 mpctx->audio_out->resume(); // resume audio
2386 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2387 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2388 (void)get_relative_time(mpctx); // ignore time that passed during pause
2389 #ifdef HAVE_NEW_GUI
2390 if (use_gui) {
2391 if (guiIntfStruct.Playing == guiSetStop)
2392 mpctx->eof = 1;
2393 else
2394 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2396 #endif
2399 void print_version(void){
2400 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2402 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2403 GetCpuCaps(&gCpuCaps);
2404 #ifdef ARCH_X86
2405 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2406 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2407 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2408 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2409 #ifdef RUNTIME_CPUDETECT
2410 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2411 #else
2412 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2413 #ifdef HAVE_MMX
2414 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2415 #endif
2416 #ifdef HAVE_MMX2
2417 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2418 #endif
2419 #ifdef HAVE_3DNOW
2420 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2421 #endif
2422 #ifdef HAVE_3DNOWEX
2423 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2424 #endif
2425 #ifdef HAVE_SSE
2426 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2427 #endif
2428 #ifdef HAVE_SSE2
2429 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2430 #endif
2431 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2432 #endif /* RUNTIME_CPUDETECT */
2433 #endif /* ARCH_X86 */
2437 // Find the right mute status and record position for new file position
2438 static void edl_seek_reset(MPContext *mpctx)
2440 mpctx->edl_muted = 0;
2441 next_edl_record = edl_records;
2443 while (next_edl_record) {
2444 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2445 break;
2447 if (next_edl_record->action == EDL_MUTE)
2448 mpctx->edl_muted = !mpctx->edl_muted;
2449 next_edl_record = next_edl_record->next;
2451 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2452 mixer_mute(&mpctx->mixer);
2456 // Execute EDL command for the current position if one exists
2457 static void edl_update(MPContext *mpctx)
2459 if (!next_edl_record)
2460 return;
2462 if (!mpctx->sh_video) {
2463 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2464 free_edl(edl_records);
2465 next_edl_record = NULL;
2466 edl_records = NULL;
2467 return;
2470 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2471 if (next_edl_record->action == EDL_SKIP) {
2472 mpctx->osd_function = OSD_FFW;
2473 mpctx->abs_seek_pos = 0;
2474 mpctx->rel_seek_secs = next_edl_record->length_sec;
2475 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2476 "[%f], length [%f]\n", next_edl_record->start_sec,
2477 next_edl_record->stop_sec, next_edl_record->length_sec);
2478 edl_decision = 1;
2480 else if (next_edl_record->action == EDL_MUTE) {
2481 mpctx->edl_muted = !mpctx->edl_muted;
2482 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2483 mixer_mute(&mpctx->mixer);
2484 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2485 next_edl_record->start_sec );
2487 next_edl_record = next_edl_record->next;
2492 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2493 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2494 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2495 static int seek(MPContext *mpctx, double amount, int style)
2497 current_module = "seek";
2498 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2499 return -1;
2501 if (mpctx->sh_video) {
2502 current_module = "seek_video_reset";
2503 resync_video_stream(mpctx->sh_video);
2504 if (mpctx->video_out->config_ok)
2505 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2506 mpctx->sh_video->num_buffered_pts = 0;
2507 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2508 mpctx->num_buffered_frames = 0;
2509 mpctx->delay = 0;
2510 // Not all demuxers set d_video->pts during seek, so this value
2511 // (which is used by at least vobsub and edl code below) may
2512 // be completely wrong (probably 0).
2513 mpctx->sh_video->pts = mpctx->d_video->pts;
2514 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2515 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2518 if (mpctx->sh_audio) {
2519 current_module = "seek_audio_reset";
2520 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2521 mpctx->sh_audio->a_buffer_len = 0;
2522 mpctx->sh_audio->a_out_buffer_len = 0;
2525 if (vo_vobsub && mpctx->sh_video) {
2526 current_module = "seek_vobsub_reset";
2527 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2530 edl_seek_reset(mpctx);
2532 c_total = 0;
2533 max_pts_correction = 0.1;
2534 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2535 drop_frame_cnt = 0;
2537 current_module = NULL;
2538 return 0;
2542 static int read_keys(void *ctx, int fd)
2544 getch2(ctx);
2545 return mplayer_get_key(ctx, 0);
2549 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2550 * file for some tools to link against. */
2551 #ifndef DISABLE_MAIN
2552 int main(int argc,char* argv[]){
2555 char * mem_ptr;
2557 // movie info:
2559 /* Flag indicating whether MPlayer should exit without playing anything. */
2560 int opt_exit = 0;
2562 //float a_frame=0; // Audio
2564 int i;
2566 int gui_no_filename=0;
2568 srand((int) time(NULL));
2570 struct MPContext *mpctx = &(struct MPContext){
2571 .osd_function = OSD_PLAY,
2572 .begin_skip = MP_NOPTS_VALUE,
2573 .play_tree_step = 1,
2574 .global_sub_pos = -1,
2575 .set_of_sub_pos = -1,
2576 .file_format = DEMUXER_TYPE_UNKNOWN,
2577 .last_dvb_step = 1,
2580 InitTimer();
2582 mp_msg_init();
2584 #ifdef HAVE_X11
2585 mpctx->x11_state = vo_x11_init_state();
2586 #endif
2587 struct MPOpts *opts = &mpctx->opts;
2588 set_default_mplayer_options(opts);
2589 // Create the config context and register the options
2590 mpctx->mconfig = m_config_new(opts, cfg_include);
2591 m_config_register_options(mpctx->mconfig,mplayer_opts);
2592 mp_input_register_options(mpctx->mconfig);
2594 // Preparse the command line
2595 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2597 print_version();
2598 #if defined(WIN32) && defined(USE_WIN32DLL)
2599 set_path_env();
2600 #endif /*WIN32 && USE_WIN32DLL*/
2602 #ifdef USE_TV
2603 stream_tv_defaults.immediate = 1;
2604 #endif
2606 if (argc > 1 && argv[1] &&
2607 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2608 use_gui = !strcmp(argv[1], "-gui");
2609 } else
2610 if ( argv[0] )
2612 char *base = strrchr(argv[0], '/');
2613 if (!base)
2614 base = strrchr(argv[0], '\\');
2615 if (!base)
2616 base = argv[0];
2617 if(strstr(base, "gmplayer"))
2618 use_gui=1;
2621 parse_cfgfiles(mpctx, mpctx->mconfig);
2623 #ifdef HAVE_NEW_GUI
2624 if ( use_gui ) cfg_read();
2625 #endif
2627 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2628 if(mpctx->playtree == NULL)
2629 opt_exit = 1;
2630 else {
2631 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2632 if(mpctx->playtree) {
2633 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2634 if(mpctx->playtree_iter) {
2635 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2636 play_tree_iter_free(mpctx->playtree_iter);
2637 mpctx->playtree_iter = NULL;
2639 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2643 mpctx->key_fifo = mp_fifo_create(opts);
2645 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2646 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2647 if(runningmplayer && mpctx->filename && use_gui){
2648 COPYDATASTRUCT csData;
2649 char file[MAX_PATH];
2650 char *filepart = mpctx->filename;
2651 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2652 csData.dwData = 0;
2653 csData.cbData = strlen(file)*2;
2654 csData.lpData = file;
2655 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2658 #endif
2660 #ifdef WIN32
2661 // request 1ms timer resolution
2662 timeBeginPeriod(1);
2663 if(proc_priority){
2664 int i;
2665 for(i=0; priority_presets_defs[i].name; i++){
2666 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2667 break;
2669 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2670 priority_presets_defs[i].name);
2671 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2673 #endif
2674 #ifndef HAVE_NEW_GUI
2675 if(use_gui){
2676 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2677 use_gui=0;
2679 #else
2680 #ifndef WIN32
2681 if(use_gui && !vo_init()){
2682 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2683 use_gui=0;
2685 #endif
2686 if (use_gui && mpctx->playtree_iter){
2687 char cwd[PATH_MAX+2];
2688 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2689 play_tree_iter_free(mpctx->playtree_iter);
2690 mpctx->playtree_iter=NULL;
2692 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2694 strcat(cwd, "/");
2695 // Prefix relative paths with current working directory
2696 play_tree_add_bpf(mpctx->playtree, cwd);
2698 // Import initital playtree into GUI.
2699 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2701 #endif /* HAVE_NEW_GUI */
2703 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2704 list_video_out();
2705 opt_exit = 1;
2708 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2709 list_audio_out();
2710 opt_exit = 1;
2713 /* Check codecs.conf. */
2714 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2715 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2716 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2717 if(!parse_codec_cfg(NULL)){
2718 exit_player_with_rc(mpctx, NULL, 0);
2720 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2723 free( mem_ptr ); // release the buffer created by get_path()
2726 #if 0
2727 if(video_codec_list){
2728 int i;
2729 video_codec=video_codec_list[0];
2730 for(i=0;video_codec_list[i];i++)
2731 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2733 #endif
2734 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2735 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2736 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2737 list_codecs(1);
2738 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2739 opt_exit = 1;
2741 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2742 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2743 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2744 list_codecs(0);
2745 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2746 opt_exit = 1;
2748 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2749 vfm_help();
2750 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2751 opt_exit = 1;
2753 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2754 afm_help();
2755 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2756 opt_exit = 1;
2758 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2759 af_help();
2760 printf("\n");
2761 opt_exit = 1;
2763 #ifdef HAVE_X11
2764 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2765 fstype_help();
2766 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2767 opt_exit = 1;
2769 #endif
2770 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2771 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2772 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2773 demuxer_help();
2774 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2775 opt_exit = 1;
2777 if(list_properties) {
2778 property_print_help();
2779 opt_exit = 1;
2782 if(opt_exit)
2783 exit_player(mpctx, NULL);
2785 if (player_idle_mode && use_gui) {
2786 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2787 exit_player_with_rc(mpctx, NULL, 1);
2790 if(!mpctx->filename && !player_idle_mode){
2791 if(!use_gui){
2792 // no file/vcd/dvd -> show HELP:
2793 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2794 exit_player_with_rc(mpctx, NULL, 0);
2795 } else gui_no_filename=1;
2798 /* Display what configure line was used */
2799 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2801 // Many users forget to include command line in bugreports...
2802 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2803 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2804 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2805 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2808 //------ load global data first ------
2810 mpctx->osd = osd_create();
2812 // check font
2813 #ifdef HAVE_FREETYPE
2814 init_freetype();
2815 #endif
2816 #ifdef HAVE_FONTCONFIG
2817 if(font_fontconfig <= 0)
2819 #endif
2820 #ifdef HAVE_BITMAP_FONT
2821 if(font_name){
2822 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2823 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2824 filename_recode(font_name));
2825 } else {
2826 // try default:
2827 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2828 free(mem_ptr); // release the buffer created by get_path()
2829 if(!vo_font)
2830 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2832 if (sub_font_name)
2833 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2834 else
2835 mpctx->osd->sub_font = vo_font;
2836 #endif
2837 #ifdef HAVE_FONTCONFIG
2839 #endif
2841 #ifdef USE_ASS
2842 ass_library = ass_init();
2843 #endif
2845 #ifdef HAVE_RTC
2846 if(!nortc)
2848 // seteuid(0); /* Can't hurt to try to get root here */
2849 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2850 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2851 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2852 else {
2853 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2855 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2856 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2857 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2858 close (rtc_fd);
2859 rtc_fd = -1;
2860 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2861 /* variable only by the root */
2862 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2863 close (rtc_fd);
2864 rtc_fd = -1;
2865 } else
2866 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2869 #ifdef HAVE_NEW_GUI
2870 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2871 // and now ? -- Pontscho
2872 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2873 #endif
2874 if(rtc_fd<0)
2875 #endif /* HAVE_RTC */
2876 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2877 softsleep?"software":timer_name);
2879 #ifdef USE_TERMCAP
2880 if ( !use_gui ) load_termcap(NULL); // load key-codes
2881 #endif
2883 // ========== Init keyboard FIFO (connection to libvo) ============
2885 // Init input system
2886 current_module = "init_input";
2887 mpctx->input = mp_input_init(&opts->input, use_gui);
2888 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2889 if(slave_mode)
2890 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2891 else if(!noconsolecontrols)
2892 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2893 // Set the libstream interrupt callback
2894 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2896 #ifdef HAVE_MENU
2897 if(use_menu) {
2898 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2899 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2900 else {
2901 menu_cfg = get_path("menu.conf");
2902 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2903 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2904 else {
2905 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2906 MPLAYER_CONFDIR "/menu.conf"))
2907 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2908 else {
2909 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2910 use_menu = 0;
2915 #endif
2917 current_module = NULL;
2919 /// Catch signals
2920 #ifndef __MINGW32__
2921 signal(SIGCHLD,child_sighandler);
2922 #endif
2924 #ifdef CRASH_DEBUG
2925 prog_path = argv[0];
2926 #endif
2927 //========= Catch terminate signals: ================
2928 // terminate requests:
2929 signal(SIGTERM,exit_sighandler); // kill
2930 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2932 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2934 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2935 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2936 #ifdef ENABLE_SIGHANDLER
2937 // fatal errors:
2938 signal(SIGBUS,exit_sighandler); // bus error
2939 signal(SIGSEGV,exit_sighandler); // segfault
2940 signal(SIGILL,exit_sighandler); // illegal instruction
2941 signal(SIGFPE,exit_sighandler); // floating point exc.
2942 signal(SIGABRT,exit_sighandler); // abort()
2943 #ifdef CRASH_DEBUG
2944 if (crash_debug)
2945 signal(SIGTRAP,exit_sighandler);
2946 #endif
2947 #endif
2949 #ifdef HAVE_NEW_GUI
2950 if(use_gui){
2951 guiInit();
2952 guiGetEvent(guiSetContext, mpctx);
2953 mpctx->initialized_flags|=INITIALIZED_GUI;
2954 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2956 #endif
2958 // ******************* Now, let's see the per-file stuff ********************
2960 play_next_file:
2962 // init global sub numbers
2963 mpctx->global_sub_size = 0;
2964 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2966 if (mpctx->filename) {
2967 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2968 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2969 load_per_file_config (mpctx->mconfig, mpctx->filename);
2972 if (opts->video_driver_list)
2973 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2974 if (opts->audio_driver_list)
2975 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2977 // We must enable getch2 here to be able to interrupt network connection
2978 // or cache filling
2979 if(!noconsolecontrols && !slave_mode){
2980 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2981 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2982 else
2983 getch2_enable(); // prepare stdin for hotkeys...
2984 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2985 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2988 // =================== GUI idle loop (STOP state) ===========================
2989 #ifdef HAVE_NEW_GUI
2990 if ( use_gui ) {
2991 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2992 guiGetEvent( guiSetDefaults,0 );
2993 while ( guiIntfStruct.Playing != 1 )
2995 mp_cmd_t* cmd;
2996 usec_sleep(20000);
2997 guiEventHandling();
2998 guiGetEvent( guiReDraw,NULL );
2999 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3000 guiGetEvent(guiIEvent, (char *)cmd->id);
3001 mp_cmd_free(cmd);
3004 guiGetEvent( guiSetParameters,NULL );
3005 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3007 play_tree_t * entry = play_tree_new();
3008 play_tree_add_file( entry,guiIntfStruct.Filename );
3009 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3010 else mpctx->playtree=play_tree_new();
3011 play_tree_set_child( mpctx->playtree,entry );
3012 if(mpctx->playtree)
3014 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3015 if(mpctx->playtree_iter)
3017 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3019 play_tree_iter_free(mpctx->playtree_iter);
3020 mpctx->playtree_iter = NULL;
3022 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3027 #endif /* HAVE_NEW_GUI */
3029 while (player_idle_mode && !mpctx->filename) {
3030 play_tree_t * entry = NULL;
3031 mp_cmd_t * cmd;
3032 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3033 if (mpctx->video_out)
3034 vo_check_events(mpctx->video_out);
3035 usec_sleep(20000);
3037 switch (cmd->id) {
3038 case MP_CMD_LOADFILE:
3039 // prepare a tree entry with the new filename
3040 entry = play_tree_new();
3041 play_tree_add_file(entry, cmd->args[0].v.s);
3042 // The entry is added to the main playtree after the switch().
3043 break;
3044 case MP_CMD_LOADLIST:
3045 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3046 break;
3047 case MP_CMD_QUIT:
3048 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3049 break;
3050 case MP_CMD_GET_PROPERTY:
3051 case MP_CMD_SET_PROPERTY:
3052 case MP_CMD_STEP_PROPERTY:
3053 run_command(mpctx, cmd);
3054 break;
3057 mp_cmd_free(cmd);
3059 if (entry) { // user entered a command that gave a valid entry
3060 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3061 play_tree_free_list(mpctx->playtree->child, 1);
3062 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3064 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3066 play_tree_set_child(mpctx->playtree, entry);
3068 /* Make iterator start at the top the of tree. */
3069 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3070 if (!mpctx->playtree_iter) continue;
3072 // find the first real item in the tree
3073 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3074 // no items!
3075 play_tree_iter_free(mpctx->playtree_iter);
3076 mpctx->playtree_iter = NULL;
3077 continue; // wait for next command
3079 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3082 //---------------------------------------------------------------------------
3084 if(mpctx->filename)
3085 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3086 filename_recode(mpctx->filename));
3088 if (edl_filename) {
3089 if (edl_records) free_edl(edl_records);
3090 next_edl_record = edl_records = edl_parse_file();
3092 if (edl_output_filename) {
3093 if (edl_fd) fclose(edl_fd);
3094 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3096 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3097 filename_recode(edl_output_filename));
3101 //==================== Open VOB-Sub ============================
3103 current_module="vobsub";
3104 if (vobsub_name){
3105 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3106 if(vo_vobsub==NULL)
3107 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3108 filename_recode(vobsub_name));
3109 } else if (sub_auto && mpctx->filename){
3110 /* try to autodetect vobsub from movie filename ::atmos */
3111 char *buf = strdup(mpctx->filename), *psub;
3112 char *pdot = strrchr(buf, '.');
3113 char *pslash = strrchr(buf, '/');
3114 #ifdef WIN32
3115 if (!pslash) pslash = strrchr(buf, '\\');
3116 #endif
3117 if (pdot && (!pslash || pdot > pslash))
3118 *pdot = '\0';
3119 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3120 /* try from ~/.mplayer/sub */
3121 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3122 char *bname;
3123 int l;
3124 bname = strrchr(buf,'/');
3125 #ifdef WIN32
3126 if(!bname) bname = strrchr(buf,'\\');
3127 #endif
3128 if(bname) bname++;
3129 else bname = buf;
3130 l = strlen(psub) + strlen(bname) + 1;
3131 psub = realloc(psub,l);
3132 strcat(psub,bname);
3133 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3134 free(psub);
3136 free(buf);
3138 if(vo_vobsub){
3139 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3140 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3141 // check if vobsub requested only to display forced subtitles
3142 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
3144 // setup global sub numbering
3145 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3146 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3149 //============ Open & Sync STREAM --- fork cache2 ====================
3151 mpctx->stream=NULL;
3152 mpctx->demuxer=NULL;
3153 if (mpctx->d_audio) {
3154 //free_demuxer_stream(mpctx->d_audio);
3155 mpctx->d_audio=NULL;
3157 if (mpctx->d_video) {
3158 //free_demuxer_stream(d_video);
3159 mpctx->d_video=NULL;
3161 mpctx->sh_audio=NULL;
3162 mpctx->sh_video=NULL;
3164 current_module="open_stream";
3165 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3166 if(!mpctx->stream) { // error...
3167 mpctx->eof = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3168 goto goto_next_file;
3170 mpctx->initialized_flags|=INITIALIZED_STREAM;
3172 #ifdef HAVE_NEW_GUI
3173 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3174 #endif
3176 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3177 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3178 "playlist support will not be used automatically.\n"
3179 "MPlayer's playlist code is unsafe and should only be used with "
3180 "trusted sources.\nPlayback will probably fail.\n\n");
3181 #if 0
3182 play_tree_t* entry;
3183 // Handle playlist
3184 current_module="handle_playlist";
3185 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3186 filename_recode(mpctx->filename));
3187 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3188 mpctx->eof=playtree_add_playlist(mpctx, entry);
3189 goto goto_next_file;
3190 #endif
3192 mpctx->stream->start_pos+=seek_to_byte;
3194 if(stream_dump_type==5){
3195 unsigned char buf[4096];
3196 int len;
3197 FILE *f;
3198 current_module="dumpstream";
3199 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3200 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3201 exit_player(mpctx, MSGTR_Exit_error);
3203 stream_reset(mpctx->stream);
3204 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3205 f=fopen(stream_dump_name,"wb");
3206 if(!f){
3207 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3208 exit_player(mpctx, MSGTR_Exit_error);
3210 if (dvd_chapter > 1) {
3211 int chapter = dvd_chapter - 1;
3212 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3214 while(!mpctx->stream->eof && !async_quit_request){
3215 len=stream_read(mpctx->stream,buf,4096);
3216 if(len>0) {
3217 if(fwrite(buf,len,1,f) != 1) {
3218 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3219 exit_player(mpctx, MSGTR_Exit_error);
3223 if(fclose(f)) {
3224 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3225 exit_player(mpctx, MSGTR_Exit_error);
3227 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3228 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3231 #ifdef USE_DVDREAD
3232 if(mpctx->stream->type==STREAMTYPE_DVD){
3233 current_module="dvd lang->id";
3234 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3235 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3236 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3237 // setup global sub numbering
3238 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3239 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3240 current_module=NULL;
3242 #endif
3244 #ifdef USE_DVDNAV
3245 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3246 current_module="dvdnav lang->id";
3247 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3248 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3249 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3250 // setup global sub numbering
3251 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3252 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3253 current_module=NULL;
3255 #endif
3257 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3258 goto_enable_cache:
3259 if(stream_cache_size>0){
3260 current_module="enable_cache";
3261 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3262 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3263 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3264 if((mpctx->eof = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3267 //============ Open DEMUXERS --- DETECT file type =======================
3268 current_module="demux_open";
3270 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3272 // HACK to get MOV Reference Files working
3274 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3276 unsigned char* playlist_entry;
3277 play_tree_t *list = NULL, *entry = NULL;
3279 current_module="handle_demux_playlist";
3280 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3282 char *temp, *bname;
3284 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3285 filename_recode(playlist_entry));
3287 bname=mp_basename(playlist_entry);
3288 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3289 continue;
3291 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3292 continue;
3294 entry = play_tree_new();
3296 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3298 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3299 if (temp)
3301 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3302 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3303 strcat(temp, playlist_entry);
3304 play_tree_add_file(entry,temp);
3305 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3306 free(temp);
3309 else
3310 play_tree_add_file(entry,playlist_entry);
3312 if(!list)
3313 list = entry;
3314 else
3315 play_tree_append_entry(list,entry);
3317 free_demuxer(mpctx->demuxer);
3318 mpctx->demuxer = NULL;
3320 if (list)
3322 entry = play_tree_new();
3323 play_tree_set_child(entry,list);
3324 mpctx->eof=playtree_add_playlist(mpctx, entry);
3325 goto goto_next_file;
3329 if(!mpctx->demuxer)
3330 goto goto_next_file;
3331 if(dvd_chapter>1) {
3332 float pts;
3333 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3334 seek(mpctx, pts, SEEK_ABSOLUTE);
3337 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3339 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3340 int i;
3341 int maxid = -1;
3342 // setup global sub numbering
3343 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3344 for (i = 0; i < MAX_S_STREAMS; i++)
3345 if (mpctx->demuxer->s_streams[i])
3346 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3347 mpctx->global_sub_size += maxid + 1;
3349 // Make opts->sub_id always selectable if set.
3350 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3351 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3353 #ifdef USE_ASS
3354 if (ass_enabled && ass_library) {
3355 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3356 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3357 if (extract_embedded_fonts &&
3358 att->name && att->type && att->data && att->data_size &&
3359 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3360 strcmp(att->type, "application/x-font") == 0))
3361 ass_add_font(ass_library, att->name, att->data, att->data_size);
3364 #endif
3366 current_module="demux_open2";
3368 //file_format=demuxer->file_format;
3370 mpctx->d_audio=mpctx->demuxer->audio;
3371 mpctx->d_video=mpctx->demuxer->video;
3372 mpctx->d_sub=mpctx->demuxer->sub;
3374 // select audio stream
3375 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3377 // DUMP STREAMS:
3378 if((stream_dump_type)&&(stream_dump_type<4)){
3379 FILE *f;
3380 demux_stream_t *ds=NULL;
3381 current_module="dump";
3382 // select stream to dump
3383 switch(stream_dump_type){
3384 case 1: ds=mpctx->d_audio;break;
3385 case 2: ds=mpctx->d_video;break;
3386 case 3: ds=mpctx->d_sub;break;
3388 if(!ds){
3389 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3390 exit_player(mpctx, MSGTR_Exit_error);
3392 // disable other streams:
3393 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3394 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3395 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3396 // let's dump it!
3397 f=fopen(stream_dump_name,"wb");
3398 if(!f){
3399 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3400 exit_player(mpctx, MSGTR_Exit_error);
3402 while(!ds->eof){
3403 unsigned char* start;
3404 int in_size=ds_get_packet(ds,&start);
3405 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3406 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3407 if(in_size>0) fwrite(start,in_size,1,f);
3409 fclose(f);
3410 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3411 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3414 mpctx->sh_audio=mpctx->d_audio->sh;
3415 mpctx->sh_video=mpctx->d_video->sh;
3417 if(mpctx->sh_video){
3419 current_module="video_read_properties";
3420 if(!video_read_properties(mpctx->sh_video)) {
3421 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3422 mpctx->sh_video=mpctx->d_video->sh=NULL;
3423 } else {
3424 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3425 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3426 mpctx->sh_video->fps,mpctx->sh_video->frametime
3429 /* need to set fps here for output encoders to pick it up in their init */
3430 if(force_fps){
3431 mpctx->sh_video->fps=force_fps;
3432 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3434 vo_fps = mpctx->sh_video->fps;
3436 if(!mpctx->sh_video->fps && !force_fps){
3437 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3438 mpctx->sh_video=mpctx->d_video->sh=NULL;
3444 if(!mpctx->sh_video && !mpctx->sh_audio){
3445 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3446 #ifdef HAS_DVBIN_SUPPORT
3447 if(mpctx->stream->type == STREAMTYPE_DVB)
3449 int dir;
3450 int v = mpctx->last_dvb_step;
3451 if(v > 0)
3452 dir = DVB_CHANNEL_HIGHER;
3453 else
3454 dir = DVB_CHANNEL_LOWER;
3456 if(dvb_step_channel(mpctx->stream, dir))
3457 mpctx->eof = mpctx->dvbin_reopen = 1;
3459 #endif
3460 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3463 /* display clip info */
3464 demux_info_print(mpctx->demuxer);
3466 //================== Read SUBTITLES (DVD & TEXT) ==========================
3467 if(vo_spudec==NULL && mpctx->sh_video &&
3468 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3469 init_vo_spudec(mpctx);
3472 // Apply current settings for forced subs
3473 if (vo_spudec!=NULL)
3474 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3476 if(mpctx->sh_video) {
3477 // after reading video params we should load subtitles because
3478 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3479 // check .sub
3480 current_module="read_subtitles_file";
3481 if(sub_name){
3482 for (i = 0; sub_name[i] != NULL; ++i)
3483 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3485 if(sub_auto) { // auto load sub file ...
3486 char *psub = get_path( "sub/" );
3487 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3488 int i = 0;
3489 free(psub); // release the buffer created by get_path() above
3490 while (tmp[i]) {
3491 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3492 free(tmp[i++]);
3494 free(tmp);
3496 if (mpctx->set_of_sub_size > 0) {
3497 // setup global sub numbering
3498 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3499 mpctx->global_sub_size += mpctx->set_of_sub_size;
3503 if (mpctx->global_sub_size) {
3504 // find the best sub to use
3505 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3506 mpctx->global_sub_pos = -1; // no subs by default
3507 if (vobsub_index_id >= 0) {
3508 // if user asks for a vobsub id, use that first.
3509 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3510 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3511 // if user asks for a dvd sub id, use that next.
3512 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3513 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3514 // if there are text subs to use, use those. (autosubs come last here)
3515 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3516 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3517 // finally select subs by language and container hints
3518 if (opts->sub_id < 0 && dvdsub_lang)
3519 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3520 if (opts->sub_id < 0)
3521 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3522 if (opts->sub_id >= 0)
3523 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3525 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3526 mpctx->global_sub_pos--;
3527 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3528 if(subdata)
3529 switch (stream_dump_type) {
3530 case 3: list_sub_file(subdata); break;
3531 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3532 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3533 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3534 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3535 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3539 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3540 filename_recode(mpctx->filename));
3541 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3542 if (mpctx->sh_video) {
3543 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3544 if (mpctx->sh_video->format >= 0x20202020)
3545 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3546 else
3547 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3548 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3549 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3550 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3551 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3552 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3554 if (mpctx->sh_audio) {
3555 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3556 if (mpctx->sh_audio->format >= 0x20202020)
3557 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3558 else
3559 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3560 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3561 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3562 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3564 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3565 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3567 if(!mpctx->sh_video) goto main; // audio-only
3569 if(!reinit_video_chain(mpctx)) {
3570 if(!mpctx->sh_video){
3571 if(!mpctx->sh_audio) goto goto_next_file;
3572 goto main; // exit_player(MSGTR_Exit_error);
3576 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3577 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3579 #ifdef HAVE_FREETYPE
3580 force_load_font = 1;
3581 #endif
3583 //================== MAIN: ==========================
3584 main:
3585 current_module="main";
3587 if(playing_msg) {
3588 char* msg = property_expand_string(mpctx, playing_msg);
3589 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3590 free(msg);
3594 // Disable the term OSD in verbose mode
3595 if(verbose) term_osd = 0;
3598 //int frame_corr_num=0; //
3599 //float v_frame=0; // Video
3600 float time_frame=0; // Timer
3601 //float num_frames=0; // number of frames played
3603 int frame_time_remaining=0; // flag
3604 int blit_frame=0;
3605 mpctx->num_buffered_frames=0;
3607 // Make sure old OSD does not stay around,
3608 // e.g. with -fixed-vo and same-resolution files
3609 clear_osd_msgs();
3610 update_osd_msg(mpctx);
3612 //================ SETUP AUDIO ==========================
3614 if(mpctx->sh_audio){
3615 reinit_audio_chain(mpctx);
3616 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3617 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3620 current_module="av_init";
3622 if(mpctx->sh_video){
3623 mpctx->sh_video->timer=0;
3624 if (! ignore_start)
3625 audio_delay += mpctx->sh_video->stream_delay;
3627 if(mpctx->sh_audio){
3628 if (! ignore_start)
3629 audio_delay -= mpctx->sh_audio->stream_delay;
3630 mpctx->delay=-audio_delay;
3633 if(!mpctx->sh_audio){
3634 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3635 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3636 ds_free_packs(mpctx->d_audio); // free buffered chunks
3637 //mpctx->d_audio->id=-2; // do not read audio chunks
3638 //uninit_player(mpctx, INITIALIZED_AO); // close device
3640 if(!mpctx->sh_video){
3641 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3642 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3643 ds_free_packs(mpctx->d_video);
3644 mpctx->d_video->id=-2;
3645 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3648 if (!mpctx->sh_video && !mpctx->sh_audio)
3649 goto goto_next_file;
3651 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3652 if(force_fps && mpctx->sh_video){
3653 vo_fps = mpctx->sh_video->fps=force_fps;
3654 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3655 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3658 #ifdef HAVE_NEW_GUI
3659 if ( use_gui ) {
3660 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3661 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3662 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3663 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3664 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3666 #endif
3668 mp_input_set_section(mpctx->input, NULL);
3669 //TODO: add desired (stream-based) sections here
3670 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3671 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3673 //==================== START PLAYING =======================
3675 if(opts->loop_times>1) opts->loop_times--; else
3676 if(opts->loop_times==1) opts->loop_times = -1;
3678 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3680 total_time_usage_start=GetTimer();
3681 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3682 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3683 play_n_frames=play_n_frames_mf;
3685 if(play_n_frames==0){
3686 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3689 if (seek_to_sec) {
3690 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3691 end_at.pos += seek_to_sec;
3694 if (end_at.type == END_AT_SIZE) {
3695 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3696 end_at.type = END_AT_NONE;
3699 #ifdef USE_DVDNAV
3700 mp_dvdnav_context_free(mpctx);
3701 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3702 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3703 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3705 #endif
3707 get_relative_time(mpctx); // reset current delta
3709 while(!mpctx->eof){
3710 float aq_sleep_time=0;
3712 if(dvd_last_chapter>0) {
3713 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3714 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3715 goto goto_next_file;
3718 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3719 mpctx->sh_audio = mpctx->d_audio->sh;
3720 mpctx->sh_audio->ds = mpctx->d_audio;
3721 reinit_audio_chain(mpctx);
3724 /*========================== PLAY AUDIO ============================*/
3726 if (mpctx->sh_audio)
3727 if (!fill_audio_out_buffers(mpctx))
3728 // at eof, all audio at least written to ao
3729 if (!mpctx->sh_video)
3730 mpctx->eof = PT_NEXT_ENTRY;
3733 if(!mpctx->sh_video) {
3734 // handle audio-only case:
3735 double a_pos=0;
3736 // sh_audio can be NULL due to video stream switching
3737 // TODO: handle this better
3738 if(!quiet || end_at.type == END_AT_TIME && mpctx->sh_audio)
3739 a_pos = playing_audio_pts(mpctx);
3741 if(!quiet)
3742 print_status(mpctx, a_pos, 0, 0);
3744 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3745 mpctx->eof = PT_NEXT_ENTRY;
3746 update_osd_msg(mpctx);
3748 } else {
3750 /*========================== PLAY VIDEO ============================*/
3752 vo_pts=mpctx->sh_video->timer*90000.0;
3753 vo_fps=mpctx->sh_video->fps;
3755 if (!mpctx->num_buffered_frames) {
3756 double frame_time = update_video(mpctx, &blit_frame);
3757 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3758 if (mpctx->sh_video->vf_initialized < 0) {
3759 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3760 mpctx->eof = 1; goto goto_next_file;
3762 if (frame_time < 0)
3763 mpctx->eof = 1;
3764 else {
3765 // might return with !eof && !blit_frame if !correct_pts
3766 mpctx->num_buffered_frames += blit_frame;
3767 time_frame += frame_time / opts->playback_speed; // for nosound
3771 // ==========================================================================
3773 // current_module="draw_osd";
3774 // if(vo_config_count) mpctx->video_out->draw_osd();
3776 #ifdef HAVE_NEW_GUI
3777 if(use_gui) guiEventHandling();
3778 #endif
3780 current_module="vo_check_events";
3781 vo_check_events(mpctx->video_out);
3783 #ifdef HAVE_X11
3784 if (stop_xscreensaver) {
3785 current_module = "stop_xscreensaver";
3786 xscreensaver_heartbeat(mpctx->x11_state);
3788 #endif
3789 if (heartbeat_cmd) {
3790 static unsigned last_heartbeat;
3791 unsigned now = GetTimerMS();
3792 if (now - last_heartbeat > 30000) {
3793 last_heartbeat = now;
3794 system(heartbeat_cmd);
3798 frame_time_remaining = sleep_until_update(mpctx, &time_frame, &aq_sleep_time);
3800 //====================== FLIP PAGE (VIDEO BLT): =========================
3802 current_module="flip_page";
3803 if (!frame_time_remaining && blit_frame) {
3804 unsigned int t2=GetTimer();
3806 vo_flip_page(mpctx->video_out);
3807 mpctx->num_buffered_frames--;
3809 vout_time_usage += (GetTimer() - t2) * 0.000001;
3811 //====================== A-V TIMESTAMP CORRECTION: =========================
3813 adjust_sync_and_print_status(mpctx, frame_time_remaining, time_frame);
3815 //============================ Auto QUALITY ============================
3817 /*Output quality adjustments:*/
3818 if(auto_quality>0){
3819 current_module="autoq";
3820 // float total=0.000001f * (GetTimer()-aq_total_time);
3821 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3822 if(output_quality<auto_quality && aq_sleep_time>0)
3823 ++output_quality;
3824 else
3825 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3826 if(output_quality>1 && aq_sleep_time<0)
3827 --output_quality;
3828 else
3829 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3830 output_quality=0;
3831 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3832 set_video_quality(mpctx->sh_video,output_quality);
3835 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3836 --play_n_frames;
3837 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3841 // FIXME: add size based support for -endpos
3842 if (end_at.type == END_AT_TIME &&
3843 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3844 mpctx->eof = PT_NEXT_ENTRY;
3846 } // end if(mpctx->sh_video)
3848 #ifdef USE_DVDNAV
3849 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3850 nav_highlight_t hl;
3851 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3852 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3853 vo_osd_changed (OSDTYPE_DVDNAV);
3855 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3856 double ar = -1.0;
3857 if (stream_control (mpctx->demuxer->stream,
3858 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3859 != STREAM_UNSUPPORTED)
3860 mpctx->sh_video->stream_aspect = ar;
3863 #endif
3865 //============================ Handle PAUSE ===============================
3867 current_module="pause";
3869 if (mpctx->osd_function == OSD_PAUSE) {
3870 pause_loop(mpctx);
3871 mpctx->was_paused = 1;
3874 // handle -sstep
3875 if(step_sec>0) {
3876 mpctx->osd_function=OSD_FFW;
3877 mpctx->rel_seek_secs+=step_sec;
3880 edl_update(mpctx);
3882 //================= Keyboard events, SEEKing ====================
3884 current_module="key_events";
3887 mp_cmd_t* cmd;
3888 int brk_cmd = 0;
3889 while( !brk_cmd && (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3890 brk_cmd = run_command(mpctx, cmd);
3891 mp_cmd_free(cmd);
3892 if (brk_cmd == 2)
3893 goto goto_enable_cache;
3896 mpctx->was_paused = 0;
3898 /* Looping. */
3899 if(mpctx->eof==1 && opts->loop_times>=0) {
3900 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", opts->loop_times,mpctx->eof);
3902 if(opts->loop_times>1) opts->loop_times--; else
3903 if(opts->loop_times==1) opts->loop_times=-1;
3904 play_n_frames=play_n_frames_mf;
3905 mpctx->eof=0;
3906 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3907 loop_seek = 1;
3910 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3911 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3912 // Set OSD:
3913 if(!loop_seek){
3914 if( !edl_decision )
3915 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3919 mpctx->rel_seek_secs=0;
3920 mpctx->abs_seek_pos=0;
3921 loop_seek=0;
3922 edl_decision = 0;
3925 #ifdef HAVE_NEW_GUI
3926 if(use_gui){
3927 guiEventHandling();
3928 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3929 // get pos from frame number / total frames
3930 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3931 } else {
3932 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3934 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3935 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3936 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3937 guiGetEvent( guiReDraw,NULL );
3938 guiGetEvent( guiSetVolume,NULL );
3939 if(guiIntfStruct.Playing==0) break; // STOP
3940 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3941 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3942 #ifdef USE_DVDREAD
3943 if ( mpctx->stream->type == STREAMTYPE_DVD )
3945 dvd_priv_t * dvdp = mpctx->stream->priv;
3946 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3948 #endif
3950 #endif /* HAVE_NEW_GUI */
3952 } // while(!mpctx->eof)
3954 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3956 #ifdef HAS_DVBIN_SUPPORT
3957 if(mpctx->dvbin_reopen)
3959 mpctx->eof = 0;
3960 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3961 cache_uninit(mpctx->stream);
3962 mpctx->dvbin_reopen = 0;
3963 goto goto_enable_cache;
3965 #endif
3968 goto_next_file: // don't jump here after ao/vo/getch initialization!
3970 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3972 if(benchmark){
3973 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3974 double total_time_usage;
3975 total_time_usage_start=GetTimer()-total_time_usage_start;
3976 total_time_usage = (float)total_time_usage_start*0.000001;
3977 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3978 video_time_usage,vout_time_usage,audio_time_usage,
3979 total_time_usage-tot,total_time_usage);
3980 if(total_time_usage>0.0)
3981 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3982 100.0*video_time_usage/total_time_usage,
3983 100.0*vout_time_usage/total_time_usage,
3984 100.0*audio_time_usage/total_time_usage,
3985 100.0*(total_time_usage-tot)/total_time_usage,
3986 100.0);
3987 if(total_frame_cnt && frame_dropping)
3988 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3989 total_frame_cnt-drop_frame_cnt,
3990 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3991 drop_frame_cnt,
3992 100*drop_frame_cnt/total_frame_cnt,
3993 total_frame_cnt,
3994 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3997 // time to uninit all, except global stuff:
3998 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4000 if(mpctx->set_of_sub_size > 0) {
4001 current_module="sub_free";
4002 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4003 sub_free(mpctx->set_of_subtitles[i]);
4004 #ifdef USE_ASS
4005 if(mpctx->set_of_ass_tracks[i])
4006 ass_free_track( mpctx->set_of_ass_tracks[i] );
4007 #endif
4009 mpctx->set_of_sub_size = 0;
4011 vo_sub_last = vo_sub=NULL;
4012 subdata=NULL;
4013 #ifdef USE_ASS
4014 ass_track = NULL;
4015 if(ass_library)
4016 ass_clear_fonts(ass_library);
4017 #endif
4019 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
4020 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
4021 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
4022 mpctx->eof = 1;
4023 } else {
4024 play_tree_iter_free(mpctx->playtree_iter);
4025 mpctx->playtree_iter = NULL;
4027 mpctx->play_tree_step = 1;
4028 } else if(mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
4029 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
4030 if(mpctx->playtree_iter) {
4031 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
4032 mpctx->eof = 1;
4033 } else {
4034 play_tree_iter_free(mpctx->playtree_iter);
4035 mpctx->playtree_iter = NULL;
4038 } else if (mpctx->eof == PT_STOP) {
4039 play_tree_iter_free(mpctx->playtree_iter);
4040 mpctx->playtree_iter = NULL;
4041 } else { // NEXT PREV SRC
4042 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
4045 if(mpctx->eof == 0) mpctx->eof = 1;
4047 while(mpctx->playtree_iter != NULL) {
4048 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
4049 if(mpctx->filename == NULL) {
4050 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
4051 play_tree_iter_free(mpctx->playtree_iter);
4052 mpctx->playtree_iter = NULL;
4054 } else
4055 break;
4058 #ifdef HAVE_NEW_GUI
4059 if(use_gui && !mpctx->playtree_iter) {
4060 #ifdef USE_DVDREAD
4061 if(!guiIntfStruct.DiskChanged)
4062 #endif
4063 mplEnd();
4065 #endif
4067 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4068 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4069 mpctx->eof = 0;
4070 goto play_next_file;
4074 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
4076 return 1;
4078 #endif /* DISABLE_MAIN */