Merge svn changes up to r28204
[mplayer/glamo.git] / mplayer.c
blobcaa96b143eebcba958b67c7123970f0dc55976db
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
8 #include "talloc.h"
10 #if defined(__MINGW32__) || defined(__CYGWIN__)
11 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
12 #include <windows.h>
13 #endif
14 #include <string.h>
15 #include <unistd.h>
17 // #include <sys/mman.h>
18 #include <sys/types.h>
19 #ifndef __MINGW32__
20 #include <sys/ioctl.h>
21 #include <sys/wait.h>
22 #else
23 #define SIGHUP 1 /* hangup */
24 #define SIGQUIT 3 /* quit */
25 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
26 #define SIGBUS 10 /* bus error */
27 #define SIGPIPE 13 /* broken pipe */
28 #endif
30 #include <sys/time.h>
31 #include <sys/stat.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include <errno.h>
40 #include "version.h"
42 #include "mp_msg.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "mplayer.h"
50 #include "access_mpcontext.h"
51 #include "m_property.h"
53 #include "cfg-mplayer-def.h"
55 #include "libavutil/intreadwrite.h"
57 #include "subreader.h"
59 #include "mp_osd.h"
60 #include "libvo/video_out.h"
62 #include "libvo/font_load.h"
63 #include "libvo/sub.h"
65 #ifdef CONFIG_X11
66 #include "libvo/x11_common.h"
67 #endif
69 #include "libao2/audio_out.h"
71 #include "codec-cfg.h"
73 #include "edl.h"
75 #include "spudec.h"
76 #include "vobsub.h"
78 #include "osdep/getch2.h"
79 #include "osdep/timer.h"
81 #include "cpudetect.h"
83 #ifdef CONFIG_GUI
84 #include "gui/interface.h"
85 #endif
87 #include "input/input.h"
89 int slave_mode=0;
90 int player_idle_mode=0;
91 int quiet=0;
92 int enable_mouse_movements=0;
93 float start_volume = -1;
95 #if defined(__MINGW32__) || defined(__CYGWIN__)
96 char * proc_priority=NULL;
97 #endif
99 char *heartbeat_cmd;
101 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
103 #ifdef HAVE_RTC
104 #ifdef __linux__
105 #include <linux/rtc.h>
106 #else
107 #include <rtc.h>
108 #define RTC_IRQP_SET RTCIO_IRQP_SET
109 #define RTC_PIE_ON RTCIO_PIE_ON
110 #endif /* __linux__ */
111 #endif /* HAVE_RTC */
113 #ifdef CONFIG_TV
114 #include "stream/tv.h"
115 #endif
116 #ifdef CONFIG_RADIO
117 #include "stream/stream_radio.h"
118 #endif
120 #ifdef CONFIG_DVBIN
121 #include "stream/dvbin.h"
122 #include "stream/cache2.h"
123 #endif
125 //**************************************************************************//
126 // Playtree
127 //**************************************************************************//
128 #include "playtree.h"
129 #include "playtreeparser.h"
131 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
132 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
134 //**************************************************************************//
135 // Config
136 //**************************************************************************//
137 #include "parser-cfg.h"
138 #include "parser-mpcmd.h"
140 //**************************************************************************//
141 // Config file
142 //**************************************************************************//
144 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
146 #include "get_path.h"
148 //**************************************************************************//
149 //**************************************************************************//
150 // Input media streaming & demultiplexer:
151 //**************************************************************************//
153 static int max_framesize=0;
155 #include "stream/stream.h"
156 #include "libmpdemux/demuxer.h"
157 #include "libmpdemux/stheader.h"
159 #ifdef CONFIG_DVDREAD
160 #include "stream/stream_dvd.h"
161 #endif
163 #ifdef CONFIG_DVDNAV
164 #include "stream/stream_dvdnav.h"
165 #endif
167 #include "libmpcodecs/dec_audio.h"
168 #include "libmpcodecs/dec_video.h"
169 #include "libmpcodecs/mp_image.h"
170 #include "libmpcodecs/vf.h"
171 #include "libmpcodecs/vd.h"
173 #include "mixer.h"
175 #include "mp_core.h"
176 #include "options.h"
177 #include "defaultopts.h"
179 //**************************************************************************//
180 //**************************************************************************//
182 // Common FIFO functions, and keyboard/event FIFO code
183 #include "mp_fifo.h"
184 int noconsolecontrols=0;
185 //**************************************************************************//
187 // benchmark:
188 double video_time_usage=0;
189 double vout_time_usage=0;
190 static double audio_time_usage=0;
191 static int total_time_usage_start=0;
192 static int total_frame_cnt=0;
193 static int drop_frame_cnt=0; // total number of dropped frames
194 int benchmark=0;
196 // options:
197 int auto_quality=0;
198 static int output_quality=0;
200 int use_gui=0;
202 #ifdef CONFIG_GUI
203 int enqueue=0;
204 #endif
206 static int list_properties = 0;
208 int osd_level=1;
209 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
210 unsigned int osd_visible;
211 int osd_duration = 1000;
213 int term_osd = 1;
214 static char* term_osd_esc = "\x1b[A\r\x1b[K";
215 static char* playing_msg = NULL;
216 // seek:
217 static double seek_to_sec;
218 static off_t seek_to_byte=0;
219 static off_t step_sec=0;
220 static int loop_seek=0;
222 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
224 // A/V sync:
225 int autosync=0; // 30 might be a good default value.
227 // codecs:
228 char **audio_codec_list=NULL; // override audio codec
229 char **video_codec_list=NULL; // override video codec
230 char **audio_fm_list=NULL; // override audio codec family
231 char **video_fm_list=NULL; // override video codec family
233 // demuxer:
234 extern char *demuxer_name; // override demuxer
235 extern char *audio_demuxer_name; // override audio demuxer
236 extern char *sub_demuxer_name; // override sub demuxer
238 int vobsub_id=-1;
239 char* audio_lang=NULL;
240 char* dvdsub_lang=NULL;
241 static char* spudec_ifo=NULL;
242 int forced_subs_only=0;
243 int file_filter=1;
245 // cache2:
246 int stream_cache_size=-1;
247 #ifdef CONFIG_STREAM_CACHE
248 extern int cache_fill_status;
250 float stream_cache_min_percent=20.0;
251 float stream_cache_seek_min_percent=50.0;
252 #else
253 #define cache_fill_status 0
254 #endif
256 // dump:
257 static char *stream_dump_name="stream.dump";
258 int stream_dump_type=0;
260 // A-V sync:
261 static float default_max_pts_correction=-1;//0.01f;
262 static float max_pts_correction=0;//default_max_pts_correction;
263 static float c_total=0;
264 float audio_delay=0;
265 static int ignore_start=0;
267 static int softsleep=0;
269 double force_fps=0;
270 static int force_srate=0;
271 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
272 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
273 static int play_n_frames=-1;
274 static int play_n_frames_mf=-1;
276 // sub:
277 char *font_name=NULL;
278 char *sub_font_name=NULL;
279 extern int font_fontconfig;
280 float font_factor=0.75;
281 char **sub_name=NULL;
282 float sub_delay=0;
283 float sub_fps=0;
284 int sub_auto = 1;
285 char *vobsub_name=NULL;
286 /*DSP!!char *dsp=NULL;*/
287 int subcc_enabled=0;
288 int suboverlap_enabled = 1;
290 #ifdef CONFIG_ASS
291 #include "libass/ass.h"
292 #include "libass/ass_mp.h"
293 #endif
295 char* current_module=NULL; // for debugging
298 // ---
300 #ifdef CONFIG_MENU
301 #include "m_struct.h"
302 #include "libmenu/menu.h"
303 void vf_menu_pause_update(struct vf_instance* vf);
304 extern vf_info_t vf_info_menu;
305 static const vf_info_t* const libmenu_vfs[] = {
306 &vf_info_menu,
307 NULL
309 static vf_instance_t* vf_menu = NULL;
310 int use_menu = 0;
311 static char* menu_cfg = NULL;
312 static char* menu_root = "main";
313 #endif
316 #ifdef HAVE_RTC
317 static int nortc = 1;
318 static char* rtc_device;
319 #endif
321 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
322 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
323 short edl_decision = 0; ///< 1 when an EDL operation has been made.
324 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
325 int use_filedir_conf;
327 #include "mpcommon.h"
328 #include "command.h"
330 #include "metadata.h"
332 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
334 const void *mpctx_get_video_out(MPContext *mpctx)
336 return mpctx->video_out;
339 const void *mpctx_get_audio_out(MPContext *mpctx)
341 return mpctx->audio_out;
344 void *mpctx_get_demuxer(MPContext *mpctx)
346 return mpctx->demuxer;
349 void *mpctx_get_playtree_iter(MPContext *mpctx)
351 return mpctx->playtree_iter;
354 void *mpctx_get_mixer(MPContext *mpctx)
356 return &mpctx->mixer;
359 int mpctx_get_global_sub_size(MPContext *mpctx)
361 return mpctx->global_sub_size;
364 int mpctx_get_osd_function(MPContext *mpctx)
366 return mpctx->osd_function;
369 static float get_relative_time(struct MPContext *mpctx)
371 unsigned int new_time = GetTimer();
372 unsigned int delta = new_time - mpctx->last_time;
373 mpctx->last_time = new_time;
374 return delta * 0.000001;
377 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
378 switch (type)
380 /* check for valid video stream */
381 case META_VIDEO_CODEC:
382 case META_VIDEO_BITRATE:
383 case META_VIDEO_RESOLUTION:
385 if (!mpctx->sh_video)
386 return 0;
387 break;
390 /* check for valid audio stream */
391 case META_AUDIO_CODEC:
392 case META_AUDIO_BITRATE:
393 case META_AUDIO_SAMPLES:
395 if (!mpctx->sh_audio)
396 return 0;
397 break;
400 /* check for valid demuxer */
401 case META_INFO_TITLE:
402 case META_INFO_ARTIST:
403 case META_INFO_ALBUM:
404 case META_INFO_YEAR:
405 case META_INFO_COMMENT:
406 case META_INFO_TRACK:
407 case META_INFO_GENRE:
409 if (!mpctx->demuxer)
410 return 0;
411 break;
414 default:
415 break;
418 return 1;
421 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
422 char **info = mpctx->demuxer->info;
423 int n;
425 if (!info || !tag)
426 return NULL;
428 for (n = 0; info[2*n] != NULL ; n++)
429 if (!strcmp (info[2*n], tag))
430 break;
432 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
435 char *get_metadata(struct MPContext *mpctx, metadata_t type)
437 char *meta = NULL;
438 sh_audio_t * const sh_audio = mpctx->sh_audio;
439 sh_video_t * const sh_video = mpctx->sh_video;
441 if (!is_valid_metadata_type(mpctx, type))
442 return NULL;
444 switch (type)
446 case META_NAME:
448 return strdup (mp_basename2 (mpctx->filename));
451 case META_VIDEO_CODEC:
453 if (sh_video->format == 0x10000001)
454 meta = strdup ("mpeg1");
455 else if (sh_video->format == 0x10000002)
456 meta = strdup ("mpeg2");
457 else if (sh_video->format == 0x10000004)
458 meta = strdup ("mpeg4");
459 else if (sh_video->format == 0x10000005)
460 meta = strdup ("h264");
461 else if (sh_video->format >= 0x20202020)
463 meta = malloc (8);
464 sprintf (meta, "%.4s", (char *) &sh_video->format);
466 else
468 meta = malloc (8);
469 sprintf (meta, "0x%08X", sh_video->format);
471 return meta;
474 case META_VIDEO_BITRATE:
476 meta = malloc (16);
477 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
478 return meta;
481 case META_VIDEO_RESOLUTION:
483 meta = malloc (16);
484 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
485 return meta;
488 case META_AUDIO_CODEC:
490 if (sh_audio->codec && sh_audio->codec->name)
491 meta = strdup (sh_audio->codec->name);
492 return meta;
495 case META_AUDIO_BITRATE:
497 meta = malloc (16);
498 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
499 return meta;
502 case META_AUDIO_SAMPLES:
504 meta = malloc (16);
505 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
506 return meta;
509 /* check for valid demuxer */
510 case META_INFO_TITLE:
511 return get_demuxer_info(mpctx, "Title");
513 case META_INFO_ARTIST:
514 return get_demuxer_info(mpctx, "Artist");
516 case META_INFO_ALBUM:
517 return get_demuxer_info(mpctx, "Album");
519 case META_INFO_YEAR:
520 return get_demuxer_info(mpctx, "Year");
522 case META_INFO_COMMENT:
523 return get_demuxer_info(mpctx, "Comment");
525 case META_INFO_TRACK:
526 return get_demuxer_info(mpctx, "Track");
528 case META_INFO_GENRE:
529 return get_demuxer_info(mpctx, "Genre");
531 default:
532 break;
535 return meta;
538 /// step size of mixer changes
539 int volstep = 3;
541 #ifdef CONFIG_DVDNAV
542 static void mp_dvdnav_context_free(MPContext *ctx){
543 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
544 ctx->nav_smpi = NULL;
545 if (ctx->nav_buffer) free(ctx->nav_buffer);
546 ctx->nav_buffer = NULL;
547 ctx->nav_start = NULL;
548 ctx->nav_in_size = 0;
550 #endif
552 void uninit_player(struct MPContext *mpctx, unsigned int mask){
553 mask=mpctx->initialized_flags&mask;
555 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
557 if(mask&INITIALIZED_ACODEC){
558 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
559 current_module="uninit_acodec";
560 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
561 #ifdef CONFIG_GUI
562 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
563 #endif
564 mpctx->sh_audio=NULL;
565 mpctx->mixer.afilter = NULL;
568 if(mask&INITIALIZED_VCODEC){
569 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
570 current_module="uninit_vcodec";
571 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
572 mpctx->sh_video=NULL;
573 #ifdef CONFIG_MENU
574 vf_menu=NULL;
575 #endif
578 if(mask&INITIALIZED_DEMUXER){
579 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
580 current_module="free_demuxer";
581 if(mpctx->demuxer){
582 mpctx->stream=mpctx->demuxer->stream;
583 free_demuxer(mpctx->demuxer);
585 mpctx->demuxer=NULL;
588 // kill the cache process:
589 if(mask&INITIALIZED_STREAM){
590 mpctx->initialized_flags&=~INITIALIZED_STREAM;
591 current_module="uninit_stream";
592 if(mpctx->stream) free_stream(mpctx->stream);
593 mpctx->stream=NULL;
596 if(mask&INITIALIZED_VO){
597 mpctx->initialized_flags&=~INITIALIZED_VO;
598 current_module="uninit_vo";
599 vo_destroy(mpctx->video_out);
600 mpctx->video_out=NULL;
601 #ifdef CONFIG_DVDNAV
602 mp_dvdnav_context_free(mpctx);
603 #endif
606 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
607 if(mask&INITIALIZED_GETCH2){
608 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
609 current_module="uninit_getch2";
610 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
611 // restore terminal:
612 getch2_disable();
615 if(mask&INITIALIZED_VOBSUB){
616 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
617 current_module="uninit_vobsub";
618 if(vo_vobsub) vobsub_close(vo_vobsub);
619 vo_vobsub=NULL;
622 if (mask&INITIALIZED_SPUDEC){
623 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
624 current_module="uninit_spudec";
625 spudec_free(vo_spudec);
626 vo_spudec=NULL;
629 if(mask&INITIALIZED_AO){
630 mpctx->initialized_flags&=~INITIALIZED_AO;
631 current_module="uninit_ao";
632 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
633 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
634 mpctx->audio_out=NULL;
637 #ifdef CONFIG_GUI
638 if(mask&INITIALIZED_GUI){
639 mpctx->initialized_flags&=~INITIALIZED_GUI;
640 current_module="uninit_gui";
641 guiDone();
643 #endif
645 current_module=NULL;
648 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
650 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
651 uninit_player(mpctx, INITIALIZED_ALL);
652 #if defined(__MINGW32__) || defined(__CYGWIN__)
653 timeEndPeriod(1);
654 #endif
655 #ifdef CONFIG_X11
656 #ifdef CONFIG_GUI
657 if ( !use_gui )
658 #endif
659 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
660 #endif
662 current_module="uninit_input";
663 mp_input_uninit(mpctx->input);
664 #ifdef CONFIG_MENU
665 if (use_menu)
666 menu_uninit();
667 #endif
669 #ifdef CONFIG_FREETYPE
670 current_module="uninit_font";
671 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
672 free_font_desc(mpctx->osd->sub_font);
673 free_font_desc(vo_font);
674 vo_font = NULL;
675 done_freetype();
676 #endif
677 osd_free(mpctx->osd);
679 #ifdef CONFIG_ASS
680 ass_library_done(ass_library);
681 #endif
683 current_module="exit_player";
685 // free mplayer config
686 if(mpctx->mconfig)
687 m_config_free(mpctx->mconfig);
689 if(mpctx->playtree)
690 play_tree_free(mpctx->playtree, 1);
692 talloc_free(mpctx->key_fifo);
694 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
695 switch(how) {
696 case EXIT_QUIT:
697 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
698 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
699 break;
700 case EXIT_EOF:
701 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
702 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
703 break;
704 case EXIT_ERROR:
705 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
706 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
707 break;
708 default:
709 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
711 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
713 exit(rc);
716 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
718 exit_player_with_rc(mpctx, how, 1);
721 #ifndef __MINGW32__
722 static void child_sighandler(int x){
723 pid_t pid;
724 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
726 #endif
728 #ifdef CONFIG_CRASH_DEBUG
729 static char *prog_path;
730 static int crash_debug = 0;
731 #endif
733 static void exit_sighandler(int x){
734 static int sig_count=0;
735 #ifdef CONFIG_CRASH_DEBUG
736 if (!crash_debug || x != SIGTRAP)
737 #endif
738 ++sig_count;
739 if(sig_count==5)
741 /* We're crashing bad and can't uninit cleanly :(
742 * by popular request, we make one last (dirty)
743 * effort to restore the user's
744 * terminal. */
745 getch2_disable();
746 exit(1);
748 if(sig_count==6) exit(1);
749 if(sig_count>6){
750 // can't stop :(
751 #ifndef __MINGW32__
752 kill(getpid(),SIGKILL);
753 #endif
755 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
756 current_module?current_module:"unknown"
758 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
759 if(sig_count<=1)
760 switch(x){
761 case SIGINT:
762 case SIGQUIT:
763 case SIGTERM:
764 case SIGKILL:
765 async_quit_request = 1;
766 return; // killed from keyboard (^C) or killed [-9]
767 case SIGILL:
768 #ifdef RUNTIME_CPUDETECT
769 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
770 #else
771 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
772 #endif
773 case SIGFPE:
774 case SIGSEGV:
775 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
776 default:
777 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
778 #ifdef CONFIG_CRASH_DEBUG
779 if (crash_debug) {
780 int gdb_pid;
781 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
782 gdb_pid = fork();
783 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
784 if (gdb_pid == 0) { // We are the child
785 char spid[20];
786 snprintf(spid, sizeof(spid), "%i", getppid());
787 getch2_disable(); // allow terminal to work properly with gdb
788 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
789 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
790 } else if (gdb_pid < 0)
791 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
792 else {
793 waitpid(gdb_pid, NULL, 0);
795 if (x == SIGTRAP) return;
797 #endif
799 getch2_disable();
800 exit(1);
803 #include "cfg-mplayer.h"
805 static int cfg_include(m_option_t *conf, char *filename)
807 return m_config_parse_config_file(conf->priv, filename);
810 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
812 char *conffile;
813 int conffile_fd;
814 if (!disable_system_conf &&
815 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
816 exit_player(mpctx, EXIT_NONE);
817 if ((conffile = get_path("")) == NULL) {
818 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
819 } else {
820 #ifdef __MINGW32__
821 mkdir(conffile);
822 #else
823 mkdir(conffile, 0777);
824 #endif
825 free(conffile);
826 if ((conffile = get_path("config")) == NULL) {
827 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
828 } else {
829 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
830 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
831 write(conffile_fd, default_config, strlen(default_config));
832 close(conffile_fd);
834 if (!disable_user_conf &&
835 m_config_parse_config_file(conf, conffile) < 0)
836 exit_player(mpctx, EXIT_NONE);
837 free(conffile);
842 #define PROFILE_CFG_PROTOCOL "protocol."
844 static void load_per_protocol_config (m_config_t* conf, const char *const file)
846 char *str;
847 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
848 m_profile_t *p;
850 /* does filename actually uses a protocol ? */
851 str = strstr (file, "://");
852 if (!str)
853 return;
855 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
856 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
857 p = m_config_get_profile (conf, protocol);
858 if (p)
860 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
861 m_config_set_profile(conf,p);
865 #define PROFILE_CFG_EXTENSION "extension."
867 static void load_per_extension_config (m_config_t* conf, const char *const file)
869 char *str;
870 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
871 m_profile_t *p;
873 /* does filename actually have an extension ? */
874 str = strrchr (file, '.');
875 if (!str)
876 return;
878 sprintf (extension, PROFILE_CFG_EXTENSION);
879 strncat (extension, ++str, 7);
880 p = m_config_get_profile (conf, extension);
881 if (p)
883 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
884 m_config_set_profile(conf,p);
888 #define PROFILE_CFG_VO "vo."
889 #define PROFILE_CFG_AO "ao."
891 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
893 char profile[strlen (cfg) + strlen (out) + 1];
894 m_profile_t *p;
896 sprintf (profile, "%s%s", cfg, out);
897 p = m_config_get_profile (conf, profile);
898 if (p)
900 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
901 m_config_set_profile(conf,p);
905 static void load_per_file_config (m_config_t* conf, const char *const file)
907 char *confpath;
908 char cfg[strlen(file)+10];
909 struct stat st;
910 char *name;
912 sprintf (cfg, "%s.conf", file);
914 if (use_filedir_conf && !stat (cfg, &st))
916 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
917 m_config_parse_config_file (conf, cfg);
918 return;
921 if ((name = strrchr (cfg, '/')) == NULL)
922 name = cfg;
923 else
924 name++;
926 if ((confpath = get_path (name)) != NULL)
928 if (!stat (confpath, &st))
930 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
931 m_config_parse_config_file (conf, confpath);
934 free (confpath);
938 /* When libmpdemux performs a blocking operation (network connection or
939 * cache filling) if the operation fails we use this function to check
940 * if it was interrupted by the user.
941 * The function returns a new value for eof. */
942 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
944 mp_cmd_t* cmd;
945 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
946 switch(cmd->id) {
947 case MP_CMD_QUIT:
948 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
949 case MP_CMD_PLAY_TREE_STEP: {
950 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
951 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
952 } break;
953 case MP_CMD_PLAY_TREE_UP_STEP: {
954 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
955 } break;
956 case MP_CMD_PLAY_ALT_SRC_STEP: {
957 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
958 } break;
960 mp_cmd_free(cmd);
962 return stop_play;
965 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
967 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
969 play_tree_add_bpf(entry,mpctx->filename);
971 #ifdef CONFIG_GUI
972 if (use_gui) {
973 if (entry) {
974 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
975 play_tree_free_list(entry,1);
977 } else
978 #endif
980 if(!entry) {
981 entry = mpctx->playtree_iter->tree;
982 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
983 return PT_NEXT_ENTRY;
985 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
986 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
987 return PT_NEXT_ENTRY;
990 play_tree_remove(entry,1,1);
991 return PT_NEXT_SRC;
993 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
994 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
995 entry = mpctx->playtree_iter->tree;
996 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
997 return PT_NEXT_ENTRY;
999 play_tree_remove(entry,1,1);
1001 return PT_NEXT_SRC;
1004 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1006 sub_data *subd;
1007 #ifdef CONFIG_ASS
1008 ass_track_t *asst = 0;
1009 #endif
1011 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1012 return;
1015 subd = sub_read_file(filename, fps);
1016 #ifdef CONFIG_ASS
1017 if (ass_enabled)
1018 #ifdef CONFIG_ICONV
1019 asst = ass_read_file(ass_library, filename, sub_cp);
1020 #else
1021 asst = ass_read_file(ass_library, filename, 0);
1022 #endif
1023 if (ass_enabled && subd && !asst)
1024 asst = ass_read_subdata(ass_library, subd, fps);
1026 if (!asst && !subd)
1027 #else
1028 if(!subd)
1029 #endif
1030 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1031 filename_recode(filename));
1033 #ifdef CONFIG_ASS
1034 if (!asst && !subd) return;
1035 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1036 #else
1037 if (!subd) return;
1038 #endif
1039 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1040 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1041 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1042 filename_recode(filename));
1043 ++mpctx->set_of_sub_size;
1044 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1045 filename_recode(filename));
1048 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1049 void update_set_of_subtitles(struct MPContext *mpctx)
1050 // subdata was changed, set_of_sub... have to be updated.
1052 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1053 int i;
1054 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1055 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1056 set_of_subtitles[i-1] = set_of_subtitles[i];
1057 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1058 --mpctx->set_of_sub_size;
1059 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1061 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1062 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1064 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1065 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1066 ++mpctx->set_of_sub_size;
1070 void init_vo_spudec(struct MPContext *mpctx)
1072 if (vo_spudec)
1073 spudec_free(vo_spudec);
1074 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1075 vo_spudec = NULL;
1076 if (spudec_ifo) {
1077 unsigned int palette[16], width, height;
1078 current_module="spudec_init_vobsub";
1079 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1080 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1083 #ifdef CONFIG_DVDREAD
1084 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1085 current_module="spudec_init_dvdread";
1086 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1087 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1088 NULL, 0);
1090 #endif
1092 #ifdef CONFIG_DVDNAV
1093 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1094 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1095 current_module="spudec_init_dvdnav";
1096 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1098 #endif
1100 if (vo_spudec==NULL) {
1101 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1102 current_module="spudec_init_normal";
1103 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1104 spudec_set_font_factor(vo_spudec,font_factor);
1107 if (vo_spudec!=NULL)
1108 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1112 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1113 * make it all work is to use the builtin SDL-bootstrap code, which
1114 * will be done automatically by replacing our main() if we include SDL.h.
1116 #if defined(__APPLE__) && defined(CONFIG_SDL)
1117 #include <SDL.h>
1118 #endif
1121 * \brief append a formatted string
1122 * \param buf buffer to print into
1123 * \param pos position of terminating 0 in buf
1124 * \param len maximum number of characters in buf, not including terminating 0
1125 * \param format printf format string
1127 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1129 va_list va;
1130 va_start(va, format);
1131 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1132 va_end(va);
1133 if (*pos >= len ) {
1134 buf[len] = 0;
1135 *pos = len;
1140 * \brief append time in the hh:mm:ss.f format
1141 * \param buf buffer to print into
1142 * \param pos position of terminating 0 in buf
1143 * \param len maximum number of characters in buf, not including terminating 0
1144 * \param time time value to convert/append
1146 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1147 long tenths = 10 * time;
1148 int f1 = tenths % 10;
1149 int ss = (tenths / 10) % 60;
1150 int mm = (tenths / 600) % 60;
1151 int hh = tenths / 36000;
1152 if (time <= 0) {
1153 saddf(buf, pos, len, "unknown");
1154 return;
1156 if (hh > 0)
1157 saddf(buf, pos, len, "%2d:", hh);
1158 if (hh > 0 || mm > 0)
1159 saddf(buf, pos, len, "%02d:", mm);
1160 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1164 * \brief print the status line
1165 * \param a_pos audio position
1166 * \param a_v A-V desynchronization
1167 * \param corr amount out A-V synchronization
1169 static void print_status(struct MPContext *mpctx, float a_pos, float a_v, float corr)
1171 struct MPOpts *opts = &mpctx->opts;
1172 sh_video_t * const sh_video = mpctx->sh_video;
1173 int width;
1174 char *line;
1175 unsigned pos = 0;
1176 get_screen_size();
1177 if (screen_width > 0)
1178 width = screen_width;
1179 else
1180 width = 80;
1181 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1182 /* Windows command line is broken (MinGW's rxvt works, but we
1183 * should not depend on that). */
1184 width--;
1185 #endif
1186 line = malloc(width + 1); // one additional char for the terminating null
1188 // Audio time
1189 if (mpctx->sh_audio) {
1190 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1191 if (!sh_video) {
1192 float len = demuxer_get_time_length(mpctx->demuxer);
1193 saddf(line, &pos, width, "(");
1194 sadd_hhmmssf(line, &pos, width, a_pos);
1195 saddf(line, &pos, width, ") of %.1f (", len);
1196 sadd_hhmmssf(line, &pos, width, len);
1197 saddf(line, &pos, width, ") ");
1201 // Video time
1202 if (sh_video)
1203 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1205 // A-V sync
1206 if (mpctx->sh_audio && sh_video)
1207 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1209 // Video stats
1210 if (sh_video)
1211 saddf(line, &pos, width, "%3d/%3d ",
1212 (int)sh_video->num_frames,
1213 (int)sh_video->num_frames_decoded);
1215 // CPU usage
1216 if (sh_video) {
1217 if (sh_video->timer > 0.5)
1218 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1219 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1220 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1221 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1222 else
1223 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1224 } else if (mpctx->sh_audio) {
1225 if (mpctx->delay > 0.5)
1226 saddf(line, &pos, width, "%4.1f%% ",
1227 100.0*audio_time_usage/(double)mpctx->delay);
1228 else
1229 saddf(line, &pos, width, "??,?%% ");
1232 // VO stats
1233 if (sh_video)
1234 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1236 #ifdef CONFIG_STREAM_CACHE
1237 // cache stats
1238 if (stream_cache_size > 0)
1239 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1240 #endif
1242 // other
1243 if (opts->playback_speed != 1)
1244 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1246 // end
1247 if (erase_to_end_of_line) {
1248 line[pos] = 0;
1249 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1250 } else {
1251 memset(&line[pos], ' ', width - pos);
1252 line[width] = 0;
1253 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1255 free(line);
1259 * \brief build a chain of audio filters that converts the input format
1260 * to the ao's format, taking into account the current playback_speed.
1261 * \param sh_audio describes the requested input format of the chain.
1262 * \param ao_data describes the requested output format of the chain.
1264 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1266 struct MPOpts *opts = &mpctx->opts;
1267 int new_srate;
1268 int result;
1269 if (!sh_audio)
1271 #ifdef CONFIG_GUI
1272 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1273 #endif
1274 mpctx->mixer.afilter = NULL;
1275 return 0;
1277 if(af_control_any_rev(sh_audio->afilter,
1278 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1279 &opts->playback_speed)) {
1280 new_srate = sh_audio->samplerate;
1281 } else {
1282 new_srate = sh_audio->samplerate * opts->playback_speed;
1283 if (new_srate != ao_data->samplerate) {
1284 // limits are taken from libaf/af_resample.c
1285 if (new_srate < 8000)
1286 new_srate = 8000;
1287 if (new_srate > 192000)
1288 new_srate = 192000;
1289 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1292 result = init_audio_filters(sh_audio, new_srate,
1293 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1294 mpctx->mixer.afilter = sh_audio->afilter;
1295 #ifdef CONFIG_GUI
1296 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1297 #endif
1298 return result;
1302 typedef struct mp_osd_msg mp_osd_msg_t;
1303 struct mp_osd_msg {
1304 /// Previous message on the stack.
1305 mp_osd_msg_t* prev;
1306 /// Message text.
1307 char msg[64];
1308 int id,level,started;
1309 /// Display duration in ms.
1310 unsigned time;
1313 /// OSD message stack.
1314 static mp_osd_msg_t* osd_msg_stack = NULL;
1317 * \brief Add a message on the OSD message stack
1319 * If a message with the same id is already present in the stack
1320 * it is pulled on top of the stack, otherwise a new message is created.
1324 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1325 mp_osd_msg_t *msg,*last=NULL;
1326 va_list va;
1327 int r;
1329 // look if the id is already in the stack
1330 for(msg = osd_msg_stack ; msg && msg->id != id ;
1331 last = msg, msg = msg->prev);
1332 // not found: alloc it
1333 if(!msg) {
1334 msg = calloc(1,sizeof(mp_osd_msg_t));
1335 msg->prev = osd_msg_stack;
1336 osd_msg_stack = msg;
1337 } else if(last) { // found, but it's not on top of the stack
1338 last->prev = msg->prev;
1339 msg->prev = osd_msg_stack;
1340 osd_msg_stack = msg;
1342 // write the msg
1343 va_start(va,fmt);
1344 r = vsnprintf(msg->msg, 64, fmt, va);
1345 va_end(va);
1346 if(r >= 64) msg->msg[63] = 0;
1347 // set id and time
1348 msg->id = id;
1349 msg->level = level;
1350 msg->time = time;
1355 * \brief Remove a message from the OSD stack
1357 * This function can be used to get rid of a message right away.
1361 void rm_osd_msg(int id) {
1362 mp_osd_msg_t *msg,*last=NULL;
1364 // Search for the msg
1365 for(msg = osd_msg_stack ; msg && msg->id != id ;
1366 last = msg, msg = msg->prev);
1367 if(!msg) return;
1369 // Detach it from the stack and free it
1370 if(last)
1371 last->prev = msg->prev;
1372 else
1373 osd_msg_stack = msg->prev;
1374 free(msg);
1378 * \brief Remove all messages from the OSD stack
1382 static void clear_osd_msgs(void) {
1383 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1384 while(msg) {
1385 prev = msg->prev;
1386 free(msg);
1387 msg = prev;
1389 osd_msg_stack = NULL;
1393 * \brief Get the current message from the OSD stack.
1395 * This function decrements the message timer and destroys the old ones.
1396 * The message that should be displayed is returned (if any).
1400 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1402 mp_osd_msg_t *msg,*prev,*last = NULL;
1403 static unsigned last_update = 0;
1404 unsigned now = GetTimerMS();
1405 unsigned diff;
1406 char hidden_dec_done = 0;
1408 if (osd_visible) {
1409 // 36000000 means max timed visibility is 1 hour into the future, if
1410 // the difference is greater assume it's wrapped around from below 0
1411 if (osd_visible - now > 36000000) {
1412 osd_visible = 0;
1413 vo_osd_progbar_type = -1; // disable
1414 vo_osd_changed(OSDTYPE_PROGBAR);
1415 if (mpctx->osd_function != OSD_PAUSE)
1416 mpctx->osd_function = OSD_PLAY;
1420 if(!last_update) last_update = now;
1421 diff = now >= last_update ? now - last_update : 0;
1423 last_update = now;
1425 // Look for the first message in the stack with high enough level.
1426 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1427 prev = msg->prev;
1428 if(msg->level > osd_level && hidden_dec_done) continue;
1429 // The message has a high enough level or it is the first hidden one
1430 // in both cases we decrement the timer or kill it.
1431 if(!msg->started || msg->time > diff) {
1432 if(msg->started) msg->time -= diff;
1433 else msg->started = 1;
1434 // display it
1435 if(msg->level <= osd_level) return msg;
1436 hidden_dec_done = 1;
1437 continue;
1439 // kill the message
1440 free(msg);
1441 if(last) {
1442 last->prev = prev;
1443 msg = last;
1444 } else {
1445 osd_msg_stack = prev;
1446 msg = NULL;
1449 // Nothing found
1450 return NULL;
1454 * \brief Display the OSD bar.
1456 * Display the OSD bar or fall back on a simple message.
1460 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1462 if(osd_level < 1) return;
1464 if(mpctx->sh_video) {
1465 osd_visible = (GetTimerMS() + 1000) | 1;
1466 vo_osd_progbar_type = type;
1467 vo_osd_progbar_value = 256*(val-min)/(max-min);
1468 vo_osd_changed(OSDTYPE_PROGBAR);
1469 return;
1472 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1473 name,ROUND(100*(val-min)/(max-min)));
1478 * \brief Update the OSD message line.
1480 * This function displays the current message on the vo OSD or on the term.
1481 * If the stack is empty and the OSD level is high enough the timer
1482 * is displayed (only on the vo OSD).
1486 static void update_osd_msg(struct MPContext *mpctx)
1488 mp_osd_msg_t *msg;
1489 struct osd_state *osd = mpctx->osd;
1490 char osd_text_timer[64];
1492 // Look if we have a msg
1493 if((msg = get_osd_msg(mpctx))) {
1494 if (strcmp(osd->osd_text, msg->msg)) {
1495 strncpy(osd->osd_text, msg->msg, 63);
1496 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1497 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1499 return;
1502 if(mpctx->sh_video) {
1503 // fallback on the timer
1504 if(osd_level>=2) {
1505 int len = demuxer_get_time_length(mpctx->demuxer);
1506 int percentage = -1;
1507 char percentage_text[10];
1508 int pts = demuxer_get_current_time(mpctx->demuxer);
1510 if (mpctx->osd_show_percentage)
1511 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1513 if (percentage >= 0)
1514 snprintf(percentage_text, 9, " (%d%%)", percentage);
1515 else
1516 percentage_text[0] = 0;
1518 if (osd_level == 3)
1519 snprintf(osd_text_timer, 63,
1520 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1521 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1522 len/3600,(len/60)%60,len%60,percentage_text);
1523 else
1524 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1525 mpctx->osd_function,pts/3600,(pts/60)%60,
1526 pts%60,percentage_text);
1527 } else
1528 osd_text_timer[0]=0;
1530 // always decrement the percentage timer
1531 if(mpctx->osd_show_percentage)
1532 mpctx->osd_show_percentage--;
1534 if (strcmp(osd->osd_text, osd_text_timer)) {
1535 strncpy(osd->osd_text, osd_text_timer, 63);
1536 vo_osd_changed(OSDTYPE_OSD);
1538 return;
1541 // Clear the term osd line
1542 if (term_osd && osd->osd_text[0]) {
1543 osd->osd_text[0] = 0;
1544 printf("%s\n",term_osd_esc);
1548 ///@}
1549 // OSDMsgStack
1552 void reinit_audio_chain(struct MPContext *mpctx)
1554 struct MPOpts *opts = &mpctx->opts;
1555 if(mpctx->sh_audio){
1556 current_module="init_audio_codec";
1557 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1558 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1559 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1560 mpctx->d_audio->id = -2;
1561 return;
1562 } else
1563 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1564 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1567 //const ao_info_t *info=audio_out->info;
1568 current_module="af_preinit";
1569 ao_data.samplerate=force_srate;
1570 ao_data.channels=0;
1571 ao_data.format=audio_output_format;
1572 #if 1
1573 // first init to detect best values
1574 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1575 // input:
1576 mpctx->sh_audio->samplerate,
1577 // output:
1578 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1579 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1580 exit_player(mpctx, EXIT_ERROR);
1582 #endif
1583 current_module="ao2_init";
1584 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1585 0, // plugin flag
1586 ao_data.samplerate,
1587 ao_data.channels,
1588 ao_data.format,0))){
1589 // FAILED:
1590 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1591 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1592 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1593 mpctx->d_audio->id = -2;
1594 return;
1595 } else {
1596 // SUCCESS:
1597 mpctx->initialized_flags|=INITIALIZED_AO;
1598 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1599 mpctx->audio_out->info->short_name,
1600 ao_data.samplerate, ao_data.channels,
1601 af_fmt2str_short(ao_data.format),
1602 af_fmt2bits(ao_data.format)/8 );
1603 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1604 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1605 if(strlen(mpctx->audio_out->info->comment) > 0)
1606 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1607 // init audio filters:
1608 #if 1
1609 current_module="af_init";
1610 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1611 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1612 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1613 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1614 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1616 #endif
1618 mpctx->mixer.audio_out = mpctx->audio_out;
1619 mpctx->mixer.volstep = volstep;
1624 ///@}
1625 // Command2Property
1628 // Return pts value corresponding to the end point of audio written to the
1629 // ao so far.
1630 static double written_audio_pts(struct MPContext *mpctx)
1632 sh_audio_t *sh_audio = mpctx->sh_audio;
1633 demux_stream_t *d_audio = mpctx->d_audio;
1634 double buffered_output;
1635 // first calculate the end pts of audio that has been output by decoder
1636 double a_pts = sh_audio->pts;
1637 if (a_pts != MP_NOPTS_VALUE)
1638 // Good, decoder supports new way of calculating audio pts.
1639 // sh_audio->pts is the timestamp of the latest input packet with
1640 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1641 // the amount of bytes the decoder has written after that timestamp.
1642 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1643 else {
1644 // Decoder doesn't support new way of calculating pts (or we're
1645 // being called before it has decoded anything with known timestamp).
1646 // Use the old method of audio pts calculation: take the timestamp
1647 // of last packet with known pts the decoder has read data from,
1648 // and add amount of bytes read after the beginning of that packet
1649 // divided by input bps. This will be inaccurate if the input/output
1650 // ratio is not constant for every audio packet or if it is constant
1651 // but not accurately known in sh_audio->i_bps.
1653 a_pts = d_audio->pts;
1654 // ds_tell_pts returns bytes read after last timestamp from
1655 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1656 // it has read but not decoded
1657 if (sh_audio->i_bps)
1658 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1659 (double)sh_audio->i_bps;
1661 // Now a_pts hopefully holds the pts for end of audio from decoder.
1662 // Substract data in buffers between decoder and audio out.
1664 // Decoded but not filtered
1665 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1667 // Data buffered in audio filters, measured in bytes of "missing" output
1668 buffered_output = af_calc_delay(sh_audio->afilter);
1670 // Data that was ready for ao but was buffered because ao didn't fully
1671 // accept everything to internal buffers yet
1672 buffered_output += sh_audio->a_out_buffer_len;
1674 // Filters divide audio length by playback_speed, so multiply by it
1675 // to get the length in original units without speedup or slowdown
1676 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1678 return a_pts;
1681 // Return pts value corresponding to currently playing audio.
1682 double playing_audio_pts(struct MPContext *mpctx)
1684 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1685 mpctx->audio_out->get_delay();
1688 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1689 struct MPOpts *opts = &mpctx->opts;
1690 // check for frame-drop:
1691 current_module = "check_framedrop";
1692 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1693 static int dropped_frames;
1694 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1695 float d = delay-mpctx->delay;
1696 ++total_frame_cnt;
1697 // we should avoid dropping too many frames in sequence unless we
1698 // are too late. and we allow 100ms A-V delay here:
1699 if (d < -dropped_frames*frame_time-0.100 &&
1700 mpctx->osd_function != OSD_PAUSE) {
1701 ++drop_frame_cnt;
1702 ++dropped_frames;
1703 return frame_dropping;
1704 } else
1705 dropped_frames = 0;
1707 return 0;
1710 static int generate_video_frame(struct MPContext *mpctx)
1712 sh_video_t * const sh_video = mpctx->sh_video;
1713 demux_stream_t *d_video = mpctx->d_video;
1715 unsigned char *start;
1716 int in_size;
1717 int hit_eof=0;
1718 double pts;
1720 while (1) {
1721 int drop_frame = check_framedrop(mpctx, sh_video->frametime);
1722 void *decoded_frame;
1723 current_module = "decode video";
1724 // XXX Time used in this call is not counted in any performance
1725 // timer now, OSD is not updated correctly for filter-added frames
1726 if (vf_output_queued_frame(sh_video->vfilter))
1727 break;
1728 current_module = "video_read_frame";
1729 in_size = ds_get_packet_pts(d_video, &start, &pts);
1730 if (in_size < 0) {
1731 // try to extract last frames in case of decoder lag
1732 in_size = 0;
1733 pts = 1e300;
1734 hit_eof = 1;
1736 if (in_size > max_framesize)
1737 max_framesize = in_size;
1738 current_module = "decode video";
1739 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1740 if (decoded_frame) {
1741 update_subtitles(sh_video, mpctx->d_sub, 0);
1742 update_teletext(sh_video, mpctx->demuxer, 0);
1743 update_osd_msg(mpctx);
1744 current_module = "filter video";
1745 if (filter_video(sh_video, decoded_frame, sh_video->pts,
1746 mpctx->osd))
1747 break;
1748 } else if (drop_frame)
1749 return -1;
1750 if (hit_eof)
1751 return 0;
1753 return 1;
1756 #ifdef HAVE_RTC
1757 int rtc_fd = -1;
1758 #endif
1760 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1762 #ifdef HAVE_RTC
1763 if (rtc_fd >= 0){
1764 // -------- RTC -----------
1765 current_module="sleep_rtc";
1766 while (time_frame > 0.000) {
1767 unsigned long rtc_ts;
1768 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1769 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1770 time_frame -= get_relative_time(mpctx);
1772 } else
1773 #endif
1775 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1776 // unnecessarily high CPU usage
1777 float margin = softsleep ? 0.011 : 0;
1778 current_module = "sleep_timer";
1779 while (time_frame > margin) {
1780 usec_sleep(1000000 * (time_frame - margin));
1781 time_frame -= get_relative_time(mpctx);
1783 if (softsleep){
1784 current_module = "sleep_soft";
1785 if (time_frame < 0)
1786 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1787 while (time_frame > 0)
1788 time_frame -= get_relative_time(mpctx); // burn the CPU
1791 return time_frame;
1794 #ifdef CONFIG_DVDNAV
1795 #ifndef FF_B_TYPE
1796 #define FF_B_TYPE 3
1797 #endif
1798 /// store decoded video image
1799 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1800 mp_image_t *from_mpi) {
1801 mp_image_t *mpi;
1803 /// Do not store B-frames
1804 if (from_mpi->pict_type == FF_B_TYPE)
1805 return to_mpi;
1807 if (to_mpi &&
1808 to_mpi->w == from_mpi->w &&
1809 to_mpi->h == from_mpi->h &&
1810 to_mpi->imgfmt == from_mpi->imgfmt)
1811 mpi = to_mpi;
1812 else {
1813 if (to_mpi)
1814 free_mp_image(to_mpi);
1815 if (from_mpi->w == 0 || from_mpi->h == 0)
1816 return NULL;
1817 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1820 copy_mpi(mpi,from_mpi);
1821 return mpi;
1824 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1825 struct MPOpts *opts = &ctx->opts;
1826 if (ctx->sh_video) {
1827 /// clear video pts
1828 ctx->d_video->pts = 0.0f;
1829 ctx->sh_video->pts = 0.0f;
1830 ctx->sh_video->i_pts = 0.0f;
1831 ctx->sh_video->last_pts = 0.0f;
1832 ctx->sh_video->num_buffered_pts = 0;
1833 ctx->sh_video->num_frames = 0;
1834 ctx->sh_video->num_frames_decoded = 0;
1835 ctx->sh_video->timer = 0.0f;
1836 ctx->sh_video->stream_delay = 0.0f;
1837 ctx->sh_video->timer = 0;
1838 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1841 if (ctx->sh_audio) {
1842 /// free audio packets and reset
1843 ds_free_packs(ctx->d_audio);
1844 audio_delay -= ctx->sh_audio->stream_delay;
1845 ctx->delay =- audio_delay;
1846 ctx->audio_out->reset();
1847 resync_audio_stream(ctx->sh_audio);
1850 if (ctx->d_sub) opts->sub_id = -2;
1852 audio_delay = 0.0f;
1854 /// clear all EOF related flags
1855 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1858 /// Restore last decoded DVDNAV (still frame)
1859 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1860 int *in_size,
1861 unsigned char **start,
1862 mp_image_t *decoded_frame)
1864 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1865 return decoded_frame;
1867 /// a change occured in dvdnav stream
1868 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1869 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1870 mp_dvdnav_context_free(mpctx);
1871 mp_dvdnav_reset_stream(mpctx);
1872 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1873 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1876 if (*in_size < 0) {
1877 float len;
1879 /// Display still frame, if any
1880 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1881 decoded_frame = mpctx->nav_smpi;
1883 /// increment video frame : continue playing after still frame
1884 len = demuxer_get_time_length(mpctx->demuxer);
1885 if (mpctx->sh_video->pts >= len &&
1886 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1887 mp_dvdnav_skip_still(mpctx->stream);
1888 mp_dvdnav_skip_wait(mpctx->stream);
1890 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1892 if (mpctx->nav_buffer) {
1893 *start = mpctx->nav_start;
1894 *in_size = mpctx->nav_in_size;
1895 if (mpctx->nav_start)
1896 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1900 return decoded_frame;
1903 /// Save last decoded DVDNAV (still frame)
1904 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1905 unsigned char *start,
1906 mp_image_t *decoded_frame)
1908 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1909 return;
1911 if (mpctx->nav_buffer)
1912 free(mpctx->nav_buffer);
1914 mpctx->nav_buffer = malloc(in_size);
1915 mpctx->nav_start = start;
1916 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1917 if (mpctx->nav_buffer)
1918 memcpy(mpctx->nav_buffer,start,in_size);
1920 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1921 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1923 #endif /* CONFIG_DVDNAV */
1925 static void adjust_sync_and_print_status(struct MPContext *mpctx,
1926 int between_frames,
1927 float timing_error)
1929 struct MPOpts *opts = &mpctx->opts;
1930 current_module="av_sync";
1932 if(mpctx->sh_audio){
1933 double a_pts, v_pts;
1935 if (autosync)
1937 * If autosync is enabled, the value for delay must be calculated
1938 * a bit differently. It is set only to the difference between
1939 * the audio and video timers. Any attempt to include the real
1940 * or corrected delay causes the pts_correction code below to
1941 * try to correct for the changes in delay which autosync is
1942 * trying to measure. This keeps the two from competing, but still
1943 * allows the code to correct for PTS drift *only*. (Using a delay
1944 * value here, even a "corrected" one, would be incompatible with
1945 * autosync mode.)
1947 a_pts = written_audio_pts(mpctx) - mpctx->delay;
1948 else
1949 a_pts = playing_audio_pts(mpctx);
1951 v_pts = mpctx->sh_video->pts;
1954 static int drop_message=0;
1955 double AV_delay = a_pts - audio_delay - v_pts;
1956 double x;
1957 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1958 ++drop_message;
1959 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1961 if (autosync)
1962 x = AV_delay*0.1f;
1963 else
1964 /* Do not correct target time for the next frame if this frame
1965 * was late not because of wrong target time but because the
1966 * target time could not be met */
1967 x = (AV_delay + timing_error * opts->playback_speed) * 0.1f;
1968 if (x < -max_pts_correction)
1969 x = -max_pts_correction;
1970 else if (x> max_pts_correction)
1971 x = max_pts_correction;
1972 if (default_max_pts_correction >= 0)
1973 max_pts_correction = default_max_pts_correction;
1974 else
1975 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1976 if (!between_frames) {
1977 mpctx->delay+=x;
1978 c_total+=x;
1980 if(!quiet)
1981 print_status(mpctx, a_pts - audio_delay, AV_delay, c_total);
1984 } else {
1985 // No audio:
1987 if (!quiet)
1988 print_status(mpctx, 0, 0, 0);
1992 static int fill_audio_out_buffers(struct MPContext *mpctx)
1994 struct MPOpts *opts = &mpctx->opts;
1995 unsigned int t;
1996 double tt;
1997 int playsize;
1998 int playflags=0;
1999 int audio_eof=0;
2000 int bytes_to_write;
2001 sh_audio_t * const sh_audio = mpctx->sh_audio;
2003 current_module="play_audio";
2005 while (1) {
2006 // all the current uses of ao_data.pts seem to be in aos that handle
2007 // sync completely wrong; there should be no need to use ao_data.pts
2008 // in get_space()
2009 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2010 bytes_to_write = mpctx->audio_out->get_space();
2011 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2012 break;
2014 // handle audio-only case:
2015 // this is where mplayer sleeps during audio-only playback
2016 // to avoid 100% CPU use
2017 usec_sleep(10000); // Wait a tick before retry
2020 while (bytes_to_write) {
2021 playsize = bytes_to_write;
2022 if (playsize > MAX_OUTBURST)
2023 playsize = MAX_OUTBURST;
2024 bytes_to_write -= playsize;
2026 // Fill buffer if needed:
2027 current_module="decode_audio";
2028 t = GetTimer();
2029 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2030 if (mpctx->d_audio->eof) {
2031 audio_eof = 1;
2032 if (sh_audio->a_out_buffer_len == 0)
2033 return 0;
2035 t = GetTimer() - t;
2036 tt = t*0.000001f; audio_time_usage+=tt;
2037 if (playsize > sh_audio->a_out_buffer_len) {
2038 playsize = sh_audio->a_out_buffer_len;
2039 if (audio_eof)
2040 playflags |= AOPLAY_FINAL_CHUNK;
2042 if (!playsize)
2043 break;
2045 // play audio:
2046 current_module="play_audio";
2048 // Is this pts value actually useful for the aos that access it?
2049 // They're obviously badly broken in the way they handle av sync;
2050 // would not having access to this make them more broken?
2051 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2052 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2054 if (playsize > 0) {
2055 sh_audio->a_out_buffer_len -= playsize;
2056 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2057 sh_audio->a_out_buffer_len);
2058 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2060 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2061 // Sanity check to avoid hanging in case current ao doesn't output
2062 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2063 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2064 sh_audio->a_out_buffer_len = 0;
2067 return 1;
2070 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2071 float *aq_sleep_time)
2073 struct MPOpts *opts = &mpctx->opts;
2074 int frame_time_remaining = 0;
2075 current_module="calc_sleep_time";
2077 *time_frame -= get_relative_time(mpctx); // reset timer
2079 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2080 float delay = mpctx->audio_out->get_delay();
2081 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2083 if (autosync) {
2085 * Adjust this raw delay value by calculating the expected
2086 * delay for this frame and generating a new value which is
2087 * weighted between the two. The higher autosync is, the
2088 * closer to the delay value gets to that which "-nosound"
2089 * would have used, and the longer it will take for A/V
2090 * sync to settle at the right value (but it eventually will.)
2091 * This settling time is very short for values below 100.
2093 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2094 float difference = delay - predicted;
2095 delay = predicted + difference / (float)autosync;
2098 *time_frame = delay - mpctx->delay / opts->playback_speed;
2100 // delay = amount of audio buffered in soundcard/driver
2101 if (delay > 0.25) delay=0.25; else
2102 if (delay < 0.10) delay=0.10;
2103 if (*time_frame > delay*0.6) {
2104 // sleep time too big - may cause audio drops (buffer underrun)
2105 frame_time_remaining = 1;
2106 *time_frame = delay*0.5;
2108 } else {
2109 // If we're lagging more than 200 ms behind the right playback rate,
2110 // don't try to "catch up".
2111 // If benchmark is set always output frames as fast as possible
2112 // without sleeping.
2113 if (*time_frame < -0.2 || benchmark)
2114 *time_frame = 0;
2117 *aq_sleep_time += *time_frame;
2120 //============================== SLEEP: ===================================
2122 // flag 256 means: libvo driver does its timing (dvb card)
2123 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2124 *time_frame = timing_sleep(mpctx, *time_frame);
2125 return frame_time_remaining;
2128 int reinit_video_chain(struct MPContext *mpctx)
2130 struct MPOpts *opts = &mpctx->opts;
2131 sh_video_t * const sh_video = mpctx->sh_video;
2132 double ar=-1.0;
2133 //================== Init VIDEO (codec & libvo) ==========================
2134 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2135 current_module="preinit_libvo";
2137 //shouldn't we set dvideo->id=-2 when we fail?
2138 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2139 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2140 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2141 goto err_out;
2143 mpctx->initialized_flags|=INITIALIZED_VO;
2146 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2147 mpctx->sh_video->stream_aspect = ar;
2148 current_module="init_video_filters";
2150 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2151 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2153 #ifdef CONFIG_MENU
2154 if(use_menu) {
2155 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2156 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2157 if(!vf_menu) {
2158 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2159 use_menu = 0;
2162 if(vf_menu)
2163 sh_video->vfilter = vf_menu;
2164 #endif
2166 #ifdef CONFIG_ASS
2167 if(ass_enabled) {
2168 int i;
2169 int insert = 1;
2170 if (opts->vf_settings)
2171 for (i = 0; opts->vf_settings[i].name; ++i)
2172 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2173 insert = 0;
2174 break;
2176 if (insert) {
2177 extern vf_info_t vf_info_ass;
2178 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2179 char* vf_arg[] = {"auto", "1", NULL};
2180 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2181 if (vf_ass)
2182 sh_video->vfilter = vf_ass;
2183 else
2184 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2187 #endif
2189 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2191 #ifdef CONFIG_ASS
2192 if (ass_enabled)
2193 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2194 #endif
2196 current_module="init_video_codec";
2198 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2199 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2200 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2202 if(!sh_video->initialized){
2203 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2204 goto err_out;
2207 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2209 if (sh_video->codec)
2210 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2212 sh_video->last_pts = MP_NOPTS_VALUE;
2213 sh_video->num_buffered_pts = 0;
2214 sh_video->next_frame_time = 0;
2216 if(auto_quality>0){
2217 // Auto quality option enabled
2218 output_quality=get_video_quality_max(sh_video);
2219 if(auto_quality>output_quality) auto_quality=output_quality;
2220 else output_quality=auto_quality;
2221 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2222 set_video_quality(sh_video,output_quality);
2225 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2227 current_module="init_vo";
2229 return 1;
2231 err_out:
2232 mpctx->sh_video = mpctx->d_video->sh = NULL;
2233 return 0;
2236 static double update_video(struct MPContext *mpctx, int *blit_frame)
2238 struct MPOpts *opts = &mpctx->opts;
2239 sh_video_t * const sh_video = mpctx->sh_video;
2240 //-------------------- Decode a frame: -----------------------
2241 double frame_time;
2242 *blit_frame = 0; // Don't blit if we hit EOF
2243 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2244 mpctx->osd); // hack for vf_expand
2245 if (!opts->correct_pts) {
2246 unsigned char* start=NULL;
2247 void *decoded_frame = NULL;
2248 int drop_frame=0;
2249 int in_size;
2251 current_module = "video_read_frame";
2252 frame_time = sh_video->next_frame_time;
2253 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2254 &start, force_fps);
2255 #ifdef CONFIG_DVDNAV
2256 /// wait, still frame or EOF
2257 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2258 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2259 if (mpctx->d_video) mpctx->d_video->eof = 0;
2260 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2261 mpctx->stream->eof = 0;
2262 } else
2263 #endif
2264 if (in_size < 0)
2265 return -1;
2266 if (in_size > max_framesize)
2267 max_framesize = in_size; // stats
2268 sh_video->timer += frame_time;
2269 if (mpctx->sh_audio)
2270 mpctx->delay -= frame_time;
2271 // video_read_frame can change fps (e.g. for ASF video)
2272 vo_fps = sh_video->fps;
2273 drop_frame = check_framedrop(mpctx, frame_time);
2274 update_subtitles(sh_video, mpctx->d_sub, 0);
2275 update_teletext(sh_video, mpctx->demuxer, 0);
2276 update_osd_msg(mpctx);
2277 current_module = "decode_video";
2278 #ifdef CONFIG_DVDNAV
2279 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size,&start,
2280 decoded_frame);
2281 /// still frame has been reached, no need to decode
2282 if (in_size > 0 && !decoded_frame)
2283 #endif
2284 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2285 sh_video->pts);
2286 #ifdef CONFIG_DVDNAV
2287 /// save back last still frame for future display
2288 mp_dvdnav_save_smpi(mpctx, in_size, start, decoded_frame);
2289 #endif
2290 current_module = "filter_video";
2291 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2292 sh_video->pts,
2293 mpctx->osd));
2295 else {
2296 int res = generate_video_frame(mpctx);
2297 if (!res)
2298 return -1;
2299 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS,
2300 &sh_video->pts);
2301 if (sh_video->pts == MP_NOPTS_VALUE) {
2302 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2303 sh_video->pts = sh_video->last_pts;
2305 if (sh_video->last_pts == MP_NOPTS_VALUE)
2306 sh_video->last_pts= sh_video->pts;
2307 else if (sh_video->last_pts >= sh_video->pts) {
2308 sh_video->last_pts = sh_video->pts;
2309 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2311 frame_time = sh_video->pts - sh_video->last_pts;
2312 sh_video->last_pts = sh_video->pts;
2313 sh_video->timer += frame_time;
2314 if(mpctx->sh_audio)
2315 mpctx->delay -= frame_time;
2316 *blit_frame = res > 0;
2318 return frame_time;
2321 static void pause_loop(struct MPContext *mpctx)
2323 mp_cmd_t* cmd;
2324 if (!quiet) {
2325 // Small hack to display the pause message on the OSD line.
2326 // The pause string is: "\n == PAUSE == \r" so we need to
2327 // take the first and the last char out
2328 if (term_osd && !mpctx->sh_video) {
2329 char msg[128] = MSGTR_Paused;
2330 int mlen = strlen(msg);
2331 msg[mlen-1] = '\0';
2332 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2333 update_osd_msg(mpctx);
2334 } else
2335 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2336 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2338 #ifdef CONFIG_GUI
2339 if (use_gui)
2340 guiGetEvent(guiCEvent, (char *)guiSetPause);
2341 #endif
2342 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2343 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2345 if (mpctx->audio_out && mpctx->sh_audio)
2346 mpctx->audio_out->pause(); // pause audio, keep data if possible
2348 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2349 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2350 if (cmd) {
2351 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2352 run_command(mpctx, cmd);
2353 mp_cmd_free(cmd);
2354 continue;
2356 if (mpctx->sh_video && mpctx->video_out)
2357 vo_check_events(mpctx->video_out);
2358 #ifdef CONFIG_GUI
2359 if (use_gui) {
2360 guiEventHandling();
2361 guiGetEvent(guiReDraw, NULL);
2362 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2363 break;
2365 #endif
2366 #ifdef CONFIG_MENU
2367 if (vf_menu)
2368 vf_menu_pause_update(vf_menu);
2369 #endif
2370 usec_sleep(20000);
2372 if (cmd && cmd->id == MP_CMD_PAUSE) {
2373 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2374 mp_cmd_free(cmd);
2376 mpctx->osd_function=OSD_PLAY;
2377 if (mpctx->audio_out && mpctx->sh_audio)
2378 mpctx->audio_out->resume(); // resume audio
2379 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2380 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2381 (void)get_relative_time(mpctx); // ignore time that passed during pause
2382 #ifdef CONFIG_GUI
2383 if (use_gui) {
2384 if (guiIntfStruct.Playing == guiSetStop)
2385 mpctx->eof = 1;
2386 else
2387 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2389 #endif
2392 static void print_version(void){
2393 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2395 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2396 GetCpuCaps(&gCpuCaps);
2397 #ifdef ARCH_X86
2398 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2399 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2400 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2401 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2402 #ifdef RUNTIME_CPUDETECT
2403 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2404 #else
2405 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2406 #ifdef HAVE_MMX
2407 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2408 #endif
2409 #ifdef HAVE_MMX2
2410 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2411 #endif
2412 #ifdef HAVE_3DNOW
2413 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2414 #endif
2415 #ifdef HAVE_3DNOWEX
2416 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2417 #endif
2418 #ifdef HAVE_SSE
2419 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2420 #endif
2421 #ifdef HAVE_SSE2
2422 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2423 #endif
2424 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2425 #endif /* RUNTIME_CPUDETECT */
2426 #endif /* ARCH_X86 */
2430 // Find the right mute status and record position for new file position
2431 static void edl_seek_reset(MPContext *mpctx)
2433 mpctx->edl_muted = 0;
2434 next_edl_record = edl_records;
2436 while (next_edl_record) {
2437 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2438 break;
2440 if (next_edl_record->action == EDL_MUTE)
2441 mpctx->edl_muted = !mpctx->edl_muted;
2442 next_edl_record = next_edl_record->next;
2444 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2445 mixer_mute(&mpctx->mixer);
2449 // Execute EDL command for the current position if one exists
2450 static void edl_update(MPContext *mpctx)
2452 if (!next_edl_record)
2453 return;
2455 if (!mpctx->sh_video) {
2456 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2457 free_edl(edl_records);
2458 next_edl_record = NULL;
2459 edl_records = NULL;
2460 return;
2463 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2464 if (next_edl_record->action == EDL_SKIP) {
2465 mpctx->osd_function = OSD_FFW;
2466 mpctx->abs_seek_pos = 0;
2467 mpctx->rel_seek_secs = next_edl_record->length_sec;
2468 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2469 "[%f], length [%f]\n", next_edl_record->start_sec,
2470 next_edl_record->stop_sec, next_edl_record->length_sec);
2471 edl_decision = 1;
2473 else if (next_edl_record->action == EDL_MUTE) {
2474 mpctx->edl_muted = !mpctx->edl_muted;
2475 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2476 mixer_mute(&mpctx->mixer);
2477 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2478 next_edl_record->start_sec );
2480 next_edl_record = next_edl_record->next;
2485 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2486 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2487 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2488 static int seek(MPContext *mpctx, double amount, int style)
2490 current_module = "seek";
2491 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2492 return -1;
2494 if (mpctx->sh_video) {
2495 current_module = "seek_video_reset";
2496 resync_video_stream(mpctx->sh_video);
2497 if (mpctx->video_out->config_ok)
2498 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2499 mpctx->sh_video->num_buffered_pts = 0;
2500 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2501 mpctx->num_buffered_frames = 0;
2502 mpctx->delay = 0;
2503 // Not all demuxers set d_video->pts during seek, so this value
2504 // (which is used by at least vobsub and edl code below) may
2505 // be completely wrong (probably 0).
2506 mpctx->sh_video->pts = mpctx->d_video->pts;
2507 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2508 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2511 if (mpctx->sh_audio) {
2512 current_module = "seek_audio_reset";
2513 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2514 mpctx->sh_audio->a_buffer_len = 0;
2515 mpctx->sh_audio->a_out_buffer_len = 0;
2518 if (vo_vobsub && mpctx->sh_video) {
2519 current_module = "seek_vobsub_reset";
2520 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2523 edl_seek_reset(mpctx);
2525 c_total = 0;
2526 max_pts_correction = 0.1;
2527 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2528 drop_frame_cnt = 0;
2530 current_module = NULL;
2531 return 0;
2535 static int read_keys(void *ctx, int fd)
2537 getch2(ctx);
2538 return mplayer_get_key(ctx, 0);
2542 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2543 * file for some tools to link against. */
2544 #ifndef DISABLE_MAIN
2545 int main(int argc,char* argv[]){
2548 char * mem_ptr;
2550 // movie info:
2552 /* Flag indicating whether MPlayer should exit without playing anything. */
2553 int opt_exit = 0;
2555 //float a_frame=0; // Audio
2557 int i;
2559 int gui_no_filename=0;
2561 struct MPContext *mpctx = &(struct MPContext){
2562 .osd_function = OSD_PLAY,
2563 .begin_skip = MP_NOPTS_VALUE,
2564 .play_tree_step = 1,
2565 .global_sub_pos = -1,
2566 .set_of_sub_pos = -1,
2567 .file_format = DEMUXER_TYPE_UNKNOWN,
2568 .last_dvb_step = 1,
2571 InitTimer();
2572 srand(GetTimerMS());
2574 mp_msg_init();
2576 #ifdef CONFIG_X11
2577 mpctx->x11_state = vo_x11_init_state();
2578 #endif
2579 struct MPOpts *opts = &mpctx->opts;
2580 set_default_mplayer_options(opts);
2581 // Create the config context and register the options
2582 mpctx->mconfig = m_config_new(opts, cfg_include);
2583 m_config_register_options(mpctx->mconfig,mplayer_opts);
2584 mp_input_register_options(mpctx->mconfig);
2586 // Preparse the command line
2587 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2589 print_version();
2590 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2591 set_path_env();
2592 #endif
2594 #ifdef CONFIG_TV
2595 stream_tv_defaults.immediate = 1;
2596 #endif
2598 if (argc > 1 && argv[1] &&
2599 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2600 use_gui = !strcmp(argv[1], "-gui");
2601 } else
2602 if ( argv[0] )
2604 char *base = strrchr(argv[0], '/');
2605 if (!base)
2606 base = strrchr(argv[0], '\\');
2607 if (!base)
2608 base = argv[0];
2609 if(strstr(base, "gmplayer"))
2610 use_gui=1;
2613 parse_cfgfiles(mpctx, mpctx->mconfig);
2615 #ifdef CONFIG_GUI
2616 if ( use_gui ) cfg_read();
2617 #endif
2619 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2620 if(mpctx->playtree == NULL)
2621 opt_exit = 1;
2622 else {
2623 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2624 if(mpctx->playtree) {
2625 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2626 if(mpctx->playtree_iter) {
2627 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2628 play_tree_iter_free(mpctx->playtree_iter);
2629 mpctx->playtree_iter = NULL;
2631 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2635 mpctx->key_fifo = mp_fifo_create(opts);
2637 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2638 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2639 if(runningmplayer && mpctx->filename && use_gui){
2640 COPYDATASTRUCT csData;
2641 char file[MAX_PATH];
2642 char *filepart = mpctx->filename;
2643 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2644 csData.dwData = 0;
2645 csData.cbData = strlen(file)*2;
2646 csData.lpData = file;
2647 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2650 #endif
2652 #if defined(__MINGW32__) || defined(__CYGWIN__)
2653 // request 1ms timer resolution
2654 timeBeginPeriod(1);
2655 if(proc_priority){
2656 int i;
2657 for(i=0; priority_presets_defs[i].name; i++){
2658 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2659 break;
2661 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
2662 priority_presets_defs[i].name);
2663 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2665 #endif
2666 #ifndef CONFIG_GUI
2667 if(use_gui){
2668 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2669 use_gui=0;
2671 #else
2672 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2673 if(use_gui && !vo_init()){
2674 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2675 use_gui=0;
2677 #endif
2678 if (use_gui && mpctx->playtree_iter){
2679 char cwd[PATH_MAX+2];
2680 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2681 play_tree_iter_free(mpctx->playtree_iter);
2682 mpctx->playtree_iter=NULL;
2684 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2686 strcat(cwd, "/");
2687 // Prefix relative paths with current working directory
2688 play_tree_add_bpf(mpctx->playtree, cwd);
2690 // Import initital playtree into GUI.
2691 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2693 #endif /* CONFIG_GUI */
2695 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2696 list_video_out();
2697 opt_exit = 1;
2700 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2701 list_audio_out();
2702 opt_exit = 1;
2705 /* Check codecs.conf. */
2706 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2707 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2708 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2709 if(!parse_codec_cfg(NULL)){
2710 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2712 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2715 free( mem_ptr ); // release the buffer created by get_path()
2718 #if 0
2719 if(video_codec_list){
2720 int i;
2721 video_codec=video_codec_list[0];
2722 for(i=0;video_codec_list[i];i++)
2723 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2725 #endif
2726 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2727 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2728 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2729 list_codecs(1);
2730 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2731 opt_exit = 1;
2733 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2734 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2735 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2736 list_codecs(0);
2737 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2738 opt_exit = 1;
2740 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2741 vfm_help();
2742 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2743 opt_exit = 1;
2745 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2746 afm_help();
2747 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2748 opt_exit = 1;
2750 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2751 af_help();
2752 printf("\n");
2753 opt_exit = 1;
2755 #ifdef CONFIG_X11
2756 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2757 fstype_help();
2758 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2759 opt_exit = 1;
2761 #endif
2762 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2763 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2764 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2765 demuxer_help();
2766 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2767 opt_exit = 1;
2769 if(list_properties) {
2770 property_print_help();
2771 opt_exit = 1;
2774 if(opt_exit)
2775 exit_player(mpctx, EXIT_NONE);
2777 if (player_idle_mode && use_gui) {
2778 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2779 exit_player_with_rc(mpctx, EXIT_NONE, 1);
2782 if(!mpctx->filename && !player_idle_mode){
2783 if(!use_gui){
2784 // no file/vcd/dvd -> show HELP:
2785 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2786 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2787 } else gui_no_filename=1;
2790 /* Display what configure line was used */
2791 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2793 // Many users forget to include command line in bugreports...
2794 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2795 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2796 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2797 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2800 //------ load global data first ------
2802 mpctx->osd = osd_create();
2804 // check font
2805 #ifdef CONFIG_FREETYPE
2806 init_freetype();
2807 #endif
2808 #ifdef CONFIG_FONTCONFIG
2809 if(font_fontconfig <= 0)
2811 #endif
2812 #ifdef CONFIG_BITMAP_FONT
2813 if(font_name){
2814 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2815 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2816 filename_recode(font_name));
2817 } else {
2818 // try default:
2819 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2820 free(mem_ptr); // release the buffer created by get_path()
2821 if(!vo_font)
2822 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2824 if (sub_font_name)
2825 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2826 else
2827 mpctx->osd->sub_font = vo_font;
2828 #endif
2829 #ifdef CONFIG_FONTCONFIG
2831 #endif
2833 #ifdef CONFIG_ASS
2834 ass_library = ass_init();
2835 #endif
2837 #ifdef HAVE_RTC
2838 if(!nortc)
2840 // seteuid(0); /* Can't hurt to try to get root here */
2841 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2842 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2843 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2844 else {
2845 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2847 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2848 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2849 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2850 close (rtc_fd);
2851 rtc_fd = -1;
2852 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2853 /* variable only by the root */
2854 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2855 close (rtc_fd);
2856 rtc_fd = -1;
2857 } else
2858 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2861 #ifdef CONFIG_GUI
2862 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2863 // and now ? -- Pontscho
2864 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2865 #endif
2866 if(rtc_fd<0)
2867 #endif /* HAVE_RTC */
2868 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2869 softsleep?"software":timer_name);
2871 #ifdef HAVE_TERMCAP
2872 if ( !use_gui ) load_termcap(NULL); // load key-codes
2873 #endif
2875 // ========== Init keyboard FIFO (connection to libvo) ============
2877 // Init input system
2878 current_module = "init_input";
2879 mpctx->input = mp_input_init(&opts->input, use_gui);
2880 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2881 if(slave_mode)
2882 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2883 else if(!noconsolecontrols)
2884 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2885 // Set the libstream interrupt callback
2886 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2888 #ifdef CONFIG_MENU
2889 if(use_menu) {
2890 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2891 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2892 else {
2893 menu_cfg = get_path("menu.conf");
2894 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2895 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2896 else {
2897 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2898 MPLAYER_CONFDIR "/menu.conf"))
2899 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2900 else {
2901 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2902 use_menu = 0;
2907 #endif
2909 current_module = NULL;
2911 /// Catch signals
2912 #ifndef __MINGW32__
2913 signal(SIGCHLD,child_sighandler);
2914 #endif
2916 #ifdef CONFIG_CRASH_DEBUG
2917 prog_path = argv[0];
2918 #endif
2919 //========= Catch terminate signals: ================
2920 // terminate requests:
2921 signal(SIGTERM,exit_sighandler); // kill
2922 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2924 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2926 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2927 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2928 #ifdef CONFIG_SIGHANDLER
2929 // fatal errors:
2930 signal(SIGBUS,exit_sighandler); // bus error
2931 signal(SIGSEGV,exit_sighandler); // segfault
2932 signal(SIGILL,exit_sighandler); // illegal instruction
2933 signal(SIGFPE,exit_sighandler); // floating point exc.
2934 signal(SIGABRT,exit_sighandler); // abort()
2935 #ifdef CONFIG_CRASH_DEBUG
2936 if (crash_debug)
2937 signal(SIGTRAP,exit_sighandler);
2938 #endif
2939 #endif
2941 #ifdef CONFIG_GUI
2942 if(use_gui){
2943 guiInit();
2944 guiGetEvent(guiSetContext, mpctx);
2945 mpctx->initialized_flags|=INITIALIZED_GUI;
2946 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2948 #endif
2950 // ******************* Now, let's see the per-file stuff ********************
2952 play_next_file:
2954 // init global sub numbers
2955 mpctx->global_sub_size = 0;
2956 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2958 if (mpctx->filename) {
2959 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2960 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2961 load_per_file_config (mpctx->mconfig, mpctx->filename);
2964 if (opts->video_driver_list)
2965 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2966 if (opts->audio_driver_list)
2967 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2969 // We must enable getch2 here to be able to interrupt network connection
2970 // or cache filling
2971 if(!noconsolecontrols && !slave_mode){
2972 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2973 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2974 else
2975 getch2_enable(); // prepare stdin for hotkeys...
2976 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2977 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2980 // =================== GUI idle loop (STOP state) ===========================
2981 #ifdef CONFIG_GUI
2982 if ( use_gui ) {
2983 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2984 guiGetEvent( guiSetDefaults,0 );
2985 while ( guiIntfStruct.Playing != 1 )
2987 mp_cmd_t* cmd;
2988 usec_sleep(20000);
2989 guiEventHandling();
2990 guiGetEvent( guiReDraw,NULL );
2991 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
2992 guiGetEvent(guiIEvent, (char *)cmd->id);
2993 mp_cmd_free(cmd);
2996 guiGetEvent( guiSetParameters,NULL );
2997 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2999 play_tree_t * entry = play_tree_new();
3000 play_tree_add_file( entry,guiIntfStruct.Filename );
3001 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3002 else mpctx->playtree=play_tree_new();
3003 play_tree_set_child( mpctx->playtree,entry );
3004 if(mpctx->playtree)
3006 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3007 if(mpctx->playtree_iter)
3009 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3011 play_tree_iter_free(mpctx->playtree_iter);
3012 mpctx->playtree_iter = NULL;
3014 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3019 #endif /* CONFIG_GUI */
3021 while (player_idle_mode && !mpctx->filename) {
3022 play_tree_t * entry = NULL;
3023 mp_cmd_t * cmd;
3024 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3025 if (mpctx->video_out)
3026 vo_check_events(mpctx->video_out);
3027 usec_sleep(20000);
3029 switch (cmd->id) {
3030 case MP_CMD_LOADFILE:
3031 // prepare a tree entry with the new filename
3032 entry = play_tree_new();
3033 play_tree_add_file(entry, cmd->args[0].v.s);
3034 // The entry is added to the main playtree after the switch().
3035 break;
3036 case MP_CMD_LOADLIST:
3037 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3038 break;
3039 case MP_CMD_QUIT:
3040 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3041 break;
3042 case MP_CMD_GET_PROPERTY:
3043 case MP_CMD_SET_PROPERTY:
3044 case MP_CMD_STEP_PROPERTY:
3045 run_command(mpctx, cmd);
3046 break;
3049 mp_cmd_free(cmd);
3051 if (entry) { // user entered a command that gave a valid entry
3052 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3053 play_tree_free_list(mpctx->playtree->child, 1);
3054 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3056 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3058 play_tree_set_child(mpctx->playtree, entry);
3060 /* Make iterator start at the top the of tree. */
3061 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3062 if (!mpctx->playtree_iter) continue;
3064 // find the first real item in the tree
3065 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3066 // no items!
3067 play_tree_iter_free(mpctx->playtree_iter);
3068 mpctx->playtree_iter = NULL;
3069 continue; // wait for next command
3071 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3074 //---------------------------------------------------------------------------
3076 if(mpctx->filename)
3077 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3078 filename_recode(mpctx->filename));
3080 if (edl_filename) {
3081 if (edl_records) free_edl(edl_records);
3082 next_edl_record = edl_records = edl_parse_file();
3084 if (edl_output_filename) {
3085 if (edl_fd) fclose(edl_fd);
3086 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3088 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3089 filename_recode(edl_output_filename));
3093 //==================== Open VOB-Sub ============================
3095 current_module="vobsub";
3096 if (vobsub_name){
3097 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3098 if(vo_vobsub==NULL)
3099 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3100 filename_recode(vobsub_name));
3101 } else if (sub_auto && mpctx->filename){
3102 /* try to autodetect vobsub from movie filename ::atmos */
3103 char *buf = strdup(mpctx->filename), *psub;
3104 char *pdot = strrchr(buf, '.');
3105 char *pslash = strrchr(buf, '/');
3106 #if defined(__MINGW32__) || defined(__CYGWIN__)
3107 if (!pslash) pslash = strrchr(buf, '\\');
3108 #endif
3109 if (pdot && (!pslash || pdot > pslash))
3110 *pdot = '\0';
3111 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3112 /* try from ~/.mplayer/sub */
3113 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3114 char *bname;
3115 int l;
3116 bname = strrchr(buf,'/');
3117 #if defined(__MINGW32__) || defined(__CYGWIN__)
3118 if(!bname) bname = strrchr(buf,'\\');
3119 #endif
3120 if(bname) bname++;
3121 else bname = buf;
3122 l = strlen(psub) + strlen(bname) + 1;
3123 psub = realloc(psub,l);
3124 strcat(psub,bname);
3125 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3126 free(psub);
3128 free(buf);
3130 if(vo_vobsub){
3131 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3132 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3134 // setup global sub numbering
3135 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3136 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3139 //============ Open & Sync STREAM --- fork cache2 ====================
3141 mpctx->stream=NULL;
3142 mpctx->demuxer=NULL;
3143 if (mpctx->d_audio) {
3144 //free_demuxer_stream(mpctx->d_audio);
3145 mpctx->d_audio=NULL;
3147 if (mpctx->d_video) {
3148 //free_demuxer_stream(d_video);
3149 mpctx->d_video=NULL;
3151 mpctx->sh_audio=NULL;
3152 mpctx->sh_video=NULL;
3154 current_module="open_stream";
3155 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3156 if(!mpctx->stream) { // error...
3157 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3158 goto goto_next_file;
3160 mpctx->initialized_flags|=INITIALIZED_STREAM;
3162 #ifdef CONFIG_GUI
3163 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3164 #endif
3166 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3167 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3168 "playlist support will not be used automatically.\n"
3169 "MPlayer's playlist code is unsafe and should only be used with "
3170 "trusted sources.\nPlayback will probably fail.\n\n");
3171 #if 0
3172 play_tree_t* entry;
3173 // Handle playlist
3174 current_module="handle_playlist";
3175 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3176 filename_recode(mpctx->filename));
3177 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3178 mpctx->eof=playtree_add_playlist(mpctx, entry);
3179 goto goto_next_file;
3180 #endif
3182 mpctx->stream->start_pos+=seek_to_byte;
3184 if(stream_dump_type==5){
3185 unsigned char buf[4096];
3186 int len;
3187 FILE *f;
3188 current_module="dumpstream";
3189 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3190 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3191 exit_player(mpctx, EXIT_ERROR);
3193 stream_reset(mpctx->stream);
3194 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3195 f=fopen(stream_dump_name,"wb");
3196 if(!f){
3197 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3198 exit_player(mpctx, EXIT_ERROR);
3200 if (dvd_chapter > 1) {
3201 int chapter = dvd_chapter - 1;
3202 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3204 while(!mpctx->stream->eof && !async_quit_request){
3205 len=stream_read(mpctx->stream,buf,4096);
3206 if(len>0) {
3207 if(fwrite(buf,len,1,f) != 1) {
3208 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3209 exit_player(mpctx, EXIT_ERROR);
3212 if(dvd_last_chapter > 0) {
3213 int chapter = -1;
3214 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3215 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3216 break;
3219 if(fclose(f)) {
3220 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3221 exit_player(mpctx, EXIT_ERROR);
3223 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3224 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3227 #ifdef CONFIG_DVDREAD
3228 if(mpctx->stream->type==STREAMTYPE_DVD){
3229 current_module="dvd lang->id";
3230 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3231 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3232 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3233 // setup global sub numbering
3234 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3235 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3236 current_module=NULL;
3238 #endif
3240 #ifdef CONFIG_DVDNAV
3241 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3242 current_module="dvdnav lang->id";
3243 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3244 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3245 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3246 // setup global sub numbering
3247 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3248 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3249 current_module=NULL;
3251 #endif
3253 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3254 goto_enable_cache:
3255 if(stream_cache_size>0){
3256 current_module="enable_cache";
3257 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3258 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3259 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3260 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3263 //============ Open DEMUXERS --- DETECT file type =======================
3264 current_module="demux_open";
3266 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3268 // HACK to get MOV Reference Files working
3270 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3272 unsigned char* playlist_entry;
3273 play_tree_t *list = NULL, *entry = NULL;
3275 current_module="handle_demux_playlist";
3276 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3278 char *temp, *bname;
3280 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3281 filename_recode(playlist_entry));
3283 bname=mp_basename(playlist_entry);
3284 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3285 continue;
3287 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3288 continue;
3290 entry = play_tree_new();
3292 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3294 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3295 if (temp)
3297 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3298 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3299 strcat(temp, playlist_entry);
3300 play_tree_add_file(entry,temp);
3301 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3302 free(temp);
3305 else
3306 play_tree_add_file(entry,playlist_entry);
3308 if(!list)
3309 list = entry;
3310 else
3311 play_tree_append_entry(list,entry);
3313 free_demuxer(mpctx->demuxer);
3314 mpctx->demuxer = NULL;
3316 if (list)
3318 entry = play_tree_new();
3319 play_tree_set_child(entry,list);
3320 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3321 goto goto_next_file;
3325 if(!mpctx->demuxer)
3326 goto goto_next_file;
3327 if(dvd_chapter>1) {
3328 float pts;
3329 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3330 seek(mpctx, pts, SEEK_ABSOLUTE);
3333 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3335 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3336 int i;
3337 int maxid = -1;
3338 // setup global sub numbering
3339 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3340 for (i = 0; i < MAX_S_STREAMS; i++)
3341 if (mpctx->demuxer->s_streams[i])
3342 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3343 mpctx->global_sub_size += maxid + 1;
3345 // Make opts->sub_id always selectable if set.
3346 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3347 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3349 #ifdef CONFIG_ASS
3350 if (ass_enabled && ass_library) {
3351 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3352 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3353 if (extract_embedded_fonts &&
3354 att->name && att->type && att->data && att->data_size &&
3355 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3356 strcmp(att->type, "application/x-font") == 0))
3357 ass_add_font(ass_library, att->name, att->data, att->data_size);
3360 #endif
3362 current_module="demux_open2";
3364 //file_format=demuxer->file_format;
3366 mpctx->d_audio=mpctx->demuxer->audio;
3367 mpctx->d_video=mpctx->demuxer->video;
3368 mpctx->d_sub=mpctx->demuxer->sub;
3370 // select audio stream
3371 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3373 // DUMP STREAMS:
3374 if((stream_dump_type)&&(stream_dump_type<4)){
3375 FILE *f;
3376 demux_stream_t *ds=NULL;
3377 current_module="dump";
3378 // select stream to dump
3379 switch(stream_dump_type){
3380 case 1: ds=mpctx->d_audio;break;
3381 case 2: ds=mpctx->d_video;break;
3382 case 3: ds=mpctx->d_sub;break;
3384 if(!ds){
3385 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3386 exit_player(mpctx, EXIT_ERROR);
3388 // disable other streams:
3389 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3390 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3391 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3392 // let's dump it!
3393 f=fopen(stream_dump_name,"wb");
3394 if(!f){
3395 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3396 exit_player(mpctx, EXIT_ERROR);
3398 while(!ds->eof){
3399 unsigned char* start;
3400 int in_size=ds_get_packet(ds,&start);
3401 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3402 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3403 if(in_size>0) fwrite(start,in_size,1,f);
3404 if(dvd_last_chapter>0) {
3405 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3406 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3407 break;
3410 fclose(f);
3411 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3412 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3415 mpctx->sh_audio=mpctx->d_audio->sh;
3416 mpctx->sh_video=mpctx->d_video->sh;
3418 if(mpctx->sh_video){
3420 current_module="video_read_properties";
3421 if(!video_read_properties(mpctx->sh_video)) {
3422 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3423 mpctx->sh_video=mpctx->d_video->sh=NULL;
3424 } else {
3425 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3426 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3427 mpctx->sh_video->fps,mpctx->sh_video->frametime
3430 /* need to set fps here for output encoders to pick it up in their init */
3431 if(force_fps){
3432 mpctx->sh_video->fps=force_fps;
3433 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3435 vo_fps = mpctx->sh_video->fps;
3437 if(!mpctx->sh_video->fps && !force_fps){
3438 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3439 mpctx->sh_video=mpctx->d_video->sh=NULL;
3445 if(!mpctx->sh_video && !mpctx->sh_audio){
3446 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3447 #ifdef CONFIG_DVBIN
3448 if(mpctx->stream->type == STREAMTYPE_DVB)
3450 int dir;
3451 int v = mpctx->last_dvb_step;
3452 if(v > 0)
3453 dir = DVB_CHANNEL_HIGHER;
3454 else
3455 dir = DVB_CHANNEL_LOWER;
3457 if(dvb_step_channel(mpctx->stream, dir)) {
3458 mpctx->stop_play = PT_NEXT_ENTRY;
3459 mpctx->dvbin_reopen = 1;
3462 #endif
3463 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3466 /* display clip info */
3467 demux_info_print(mpctx->demuxer);
3469 //================== Read SUBTITLES (DVD & TEXT) ==========================
3470 if(vo_spudec==NULL && mpctx->sh_video &&
3471 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3472 init_vo_spudec(mpctx);
3475 if(mpctx->sh_video) {
3476 // after reading video params we should load subtitles because
3477 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3478 // check .sub
3479 current_module="read_subtitles_file";
3480 if(sub_name){
3481 for (i = 0; sub_name[i] != NULL; ++i)
3482 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3484 if(sub_auto) { // auto load sub file ...
3485 char *psub = get_path( "sub/" );
3486 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3487 int i = 0;
3488 free(psub); // release the buffer created by get_path() above
3489 while (tmp[i]) {
3490 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3491 free(tmp[i++]);
3493 free(tmp);
3495 if (mpctx->set_of_sub_size > 0) {
3496 // setup global sub numbering
3497 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3498 mpctx->global_sub_size += mpctx->set_of_sub_size;
3502 if (mpctx->global_sub_size) {
3503 // find the best sub to use
3504 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3505 mpctx->global_sub_pos = -1; // no subs by default
3506 if (vobsub_index_id >= 0) {
3507 // if user asks for a vobsub id, use that first.
3508 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3509 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3510 // if user asks for a dvd sub id, use that next.
3511 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3512 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3513 // if there are text subs to use, use those. (autosubs come last here)
3514 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3515 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3516 // finally select subs by language and container hints
3517 if (opts->sub_id < 0 && dvdsub_lang)
3518 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3519 if (opts->sub_id < 0)
3520 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3521 if (opts->sub_id >= 0)
3522 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3524 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3525 mpctx->global_sub_pos--;
3526 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3527 if(subdata)
3528 switch (stream_dump_type) {
3529 case 3: list_sub_file(subdata); break;
3530 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3531 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3532 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3533 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3534 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3539 filename_recode(mpctx->filename));
3540 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3541 if (mpctx->sh_video) {
3542 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3543 if (mpctx->sh_video->format >= 0x20202020)
3544 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3545 else
3546 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3547 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3548 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3549 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3550 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3551 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3553 if (mpctx->sh_audio) {
3554 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3555 if (mpctx->sh_audio->format >= 0x20202020)
3556 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3557 else
3558 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3559 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3560 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3561 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3563 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3564 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3565 if (mpctx->demuxer) {
3566 if (mpctx->demuxer->num_chapters == 0)
3567 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3568 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3571 if(!mpctx->sh_video) goto main; // audio-only
3573 if(!reinit_video_chain(mpctx)) {
3574 if(!mpctx->sh_video){
3575 if(!mpctx->sh_audio) goto goto_next_file;
3576 goto main; // exit_player(MSGTR_Exit_error);
3580 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3581 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3583 #ifdef CONFIG_FREETYPE
3584 force_load_font = 1;
3585 #endif
3587 //================== MAIN: ==========================
3588 main:
3589 current_module="main";
3591 if(playing_msg) {
3592 char* msg = property_expand_string(mpctx, playing_msg);
3593 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3594 free(msg);
3598 // Disable the term OSD in verbose mode
3599 if(verbose) term_osd = 0;
3602 //int frame_corr_num=0; //
3603 //float v_frame=0; // Video
3604 float time_frame=0; // Timer
3605 //float num_frames=0; // number of frames played
3607 int frame_time_remaining=0; // flag
3608 int blit_frame=0;
3609 mpctx->num_buffered_frames=0;
3611 // Make sure old OSD does not stay around,
3612 // e.g. with -fixed-vo and same-resolution files
3613 clear_osd_msgs();
3614 update_osd_msg(mpctx);
3616 //================ SETUP AUDIO ==========================
3618 if(mpctx->sh_audio){
3619 reinit_audio_chain(mpctx);
3620 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3621 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3624 current_module="av_init";
3626 if(mpctx->sh_video){
3627 mpctx->sh_video->timer=0;
3628 if (! ignore_start)
3629 audio_delay += mpctx->sh_video->stream_delay;
3631 if(mpctx->sh_audio){
3632 if (start_volume >= 0)
3633 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3634 if (! ignore_start)
3635 audio_delay -= mpctx->sh_audio->stream_delay;
3636 mpctx->delay=-audio_delay;
3639 if(!mpctx->sh_audio){
3640 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3641 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3642 ds_free_packs(mpctx->d_audio); // free buffered chunks
3643 //mpctx->d_audio->id=-2; // do not read audio chunks
3644 //uninit_player(mpctx, INITIALIZED_AO); // close device
3646 if(!mpctx->sh_video){
3647 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3648 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3649 ds_free_packs(mpctx->d_video);
3650 mpctx->d_video->id=-2;
3651 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3654 if (!mpctx->sh_video && !mpctx->sh_audio)
3655 goto goto_next_file;
3657 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3658 if(force_fps && mpctx->sh_video){
3659 vo_fps = mpctx->sh_video->fps=force_fps;
3660 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3661 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3664 #ifdef CONFIG_GUI
3665 if ( use_gui ) {
3666 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3667 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3668 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3669 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3670 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3672 #endif
3674 mp_input_set_section(mpctx->input, NULL);
3675 //TODO: add desired (stream-based) sections here
3676 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3677 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3679 //==================== START PLAYING =======================
3681 if(opts->loop_times>1) opts->loop_times--; else
3682 if(opts->loop_times==1) opts->loop_times = -1;
3684 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3686 total_time_usage_start=GetTimer();
3687 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3688 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3689 play_n_frames=play_n_frames_mf;
3691 if(play_n_frames==0){
3692 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3695 if (seek_to_sec) {
3696 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3697 end_at.pos += seek_to_sec;
3700 if (end_at.type == END_AT_SIZE) {
3701 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3702 end_at.type = END_AT_NONE;
3705 #ifdef CONFIG_DVDNAV
3706 mp_dvdnav_context_free(mpctx);
3707 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3708 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3709 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3711 #endif
3713 get_relative_time(mpctx); // reset current delta
3715 while(!mpctx->stop_play){
3716 float aq_sleep_time=0;
3718 if(dvd_last_chapter>0) {
3719 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3720 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3721 goto goto_next_file;
3724 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3725 mpctx->sh_audio = mpctx->d_audio->sh;
3726 mpctx->sh_audio->ds = mpctx->d_audio;
3727 reinit_audio_chain(mpctx);
3730 /*========================== PLAY AUDIO ============================*/
3732 if (mpctx->sh_audio)
3733 if (!fill_audio_out_buffers(mpctx))
3734 // at eof, all audio at least written to ao
3735 if (!mpctx->sh_video)
3736 mpctx->stop_play = AT_END_OF_FILE;
3739 if(!mpctx->sh_video) {
3740 // handle audio-only case:
3741 double a_pos=0;
3742 // sh_audio can be NULL due to video stream switching
3743 // TODO: handle this better
3744 if(!quiet || end_at.type == END_AT_TIME && mpctx->sh_audio)
3745 a_pos = playing_audio_pts(mpctx);
3747 if(!quiet)
3748 print_status(mpctx, a_pos, 0, 0);
3750 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3751 mpctx->stop_play = PT_NEXT_ENTRY;
3752 update_osd_msg(mpctx);
3754 } else {
3756 /*========================== PLAY VIDEO ============================*/
3758 vo_pts=mpctx->sh_video->timer*90000.0;
3759 vo_fps=mpctx->sh_video->fps;
3761 if (!mpctx->num_buffered_frames) {
3762 double frame_time = update_video(mpctx, &blit_frame);
3763 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3764 if (mpctx->sh_video->vf_initialized < 0) {
3765 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3766 mpctx->stop_play = PT_NEXT_ENTRY;
3767 goto goto_next_file;
3769 if (frame_time < 0)
3770 mpctx->stop_play = AT_END_OF_FILE;
3771 else {
3772 // might return with !eof && !blit_frame if !correct_pts
3773 mpctx->num_buffered_frames += blit_frame;
3774 time_frame += frame_time / opts->playback_speed; // for nosound
3778 // ==========================================================================
3780 // current_module="draw_osd";
3781 // if(vo_config_count) mpctx->video_out->draw_osd();
3783 #ifdef CONFIG_GUI
3784 if(use_gui) guiEventHandling();
3785 #endif
3787 current_module="vo_check_events";
3788 vo_check_events(mpctx->video_out);
3790 #ifdef CONFIG_X11
3791 if (stop_xscreensaver) {
3792 current_module = "stop_xscreensaver";
3793 xscreensaver_heartbeat(mpctx->x11_state);
3795 #endif
3796 if (heartbeat_cmd) {
3797 static unsigned last_heartbeat;
3798 unsigned now = GetTimerMS();
3799 if (now - last_heartbeat > 30000) {
3800 last_heartbeat = now;
3801 system(heartbeat_cmd);
3805 frame_time_remaining = sleep_until_update(mpctx, &time_frame, &aq_sleep_time);
3807 //====================== FLIP PAGE (VIDEO BLT): =========================
3809 current_module="flip_page";
3810 if (!frame_time_remaining && blit_frame) {
3811 unsigned int t2=GetTimer();
3813 vo_flip_page(mpctx->video_out);
3814 mpctx->num_buffered_frames--;
3816 vout_time_usage += (GetTimer() - t2) * 0.000001;
3818 //====================== A-V TIMESTAMP CORRECTION: =========================
3820 adjust_sync_and_print_status(mpctx, frame_time_remaining, time_frame);
3822 //============================ Auto QUALITY ============================
3824 /*Output quality adjustments:*/
3825 if(auto_quality>0){
3826 current_module="autoq";
3827 // float total=0.000001f * (GetTimer()-aq_total_time);
3828 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3829 if(output_quality<auto_quality && aq_sleep_time>0)
3830 ++output_quality;
3831 else
3832 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3833 if(output_quality>1 && aq_sleep_time<0)
3834 --output_quality;
3835 else
3836 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3837 output_quality=0;
3838 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3839 set_video_quality(mpctx->sh_video,output_quality);
3842 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3843 --play_n_frames;
3844 if (play_n_frames <= 0) mpctx->stop_play = PT_NEXT_ENTRY;
3848 // FIXME: add size based support for -endpos
3849 if (end_at.type == END_AT_TIME &&
3850 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3851 mpctx->stop_play = PT_NEXT_ENTRY;
3853 } // end if(mpctx->sh_video)
3855 #ifdef CONFIG_DVDNAV
3856 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3857 nav_highlight_t hl;
3858 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3859 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3860 vo_osd_changed (OSDTYPE_DVDNAV);
3862 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3863 double ar = -1.0;
3864 if (stream_control (mpctx->demuxer->stream,
3865 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3866 != STREAM_UNSUPPORTED)
3867 mpctx->sh_video->stream_aspect = ar;
3870 #endif
3872 //============================ Handle PAUSE ===============================
3874 current_module="pause";
3876 if (mpctx->osd_function == OSD_PAUSE) {
3877 mpctx->was_paused = 1;
3878 pause_loop(mpctx);
3881 // handle -sstep
3882 if(step_sec>0) {
3883 mpctx->osd_function=OSD_FFW;
3884 mpctx->rel_seek_secs+=step_sec;
3887 edl_update(mpctx);
3889 //================= Keyboard events, SEEKing ====================
3891 current_module="key_events";
3894 mp_cmd_t* cmd;
3895 int brk_cmd = 0;
3896 while( !brk_cmd && (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3897 brk_cmd = run_command(mpctx, cmd);
3898 mp_cmd_free(cmd);
3899 if (brk_cmd == 2)
3900 goto goto_enable_cache;
3903 mpctx->was_paused = 0;
3905 /* Looping. */
3906 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3907 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3909 if(opts->loop_times>1) opts->loop_times--; else
3910 if(opts->loop_times==1) opts->loop_times=-1;
3911 play_n_frames=play_n_frames_mf;
3912 mpctx->stop_play=0;
3913 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3914 loop_seek = 1;
3917 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3918 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3919 // Set OSD:
3920 if(!loop_seek){
3921 if( !edl_decision )
3922 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3926 mpctx->rel_seek_secs=0;
3927 mpctx->abs_seek_pos=0;
3928 loop_seek=0;
3929 edl_decision = 0;
3932 #ifdef CONFIG_GUI
3933 if(use_gui){
3934 guiEventHandling();
3935 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3936 // get pos from frame number / total frames
3937 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3938 } else {
3939 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3941 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3942 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3943 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3944 guiGetEvent( guiReDraw,NULL );
3945 guiGetEvent( guiSetVolume,NULL );
3946 if(guiIntfStruct.Playing==0) break; // STOP
3947 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3948 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3949 #ifdef CONFIG_DVDREAD
3950 if ( mpctx->stream->type == STREAMTYPE_DVD )
3952 dvd_priv_t * dvdp = mpctx->stream->priv;
3953 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3955 #endif
3957 #endif /* CONFIG_GUI */
3959 } // while(!mpctx->stop_play)
3961 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
3963 #ifdef CONFIG_DVBIN
3964 if(mpctx->dvbin_reopen)
3966 mpctx->stop_play = 0;
3967 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3968 cache_uninit(mpctx->stream);
3969 mpctx->dvbin_reopen = 0;
3970 goto goto_enable_cache;
3972 #endif
3975 goto_next_file: // don't jump here after ao/vo/getch initialization!
3977 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3979 if(benchmark){
3980 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3981 double total_time_usage;
3982 total_time_usage_start=GetTimer()-total_time_usage_start;
3983 total_time_usage = (float)total_time_usage_start*0.000001;
3984 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3985 video_time_usage,vout_time_usage,audio_time_usage,
3986 total_time_usage-tot,total_time_usage);
3987 if(total_time_usage>0.0)
3988 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3989 100.0*video_time_usage/total_time_usage,
3990 100.0*vout_time_usage/total_time_usage,
3991 100.0*audio_time_usage/total_time_usage,
3992 100.0*(total_time_usage-tot)/total_time_usage,
3993 100.0);
3994 if(total_frame_cnt && frame_dropping)
3995 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3996 total_frame_cnt-drop_frame_cnt,
3997 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3998 drop_frame_cnt,
3999 100*drop_frame_cnt/total_frame_cnt,
4000 total_frame_cnt,
4001 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4004 // time to uninit all, except global stuff:
4005 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4007 if(mpctx->set_of_sub_size > 0) {
4008 current_module="sub_free";
4009 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4010 sub_free(mpctx->set_of_subtitles[i]);
4011 #ifdef CONFIG_ASS
4012 if(mpctx->set_of_ass_tracks[i])
4013 ass_free_track( mpctx->set_of_ass_tracks[i] );
4014 #endif
4016 mpctx->set_of_sub_size = 0;
4018 vo_sub_last = vo_sub=NULL;
4019 subdata=NULL;
4020 #ifdef CONFIG_ASS
4021 ass_track = NULL;
4022 if(ass_library)
4023 ass_clear_fonts(ass_library);
4024 #endif
4026 if (!mpctx->stop_play) // In case some goto jumped here...
4027 mpctx->stop_play = PT_NEXT_ENTRY;
4029 int playtree_direction = 1;
4031 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4032 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4033 play_tree_iter_free(mpctx->playtree_iter);
4034 mpctx->playtree_iter = NULL;
4036 mpctx->play_tree_step = 1;
4037 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4038 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4039 if(mpctx->playtree_iter) {
4040 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4041 play_tree_iter_free(mpctx->playtree_iter);
4042 mpctx->playtree_iter = NULL;
4045 } else if (mpctx->stop_play == PT_STOP) {
4046 play_tree_iter_free(mpctx->playtree_iter);
4047 mpctx->playtree_iter = NULL;
4048 } else { // NEXT PREV SRC
4049 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4052 while(mpctx->playtree_iter != NULL) {
4053 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4054 if(mpctx->filename == NULL) {
4055 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4056 play_tree_iter_free(mpctx->playtree_iter);
4057 mpctx->playtree_iter = NULL;
4059 } else
4060 break;
4063 #ifdef CONFIG_GUI
4064 if(use_gui && !mpctx->playtree_iter) {
4065 #ifdef CONFIG_DVDREAD
4066 if(!guiIntfStruct.DiskChanged)
4067 #endif
4068 mplEnd();
4070 #endif
4072 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4073 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4074 mpctx->stop_play = 0;
4075 goto play_next_file;
4079 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4081 return 1;
4083 #endif /* DISABLE_MAIN */