Merge svn changes up to r28038
[mplayer/glamo.git] / mplayer.c
blobd9eb28129de269ba0c8ee7f52075202b094b707a
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
8 #include "talloc.h"
10 #if defined(__MINGW32__) || defined(__CYGWIN__)
11 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
12 #include <windows.h>
13 #endif
14 #include <string.h>
15 #include <unistd.h>
17 // #include <sys/mman.h>
18 #include <sys/types.h>
19 #ifndef __MINGW32__
20 #include <sys/ioctl.h>
21 #include <sys/wait.h>
22 #else
23 #define SIGHUP 1 /* hangup */
24 #define SIGQUIT 3 /* quit */
25 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
26 #define SIGBUS 10 /* bus error */
27 #define SIGPIPE 13 /* broken pipe */
28 #endif
30 #include <sys/time.h>
31 #include <sys/stat.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include <errno.h>
40 #include "version.h"
42 #include "mp_msg.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "mplayer.h"
50 #include "access_mpcontext.h"
51 #include "m_property.h"
53 #include "cfg-mplayer-def.h"
55 #include "libavutil/intreadwrite.h"
57 #include "subreader.h"
59 #include "mp_osd.h"
60 #include "libvo/video_out.h"
62 #include "libvo/font_load.h"
63 #include "libvo/sub.h"
65 #ifdef CONFIG_X11
66 #include "libvo/x11_common.h"
67 #endif
69 #include "libao2/audio_out.h"
71 #include "codec-cfg.h"
73 #include "edl.h"
75 #include "spudec.h"
76 #include "vobsub.h"
78 #include "osdep/getch2.h"
79 #include "osdep/timer.h"
81 #include "cpudetect.h"
83 #ifdef CONFIG_GUI
84 #include "gui/interface.h"
85 #endif
87 #include "input/input.h"
89 int slave_mode=0;
90 int player_idle_mode=0;
91 int quiet=0;
92 int enable_mouse_movements=0;
93 float start_volume = -1;
95 #if defined(__MINGW32__) || defined(__CYGWIN__)
96 char * proc_priority=NULL;
97 #endif
99 char *heartbeat_cmd;
101 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
103 #ifdef HAVE_RTC
104 #ifdef __linux__
105 #include <linux/rtc.h>
106 #else
107 #include <rtc.h>
108 #define RTC_IRQP_SET RTCIO_IRQP_SET
109 #define RTC_PIE_ON RTCIO_PIE_ON
110 #endif /* __linux__ */
111 #endif /* HAVE_RTC */
113 #ifdef CONFIG_TV
114 #include "stream/tv.h"
115 #endif
116 #ifdef CONFIG_RADIO
117 #include "stream/stream_radio.h"
118 #endif
120 #ifdef CONFIG_DVBIN
121 #include "stream/dvbin.h"
122 #include "stream/cache2.h"
123 #endif
125 //**************************************************************************//
126 // Playtree
127 //**************************************************************************//
128 #include "playtree.h"
129 #include "playtreeparser.h"
131 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
132 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
134 //**************************************************************************//
135 // Config
136 //**************************************************************************//
137 #include "parser-cfg.h"
138 #include "parser-mpcmd.h"
140 //**************************************************************************//
141 // Config file
142 //**************************************************************************//
144 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
146 #include "get_path.h"
148 //**************************************************************************//
149 //**************************************************************************//
150 // Input media streaming & demultiplexer:
151 //**************************************************************************//
153 static int max_framesize=0;
155 #include "stream/stream.h"
156 #include "libmpdemux/demuxer.h"
157 #include "libmpdemux/stheader.h"
159 #ifdef CONFIG_DVDREAD
160 #include "stream/stream_dvd.h"
161 #endif
163 #ifdef CONFIG_DVDNAV
164 #include "stream/stream_dvdnav.h"
165 #endif
167 #include "libmpcodecs/dec_audio.h"
168 #include "libmpcodecs/dec_video.h"
169 #include "libmpcodecs/mp_image.h"
170 #include "libmpcodecs/vf.h"
171 #include "libmpcodecs/vd.h"
173 #include "mixer.h"
175 #include "mp_core.h"
176 #include "options.h"
177 #include "defaultopts.h"
179 //**************************************************************************//
180 //**************************************************************************//
182 // Common FIFO functions, and keyboard/event FIFO code
183 #include "mp_fifo.h"
184 int noconsolecontrols=0;
185 //**************************************************************************//
187 // benchmark:
188 double video_time_usage=0;
189 double vout_time_usage=0;
190 static double audio_time_usage=0;
191 static int total_time_usage_start=0;
192 static int total_frame_cnt=0;
193 static int drop_frame_cnt=0; // total number of dropped frames
194 int benchmark=0;
196 // options:
197 int auto_quality=0;
198 static int output_quality=0;
200 int use_gui=0;
202 #ifdef CONFIG_GUI
203 int enqueue=0;
204 #endif
206 static int list_properties = 0;
208 int osd_level=1;
209 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
210 unsigned int osd_visible;
211 int osd_duration = 1000;
213 int term_osd = 1;
214 static char* term_osd_esc = "\x1b[A\r\x1b[K";
215 static char* playing_msg = NULL;
216 // seek:
217 static double seek_to_sec;
218 static off_t seek_to_byte=0;
219 static off_t step_sec=0;
220 static int loop_seek=0;
222 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
224 // A/V sync:
225 int autosync=0; // 30 might be a good default value.
227 // codecs:
228 char **audio_codec_list=NULL; // override audio codec
229 char **video_codec_list=NULL; // override video codec
230 char **audio_fm_list=NULL; // override audio codec family
231 char **video_fm_list=NULL; // override video codec family
233 // demuxer:
234 extern char *demuxer_name; // override demuxer
235 extern char *audio_demuxer_name; // override audio demuxer
236 extern char *sub_demuxer_name; // override sub demuxer
238 int vobsub_id=-1;
239 char* audio_lang=NULL;
240 char* dvdsub_lang=NULL;
241 static char* spudec_ifo=NULL;
242 int forced_subs_only=0;
243 int file_filter=1;
245 // cache2:
246 int stream_cache_size=-1;
247 #ifdef CONFIG_STREAM_CACHE
248 extern int cache_fill_status;
250 float stream_cache_min_percent=20.0;
251 float stream_cache_seek_min_percent=50.0;
252 #else
253 #define cache_fill_status 0
254 #endif
256 // dump:
257 static char *stream_dump_name="stream.dump";
258 int stream_dump_type=0;
260 // A-V sync:
261 static float default_max_pts_correction=-1;//0.01f;
262 static float max_pts_correction=0;//default_max_pts_correction;
263 static float c_total=0;
264 float audio_delay=0;
265 static int ignore_start=0;
267 static int softsleep=0;
269 double force_fps=0;
270 static int force_srate=0;
271 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
272 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
273 static int play_n_frames=-1;
274 static int play_n_frames_mf=-1;
276 // sub:
277 char *font_name=NULL;
278 char *sub_font_name=NULL;
279 extern int font_fontconfig;
280 float font_factor=0.75;
281 char **sub_name=NULL;
282 float sub_delay=0;
283 float sub_fps=0;
284 int sub_auto = 1;
285 char *vobsub_name=NULL;
286 /*DSP!!char *dsp=NULL;*/
287 int subcc_enabled=0;
288 int suboverlap_enabled = 1;
290 #ifdef CONFIG_ASS
291 #include "libass/ass.h"
292 #include "libass/ass_mp.h"
293 #endif
295 char* current_module=NULL; // for debugging
298 // ---
300 #ifdef CONFIG_MENU
301 #include "m_struct.h"
302 #include "libmenu/menu.h"
303 extern 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, const char* 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 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
696 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
698 exit(rc);
701 static void exit_player(struct MPContext *mpctx, const char* how)
703 exit_player_with_rc(mpctx, how, 1);
706 #ifndef __MINGW32__
707 static void child_sighandler(int x){
708 pid_t pid;
709 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
711 #endif
713 #ifdef CONFIG_CRASH_DEBUG
714 static char *prog_path;
715 static int crash_debug = 0;
716 #endif
718 static void exit_sighandler(int x){
719 static int sig_count=0;
720 #ifdef CONFIG_CRASH_DEBUG
721 if (!crash_debug || x != SIGTRAP)
722 #endif
723 ++sig_count;
724 if(sig_count==5)
726 /* We're crashing bad and can't uninit cleanly :(
727 * by popular request, we make one last (dirty)
728 * effort to restore the user's
729 * terminal. */
730 getch2_disable();
731 exit(1);
733 if(sig_count==6) exit(1);
734 if(sig_count>6){
735 // can't stop :(
736 #ifndef __MINGW32__
737 kill(getpid(),SIGKILL);
738 #endif
740 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
741 current_module?current_module:"unknown"
743 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
744 if(sig_count<=1)
745 switch(x){
746 case SIGINT:
747 case SIGQUIT:
748 case SIGTERM:
749 case SIGKILL:
750 async_quit_request = 1;
751 return; // killed from keyboard (^C) or killed [-9]
752 case SIGILL:
753 #ifdef RUNTIME_CPUDETECT
754 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
755 #else
756 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
757 #endif
758 case SIGFPE:
759 case SIGSEGV:
760 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
761 default:
762 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
763 #ifdef CONFIG_CRASH_DEBUG
764 if (crash_debug) {
765 int gdb_pid;
766 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
767 gdb_pid = fork();
768 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
769 if (gdb_pid == 0) { // We are the child
770 char spid[20];
771 snprintf(spid, sizeof(spid), "%i", getppid());
772 getch2_disable(); // allow terminal to work properly with gdb
773 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
774 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
775 } else if (gdb_pid < 0)
776 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
777 else {
778 waitpid(gdb_pid, NULL, 0);
780 if (x == SIGTRAP) return;
782 #endif
784 getch2_disable();
785 exit(1);
788 #include "cfg-mplayer.h"
790 static int cfg_include(m_option_t *conf, char *filename)
792 return m_config_parse_config_file(conf->priv, filename);
795 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
797 char *conffile;
798 int conffile_fd;
799 if (!disable_system_conf &&
800 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
801 exit_player(mpctx, NULL);
802 if ((conffile = get_path("")) == NULL) {
803 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
804 } else {
805 #ifdef __MINGW32__
806 mkdir(conffile);
807 #else
808 mkdir(conffile, 0777);
809 #endif
810 free(conffile);
811 if ((conffile = get_path("config")) == NULL) {
812 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
813 } else {
814 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
815 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
816 write(conffile_fd, default_config, strlen(default_config));
817 close(conffile_fd);
819 if (!disable_user_conf &&
820 m_config_parse_config_file(conf, conffile) < 0)
821 exit_player(mpctx, NULL);
822 free(conffile);
827 #define PROFILE_CFG_PROTOCOL "protocol."
829 static void load_per_protocol_config (m_config_t* conf, const char *const file)
831 char *str;
832 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
833 m_profile_t *p;
835 /* does filename actually uses a protocol ? */
836 str = strstr (file, "://");
837 if (!str)
838 return;
840 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
841 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
842 p = m_config_get_profile (conf, protocol);
843 if (p)
845 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
846 m_config_set_profile(conf,p);
850 #define PROFILE_CFG_EXTENSION "extension."
852 static void load_per_extension_config (m_config_t* conf, const char *const file)
854 char *str;
855 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
856 m_profile_t *p;
858 /* does filename actually have an extension ? */
859 str = strrchr (file, '.');
860 if (!str)
861 return;
863 sprintf (extension, PROFILE_CFG_EXTENSION);
864 strncat (extension, ++str, 7);
865 p = m_config_get_profile (conf, extension);
866 if (p)
868 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
869 m_config_set_profile(conf,p);
873 #define PROFILE_CFG_VO "vo."
874 #define PROFILE_CFG_AO "ao."
876 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
878 char profile[strlen (cfg) + strlen (out) + 1];
879 m_profile_t *p;
881 sprintf (profile, "%s%s", cfg, out);
882 p = m_config_get_profile (conf, profile);
883 if (p)
885 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
886 m_config_set_profile(conf,p);
890 static void load_per_file_config (m_config_t* conf, const char *const file)
892 char *confpath;
893 char cfg[strlen(file)+10];
894 struct stat st;
895 char *name;
897 sprintf (cfg, "%s.conf", file);
899 if (use_filedir_conf && !stat (cfg, &st))
901 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
902 m_config_parse_config_file (conf, cfg);
903 return;
906 if ((name = strrchr (cfg, '/')) == NULL)
907 name = cfg;
908 else
909 name++;
911 if ((confpath = get_path (name)) != NULL)
913 if (!stat (confpath, &st))
915 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
916 m_config_parse_config_file (conf, confpath);
919 free (confpath);
923 /* When libmpdemux performs a blocking operation (network connection or
924 * cache filling) if the operation fails we use this function to check
925 * if it was interrupted by the user.
926 * The function returns a new value for eof. */
927 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
929 mp_cmd_t* cmd;
930 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
931 switch(cmd->id) {
932 case MP_CMD_QUIT:
933 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
934 case MP_CMD_PLAY_TREE_STEP: {
935 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
936 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
937 } break;
938 case MP_CMD_PLAY_TREE_UP_STEP: {
939 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
940 } break;
941 case MP_CMD_PLAY_ALT_SRC_STEP: {
942 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
943 } break;
945 mp_cmd_free(cmd);
947 return stop_play;
950 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
952 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
954 play_tree_add_bpf(entry,mpctx->filename);
956 #ifdef CONFIG_GUI
957 if (use_gui) {
958 if (entry) {
959 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
960 play_tree_free_list(entry,1);
962 } else
963 #endif
965 if(!entry) {
966 entry = mpctx->playtree_iter->tree;
967 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
968 return PT_NEXT_ENTRY;
970 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
971 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
972 return PT_NEXT_ENTRY;
975 play_tree_remove(entry,1,1);
976 return PT_NEXT_SRC;
978 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
979 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
980 entry = mpctx->playtree_iter->tree;
981 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
982 return PT_NEXT_ENTRY;
984 play_tree_remove(entry,1,1);
986 return PT_NEXT_SRC;
989 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
991 sub_data *subd;
992 #ifdef CONFIG_ASS
993 ass_track_t *asst = 0;
994 #endif
996 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
997 return;
1000 subd = sub_read_file(filename, fps);
1001 #ifdef CONFIG_ASS
1002 if (ass_enabled)
1003 #ifdef CONFIG_ICONV
1004 asst = ass_read_file(ass_library, filename, sub_cp);
1005 #else
1006 asst = ass_read_file(ass_library, filename, 0);
1007 #endif
1008 if (ass_enabled && subd && !asst)
1009 asst = ass_read_subdata(ass_library, subd, fps);
1011 if (!asst && !subd)
1012 #else
1013 if(!subd)
1014 #endif
1015 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1016 filename_recode(filename));
1018 #ifdef CONFIG_ASS
1019 if (!asst && !subd) return;
1020 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1021 #else
1022 if (!subd) return;
1023 #endif
1024 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1025 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1026 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1027 filename_recode(filename));
1028 ++mpctx->set_of_sub_size;
1029 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1030 filename_recode(filename));
1033 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1034 void update_set_of_subtitles(struct MPContext *mpctx)
1035 // subdata was changed, set_of_sub... have to be updated.
1037 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1038 int i;
1039 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1040 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1041 set_of_subtitles[i-1] = set_of_subtitles[i];
1042 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1043 --mpctx->set_of_sub_size;
1044 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1046 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1047 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1049 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1050 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1051 ++mpctx->set_of_sub_size;
1055 void init_vo_spudec(struct MPContext *mpctx)
1057 if (vo_spudec)
1058 spudec_free(vo_spudec);
1059 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1060 vo_spudec = NULL;
1061 if (spudec_ifo) {
1062 unsigned int palette[16], width, height;
1063 current_module="spudec_init_vobsub";
1064 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1065 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1068 #ifdef CONFIG_DVDREAD
1069 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1070 current_module="spudec_init_dvdread";
1071 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1072 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1073 NULL, 0);
1075 #endif
1077 #ifdef CONFIG_DVDNAV
1078 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1079 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1080 current_module="spudec_init_dvdnav";
1081 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1083 #endif
1085 if (vo_spudec==NULL) {
1086 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1087 current_module="spudec_init_normal";
1088 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1089 spudec_set_font_factor(vo_spudec,font_factor);
1092 if (vo_spudec!=NULL)
1093 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1097 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1098 * make it all work is to use the builtin SDL-bootstrap code, which
1099 * will be done automatically by replacing our main() if we include SDL.h.
1101 #if defined(__APPLE__) && defined(CONFIG_SDL)
1102 #include <SDL.h>
1103 #endif
1106 * \brief append a formatted string
1107 * \param buf buffer to print into
1108 * \param pos position of terminating 0 in buf
1109 * \param len maximum number of characters in buf, not including terminating 0
1110 * \param format printf format string
1112 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1114 va_list va;
1115 va_start(va, format);
1116 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1117 va_end(va);
1118 if (*pos >= len ) {
1119 buf[len] = 0;
1120 *pos = len;
1125 * \brief append time in the hh:mm:ss.f format
1126 * \param buf buffer to print into
1127 * \param pos position of terminating 0 in buf
1128 * \param len maximum number of characters in buf, not including terminating 0
1129 * \param time time value to convert/append
1131 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1132 long tenths = 10 * time;
1133 int f1 = tenths % 10;
1134 int ss = (tenths / 10) % 60;
1135 int mm = (tenths / 600) % 60;
1136 int hh = tenths / 36000;
1137 if (time <= 0) {
1138 saddf(buf, pos, len, "unknown");
1139 return;
1141 if (hh > 0)
1142 saddf(buf, pos, len, "%2d:", hh);
1143 if (hh > 0 || mm > 0)
1144 saddf(buf, pos, len, "%02d:", mm);
1145 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1149 * \brief print the status line
1150 * \param a_pos audio position
1151 * \param a_v A-V desynchronization
1152 * \param corr amount out A-V synchronization
1154 static void print_status(struct MPContext *mpctx, float a_pos, float a_v, float corr)
1156 struct MPOpts *opts = &mpctx->opts;
1157 sh_video_t * const sh_video = mpctx->sh_video;
1158 int width;
1159 char *line;
1160 unsigned pos = 0;
1161 get_screen_size();
1162 if (screen_width > 0)
1163 width = screen_width;
1164 else
1165 width = 80;
1166 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1167 /* Windows command line is broken (MinGW's rxvt works, but we
1168 * should not depend on that). */
1169 width--;
1170 #endif
1171 line = malloc(width + 1); // one additional char for the terminating null
1173 // Audio time
1174 if (mpctx->sh_audio) {
1175 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1176 if (!sh_video) {
1177 float len = demuxer_get_time_length(mpctx->demuxer);
1178 saddf(line, &pos, width, "(");
1179 sadd_hhmmssf(line, &pos, width, a_pos);
1180 saddf(line, &pos, width, ") of %.1f (", len);
1181 sadd_hhmmssf(line, &pos, width, len);
1182 saddf(line, &pos, width, ") ");
1186 // Video time
1187 if (sh_video)
1188 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1190 // A-V sync
1191 if (mpctx->sh_audio && sh_video)
1192 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1194 // Video stats
1195 if (sh_video)
1196 saddf(line, &pos, width, "%3d/%3d ",
1197 (int)sh_video->num_frames,
1198 (int)sh_video->num_frames_decoded);
1200 // CPU usage
1201 if (sh_video) {
1202 if (sh_video->timer > 0.5)
1203 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1204 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1205 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1206 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1207 else
1208 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1209 } else if (mpctx->sh_audio) {
1210 if (mpctx->delay > 0.5)
1211 saddf(line, &pos, width, "%4.1f%% ",
1212 100.0*audio_time_usage/(double)mpctx->delay);
1213 else
1214 saddf(line, &pos, width, "??,?%% ");
1217 // VO stats
1218 if (sh_video)
1219 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1221 #ifdef CONFIG_STREAM_CACHE
1222 // cache stats
1223 if (stream_cache_size > 0)
1224 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1225 #endif
1227 // other
1228 if (opts->playback_speed != 1)
1229 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1231 // end
1232 if (erase_to_end_of_line) {
1233 line[pos] = 0;
1234 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1235 } else {
1236 memset(&line[pos], ' ', width - pos);
1237 line[width] = 0;
1238 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1240 free(line);
1244 * \brief build a chain of audio filters that converts the input format
1245 * to the ao's format, taking into account the current playback_speed.
1246 * \param sh_audio describes the requested input format of the chain.
1247 * \param ao_data describes the requested output format of the chain.
1249 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1251 struct MPOpts *opts = &mpctx->opts;
1252 int new_srate;
1253 int result;
1254 if (!sh_audio)
1256 #ifdef CONFIG_GUI
1257 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1258 #endif
1259 mpctx->mixer.afilter = NULL;
1260 return 0;
1262 if(af_control_any_rev(sh_audio->afilter,
1263 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1264 &opts->playback_speed)) {
1265 new_srate = sh_audio->samplerate;
1266 } else {
1267 new_srate = sh_audio->samplerate * opts->playback_speed;
1268 if (new_srate != ao_data->samplerate) {
1269 // limits are taken from libaf/af_resample.c
1270 if (new_srate < 8000)
1271 new_srate = 8000;
1272 if (new_srate > 192000)
1273 new_srate = 192000;
1274 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1277 result = init_audio_filters(sh_audio, new_srate,
1278 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1279 mpctx->mixer.afilter = sh_audio->afilter;
1280 #ifdef CONFIG_GUI
1281 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1282 #endif
1283 return result;
1287 typedef struct mp_osd_msg mp_osd_msg_t;
1288 struct mp_osd_msg {
1289 /// Previous message on the stack.
1290 mp_osd_msg_t* prev;
1291 /// Message text.
1292 char msg[64];
1293 int id,level,started;
1294 /// Display duration in ms.
1295 unsigned time;
1298 /// OSD message stack.
1299 static mp_osd_msg_t* osd_msg_stack = NULL;
1302 * \brief Add a message on the OSD message stack
1304 * If a message with the same id is already present in the stack
1305 * it is pulled on top of the stack, otherwise a new message is created.
1309 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1310 mp_osd_msg_t *msg,*last=NULL;
1311 va_list va;
1312 int r;
1314 // look if the id is already in the stack
1315 for(msg = osd_msg_stack ; msg && msg->id != id ;
1316 last = msg, msg = msg->prev);
1317 // not found: alloc it
1318 if(!msg) {
1319 msg = calloc(1,sizeof(mp_osd_msg_t));
1320 msg->prev = osd_msg_stack;
1321 osd_msg_stack = msg;
1322 } else if(last) { // found, but it's not on top of the stack
1323 last->prev = msg->prev;
1324 msg->prev = osd_msg_stack;
1325 osd_msg_stack = msg;
1327 // write the msg
1328 va_start(va,fmt);
1329 r = vsnprintf(msg->msg, 64, fmt, va);
1330 va_end(va);
1331 if(r >= 64) msg->msg[63] = 0;
1332 // set id and time
1333 msg->id = id;
1334 msg->level = level;
1335 msg->time = time;
1340 * \brief Remove a message from the OSD stack
1342 * This function can be used to get rid of a message right away.
1346 void rm_osd_msg(int id) {
1347 mp_osd_msg_t *msg,*last=NULL;
1349 // Search for the msg
1350 for(msg = osd_msg_stack ; msg && msg->id != id ;
1351 last = msg, msg = msg->prev);
1352 if(!msg) return;
1354 // Detach it from the stack and free it
1355 if(last)
1356 last->prev = msg->prev;
1357 else
1358 osd_msg_stack = msg->prev;
1359 free(msg);
1363 * \brief Remove all messages from the OSD stack
1367 static void clear_osd_msgs(void) {
1368 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1369 while(msg) {
1370 prev = msg->prev;
1371 free(msg);
1372 msg = prev;
1374 osd_msg_stack = NULL;
1378 * \brief Get the current message from the OSD stack.
1380 * This function decrements the message timer and destroys the old ones.
1381 * The message that should be displayed is returned (if any).
1385 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1387 mp_osd_msg_t *msg,*prev,*last = NULL;
1388 static unsigned last_update = 0;
1389 unsigned now = GetTimerMS();
1390 unsigned diff;
1391 char hidden_dec_done = 0;
1393 if (osd_visible) {
1394 // 36000000 means max timed visibility is 1 hour into the future, if
1395 // the difference is greater assume it's wrapped around from below 0
1396 if (osd_visible - now > 36000000) {
1397 osd_visible = 0;
1398 vo_osd_progbar_type = -1; // disable
1399 vo_osd_changed(OSDTYPE_PROGBAR);
1400 if (mpctx->osd_function != OSD_PAUSE)
1401 mpctx->osd_function = OSD_PLAY;
1405 if(!last_update) last_update = now;
1406 diff = now >= last_update ? now - last_update : 0;
1408 last_update = now;
1410 // Look for the first message in the stack with high enough level.
1411 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1412 prev = msg->prev;
1413 if(msg->level > osd_level && hidden_dec_done) continue;
1414 // The message has a high enough level or it is the first hidden one
1415 // in both cases we decrement the timer or kill it.
1416 if(!msg->started || msg->time > diff) {
1417 if(msg->started) msg->time -= diff;
1418 else msg->started = 1;
1419 // display it
1420 if(msg->level <= osd_level) return msg;
1421 hidden_dec_done = 1;
1422 continue;
1424 // kill the message
1425 free(msg);
1426 if(last) {
1427 last->prev = prev;
1428 msg = last;
1429 } else {
1430 osd_msg_stack = prev;
1431 msg = NULL;
1434 // Nothing found
1435 return NULL;
1439 * \brief Display the OSD bar.
1441 * Display the OSD bar or fall back on a simple message.
1445 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1447 if(osd_level < 1) return;
1449 if(mpctx->sh_video) {
1450 osd_visible = (GetTimerMS() + 1000) | 1;
1451 vo_osd_progbar_type = type;
1452 vo_osd_progbar_value = 256*(val-min)/(max-min);
1453 vo_osd_changed(OSDTYPE_PROGBAR);
1454 return;
1457 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1458 name,ROUND(100*(val-min)/(max-min)));
1463 * \brief Update the OSD message line.
1465 * This function displays the current message on the vo OSD or on the term.
1466 * If the stack is empty and the OSD level is high enough the timer
1467 * is displayed (only on the vo OSD).
1471 static void update_osd_msg(struct MPContext *mpctx)
1473 mp_osd_msg_t *msg;
1474 struct osd_state *osd = mpctx->osd;
1475 char osd_text_timer[64];
1477 // Look if we have a msg
1478 if((msg = get_osd_msg(mpctx))) {
1479 if (strcmp(osd->osd_text, msg->msg)) {
1480 strncpy(osd->osd_text, msg->msg, 63);
1481 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1482 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1484 return;
1487 if(mpctx->sh_video) {
1488 // fallback on the timer
1489 if(osd_level>=2) {
1490 int len = demuxer_get_time_length(mpctx->demuxer);
1491 int percentage = -1;
1492 char percentage_text[10];
1493 int pts = demuxer_get_current_time(mpctx->demuxer);
1495 if (mpctx->osd_show_percentage)
1496 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1498 if (percentage >= 0)
1499 snprintf(percentage_text, 9, " (%d%%)", percentage);
1500 else
1501 percentage_text[0] = 0;
1503 if (osd_level == 3)
1504 snprintf(osd_text_timer, 63,
1505 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1506 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1507 len/3600,(len/60)%60,len%60,percentage_text);
1508 else
1509 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1510 mpctx->osd_function,pts/3600,(pts/60)%60,
1511 pts%60,percentage_text);
1512 } else
1513 osd_text_timer[0]=0;
1515 // always decrement the percentage timer
1516 if(mpctx->osd_show_percentage)
1517 mpctx->osd_show_percentage--;
1519 if (strcmp(osd->osd_text, osd_text_timer)) {
1520 strncpy(osd->osd_text, osd_text_timer, 63);
1521 vo_osd_changed(OSDTYPE_OSD);
1523 return;
1526 // Clear the term osd line
1527 if (term_osd && osd->osd_text[0]) {
1528 osd->osd_text[0] = 0;
1529 printf("%s\n",term_osd_esc);
1533 ///@}
1534 // OSDMsgStack
1537 void reinit_audio_chain(struct MPContext *mpctx)
1539 struct MPOpts *opts = &mpctx->opts;
1540 if(mpctx->sh_audio){
1541 current_module="init_audio_codec";
1542 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1543 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1544 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1545 mpctx->d_audio->id = -2;
1546 return;
1547 } else
1548 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1549 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1552 //const ao_info_t *info=audio_out->info;
1553 current_module="af_preinit";
1554 ao_data.samplerate=force_srate;
1555 ao_data.channels=0;
1556 ao_data.format=audio_output_format;
1557 #if 1
1558 // first init to detect best values
1559 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1560 // input:
1561 mpctx->sh_audio->samplerate,
1562 // output:
1563 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1564 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1565 exit_player(mpctx, MSGTR_Exit_error);
1567 #endif
1568 current_module="ao2_init";
1569 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1570 0, // plugin flag
1571 ao_data.samplerate,
1572 ao_data.channels,
1573 ao_data.format,0))){
1574 // FAILED:
1575 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1576 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1577 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1578 mpctx->d_audio->id = -2;
1579 return;
1580 } else {
1581 // SUCCESS:
1582 mpctx->initialized_flags|=INITIALIZED_AO;
1583 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1584 mpctx->audio_out->info->short_name,
1585 ao_data.samplerate, ao_data.channels,
1586 af_fmt2str_short(ao_data.format),
1587 af_fmt2bits(ao_data.format)/8 );
1588 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1589 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1590 if(strlen(mpctx->audio_out->info->comment) > 0)
1591 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1592 // init audio filters:
1593 #if 1
1594 current_module="af_init";
1595 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1596 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1597 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1598 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1599 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1601 #endif
1603 mpctx->mixer.audio_out = mpctx->audio_out;
1604 mpctx->mixer.volstep = volstep;
1609 ///@}
1610 // Command2Property
1613 // Return pts value corresponding to the end point of audio written to the
1614 // ao so far.
1615 static double written_audio_pts(struct MPContext *mpctx)
1617 sh_audio_t *sh_audio = mpctx->sh_audio;
1618 demux_stream_t *d_audio = mpctx->d_audio;
1619 double buffered_output;
1620 // first calculate the end pts of audio that has been output by decoder
1621 double a_pts = sh_audio->pts;
1622 if (a_pts != MP_NOPTS_VALUE)
1623 // Good, decoder supports new way of calculating audio pts.
1624 // sh_audio->pts is the timestamp of the latest input packet with
1625 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1626 // the amount of bytes the decoder has written after that timestamp.
1627 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1628 else {
1629 // Decoder doesn't support new way of calculating pts (or we're
1630 // being called before it has decoded anything with known timestamp).
1631 // Use the old method of audio pts calculation: take the timestamp
1632 // of last packet with known pts the decoder has read data from,
1633 // and add amount of bytes read after the beginning of that packet
1634 // divided by input bps. This will be inaccurate if the input/output
1635 // ratio is not constant for every audio packet or if it is constant
1636 // but not accurately known in sh_audio->i_bps.
1638 a_pts = d_audio->pts;
1639 // ds_tell_pts returns bytes read after last timestamp from
1640 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1641 // it has read but not decoded
1642 if (sh_audio->i_bps)
1643 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1644 (double)sh_audio->i_bps;
1646 // Now a_pts hopefully holds the pts for end of audio from decoder.
1647 // Substract data in buffers between decoder and audio out.
1649 // Decoded but not filtered
1650 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1652 // Data buffered in audio filters, measured in bytes of "missing" output
1653 buffered_output = af_calc_delay(sh_audio->afilter);
1655 // Data that was ready for ao but was buffered because ao didn't fully
1656 // accept everything to internal buffers yet
1657 buffered_output += sh_audio->a_out_buffer_len;
1659 // Filters divide audio length by playback_speed, so multiply by it
1660 // to get the length in original units without speedup or slowdown
1661 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1663 return a_pts;
1666 // Return pts value corresponding to currently playing audio.
1667 double playing_audio_pts(struct MPContext *mpctx)
1669 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1670 mpctx->audio_out->get_delay();
1673 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1674 struct MPOpts *opts = &mpctx->opts;
1675 // check for frame-drop:
1676 current_module = "check_framedrop";
1677 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1678 static int dropped_frames;
1679 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1680 float d = delay-mpctx->delay;
1681 ++total_frame_cnt;
1682 // we should avoid dropping too many frames in sequence unless we
1683 // are too late. and we allow 100ms A-V delay here:
1684 if (d < -dropped_frames*frame_time-0.100 &&
1685 mpctx->osd_function != OSD_PAUSE) {
1686 ++drop_frame_cnt;
1687 ++dropped_frames;
1688 return frame_dropping;
1689 } else
1690 dropped_frames = 0;
1692 return 0;
1695 static int generate_video_frame(struct MPContext *mpctx)
1697 sh_video_t * const sh_video = mpctx->sh_video;
1698 demux_stream_t *d_video = mpctx->d_video;
1700 unsigned char *start;
1701 int in_size;
1702 int hit_eof=0;
1703 double pts;
1705 while (1) {
1706 int drop_frame = check_framedrop(mpctx, sh_video->frametime);
1707 void *decoded_frame;
1708 current_module = "decode video";
1709 // XXX Time used in this call is not counted in any performance
1710 // timer now, OSD is not updated correctly for filter-added frames
1711 if (vf_output_queued_frame(sh_video->vfilter))
1712 break;
1713 current_module = "video_read_frame";
1714 in_size = ds_get_packet_pts(d_video, &start, &pts);
1715 if (in_size < 0) {
1716 // try to extract last frames in case of decoder lag
1717 in_size = 0;
1718 pts = 1e300;
1719 hit_eof = 1;
1721 if (in_size > max_framesize)
1722 max_framesize = in_size;
1723 current_module = "decode video";
1724 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1725 if (decoded_frame) {
1726 update_subtitles(sh_video, mpctx->d_sub, 0);
1727 update_teletext(sh_video, mpctx->demuxer, 0);
1728 update_osd_msg(mpctx);
1729 current_module = "filter video";
1730 if (filter_video(sh_video, decoded_frame, sh_video->pts,
1731 mpctx->osd))
1732 break;
1733 } else if (drop_frame)
1734 return -1;
1735 if (hit_eof)
1736 return 0;
1738 return 1;
1741 #ifdef HAVE_RTC
1742 int rtc_fd = -1;
1743 #endif
1745 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1747 #ifdef HAVE_RTC
1748 if (rtc_fd >= 0){
1749 // -------- RTC -----------
1750 current_module="sleep_rtc";
1751 while (time_frame > 0.000) {
1752 unsigned long rtc_ts;
1753 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1754 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1755 time_frame -= get_relative_time(mpctx);
1757 } else
1758 #endif
1760 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1761 // unnecessarily high CPU usage
1762 float margin = softsleep ? 0.011 : 0;
1763 current_module = "sleep_timer";
1764 while (time_frame > margin) {
1765 usec_sleep(1000000 * (time_frame - margin));
1766 time_frame -= get_relative_time(mpctx);
1768 if (softsleep){
1769 current_module = "sleep_soft";
1770 if (time_frame < 0)
1771 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1772 while (time_frame > 0)
1773 time_frame -= get_relative_time(mpctx); // burn the CPU
1776 return time_frame;
1779 #ifdef CONFIG_DVDNAV
1780 #ifndef FF_B_TYPE
1781 #define FF_B_TYPE 3
1782 #endif
1783 /// store decoded video image
1784 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1785 mp_image_t *from_mpi) {
1786 mp_image_t *mpi;
1788 /// Do not store B-frames
1789 if (from_mpi->pict_type == FF_B_TYPE)
1790 return to_mpi;
1792 if (to_mpi &&
1793 to_mpi->w == from_mpi->w &&
1794 to_mpi->h == from_mpi->h &&
1795 to_mpi->imgfmt == from_mpi->imgfmt)
1796 mpi = to_mpi;
1797 else {
1798 if (to_mpi)
1799 free_mp_image(to_mpi);
1800 if (from_mpi->w == 0 || from_mpi->h == 0)
1801 return NULL;
1802 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1805 copy_mpi(mpi,from_mpi);
1806 return mpi;
1809 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1810 struct MPOpts *opts = &ctx->opts;
1811 if (ctx->sh_video) {
1812 /// clear video pts
1813 ctx->d_video->pts = 0.0f;
1814 ctx->sh_video->pts = 0.0f;
1815 ctx->sh_video->i_pts = 0.0f;
1816 ctx->sh_video->last_pts = 0.0f;
1817 ctx->sh_video->num_buffered_pts = 0;
1818 ctx->sh_video->num_frames = 0;
1819 ctx->sh_video->num_frames_decoded = 0;
1820 ctx->sh_video->timer = 0.0f;
1821 ctx->sh_video->stream_delay = 0.0f;
1822 ctx->sh_video->timer = 0;
1823 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1826 if (ctx->sh_audio) {
1827 /// free audio packets and reset
1828 ds_free_packs(ctx->d_audio);
1829 audio_delay -= ctx->sh_audio->stream_delay;
1830 ctx->delay =- audio_delay;
1831 ctx->audio_out->reset();
1832 resync_audio_stream(ctx->sh_audio);
1835 if (ctx->d_sub) opts->sub_id = -2;
1837 audio_delay = 0.0f;
1839 /// clear all EOF related flags
1840 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1843 /// Restore last decoded DVDNAV (still frame)
1844 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1845 int *in_size,
1846 unsigned char **start,
1847 mp_image_t *decoded_frame)
1849 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1850 return decoded_frame;
1852 /// a change occured in dvdnav stream
1853 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1854 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1855 mp_dvdnav_context_free(mpctx);
1856 mp_dvdnav_reset_stream(mpctx);
1857 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1858 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1861 if (*in_size < 0) {
1862 float len;
1864 /// Display still frame, if any
1865 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1866 decoded_frame = mpctx->nav_smpi;
1868 /// increment video frame : continue playing after still frame
1869 len = demuxer_get_time_length(mpctx->demuxer);
1870 if (mpctx->sh_video->pts >= len &&
1871 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1872 mp_dvdnav_skip_still(mpctx->stream);
1873 mp_dvdnav_skip_wait(mpctx->stream);
1875 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1877 if (mpctx->nav_buffer) {
1878 *start = mpctx->nav_start;
1879 *in_size = mpctx->nav_in_size;
1880 if (mpctx->nav_start)
1881 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1885 return decoded_frame;
1888 /// Save last decoded DVDNAV (still frame)
1889 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1890 unsigned char *start,
1891 mp_image_t *decoded_frame)
1893 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1894 return;
1896 if (mpctx->nav_buffer)
1897 free(mpctx->nav_buffer);
1899 mpctx->nav_buffer = malloc(in_size);
1900 mpctx->nav_start = start;
1901 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1902 if (mpctx->nav_buffer)
1903 memcpy(mpctx->nav_buffer,start,in_size);
1905 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1906 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1908 #endif /* CONFIG_DVDNAV */
1910 static void adjust_sync_and_print_status(struct MPContext *mpctx,
1911 int between_frames,
1912 float timing_error)
1914 struct MPOpts *opts = &mpctx->opts;
1915 current_module="av_sync";
1917 if(mpctx->sh_audio){
1918 double a_pts, v_pts;
1920 if (autosync)
1922 * If autosync is enabled, the value for delay must be calculated
1923 * a bit differently. It is set only to the difference between
1924 * the audio and video timers. Any attempt to include the real
1925 * or corrected delay causes the pts_correction code below to
1926 * try to correct for the changes in delay which autosync is
1927 * trying to measure. This keeps the two from competing, but still
1928 * allows the code to correct for PTS drift *only*. (Using a delay
1929 * value here, even a "corrected" one, would be incompatible with
1930 * autosync mode.)
1932 a_pts = written_audio_pts(mpctx) - mpctx->delay;
1933 else
1934 a_pts = playing_audio_pts(mpctx);
1936 v_pts = mpctx->sh_video->pts;
1939 static int drop_message=0;
1940 double AV_delay = a_pts - audio_delay - v_pts;
1941 double x;
1942 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1943 ++drop_message;
1944 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1946 if (autosync)
1947 x = AV_delay*0.1f;
1948 else
1949 /* Do not correct target time for the next frame if this frame
1950 * was late not because of wrong target time but because the
1951 * target time could not be met */
1952 x = (AV_delay + timing_error * opts->playback_speed) * 0.1f;
1953 if (x < -max_pts_correction)
1954 x = -max_pts_correction;
1955 else if (x> max_pts_correction)
1956 x = max_pts_correction;
1957 if (default_max_pts_correction >= 0)
1958 max_pts_correction = default_max_pts_correction;
1959 else
1960 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1961 if (!between_frames) {
1962 mpctx->delay+=x;
1963 c_total+=x;
1965 if(!quiet)
1966 print_status(mpctx, a_pts - audio_delay, AV_delay, c_total);
1969 } else {
1970 // No audio:
1972 if (!quiet)
1973 print_status(mpctx, 0, 0, 0);
1977 static int fill_audio_out_buffers(struct MPContext *mpctx)
1979 struct MPOpts *opts = &mpctx->opts;
1980 unsigned int t;
1981 double tt;
1982 int playsize;
1983 int playflags=0;
1984 int audio_eof=0;
1985 int bytes_to_write;
1986 sh_audio_t * const sh_audio = mpctx->sh_audio;
1988 current_module="play_audio";
1990 while (1) {
1991 // all the current uses of ao_data.pts seem to be in aos that handle
1992 // sync completely wrong; there should be no need to use ao_data.pts
1993 // in get_space()
1994 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1995 bytes_to_write = mpctx->audio_out->get_space();
1996 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1997 break;
1999 // handle audio-only case:
2000 // this is where mplayer sleeps during audio-only playback
2001 // to avoid 100% CPU use
2002 usec_sleep(10000); // Wait a tick before retry
2005 while (bytes_to_write) {
2006 playsize = bytes_to_write;
2007 if (playsize > MAX_OUTBURST)
2008 playsize = MAX_OUTBURST;
2009 bytes_to_write -= playsize;
2011 // Fill buffer if needed:
2012 current_module="decode_audio";
2013 t = GetTimer();
2014 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2015 if (mpctx->d_audio->eof) {
2016 audio_eof = 1;
2017 if (sh_audio->a_out_buffer_len == 0)
2018 return 0;
2020 t = GetTimer() - t;
2021 tt = t*0.000001f; audio_time_usage+=tt;
2022 if (playsize > sh_audio->a_out_buffer_len) {
2023 playsize = sh_audio->a_out_buffer_len;
2024 if (audio_eof)
2025 playflags |= AOPLAY_FINAL_CHUNK;
2027 if (!playsize)
2028 break;
2030 // play audio:
2031 current_module="play_audio";
2033 // Is this pts value actually useful for the aos that access it?
2034 // They're obviously badly broken in the way they handle av sync;
2035 // would not having access to this make them more broken?
2036 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2037 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2039 if (playsize > 0) {
2040 sh_audio->a_out_buffer_len -= playsize;
2041 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2042 sh_audio->a_out_buffer_len);
2043 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2045 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2046 // Sanity check to avoid hanging in case current ao doesn't output
2047 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2048 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2049 sh_audio->a_out_buffer_len = 0;
2052 return 1;
2055 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2056 float *aq_sleep_time)
2058 struct MPOpts *opts = &mpctx->opts;
2059 int frame_time_remaining = 0;
2060 current_module="calc_sleep_time";
2062 *time_frame -= get_relative_time(mpctx); // reset timer
2064 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2065 float delay = mpctx->audio_out->get_delay();
2066 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2068 if (autosync) {
2070 * Adjust this raw delay value by calculating the expected
2071 * delay for this frame and generating a new value which is
2072 * weighted between the two. The higher autosync is, the
2073 * closer to the delay value gets to that which "-nosound"
2074 * would have used, and the longer it will take for A/V
2075 * sync to settle at the right value (but it eventually will.)
2076 * This settling time is very short for values below 100.
2078 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2079 float difference = delay - predicted;
2080 delay = predicted + difference / (float)autosync;
2083 *time_frame = delay - mpctx->delay / opts->playback_speed;
2085 // delay = amount of audio buffered in soundcard/driver
2086 if (delay > 0.25) delay=0.25; else
2087 if (delay < 0.10) delay=0.10;
2088 if (*time_frame > delay*0.6) {
2089 // sleep time too big - may cause audio drops (buffer underrun)
2090 frame_time_remaining = 1;
2091 *time_frame = delay*0.5;
2093 } else {
2094 // If we're lagging more than 200 ms behind the right playback rate,
2095 // don't try to "catch up".
2096 // If benchmark is set always output frames as fast as possible
2097 // without sleeping.
2098 if (*time_frame < -0.2 || benchmark)
2099 *time_frame = 0;
2102 *aq_sleep_time += *time_frame;
2105 //============================== SLEEP: ===================================
2107 // flag 256 means: libvo driver does its timing (dvb card)
2108 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2109 *time_frame = timing_sleep(mpctx, *time_frame);
2110 return frame_time_remaining;
2113 int reinit_video_chain(struct MPContext *mpctx)
2115 struct MPOpts *opts = &mpctx->opts;
2116 sh_video_t * const sh_video = mpctx->sh_video;
2117 double ar=-1.0;
2118 //================== Init VIDEO (codec & libvo) ==========================
2119 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2120 current_module="preinit_libvo";
2122 //shouldn't we set dvideo->id=-2 when we fail?
2123 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2124 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2125 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2126 goto err_out;
2128 mpctx->initialized_flags|=INITIALIZED_VO;
2131 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2132 mpctx->sh_video->stream_aspect = ar;
2133 current_module="init_video_filters";
2135 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2136 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2138 #ifdef CONFIG_MENU
2139 if(use_menu) {
2140 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2141 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2142 if(!vf_menu) {
2143 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2144 use_menu = 0;
2147 if(vf_menu)
2148 sh_video->vfilter = vf_menu;
2149 #endif
2151 #ifdef CONFIG_ASS
2152 if(ass_enabled) {
2153 int i;
2154 int insert = 1;
2155 if (opts->vf_settings)
2156 for (i = 0; opts->vf_settings[i].name; ++i)
2157 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2158 insert = 0;
2159 break;
2161 if (insert) {
2162 extern vf_info_t vf_info_ass;
2163 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2164 char* vf_arg[] = {"auto", "1", NULL};
2165 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2166 if (vf_ass)
2167 sh_video->vfilter = vf_ass;
2168 else
2169 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2172 #endif
2174 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2176 #ifdef CONFIG_ASS
2177 if (ass_enabled)
2178 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2179 #endif
2181 current_module="init_video_codec";
2183 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2184 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2185 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2187 if(!sh_video->initialized){
2188 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2189 goto err_out;
2192 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2194 if (sh_video->codec)
2195 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2197 sh_video->last_pts = MP_NOPTS_VALUE;
2198 sh_video->num_buffered_pts = 0;
2199 sh_video->next_frame_time = 0;
2201 if(auto_quality>0){
2202 // Auto quality option enabled
2203 output_quality=get_video_quality_max(sh_video);
2204 if(auto_quality>output_quality) auto_quality=output_quality;
2205 else output_quality=auto_quality;
2206 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2207 set_video_quality(sh_video,output_quality);
2210 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2212 current_module="init_vo";
2214 return 1;
2216 err_out:
2217 mpctx->sh_video = mpctx->d_video->sh = NULL;
2218 return 0;
2221 static double update_video(struct MPContext *mpctx, int *blit_frame)
2223 struct MPOpts *opts = &mpctx->opts;
2224 sh_video_t * const sh_video = mpctx->sh_video;
2225 //-------------------- Decode a frame: -----------------------
2226 double frame_time;
2227 *blit_frame = 0; // Don't blit if we hit EOF
2228 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2229 mpctx->osd); // hack for vf_expand
2230 if (!opts->correct_pts) {
2231 unsigned char* start=NULL;
2232 void *decoded_frame = NULL;
2233 int drop_frame=0;
2234 int in_size;
2236 current_module = "video_read_frame";
2237 frame_time = sh_video->next_frame_time;
2238 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2239 &start, force_fps);
2240 #ifdef CONFIG_DVDNAV
2241 /// wait, still frame or EOF
2242 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2243 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2244 if (mpctx->d_video) mpctx->d_video->eof = 0;
2245 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2246 mpctx->stream->eof = 0;
2247 } else
2248 #endif
2249 if (in_size < 0)
2250 return -1;
2251 if (in_size > max_framesize)
2252 max_framesize = in_size; // stats
2253 sh_video->timer += frame_time;
2254 if (mpctx->sh_audio)
2255 mpctx->delay -= frame_time;
2256 // video_read_frame can change fps (e.g. for ASF video)
2257 vo_fps = sh_video->fps;
2258 drop_frame = check_framedrop(mpctx, frame_time);
2259 update_subtitles(sh_video, mpctx->d_sub, 0);
2260 update_teletext(sh_video, mpctx->demuxer, 0);
2261 update_osd_msg(mpctx);
2262 current_module = "decode_video";
2263 #ifdef CONFIG_DVDNAV
2264 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size,&start,
2265 decoded_frame);
2266 /// still frame has been reached, no need to decode
2267 if (in_size > 0 && !decoded_frame)
2268 #endif
2269 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2270 sh_video->pts);
2271 #ifdef CONFIG_DVDNAV
2272 /// save back last still frame for future display
2273 mp_dvdnav_save_smpi(mpctx, in_size, start, decoded_frame);
2274 #endif
2275 current_module = "filter_video";
2276 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2277 sh_video->pts,
2278 mpctx->osd));
2280 else {
2281 int res = generate_video_frame(mpctx);
2282 if (!res)
2283 return -1;
2284 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS,
2285 &sh_video->pts);
2286 if (sh_video->pts == MP_NOPTS_VALUE) {
2287 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2288 sh_video->pts = sh_video->last_pts;
2290 if (sh_video->last_pts == MP_NOPTS_VALUE)
2291 sh_video->last_pts= sh_video->pts;
2292 else if (sh_video->last_pts >= sh_video->pts) {
2293 sh_video->last_pts = sh_video->pts;
2294 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2296 frame_time = sh_video->pts - sh_video->last_pts;
2297 sh_video->last_pts = sh_video->pts;
2298 sh_video->timer += frame_time;
2299 if(mpctx->sh_audio)
2300 mpctx->delay -= frame_time;
2301 *blit_frame = res > 0;
2303 return frame_time;
2306 static void pause_loop(struct MPContext *mpctx)
2308 mp_cmd_t* cmd;
2309 if (!quiet) {
2310 // Small hack to display the pause message on the OSD line.
2311 // The pause string is: "\n == PAUSE == \r" so we need to
2312 // take the first and the last char out
2313 if (term_osd && !mpctx->sh_video) {
2314 char msg[128] = MSGTR_Paused;
2315 int mlen = strlen(msg);
2316 msg[mlen-1] = '\0';
2317 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2318 update_osd_msg(mpctx);
2319 } else
2320 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2321 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2323 #ifdef CONFIG_GUI
2324 if (use_gui)
2325 guiGetEvent(guiCEvent, (char *)guiSetPause);
2326 #endif
2327 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2328 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2330 if (mpctx->audio_out && mpctx->sh_audio)
2331 mpctx->audio_out->pause(); // pause audio, keep data if possible
2333 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2334 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2335 if (cmd) {
2336 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2337 run_command(mpctx, cmd);
2338 mp_cmd_free(cmd);
2339 continue;
2341 if (mpctx->sh_video && mpctx->video_out)
2342 vo_check_events(mpctx->video_out);
2343 #ifdef CONFIG_GUI
2344 if (use_gui) {
2345 guiEventHandling();
2346 guiGetEvent(guiReDraw, NULL);
2347 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2348 break;
2350 #endif
2351 #ifdef CONFIG_MENU
2352 if (vf_menu)
2353 vf_menu_pause_update(vf_menu);
2354 #endif
2355 usec_sleep(20000);
2357 if (cmd && cmd->id == MP_CMD_PAUSE) {
2358 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2359 mp_cmd_free(cmd);
2361 mpctx->osd_function=OSD_PLAY;
2362 if (mpctx->audio_out && mpctx->sh_audio)
2363 mpctx->audio_out->resume(); // resume audio
2364 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2365 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2366 (void)get_relative_time(mpctx); // ignore time that passed during pause
2367 #ifdef CONFIG_GUI
2368 if (use_gui) {
2369 if (guiIntfStruct.Playing == guiSetStop)
2370 mpctx->eof = 1;
2371 else
2372 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2374 #endif
2377 static void print_version(void){
2378 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2380 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2381 GetCpuCaps(&gCpuCaps);
2382 #ifdef ARCH_X86
2383 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2384 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2385 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2386 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2387 #ifdef RUNTIME_CPUDETECT
2388 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2389 #else
2390 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2391 #ifdef HAVE_MMX
2392 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2393 #endif
2394 #ifdef HAVE_MMX2
2395 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2396 #endif
2397 #ifdef HAVE_3DNOW
2398 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2399 #endif
2400 #ifdef HAVE_3DNOWEX
2401 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2402 #endif
2403 #ifdef HAVE_SSE
2404 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2405 #endif
2406 #ifdef HAVE_SSE2
2407 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2408 #endif
2409 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2410 #endif /* RUNTIME_CPUDETECT */
2411 #endif /* ARCH_X86 */
2415 // Find the right mute status and record position for new file position
2416 static void edl_seek_reset(MPContext *mpctx)
2418 mpctx->edl_muted = 0;
2419 next_edl_record = edl_records;
2421 while (next_edl_record) {
2422 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2423 break;
2425 if (next_edl_record->action == EDL_MUTE)
2426 mpctx->edl_muted = !mpctx->edl_muted;
2427 next_edl_record = next_edl_record->next;
2429 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2430 mixer_mute(&mpctx->mixer);
2434 // Execute EDL command for the current position if one exists
2435 static void edl_update(MPContext *mpctx)
2437 if (!next_edl_record)
2438 return;
2440 if (!mpctx->sh_video) {
2441 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2442 free_edl(edl_records);
2443 next_edl_record = NULL;
2444 edl_records = NULL;
2445 return;
2448 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2449 if (next_edl_record->action == EDL_SKIP) {
2450 mpctx->osd_function = OSD_FFW;
2451 mpctx->abs_seek_pos = 0;
2452 mpctx->rel_seek_secs = next_edl_record->length_sec;
2453 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2454 "[%f], length [%f]\n", next_edl_record->start_sec,
2455 next_edl_record->stop_sec, next_edl_record->length_sec);
2456 edl_decision = 1;
2458 else if (next_edl_record->action == EDL_MUTE) {
2459 mpctx->edl_muted = !mpctx->edl_muted;
2460 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2461 mixer_mute(&mpctx->mixer);
2462 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2463 next_edl_record->start_sec );
2465 next_edl_record = next_edl_record->next;
2470 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2471 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2472 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2473 static int seek(MPContext *mpctx, double amount, int style)
2475 current_module = "seek";
2476 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2477 return -1;
2479 if (mpctx->sh_video) {
2480 current_module = "seek_video_reset";
2481 resync_video_stream(mpctx->sh_video);
2482 if (mpctx->video_out->config_ok)
2483 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2484 mpctx->sh_video->num_buffered_pts = 0;
2485 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2486 mpctx->num_buffered_frames = 0;
2487 mpctx->delay = 0;
2488 // Not all demuxers set d_video->pts during seek, so this value
2489 // (which is used by at least vobsub and edl code below) may
2490 // be completely wrong (probably 0).
2491 mpctx->sh_video->pts = mpctx->d_video->pts;
2492 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2493 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2496 if (mpctx->sh_audio) {
2497 current_module = "seek_audio_reset";
2498 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2499 mpctx->sh_audio->a_buffer_len = 0;
2500 mpctx->sh_audio->a_out_buffer_len = 0;
2503 if (vo_vobsub && mpctx->sh_video) {
2504 current_module = "seek_vobsub_reset";
2505 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2508 edl_seek_reset(mpctx);
2510 c_total = 0;
2511 max_pts_correction = 0.1;
2512 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2513 drop_frame_cnt = 0;
2515 current_module = NULL;
2516 return 0;
2520 static int read_keys(void *ctx, int fd)
2522 getch2(ctx);
2523 return mplayer_get_key(ctx, 0);
2527 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2528 * file for some tools to link against. */
2529 #ifndef DISABLE_MAIN
2530 int main(int argc,char* argv[]){
2533 char * mem_ptr;
2535 // movie info:
2537 /* Flag indicating whether MPlayer should exit without playing anything. */
2538 int opt_exit = 0;
2540 //float a_frame=0; // Audio
2542 int i;
2544 int gui_no_filename=0;
2546 struct MPContext *mpctx = &(struct MPContext){
2547 .osd_function = OSD_PLAY,
2548 .begin_skip = MP_NOPTS_VALUE,
2549 .play_tree_step = 1,
2550 .global_sub_pos = -1,
2551 .set_of_sub_pos = -1,
2552 .file_format = DEMUXER_TYPE_UNKNOWN,
2553 .last_dvb_step = 1,
2556 InitTimer();
2557 srand(GetTimerMS());
2559 mp_msg_init();
2561 #ifdef CONFIG_X11
2562 mpctx->x11_state = vo_x11_init_state();
2563 #endif
2564 struct MPOpts *opts = &mpctx->opts;
2565 set_default_mplayer_options(opts);
2566 // Create the config context and register the options
2567 mpctx->mconfig = m_config_new(opts, cfg_include);
2568 m_config_register_options(mpctx->mconfig,mplayer_opts);
2569 mp_input_register_options(mpctx->mconfig);
2571 // Preparse the command line
2572 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2574 print_version();
2575 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2576 set_path_env();
2577 #endif
2579 #ifdef CONFIG_TV
2580 stream_tv_defaults.immediate = 1;
2581 #endif
2583 if (argc > 1 && argv[1] &&
2584 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2585 use_gui = !strcmp(argv[1], "-gui");
2586 } else
2587 if ( argv[0] )
2589 char *base = strrchr(argv[0], '/');
2590 if (!base)
2591 base = strrchr(argv[0], '\\');
2592 if (!base)
2593 base = argv[0];
2594 if(strstr(base, "gmplayer"))
2595 use_gui=1;
2598 parse_cfgfiles(mpctx, mpctx->mconfig);
2600 #ifdef CONFIG_GUI
2601 if ( use_gui ) cfg_read();
2602 #endif
2604 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2605 if(mpctx->playtree == NULL)
2606 opt_exit = 1;
2607 else {
2608 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2609 if(mpctx->playtree) {
2610 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2611 if(mpctx->playtree_iter) {
2612 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2613 play_tree_iter_free(mpctx->playtree_iter);
2614 mpctx->playtree_iter = NULL;
2616 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2620 mpctx->key_fifo = mp_fifo_create(opts);
2622 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2623 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2624 if(runningmplayer && mpctx->filename && use_gui){
2625 COPYDATASTRUCT csData;
2626 char file[MAX_PATH];
2627 char *filepart = mpctx->filename;
2628 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2629 csData.dwData = 0;
2630 csData.cbData = strlen(file)*2;
2631 csData.lpData = file;
2632 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2635 #endif
2637 #if defined(__MINGW32__) || defined(__CYGWIN__)
2638 // request 1ms timer resolution
2639 timeBeginPeriod(1);
2640 if(proc_priority){
2641 int i;
2642 for(i=0; priority_presets_defs[i].name; i++){
2643 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2644 break;
2646 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
2647 priority_presets_defs[i].name);
2648 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2650 #endif
2651 #ifndef CONFIG_GUI
2652 if(use_gui){
2653 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2654 use_gui=0;
2656 #else
2657 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2658 if(use_gui && !vo_init()){
2659 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2660 use_gui=0;
2662 #endif
2663 if (use_gui && mpctx->playtree_iter){
2664 char cwd[PATH_MAX+2];
2665 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2666 play_tree_iter_free(mpctx->playtree_iter);
2667 mpctx->playtree_iter=NULL;
2669 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2671 strcat(cwd, "/");
2672 // Prefix relative paths with current working directory
2673 play_tree_add_bpf(mpctx->playtree, cwd);
2675 // Import initital playtree into GUI.
2676 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2678 #endif /* CONFIG_GUI */
2680 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2681 list_video_out();
2682 opt_exit = 1;
2685 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2686 list_audio_out();
2687 opt_exit = 1;
2690 /* Check codecs.conf. */
2691 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2692 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2693 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2694 if(!parse_codec_cfg(NULL)){
2695 exit_player_with_rc(mpctx, NULL, 0);
2697 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2700 free( mem_ptr ); // release the buffer created by get_path()
2703 #if 0
2704 if(video_codec_list){
2705 int i;
2706 video_codec=video_codec_list[0];
2707 for(i=0;video_codec_list[i];i++)
2708 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2710 #endif
2711 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2712 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2713 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2714 list_codecs(1);
2715 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2716 opt_exit = 1;
2718 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2719 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2720 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2721 list_codecs(0);
2722 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2723 opt_exit = 1;
2725 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2726 vfm_help();
2727 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2728 opt_exit = 1;
2730 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2731 afm_help();
2732 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2733 opt_exit = 1;
2735 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2736 af_help();
2737 printf("\n");
2738 opt_exit = 1;
2740 #ifdef CONFIG_X11
2741 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2742 fstype_help();
2743 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2744 opt_exit = 1;
2746 #endif
2747 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2748 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2749 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2750 demuxer_help();
2751 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2752 opt_exit = 1;
2754 if(list_properties) {
2755 property_print_help();
2756 opt_exit = 1;
2759 if(opt_exit)
2760 exit_player(mpctx, NULL);
2762 if (player_idle_mode && use_gui) {
2763 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2764 exit_player_with_rc(mpctx, NULL, 1);
2767 if(!mpctx->filename && !player_idle_mode){
2768 if(!use_gui){
2769 // no file/vcd/dvd -> show HELP:
2770 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2771 exit_player_with_rc(mpctx, NULL, 0);
2772 } else gui_no_filename=1;
2775 /* Display what configure line was used */
2776 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2778 // Many users forget to include command line in bugreports...
2779 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2780 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2781 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2782 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2785 //------ load global data first ------
2787 mpctx->osd = osd_create();
2789 // check font
2790 #ifdef CONFIG_FREETYPE
2791 init_freetype();
2792 #endif
2793 #ifdef CONFIG_FONTCONFIG
2794 if(font_fontconfig <= 0)
2796 #endif
2797 #ifdef CONFIG_BITMAP_FONT
2798 if(font_name){
2799 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2800 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2801 filename_recode(font_name));
2802 } else {
2803 // try default:
2804 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2805 free(mem_ptr); // release the buffer created by get_path()
2806 if(!vo_font)
2807 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2809 if (sub_font_name)
2810 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2811 else
2812 mpctx->osd->sub_font = vo_font;
2813 #endif
2814 #ifdef CONFIG_FONTCONFIG
2816 #endif
2818 #ifdef CONFIG_ASS
2819 ass_library = ass_init();
2820 #endif
2822 #ifdef HAVE_RTC
2823 if(!nortc)
2825 // seteuid(0); /* Can't hurt to try to get root here */
2826 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2827 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2828 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2829 else {
2830 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2832 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2833 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2834 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2835 close (rtc_fd);
2836 rtc_fd = -1;
2837 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2838 /* variable only by the root */
2839 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2840 close (rtc_fd);
2841 rtc_fd = -1;
2842 } else
2843 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2846 #ifdef CONFIG_GUI
2847 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2848 // and now ? -- Pontscho
2849 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2850 #endif
2851 if(rtc_fd<0)
2852 #endif /* HAVE_RTC */
2853 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2854 softsleep?"software":timer_name);
2856 #ifdef HAVE_TERMCAP
2857 if ( !use_gui ) load_termcap(NULL); // load key-codes
2858 #endif
2860 // ========== Init keyboard FIFO (connection to libvo) ============
2862 // Init input system
2863 current_module = "init_input";
2864 mpctx->input = mp_input_init(&opts->input, use_gui);
2865 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2866 if(slave_mode)
2867 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2868 else if(!noconsolecontrols)
2869 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2870 // Set the libstream interrupt callback
2871 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2873 #ifdef CONFIG_MENU
2874 if(use_menu) {
2875 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2876 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2877 else {
2878 menu_cfg = get_path("menu.conf");
2879 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2880 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2881 else {
2882 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2883 MPLAYER_CONFDIR "/menu.conf"))
2884 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2885 else {
2886 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2887 use_menu = 0;
2892 #endif
2894 current_module = NULL;
2896 /// Catch signals
2897 #ifndef __MINGW32__
2898 signal(SIGCHLD,child_sighandler);
2899 #endif
2901 #ifdef CONFIG_CRASH_DEBUG
2902 prog_path = argv[0];
2903 #endif
2904 //========= Catch terminate signals: ================
2905 // terminate requests:
2906 signal(SIGTERM,exit_sighandler); // kill
2907 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2909 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2911 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2912 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2913 #ifdef CONFIG_SIGHANDLER
2914 // fatal errors:
2915 signal(SIGBUS,exit_sighandler); // bus error
2916 signal(SIGSEGV,exit_sighandler); // segfault
2917 signal(SIGILL,exit_sighandler); // illegal instruction
2918 signal(SIGFPE,exit_sighandler); // floating point exc.
2919 signal(SIGABRT,exit_sighandler); // abort()
2920 #ifdef CONFIG_CRASH_DEBUG
2921 if (crash_debug)
2922 signal(SIGTRAP,exit_sighandler);
2923 #endif
2924 #endif
2926 #ifdef CONFIG_GUI
2927 if(use_gui){
2928 guiInit();
2929 guiGetEvent(guiSetContext, mpctx);
2930 mpctx->initialized_flags|=INITIALIZED_GUI;
2931 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2933 #endif
2935 // ******************* Now, let's see the per-file stuff ********************
2937 play_next_file:
2939 // init global sub numbers
2940 mpctx->global_sub_size = 0;
2941 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2943 if (mpctx->filename) {
2944 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2945 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2946 load_per_file_config (mpctx->mconfig, mpctx->filename);
2949 if (opts->video_driver_list)
2950 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2951 if (opts->audio_driver_list)
2952 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2954 // We must enable getch2 here to be able to interrupt network connection
2955 // or cache filling
2956 if(!noconsolecontrols && !slave_mode){
2957 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2958 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2959 else
2960 getch2_enable(); // prepare stdin for hotkeys...
2961 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2962 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2965 // =================== GUI idle loop (STOP state) ===========================
2966 #ifdef CONFIG_GUI
2967 if ( use_gui ) {
2968 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2969 guiGetEvent( guiSetDefaults,0 );
2970 while ( guiIntfStruct.Playing != 1 )
2972 mp_cmd_t* cmd;
2973 usec_sleep(20000);
2974 guiEventHandling();
2975 guiGetEvent( guiReDraw,NULL );
2976 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
2977 guiGetEvent(guiIEvent, (char *)cmd->id);
2978 mp_cmd_free(cmd);
2981 guiGetEvent( guiSetParameters,NULL );
2982 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2984 play_tree_t * entry = play_tree_new();
2985 play_tree_add_file( entry,guiIntfStruct.Filename );
2986 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2987 else mpctx->playtree=play_tree_new();
2988 play_tree_set_child( mpctx->playtree,entry );
2989 if(mpctx->playtree)
2991 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2992 if(mpctx->playtree_iter)
2994 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2996 play_tree_iter_free(mpctx->playtree_iter);
2997 mpctx->playtree_iter = NULL;
2999 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3004 #endif /* CONFIG_GUI */
3006 while (player_idle_mode && !mpctx->filename) {
3007 play_tree_t * entry = NULL;
3008 mp_cmd_t * cmd;
3009 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3010 if (mpctx->video_out)
3011 vo_check_events(mpctx->video_out);
3012 usec_sleep(20000);
3014 switch (cmd->id) {
3015 case MP_CMD_LOADFILE:
3016 // prepare a tree entry with the new filename
3017 entry = play_tree_new();
3018 play_tree_add_file(entry, cmd->args[0].v.s);
3019 // The entry is added to the main playtree after the switch().
3020 break;
3021 case MP_CMD_LOADLIST:
3022 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3023 break;
3024 case MP_CMD_QUIT:
3025 exit_player_with_rc(mpctx, MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3026 break;
3027 case MP_CMD_GET_PROPERTY:
3028 case MP_CMD_SET_PROPERTY:
3029 case MP_CMD_STEP_PROPERTY:
3030 run_command(mpctx, cmd);
3031 break;
3034 mp_cmd_free(cmd);
3036 if (entry) { // user entered a command that gave a valid entry
3037 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3038 play_tree_free_list(mpctx->playtree->child, 1);
3039 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3041 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3043 play_tree_set_child(mpctx->playtree, entry);
3045 /* Make iterator start at the top the of tree. */
3046 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3047 if (!mpctx->playtree_iter) continue;
3049 // find the first real item in the tree
3050 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3051 // no items!
3052 play_tree_iter_free(mpctx->playtree_iter);
3053 mpctx->playtree_iter = NULL;
3054 continue; // wait for next command
3056 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3059 //---------------------------------------------------------------------------
3061 if(mpctx->filename)
3062 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3063 filename_recode(mpctx->filename));
3065 if (edl_filename) {
3066 if (edl_records) free_edl(edl_records);
3067 next_edl_record = edl_records = edl_parse_file();
3069 if (edl_output_filename) {
3070 if (edl_fd) fclose(edl_fd);
3071 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3073 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3074 filename_recode(edl_output_filename));
3078 //==================== Open VOB-Sub ============================
3080 current_module="vobsub";
3081 if (vobsub_name){
3082 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3083 if(vo_vobsub==NULL)
3084 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3085 filename_recode(vobsub_name));
3086 } else if (sub_auto && mpctx->filename){
3087 /* try to autodetect vobsub from movie filename ::atmos */
3088 char *buf = strdup(mpctx->filename), *psub;
3089 char *pdot = strrchr(buf, '.');
3090 char *pslash = strrchr(buf, '/');
3091 #if defined(__MINGW32__) || defined(__CYGWIN__)
3092 if (!pslash) pslash = strrchr(buf, '\\');
3093 #endif
3094 if (pdot && (!pslash || pdot > pslash))
3095 *pdot = '\0';
3096 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3097 /* try from ~/.mplayer/sub */
3098 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3099 char *bname;
3100 int l;
3101 bname = strrchr(buf,'/');
3102 #if defined(__MINGW32__) || defined(__CYGWIN__)
3103 if(!bname) bname = strrchr(buf,'\\');
3104 #endif
3105 if(bname) bname++;
3106 else bname = buf;
3107 l = strlen(psub) + strlen(bname) + 1;
3108 psub = realloc(psub,l);
3109 strcat(psub,bname);
3110 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3111 free(psub);
3113 free(buf);
3115 if(vo_vobsub){
3116 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3117 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3119 // setup global sub numbering
3120 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3121 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3124 //============ Open & Sync STREAM --- fork cache2 ====================
3126 mpctx->stream=NULL;
3127 mpctx->demuxer=NULL;
3128 if (mpctx->d_audio) {
3129 //free_demuxer_stream(mpctx->d_audio);
3130 mpctx->d_audio=NULL;
3132 if (mpctx->d_video) {
3133 //free_demuxer_stream(d_video);
3134 mpctx->d_video=NULL;
3136 mpctx->sh_audio=NULL;
3137 mpctx->sh_video=NULL;
3139 current_module="open_stream";
3140 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3141 if(!mpctx->stream) { // error...
3142 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3143 goto goto_next_file;
3145 mpctx->initialized_flags|=INITIALIZED_STREAM;
3147 #ifdef CONFIG_GUI
3148 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3149 #endif
3151 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3152 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3153 "playlist support will not be used automatically.\n"
3154 "MPlayer's playlist code is unsafe and should only be used with "
3155 "trusted sources.\nPlayback will probably fail.\n\n");
3156 #if 0
3157 play_tree_t* entry;
3158 // Handle playlist
3159 current_module="handle_playlist";
3160 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3161 filename_recode(mpctx->filename));
3162 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3163 mpctx->eof=playtree_add_playlist(mpctx, entry);
3164 goto goto_next_file;
3165 #endif
3167 mpctx->stream->start_pos+=seek_to_byte;
3169 if(stream_dump_type==5){
3170 unsigned char buf[4096];
3171 int len;
3172 FILE *f;
3173 current_module="dumpstream";
3174 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3175 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3176 exit_player(mpctx, MSGTR_Exit_error);
3178 stream_reset(mpctx->stream);
3179 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3180 f=fopen(stream_dump_name,"wb");
3181 if(!f){
3182 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3183 exit_player(mpctx, MSGTR_Exit_error);
3185 if (dvd_chapter > 1) {
3186 int chapter = dvd_chapter - 1;
3187 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3189 while(!mpctx->stream->eof && !async_quit_request){
3190 len=stream_read(mpctx->stream,buf,4096);
3191 if(len>0) {
3192 if(fwrite(buf,len,1,f) != 1) {
3193 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3194 exit_player(mpctx, MSGTR_Exit_error);
3197 if(dvd_last_chapter > 0) {
3198 int chapter = -1;
3199 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3200 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3201 break;
3204 if(fclose(f)) {
3205 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3206 exit_player(mpctx, MSGTR_Exit_error);
3208 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3209 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3212 #ifdef CONFIG_DVDREAD
3213 if(mpctx->stream->type==STREAMTYPE_DVD){
3214 current_module="dvd lang->id";
3215 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3216 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3217 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3218 // setup global sub numbering
3219 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3220 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3221 current_module=NULL;
3223 #endif
3225 #ifdef CONFIG_DVDNAV
3226 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3227 current_module="dvdnav lang->id";
3228 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3229 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3230 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3231 // setup global sub numbering
3232 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3233 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3234 current_module=NULL;
3236 #endif
3238 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3239 goto_enable_cache:
3240 if(stream_cache_size>0){
3241 current_module="enable_cache";
3242 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3243 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3244 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3245 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3248 //============ Open DEMUXERS --- DETECT file type =======================
3249 current_module="demux_open";
3251 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3253 // HACK to get MOV Reference Files working
3255 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3257 unsigned char* playlist_entry;
3258 play_tree_t *list = NULL, *entry = NULL;
3260 current_module="handle_demux_playlist";
3261 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3263 char *temp, *bname;
3265 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3266 filename_recode(playlist_entry));
3268 bname=mp_basename(playlist_entry);
3269 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3270 continue;
3272 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3273 continue;
3275 entry = play_tree_new();
3277 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3279 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3280 if (temp)
3282 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3283 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3284 strcat(temp, playlist_entry);
3285 play_tree_add_file(entry,temp);
3286 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3287 free(temp);
3290 else
3291 play_tree_add_file(entry,playlist_entry);
3293 if(!list)
3294 list = entry;
3295 else
3296 play_tree_append_entry(list,entry);
3298 free_demuxer(mpctx->demuxer);
3299 mpctx->demuxer = NULL;
3301 if (list)
3303 entry = play_tree_new();
3304 play_tree_set_child(entry,list);
3305 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3306 goto goto_next_file;
3310 if(!mpctx->demuxer)
3311 goto goto_next_file;
3312 if(dvd_chapter>1) {
3313 float pts;
3314 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3315 seek(mpctx, pts, SEEK_ABSOLUTE);
3318 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3320 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3321 int i;
3322 int maxid = -1;
3323 // setup global sub numbering
3324 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3325 for (i = 0; i < MAX_S_STREAMS; i++)
3326 if (mpctx->demuxer->s_streams[i])
3327 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3328 mpctx->global_sub_size += maxid + 1;
3330 // Make opts->sub_id always selectable if set.
3331 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3332 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3334 #ifdef CONFIG_ASS
3335 if (ass_enabled && ass_library) {
3336 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3337 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3338 if (extract_embedded_fonts &&
3339 att->name && att->type && att->data && att->data_size &&
3340 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3341 strcmp(att->type, "application/x-font") == 0))
3342 ass_add_font(ass_library, att->name, att->data, att->data_size);
3345 #endif
3347 current_module="demux_open2";
3349 //file_format=demuxer->file_format;
3351 mpctx->d_audio=mpctx->demuxer->audio;
3352 mpctx->d_video=mpctx->demuxer->video;
3353 mpctx->d_sub=mpctx->demuxer->sub;
3355 // select audio stream
3356 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3358 // DUMP STREAMS:
3359 if((stream_dump_type)&&(stream_dump_type<4)){
3360 FILE *f;
3361 demux_stream_t *ds=NULL;
3362 current_module="dump";
3363 // select stream to dump
3364 switch(stream_dump_type){
3365 case 1: ds=mpctx->d_audio;break;
3366 case 2: ds=mpctx->d_video;break;
3367 case 3: ds=mpctx->d_sub;break;
3369 if(!ds){
3370 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3371 exit_player(mpctx, MSGTR_Exit_error);
3373 // disable other streams:
3374 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3375 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3376 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3377 // let's dump it!
3378 f=fopen(stream_dump_name,"wb");
3379 if(!f){
3380 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3381 exit_player(mpctx, MSGTR_Exit_error);
3383 while(!ds->eof){
3384 unsigned char* start;
3385 int in_size=ds_get_packet(ds,&start);
3386 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3387 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3388 if(in_size>0) fwrite(start,in_size,1,f);
3389 if(dvd_last_chapter>0) {
3390 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3391 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3392 break;
3395 fclose(f);
3396 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3397 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
3400 mpctx->sh_audio=mpctx->d_audio->sh;
3401 mpctx->sh_video=mpctx->d_video->sh;
3403 if(mpctx->sh_video){
3405 current_module="video_read_properties";
3406 if(!video_read_properties(mpctx->sh_video)) {
3407 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3408 mpctx->sh_video=mpctx->d_video->sh=NULL;
3409 } else {
3410 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3411 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3412 mpctx->sh_video->fps,mpctx->sh_video->frametime
3415 /* need to set fps here for output encoders to pick it up in their init */
3416 if(force_fps){
3417 mpctx->sh_video->fps=force_fps;
3418 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3420 vo_fps = mpctx->sh_video->fps;
3422 if(!mpctx->sh_video->fps && !force_fps){
3423 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3424 mpctx->sh_video=mpctx->d_video->sh=NULL;
3430 if(!mpctx->sh_video && !mpctx->sh_audio){
3431 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3432 #ifdef CONFIG_DVBIN
3433 if(mpctx->stream->type == STREAMTYPE_DVB)
3435 int dir;
3436 int v = mpctx->last_dvb_step;
3437 if(v > 0)
3438 dir = DVB_CHANNEL_HIGHER;
3439 else
3440 dir = DVB_CHANNEL_LOWER;
3442 if(dvb_step_channel(mpctx->stream, dir)) {
3443 mpctx->stop_play = PT_NEXT_ENTRY;
3444 mpctx->dvbin_reopen = 1;
3447 #endif
3448 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3451 /* display clip info */
3452 demux_info_print(mpctx->demuxer);
3454 //================== Read SUBTITLES (DVD & TEXT) ==========================
3455 if(vo_spudec==NULL && mpctx->sh_video &&
3456 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3457 init_vo_spudec(mpctx);
3460 if(mpctx->sh_video) {
3461 // after reading video params we should load subtitles because
3462 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3463 // check .sub
3464 current_module="read_subtitles_file";
3465 if(sub_name){
3466 for (i = 0; sub_name[i] != NULL; ++i)
3467 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3469 if(sub_auto) { // auto load sub file ...
3470 char *psub = get_path( "sub/" );
3471 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3472 int i = 0;
3473 free(psub); // release the buffer created by get_path() above
3474 while (tmp[i]) {
3475 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3476 free(tmp[i++]);
3478 free(tmp);
3480 if (mpctx->set_of_sub_size > 0) {
3481 // setup global sub numbering
3482 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3483 mpctx->global_sub_size += mpctx->set_of_sub_size;
3487 if (mpctx->global_sub_size) {
3488 // find the best sub to use
3489 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3490 mpctx->global_sub_pos = -1; // no subs by default
3491 if (vobsub_index_id >= 0) {
3492 // if user asks for a vobsub id, use that first.
3493 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3494 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3495 // if user asks for a dvd sub id, use that next.
3496 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3497 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3498 // if there are text subs to use, use those. (autosubs come last here)
3499 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3500 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3501 // finally select subs by language and container hints
3502 if (opts->sub_id < 0 && dvdsub_lang)
3503 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3504 if (opts->sub_id < 0)
3505 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3506 if (opts->sub_id >= 0)
3507 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3509 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3510 mpctx->global_sub_pos--;
3511 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3512 if(subdata)
3513 switch (stream_dump_type) {
3514 case 3: list_sub_file(subdata); break;
3515 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3516 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3517 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3518 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3519 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3523 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3524 filename_recode(mpctx->filename));
3525 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3526 if (mpctx->sh_video) {
3527 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3528 if (mpctx->sh_video->format >= 0x20202020)
3529 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3530 else
3531 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3532 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3533 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3534 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3535 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3538 if (mpctx->sh_audio) {
3539 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3540 if (mpctx->sh_audio->format >= 0x20202020)
3541 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3542 else
3543 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3544 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3545 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3546 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3548 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3549 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3550 if (mpctx->demuxer) {
3551 if (mpctx->demuxer->num_chapters == 0)
3552 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3553 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3556 if(!mpctx->sh_video) goto main; // audio-only
3558 if(!reinit_video_chain(mpctx)) {
3559 if(!mpctx->sh_video){
3560 if(!mpctx->sh_audio) goto goto_next_file;
3561 goto main; // exit_player(MSGTR_Exit_error);
3565 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3566 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3568 #ifdef CONFIG_FREETYPE
3569 force_load_font = 1;
3570 #endif
3572 //================== MAIN: ==========================
3573 main:
3574 current_module="main";
3576 if(playing_msg) {
3577 char* msg = property_expand_string(mpctx, playing_msg);
3578 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3579 free(msg);
3583 // Disable the term OSD in verbose mode
3584 if(verbose) term_osd = 0;
3587 //int frame_corr_num=0; //
3588 //float v_frame=0; // Video
3589 float time_frame=0; // Timer
3590 //float num_frames=0; // number of frames played
3592 int frame_time_remaining=0; // flag
3593 int blit_frame=0;
3594 mpctx->num_buffered_frames=0;
3596 // Make sure old OSD does not stay around,
3597 // e.g. with -fixed-vo and same-resolution files
3598 clear_osd_msgs();
3599 update_osd_msg(mpctx);
3601 //================ SETUP AUDIO ==========================
3603 if(mpctx->sh_audio){
3604 reinit_audio_chain(mpctx);
3605 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3606 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3609 current_module="av_init";
3611 if(mpctx->sh_video){
3612 mpctx->sh_video->timer=0;
3613 if (! ignore_start)
3614 audio_delay += mpctx->sh_video->stream_delay;
3616 if(mpctx->sh_audio){
3617 if (start_volume >= 0)
3618 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3619 if (! ignore_start)
3620 audio_delay -= mpctx->sh_audio->stream_delay;
3621 mpctx->delay=-audio_delay;
3624 if(!mpctx->sh_audio){
3625 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3626 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3627 ds_free_packs(mpctx->d_audio); // free buffered chunks
3628 //mpctx->d_audio->id=-2; // do not read audio chunks
3629 //uninit_player(mpctx, INITIALIZED_AO); // close device
3631 if(!mpctx->sh_video){
3632 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3633 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3634 ds_free_packs(mpctx->d_video);
3635 mpctx->d_video->id=-2;
3636 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3639 if (!mpctx->sh_video && !mpctx->sh_audio)
3640 goto goto_next_file;
3642 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3643 if(force_fps && mpctx->sh_video){
3644 vo_fps = mpctx->sh_video->fps=force_fps;
3645 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3646 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3649 #ifdef CONFIG_GUI
3650 if ( use_gui ) {
3651 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3652 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3653 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3654 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3655 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3657 #endif
3659 mp_input_set_section(mpctx->input, NULL);
3660 //TODO: add desired (stream-based) sections here
3661 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3662 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3664 //==================== START PLAYING =======================
3666 if(opts->loop_times>1) opts->loop_times--; else
3667 if(opts->loop_times==1) opts->loop_times = -1;
3669 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3671 total_time_usage_start=GetTimer();
3672 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3673 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3674 play_n_frames=play_n_frames_mf;
3676 if(play_n_frames==0){
3677 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3680 if (seek_to_sec) {
3681 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3682 end_at.pos += seek_to_sec;
3685 if (end_at.type == END_AT_SIZE) {
3686 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3687 end_at.type = END_AT_NONE;
3690 #ifdef CONFIG_DVDNAV
3691 mp_dvdnav_context_free(mpctx);
3692 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3693 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3694 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3696 #endif
3698 get_relative_time(mpctx); // reset current delta
3700 while(!mpctx->stop_play){
3701 float aq_sleep_time=0;
3703 if(dvd_last_chapter>0) {
3704 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3705 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3706 goto goto_next_file;
3709 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3710 mpctx->sh_audio = mpctx->d_audio->sh;
3711 mpctx->sh_audio->ds = mpctx->d_audio;
3712 reinit_audio_chain(mpctx);
3715 /*========================== PLAY AUDIO ============================*/
3717 if (mpctx->sh_audio)
3718 if (!fill_audio_out_buffers(mpctx))
3719 // at eof, all audio at least written to ao
3720 if (!mpctx->sh_video)
3721 mpctx->stop_play = AT_END_OF_FILE;
3724 if(!mpctx->sh_video) {
3725 // handle audio-only case:
3726 double a_pos=0;
3727 // sh_audio can be NULL due to video stream switching
3728 // TODO: handle this better
3729 if(!quiet || end_at.type == END_AT_TIME && mpctx->sh_audio)
3730 a_pos = playing_audio_pts(mpctx);
3732 if(!quiet)
3733 print_status(mpctx, a_pos, 0, 0);
3735 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3736 mpctx->stop_play = PT_NEXT_ENTRY;
3737 update_osd_msg(mpctx);
3739 } else {
3741 /*========================== PLAY VIDEO ============================*/
3743 vo_pts=mpctx->sh_video->timer*90000.0;
3744 vo_fps=mpctx->sh_video->fps;
3746 if (!mpctx->num_buffered_frames) {
3747 double frame_time = update_video(mpctx, &blit_frame);
3748 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3749 if (mpctx->sh_video->vf_initialized < 0) {
3750 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3751 mpctx->stop_play = PT_NEXT_ENTRY;
3752 goto goto_next_file;
3754 if (frame_time < 0)
3755 mpctx->stop_play = AT_END_OF_FILE;
3756 else {
3757 // might return with !eof && !blit_frame if !correct_pts
3758 mpctx->num_buffered_frames += blit_frame;
3759 time_frame += frame_time / opts->playback_speed; // for nosound
3763 // ==========================================================================
3765 // current_module="draw_osd";
3766 // if(vo_config_count) mpctx->video_out->draw_osd();
3768 #ifdef CONFIG_GUI
3769 if(use_gui) guiEventHandling();
3770 #endif
3772 current_module="vo_check_events";
3773 vo_check_events(mpctx->video_out);
3775 #ifdef CONFIG_X11
3776 if (stop_xscreensaver) {
3777 current_module = "stop_xscreensaver";
3778 xscreensaver_heartbeat(mpctx->x11_state);
3780 #endif
3781 if (heartbeat_cmd) {
3782 static unsigned last_heartbeat;
3783 unsigned now = GetTimerMS();
3784 if (now - last_heartbeat > 30000) {
3785 last_heartbeat = now;
3786 system(heartbeat_cmd);
3790 frame_time_remaining = sleep_until_update(mpctx, &time_frame, &aq_sleep_time);
3792 //====================== FLIP PAGE (VIDEO BLT): =========================
3794 current_module="flip_page";
3795 if (!frame_time_remaining && blit_frame) {
3796 unsigned int t2=GetTimer();
3798 vo_flip_page(mpctx->video_out);
3799 mpctx->num_buffered_frames--;
3801 vout_time_usage += (GetTimer() - t2) * 0.000001;
3803 //====================== A-V TIMESTAMP CORRECTION: =========================
3805 adjust_sync_and_print_status(mpctx, frame_time_remaining, time_frame);
3807 //============================ Auto QUALITY ============================
3809 /*Output quality adjustments:*/
3810 if(auto_quality>0){
3811 current_module="autoq";
3812 // float total=0.000001f * (GetTimer()-aq_total_time);
3813 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3814 if(output_quality<auto_quality && aq_sleep_time>0)
3815 ++output_quality;
3816 else
3817 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3818 if(output_quality>1 && aq_sleep_time<0)
3819 --output_quality;
3820 else
3821 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3822 output_quality=0;
3823 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3824 set_video_quality(mpctx->sh_video,output_quality);
3827 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3828 --play_n_frames;
3829 if (play_n_frames <= 0) mpctx->stop_play = PT_NEXT_ENTRY;
3833 // FIXME: add size based support for -endpos
3834 if (end_at.type == END_AT_TIME &&
3835 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3836 mpctx->stop_play = PT_NEXT_ENTRY;
3838 } // end if(mpctx->sh_video)
3840 #ifdef CONFIG_DVDNAV
3841 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3842 nav_highlight_t hl;
3843 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3844 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3845 vo_osd_changed (OSDTYPE_DVDNAV);
3847 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3848 double ar = -1.0;
3849 if (stream_control (mpctx->demuxer->stream,
3850 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3851 != STREAM_UNSUPPORTED)
3852 mpctx->sh_video->stream_aspect = ar;
3855 #endif
3857 //============================ Handle PAUSE ===============================
3859 current_module="pause";
3861 if (mpctx->osd_function == OSD_PAUSE) {
3862 mpctx->was_paused = 1;
3863 pause_loop(mpctx);
3866 // handle -sstep
3867 if(step_sec>0) {
3868 mpctx->osd_function=OSD_FFW;
3869 mpctx->rel_seek_secs+=step_sec;
3872 edl_update(mpctx);
3874 //================= Keyboard events, SEEKing ====================
3876 current_module="key_events";
3879 mp_cmd_t* cmd;
3880 int brk_cmd = 0;
3881 while( !brk_cmd && (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3882 brk_cmd = run_command(mpctx, cmd);
3883 mp_cmd_free(cmd);
3884 if (brk_cmd == 2)
3885 goto goto_enable_cache;
3888 mpctx->was_paused = 0;
3890 /* Looping. */
3891 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3892 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3894 if(opts->loop_times>1) opts->loop_times--; else
3895 if(opts->loop_times==1) opts->loop_times=-1;
3896 play_n_frames=play_n_frames_mf;
3897 mpctx->stop_play=0;
3898 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3899 loop_seek = 1;
3902 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3903 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3904 // Set OSD:
3905 if(!loop_seek){
3906 if( !edl_decision )
3907 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3911 mpctx->rel_seek_secs=0;
3912 mpctx->abs_seek_pos=0;
3913 loop_seek=0;
3914 edl_decision = 0;
3917 #ifdef CONFIG_GUI
3918 if(use_gui){
3919 guiEventHandling();
3920 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3921 // get pos from frame number / total frames
3922 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3923 } else {
3924 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3926 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3927 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3928 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3929 guiGetEvent( guiReDraw,NULL );
3930 guiGetEvent( guiSetVolume,NULL );
3931 if(guiIntfStruct.Playing==0) break; // STOP
3932 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3933 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3934 #ifdef CONFIG_DVDREAD
3935 if ( mpctx->stream->type == STREAMTYPE_DVD )
3937 dvd_priv_t * dvdp = mpctx->stream->priv;
3938 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3940 #endif
3942 #endif /* CONFIG_GUI */
3944 } // while(!mpctx->stop_play)
3946 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
3948 #ifdef CONFIG_DVBIN
3949 if(mpctx->dvbin_reopen)
3951 mpctx->stop_play = 0;
3952 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3953 cache_uninit(mpctx->stream);
3954 mpctx->dvbin_reopen = 0;
3955 goto goto_enable_cache;
3957 #endif
3960 goto_next_file: // don't jump here after ao/vo/getch initialization!
3962 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3964 if(benchmark){
3965 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3966 double total_time_usage;
3967 total_time_usage_start=GetTimer()-total_time_usage_start;
3968 total_time_usage = (float)total_time_usage_start*0.000001;
3969 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3970 video_time_usage,vout_time_usage,audio_time_usage,
3971 total_time_usage-tot,total_time_usage);
3972 if(total_time_usage>0.0)
3973 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3974 100.0*video_time_usage/total_time_usage,
3975 100.0*vout_time_usage/total_time_usage,
3976 100.0*audio_time_usage/total_time_usage,
3977 100.0*(total_time_usage-tot)/total_time_usage,
3978 100.0);
3979 if(total_frame_cnt && frame_dropping)
3980 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3981 total_frame_cnt-drop_frame_cnt,
3982 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3983 drop_frame_cnt,
3984 100*drop_frame_cnt/total_frame_cnt,
3985 total_frame_cnt,
3986 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3989 // time to uninit all, except global stuff:
3990 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
3992 if(mpctx->set_of_sub_size > 0) {
3993 current_module="sub_free";
3994 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
3995 sub_free(mpctx->set_of_subtitles[i]);
3996 #ifdef CONFIG_ASS
3997 if(mpctx->set_of_ass_tracks[i])
3998 ass_free_track( mpctx->set_of_ass_tracks[i] );
3999 #endif
4001 mpctx->set_of_sub_size = 0;
4003 vo_sub_last = vo_sub=NULL;
4004 subdata=NULL;
4005 #ifdef CONFIG_ASS
4006 ass_track = NULL;
4007 if(ass_library)
4008 ass_clear_fonts(ass_library);
4009 #endif
4011 if (!mpctx->stop_play) // In case some goto jumped here...
4012 mpctx->stop_play = PT_NEXT_ENTRY;
4014 int playtree_direction = 1;
4016 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4017 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4018 play_tree_iter_free(mpctx->playtree_iter);
4019 mpctx->playtree_iter = NULL;
4021 mpctx->play_tree_step = 1;
4022 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4023 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4024 if(mpctx->playtree_iter) {
4025 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4026 play_tree_iter_free(mpctx->playtree_iter);
4027 mpctx->playtree_iter = NULL;
4030 } else if (mpctx->stop_play == PT_STOP) {
4031 play_tree_iter_free(mpctx->playtree_iter);
4032 mpctx->playtree_iter = NULL;
4033 } else { // NEXT PREV SRC
4034 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4037 while(mpctx->playtree_iter != NULL) {
4038 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4039 if(mpctx->filename == NULL) {
4040 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4041 play_tree_iter_free(mpctx->playtree_iter);
4042 mpctx->playtree_iter = NULL;
4044 } else
4045 break;
4048 #ifdef CONFIG_GUI
4049 if(use_gui && !mpctx->playtree_iter) {
4050 #ifdef CONFIG_DVDREAD
4051 if(!guiIntfStruct.DiskChanged)
4052 #endif
4053 mplEnd();
4055 #endif
4057 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4058 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4059 mpctx->stop_play = 0;
4060 goto play_next_file;
4064 exit_player_with_rc(mpctx, MSGTR_Exit_eof, 0);
4066 return 1;
4068 #endif /* DISABLE_MAIN */