Fix dvdnav call broken in pause changes
[mplayer.git] / mplayer.c
blob955c1437863e6b7c743db8deed94f2dad2a0faf4
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include "config.h"
9 #include "talloc.h"
11 #if defined(__MINGW32__) || defined(__CYGWIN__)
12 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
13 #include <windows.h>
14 #endif
15 #include <string.h>
16 #include <unistd.h>
18 // #include <sys/mman.h>
19 #include <sys/types.h>
20 #ifndef __MINGW32__
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #else
24 #define SIGHUP 1 /* hangup */
25 #define SIGQUIT 3 /* quit */
26 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
27 #define SIGBUS 10 /* bus error */
28 #define SIGPIPE 13 /* broken pipe */
29 #endif
31 #include <sys/time.h>
32 #include <sys/stat.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <limits.h>
39 #include <errno.h>
41 #include "version.h"
43 #include "mp_msg.h"
45 #define HELP_MP_DEFINE_STATIC
46 #include "help_mp.h"
48 #include "m_option.h"
49 #include "m_config.h"
50 #include "mplayer.h"
51 #include "access_mpcontext.h"
52 #include "m_property.h"
54 #include "cfg-mplayer-def.h"
56 #include "libavutil/intreadwrite.h"
58 #include "subreader.h"
60 #include "mp_osd.h"
61 #include "libvo/video_out.h"
63 #include "libvo/font_load.h"
64 #include "libvo/sub.h"
66 #ifdef CONFIG_X11
67 #include "libvo/x11_common.h"
68 #endif
70 #include "libao2/audio_out.h"
72 #include "codec-cfg.h"
74 #include "edl.h"
76 #include "spudec.h"
77 #include "vobsub.h"
79 #include "osdep/getch2.h"
80 #include "osdep/timer.h"
82 #include "cpudetect.h"
84 #ifdef CONFIG_GUI
85 #include "gui/interface.h"
86 #endif
88 #include "input/input.h"
90 const int under_mencoder = 0;
91 int slave_mode=0;
92 int player_idle_mode=0;
93 int quiet=0;
94 int enable_mouse_movements=0;
95 float start_volume = -1;
97 #if defined(__MINGW32__) || defined(__CYGWIN__)
98 char * proc_priority=NULL;
99 #endif
101 char *heartbeat_cmd;
103 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
105 #ifdef HAVE_RTC
106 #ifdef __linux__
107 #include <linux/rtc.h>
108 #else
109 #include <rtc.h>
110 #define RTC_IRQP_SET RTCIO_IRQP_SET
111 #define RTC_PIE_ON RTCIO_PIE_ON
112 #endif /* __linux__ */
113 #endif /* HAVE_RTC */
115 #ifdef CONFIG_TV
116 #include "stream/tv.h"
117 #endif
118 #ifdef CONFIG_RADIO
119 #include "stream/stream_radio.h"
120 #endif
122 #ifdef CONFIG_DVBIN
123 #include "stream/dvbin.h"
124 #include "stream/cache2.h"
125 #endif
127 //**************************************************************************//
128 // Playtree
129 //**************************************************************************//
130 #include "playtree.h"
131 #include "playtreeparser.h"
133 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
134 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
136 //**************************************************************************//
137 // Config
138 //**************************************************************************//
139 #include "parser-cfg.h"
140 #include "parser-mpcmd.h"
142 //**************************************************************************//
143 // Config file
144 //**************************************************************************//
146 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
148 #include "get_path.h"
150 //**************************************************************************//
151 //**************************************************************************//
152 // Input media streaming & demultiplexer:
153 //**************************************************************************//
155 static int max_framesize=0;
157 #include "stream/stream.h"
158 #include "libmpdemux/demuxer.h"
159 #include "libmpdemux/stheader.h"
161 #ifdef CONFIG_DVDREAD
162 #include "stream/stream_dvd.h"
163 #endif
165 #ifdef CONFIG_DVDNAV
166 #include "stream/stream_dvdnav.h"
167 #endif
169 #include "libmpcodecs/dec_audio.h"
170 #include "libmpcodecs/dec_video.h"
171 #include "libmpcodecs/mp_image.h"
172 #include "libmpcodecs/vf.h"
173 #include "libmpcodecs/vd.h"
175 #include "mixer.h"
177 #include "mp_core.h"
178 #include "options.h"
179 #include "defaultopts.h"
181 //**************************************************************************//
182 //**************************************************************************//
184 // Common FIFO functions, and keyboard/event FIFO code
185 #include "mp_fifo.h"
186 int noconsolecontrols=0;
187 //**************************************************************************//
189 // benchmark:
190 double video_time_usage=0;
191 double vout_time_usage=0;
192 static double audio_time_usage=0;
193 static int total_time_usage_start=0;
194 static int total_frame_cnt=0;
195 static int drop_frame_cnt=0; // total number of dropped frames
196 int benchmark=0;
198 // options:
199 int auto_quality=0;
200 static int output_quality=0;
202 int use_gui=0;
204 #ifdef CONFIG_GUI
205 int enqueue=0;
206 #endif
208 static int list_properties = 0;
210 int osd_level=1;
211 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
212 unsigned int osd_visible;
213 int osd_duration = 1000;
215 int term_osd = 1;
216 static char* term_osd_esc = "\x1b[A\r\x1b[K";
217 static char* playing_msg = NULL;
218 // seek:
219 static double seek_to_sec;
220 static off_t seek_to_byte=0;
221 static off_t step_sec=0;
222 static int loop_seek=0;
224 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
226 // A/V sync:
227 int autosync=0; // 30 might be a good default value.
229 // codecs:
230 char **audio_codec_list=NULL; // override audio codec
231 char **video_codec_list=NULL; // override video codec
232 char **audio_fm_list=NULL; // override audio codec family
233 char **video_fm_list=NULL; // override video codec family
235 // demuxer:
236 extern char *demuxer_name; // override demuxer
237 extern char *audio_demuxer_name; // override audio demuxer
238 extern char *sub_demuxer_name; // override sub demuxer
240 int vobsub_id=-1;
241 char* audio_lang=NULL;
242 char* dvdsub_lang=NULL;
243 static char* spudec_ifo=NULL;
244 int forced_subs_only=0;
245 int file_filter=1;
247 // cache2:
248 int stream_cache_size=-1;
249 #ifdef CONFIG_STREAM_CACHE
250 extern int cache_fill_status;
252 float stream_cache_min_percent=20.0;
253 float stream_cache_seek_min_percent=50.0;
254 #else
255 #define cache_fill_status 0
256 #endif
258 // dump:
259 static char *stream_dump_name="stream.dump";
260 int stream_dump_type=0;
262 // A-V sync:
263 static float default_max_pts_correction=-1;//0.01f;
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);
1163 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1165 struct MPOpts *opts = &mpctx->opts;
1166 sh_video_t * const sh_video = mpctx->sh_video;
1168 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1169 a_pos = playing_audio_pts(mpctx);
1170 if (mpctx->sh_audio && sh_video && at_frame) {
1171 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1172 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1173 && !mpctx->drop_message_shown) {
1174 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1175 mpctx->drop_message_shown = true;
1178 if (quiet)
1179 return;
1182 int width;
1183 char *line;
1184 unsigned pos = 0;
1185 get_screen_size();
1186 if (screen_width > 0)
1187 width = screen_width;
1188 else
1189 width = 80;
1190 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1191 /* Windows command line is broken (MinGW's rxvt works, but we
1192 * should not depend on that). */
1193 width--;
1194 #endif
1195 line = malloc(width + 1); // one additional char for the terminating null
1197 // Audio time
1198 if (mpctx->sh_audio) {
1199 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1200 if (!sh_video) {
1201 float len = demuxer_get_time_length(mpctx->demuxer);
1202 saddf(line, &pos, width, "(");
1203 sadd_hhmmssf(line, &pos, width, a_pos);
1204 saddf(line, &pos, width, ") of %.1f (", len);
1205 sadd_hhmmssf(line, &pos, width, len);
1206 saddf(line, &pos, width, ") ");
1210 // Video time
1211 if (sh_video)
1212 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1214 // A-V sync
1215 if (mpctx->sh_audio && sh_video)
1216 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1217 mpctx->last_av_difference, mpctx->total_avsync_change);
1219 // Video stats
1220 if (sh_video)
1221 saddf(line, &pos, width, "%3d/%3d ",
1222 (int)sh_video->num_frames,
1223 (int)sh_video->num_frames_decoded);
1225 // CPU usage
1226 if (sh_video) {
1227 if (sh_video->timer > 0.5)
1228 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1229 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1230 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1231 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1232 else
1233 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1234 } else if (mpctx->sh_audio) {
1235 if (mpctx->delay > 0.5)
1236 saddf(line, &pos, width, "%4.1f%% ",
1237 100.0*audio_time_usage/(double)mpctx->delay);
1238 else
1239 saddf(line, &pos, width, "??,?%% ");
1242 // VO stats
1243 if (sh_video)
1244 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1246 #ifdef CONFIG_STREAM_CACHE
1247 // cache stats
1248 if (stream_cache_size > 0)
1249 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1250 #endif
1252 // other
1253 if (opts->playback_speed != 1)
1254 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1256 // end
1257 if (erase_to_end_of_line) {
1258 line[pos] = 0;
1259 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1260 } else {
1261 memset(&line[pos], ' ', width - pos);
1262 line[width] = 0;
1263 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1265 free(line);
1269 * \brief build a chain of audio filters that converts the input format
1270 * to the ao's format, taking into account the current playback_speed.
1271 * \param sh_audio describes the requested input format of the chain.
1272 * \param ao_data describes the requested output format of the chain.
1274 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1276 struct MPOpts *opts = &mpctx->opts;
1277 int new_srate;
1278 int result;
1279 if (!sh_audio)
1281 #ifdef CONFIG_GUI
1282 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1283 #endif
1284 mpctx->mixer.afilter = NULL;
1285 return 0;
1287 if(af_control_any_rev(sh_audio->afilter,
1288 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1289 &opts->playback_speed)) {
1290 new_srate = sh_audio->samplerate;
1291 } else {
1292 new_srate = sh_audio->samplerate * opts->playback_speed;
1293 if (new_srate != ao_data->samplerate) {
1294 // limits are taken from libaf/af_resample.c
1295 if (new_srate < 8000)
1296 new_srate = 8000;
1297 if (new_srate > 192000)
1298 new_srate = 192000;
1299 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1302 result = init_audio_filters(sh_audio, new_srate,
1303 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1304 mpctx->mixer.afilter = sh_audio->afilter;
1305 #ifdef CONFIG_GUI
1306 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1307 #endif
1308 return result;
1312 typedef struct mp_osd_msg mp_osd_msg_t;
1313 struct mp_osd_msg {
1314 /// Previous message on the stack.
1315 mp_osd_msg_t* prev;
1316 /// Message text.
1317 char msg[64];
1318 int id,level,started;
1319 /// Display duration in ms.
1320 unsigned time;
1323 /// OSD message stack.
1324 static mp_osd_msg_t* osd_msg_stack = NULL;
1327 * \brief Add a message on the OSD message stack
1329 * If a message with the same id is already present in the stack
1330 * it is pulled on top of the stack, otherwise a new message is created.
1334 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1335 mp_osd_msg_t *msg,*last=NULL;
1336 va_list va;
1337 int r;
1339 // look if the id is already in the stack
1340 for(msg = osd_msg_stack ; msg && msg->id != id ;
1341 last = msg, msg = msg->prev);
1342 // not found: alloc it
1343 if(!msg) {
1344 msg = calloc(1,sizeof(mp_osd_msg_t));
1345 msg->prev = osd_msg_stack;
1346 osd_msg_stack = msg;
1347 } else if(last) { // found, but it's not on top of the stack
1348 last->prev = msg->prev;
1349 msg->prev = osd_msg_stack;
1350 osd_msg_stack = msg;
1352 // write the msg
1353 va_start(va,fmt);
1354 r = vsnprintf(msg->msg, 64, fmt, va);
1355 va_end(va);
1356 if(r >= 64) msg->msg[63] = 0;
1357 // set id and time
1358 msg->id = id;
1359 msg->level = level;
1360 msg->time = time;
1365 * \brief Remove a message from the OSD stack
1367 * This function can be used to get rid of a message right away.
1371 void rm_osd_msg(int id) {
1372 mp_osd_msg_t *msg,*last=NULL;
1374 // Search for the msg
1375 for(msg = osd_msg_stack ; msg && msg->id != id ;
1376 last = msg, msg = msg->prev);
1377 if(!msg) return;
1379 // Detach it from the stack and free it
1380 if(last)
1381 last->prev = msg->prev;
1382 else
1383 osd_msg_stack = msg->prev;
1384 free(msg);
1388 * \brief Remove all messages from the OSD stack
1392 static void clear_osd_msgs(void) {
1393 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1394 while(msg) {
1395 prev = msg->prev;
1396 free(msg);
1397 msg = prev;
1399 osd_msg_stack = NULL;
1403 * \brief Get the current message from the OSD stack.
1405 * This function decrements the message timer and destroys the old ones.
1406 * The message that should be displayed is returned (if any).
1410 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1412 mp_osd_msg_t *msg,*prev,*last = NULL;
1413 static unsigned last_update = 0;
1414 unsigned now = GetTimerMS();
1415 unsigned diff;
1416 char hidden_dec_done = 0;
1418 if (osd_visible) {
1419 // 36000000 means max timed visibility is 1 hour into the future, if
1420 // the difference is greater assume it's wrapped around from below 0
1421 if (osd_visible - now > 36000000) {
1422 osd_visible = 0;
1423 vo_osd_progbar_type = -1; // disable
1424 vo_osd_changed(OSDTYPE_PROGBAR);
1425 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1429 if(!last_update) last_update = now;
1430 diff = now >= last_update ? now - last_update : 0;
1432 last_update = now;
1434 // Look for the first message in the stack with high enough level.
1435 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1436 prev = msg->prev;
1437 if(msg->level > osd_level && hidden_dec_done) continue;
1438 // The message has a high enough level or it is the first hidden one
1439 // in both cases we decrement the timer or kill it.
1440 if(!msg->started || msg->time > diff) {
1441 if(msg->started) msg->time -= diff;
1442 else msg->started = 1;
1443 // display it
1444 if(msg->level <= osd_level) return msg;
1445 hidden_dec_done = 1;
1446 continue;
1448 // kill the message
1449 free(msg);
1450 if(last) {
1451 last->prev = prev;
1452 msg = last;
1453 } else {
1454 osd_msg_stack = prev;
1455 msg = NULL;
1458 // Nothing found
1459 return NULL;
1463 * \brief Display the OSD bar.
1465 * Display the OSD bar or fall back on a simple message.
1469 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1471 if(osd_level < 1) return;
1473 if(mpctx->sh_video) {
1474 osd_visible = (GetTimerMS() + 1000) | 1;
1475 vo_osd_progbar_type = type;
1476 vo_osd_progbar_value = 256*(val-min)/(max-min);
1477 vo_osd_changed(OSDTYPE_PROGBAR);
1478 return;
1481 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1482 name,ROUND(100*(val-min)/(max-min)));
1487 * \brief Update the OSD message line.
1489 * This function displays the current message on the vo OSD or on the term.
1490 * If the stack is empty and the OSD level is high enough the timer
1491 * is displayed (only on the vo OSD).
1495 static void update_osd_msg(struct MPContext *mpctx)
1497 mp_osd_msg_t *msg;
1498 struct osd_state *osd = mpctx->osd;
1499 char osd_text_timer[64];
1501 // Look if we have a msg
1502 if((msg = get_osd_msg(mpctx))) {
1503 if (strcmp(osd->osd_text, msg->msg)) {
1504 strncpy(osd->osd_text, msg->msg, 63);
1505 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1506 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1508 return;
1511 if(mpctx->sh_video) {
1512 // fallback on the timer
1513 if(osd_level>=2) {
1514 int len = demuxer_get_time_length(mpctx->demuxer);
1515 int percentage = -1;
1516 char percentage_text[10];
1517 int pts = demuxer_get_current_time(mpctx->demuxer);
1519 if (mpctx->osd_show_percentage)
1520 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1522 if (percentage >= 0)
1523 snprintf(percentage_text, 9, " (%d%%)", percentage);
1524 else
1525 percentage_text[0] = 0;
1527 if (osd_level == 3)
1528 snprintf(osd_text_timer, 63,
1529 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1530 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1531 len/3600,(len/60)%60,len%60,percentage_text);
1532 else
1533 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1534 mpctx->osd_function,pts/3600,(pts/60)%60,
1535 pts%60,percentage_text);
1536 } else
1537 osd_text_timer[0]=0;
1539 // always decrement the percentage timer
1540 if(mpctx->osd_show_percentage)
1541 mpctx->osd_show_percentage--;
1543 if (strcmp(osd->osd_text, osd_text_timer)) {
1544 strncpy(osd->osd_text, osd_text_timer, 63);
1545 vo_osd_changed(OSDTYPE_OSD);
1547 return;
1550 // Clear the term osd line
1551 if (term_osd && osd->osd_text[0]) {
1552 osd->osd_text[0] = 0;
1553 printf("%s\n",term_osd_esc);
1557 ///@}
1558 // OSDMsgStack
1561 void reinit_audio_chain(struct MPContext *mpctx)
1563 struct MPOpts *opts = &mpctx->opts;
1564 if(mpctx->sh_audio){
1565 current_module="init_audio_codec";
1566 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1567 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1568 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1569 mpctx->d_audio->id = -2;
1570 return;
1571 } else
1572 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1573 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1576 //const ao_info_t *info=audio_out->info;
1577 current_module="af_preinit";
1578 ao_data.samplerate=force_srate;
1579 ao_data.channels=0;
1580 ao_data.format=audio_output_format;
1581 #if 1
1582 // first init to detect best values
1583 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1584 // input:
1585 mpctx->sh_audio->samplerate,
1586 // output:
1587 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1588 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1589 exit_player(mpctx, EXIT_ERROR);
1591 #endif
1592 current_module="ao2_init";
1593 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1594 0, // plugin flag
1595 ao_data.samplerate,
1596 ao_data.channels,
1597 ao_data.format,0))){
1598 // FAILED:
1599 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1600 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1601 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1602 mpctx->d_audio->id = -2;
1603 return;
1604 } else {
1605 // SUCCESS:
1606 mpctx->initialized_flags|=INITIALIZED_AO;
1607 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1608 mpctx->audio_out->info->short_name,
1609 ao_data.samplerate, ao_data.channels,
1610 af_fmt2str_short(ao_data.format),
1611 af_fmt2bits(ao_data.format)/8 );
1612 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1613 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1614 if(strlen(mpctx->audio_out->info->comment) > 0)
1615 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1616 // init audio filters:
1617 #if 1
1618 current_module="af_init";
1619 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1620 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1621 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1622 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1623 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1625 #endif
1627 mpctx->mixer.audio_out = mpctx->audio_out;
1628 mpctx->mixer.volstep = volstep;
1633 ///@}
1634 // Command2Property
1637 // Return pts value corresponding to the end point of audio written to the
1638 // ao so far.
1639 static double written_audio_pts(struct MPContext *mpctx)
1641 sh_audio_t *sh_audio = mpctx->sh_audio;
1642 demux_stream_t *d_audio = mpctx->d_audio;
1643 double buffered_output;
1644 // first calculate the end pts of audio that has been output by decoder
1645 double a_pts = sh_audio->pts;
1646 if (a_pts != MP_NOPTS_VALUE)
1647 // Good, decoder supports new way of calculating audio pts.
1648 // sh_audio->pts is the timestamp of the latest input packet with
1649 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1650 // the amount of bytes the decoder has written after that timestamp.
1651 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1652 else {
1653 // Decoder doesn't support new way of calculating pts (or we're
1654 // being called before it has decoded anything with known timestamp).
1655 // Use the old method of audio pts calculation: take the timestamp
1656 // of last packet with known pts the decoder has read data from,
1657 // and add amount of bytes read after the beginning of that packet
1658 // divided by input bps. This will be inaccurate if the input/output
1659 // ratio is not constant for every audio packet or if it is constant
1660 // but not accurately known in sh_audio->i_bps.
1662 a_pts = d_audio->pts;
1663 // ds_tell_pts returns bytes read after last timestamp from
1664 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1665 // it has read but not decoded
1666 if (sh_audio->i_bps)
1667 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1668 (double)sh_audio->i_bps;
1670 // Now a_pts hopefully holds the pts for end of audio from decoder.
1671 // Substract data in buffers between decoder and audio out.
1673 // Decoded but not filtered
1674 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1676 // Data buffered in audio filters, measured in bytes of "missing" output
1677 buffered_output = af_calc_delay(sh_audio->afilter);
1679 // Data that was ready for ao but was buffered because ao didn't fully
1680 // accept everything to internal buffers yet
1681 buffered_output += sh_audio->a_out_buffer_len;
1683 // Filters divide audio length by playback_speed, so multiply by it
1684 // to get the length in original units without speedup or slowdown
1685 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1687 return a_pts;
1690 // Return pts value corresponding to currently playing audio.
1691 double playing_audio_pts(struct MPContext *mpctx)
1693 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1694 mpctx->audio_out->get_delay();
1697 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1698 struct MPOpts *opts = &mpctx->opts;
1699 // check for frame-drop:
1700 current_module = "check_framedrop";
1701 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1702 static int dropped_frames;
1703 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1704 float d = delay-mpctx->delay;
1705 ++total_frame_cnt;
1706 // we should avoid dropping too many frames in sequence unless we
1707 // are too late. and we allow 100ms A-V delay here:
1708 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1709 && !mpctx->update_video_immediately) {
1710 ++drop_frame_cnt;
1711 ++dropped_frames;
1712 return frame_dropping;
1713 } else
1714 dropped_frames = 0;
1716 return 0;
1720 #ifdef HAVE_RTC
1721 int rtc_fd = -1;
1722 #endif
1724 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1726 #ifdef HAVE_RTC
1727 if (rtc_fd >= 0){
1728 // -------- RTC -----------
1729 current_module="sleep_rtc";
1730 while (time_frame > 0.000) {
1731 unsigned long rtc_ts;
1732 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1733 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1734 time_frame -= get_relative_time(mpctx);
1736 } else
1737 #endif
1739 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1740 // unnecessarily high CPU usage
1741 float margin = softsleep ? 0.011 : 0;
1742 current_module = "sleep_timer";
1743 while (time_frame > margin) {
1744 usec_sleep(1000000 * (time_frame - margin));
1745 time_frame -= get_relative_time(mpctx);
1747 if (softsleep){
1748 current_module = "sleep_soft";
1749 if (time_frame < 0)
1750 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1751 while (time_frame > 0)
1752 time_frame -= get_relative_time(mpctx); // burn the CPU
1755 return time_frame;
1758 #ifdef CONFIG_DVDNAV
1759 #ifndef FF_B_TYPE
1760 #define FF_B_TYPE 3
1761 #endif
1762 /// store decoded video image
1763 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1764 mp_image_t *from_mpi) {
1765 mp_image_t *mpi;
1767 /// Do not store B-frames
1768 if (from_mpi->pict_type == FF_B_TYPE)
1769 return to_mpi;
1771 if (to_mpi &&
1772 to_mpi->w == from_mpi->w &&
1773 to_mpi->h == from_mpi->h &&
1774 to_mpi->imgfmt == from_mpi->imgfmt)
1775 mpi = to_mpi;
1776 else {
1777 if (to_mpi)
1778 free_mp_image(to_mpi);
1779 if (from_mpi->w == 0 || from_mpi->h == 0)
1780 return NULL;
1781 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1784 copy_mpi(mpi,from_mpi);
1785 return mpi;
1788 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1789 struct MPOpts *opts = &ctx->opts;
1790 if (ctx->sh_video) {
1791 /// clear video pts
1792 ctx->d_video->pts = 0.0f;
1793 ctx->sh_video->pts = 0.0f;
1794 ctx->sh_video->i_pts = 0.0f;
1795 ctx->sh_video->last_pts = 0.0f;
1796 ctx->sh_video->num_buffered_pts = 0;
1797 ctx->sh_video->num_frames = 0;
1798 ctx->sh_video->num_frames_decoded = 0;
1799 ctx->sh_video->timer = 0.0f;
1800 ctx->sh_video->stream_delay = 0.0f;
1801 ctx->sh_video->timer = 0;
1802 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1805 if (ctx->sh_audio) {
1806 /// free audio packets and reset
1807 ds_free_packs(ctx->d_audio);
1808 audio_delay -= ctx->sh_audio->stream_delay;
1809 ctx->delay =- audio_delay;
1810 ctx->audio_out->reset();
1811 resync_audio_stream(ctx->sh_audio);
1814 if (ctx->d_sub) opts->sub_id = -2;
1816 audio_delay = 0.0f;
1818 /// clear all EOF related flags
1819 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1822 /// Restore last decoded DVDNAV (still frame)
1823 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1824 int *in_size,
1825 unsigned char **start,
1826 mp_image_t *decoded_frame)
1828 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1829 return decoded_frame;
1831 /// a change occured in dvdnav stream
1832 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1833 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1834 mp_dvdnav_context_free(mpctx);
1835 mp_dvdnav_reset_stream(mpctx);
1836 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1837 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1840 if (*in_size < 0) {
1841 float len;
1843 /// Display still frame, if any
1844 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1845 decoded_frame = mpctx->nav_smpi;
1847 /// increment video frame : continue playing after still frame
1848 len = demuxer_get_time_length(mpctx->demuxer);
1849 if (mpctx->sh_video->pts >= len &&
1850 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1851 mp_dvdnav_skip_still(mpctx->stream);
1852 mp_dvdnav_skip_wait(mpctx->stream);
1854 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1856 if (mpctx->nav_buffer) {
1857 *start = mpctx->nav_start;
1858 *in_size = mpctx->nav_in_size;
1859 if (mpctx->nav_start)
1860 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1864 return decoded_frame;
1867 /// Save last decoded DVDNAV (still frame)
1868 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1869 unsigned char *start,
1870 mp_image_t *decoded_frame)
1872 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1873 return;
1875 if (mpctx->nav_buffer)
1876 free(mpctx->nav_buffer);
1878 mpctx->nav_buffer = malloc(in_size);
1879 mpctx->nav_start = start;
1880 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1881 if (mpctx->nav_buffer)
1882 memcpy(mpctx->nav_buffer,start,in_size);
1884 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1885 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1887 #endif /* CONFIG_DVDNAV */
1889 /* Modify video timing to match the audio timeline. There are two main
1890 * reasons this is needed. First, video and audio can start from different
1891 * positions at beginning of file or after a seek (MPlayer starts both
1892 * immediately even if they have different pts). Second, the file can have
1893 * audio timestamps that are inconsistent with the duration of the audio
1894 * packets, for example two consecutive timestamp values differing by
1895 * one second but only a packet with enough samples for half a second
1896 * of playback between them.
1898 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1900 struct MPOpts *opts = &mpctx->opts;
1901 current_module = "av_sync";
1903 if (!mpctx->sh_audio)
1904 return;
1906 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1907 double v_pts = mpctx->sh_video->pts;
1908 double av_delay = a_pts - v_pts;
1909 // Try to sync vo_flip() so it will *finish* at given time
1910 av_delay += mpctx->last_vo_flip_duration;
1911 av_delay -= audio_delay; // This much pts difference is desired
1913 double change = av_delay * 0.1;
1914 double max_change = default_max_pts_correction >= 0 ?
1915 default_max_pts_correction : frame_time * 0.1;
1916 if (change < -max_change)
1917 change = -max_change;
1918 else if (change > max_change)
1919 change = max_change;
1920 mpctx->delay += change;
1921 mpctx->total_avsync_change += change;
1924 static int fill_audio_out_buffers(struct MPContext *mpctx)
1926 struct MPOpts *opts = &mpctx->opts;
1927 unsigned int t;
1928 double tt;
1929 int playsize;
1930 int playflags=0;
1931 int audio_eof=0;
1932 int bytes_to_write;
1933 sh_audio_t * const sh_audio = mpctx->sh_audio;
1935 current_module="play_audio";
1937 while (1) {
1938 int sleep_time;
1939 // all the current uses of ao_data.pts seem to be in aos that handle
1940 // sync completely wrong; there should be no need to use ao_data.pts
1941 // in get_space()
1942 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1943 bytes_to_write = mpctx->audio_out->get_space();
1944 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1945 break;
1947 // handle audio-only case:
1948 // this is where mplayer sleeps during audio-only playback
1949 // to avoid 100% CPU use
1950 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.samplerate;
1951 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
1952 usec_sleep(sleep_time * 1000);
1955 while (bytes_to_write) {
1956 playsize = bytes_to_write;
1957 if (playsize > MAX_OUTBURST)
1958 playsize = MAX_OUTBURST;
1959 bytes_to_write -= playsize;
1961 // Fill buffer if needed:
1962 current_module="decode_audio";
1963 t = GetTimer();
1964 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1965 if (mpctx->d_audio->eof) {
1966 audio_eof = 1;
1967 if (sh_audio->a_out_buffer_len == 0)
1968 return 0;
1970 t = GetTimer() - t;
1971 tt = t*0.000001f; audio_time_usage+=tt;
1972 if (playsize > sh_audio->a_out_buffer_len) {
1973 playsize = sh_audio->a_out_buffer_len;
1974 if (audio_eof)
1975 playflags |= AOPLAY_FINAL_CHUNK;
1977 if (!playsize)
1978 break;
1980 // play audio:
1981 current_module="play_audio";
1983 // Is this pts value actually useful for the aos that access it?
1984 // They're obviously badly broken in the way they handle av sync;
1985 // would not having access to this make them more broken?
1986 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1987 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1989 if (playsize > 0) {
1990 sh_audio->a_out_buffer_len -= playsize;
1991 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1992 sh_audio->a_out_buffer_len);
1993 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
1995 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1996 // Sanity check to avoid hanging in case current ao doesn't output
1997 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1998 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1999 sh_audio->a_out_buffer_len = 0;
2002 return 1;
2005 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2006 float *aq_sleep_time)
2008 struct MPOpts *opts = &mpctx->opts;
2009 int frame_time_remaining = 0;
2010 current_module="calc_sleep_time";
2012 *time_frame -= get_relative_time(mpctx); // reset timer
2014 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2015 float delay = mpctx->audio_out->get_delay();
2016 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2018 if (autosync) {
2020 * Adjust this raw delay value by calculating the expected
2021 * delay for this frame and generating a new value which is
2022 * weighted between the two. The higher autosync is, the
2023 * closer to the delay value gets to that which "-nosound"
2024 * would have used, and the longer it will take for A/V
2025 * sync to settle at the right value (but it eventually will.)
2026 * This settling time is very short for values below 100.
2028 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2029 float difference = delay - predicted;
2030 delay = predicted + difference / (float)autosync;
2033 *time_frame = delay - mpctx->delay / opts->playback_speed;
2035 // delay = amount of audio buffered in soundcard/driver
2036 if (delay > 0.25) delay=0.25; else
2037 if (delay < 0.10) delay=0.10;
2038 if (*time_frame > delay*0.6) {
2039 // sleep time too big - may cause audio drops (buffer underrun)
2040 frame_time_remaining = 1;
2041 *time_frame = delay*0.5;
2043 } else {
2044 // If we're lagging more than 200 ms behind the right playback rate,
2045 // don't try to "catch up".
2046 // If benchmark is set always output frames as fast as possible
2047 // without sleeping.
2048 if (*time_frame < -0.2 || benchmark)
2049 *time_frame = 0;
2052 *aq_sleep_time += *time_frame;
2055 //============================== SLEEP: ===================================
2057 // flag 256 means: libvo driver does its timing (dvb card)
2058 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2059 *time_frame = timing_sleep(mpctx, *time_frame);
2060 return frame_time_remaining;
2063 int reinit_video_chain(struct MPContext *mpctx)
2065 struct MPOpts *opts = &mpctx->opts;
2066 sh_video_t * const sh_video = mpctx->sh_video;
2067 double ar=-1.0;
2068 //================== Init VIDEO (codec & libvo) ==========================
2069 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2070 current_module="preinit_libvo";
2072 //shouldn't we set dvideo->id=-2 when we fail?
2073 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2074 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2075 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2076 goto err_out;
2078 mpctx->initialized_flags|=INITIALIZED_VO;
2081 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2082 mpctx->sh_video->stream_aspect = ar;
2083 current_module="init_video_filters";
2085 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2086 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2088 #ifdef CONFIG_MENU
2089 if(use_menu) {
2090 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2091 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2092 if(!vf_menu) {
2093 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2094 use_menu = 0;
2097 if(vf_menu)
2098 sh_video->vfilter = vf_menu;
2099 #endif
2101 #ifdef CONFIG_ASS
2102 if(ass_enabled) {
2103 int i;
2104 int insert = 1;
2105 if (opts->vf_settings)
2106 for (i = 0; opts->vf_settings[i].name; ++i)
2107 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2108 insert = 0;
2109 break;
2111 if (insert) {
2112 extern vf_info_t vf_info_ass;
2113 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2114 char* vf_arg[] = {"auto", "1", NULL};
2115 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2116 if (vf_ass)
2117 sh_video->vfilter = vf_ass;
2118 else
2119 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2122 #endif
2124 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2126 #ifdef CONFIG_ASS
2127 if (ass_enabled)
2128 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2129 #endif
2131 current_module="init_video_codec";
2133 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2134 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2135 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2137 if(!sh_video->initialized){
2138 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2139 goto err_out;
2142 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2144 if (sh_video->codec)
2145 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2147 sh_video->last_pts = MP_NOPTS_VALUE;
2148 sh_video->num_buffered_pts = 0;
2149 sh_video->next_frame_time = 0;
2151 if(auto_quality>0){
2152 // Auto quality option enabled
2153 output_quality=get_video_quality_max(sh_video);
2154 if(auto_quality>output_quality) auto_quality=output_quality;
2155 else output_quality=auto_quality;
2156 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2157 set_video_quality(sh_video,output_quality);
2160 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2162 current_module="init_vo";
2164 return 1;
2166 err_out:
2167 mpctx->sh_video = mpctx->d_video->sh = NULL;
2168 return 0;
2171 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2172 int *blit_frame)
2174 struct sh_video *sh_video = mpctx->sh_video;
2175 *blit_frame = 0;
2176 double frame_time = 0;
2177 while (1) {
2178 current_module = "filter_video";
2179 // In nocorrect-pts mode there is no way to properly time these frames
2180 if (vf_output_queued_frame(sh_video->vfilter))
2181 break;
2182 unsigned char *packet = NULL;
2183 frame_time = sh_video->next_frame_time;
2184 if (mpctx->update_video_immediately)
2185 frame_time = 0;
2186 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2187 &packet, force_fps);
2188 if (in_size < 0) {
2189 #ifdef CONFIG_DVDNAV
2190 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2191 if (mp_dvdnav_is_eof(mpctx->stream))
2192 return -1;
2193 if (mpctx->d_video)
2194 mpctx->d_video->eof = 0;
2195 if (mpctx->d_audio)
2196 mpctx->d_audio->eof = 0;
2197 mpctx->stream->eof = 0;
2198 } else
2199 #endif
2200 return -1;
2202 if (in_size > max_framesize)
2203 max_framesize = in_size;
2204 sh_video->timer += frame_time;
2205 if (mpctx->sh_audio)
2206 mpctx->delay -= frame_time;
2207 // video_read_frame can change fps (e.g. for ASF video)
2208 vo_fps = sh_video->fps;
2209 int framedrop_type = check_framedrop(mpctx, frame_time);
2210 current_module = "decode video";
2212 void *decoded_frame;
2213 #ifdef CONFIG_DVDNAV
2214 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2215 if (in_size >= 0 && !decoded_frame)
2216 #endif
2217 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2218 sh_video->pts);
2219 #ifdef CONFIG_DVDNAV
2220 // Save last still frame for future display
2221 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2222 #endif
2223 if (decoded_frame) {
2224 // These updates are done here for vf_expand OSD/subtitles
2225 update_subtitles(sh_video, mpctx->d_sub, 0);
2226 update_teletext(sh_video, mpctx->demuxer, 0);
2227 update_osd_msg(mpctx);
2228 current_module = "filter video";
2229 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2230 break;
2233 *blit_frame = 1;
2234 return frame_time;
2237 static double update_video(struct MPContext *mpctx, int *blit_frame)
2239 struct sh_video *sh_video = mpctx->sh_video;
2240 *blit_frame = 0;
2241 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2242 mpctx->osd); // hack for vf_expand
2243 if (!mpctx->opts.correct_pts)
2244 return update_video_nocorrect_pts(mpctx, blit_frame);
2246 double pts;
2248 while (1) {
2249 current_module = "filter_video";
2250 // XXX Time used in this call is not counted in any performance
2251 // timer now, OSD time is not updated correctly for filter-added frames
2252 if (vf_output_queued_frame(sh_video->vfilter))
2253 break;
2254 unsigned char *packet = NULL;
2255 bool hit_eof = false;
2256 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2257 if (in_size < 0) {
2258 // try to extract last frames in case of decoder lag
2259 in_size = 0;
2260 pts = 1e300;
2261 hit_eof = true;
2263 if (in_size > max_framesize)
2264 max_framesize = in_size;
2265 current_module = "decode video";
2266 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2267 void *decoded_frame = decode_video(sh_video, packet, in_size,
2268 framedrop_type, pts);
2269 if (decoded_frame) {
2270 // These updates are done here for vf_expand OSD/subtitles
2271 update_subtitles(sh_video, mpctx->d_sub, 0);
2272 update_teletext(sh_video, mpctx->demuxer, 0);
2273 update_osd_msg(mpctx);
2274 current_module = "filter video";
2275 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2276 break;
2277 } else if (hit_eof)
2278 return -1;
2281 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2282 if (pts == MP_NOPTS_VALUE) {
2283 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2284 // Try to use decoder pts from before filters
2285 pts = sh_video->pts;
2287 sh_video->pts = pts;
2288 if (sh_video->last_pts == MP_NOPTS_VALUE)
2289 sh_video->last_pts = sh_video->pts;
2290 else if (sh_video->last_pts >= sh_video->pts) {
2291 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Non-increasing video pts: %f <= %f\n",
2292 sh_video->pts, sh_video->last_pts);
2293 /* If the difference in pts is small treat it as jitter around the
2294 * right value (possibly caused by incorrect timestamp ordering) and
2295 * just show this frame immediately after the last one.
2296 * Treat bigger differences as timestamp resets and start counting
2297 * timing of later frames from the position of this one. */
2298 if (sh_video->last_pts - sh_video->pts > 0.5)
2299 sh_video->last_pts = sh_video->pts;
2300 else
2301 sh_video->pts = sh_video->last_pts;
2303 double frame_time = sh_video->pts - sh_video->last_pts;
2304 sh_video->last_pts = sh_video->pts;
2305 sh_video->timer += frame_time;
2306 if (mpctx->sh_audio)
2307 mpctx->delay -= frame_time;
2308 *blit_frame = 1;
2309 return frame_time;
2312 void pause_player(struct MPContext *mpctx)
2314 if (mpctx->paused)
2315 return;
2316 mpctx->paused = 1;
2317 mpctx->step_frames = 0;
2318 mpctx->time_frame -= get_relative_time(mpctx);
2320 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2321 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2323 if (mpctx->audio_out && mpctx->sh_audio)
2324 mpctx->audio_out->pause(); // pause audio, keep data if possible
2327 void unpause_player(struct MPContext *mpctx)
2329 if (!mpctx->paused)
2330 return;
2331 mpctx->paused = 0;
2333 if (mpctx->audio_out && mpctx->sh_audio)
2334 mpctx->audio_out->resume(); // resume audio
2335 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2336 && !mpctx->step_frames)
2337 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2338 (void)get_relative_time(mpctx); // ignore time that passed during pause
2341 void add_step_frame(struct MPContext *mpctx)
2343 mpctx->step_frames++;
2344 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2345 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2346 unpause_player(mpctx);
2349 static void pause_loop(struct MPContext *mpctx)
2351 mp_cmd_t* cmd;
2352 if (!quiet) {
2353 // Small hack to display the pause message on the OSD line.
2354 // The pause string is: "\n == PAUSE == \r" so we need to
2355 // take the first and the last char out
2356 if (term_osd && !mpctx->sh_video) {
2357 char msg[128] = MSGTR_Paused;
2358 int mlen = strlen(msg);
2359 msg[mlen-1] = '\0';
2360 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2361 update_osd_msg(mpctx);
2362 } else
2363 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2364 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2366 #ifdef CONFIG_GUI
2367 if (use_gui)
2368 guiGetEvent(guiCEvent, (char *)guiSetPause);
2369 #endif
2371 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2372 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2373 if (cmd) {
2374 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2375 run_command(mpctx, cmd);
2376 mp_cmd_free(cmd);
2377 continue;
2379 if (mpctx->sh_video && mpctx->video_out)
2380 vo_check_events(mpctx->video_out);
2381 #ifdef CONFIG_GUI
2382 if (use_gui) {
2383 guiEventHandling();
2384 guiGetEvent(guiReDraw, NULL);
2385 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2386 break;
2388 #endif
2389 #ifdef CONFIG_MENU
2390 if (vf_menu)
2391 vf_menu_pause_update(vf_menu);
2392 #endif
2393 usec_sleep(20000);
2394 update_osd_msg(mpctx);
2395 int hack = vo_osd_changed(0);
2396 vo_osd_changed(hack);
2397 if (hack)
2398 break;
2400 #ifdef CONFIG_GUI
2401 if (use_gui) {
2402 if (guiIntfStruct.Playing == guiSetStop)
2403 mpctx->eof = 1;
2404 else
2405 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2407 #endif
2410 static void print_version(void){
2411 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2413 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2414 GetCpuCaps(&gCpuCaps);
2415 #ifdef ARCH_X86
2416 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2417 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2418 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2419 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2420 #ifdef RUNTIME_CPUDETECT
2421 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2422 #else
2423 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2424 #ifdef HAVE_MMX
2425 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2426 #endif
2427 #ifdef HAVE_MMX2
2428 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2429 #endif
2430 #ifdef HAVE_3DNOW
2431 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2432 #endif
2433 #ifdef HAVE_3DNOWEX
2434 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2435 #endif
2436 #ifdef HAVE_SSE
2437 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2438 #endif
2439 #ifdef HAVE_SSE2
2440 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2441 #endif
2442 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2443 #endif /* RUNTIME_CPUDETECT */
2444 #endif /* ARCH_X86 */
2448 // Find the right mute status and record position for new file position
2449 static void edl_seek_reset(MPContext *mpctx)
2451 mpctx->edl_muted = 0;
2452 next_edl_record = edl_records;
2454 while (next_edl_record) {
2455 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2456 break;
2458 if (next_edl_record->action == EDL_MUTE)
2459 mpctx->edl_muted = !mpctx->edl_muted;
2460 next_edl_record = next_edl_record->next;
2462 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2463 mixer_mute(&mpctx->mixer);
2467 // Execute EDL command for the current position if one exists
2468 static void edl_update(MPContext *mpctx)
2470 if (!next_edl_record)
2471 return;
2473 if (!mpctx->sh_video) {
2474 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2475 free_edl(edl_records);
2476 next_edl_record = NULL;
2477 edl_records = NULL;
2478 return;
2481 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2482 if (next_edl_record->action == EDL_SKIP) {
2483 mpctx->osd_function = OSD_FFW;
2484 mpctx->abs_seek_pos = 0;
2485 mpctx->rel_seek_secs = next_edl_record->length_sec;
2486 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2487 "[%f], length [%f]\n", next_edl_record->start_sec,
2488 next_edl_record->stop_sec, next_edl_record->length_sec);
2489 edl_decision = 1;
2491 else if (next_edl_record->action == EDL_MUTE) {
2492 mpctx->edl_muted = !mpctx->edl_muted;
2493 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2494 mixer_mute(&mpctx->mixer);
2495 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2496 next_edl_record->start_sec );
2498 next_edl_record = next_edl_record->next;
2503 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2504 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2505 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2506 static int seek(MPContext *mpctx, double amount, int style)
2508 current_module = "seek";
2509 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2510 return -1;
2512 if (mpctx->sh_video) {
2513 current_module = "seek_video_reset";
2514 resync_video_stream(mpctx->sh_video);
2515 if (mpctx->video_out->config_ok)
2516 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2517 mpctx->sh_video->num_buffered_pts = 0;
2518 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2519 mpctx->num_buffered_frames = 0;
2520 mpctx->delay = 0;
2521 mpctx->time_frame = 0;
2522 mpctx->update_video_immediately = true;
2523 // Not all demuxers set d_video->pts during seek, so this value
2524 // (which is used by at least vobsub and edl code below) may
2525 // be completely wrong (probably 0).
2526 mpctx->sh_video->pts = mpctx->d_video->pts;
2527 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2528 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2531 if (mpctx->sh_audio) {
2532 current_module = "seek_audio_reset";
2533 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2534 mpctx->sh_audio->a_buffer_len = 0;
2535 mpctx->sh_audio->a_out_buffer_len = 0;
2538 if (vo_vobsub && mpctx->sh_video) {
2539 current_module = "seek_vobsub_reset";
2540 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2543 edl_seek_reset(mpctx);
2545 mpctx->total_avsync_change = 0;
2546 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2547 drop_frame_cnt = 0;
2549 current_module = NULL;
2550 return 0;
2554 static int read_keys(void *ctx, int fd)
2556 getch2(ctx);
2557 return mplayer_get_key(ctx, 0);
2561 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2562 * file for some tools to link against. */
2563 #ifndef DISABLE_MAIN
2564 int main(int argc,char* argv[]){
2567 char * mem_ptr;
2569 // movie info:
2571 /* Flag indicating whether MPlayer should exit without playing anything. */
2572 int opt_exit = 0;
2574 //float a_frame=0; // Audio
2576 int i;
2578 int gui_no_filename=0;
2580 struct MPContext *mpctx = &(struct MPContext){
2581 .osd_function = OSD_PLAY,
2582 .begin_skip = MP_NOPTS_VALUE,
2583 .play_tree_step = 1,
2584 .global_sub_pos = -1,
2585 .set_of_sub_pos = -1,
2586 .file_format = DEMUXER_TYPE_UNKNOWN,
2587 .last_dvb_step = 1,
2590 InitTimer();
2591 srand(GetTimerMS());
2593 mp_msg_init();
2595 #ifdef CONFIG_X11
2596 mpctx->x11_state = vo_x11_init_state();
2597 #endif
2598 struct MPOpts *opts = &mpctx->opts;
2599 set_default_mplayer_options(opts);
2600 // Create the config context and register the options
2601 mpctx->mconfig = m_config_new(opts, cfg_include);
2602 m_config_register_options(mpctx->mconfig,mplayer_opts);
2603 mp_input_register_options(mpctx->mconfig);
2605 // Preparse the command line
2606 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2608 print_version();
2609 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2610 set_path_env();
2611 #endif
2613 #ifdef CONFIG_TV
2614 stream_tv_defaults.immediate = 1;
2615 #endif
2617 if (argc > 1 && argv[1] &&
2618 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2619 use_gui = !strcmp(argv[1], "-gui");
2620 } else
2621 if ( argv[0] )
2623 char *base = strrchr(argv[0], '/');
2624 if (!base)
2625 base = strrchr(argv[0], '\\');
2626 if (!base)
2627 base = argv[0];
2628 if(strstr(base, "gmplayer"))
2629 use_gui=1;
2632 parse_cfgfiles(mpctx, mpctx->mconfig);
2634 #ifdef CONFIG_GUI
2635 if ( use_gui ) cfg_read();
2636 #endif
2638 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2639 if(mpctx->playtree == NULL)
2640 opt_exit = 1;
2641 else {
2642 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2643 if(mpctx->playtree) {
2644 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2645 if(mpctx->playtree_iter) {
2646 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2647 play_tree_iter_free(mpctx->playtree_iter);
2648 mpctx->playtree_iter = NULL;
2650 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2654 mpctx->key_fifo = mp_fifo_create(opts);
2656 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2657 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2658 if(runningmplayer && mpctx->filename && use_gui){
2659 COPYDATASTRUCT csData;
2660 char file[MAX_PATH];
2661 char *filepart = mpctx->filename;
2662 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2663 csData.dwData = 0;
2664 csData.cbData = strlen(file)*2;
2665 csData.lpData = file;
2666 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2669 #endif
2671 #if defined(__MINGW32__) || defined(__CYGWIN__)
2672 // request 1ms timer resolution
2673 timeBeginPeriod(1);
2674 if(proc_priority){
2675 int i;
2676 for(i=0; priority_presets_defs[i].name; i++){
2677 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2678 break;
2680 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
2681 priority_presets_defs[i].name);
2682 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2684 #endif
2685 #ifndef CONFIG_GUI
2686 if(use_gui){
2687 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2688 use_gui=0;
2690 #else
2691 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2692 if(use_gui && !vo_init()){
2693 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2694 use_gui=0;
2696 #endif
2697 if (use_gui && mpctx->playtree_iter){
2698 char cwd[PATH_MAX+2];
2699 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2700 play_tree_iter_free(mpctx->playtree_iter);
2701 mpctx->playtree_iter=NULL;
2703 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2705 strcat(cwd, "/");
2706 // Prefix relative paths with current working directory
2707 play_tree_add_bpf(mpctx->playtree, cwd);
2709 // Import initital playtree into GUI.
2710 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2712 #endif /* CONFIG_GUI */
2714 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2715 list_video_out();
2716 opt_exit = 1;
2719 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2720 list_audio_out();
2721 opt_exit = 1;
2724 /* Check codecs.conf. */
2725 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2726 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2727 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2728 if(!parse_codec_cfg(NULL)){
2729 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2731 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2734 free( mem_ptr ); // release the buffer created by get_path()
2737 #if 0
2738 if(video_codec_list){
2739 int i;
2740 video_codec=video_codec_list[0];
2741 for(i=0;video_codec_list[i];i++)
2742 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2744 #endif
2745 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2746 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2747 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2748 list_codecs(1);
2749 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2750 opt_exit = 1;
2752 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2753 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2754 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2755 list_codecs(0);
2756 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2757 opt_exit = 1;
2759 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2760 vfm_help();
2761 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2762 opt_exit = 1;
2764 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2765 afm_help();
2766 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2767 opt_exit = 1;
2769 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2770 af_help();
2771 printf("\n");
2772 opt_exit = 1;
2774 #ifdef CONFIG_X11
2775 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2776 fstype_help();
2777 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2778 opt_exit = 1;
2780 #endif
2781 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2782 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2783 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2784 demuxer_help();
2785 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2786 opt_exit = 1;
2788 if(list_properties) {
2789 property_print_help();
2790 opt_exit = 1;
2793 if(opt_exit)
2794 exit_player(mpctx, EXIT_NONE);
2796 if (player_idle_mode && use_gui) {
2797 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2798 exit_player_with_rc(mpctx, EXIT_NONE, 1);
2801 if(!mpctx->filename && !player_idle_mode){
2802 if(!use_gui){
2803 // no file/vcd/dvd -> show HELP:
2804 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2805 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2806 } else gui_no_filename=1;
2809 /* Display what configure line was used */
2810 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2812 // Many users forget to include command line in bugreports...
2813 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2814 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2815 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2816 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2819 //------ load global data first ------
2821 mpctx->osd = osd_create();
2823 // check font
2824 #ifdef CONFIG_FREETYPE
2825 init_freetype();
2826 #endif
2827 #ifdef CONFIG_FONTCONFIG
2828 if(font_fontconfig <= 0)
2830 #endif
2831 #ifdef CONFIG_BITMAP_FONT
2832 if(font_name){
2833 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2834 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2835 filename_recode(font_name));
2836 } else {
2837 // try default:
2838 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2839 free(mem_ptr); // release the buffer created by get_path()
2840 if(!vo_font)
2841 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2843 if (sub_font_name)
2844 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2845 else
2846 mpctx->osd->sub_font = vo_font;
2847 #endif
2848 #ifdef CONFIG_FONTCONFIG
2850 #endif
2852 #ifdef CONFIG_ASS
2853 ass_library = ass_init();
2854 #endif
2856 #ifdef HAVE_RTC
2857 if(!nortc)
2859 // seteuid(0); /* Can't hurt to try to get root here */
2860 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2861 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2862 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2863 else {
2864 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2866 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2867 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2868 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2869 close (rtc_fd);
2870 rtc_fd = -1;
2871 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2872 /* variable only by the root */
2873 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2874 close (rtc_fd);
2875 rtc_fd = -1;
2876 } else
2877 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2880 #ifdef CONFIG_GUI
2881 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2882 // and now ? -- Pontscho
2883 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2884 #endif
2885 if(rtc_fd<0)
2886 #endif /* HAVE_RTC */
2887 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2888 softsleep?"software":timer_name);
2890 #ifdef HAVE_TERMCAP
2891 if ( !use_gui ) load_termcap(NULL); // load key-codes
2892 #endif
2894 // ========== Init keyboard FIFO (connection to libvo) ============
2896 // Init input system
2897 current_module = "init_input";
2898 mpctx->input = mp_input_init(&opts->input, use_gui);
2899 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2900 if(slave_mode)
2901 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2902 else if(!noconsolecontrols)
2903 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2904 // Set the libstream interrupt callback
2905 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2907 #ifdef CONFIG_MENU
2908 if(use_menu) {
2909 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2910 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2911 else {
2912 menu_cfg = get_path("menu.conf");
2913 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2914 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2915 else {
2916 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2917 MPLAYER_CONFDIR "/menu.conf"))
2918 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2919 else {
2920 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2921 use_menu = 0;
2926 #endif
2928 current_module = NULL;
2930 /// Catch signals
2931 #ifndef __MINGW32__
2932 signal(SIGCHLD,child_sighandler);
2933 #endif
2935 #ifdef CONFIG_CRASH_DEBUG
2936 prog_path = argv[0];
2937 #endif
2938 //========= Catch terminate signals: ================
2939 // terminate requests:
2940 signal(SIGTERM,exit_sighandler); // kill
2941 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2943 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2945 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2946 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2947 #ifdef CONFIG_SIGHANDLER
2948 // fatal errors:
2949 signal(SIGBUS,exit_sighandler); // bus error
2950 signal(SIGSEGV,exit_sighandler); // segfault
2951 signal(SIGILL,exit_sighandler); // illegal instruction
2952 signal(SIGFPE,exit_sighandler); // floating point exc.
2953 signal(SIGABRT,exit_sighandler); // abort()
2954 #ifdef CONFIG_CRASH_DEBUG
2955 if (crash_debug)
2956 signal(SIGTRAP,exit_sighandler);
2957 #endif
2958 #endif
2960 #ifdef CONFIG_GUI
2961 if(use_gui){
2962 guiInit();
2963 guiGetEvent(guiSetContext, mpctx);
2964 mpctx->initialized_flags|=INITIALIZED_GUI;
2965 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2967 #endif
2969 // ******************* Now, let's see the per-file stuff ********************
2971 play_next_file:
2973 // init global sub numbers
2974 mpctx->global_sub_size = 0;
2975 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2977 if (mpctx->filename) {
2978 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2979 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2980 load_per_file_config (mpctx->mconfig, mpctx->filename);
2983 if (opts->video_driver_list)
2984 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2985 if (opts->audio_driver_list)
2986 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2988 // We must enable getch2 here to be able to interrupt network connection
2989 // or cache filling
2990 if(!noconsolecontrols && !slave_mode){
2991 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2992 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2993 else
2994 getch2_enable(); // prepare stdin for hotkeys...
2995 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2996 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2999 // =================== GUI idle loop (STOP state) ===========================
3000 #ifdef CONFIG_GUI
3001 if ( use_gui ) {
3002 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
3003 guiGetEvent( guiSetDefaults,0 );
3004 while ( guiIntfStruct.Playing != 1 )
3006 mp_cmd_t* cmd;
3007 usec_sleep(20000);
3008 guiEventHandling();
3009 guiGetEvent( guiReDraw,NULL );
3010 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3011 guiGetEvent(guiIEvent, (char *)cmd->id);
3012 mp_cmd_free(cmd);
3015 guiGetEvent( guiSetParameters,NULL );
3016 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3018 play_tree_t * entry = play_tree_new();
3019 play_tree_add_file( entry,guiIntfStruct.Filename );
3020 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3021 else mpctx->playtree=play_tree_new();
3022 play_tree_set_child( mpctx->playtree,entry );
3023 if(mpctx->playtree)
3025 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3026 if(mpctx->playtree_iter)
3028 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3030 play_tree_iter_free(mpctx->playtree_iter);
3031 mpctx->playtree_iter = NULL;
3033 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3038 #endif /* CONFIG_GUI */
3040 while (player_idle_mode && !mpctx->filename) {
3041 play_tree_t * entry = NULL;
3042 mp_cmd_t * cmd;
3043 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3044 if (mpctx->video_out)
3045 vo_check_events(mpctx->video_out);
3046 usec_sleep(20000);
3048 switch (cmd->id) {
3049 case MP_CMD_LOADFILE:
3050 // prepare a tree entry with the new filename
3051 entry = play_tree_new();
3052 play_tree_add_file(entry, cmd->args[0].v.s);
3053 // The entry is added to the main playtree after the switch().
3054 break;
3055 case MP_CMD_LOADLIST:
3056 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3057 break;
3058 case MP_CMD_QUIT:
3059 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3060 break;
3061 case MP_CMD_GET_PROPERTY:
3062 case MP_CMD_SET_PROPERTY:
3063 case MP_CMD_STEP_PROPERTY:
3064 run_command(mpctx, cmd);
3065 break;
3068 mp_cmd_free(cmd);
3070 if (entry) { // user entered a command that gave a valid entry
3071 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3072 play_tree_free_list(mpctx->playtree->child, 1);
3073 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3075 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3077 play_tree_set_child(mpctx->playtree, entry);
3079 /* Make iterator start at the top the of tree. */
3080 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3081 if (!mpctx->playtree_iter) continue;
3083 // find the first real item in the tree
3084 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3085 // no items!
3086 play_tree_iter_free(mpctx->playtree_iter);
3087 mpctx->playtree_iter = NULL;
3088 continue; // wait for next command
3090 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3093 //---------------------------------------------------------------------------
3095 if(mpctx->filename)
3096 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3097 filename_recode(mpctx->filename));
3099 if (edl_filename) {
3100 if (edl_records) free_edl(edl_records);
3101 next_edl_record = edl_records = edl_parse_file();
3103 if (edl_output_filename) {
3104 if (edl_fd) fclose(edl_fd);
3105 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3107 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3108 filename_recode(edl_output_filename));
3112 //==================== Open VOB-Sub ============================
3114 current_module="vobsub";
3115 if (vobsub_name){
3116 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3117 if(vo_vobsub==NULL)
3118 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3119 filename_recode(vobsub_name));
3120 } else if (sub_auto && mpctx->filename){
3121 /* try to autodetect vobsub from movie filename ::atmos */
3122 char *buf = strdup(mpctx->filename), *psub;
3123 char *pdot = strrchr(buf, '.');
3124 char *pslash = strrchr(buf, '/');
3125 #if defined(__MINGW32__) || defined(__CYGWIN__)
3126 if (!pslash) pslash = strrchr(buf, '\\');
3127 #endif
3128 if (pdot && (!pslash || pdot > pslash))
3129 *pdot = '\0';
3130 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3131 /* try from ~/.mplayer/sub */
3132 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3133 char *bname;
3134 int l;
3135 bname = strrchr(buf,'/');
3136 #if defined(__MINGW32__) || defined(__CYGWIN__)
3137 if(!bname) bname = strrchr(buf,'\\');
3138 #endif
3139 if(bname) bname++;
3140 else bname = buf;
3141 l = strlen(psub) + strlen(bname) + 1;
3142 psub = realloc(psub,l);
3143 strcat(psub,bname);
3144 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3145 free(psub);
3147 free(buf);
3149 if(vo_vobsub){
3150 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3151 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3153 // setup global sub numbering
3154 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3155 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3158 //============ Open & Sync STREAM --- fork cache2 ====================
3160 mpctx->stream=NULL;
3161 mpctx->demuxer=NULL;
3162 if (mpctx->d_audio) {
3163 //free_demuxer_stream(mpctx->d_audio);
3164 mpctx->d_audio=NULL;
3166 if (mpctx->d_video) {
3167 //free_demuxer_stream(d_video);
3168 mpctx->d_video=NULL;
3170 mpctx->sh_audio=NULL;
3171 mpctx->sh_video=NULL;
3173 current_module="open_stream";
3174 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3175 if(!mpctx->stream) { // error...
3176 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3177 goto goto_next_file;
3179 mpctx->initialized_flags|=INITIALIZED_STREAM;
3181 #ifdef CONFIG_GUI
3182 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3183 #endif
3185 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3186 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3187 "playlist support will not be used automatically.\n"
3188 "MPlayer's playlist code is unsafe and should only be used with "
3189 "trusted sources.\nPlayback will probably fail.\n\n");
3190 #if 0
3191 play_tree_t* entry;
3192 // Handle playlist
3193 current_module="handle_playlist";
3194 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3195 filename_recode(mpctx->filename));
3196 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3197 mpctx->eof=playtree_add_playlist(mpctx, entry);
3198 goto goto_next_file;
3199 #endif
3201 mpctx->stream->start_pos+=seek_to_byte;
3203 if(stream_dump_type==5){
3204 unsigned char buf[4096];
3205 int len;
3206 FILE *f;
3207 current_module="dumpstream";
3208 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3209 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3210 exit_player(mpctx, EXIT_ERROR);
3212 stream_reset(mpctx->stream);
3213 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3214 f=fopen(stream_dump_name,"wb");
3215 if(!f){
3216 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3217 exit_player(mpctx, EXIT_ERROR);
3219 if (dvd_chapter > 1) {
3220 int chapter = dvd_chapter - 1;
3221 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3223 while(!mpctx->stream->eof && !async_quit_request){
3224 len=stream_read(mpctx->stream,buf,4096);
3225 if(len>0) {
3226 if(fwrite(buf,len,1,f) != 1) {
3227 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3228 exit_player(mpctx, EXIT_ERROR);
3231 if(dvd_last_chapter > 0) {
3232 int chapter = -1;
3233 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3234 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3235 break;
3238 if(fclose(f)) {
3239 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3240 exit_player(mpctx, EXIT_ERROR);
3242 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3243 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3246 #ifdef CONFIG_DVDREAD
3247 if(mpctx->stream->type==STREAMTYPE_DVD){
3248 current_module="dvd lang->id";
3249 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3250 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3251 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3252 // setup global sub numbering
3253 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3254 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3255 current_module=NULL;
3257 #endif
3259 #ifdef CONFIG_DVDNAV
3260 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3261 current_module="dvdnav lang->id";
3262 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3263 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3264 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3265 // setup global sub numbering
3266 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3267 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3268 current_module=NULL;
3270 #endif
3272 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3273 goto_enable_cache:
3274 if(stream_cache_size>0){
3275 current_module="enable_cache";
3276 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3277 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3278 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3279 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3282 //============ Open DEMUXERS --- DETECT file type =======================
3283 current_module="demux_open";
3285 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3287 // HACK to get MOV Reference Files working
3289 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3291 unsigned char* playlist_entry;
3292 play_tree_t *list = NULL, *entry = NULL;
3294 current_module="handle_demux_playlist";
3295 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3297 char *temp, *bname;
3299 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3300 filename_recode(playlist_entry));
3302 bname=mp_basename(playlist_entry);
3303 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3304 continue;
3306 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3307 continue;
3309 entry = play_tree_new();
3311 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3313 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3314 if (temp)
3316 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3317 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3318 strcat(temp, playlist_entry);
3319 play_tree_add_file(entry,temp);
3320 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3321 free(temp);
3324 else
3325 play_tree_add_file(entry,playlist_entry);
3327 if(!list)
3328 list = entry;
3329 else
3330 play_tree_append_entry(list,entry);
3332 free_demuxer(mpctx->demuxer);
3333 mpctx->demuxer = NULL;
3335 if (list)
3337 entry = play_tree_new();
3338 play_tree_set_child(entry,list);
3339 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3340 goto goto_next_file;
3344 if(!mpctx->demuxer)
3345 goto goto_next_file;
3346 if(dvd_chapter>1) {
3347 float pts;
3348 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3349 seek(mpctx, pts, SEEK_ABSOLUTE);
3352 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3354 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3355 int i;
3356 int maxid = -1;
3357 // setup global sub numbering
3358 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3359 for (i = 0; i < MAX_S_STREAMS; i++)
3360 if (mpctx->demuxer->s_streams[i])
3361 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3362 mpctx->global_sub_size += maxid + 1;
3364 // Make opts->sub_id always selectable if set.
3365 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3366 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3368 #ifdef CONFIG_ASS
3369 if (ass_enabled && ass_library) {
3370 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3371 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3372 if (extract_embedded_fonts &&
3373 att->name && att->type && att->data && att->data_size &&
3374 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3375 strcmp(att->type, "application/x-font") == 0))
3376 ass_add_font(ass_library, att->name, att->data, att->data_size);
3379 #endif
3381 current_module="demux_open2";
3383 //file_format=demuxer->file_format;
3385 mpctx->d_audio=mpctx->demuxer->audio;
3386 mpctx->d_video=mpctx->demuxer->video;
3387 mpctx->d_sub=mpctx->demuxer->sub;
3389 // select audio stream
3390 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3392 // DUMP STREAMS:
3393 if((stream_dump_type)&&(stream_dump_type<4)){
3394 FILE *f;
3395 demux_stream_t *ds=NULL;
3396 current_module="dump";
3397 // select stream to dump
3398 switch(stream_dump_type){
3399 case 1: ds=mpctx->d_audio;break;
3400 case 2: ds=mpctx->d_video;break;
3401 case 3: ds=mpctx->d_sub;break;
3403 if(!ds){
3404 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3405 exit_player(mpctx, EXIT_ERROR);
3407 // disable other streams:
3408 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3409 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3410 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3411 // let's dump it!
3412 f=fopen(stream_dump_name,"wb");
3413 if(!f){
3414 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3415 exit_player(mpctx, EXIT_ERROR);
3417 while(!ds->eof){
3418 unsigned char* start;
3419 int in_size=ds_get_packet(ds,&start);
3420 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3421 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3422 if(in_size>0) fwrite(start,in_size,1,f);
3423 if(dvd_last_chapter>0) {
3424 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3425 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3426 break;
3429 fclose(f);
3430 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3431 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3434 mpctx->sh_audio=mpctx->d_audio->sh;
3435 mpctx->sh_video=mpctx->d_video->sh;
3437 if(mpctx->sh_video){
3439 current_module="video_read_properties";
3440 if(!video_read_properties(mpctx->sh_video)) {
3441 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3442 mpctx->sh_video=mpctx->d_video->sh=NULL;
3443 } else {
3444 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3445 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3446 mpctx->sh_video->fps,mpctx->sh_video->frametime
3449 /* need to set fps here for output encoders to pick it up in their init */
3450 if(force_fps){
3451 mpctx->sh_video->fps=force_fps;
3452 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3454 vo_fps = mpctx->sh_video->fps;
3456 if(!mpctx->sh_video->fps && !force_fps){
3457 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3458 mpctx->sh_video=mpctx->d_video->sh=NULL;
3464 if(!mpctx->sh_video && !mpctx->sh_audio){
3465 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3466 #ifdef CONFIG_DVBIN
3467 if(mpctx->stream->type == STREAMTYPE_DVB)
3469 int dir;
3470 int v = mpctx->last_dvb_step;
3471 if(v > 0)
3472 dir = DVB_CHANNEL_HIGHER;
3473 else
3474 dir = DVB_CHANNEL_LOWER;
3476 if(dvb_step_channel(mpctx->stream, dir)) {
3477 mpctx->stop_play = PT_NEXT_ENTRY;
3478 mpctx->dvbin_reopen = 1;
3481 #endif
3482 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3485 /* display clip info */
3486 demux_info_print(mpctx->demuxer);
3488 //================== Read SUBTITLES (DVD & TEXT) ==========================
3489 if(vo_spudec==NULL && mpctx->sh_video &&
3490 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3491 init_vo_spudec(mpctx);
3494 if(mpctx->sh_video) {
3495 // after reading video params we should load subtitles because
3496 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3497 // check .sub
3498 current_module="read_subtitles_file";
3499 if(sub_name){
3500 for (i = 0; sub_name[i] != NULL; ++i)
3501 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3503 if(sub_auto) { // auto load sub file ...
3504 char *psub = get_path( "sub/" );
3505 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3506 int i = 0;
3507 free(psub); // release the buffer created by get_path() above
3508 while (tmp[i]) {
3509 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3510 free(tmp[i++]);
3512 free(tmp);
3514 if (mpctx->set_of_sub_size > 0) {
3515 // setup global sub numbering
3516 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3517 mpctx->global_sub_size += mpctx->set_of_sub_size;
3521 if (mpctx->global_sub_size) {
3522 // find the best sub to use
3523 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3524 mpctx->global_sub_pos = -1; // no subs by default
3525 if (vobsub_index_id >= 0) {
3526 // if user asks for a vobsub id, use that first.
3527 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3528 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3529 // if user asks for a dvd sub id, use that next.
3530 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3531 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3532 // if there are text subs to use, use those. (autosubs come last here)
3533 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3534 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3535 // finally select subs by language and container hints
3536 if (opts->sub_id < 0 && dvdsub_lang)
3537 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3538 if (opts->sub_id < 0)
3539 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3540 if (opts->sub_id >= 0)
3541 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3543 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3544 mpctx->global_sub_pos--;
3545 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3546 if(subdata)
3547 switch (stream_dump_type) {
3548 case 3: list_sub_file(subdata); break;
3549 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3550 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3551 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3552 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3553 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3557 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3558 filename_recode(mpctx->filename));
3559 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3560 if (mpctx->sh_video) {
3561 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3562 if (mpctx->sh_video->format >= 0x20202020)
3563 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3564 else
3565 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3566 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3567 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3568 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3569 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3570 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3572 if (mpctx->sh_audio) {
3573 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3574 if (mpctx->sh_audio->format >= 0x20202020)
3575 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3576 else
3577 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3578 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3579 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3580 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3582 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3583 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3584 if (mpctx->demuxer) {
3585 if (mpctx->demuxer->num_chapters == 0)
3586 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3587 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3590 if(!mpctx->sh_video) goto main; // audio-only
3592 if(!reinit_video_chain(mpctx)) {
3593 if(!mpctx->sh_video){
3594 if(!mpctx->sh_audio) goto goto_next_file;
3595 goto main; // exit_player(MSGTR_Exit_error);
3599 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3600 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3602 #ifdef CONFIG_FREETYPE
3603 force_load_font = 1;
3604 #endif
3606 //================== MAIN: ==========================
3607 main:
3608 current_module="main";
3610 if(playing_msg) {
3611 char* msg = property_expand_string(mpctx, playing_msg);
3612 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3613 free(msg);
3617 // Disable the term OSD in verbose mode
3618 if(verbose) term_osd = 0;
3621 //int frame_corr_num=0; //
3622 //float v_frame=0; // Video
3623 //float num_frames=0; // number of frames played
3625 int frame_time_remaining=0; // flag
3626 int blit_frame=0;
3627 mpctx->num_buffered_frames=0;
3629 // Make sure old OSD does not stay around,
3630 // e.g. with -fixed-vo and same-resolution files
3631 clear_osd_msgs();
3632 update_osd_msg(mpctx);
3634 //================ SETUP AUDIO ==========================
3636 if(mpctx->sh_audio){
3637 reinit_audio_chain(mpctx);
3638 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3639 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3642 current_module="av_init";
3644 if(mpctx->sh_video){
3645 mpctx->sh_video->timer=0;
3646 if (! ignore_start)
3647 audio_delay += mpctx->sh_video->stream_delay;
3649 if(mpctx->sh_audio){
3650 if (start_volume >= 0)
3651 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3652 if (! ignore_start)
3653 audio_delay -= mpctx->sh_audio->stream_delay;
3654 mpctx->delay=-audio_delay;
3657 if(!mpctx->sh_audio){
3658 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3659 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3660 ds_free_packs(mpctx->d_audio); // free buffered chunks
3661 //mpctx->d_audio->id=-2; // do not read audio chunks
3662 //uninit_player(mpctx, INITIALIZED_AO); // close device
3664 if(!mpctx->sh_video){
3665 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3666 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3667 ds_free_packs(mpctx->d_video);
3668 mpctx->d_video->id=-2;
3669 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3672 if (!mpctx->sh_video && !mpctx->sh_audio)
3673 goto goto_next_file;
3675 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3676 if(force_fps && mpctx->sh_video){
3677 vo_fps = mpctx->sh_video->fps=force_fps;
3678 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3679 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3682 #ifdef CONFIG_GUI
3683 if ( use_gui ) {
3684 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3685 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3686 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3687 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3688 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3690 #endif
3692 mp_input_set_section(mpctx->input, NULL);
3693 //TODO: add desired (stream-based) sections here
3694 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3695 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3697 //==================== START PLAYING =======================
3699 if(opts->loop_times>1) opts->loop_times--; else
3700 if(opts->loop_times==1) opts->loop_times = -1;
3702 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3704 total_time_usage_start=GetTimer();
3705 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3706 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3707 play_n_frames=play_n_frames_mf;
3709 if(play_n_frames==0){
3710 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3713 if (seek_to_sec) {
3714 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3715 end_at.pos += seek_to_sec;
3718 if (end_at.type == END_AT_SIZE) {
3719 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3720 end_at.type = END_AT_NONE;
3723 #ifdef CONFIG_DVDNAV
3724 mp_dvdnav_context_free(mpctx);
3725 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3726 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3727 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3729 #endif
3731 get_relative_time(mpctx); // reset current delta
3732 mpctx->time_frame = 0;
3733 mpctx->drop_message_shown = 0;
3734 mpctx->update_video_immediately = true;
3735 mpctx->total_avsync_change = 0;
3737 while(!mpctx->stop_play){
3738 float aq_sleep_time=0;
3740 if(dvd_last_chapter>0) {
3741 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3742 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3743 goto goto_next_file;
3746 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3747 mpctx->sh_audio = mpctx->d_audio->sh;
3748 mpctx->sh_audio->ds = mpctx->d_audio;
3749 reinit_audio_chain(mpctx);
3752 /*========================== PLAY AUDIO ============================*/
3754 if (mpctx->sh_audio && !mpctx->paused)
3755 if (!fill_audio_out_buffers(mpctx))
3756 // at eof, all audio at least written to ao
3757 if (!mpctx->sh_video)
3758 mpctx->stop_play = AT_END_OF_FILE;
3761 if(!mpctx->sh_video) {
3762 // handle audio-only case:
3763 double a_pos=0;
3764 // sh_audio can be NULL due to video stream switching
3765 // TODO: handle this better
3766 if (mpctx->sh_audio)
3767 a_pos = playing_audio_pts(mpctx);
3769 print_status(mpctx, a_pos, false);
3771 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3772 mpctx->stop_play = PT_NEXT_ENTRY;
3773 update_osd_msg(mpctx);
3775 } else {
3777 /*========================== PLAY VIDEO ============================*/
3779 vo_pts=mpctx->sh_video->timer*90000.0;
3780 vo_fps=mpctx->sh_video->fps;
3782 if (!mpctx->num_buffered_frames) {
3783 double frame_time = update_video(mpctx, &blit_frame);
3784 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3785 if (mpctx->sh_video->vf_initialized < 0) {
3786 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3787 mpctx->stop_play = PT_NEXT_ENTRY;
3788 goto goto_next_file;
3790 if (blit_frame) {
3791 struct vf_instance *vf = mpctx->sh_video->vfilter;
3792 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
3793 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
3794 vo_osd_changed(0);
3796 if (frame_time < 0)
3797 mpctx->stop_play = AT_END_OF_FILE;
3798 else {
3799 // might return with !eof && !blit_frame if !correct_pts
3800 mpctx->num_buffered_frames += blit_frame;
3801 if (mpctx->update_video_immediately) {
3802 // Show this frame immediately, rest normally
3803 mpctx->update_video_immediately = false;
3804 } else {
3805 mpctx->time_frame += frame_time / opts->playback_speed;
3806 adjust_sync(mpctx, frame_time);
3811 // ==========================================================================
3813 // current_module="draw_osd";
3814 // if(vo_config_count) mpctx->video_out->draw_osd();
3816 #ifdef CONFIG_GUI
3817 if(use_gui) guiEventHandling();
3818 #endif
3820 current_module="vo_check_events";
3821 vo_check_events(mpctx->video_out);
3823 #ifdef CONFIG_X11
3824 if (stop_xscreensaver) {
3825 current_module = "stop_xscreensaver";
3826 xscreensaver_heartbeat(mpctx->x11_state);
3828 #endif
3829 if (heartbeat_cmd) {
3830 static unsigned last_heartbeat;
3831 unsigned now = GetTimerMS();
3832 if (now - last_heartbeat > 30000) {
3833 last_heartbeat = now;
3834 system(heartbeat_cmd);
3838 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
3840 //====================== FLIP PAGE (VIDEO BLT): =========================
3842 current_module="flip_page";
3843 if (!frame_time_remaining && blit_frame) {
3844 unsigned int t2=GetTimer();
3846 vo_flip_page(mpctx->video_out);
3847 mpctx->num_buffered_frames--;
3849 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
3850 vout_time_usage += mpctx->last_vo_flip_duration;
3851 print_status(mpctx, MP_NOPTS_VALUE, true);
3853 else
3854 print_status(mpctx, MP_NOPTS_VALUE, false);
3856 //============================ Auto QUALITY ============================
3858 /*Output quality adjustments:*/
3859 if(auto_quality>0){
3860 current_module="autoq";
3861 // float total=0.000001f * (GetTimer()-aq_total_time);
3862 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3863 if(output_quality<auto_quality && aq_sleep_time>0)
3864 ++output_quality;
3865 else
3866 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3867 if(output_quality>1 && aq_sleep_time<0)
3868 --output_quality;
3869 else
3870 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3871 output_quality=0;
3872 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3873 set_video_quality(mpctx->sh_video,output_quality);
3876 if (!frame_time_remaining && blit_frame) {
3877 if (play_n_frames >= 0) {
3878 --play_n_frames;
3879 if (play_n_frames <= 0)
3880 mpctx->stop_play = PT_NEXT_ENTRY;
3882 if (mpctx->step_frames > 0) {
3883 mpctx->step_frames--;
3884 if (mpctx->step_frames == 0)
3885 pause_player(mpctx);
3890 // FIXME: add size based support for -endpos
3891 if (end_at.type == END_AT_TIME &&
3892 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3893 mpctx->stop_play = PT_NEXT_ENTRY;
3895 } // end if(mpctx->sh_video)
3897 #ifdef CONFIG_DVDNAV
3898 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3899 nav_highlight_t hl;
3900 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3901 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3902 vo_osd_changed (OSDTYPE_DVDNAV);
3904 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3905 double ar = -1.0;
3906 if (stream_control (mpctx->demuxer->stream,
3907 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3908 != STREAM_UNSUPPORTED)
3909 mpctx->sh_video->stream_aspect = ar;
3912 #endif
3914 //================= Keyboard events, SEEKing ====================
3916 current_module="key_events";
3919 while (1) {
3920 mp_cmd_t* cmd;
3921 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3922 run_command(mpctx, cmd);
3923 mp_cmd_free(cmd);
3924 if (mpctx->stop_play)
3925 break;
3927 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
3928 || mpctx->abs_seek_pos)
3929 break;
3930 if (mpctx->sh_video) {
3931 update_osd_msg(mpctx);
3932 int hack = vo_osd_changed(0);
3933 vo_osd_changed(hack);
3934 if (hack)
3935 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
3936 add_step_frame(mpctx);
3937 break;
3939 else
3940 vo_osd_changed(0);
3942 pause_loop(mpctx);
3946 // handle -sstep
3947 if (step_sec > 0 && !mpctx->paused) {
3948 mpctx->osd_function=OSD_FFW;
3949 mpctx->rel_seek_secs+=step_sec;
3952 edl_update(mpctx);
3954 /* Looping. */
3955 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3956 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3958 if(opts->loop_times>1) opts->loop_times--; else
3959 if(opts->loop_times==1) opts->loop_times=-1;
3960 play_n_frames=play_n_frames_mf;
3961 mpctx->stop_play=0;
3962 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3963 loop_seek = 1;
3966 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3967 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3968 // Set OSD:
3969 if(!loop_seek){
3970 if( !edl_decision )
3971 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3975 mpctx->rel_seek_secs=0;
3976 mpctx->abs_seek_pos=0;
3977 loop_seek=0;
3978 edl_decision = 0;
3981 #ifdef CONFIG_GUI
3982 if(use_gui){
3983 guiEventHandling();
3984 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3985 // get pos from frame number / total frames
3986 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3987 } else {
3988 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3990 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3991 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3992 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3993 guiGetEvent( guiReDraw,NULL );
3994 guiGetEvent( guiSetVolume,NULL );
3995 if(guiIntfStruct.Playing==0) break; // STOP
3996 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3997 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3998 #ifdef CONFIG_DVDREAD
3999 if ( mpctx->stream->type == STREAMTYPE_DVD )
4001 dvd_priv_t * dvdp = mpctx->stream->priv;
4002 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
4004 #endif
4006 #endif /* CONFIG_GUI */
4008 } // while(!mpctx->stop_play)
4010 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4012 #ifdef CONFIG_DVBIN
4013 if(mpctx->dvbin_reopen)
4015 mpctx->stop_play = 0;
4016 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4017 cache_uninit(mpctx->stream);
4018 mpctx->dvbin_reopen = 0;
4019 goto goto_enable_cache;
4021 #endif
4024 goto_next_file: // don't jump here after ao/vo/getch initialization!
4026 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4028 if(benchmark){
4029 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4030 double total_time_usage;
4031 total_time_usage_start=GetTimer()-total_time_usage_start;
4032 total_time_usage = (float)total_time_usage_start*0.000001;
4033 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4034 video_time_usage,vout_time_usage,audio_time_usage,
4035 total_time_usage-tot,total_time_usage);
4036 if(total_time_usage>0.0)
4037 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4038 100.0*video_time_usage/total_time_usage,
4039 100.0*vout_time_usage/total_time_usage,
4040 100.0*audio_time_usage/total_time_usage,
4041 100.0*(total_time_usage-tot)/total_time_usage,
4042 100.0);
4043 if(total_frame_cnt && frame_dropping)
4044 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4045 total_frame_cnt-drop_frame_cnt,
4046 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4047 drop_frame_cnt,
4048 100*drop_frame_cnt/total_frame_cnt,
4049 total_frame_cnt,
4050 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4053 // time to uninit all, except global stuff:
4054 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4056 if(mpctx->set_of_sub_size > 0) {
4057 current_module="sub_free";
4058 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4059 sub_free(mpctx->set_of_subtitles[i]);
4060 #ifdef CONFIG_ASS
4061 if(mpctx->set_of_ass_tracks[i])
4062 ass_free_track( mpctx->set_of_ass_tracks[i] );
4063 #endif
4065 mpctx->set_of_sub_size = 0;
4067 vo_sub_last = vo_sub=NULL;
4068 subdata=NULL;
4069 #ifdef CONFIG_ASS
4070 ass_track = NULL;
4071 if(ass_library)
4072 ass_clear_fonts(ass_library);
4073 #endif
4075 if (!mpctx->stop_play) // In case some goto jumped here...
4076 mpctx->stop_play = PT_NEXT_ENTRY;
4078 int playtree_direction = 1;
4080 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4081 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4082 play_tree_iter_free(mpctx->playtree_iter);
4083 mpctx->playtree_iter = NULL;
4085 mpctx->play_tree_step = 1;
4086 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4087 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4088 if(mpctx->playtree_iter) {
4089 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4090 play_tree_iter_free(mpctx->playtree_iter);
4091 mpctx->playtree_iter = NULL;
4094 } else if (mpctx->stop_play == PT_STOP) {
4095 play_tree_iter_free(mpctx->playtree_iter);
4096 mpctx->playtree_iter = NULL;
4097 } else { // NEXT PREV SRC
4098 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4101 while(mpctx->playtree_iter != NULL) {
4102 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4103 if(mpctx->filename == NULL) {
4104 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4105 play_tree_iter_free(mpctx->playtree_iter);
4106 mpctx->playtree_iter = NULL;
4108 } else
4109 break;
4112 #ifdef CONFIG_GUI
4113 if(use_gui && !mpctx->playtree_iter) {
4114 #ifdef CONFIG_DVDREAD
4115 if(!guiIntfStruct.DiskChanged)
4116 #endif
4117 mplEnd();
4119 #endif
4121 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4122 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4123 mpctx->stop_play = 0;
4124 goto play_next_file;
4128 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4130 return 1;
4132 #endif /* DISABLE_MAIN */