Changed default options
[mplayer/kovensky.git] / mplayer.c
blobb70dae666929985e690bcc20d81ff0613f940bc7
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include "config.h"
9 #include "talloc.h"
11 #if _WIN32
12 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
13 #define _WIN32_WINNT 0x0500 /* enable SetThreadExecutionState for disabling screensaver. Breaks binary compatibility with pre-win2000. */
14 #include <windows.h>
15 #endif
16 #include <string.h>
17 #include <unistd.h>
19 // #include <sys/mman.h>
20 #include <sys/types.h>
21 #ifndef __MINGW32__
22 #include <sys/ioctl.h>
23 #include <sys/wait.h>
24 #else
25 #define SIGHUP 1 /* hangup */
26 #define SIGQUIT 3 /* quit */
27 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
28 #define SIGBUS 10 /* bus error */
29 #define SIGPIPE 13 /* broken pipe */
30 #endif
32 #include <sys/time.h>
33 #include <sys/stat.h>
35 #include <signal.h>
36 #include <time.h>
37 #include <fcntl.h>
38 #include <limits.h>
40 #include <errno.h>
42 #include "mp_msg.h"
43 #include "av_log.h"
45 #define HELP_MP_DEFINE_STATIC
46 #include "help_mp.h"
48 #include "m_option.h"
49 #include "m_config.h"
50 #include "mplayer.h"
51 #include "access_mpcontext.h"
52 #include "m_property.h"
54 #include "cfg-mplayer-def.h"
56 #include "ffmpeg_files/intreadwrite.h"
57 #include "libavutil/avstring.h"
59 #include "subreader.h"
61 #include "mp_osd.h"
62 #include "libvo/video_out.h"
64 #include "libvo/font_load.h"
65 #include "libvo/sub.h"
67 #ifdef CONFIG_X11
68 #include "libvo/x11_common.h"
69 #endif
71 #include "libao2/audio_out.h"
73 #include "codec-cfg.h"
75 #include "edl.h"
77 #include "spudec.h"
78 #include "vobsub.h"
80 #include "osdep/getch2.h"
81 #include "osdep/timer.h"
82 #include "osdep/findfiles.h"
84 #include "input/input.h"
86 const int under_mencoder = 0;
87 int slave_mode=0;
88 int player_idle_mode=0;
89 int quiet=0;
90 int enable_mouse_movements=1;
91 float start_volume = -1;
93 #include "osdep/priority.h"
95 int stop_screensaver=1;
96 char *heartbeat_cmd;
98 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
100 #ifdef HAVE_RTC
101 #ifdef __linux__
102 #include <linux/rtc.h>
103 #else
104 #include <rtc.h>
105 #define RTC_IRQP_SET RTCIO_IRQP_SET
106 #define RTC_PIE_ON RTCIO_PIE_ON
107 #endif /* __linux__ */
108 #endif /* HAVE_RTC */
110 #include "stream/tv.h"
111 #include "stream/stream_radio.h"
112 #ifdef CONFIG_DVBIN
113 #include "stream/dvbin.h"
114 #endif
115 #include "stream/cache2.h"
117 //**************************************************************************//
118 // Playtree
119 //**************************************************************************//
120 #include "playtree.h"
121 #include "playtreeparser.h"
123 //**************************************************************************//
124 // Config
125 //**************************************************************************//
126 #include "parser-cfg.h"
127 #include "parser-mpcmd.h"
129 //**************************************************************************//
130 // Config file
131 //**************************************************************************//
133 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
135 #include "get_path.h"
137 //**************************************************************************//
138 //**************************************************************************//
139 // Input media streaming & demultiplexer:
140 //**************************************************************************//
142 static int max_framesize=0;
144 #include "stream/stream.h"
145 #include "libmpdemux/demuxer.h"
146 #include "libmpdemux/stheader.h"
148 #ifdef CONFIG_DVDREAD
149 #include "stream/stream_dvd.h"
150 #endif
151 #include "stream/stream_dvdnav.h"
153 #include "libmpcodecs/dec_audio.h"
154 #include "libmpcodecs/dec_video.h"
155 #include "libmpcodecs/mp_image.h"
156 #include "libmpcodecs/vf.h"
157 #include "libmpcodecs/vd.h"
159 #include "mixer.h"
161 #include "mp_core.h"
162 #include "options.h"
163 #include "defaultopts.h"
165 #define Exit_SIGILL_RTCpuSel _(\
166 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
167 " It may be a bug in our new runtime CPU-detection code...\n"\
168 " Please read DOCS/HTML/en/bugreports.html.\n")
170 #define Exit_SIGILL _(\
171 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
172 " It usually happens when you run it on a CPU different than the one it was\n"\
173 " compiled/optimized for.\n"\
174 " Verify this!\n")
176 #define Exit_SIGSEGV_SIGFPE _(\
177 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
178 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
179 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
181 #define Exit_SIGCRASH _(\
182 "- MPlayer crashed. This shouldn't happen.\n"\
183 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
184 " gcc version. If you think it's MPlayer's fault, please read\n"\
185 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
186 " won't help unless you provide this information when reporting a possible bug.\n")
188 #define SystemTooSlow _("\n\n"\
189 " ************************************************\n"\
190 " **** Your system is too SLOW to play this! ****\n"\
191 " ************************************************\n\n"\
192 "Possible reasons, problems, workarounds:\n"\
193 "- Most common: broken/buggy _audio_ driver\n"\
194 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
195 " - Experiment with different values for -autosync, 30 is a good start.\n"\
196 "- Slow video output\n"\
197 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
198 "- Slow CPU\n"\
199 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
200 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
201 "- Broken file\n"\
202 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
203 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
204 " - Try -cache 8192.\n"\
205 "- Are you using -cache to play a non-interleaved AVI file?\n"\
206 " - Try -nocache.\n"\
207 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
208 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
211 //**************************************************************************//
212 //**************************************************************************//
214 // Common FIFO functions, and keyboard/event FIFO code
215 #include "mp_fifo.h"
216 int noconsolecontrols=0;
217 //**************************************************************************//
219 // benchmark:
220 double video_time_usage=0;
221 double vout_time_usage=0;
222 static double audio_time_usage=0;
223 static int total_time_usage_start=0;
224 static int total_frame_cnt=0;
225 static int drop_frame_cnt=0; // total number of dropped frames
226 int benchmark=0;
228 // options:
229 int auto_quality=0;
230 static int output_quality=0;
232 static int list_properties = 0;
234 int term_osd = 1;
235 static char* term_osd_esc = "\x1b[A\r\x1b[K";
236 static char* playing_msg = NULL;
237 // seek:
238 static double seek_to_sec;
239 static off_t seek_to_byte=0;
240 static off_t step_sec=0;
242 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
244 // A/V sync:
245 int autosync=0; // 30 might be a good default value.
247 // codecs:
248 char **audio_codec_list=NULL; // override audio codec
249 char **video_codec_list=NULL; // override video codec
250 char **audio_fm_list=NULL; // override audio codec family
251 char **video_fm_list=NULL; // override video codec family
253 // demuxer:
254 extern char *demuxer_name; // override demuxer
255 extern char *audio_demuxer_name; // override audio demuxer
256 extern char *sub_demuxer_name; // override sub demuxer
258 // this dvdsub_id was selected via slang
259 // use this to allow dvdnav to follow -slang across stream resets,
260 // in particular the subtitle ID for a language changes
261 int dvdsub_lang_id;
262 int vobsub_id=-1;
263 char* audio_lang=NULL;
264 char* dvdsub_lang=NULL;
265 static char* spudec_ifo=NULL;
266 int forced_subs_only=0;
267 int file_filter=1;
269 // cache2:
270 int stream_cache_size=-1;
271 #ifdef CONFIG_STREAM_CACHE
272 extern int cache_fill_status;
274 float stream_cache_min_percent=20.0;
275 float stream_cache_seek_min_percent=50.0;
276 #else
277 #define cache_fill_status 0
278 #endif
280 // dump:
281 static char *stream_dump_name="stream.dump";
282 int stream_dump_type=0;
284 // A-V sync:
285 static float default_max_pts_correction=-1;//0.01f;
286 float audio_delay=0;
287 static int ignore_start=0;
289 static int softsleep=0;
291 double force_fps=0;
292 static int force_srate=0;
293 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
294 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
295 static int play_n_frames=-1;
296 static int play_n_frames_mf=-1;
298 // sub:
299 char *font_name=NULL;
300 char *sub_font_name=NULL;
301 extern int font_fontconfig;
302 float font_factor=0.75;
303 char **sub_name=NULL;
304 float sub_delay=0;
305 float sub_fps=0;
306 int sub_auto = 1;
307 char *vobsub_name=NULL;
308 /*DSP!!char *dsp=NULL;*/
309 int subcc_enabled=0;
310 int suboverlap_enabled = 1;
312 #include "ass_mp.h"
314 char* current_module=NULL; // for debugging
317 // ---
319 #ifdef CONFIG_MENU
320 #include "m_struct.h"
321 #include "libmenu/menu.h"
322 void vf_menu_pause_update(struct vf_instance* vf);
323 extern vf_info_t vf_info_menu;
324 static const vf_info_t* const libmenu_vfs[] = {
325 &vf_info_menu,
326 NULL
328 static vf_instance_t* vf_menu = NULL;
329 int use_menu = 0;
330 static char* menu_cfg = NULL;
331 static char* menu_root = "main";
332 #endif
335 #ifdef HAVE_RTC
336 static int nortc = 1;
337 static char* rtc_device;
338 #endif
340 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
341 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
342 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
343 int use_filedir_conf;
344 int use_filename_title;
346 #include "mpcommon.h"
347 #include "command.h"
349 #include "metadata.h"
351 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
353 const void *mpctx_get_video_out(MPContext *mpctx)
355 return mpctx->video_out;
358 const void *mpctx_get_audio_out(MPContext *mpctx)
360 return mpctx->audio_out;
363 void *mpctx_get_demuxer(MPContext *mpctx)
365 return mpctx->demuxer;
368 void *mpctx_get_playtree_iter(MPContext *mpctx)
370 return mpctx->playtree_iter;
373 void *mpctx_get_mixer(MPContext *mpctx)
375 return &mpctx->mixer;
378 int mpctx_get_global_sub_size(MPContext *mpctx)
380 return mpctx->global_sub_size;
383 int mpctx_get_osd_function(MPContext *mpctx)
385 return mpctx->osd_function;
388 static float get_relative_time(struct MPContext *mpctx)
390 unsigned int new_time = GetTimer();
391 unsigned int delta = new_time - mpctx->last_time;
392 mpctx->last_time = new_time;
393 return delta * 0.000001;
396 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
397 switch (type)
399 /* check for valid video stream */
400 case META_VIDEO_CODEC:
401 case META_VIDEO_BITRATE:
402 case META_VIDEO_RESOLUTION:
404 if (!mpctx->sh_video)
405 return 0;
406 break;
409 /* check for valid audio stream */
410 case META_AUDIO_CODEC:
411 case META_AUDIO_BITRATE:
412 case META_AUDIO_SAMPLES:
414 if (!mpctx->sh_audio)
415 return 0;
416 break;
419 /* check for valid demuxer */
420 case META_INFO_TITLE:
421 case META_INFO_ARTIST:
422 case META_INFO_ALBUM:
423 case META_INFO_YEAR:
424 case META_INFO_COMMENT:
425 case META_INFO_TRACK:
426 case META_INFO_GENRE:
428 if (!mpctx->demuxer)
429 return 0;
430 break;
433 default:
434 break;
437 return 1;
440 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
441 char **info = mpctx->demuxer->info;
442 int n;
444 if (!info || !tag)
445 return NULL;
447 for (n = 0; info[2*n] != NULL ; n++)
448 if (!strcasecmp (info[2*n], tag))
449 break;
451 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
454 char *get_metadata(struct MPContext *mpctx, metadata_t type)
456 char *meta = NULL;
457 sh_audio_t * const sh_audio = mpctx->sh_audio;
458 sh_video_t * const sh_video = mpctx->sh_video;
460 if (!is_valid_metadata_type(mpctx, type))
461 return NULL;
463 switch (type)
465 case META_NAME:
467 return strdup (mp_basename2 (mpctx->filename));
470 case META_VIDEO_CODEC:
472 if (sh_video->format == 0x10000001)
473 meta = strdup ("mpeg1");
474 else if (sh_video->format == 0x10000002)
475 meta = strdup ("mpeg2");
476 else if (sh_video->format == 0x10000004)
477 meta = strdup ("mpeg4");
478 else if (sh_video->format == 0x10000005)
479 meta = strdup ("h264");
480 else if (sh_video->format >= 0x20202020)
482 meta = malloc (8);
483 sprintf (meta, "%.4s", (char *) &sh_video->format);
485 else
487 meta = malloc (8);
488 sprintf (meta, "0x%08X", sh_video->format);
490 return meta;
493 case META_VIDEO_BITRATE:
495 meta = malloc (16);
496 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
497 return meta;
500 case META_VIDEO_RESOLUTION:
502 meta = malloc (16);
503 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
504 return meta;
507 case META_AUDIO_CODEC:
509 if (sh_audio->codec && sh_audio->codec->name)
510 meta = strdup (sh_audio->codec->name);
511 return meta;
514 case META_AUDIO_BITRATE:
516 meta = malloc (16);
517 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
518 return meta;
521 case META_AUDIO_SAMPLES:
523 meta = malloc (16);
524 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
525 return meta;
528 /* check for valid demuxer */
529 case META_INFO_TITLE:
530 return get_demuxer_info(mpctx, "Title");
532 case META_INFO_ARTIST:
533 return get_demuxer_info(mpctx, "Artist");
535 case META_INFO_ALBUM:
536 return get_demuxer_info(mpctx, "Album");
538 case META_INFO_YEAR:
539 return get_demuxer_info(mpctx, "Year");
541 case META_INFO_COMMENT:
542 return get_demuxer_info(mpctx, "Comment");
544 case META_INFO_TRACK:
545 return get_demuxer_info(mpctx, "Track");
547 case META_INFO_GENRE:
548 return get_demuxer_info(mpctx, "Genre");
550 default:
551 break;
554 return meta;
557 /// step size of mixer changes
558 int volstep = 3;
560 #ifdef CONFIG_DVDNAV
561 static void mp_dvdnav_context_free(MPContext *ctx){
562 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
563 ctx->nav_smpi = NULL;
564 if (ctx->nav_buffer) free(ctx->nav_buffer);
565 ctx->nav_buffer = NULL;
566 ctx->nav_start = NULL;
567 ctx->nav_in_size = 0;
569 #endif
571 void uninit_player(struct MPContext *mpctx, unsigned int mask){
572 mask &= mpctx->initialized_flags;
574 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
576 if(mask&INITIALIZED_ACODEC){
577 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
578 current_module="uninit_acodec";
579 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
580 mpctx->sh_audio=NULL;
581 mpctx->mixer.afilter = NULL;
584 if(mask&INITIALIZED_VCODEC){
585 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
586 current_module="uninit_vcodec";
587 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
588 mpctx->sh_video=NULL;
589 #ifdef CONFIG_MENU
590 vf_menu=NULL;
591 #endif
594 if(mask&INITIALIZED_DEMUXER){
595 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
596 current_module="free_demuxer";
597 if (mpctx->num_sources) {
598 mpctx->demuxer = mpctx->sources[0].demuxer;
599 for (int i = 1; i < mpctx->num_sources; i++) {
600 free_stream(mpctx->sources[i].stream);
601 free_demuxer(mpctx->sources[i].demuxer);
604 talloc_free(mpctx->sources);
605 mpctx->sources = NULL;
606 mpctx->num_sources = 0;
607 talloc_free(mpctx->timeline);
608 mpctx->timeline = NULL;
609 mpctx->num_timeline_parts = 0;
610 talloc_free(mpctx->chapters);
611 mpctx->chapters = NULL;
612 mpctx->num_chapters = 0;
613 mpctx->video_offset = 0;
614 if(mpctx->demuxer){
615 mpctx->stream=mpctx->demuxer->stream;
616 free_demuxer(mpctx->demuxer);
618 mpctx->demuxer=NULL;
621 // kill the cache process:
622 if(mask&INITIALIZED_STREAM){
623 mpctx->initialized_flags&=~INITIALIZED_STREAM;
624 current_module="uninit_stream";
625 if(mpctx->stream) free_stream(mpctx->stream);
626 mpctx->stream=NULL;
629 if(mask&INITIALIZED_VO){
630 mpctx->initialized_flags&=~INITIALIZED_VO;
631 current_module="uninit_vo";
632 vo_destroy(mpctx->video_out);
633 mpctx->video_out=NULL;
634 #ifdef CONFIG_DVDNAV
635 mp_dvdnav_context_free(mpctx);
636 #endif
639 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
640 if(mask&INITIALIZED_GETCH2){
641 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
642 current_module="uninit_getch2";
643 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
644 // restore terminal:
645 getch2_disable();
648 if(mask&INITIALIZED_VOBSUB){
649 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
650 current_module="uninit_vobsub";
651 if(vo_vobsub) vobsub_close(vo_vobsub);
652 vo_vobsub=NULL;
655 if (mask&INITIALIZED_SPUDEC){
656 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
657 current_module="uninit_spudec";
658 spudec_free(vo_spudec);
659 vo_spudec=NULL;
662 if(mask&INITIALIZED_AO){
663 mpctx->initialized_flags&=~INITIALIZED_AO;
664 current_module="uninit_ao";
665 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
666 if (mpctx->audio_out)
667 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
668 mpctx->audio_out=NULL;
671 current_module=NULL;
674 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
676 uninit_player(mpctx, INITIALIZED_ALL);
677 #if defined(__MINGW32__) || defined(__CYGWIN__)
678 timeEndPeriod(1);
679 #endif
680 #ifdef CONFIG_X11
681 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
682 #endif
684 current_module="uninit_input";
685 mp_input_uninit(mpctx->input);
686 #ifdef CONFIG_MENU
687 if (use_menu)
688 menu_uninit();
689 #endif
691 #ifdef CONFIG_FREETYPE
692 current_module="uninit_font";
693 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
694 free_font_desc(mpctx->osd->sub_font);
695 free_font_desc(vo_font);
696 vo_font = NULL;
697 done_freetype();
698 #endif
699 osd_free(mpctx->osd);
701 #ifdef CONFIG_ASS
702 ass_library_done(ass_library);
703 ass_library = NULL;
704 #endif
706 current_module="exit_player";
708 // free mplayer config
709 if(mpctx->mconfig)
710 m_config_free(mpctx->mconfig);
711 mpctx->mconfig = NULL;
713 if(mpctx->playtree_iter)
714 play_tree_iter_free(mpctx->playtree_iter);
715 mpctx->playtree_iter = NULL;
716 if(mpctx->playtree)
717 play_tree_free(mpctx->playtree, 1);
718 mpctx->playtree = NULL;
720 talloc_free(mpctx->key_fifo);
722 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
723 edl_records = NULL;
724 switch(how) {
725 case EXIT_QUIT:
726 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
727 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
728 break;
729 case EXIT_EOF:
730 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
731 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
732 break;
733 case EXIT_ERROR:
734 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
735 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
736 break;
737 default:
738 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
740 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
742 exit(rc);
745 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
747 exit_player_with_rc(mpctx, how, 1);
750 #ifndef __MINGW32__
751 static void child_sighandler(int x){
752 pid_t pid;
753 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
755 #endif
757 #ifdef CONFIG_CRASH_DEBUG
758 static char *prog_path;
759 static int crash_debug = 0;
760 #endif
762 static void exit_sighandler(int x){
763 static int sig_count=0;
764 #ifdef CONFIG_CRASH_DEBUG
765 if (!crash_debug || x != SIGTRAP)
766 #endif
767 ++sig_count;
768 if(sig_count==5)
770 /* We're crashing bad and can't uninit cleanly :(
771 * by popular request, we make one last (dirty)
772 * effort to restore the user's
773 * terminal. */
774 getch2_disable();
775 exit(1);
777 if(sig_count==6) exit(1);
778 if(sig_count>6){
779 // can't stop :(
780 #ifndef __MINGW32__
781 kill(getpid(),SIGKILL);
782 #endif
784 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
785 current_module?current_module:"unknown"
787 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
788 if(sig_count<=1)
789 switch(x){
790 case SIGINT:
791 case SIGQUIT:
792 case SIGTERM:
793 case SIGKILL:
794 async_quit_request = 1;
795 return; // killed from keyboard (^C) or killed [-9]
796 case SIGILL:
797 #if CONFIG_RUNTIME_CPUDETECT
798 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
799 #else
800 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
801 #endif
802 case SIGFPE:
803 case SIGSEGV:
804 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
805 default:
806 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
807 #ifdef CONFIG_CRASH_DEBUG
808 if (crash_debug) {
809 int gdb_pid;
810 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
811 gdb_pid = fork();
812 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
813 if (gdb_pid == 0) { // We are the child
814 char spid[20];
815 snprintf(spid, sizeof(spid), "%i", getppid());
816 getch2_disable(); // allow terminal to work properly with gdb
817 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
818 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
819 } else if (gdb_pid < 0)
820 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
821 else {
822 waitpid(gdb_pid, NULL, 0);
824 if (x == SIGTRAP) return;
826 #endif
828 getch2_disable();
829 exit(1);
832 #include "cfg-mplayer.h"
834 static int cfg_include(m_option_t *conf, char *filename)
836 return m_config_parse_config_file(conf->priv, filename);
839 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
841 char *conffile;
842 int conffile_fd;
843 if (!disable_system_conf &&
844 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
845 exit_player(mpctx, EXIT_NONE);
846 if ((conffile = get_path("")) == NULL) {
847 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
848 } else {
849 #ifdef __MINGW32__
850 mkdir(conffile);
851 #else
852 mkdir(conffile, 0777);
853 #endif
854 free(conffile);
855 if ((conffile = get_path("config")) == NULL) {
856 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
857 } else {
858 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
859 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
860 write(conffile_fd, default_config, strlen(default_config));
861 close(conffile_fd);
863 if (!disable_user_conf &&
864 m_config_parse_config_file(conf, conffile) < 0)
865 exit_player(mpctx, EXIT_NONE);
866 free(conffile);
871 #define PROFILE_CFG_PROTOCOL "protocol."
873 static void load_per_protocol_config (m_config_t* conf, const char *const file)
875 char *str;
876 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
877 m_profile_t *p;
879 /* does filename actually uses a protocol ? */
880 str = strstr (file, "://");
881 if (!str)
882 return;
884 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
885 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
886 p = m_config_get_profile (conf, protocol);
887 if (p)
889 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
890 m_config_set_profile(conf,p);
894 #define PROFILE_CFG_EXTENSION "extension."
896 static void load_per_extension_config (m_config_t* conf, const char *const file)
898 char *str;
899 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
900 m_profile_t *p;
902 /* does filename actually have an extension ? */
903 str = strrchr (file, '.');
904 if (!str)
905 return;
907 sprintf (extension, PROFILE_CFG_EXTENSION);
908 strncat (extension, ++str, 7);
909 p = m_config_get_profile (conf, extension);
910 if (p)
912 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
913 m_config_set_profile(conf,p);
917 #define PROFILE_CFG_VO "vo."
918 #define PROFILE_CFG_AO "ao."
920 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
922 char profile[strlen (cfg) + strlen (out) + 1];
923 m_profile_t *p;
925 sprintf (profile, "%s%s", cfg, out);
926 p = m_config_get_profile (conf, profile);
927 if (p)
929 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
930 m_config_set_profile(conf,p);
935 * Tries to load a config file
936 * @return 0 if file was not found, 1 otherwise
938 static int try_load_config(m_config_t *conf, const char *file)
940 struct stat st;
941 if (stat(file, &st))
942 return 0;
943 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
944 m_config_parse_config_file (conf, file);
945 return 1;
948 static void load_per_file_config (m_config_t* conf, const char *const file)
950 char *confpath;
951 char cfg[PATH_MAX];
952 char *name;
954 if (strlen(file) > PATH_MAX - 14) {
955 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
956 return;
958 sprintf (cfg, "%s.conf", file);
960 name = strrchr(cfg, '/');
961 if (HAVE_DOS_PATHS) {
962 char *tmp = strrchr(cfg, '\\');
963 if (!name || tmp > name)
964 name = tmp;
965 tmp = strrchr(cfg, ':');
966 if (!name || tmp > name)
967 name = tmp;
969 if (!name)
970 name = cfg;
971 else
972 name++;
974 if (use_filedir_conf) {
975 char dircfg[PATH_MAX];
976 strcpy(dircfg, cfg);
977 strcpy(dircfg + (name - cfg), "mplayer.conf");
978 try_load_config(conf, dircfg);
980 if (try_load_config(conf, cfg))
981 return;
984 if ((confpath = get_path (name)) != NULL)
986 try_load_config(conf, confpath);
988 free (confpath);
992 /* When libmpdemux performs a blocking operation (network connection or
993 * cache filling) if the operation fails we use this function to check
994 * if it was interrupted by the user.
995 * The function returns a new value for eof. */
996 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
998 mp_cmd_t* cmd;
999 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
1000 switch(cmd->id) {
1001 case MP_CMD_QUIT:
1002 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1003 case MP_CMD_PLAY_TREE_STEP: {
1004 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1005 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1006 } break;
1007 case MP_CMD_PLAY_TREE_UP_STEP: {
1008 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1009 } break;
1010 case MP_CMD_PLAY_ALT_SRC_STEP: {
1011 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1012 } break;
1014 mp_cmd_free(cmd);
1016 return stop_play;
1019 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1021 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1023 play_tree_add_bpf(entry,mpctx->filename);
1026 if(!entry) {
1027 entry = mpctx->playtree_iter->tree;
1028 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1029 return PT_NEXT_ENTRY;
1031 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1032 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1033 return PT_NEXT_ENTRY;
1036 play_tree_remove(entry,1,1);
1037 return PT_NEXT_SRC;
1039 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1040 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1041 entry = mpctx->playtree_iter->tree;
1042 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1043 return PT_NEXT_ENTRY;
1045 play_tree_remove(entry,1,1);
1047 return PT_NEXT_SRC;
1050 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1052 struct MPOpts *opts = &mpctx->opts;
1053 sub_data *subd = NULL;
1054 struct ass_track *asst = NULL;
1056 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1057 return;
1060 #ifdef CONFIG_ASS
1061 if (opts->ass_enabled) {
1062 #ifdef CONFIG_ICONV
1063 asst = ass_read_file(ass_library, filename, sub_cp);
1064 #else
1065 asst = ass_read_file(ass_library, filename, 0);
1066 #endif
1067 if (!asst) {
1068 subd = sub_read_file(filename, fps);
1069 if (subd) {
1070 asst = ass_read_subdata(ass_library, subd, fps);
1071 if (asst) {
1072 sub_free(subd);
1073 subd = NULL;
1077 } else
1078 #endif
1079 subd = sub_read_file(filename, fps);
1082 if (!asst && !subd) {
1083 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1084 "Cannot load subtitles: %s\n", filename_recode(filename));
1085 return;
1088 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1089 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1090 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1091 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1092 filename_recode(filename));
1093 ++mpctx->set_of_sub_size;
1094 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1095 filename_recode(filename));
1098 void init_vo_spudec(struct MPContext *mpctx)
1100 if (vo_spudec)
1101 spudec_free(vo_spudec);
1102 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1103 vo_spudec = NULL;
1104 if (spudec_ifo) {
1105 unsigned int palette[16], width, height;
1106 current_module="spudec_init_vobsub";
1107 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1108 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1111 #ifdef CONFIG_DVDREAD
1112 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1113 current_module="spudec_init_dvdread";
1114 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1115 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1116 NULL, 0);
1118 #endif
1120 #ifdef CONFIG_DVDNAV
1121 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1122 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1123 current_module="spudec_init_dvdnav";
1124 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1126 #endif
1128 if (vo_spudec==NULL) {
1129 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1130 current_module="spudec_init_normal";
1131 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1132 spudec_set_font_factor(vo_spudec,font_factor);
1135 if (vo_spudec!=NULL) {
1136 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1137 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1142 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1143 * make it all work is to use the builtin SDL-bootstrap code, which
1144 * will be done automatically by replacing our main() if we include SDL.h.
1146 #if defined(__APPLE__) && defined(CONFIG_SDL)
1147 #ifdef CONFIG_SDL_SDL_H
1148 #include <SDL/SDL.h>
1149 #else
1150 #include <SDL.h>
1151 #endif
1152 #endif
1155 * \brief append a formatted string
1156 * \param buf buffer to print into
1157 * \param pos position of terminating 0 in buf
1158 * \param len maximum number of characters in buf, not including terminating 0
1159 * \param format printf format string
1161 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1163 va_list va;
1164 va_start(va, format);
1165 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1166 va_end(va);
1167 if (*pos >= len ) {
1168 buf[len] = 0;
1169 *pos = len;
1174 * \brief append time in the hh:mm:ss.f format
1175 * \param buf buffer to print into
1176 * \param pos position of terminating 0 in buf
1177 * \param len maximum number of characters in buf, not including terminating 0
1178 * \param time time value to convert/append
1180 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1181 long tenths = 10 * time;
1182 int f1 = tenths % 10;
1183 int ss = (tenths / 10) % 60;
1184 int mm = (tenths / 600) % 60;
1185 int hh = tenths / 36000;
1186 if (time <= 0) {
1187 saddf(buf, pos, len, "unknown");
1188 return;
1190 if (hh > 0)
1191 saddf(buf, pos, len, "%2d:", hh);
1192 if (hh > 0 || mm > 0)
1193 saddf(buf, pos, len, "%02d:", mm);
1194 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1197 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1199 struct MPOpts *opts = &mpctx->opts;
1200 sh_video_t * const sh_video = mpctx->sh_video;
1202 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1203 a_pos = playing_audio_pts(mpctx);
1204 if (mpctx->sh_audio && sh_video && at_frame) {
1205 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1206 if (mpctx->time_frame > 0)
1207 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1208 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1209 && !mpctx->drop_message_shown) {
1210 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1211 mpctx->drop_message_shown = true;
1214 if (quiet)
1215 return;
1218 int width;
1219 char *line;
1220 unsigned pos = 0;
1221 get_screen_size();
1222 if (screen_width > 0)
1223 width = screen_width;
1224 else
1225 width = 80;
1226 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1227 /* Windows command line is broken (MinGW's rxvt works, but we
1228 * should not depend on that). */
1229 width--;
1230 #endif
1231 line = malloc(width + 1); // one additional char for the terminating null
1233 // Audio time
1234 if (mpctx->sh_audio) {
1235 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1236 if (!sh_video) {
1237 float len = demuxer_get_time_length(mpctx->demuxer);
1238 saddf(line, &pos, width, "(");
1239 sadd_hhmmssf(line, &pos, width, a_pos);
1240 saddf(line, &pos, width, ") of %.1f (", len);
1241 sadd_hhmmssf(line, &pos, width, len);
1242 saddf(line, &pos, width, ") ");
1246 // Video time
1247 if (sh_video)
1248 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1250 // A-V sync
1251 if (mpctx->sh_audio && sh_video)
1252 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1253 mpctx->last_av_difference, mpctx->total_avsync_change);
1255 // Video stats
1256 if (sh_video)
1257 saddf(line, &pos, width, "%3d/%3d ",
1258 (int)sh_video->num_frames,
1259 (int)sh_video->num_frames_decoded);
1261 // CPU usage
1262 if (sh_video) {
1263 if (sh_video->timer > 0.5)
1264 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1265 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1266 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1267 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1268 else
1269 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1270 } else if (mpctx->sh_audio) {
1271 if (mpctx->delay > 0.5)
1272 saddf(line, &pos, width, "%4.1f%% ",
1273 100.0*audio_time_usage/(double)mpctx->delay);
1274 else
1275 saddf(line, &pos, width, "??,?%% ");
1278 // VO stats
1279 if (sh_video)
1280 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1282 #ifdef CONFIG_STREAM_CACHE
1283 // cache stats
1284 if (stream_cache_size > 0)
1285 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1286 #endif
1288 // other
1289 if (opts->playback_speed != 1)
1290 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1292 // end
1293 if (erase_to_end_of_line) {
1294 line[pos] = 0;
1295 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1296 } else {
1297 memset(&line[pos], ' ', width - pos);
1298 line[width] = 0;
1299 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1301 free(line);
1305 * \brief build a chain of audio filters that converts the input format
1306 * to the ao's format, taking into account the current playback_speed.
1307 * \param sh_audio describes the requested input format of the chain.
1308 * \param ao_data describes the requested output format of the chain.
1310 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1312 struct MPOpts *opts = &mpctx->opts;
1313 int new_srate;
1314 int result;
1315 if (!sh_audio)
1317 mpctx->mixer.afilter = NULL;
1318 return 0;
1320 if(af_control_any_rev(sh_audio->afilter,
1321 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1322 &opts->playback_speed)) {
1323 new_srate = sh_audio->samplerate;
1324 } else {
1325 new_srate = sh_audio->samplerate * opts->playback_speed;
1326 if (new_srate != ao_data->samplerate) {
1327 // limits are taken from libaf/af_resample.c
1328 if (new_srate < 8000)
1329 new_srate = 8000;
1330 if (new_srate > 192000)
1331 new_srate = 192000;
1332 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1335 result = init_audio_filters(sh_audio, new_srate,
1336 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1337 mpctx->mixer.afilter = sh_audio->afilter;
1338 return result;
1342 typedef struct mp_osd_msg mp_osd_msg_t;
1343 struct mp_osd_msg {
1344 /// Previous message on the stack.
1345 mp_osd_msg_t* prev;
1346 /// Message text.
1347 char msg[128];
1348 int id,level,started;
1349 /// Display duration in ms.
1350 unsigned time;
1353 /// OSD message stack.
1354 static mp_osd_msg_t* osd_msg_stack = NULL;
1357 * \brief Add a message on the OSD message stack
1359 * If a message with the same id is already present in the stack
1360 * it is pulled on top of the stack, otherwise a new message is created.
1363 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1364 va_list ap)
1366 mp_osd_msg_t *msg,*last=NULL;
1367 int r;
1369 // look if the id is already in the stack
1370 for(msg = osd_msg_stack ; msg && msg->id != id ;
1371 last = msg, msg = msg->prev);
1372 // not found: alloc it
1373 if(!msg) {
1374 msg = calloc(1,sizeof(mp_osd_msg_t));
1375 msg->prev = osd_msg_stack;
1376 osd_msg_stack = msg;
1377 } else if(last) { // found, but it's not on top of the stack
1378 last->prev = msg->prev;
1379 msg->prev = osd_msg_stack;
1380 osd_msg_stack = msg;
1382 // write the msg
1383 r = vsnprintf(msg->msg, 128, fmt, ap);
1384 if(r >= 128) msg->msg[127] = 0;
1385 // set id and time
1386 msg->id = id;
1387 msg->level = level;
1388 msg->time = time;
1392 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1394 va_list ap;
1395 va_start(ap, fmt);
1396 set_osd_msg_va(id, level, time, fmt, ap);
1397 va_end(ap);
1400 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1402 va_list ap;
1403 va_start(ap, fmt);
1404 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1405 va_end(ap);
1410 * \brief Remove a message from the OSD stack
1412 * This function can be used to get rid of a message right away.
1416 void rm_osd_msg(int id) {
1417 mp_osd_msg_t *msg,*last=NULL;
1419 // Search for the msg
1420 for(msg = osd_msg_stack ; msg && msg->id != id ;
1421 last = msg, msg = msg->prev);
1422 if(!msg) return;
1424 // Detach it from the stack and free it
1425 if(last)
1426 last->prev = msg->prev;
1427 else
1428 osd_msg_stack = msg->prev;
1429 free(msg);
1433 * \brief Remove all messages from the OSD stack
1437 static void clear_osd_msgs(void) {
1438 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1439 while(msg) {
1440 prev = msg->prev;
1441 free(msg);
1442 msg = prev;
1444 osd_msg_stack = NULL;
1448 * \brief Get the current message from the OSD stack.
1450 * This function decrements the message timer and destroys the old ones.
1451 * The message that should be displayed is returned (if any).
1455 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1457 struct MPOpts *opts = &mpctx->opts;
1458 mp_osd_msg_t *msg,*prev,*last = NULL;
1459 static unsigned last_update = 0;
1460 unsigned now = GetTimerMS();
1461 unsigned diff;
1462 char hidden_dec_done = 0;
1464 if (mpctx->osd_visible) {
1465 // 36000000 means max timed visibility is 1 hour into the future, if
1466 // the difference is greater assume it's wrapped around from below 0
1467 if (mpctx->osd_visible - now > 36000000) {
1468 mpctx->osd_visible = 0;
1469 vo_osd_progbar_type = -1; // disable
1470 vo_osd_changed(OSDTYPE_PROGBAR);
1471 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1474 if (mpctx->osd_show_percentage_until - now > 36000000)
1475 mpctx->osd_show_percentage_until = 0;
1477 if(!last_update) last_update = now;
1478 diff = now >= last_update ? now - last_update : 0;
1480 last_update = now;
1482 // Look for the first message in the stack with high enough level.
1483 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1484 prev = msg->prev;
1485 if (msg->level > opts->osd_level && hidden_dec_done)
1486 continue;
1487 // The message has a high enough level or it is the first hidden one
1488 // in both cases we decrement the timer or kill it.
1489 if(!msg->started || msg->time > diff) {
1490 if(msg->started) msg->time -= diff;
1491 else msg->started = 1;
1492 // display it
1493 if (msg->level <= opts->osd_level)
1494 return msg;
1495 hidden_dec_done = 1;
1496 continue;
1498 // kill the message
1499 free(msg);
1500 if(last) {
1501 last->prev = prev;
1502 msg = last;
1503 } else {
1504 osd_msg_stack = prev;
1505 msg = NULL;
1508 // Nothing found
1509 return NULL;
1513 * \brief Display the OSD bar.
1515 * Display the OSD bar or fall back on a simple message.
1519 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1520 struct MPOpts *opts = &mpctx->opts;
1521 if (opts->osd_level < 1)
1522 return;
1524 if(mpctx->sh_video) {
1525 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1526 vo_osd_progbar_type = type;
1527 vo_osd_progbar_value = 256*(val-min)/(max-min);
1528 vo_osd_changed(OSDTYPE_PROGBAR);
1529 return;
1532 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1533 name, ROUND(100*(val-min)/(max-min)));
1537 * \brief Display text subtitles on the OSD
1539 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1541 int i;
1542 vo_sub = subs;
1543 vo_osd_changed(OSDTYPE_SUBTITLE);
1544 if (!mpctx->sh_video) {
1545 // reverse order, since newest set_osd_msg is displayed first
1546 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1547 if (!subs || i >= subs->lines || !subs->text[i])
1548 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1549 else {
1550 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1551 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1552 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1559 * \brief Update the OSD message line.
1561 * This function displays the current message on the vo OSD or on the term.
1562 * If the stack is empty and the OSD level is high enough the timer
1563 * is displayed (only on the vo OSD).
1567 static void update_osd_msg(struct MPContext *mpctx)
1569 struct MPOpts *opts = &mpctx->opts;
1570 mp_osd_msg_t *msg;
1571 struct osd_state *osd = mpctx->osd;
1572 char osd_text_timer[128];
1574 if (mpctx->add_osd_seek_info) {
1575 double percentage;
1576 if (mpctx->timeline && mpctx->sh_video)
1577 percentage = mpctx->sh_video->pts * 100 /
1578 mpctx->timeline[mpctx->num_timeline_parts].start;
1579 else
1580 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1581 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1582 if (mpctx->sh_video)
1583 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1584 mpctx->add_osd_seek_info = false;
1587 // Look if we have a msg
1588 if((msg = get_osd_msg(mpctx))) {
1589 if (strcmp(osd->osd_text, msg->msg)) {
1590 strncpy(osd->osd_text, msg->msg, 127);
1591 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1592 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1594 return;
1597 if(mpctx->sh_video) {
1598 // fallback on the timer
1599 if (opts->osd_level >= 2) {
1600 int len;
1601 if (mpctx->timeline)
1602 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1603 else
1604 len = demuxer_get_time_length(mpctx->demuxer);
1605 int percentage = -1;
1606 char percentage_text[10];
1607 int pts = demuxer_get_current_time(mpctx->demuxer);
1609 if (mpctx->osd_show_percentage_until)
1610 if (mpctx->timeline)
1611 percentage = mpctx->sh_video->pts * 100 /
1612 mpctx->timeline[mpctx->num_timeline_parts].start;
1613 else
1614 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1616 if (percentage >= 0)
1617 snprintf(percentage_text, 9, " (%d%%)", percentage);
1618 else
1619 percentage_text[0] = 0;
1621 if (opts->osd_level == 3)
1622 snprintf(osd_text_timer, 63,
1623 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1624 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1625 len/3600,(len/60)%60,len%60,percentage_text);
1626 else
1627 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1628 mpctx->osd_function,pts/3600,(pts/60)%60,
1629 pts%60,percentage_text);
1630 } else
1631 osd_text_timer[0]=0;
1633 if (strcmp(osd->osd_text, osd_text_timer)) {
1634 strncpy(osd->osd_text, osd_text_timer, 63);
1635 vo_osd_changed(OSDTYPE_OSD);
1637 return;
1640 // Clear the term osd line
1641 if (term_osd && osd->osd_text[0]) {
1642 osd->osd_text[0] = 0;
1643 printf("%s\n",term_osd_esc);
1647 ///@}
1648 // OSDMsgStack
1651 void reinit_audio_chain(struct MPContext *mpctx)
1653 struct MPOpts *opts = &mpctx->opts;
1654 if (!mpctx->sh_audio)
1655 return;
1656 current_module="init_audio_codec";
1657 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1658 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1659 goto init_error;
1661 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1662 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1665 current_module="af_preinit";
1666 ao_data.samplerate=force_srate;
1667 ao_data.channels=0;
1668 ao_data.format=audio_output_format;
1669 #if 1
1670 // first init to detect best values
1671 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1672 // input:
1673 mpctx->sh_audio->samplerate,
1674 // output:
1675 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1676 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1677 "pre-init!\n");
1678 exit_player(mpctx, EXIT_ERROR);
1680 #endif
1681 current_module="ao2_init";
1682 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1683 0, // plugin flag
1684 ao_data.samplerate,
1685 ao_data.channels,
1686 ao_data.format, 0);
1687 if(!mpctx->audio_out){
1688 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1689 goto init_error;
1691 mpctx->initialized_flags|=INITIALIZED_AO;
1692 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1693 mpctx->audio_out->info->short_name,
1694 ao_data.samplerate, ao_data.channels,
1695 af_fmt2str_short(ao_data.format),
1696 af_fmt2bits(ao_data.format)/8 );
1697 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1698 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1699 if(strlen(mpctx->audio_out->info->comment) > 0)
1700 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1701 // init audio filters:
1702 #if 1
1703 current_module="af_init";
1704 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1705 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1706 goto init_error;
1708 #endif
1709 mpctx->mixer.audio_out = mpctx->audio_out;
1710 mpctx->mixer.volstep = volstep;
1711 return;
1713 init_error:
1714 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1715 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1716 mpctx->d_audio->id = -2;
1720 ///@}
1721 // Command2Property
1724 // Return pts value corresponding to the end point of audio written to the
1725 // ao so far.
1726 static double written_audio_pts(struct MPContext *mpctx)
1728 sh_audio_t *sh_audio = mpctx->sh_audio;
1729 demux_stream_t *d_audio = mpctx->d_audio;
1730 double buffered_output;
1731 // first calculate the end pts of audio that has been output by decoder
1732 double a_pts = sh_audio->pts;
1733 if (a_pts != MP_NOPTS_VALUE)
1734 // Good, decoder supports new way of calculating audio pts.
1735 // sh_audio->pts is the timestamp of the latest input packet with
1736 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1737 // the amount of bytes the decoder has written after that timestamp.
1738 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1739 else {
1740 // Decoder doesn't support new way of calculating pts (or we're
1741 // being called before it has decoded anything with known timestamp).
1742 // Use the old method of audio pts calculation: take the timestamp
1743 // of last packet with known pts the decoder has read data from,
1744 // and add amount of bytes read after the beginning of that packet
1745 // divided by input bps. This will be inaccurate if the input/output
1746 // ratio is not constant for every audio packet or if it is constant
1747 // but not accurately known in sh_audio->i_bps.
1749 a_pts = d_audio->pts;
1750 // ds_tell_pts returns bytes read after last timestamp from
1751 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1752 // it has read but not decoded
1753 if (sh_audio->i_bps)
1754 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1755 (double)sh_audio->i_bps;
1757 // Now a_pts hopefully holds the pts for end of audio from decoder.
1758 // Substract data in buffers between decoder and audio out.
1760 // Decoded but not filtered
1761 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1763 // Data buffered in audio filters, measured in bytes of "missing" output
1764 buffered_output = af_calc_delay(sh_audio->afilter);
1766 // Data that was ready for ao but was buffered because ao didn't fully
1767 // accept everything to internal buffers yet
1768 buffered_output += sh_audio->a_out_buffer_len;
1770 // Filters divide audio length by playback_speed, so multiply by it
1771 // to get the length in original units without speedup or slowdown
1772 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1774 return a_pts + mpctx->video_offset;
1777 // Return pts value corresponding to currently playing audio.
1778 double playing_audio_pts(struct MPContext *mpctx)
1780 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1781 mpctx->audio_out->get_delay();
1784 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1785 struct MPOpts *opts = &mpctx->opts;
1786 // check for frame-drop:
1787 current_module = "check_framedrop";
1788 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1789 static int dropped_frames;
1790 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1791 float d = delay-mpctx->delay;
1792 ++total_frame_cnt;
1793 // we should avoid dropping too many frames in sequence unless we
1794 // are too late. and we allow 100ms A-V delay here:
1795 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1796 && !mpctx->update_video_immediately) {
1797 ++drop_frame_cnt;
1798 ++dropped_frames;
1799 return frame_dropping;
1800 } else
1801 dropped_frames = 0;
1803 return 0;
1807 #ifdef HAVE_RTC
1808 int rtc_fd = -1;
1809 #endif
1811 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1813 #ifdef HAVE_RTC
1814 if (rtc_fd >= 0){
1815 // -------- RTC -----------
1816 current_module="sleep_rtc";
1817 while (time_frame > 0.000) {
1818 unsigned long rtc_ts;
1819 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1820 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1821 time_frame -= get_relative_time(mpctx);
1823 } else
1824 #endif
1826 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1827 // unnecessarily high CPU usage
1828 float margin = softsleep ? 0.011 : 0;
1829 current_module = "sleep_timer";
1830 while (time_frame > margin) {
1831 usec_sleep(1000000 * (time_frame - margin));
1832 time_frame -= get_relative_time(mpctx);
1834 if (softsleep){
1835 current_module = "sleep_soft";
1836 if (time_frame < 0)
1837 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1838 while (time_frame > 0)
1839 time_frame -= get_relative_time(mpctx); // burn the CPU
1842 return time_frame;
1845 static void select_subtitle(MPContext *mpctx)
1847 struct MPOpts *opts = &mpctx->opts;
1848 // find the best sub to use
1849 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1850 mpctx->global_sub_pos = -1; // no subs by default
1851 if (vobsub_index_id >= 0) {
1852 // if user asks for a vobsub id, use that first.
1853 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1854 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1855 // if user asks for a dvd sub id, use that next.
1856 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1857 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1858 // if there are text subs to use, use those. (autosubs come last here)
1859 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1860 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1861 // finally select subs by language and container hints
1862 if (opts->sub_id == -1 && dvdsub_lang)
1863 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1864 if (opts->sub_id == -1)
1865 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1866 if (opts->sub_id >= 0)
1867 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1869 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1870 mpctx->global_sub_pos--;
1871 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1874 #ifdef CONFIG_DVDNAV
1875 #ifndef FF_B_TYPE
1876 #define FF_B_TYPE 3
1877 #endif
1878 /// store decoded video image
1879 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1880 mp_image_t *from_mpi) {
1881 mp_image_t *mpi;
1883 /// Do not store B-frames
1884 if (from_mpi->pict_type == FF_B_TYPE)
1885 return to_mpi;
1887 if (to_mpi &&
1888 to_mpi->w == from_mpi->w &&
1889 to_mpi->h == from_mpi->h &&
1890 to_mpi->imgfmt == from_mpi->imgfmt)
1891 mpi = to_mpi;
1892 else {
1893 if (to_mpi)
1894 free_mp_image(to_mpi);
1895 if (from_mpi->w == 0 || from_mpi->h == 0)
1896 return NULL;
1897 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1900 copy_mpi(mpi,from_mpi);
1901 return mpi;
1904 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1905 struct MPOpts *opts = &ctx->opts;
1906 if (ctx->sh_video) {
1907 /// clear video pts
1908 ctx->d_video->pts = 0.0f;
1909 ctx->sh_video->pts = 0.0f;
1910 ctx->sh_video->i_pts = 0.0f;
1911 ctx->sh_video->last_pts = 0.0f;
1912 ctx->sh_video->num_buffered_pts = 0;
1913 ctx->sh_video->num_frames = 0;
1914 ctx->sh_video->num_frames_decoded = 0;
1915 ctx->sh_video->timer = 0.0f;
1916 ctx->sh_video->stream_delay = 0.0f;
1917 ctx->sh_video->timer = 0;
1918 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1921 if (ctx->sh_audio) {
1922 /// free audio packets and reset
1923 ds_free_packs(ctx->d_audio);
1924 audio_delay -= ctx->sh_audio->stream_delay;
1925 ctx->delay =- audio_delay;
1926 ctx->audio_out->reset();
1927 resync_audio_stream(ctx->sh_audio);
1930 audio_delay = 0.0f;
1931 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1932 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1933 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1934 if (dvdsub_lang_id != opts->sub_id) {
1935 opts->sub_id = dvdsub_lang_id;
1936 select_subtitle(ctx);
1940 /// clear all EOF related flags
1941 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1944 /// Restore last decoded DVDNAV (still frame)
1945 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1946 int *in_size,
1947 unsigned char **start,
1948 mp_image_t *decoded_frame)
1950 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1951 return decoded_frame;
1953 /// a change occured in dvdnav stream
1954 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1955 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1956 mp_dvdnav_context_free(mpctx);
1957 mp_dvdnav_reset_stream(mpctx);
1958 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1959 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1962 if (*in_size < 0) {
1963 float len;
1965 /// Display still frame, if any
1966 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1967 decoded_frame = mpctx->nav_smpi;
1969 /// increment video frame : continue playing after still frame
1970 len = demuxer_get_time_length(mpctx->demuxer);
1971 if (mpctx->sh_video->pts >= len &&
1972 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1973 mp_dvdnav_skip_still(mpctx->stream);
1974 mp_dvdnav_skip_wait(mpctx->stream);
1976 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1978 if (mpctx->nav_buffer) {
1979 *start = mpctx->nav_start;
1980 *in_size = mpctx->nav_in_size;
1981 if (mpctx->nav_start)
1982 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1986 return decoded_frame;
1989 /// Save last decoded DVDNAV (still frame)
1990 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1991 unsigned char *start,
1992 mp_image_t *decoded_frame)
1994 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1995 return;
1997 if (mpctx->nav_buffer)
1998 free(mpctx->nav_buffer);
2000 mpctx->nav_buffer = malloc(in_size);
2001 mpctx->nav_start = start;
2002 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2003 if (mpctx->nav_buffer)
2004 memcpy(mpctx->nav_buffer,start,in_size);
2006 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2007 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2009 #endif /* CONFIG_DVDNAV */
2011 /* Modify video timing to match the audio timeline. There are two main
2012 * reasons this is needed. First, video and audio can start from different
2013 * positions at beginning of file or after a seek (MPlayer starts both
2014 * immediately even if they have different pts). Second, the file can have
2015 * audio timestamps that are inconsistent with the duration of the audio
2016 * packets, for example two consecutive timestamp values differing by
2017 * one second but only a packet with enough samples for half a second
2018 * of playback between them.
2020 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2022 current_module = "av_sync";
2024 if (!mpctx->sh_audio)
2025 return;
2027 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2028 double v_pts = mpctx->sh_video->pts;
2029 double av_delay = a_pts - v_pts;
2030 // Try to sync vo_flip() so it will *finish* at given time
2031 av_delay += mpctx->last_vo_flip_duration;
2032 av_delay -= audio_delay; // This much pts difference is desired
2034 double change = av_delay * 0.1;
2035 double max_change = default_max_pts_correction >= 0 ?
2036 default_max_pts_correction : frame_time * 0.1;
2037 if (change < -max_change)
2038 change = -max_change;
2039 else if (change > max_change)
2040 change = max_change;
2041 mpctx->delay += change;
2042 mpctx->total_avsync_change += change;
2045 static int fill_audio_out_buffers(struct MPContext *mpctx)
2047 struct MPOpts *opts = &mpctx->opts;
2048 unsigned int t;
2049 double tt;
2050 int playsize;
2051 int playflags=0;
2052 int audio_eof=0;
2053 sh_audio_t * const sh_audio = mpctx->sh_audio;
2055 current_module="play_audio";
2057 while (1) {
2058 int sleep_time;
2059 // all the current uses of ao_data.pts seem to be in aos that handle
2060 // sync completely wrong; there should be no need to use ao_data.pts
2061 // in get_space()
2062 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2063 playsize = mpctx->audio_out->get_space();
2064 if (mpctx->sh_video || playsize >= ao_data.outburst)
2065 break;
2067 // handle audio-only case:
2068 // this is where mplayer sleeps during audio-only playback
2069 // to avoid 100% CPU use
2070 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2071 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2072 usec_sleep(sleep_time * 1000);
2075 // Fill buffer if needed:
2076 current_module="decode_audio";
2077 t = GetTimer();
2078 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2079 if (mpctx->d_audio->eof) {
2080 audio_eof = 1;
2081 if (sh_audio->a_out_buffer_len == 0)
2082 return 0;
2084 t = GetTimer() - t;
2085 tt = t*0.000001f; audio_time_usage+=tt;
2086 if (playsize > sh_audio->a_out_buffer_len) {
2087 playsize = sh_audio->a_out_buffer_len;
2088 if (audio_eof)
2089 playflags |= AOPLAY_FINAL_CHUNK;
2091 if (!playsize)
2092 return 1;
2094 // play audio:
2095 current_module="play_audio";
2097 // Is this pts value actually useful for the aos that access it?
2098 // They're obviously badly broken in the way they handle av sync;
2099 // would not having access to this make them more broken?
2100 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2101 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2103 if (playsize > 0) {
2104 sh_audio->a_out_buffer_len -= playsize;
2105 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2106 sh_audio->a_out_buffer_len);
2107 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2109 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2110 // Sanity check to avoid hanging in case current ao doesn't output
2111 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2112 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2113 sh_audio->a_out_buffer_len = 0;
2116 return 1;
2119 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2120 float *aq_sleep_time)
2122 struct MPOpts *opts = &mpctx->opts;
2123 int frame_time_remaining = 0;
2124 current_module="calc_sleep_time";
2126 *time_frame -= get_relative_time(mpctx); // reset timer
2128 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2129 float delay = mpctx->audio_out->get_delay();
2130 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2132 if (autosync) {
2134 * Adjust this raw delay value by calculating the expected
2135 * delay for this frame and generating a new value which is
2136 * weighted between the two. The higher autosync is, the
2137 * closer to the delay value gets to that which "-nosound"
2138 * would have used, and the longer it will take for A/V
2139 * sync to settle at the right value (but it eventually will.)
2140 * This settling time is very short for values below 100.
2142 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2143 float difference = delay - predicted;
2144 delay = predicted + difference / (float)autosync;
2147 *time_frame = delay - mpctx->delay / opts->playback_speed;
2149 // delay = amount of audio buffered in soundcard/driver
2150 if (delay > 0.25) delay=0.25; else
2151 if (delay < 0.10) delay=0.10;
2152 if (*time_frame > delay*0.6) {
2153 // sleep time too big - may cause audio drops (buffer underrun)
2154 frame_time_remaining = 1;
2155 *time_frame = delay*0.5;
2157 } else {
2158 // If we're lagging more than 200 ms behind the right playback rate,
2159 // don't try to "catch up".
2160 // If benchmark is set always output frames as fast as possible
2161 // without sleeping.
2162 if (*time_frame < -0.2 || benchmark)
2163 *time_frame = 0;
2166 *aq_sleep_time += *time_frame;
2169 //============================== SLEEP: ===================================
2171 *time_frame -= mpctx->video_out->flip_queue_offset;
2172 // flag 256 means: libvo driver does its timing (dvb card)
2173 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2174 *time_frame = timing_sleep(mpctx, *time_frame);
2175 *time_frame += mpctx->video_out->flip_queue_offset;
2176 return frame_time_remaining;
2179 int reinit_video_chain(struct MPContext *mpctx)
2181 struct MPOpts *opts = &mpctx->opts;
2182 sh_video_t * const sh_video = mpctx->sh_video;
2183 double ar=-1.0;
2184 //================== Init VIDEO (codec & libvo) ==========================
2185 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2186 current_module="preinit_libvo";
2188 //shouldn't we set dvideo->id=-2 when we fail?
2189 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2190 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2191 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2192 goto err_out;
2194 mpctx->initialized_flags|=INITIALIZED_VO;
2197 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2198 mpctx->sh_video->stream_aspect = ar;
2199 current_module="init_video_filters";
2201 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2202 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2204 #ifdef CONFIG_MENU
2205 if(use_menu) {
2206 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2207 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2208 if(!vf_menu) {
2209 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2210 use_menu = 0;
2213 if(vf_menu)
2214 sh_video->vfilter = vf_menu;
2215 #endif
2217 #ifdef CONFIG_ASS
2218 if(opts->ass_enabled) {
2219 int i;
2220 int insert = 1;
2221 if (opts->vf_settings)
2222 for (i = 0; opts->vf_settings[i].name; ++i)
2223 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2224 insert = 0;
2225 break;
2227 if (insert) {
2228 extern vf_info_t vf_info_ass;
2229 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2230 char* vf_arg[] = {"auto", "1", NULL};
2231 int retcode = 0;
2232 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2233 sh_video->vfilter,
2234 "ass", vf_arg,
2235 &retcode);
2236 if (vf_ass)
2237 sh_video->vfilter = vf_ass;
2238 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2239 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2240 else
2241 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2244 #endif
2246 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2248 #ifdef CONFIG_ASS
2249 if (opts->ass_enabled)
2250 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2251 #endif
2253 current_module="init_video_codec";
2255 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2256 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2257 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2259 if(!sh_video->initialized){
2260 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2261 goto err_out;
2264 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2266 if (sh_video->codec)
2267 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2269 sh_video->last_pts = MP_NOPTS_VALUE;
2270 sh_video->num_buffered_pts = 0;
2271 sh_video->next_frame_time = 0;
2273 if(auto_quality>0){
2274 // Auto quality option enabled
2275 output_quality=get_video_quality_max(sh_video);
2276 if(auto_quality>output_quality) auto_quality=output_quality;
2277 else output_quality=auto_quality;
2278 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2279 set_video_quality(sh_video,output_quality);
2282 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2284 current_module="init_vo";
2286 return 1;
2288 err_out:
2289 mpctx->sh_video = mpctx->d_video->sh = NULL;
2290 return 0;
2293 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2295 struct sh_video *sh_video = mpctx->sh_video;
2296 double frame_time = 0;
2297 struct vo *video_out = mpctx->video_out;
2298 while (!video_out->frame_loaded) {
2299 current_module = "filter_video";
2300 // In nocorrect-pts mode there is no way to properly time these frames
2301 if (vo_get_buffered_frame(video_out, 0) >= 0)
2302 break;
2303 if (vf_output_queued_frame(sh_video->vfilter))
2304 continue;
2305 unsigned char *packet = NULL;
2306 frame_time = sh_video->next_frame_time;
2307 if (mpctx->update_video_immediately)
2308 frame_time = 0;
2309 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2310 &packet, force_fps);
2311 if (in_size < 0) {
2312 #ifdef CONFIG_DVDNAV
2313 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2314 if (mp_dvdnav_is_eof(mpctx->stream))
2315 return -1;
2316 if (mpctx->d_video)
2317 mpctx->d_video->eof = 0;
2318 if (mpctx->d_audio)
2319 mpctx->d_audio->eof = 0;
2320 mpctx->stream->eof = 0;
2321 } else
2322 #endif
2323 return -1;
2325 if (in_size > max_framesize)
2326 max_framesize = in_size;
2327 sh_video->timer += frame_time;
2328 if (mpctx->sh_audio)
2329 mpctx->delay -= frame_time;
2330 // video_read_frame can change fps (e.g. for ASF video)
2331 vo_fps = sh_video->fps;
2332 int framedrop_type = check_framedrop(mpctx, frame_time);
2333 current_module = "decode video";
2335 void *decoded_frame;
2336 #ifdef CONFIG_DVDNAV
2337 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2338 if (in_size >= 0 && !decoded_frame)
2339 #endif
2340 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2341 sh_video->pts);
2342 #ifdef CONFIG_DVDNAV
2343 // Save last still frame for future display
2344 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2345 #endif
2346 if (decoded_frame) {
2347 current_module = "filter video";
2348 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2349 if (!video_out->config_ok)
2350 break;
2353 return frame_time;
2356 static void determine_frame_pts(struct MPContext *mpctx)
2358 struct sh_video *sh_video = mpctx->sh_video;
2359 struct MPOpts *opts = &mpctx->opts;
2361 if (opts->user_pts_assoc_mode) {
2362 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2363 } else if (sh_video->pts_assoc_mode == 0) {
2364 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2365 sh_video->pts_assoc_mode = 1;
2366 else
2367 sh_video->pts_assoc_mode = 2;
2368 } else {
2369 int probcount1 = sh_video->num_reordered_pts_problems;
2370 int probcount2 = sh_video->num_sorted_pts_problems;
2371 if (sh_video->pts_assoc_mode == 2) {
2372 int tmp = probcount1;
2373 probcount1 = probcount2;
2374 probcount2 = tmp;
2376 if (probcount1 >= probcount2 * 1.5 + 2) {
2377 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2378 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2379 "%d.\n", sh_video->pts_assoc_mode);
2382 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2383 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2386 static double update_video(struct MPContext *mpctx)
2388 struct sh_video *sh_video = mpctx->sh_video;
2389 struct vo *video_out = mpctx->video_out;
2390 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2391 mpctx->osd); // hack for vf_expand
2392 if (!mpctx->opts.correct_pts)
2393 return update_video_nocorrect_pts(mpctx);
2395 double pts;
2397 bool hit_eof = false;
2398 while (!video_out->frame_loaded) {
2399 current_module = "filter_video";
2400 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2401 break;
2402 // XXX Time used in this call is not counted in any performance
2403 // timer now, OSD time is not updated correctly for filter-added frames
2404 if (vf_output_queued_frame(sh_video->vfilter))
2405 continue;
2406 if (hit_eof)
2407 return -1;
2408 unsigned char *packet = NULL;
2409 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2410 if (pts != MP_NOPTS_VALUE)
2411 pts += mpctx->video_offset;
2412 if (in_size < 0) {
2413 // try to extract last frames in case of decoder lag
2414 in_size = 0;
2415 pts = 1e300;
2416 hit_eof = true;
2418 if (in_size > max_framesize)
2419 max_framesize = in_size;
2420 current_module = "decode video";
2421 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2422 void *decoded_frame = decode_video(sh_video, packet, in_size,
2423 framedrop_type, pts);
2424 if (decoded_frame) {
2425 determine_frame_pts(mpctx);
2426 current_module = "filter video";
2427 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2428 if (!video_out->config_ok)
2429 break; // We'd likely hang in this loop otherwise
2433 pts = video_out->next_pts;
2434 if (pts == MP_NOPTS_VALUE) {
2435 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2436 // Try to use decoder pts from before filters
2437 pts = sh_video->pts;
2438 if (pts == MP_NOPTS_VALUE)
2439 pts = sh_video->last_pts;
2441 sh_video->pts = pts;
2442 if (sh_video->last_pts == MP_NOPTS_VALUE)
2443 sh_video->last_pts = sh_video->pts;
2444 else if (sh_video->last_pts > sh_video->pts) {
2445 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2446 sh_video->pts, sh_video->last_pts);
2447 /* If the difference in pts is small treat it as jitter around the
2448 * right value (possibly caused by incorrect timestamp ordering) and
2449 * just show this frame immediately after the last one.
2450 * Treat bigger differences as timestamp resets and start counting
2451 * timing of later frames from the position of this one. */
2452 if (sh_video->last_pts - sh_video->pts > 0.5)
2453 sh_video->last_pts = sh_video->pts;
2454 else
2455 sh_video->pts = sh_video->last_pts;
2457 double frame_time = sh_video->pts - sh_video->last_pts;
2458 sh_video->last_pts = sh_video->pts;
2459 sh_video->timer += frame_time;
2460 if (mpctx->sh_audio)
2461 mpctx->delay -= frame_time;
2462 return frame_time;
2465 void pause_player(struct MPContext *mpctx)
2467 if (mpctx->paused)
2468 return;
2469 mpctx->paused = 1;
2470 mpctx->step_frames = 0;
2471 mpctx->time_frame -= get_relative_time(mpctx);
2473 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2474 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2476 if (mpctx->audio_out && mpctx->sh_audio)
2477 mpctx->audio_out->pause(); // pause audio, keep data if possible
2480 void unpause_player(struct MPContext *mpctx)
2482 if (!mpctx->paused)
2483 return;
2484 mpctx->paused = 0;
2486 if (mpctx->audio_out && mpctx->sh_audio)
2487 mpctx->audio_out->resume(); // resume audio
2488 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2489 && !mpctx->step_frames)
2490 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2491 (void)get_relative_time(mpctx); // ignore time that passed during pause
2494 void add_step_frame(struct MPContext *mpctx)
2496 mpctx->step_frames++;
2497 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2498 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2499 unpause_player(mpctx);
2502 static void pause_loop(struct MPContext *mpctx)
2504 mp_cmd_t* cmd;
2505 if (!quiet) {
2506 // Small hack to display the pause message on the OSD line.
2507 // The pause string is: "\n == PAUSE == \r" so we need to
2508 // take the first and the last char out
2509 if (term_osd && !mpctx->sh_video) {
2510 char msg[128] = _("\n ===== PAUSE =====\r");
2511 int mlen = strlen(msg);
2512 msg[mlen-1] = '\0';
2513 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2514 update_osd_msg(mpctx);
2515 } else
2516 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2517 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2520 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2521 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2522 if (cmd) {
2523 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2524 run_command(mpctx, cmd);
2525 mp_cmd_free(cmd);
2526 continue;
2528 if (mpctx->sh_video && mpctx->video_out)
2529 vo_check_events(mpctx->video_out);
2530 #ifdef CONFIG_MENU
2531 if (vf_menu)
2532 vf_menu_pause_update(vf_menu);
2533 #endif
2534 usec_sleep(20000);
2535 update_osd_msg(mpctx);
2536 int hack = vo_osd_changed(0);
2537 vo_osd_changed(hack);
2538 if (hack)
2539 break;
2544 // Find the right mute status and record position for new file position
2545 static void edl_seek_reset(MPContext *mpctx)
2547 mpctx->edl_muted = 0;
2548 next_edl_record = edl_records;
2550 while (next_edl_record) {
2551 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2552 break;
2554 if (next_edl_record->action == EDL_MUTE)
2555 mpctx->edl_muted = !mpctx->edl_muted;
2556 next_edl_record = next_edl_record->next;
2558 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2559 mixer_mute(&mpctx->mixer);
2563 // Execute EDL command for the current position if one exists
2564 static void edl_update(MPContext *mpctx)
2566 if (!next_edl_record)
2567 return;
2569 if (!mpctx->sh_video) {
2570 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2571 free_edl(edl_records);
2572 next_edl_record = NULL;
2573 edl_records = NULL;
2574 return;
2577 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2578 if (next_edl_record->action == EDL_SKIP) {
2579 mpctx->osd_function = OSD_FFW;
2580 mpctx->abs_seek_pos = 0;
2581 mpctx->rel_seek_secs = next_edl_record->length_sec;
2582 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2583 "[%f], length [%f]\n", next_edl_record->start_sec,
2584 next_edl_record->stop_sec, next_edl_record->length_sec);
2586 else if (next_edl_record->action == EDL_MUTE) {
2587 mpctx->edl_muted = !mpctx->edl_muted;
2588 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2589 mixer_mute(&mpctx->mixer);
2590 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2591 next_edl_record->start_sec );
2593 next_edl_record = next_edl_record->next;
2597 static void reinit_decoders(struct MPContext *mpctx)
2599 reinit_video_chain(mpctx);
2600 reinit_audio_chain(mpctx);
2601 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2604 static bool timeline_set_part(struct MPContext *mpctx, int i)
2606 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2607 struct timeline_part *n = mpctx->timeline + i;
2608 mpctx->timeline_part = i;
2609 mpctx->video_offset = n->start - n->source_start;
2610 if (n->source == p->source)
2611 return false;
2612 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2613 mpctx->demuxer = n->source->demuxer;
2614 mpctx->d_video = mpctx->demuxer->video;
2615 mpctx->d_audio = mpctx->demuxer->audio;
2616 mpctx->d_sub = mpctx->demuxer->sub;
2617 mpctx->sh_video = mpctx->d_video->sh;
2618 mpctx->sh_audio = mpctx->d_audio->sh;
2619 return true;
2622 // Given pts, switch playback to the corresponding part.
2623 // Return offset within that part.
2624 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2625 bool *need_reset)
2627 if (pts < 0)
2628 pts = 0;
2629 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2630 struct timeline_part *p = mpctx->timeline + i;
2631 if (pts < (p+1)->start) {
2632 *need_reset = timeline_set_part(mpctx, i);
2633 return pts - p->start + p->source_start;
2636 return -1;
2640 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2641 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2642 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2643 static int seek(MPContext *mpctx, double amount, int style)
2645 current_module = "seek";
2646 if (mpctx->stop_play == AT_END_OF_FILE)
2647 mpctx->stop_play = KEEP_PLAYING;
2648 if (mpctx->timeline && style & SEEK_FACTOR) {
2649 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2650 style &= ~SEEK_FACTOR;
2652 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2653 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2654 style |= SEEK_ABSOLUTE;
2655 if (amount > 0)
2656 style |= SEEK_FORWARD;
2657 else
2658 style |= SEEK_BACKWARD;
2659 amount += mpctx->sh_video->pts;
2662 /* At least the liba52 decoder wants to read from the input stream
2663 * during initialization, so reinit must be done after the demux_seek()
2664 * call that clears possible stream EOF. */
2665 bool need_reset = false;
2666 if (mpctx->timeline) {
2667 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2668 if (amount == -1) {
2669 mpctx->stop_play = AT_END_OF_FILE;
2670 // Clear audio from current position
2671 if (mpctx->sh_audio) {
2672 mpctx->audio_out->reset();
2673 mpctx->sh_audio->a_buffer_len = 0;
2674 mpctx->sh_audio->a_out_buffer_len = 0;
2676 return -1;
2679 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2680 if (need_reset)
2681 reinit_decoders(mpctx);
2682 if (seekresult == 0)
2683 return -1;
2685 if (mpctx->sh_video) {
2686 current_module = "seek_video_reset";
2687 resync_video_stream(mpctx->sh_video);
2688 vo_seek_reset(mpctx->video_out);
2689 mpctx->sh_video->num_buffered_pts = 0;
2690 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2691 mpctx->delay = 0;
2692 mpctx->time_frame = 0;
2693 mpctx->update_video_immediately = true;
2694 // Not all demuxers set d_video->pts during seek, so this value
2695 // (which is used by at least vobsub and edl code below) may
2696 // be completely wrong (probably 0).
2697 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2698 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2699 mpctx->sh_video->pts, mpctx->video_offset,
2700 mpctx->d_sub, 1);
2701 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2704 if (mpctx->sh_audio) {
2705 current_module = "seek_audio_reset";
2706 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2707 mpctx->sh_audio->a_buffer_len = 0;
2708 mpctx->sh_audio->a_out_buffer_len = 0;
2709 if (!mpctx->sh_video)
2710 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2711 mpctx->video_offset, mpctx->d_sub, 1);
2714 if (vo_vobsub && mpctx->sh_video) {
2715 current_module = "seek_vobsub_reset";
2716 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2719 edl_seek_reset(mpctx);
2721 mpctx->total_avsync_change = 0;
2722 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2723 drop_frame_cnt = 0;
2725 current_module = NULL;
2726 return 0;
2729 int get_current_chapter(struct MPContext *mpctx)
2731 if (!mpctx->chapters || !mpctx->sh_video)
2732 return demuxer_get_current_chapter(mpctx->demuxer);
2734 int i;
2735 double current_pts = mpctx->sh_video->pts;
2736 for (i = 1; i < mpctx->num_chapters; i++)
2737 if (current_pts < mpctx->chapters[i].start)
2738 break;
2739 return i - 1;
2742 // currently returns a string allocated with malloc, not talloc
2743 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2745 if (!mpctx->chapters || !mpctx->sh_video)
2746 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2747 return strdup(mpctx->chapters[chapter].name);
2750 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2751 char **chapter_name)
2753 if (!mpctx->chapters || !mpctx->sh_video)
2754 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2755 chapter_name);
2756 if (chapter >= mpctx->num_chapters)
2757 return -1;
2758 if (chapter < 0)
2759 chapter = 0;
2760 *seek_pts = mpctx->chapters[chapter].start;
2761 if (chapter_name)
2762 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2763 return chapter;
2766 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2767 struct content_source *sources,
2768 int num_sources,
2769 unsigned char uid_map[][16])
2771 int num_filenames = 0;
2772 char **filenames = NULL;
2773 if (num_sources > 1) {
2774 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2775 "other sources.\n");
2776 if (mpctx->stream->type != STREAMTYPE_FILE) {
2777 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2778 "normal disk file. Will not search for related files.\n");
2779 } else {
2780 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2781 "same directory to find referenced sources.\n");
2782 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2783 &num_filenames);
2787 int num_left = num_sources - 1;
2788 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2789 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2790 filename_recode(filenames[i]));
2791 int format;
2792 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2793 if (!s)
2794 continue;
2795 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2796 mpctx->opts.audio_id,
2797 mpctx->opts.video_id,
2798 mpctx->opts.sub_id, filenames[i]);
2799 if (!d) {
2800 free_stream(s);
2801 continue;
2803 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2804 for (int i = 1; i < num_sources; i++) {
2805 if (sources[i].demuxer)
2806 continue;
2807 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2808 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2809 i, filename_recode(d->filename));
2810 sources[i].stream = s;
2811 sources[i].demuxer = d;
2812 num_left--;
2813 goto match;
2817 free_demuxer(d);
2818 free_stream(s);
2819 continue;
2820 match:
2823 talloc_free(filenames);
2824 if (num_left) {
2825 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2826 "There will be parts MISSING from the video!\n");
2827 for (int i = 1, j = 1; i < num_sources; i++)
2828 if (sources[i].demuxer) {
2829 sources[j] = sources[i];
2830 memcpy(uid_map[j], uid_map[i], 16);
2831 j++;
2834 return num_sources - num_left;
2837 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2839 if (!mpctx->opts.ordered_chapters) {
2840 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2841 "you have disabled support for them. Ignoring.\n");
2842 return;
2845 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2846 "edit timeline.\n");
2848 struct demuxer *demuxer = mpctx->demuxer;
2849 struct matroska_data *m = &demuxer->matroska_data;
2851 // +1 because sources/uid_map[0] is original file even if all chapters
2852 // actually use other sources and need separate entries
2853 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2854 m->num_ordered_chapters+1);
2855 sources[0].stream = mpctx->stream;
2856 sources[0].demuxer = mpctx->demuxer;
2857 unsigned char uid_map[m->num_ordered_chapters+1][16];
2858 int num_sources = 1;
2859 memcpy(uid_map[0], m->segment_uid, 16);
2861 for (int i = 0; i < m->num_ordered_chapters; i++) {
2862 struct matroska_chapter *c = m->ordered_chapters + i;
2863 if (!c->has_segment_uid)
2864 memcpy(c->segment_uid, m->segment_uid, 16);
2866 for (int j = 0; j < num_sources; j++)
2867 if (!memcmp(c->segment_uid, uid_map[j], 16))
2868 goto found1;
2869 memcpy(uid_map[num_sources], c->segment_uid, 16);
2870 sources[num_sources] = (struct content_source){};
2871 num_sources++;
2872 found1:
2876 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2877 uid_map);
2880 // +1 for terminating chapter with start time marking end of last real one
2881 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2882 m->num_ordered_chapters + 1);
2883 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2884 m->num_ordered_chapters);
2885 uint64_t starttime = 0;
2886 uint64_t missing_time = 0;
2887 int part_count = 0;
2888 int num_chapters = 0;
2889 uint64_t prev_part_offset;
2890 for (int i = 0; i < m->num_ordered_chapters; i++) {
2891 struct matroska_chapter *c = m->ordered_chapters + i;
2893 int j;
2894 for (j = 0; j < num_sources; j++) {
2895 if (!memcmp(c->segment_uid, uid_map[j], 16))
2896 goto found2;
2898 missing_time += c->end - c->start;
2899 continue;
2900 found2:;
2901 chapters[num_chapters].start = starttime / 1000.;
2902 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2903 /* Only add a separate part if the time or file actually changes.
2904 * Matroska files have chapter divisions that are redundant from
2905 * timeline point of view because the same chapter structure is used
2906 * both to specify the timeline and for normal chapter information.
2907 * Removing a missing inserted external chapter can also cause this. */
2908 if (part_count == 0 || c->start != starttime + prev_part_offset
2909 || sources + j != timeline[part_count - 1].source) {
2910 timeline[part_count].source = sources + j;
2911 timeline[part_count].start = chapters[num_chapters].start;
2912 timeline[part_count].source_start = c->start / 1000.;
2913 prev_part_offset = c->start - starttime;
2914 part_count++;
2916 starttime += c->end - c->start;
2917 num_chapters++;
2919 timeline[part_count].start = starttime / 1000.;
2921 if (!part_count) {
2922 // None of the parts come from the file itself???
2923 talloc_free(sources);
2924 talloc_free(timeline);
2925 talloc_free(chapters);
2926 return;
2929 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2930 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2931 timeline[part_count].start);
2932 if (missing_time)
2933 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2934 "from the timeline!\n", missing_time / 1000.);
2935 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2936 for (int i = 0; i < num_sources; i++)
2937 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2938 filename_recode(sources[i].demuxer->filename));
2939 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2940 "source_start, source):\n");
2941 for (int i = 0; i < part_count; i++) {
2942 struct timeline_part *p = timeline + i;
2943 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2944 p->source_start, p->source - sources);
2946 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2947 mpctx->sources = sources;
2948 mpctx->num_sources = num_sources;
2949 mpctx->timeline = timeline;
2950 mpctx->num_timeline_parts = part_count;
2951 mpctx->num_chapters = num_chapters;
2952 mpctx->chapters = chapters;
2954 mpctx->timeline_part = 0;
2955 mpctx->demuxer = timeline[0].source->demuxer;
2959 static int read_keys(void *ctx, int fd)
2961 getch2(ctx);
2962 return mplayer_get_key(ctx, 0);
2966 #ifdef PTW32_STATIC_LIB
2967 static void detach_ptw32(void)
2969 pthread_win32_thread_detach_np();
2970 pthread_win32_process_detach_np();
2972 #endif
2974 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2975 * file for some tools to link against. */
2976 #ifndef DISABLE_MAIN
2977 int main(int argc,char* argv[]){
2980 char * mem_ptr;
2982 // movie info:
2984 /* Flag indicating whether MPlayer should exit without playing anything. */
2985 int opt_exit = 0;
2987 //float a_frame=0; // Audio
2989 int i;
2991 struct MPContext *mpctx = &(struct MPContext){
2992 .osd_function = OSD_PLAY,
2993 .begin_skip = MP_NOPTS_VALUE,
2994 .play_tree_step = 1,
2995 .global_sub_pos = -1,
2996 .set_of_sub_pos = -1,
2997 .file_format = DEMUXER_TYPE_UNKNOWN,
2998 .last_dvb_step = 1,
3001 #ifdef PTW32_STATIC_LIB
3002 pthread_win32_process_attach_np();
3003 pthread_win32_thread_attach_np();
3004 atexit(detach_ptw32);
3005 #endif
3007 InitTimer();
3008 srand(GetTimerMS());
3010 mp_msg_init();
3011 set_av_log_callback();
3013 #ifdef CONFIG_X11
3014 mpctx->x11_state = vo_x11_init_state();
3015 #endif
3016 struct MPOpts *opts = &mpctx->opts;
3017 set_default_mplayer_options(opts);
3018 // Create the config context and register the options
3019 mpctx->mconfig = m_config_new(opts, cfg_include);
3020 m_config_register_options(mpctx->mconfig,mplayer_opts);
3021 mp_input_register_options(mpctx->mconfig);
3023 // Preparse the command line
3024 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3026 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3027 set_path_env();
3028 #endif
3030 #ifdef CONFIG_TV
3031 stream_tv_defaults.immediate = 1;
3032 #endif
3034 parse_cfgfiles(mpctx, mpctx->mconfig);
3036 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3037 if(mpctx->playtree == NULL)
3038 opt_exit = 1;
3039 else {
3040 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3041 if(mpctx->playtree) {
3042 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3043 if(mpctx->playtree_iter) {
3044 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3045 play_tree_iter_free(mpctx->playtree_iter);
3046 mpctx->playtree_iter = NULL;
3048 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3052 mpctx->key_fifo = mp_fifo_create(opts);
3054 print_version("MPlayer");
3056 #if defined(__MINGW32__) || defined(__CYGWIN__)
3058 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3059 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3060 if (kernel32)
3061 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3062 if (setDEP) setDEP(3);
3064 // stop Windows from showing all kinds of annoying error dialogs
3065 SetErrorMode(0x8003);
3066 // request 1ms timer resolution
3067 timeBeginPeriod(1);
3068 #endif
3070 #ifdef CONFIG_PRIORITY
3071 set_priority();
3072 #endif
3074 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3075 list_video_out();
3076 opt_exit = 1;
3079 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3080 list_audio_out();
3081 opt_exit = 1;
3084 /* Check codecs.conf. */
3085 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3086 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3087 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3088 if(!parse_codec_cfg(NULL)){
3089 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3091 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3094 free( mem_ptr ); // release the buffer created by get_path()
3097 #if 0
3098 if(video_codec_list){
3099 int i;
3100 video_codec=video_codec_list[0];
3101 for(i=0;video_codec_list[i];i++)
3102 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3104 #endif
3105 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3106 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3107 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3108 list_codecs(1);
3109 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3110 opt_exit = 1;
3112 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3113 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3114 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3115 list_codecs(0);
3116 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3117 opt_exit = 1;
3119 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3120 vfm_help();
3121 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3122 opt_exit = 1;
3124 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3125 afm_help();
3126 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3127 opt_exit = 1;
3129 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3130 af_help();
3131 printf("\n");
3132 opt_exit = 1;
3134 #ifdef CONFIG_X11
3135 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3136 fstype_help();
3137 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3138 opt_exit = 1;
3140 #endif
3141 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3142 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3143 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3144 demuxer_help();
3145 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3146 opt_exit = 1;
3148 if(list_properties) {
3149 property_print_help();
3150 opt_exit = 1;
3153 if(opt_exit)
3154 exit_player(mpctx, EXIT_NONE);
3156 if(!mpctx->filename && !player_idle_mode){
3157 // no file/vcd/dvd -> show HELP:
3158 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3159 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3162 /* Display what configure line was used */
3163 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3165 // Many users forget to include command line in bugreports...
3166 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3167 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3168 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3169 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3172 //------ load global data first ------
3174 mpctx->osd = osd_create();
3176 // check font
3177 #ifdef CONFIG_FREETYPE
3178 init_freetype();
3179 #endif
3180 #ifdef CONFIG_FONTCONFIG
3181 if(font_fontconfig <= 0)
3183 #endif
3184 #ifdef CONFIG_BITMAP_FONT
3185 if(font_name){
3186 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3187 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3188 filename_recode(font_name));
3189 } else {
3190 // try default:
3191 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3192 free(mem_ptr); // release the buffer created by get_path()
3193 if(!vo_font)
3194 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3196 if (sub_font_name)
3197 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3198 else
3199 mpctx->osd->sub_font = vo_font;
3200 #endif
3201 #ifdef CONFIG_FONTCONFIG
3203 #endif
3205 #ifdef CONFIG_ASS
3206 ass_library = ass_init();
3207 #endif
3209 #ifdef HAVE_RTC
3210 if(!nortc)
3212 // seteuid(0); /* Can't hurt to try to get root here */
3213 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3214 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3215 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3216 else {
3217 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3219 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3220 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3221 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3222 close (rtc_fd);
3223 rtc_fd = -1;
3224 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3225 /* variable only by the root */
3226 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3227 close (rtc_fd);
3228 rtc_fd = -1;
3229 } else
3230 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3233 if(rtc_fd<0)
3234 #endif /* HAVE_RTC */
3235 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3236 softsleep?"software":timer_name);
3238 #ifdef HAVE_TERMCAP
3239 load_termcap(NULL); // load key-codes
3240 #endif
3242 // ========== Init keyboard FIFO (connection to libvo) ============
3244 // Init input system
3245 current_module = "init_input";
3246 mpctx->input = mp_input_init(&opts->input);
3247 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3248 if(slave_mode)
3249 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3250 else if(!noconsolecontrols)
3251 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3252 // Set the libstream interrupt callback
3253 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3255 #ifdef CONFIG_MENU
3256 if(use_menu) {
3257 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3258 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3259 else {
3260 menu_cfg = get_path("menu.conf");
3261 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3262 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3263 else {
3264 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3265 MPLAYER_CONFDIR "/menu.conf"))
3266 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3267 else {
3268 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3269 use_menu = 0;
3274 #endif
3276 current_module = NULL;
3278 /// Catch signals
3279 #ifndef __MINGW32__
3280 signal(SIGCHLD,child_sighandler);
3281 #endif
3283 #ifdef CONFIG_CRASH_DEBUG
3284 prog_path = argv[0];
3285 #endif
3286 //========= Catch terminate signals: ================
3287 // terminate requests:
3288 signal(SIGTERM,exit_sighandler); // kill
3289 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3291 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3293 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3294 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3295 #ifdef CONFIG_SIGHANDLER
3296 // fatal errors:
3297 signal(SIGBUS,exit_sighandler); // bus error
3298 signal(SIGSEGV,exit_sighandler); // segfault
3299 signal(SIGILL,exit_sighandler); // illegal instruction
3300 signal(SIGFPE,exit_sighandler); // floating point exc.
3301 signal(SIGABRT,exit_sighandler); // abort()
3302 #ifdef CONFIG_CRASH_DEBUG
3303 if (crash_debug)
3304 signal(SIGTRAP,exit_sighandler);
3305 #endif
3306 #endif
3308 // ******************* Now, let's see the per-file stuff ********************
3310 play_next_file:
3312 // init global sub numbers
3313 mpctx->global_sub_size = 0;
3314 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3316 if (mpctx->filename) {
3317 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3318 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3319 load_per_file_config (mpctx->mconfig, mpctx->filename);
3322 if (opts->video_driver_list)
3323 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3324 if (opts->audio_driver_list)
3325 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3327 // We must enable getch2 here to be able to interrupt network connection
3328 // or cache filling
3329 if(!noconsolecontrols && !slave_mode){
3330 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3331 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3332 else
3333 getch2_enable(); // prepare stdin for hotkeys...
3334 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3335 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3338 // =================== GUI idle loop (STOP state) ===========================
3339 while (player_idle_mode && !mpctx->filename) {
3340 play_tree_t * entry = NULL;
3341 mp_cmd_t * cmd;
3342 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3343 if (mpctx->video_out)
3344 vo_check_events(mpctx->video_out);
3345 usec_sleep(20000);
3347 switch (cmd->id) {
3348 case MP_CMD_LOADFILE:
3349 // prepare a tree entry with the new filename
3350 entry = play_tree_new();
3351 play_tree_add_file(entry, cmd->args[0].v.s);
3352 // The entry is added to the main playtree after the switch().
3353 break;
3354 case MP_CMD_LOADLIST:
3355 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3356 break;
3357 case MP_CMD_QUIT:
3358 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3359 break;
3360 case MP_CMD_GET_PROPERTY:
3361 case MP_CMD_SET_PROPERTY:
3362 case MP_CMD_STEP_PROPERTY:
3363 run_command(mpctx, cmd);
3364 break;
3367 mp_cmd_free(cmd);
3369 if (entry) { // user entered a command that gave a valid entry
3370 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3371 play_tree_free_list(mpctx->playtree->child, 1);
3372 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3374 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3376 play_tree_set_child(mpctx->playtree, entry);
3378 /* Make iterator start at the top the of tree. */
3379 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3380 if (!mpctx->playtree_iter) continue;
3382 // find the first real item in the tree
3383 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3384 // no items!
3385 play_tree_iter_free(mpctx->playtree_iter);
3386 mpctx->playtree_iter = NULL;
3387 continue; // wait for next command
3389 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3392 //---------------------------------------------------------------------------
3394 if (mpctx->filename) {
3395 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3396 filename_recode(mpctx->filename));
3397 if(use_filename_title && vo_wintitle == NULL)
3398 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3401 if (edl_filename) {
3402 if (edl_records) free_edl(edl_records);
3403 next_edl_record = edl_records = edl_parse_file();
3405 if (edl_output_filename) {
3406 if (edl_fd) fclose(edl_fd);
3407 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3409 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3410 filename_recode(edl_output_filename));
3414 //==================== Open VOB-Sub ============================
3416 current_module="vobsub";
3417 if (vobsub_name){
3418 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3419 if(vo_vobsub==NULL)
3420 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3421 filename_recode(vobsub_name));
3422 } else if (sub_auto && mpctx->filename){
3423 /* try to autodetect vobsub from movie filename ::atmos */
3424 char *buf = strdup(mpctx->filename), *psub;
3425 char *pdot = strrchr(buf, '.');
3426 char *pslash = strrchr(buf, '/');
3427 #if defined(__MINGW32__) || defined(__CYGWIN__)
3428 if (!pslash) pslash = strrchr(buf, '\\');
3429 #endif
3430 if (pdot && (!pslash || pdot > pslash))
3431 *pdot = '\0';
3432 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3433 /* try from ~/.mplayer/sub */
3434 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3435 char *bname;
3436 int l;
3437 bname = strrchr(buf,'/');
3438 #if defined(__MINGW32__) || defined(__CYGWIN__)
3439 if(!bname) bname = strrchr(buf,'\\');
3440 #endif
3441 if(bname) bname++;
3442 else bname = buf;
3443 l = strlen(psub) + strlen(bname) + 1;
3444 psub = realloc(psub,l);
3445 strcat(psub,bname);
3446 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3447 free(psub);
3449 free(buf);
3451 if(vo_vobsub){
3452 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3453 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3454 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3456 // setup global sub numbering
3457 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3458 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3461 //============ Open & Sync STREAM --- fork cache2 ====================
3463 mpctx->stream=NULL;
3464 mpctx->demuxer=NULL;
3465 if (mpctx->d_audio) {
3466 //free_demuxer_stream(mpctx->d_audio);
3467 mpctx->d_audio=NULL;
3469 if (mpctx->d_video) {
3470 //free_demuxer_stream(d_video);
3471 mpctx->d_video=NULL;
3473 mpctx->sh_audio=NULL;
3474 mpctx->sh_video=NULL;
3476 current_module="open_stream";
3477 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3478 if(!mpctx->stream) { // error...
3479 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3480 goto goto_next_file;
3482 mpctx->initialized_flags|=INITIALIZED_STREAM;
3484 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3485 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3486 "playlist support will not be used automatically.\n"
3487 "MPlayer's playlist code is unsafe and should only be used with "
3488 "trusted sources.\nPlayback will probably fail.\n\n");
3489 #if 0
3490 play_tree_t* entry;
3491 // Handle playlist
3492 current_module="handle_playlist";
3493 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3494 filename_recode(mpctx->filename));
3495 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3496 mpctx->eof=playtree_add_playlist(mpctx, entry);
3497 goto goto_next_file;
3498 #endif
3500 mpctx->stream->start_pos+=seek_to_byte;
3502 if(stream_dump_type==5){
3503 unsigned char buf[4096];
3504 int len;
3505 FILE *f;
3506 current_module="dumpstream";
3507 stream_reset(mpctx->stream);
3508 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3509 f=fopen(stream_dump_name,"wb");
3510 if(!f){
3511 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3512 exit_player(mpctx, EXIT_ERROR);
3514 if (dvd_chapter > 1) {
3515 int chapter = dvd_chapter - 1;
3516 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3518 while(!mpctx->stream->eof && !async_quit_request){
3519 len=stream_read(mpctx->stream,buf,4096);
3520 if(len>0) {
3521 if(fwrite(buf,len,1,f) != 1) {
3522 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3523 exit_player(mpctx, EXIT_ERROR);
3526 if(dvd_last_chapter > 0) {
3527 int chapter = -1;
3528 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3529 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3530 break;
3533 if(fclose(f)) {
3534 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3535 exit_player(mpctx, EXIT_ERROR);
3537 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3538 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3541 #ifdef CONFIG_DVDREAD
3542 if(mpctx->stream->type==STREAMTYPE_DVD){
3543 current_module="dvd lang->id";
3544 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3545 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3546 // setup global sub numbering
3547 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3548 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3549 current_module=NULL;
3551 #endif
3553 #ifdef CONFIG_DVDNAV
3554 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3555 current_module="dvdnav lang->id";
3556 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3557 dvdsub_lang_id = -3;
3558 if(dvdsub_lang && opts->sub_id==-1)
3559 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3560 // setup global sub numbering
3561 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3562 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3563 current_module=NULL;
3565 #endif
3567 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3568 goto_enable_cache:
3569 if(stream_cache_size>0){
3570 int res;
3571 current_module="enable_cache";
3572 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3573 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3574 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3575 if(res == 0)
3576 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3579 //============ Open DEMUXERS --- DETECT file type =======================
3580 current_module="demux_open";
3582 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3584 // HACK to get MOV Reference Files working
3586 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3588 unsigned char* playlist_entry;
3589 play_tree_t *list = NULL, *entry = NULL;
3591 current_module="handle_demux_playlist";
3592 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3594 char *temp, *bname;
3596 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3597 filename_recode(playlist_entry));
3599 bname=mp_basename(playlist_entry);
3600 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3601 continue;
3603 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3604 continue;
3606 entry = play_tree_new();
3608 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3610 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3611 if (temp)
3613 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3614 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3615 strcat(temp, playlist_entry);
3616 if (!strcmp(temp, mpctx->filename)) {
3617 free(temp);
3618 continue;
3620 play_tree_add_file(entry,temp);
3621 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3622 free(temp);
3625 else
3626 play_tree_add_file(entry,playlist_entry);
3628 if(!list)
3629 list = entry;
3630 else
3631 play_tree_append_entry(list,entry);
3633 free_demuxer(mpctx->demuxer);
3634 mpctx->demuxer = NULL;
3636 if (list)
3638 entry = play_tree_new();
3639 play_tree_set_child(entry,list);
3640 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3641 goto goto_next_file;
3645 if(!mpctx->demuxer)
3646 goto goto_next_file;
3648 if (mpctx->demuxer->matroska_data.ordered_chapters)
3649 build_ordered_chapter_timeline(mpctx);
3651 if (!mpctx->sources) {
3652 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3653 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3654 .demuxer = mpctx->demuxer};
3655 mpctx->num_sources = 1;
3658 if(dvd_chapter>1) {
3659 double pts;
3660 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3661 seek(mpctx, pts, SEEK_ABSOLUTE);
3664 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3666 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3667 int i;
3668 int maxid = -1;
3669 // setup global sub numbering
3670 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3671 for (i = 0; i < MAX_S_STREAMS; i++)
3672 if (mpctx->demuxer->s_streams[i])
3673 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3674 mpctx->global_sub_size += maxid + 1;
3676 // Make opts->sub_id always selectable if set.
3677 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3678 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3680 #ifdef CONFIG_ASS
3681 if (opts->ass_enabled && ass_library && use_embedded_fonts) {
3682 for (int j = 0; j < mpctx->num_sources; j++) {
3683 struct demuxer *d = mpctx->sources[j].demuxer;
3684 for (int i = 0; i < d->num_attachments; i++) {
3685 struct demux_attachment *att = d->attachments + i;
3686 if (att->name && att->type && att->data && att->data_size
3687 && (strcmp(att->type, "application/x-truetype-font") == 0
3688 || strcmp(att->type, "application/x-font") == 0))
3689 ass_add_font(ass_library, att->name, att->data, att->data_size);
3693 #endif
3695 current_module="demux_open2";
3697 //file_format=demuxer->file_format;
3699 mpctx->d_audio=mpctx->demuxer->audio;
3700 mpctx->d_video=mpctx->demuxer->video;
3701 mpctx->d_sub=mpctx->demuxer->sub;
3703 if (ts_prog) {
3704 int tmp = ts_prog;
3705 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3707 // select audio stream
3708 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3710 // DUMP STREAMS:
3711 if((stream_dump_type)&&(stream_dump_type<4)){
3712 FILE *f;
3713 demux_stream_t *ds=NULL;
3714 current_module="dump";
3715 // select stream to dump
3716 switch(stream_dump_type){
3717 case 1: ds=mpctx->d_audio;break;
3718 case 2: ds=mpctx->d_video;break;
3719 case 3: ds=mpctx->d_sub;break;
3721 if(!ds){
3722 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3723 exit_player(mpctx, EXIT_ERROR);
3725 // disable other streams:
3726 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3727 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3728 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3729 // let's dump it!
3730 f=fopen(stream_dump_name,"wb");
3731 if(!f){
3732 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3733 exit_player(mpctx, EXIT_ERROR);
3735 while(!ds->eof){
3736 unsigned char* start;
3737 int in_size=ds_get_packet(ds,&start);
3738 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3739 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3740 if(in_size>0) fwrite(start,in_size,1,f);
3741 if(dvd_last_chapter>0) {
3742 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3743 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3744 break;
3747 fclose(f);
3748 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3749 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3752 mpctx->sh_audio=mpctx->d_audio->sh;
3753 mpctx->sh_video=mpctx->d_video->sh;
3755 if(mpctx->sh_video){
3757 current_module="video_read_properties";
3758 if(!video_read_properties(mpctx->sh_video)) {
3759 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3760 mpctx->sh_video=mpctx->d_video->sh=NULL;
3761 } else {
3762 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3763 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3764 mpctx->sh_video->fps,mpctx->sh_video->frametime
3767 /* need to set fps here for output encoders to pick it up in their init */
3768 if(force_fps){
3769 mpctx->sh_video->fps=force_fps;
3770 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3772 vo_fps = mpctx->sh_video->fps;
3774 if(!mpctx->sh_video->fps && !force_fps){
3775 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3776 mpctx->sh_video=mpctx->d_video->sh=NULL;
3782 if(!mpctx->sh_video && !mpctx->sh_audio){
3783 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3784 #ifdef CONFIG_DVBIN
3785 if(mpctx->stream->type == STREAMTYPE_DVB)
3787 int dir;
3788 int v = mpctx->last_dvb_step;
3789 if(v > 0)
3790 dir = DVB_CHANNEL_HIGHER;
3791 else
3792 dir = DVB_CHANNEL_LOWER;
3794 if(dvb_step_channel(mpctx->stream, dir)) {
3795 mpctx->stop_play = PT_NEXT_ENTRY;
3796 mpctx->dvbin_reopen = 1;
3799 #endif
3800 goto goto_next_file; // exit_player(_("Fatal error"));
3803 /* display clip info */
3804 demux_info_print(mpctx->demuxer);
3806 //================== Read SUBTITLES (DVD & TEXT) ==========================
3807 if(vo_spudec==NULL && mpctx->sh_video &&
3808 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3809 init_vo_spudec(mpctx);
3812 // after reading video params we should load subtitles because
3813 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3814 // check .sub
3815 current_module="read_subtitles_file";
3816 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3817 if(sub_name){
3818 for (i = 0; sub_name[i] != NULL; ++i)
3819 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3821 if(sub_auto) { // auto load sub file ...
3822 char *psub = get_path( "sub/" );
3823 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3824 int i = 0;
3825 free(psub); // release the buffer created by get_path() above
3826 while (tmp[i]) {
3827 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3828 free(tmp[i++]);
3830 free(tmp);
3832 if (mpctx->set_of_sub_size > 0) {
3833 // setup global sub numbering
3834 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3835 mpctx->global_sub_size += mpctx->set_of_sub_size;
3839 if (mpctx->global_sub_size) {
3840 select_subtitle(mpctx);
3841 if(subdata)
3842 switch (stream_dump_type) {
3843 case 3: list_sub_file(subdata); break;
3844 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3845 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3846 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3847 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3848 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3852 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3853 filename_recode(mpctx->filename));
3854 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3855 if (mpctx->sh_video) {
3856 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3857 if (mpctx->sh_video->format >= 0x20202020)
3858 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3859 else
3860 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3861 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3862 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3863 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3864 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3865 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3867 if (mpctx->sh_audio) {
3868 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3869 if (mpctx->sh_audio->format >= 0x20202020)
3870 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3871 else
3872 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3873 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3874 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3875 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3877 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3878 mpctx->timeline[mpctx->num_timeline_parts].start :
3879 demuxer_get_time_length(mpctx->demuxer));
3880 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3881 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3882 if (mpctx->demuxer) {
3883 if (mpctx->demuxer->num_chapters == 0)
3884 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3885 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3888 if(!mpctx->sh_video) goto main; // audio-only
3890 if(!reinit_video_chain(mpctx)) {
3891 if(!mpctx->sh_video){
3892 if(!mpctx->sh_audio) goto goto_next_file;
3893 goto main; // exit_player(_("Fatal error"));
3897 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3898 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3900 #ifdef CONFIG_FREETYPE
3901 force_load_font = 1;
3902 #endif
3904 //================== MAIN: ==========================
3905 main:
3906 current_module="main";
3908 if(playing_msg) {
3909 char* msg = property_expand_string(mpctx, playing_msg);
3910 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3911 free(msg);
3915 // Disable the term OSD in verbose mode
3916 if(verbose) term_osd = 0;
3919 //int frame_corr_num=0; //
3920 //float v_frame=0; // Video
3921 //float num_frames=0; // number of frames played
3923 int frame_time_remaining=0; // flag
3924 int blit_frame=0;
3926 // Make sure old OSD does not stay around,
3927 // e.g. with -fixed-vo and same-resolution files
3928 clear_osd_msgs();
3929 update_osd_msg(mpctx);
3931 //================ SETUP AUDIO ==========================
3933 if(mpctx->sh_audio){
3934 reinit_audio_chain(mpctx);
3935 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3936 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3939 current_module="av_init";
3941 if(mpctx->sh_video){
3942 mpctx->sh_video->timer=0;
3943 if (! ignore_start)
3944 audio_delay += mpctx->sh_video->stream_delay;
3946 if(mpctx->sh_audio){
3947 if (start_volume >= 0)
3948 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3949 if (! ignore_start)
3950 audio_delay -= mpctx->sh_audio->stream_delay;
3951 mpctx->delay=-audio_delay;
3954 if(!mpctx->sh_audio){
3955 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3956 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3957 ds_free_packs(mpctx->d_audio); // free buffered chunks
3958 //mpctx->d_audio->id=-2; // do not read audio chunks
3959 //uninit_player(mpctx, INITIALIZED_AO); // close device
3961 if(!mpctx->sh_video){
3962 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3963 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3964 ds_free_packs(mpctx->d_video);
3965 mpctx->d_video->id=-2;
3966 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3969 if (!mpctx->sh_video && !mpctx->sh_audio)
3970 goto goto_next_file;
3972 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3973 if(force_fps && mpctx->sh_video){
3974 vo_fps = mpctx->sh_video->fps=force_fps;
3975 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3976 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3979 mp_input_set_section(mpctx->input, NULL);
3980 //TODO: add desired (stream-based) sections here
3981 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3982 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3984 //==================== START PLAYING =======================
3986 if(opts->loop_times>1) opts->loop_times--; else
3987 if(opts->loop_times==1) opts->loop_times = -1;
3989 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3991 total_time_usage_start=GetTimer();
3992 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3993 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3994 play_n_frames=play_n_frames_mf;
3996 if(play_n_frames==0){
3997 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4000 // If there's a timeline force an absolute seek to initialize state
4001 if (seek_to_sec || mpctx->timeline) {
4002 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4003 end_at.pos += seek_to_sec;
4006 if (end_at.type == END_AT_SIZE) {
4007 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4008 end_at.type = END_AT_NONE;
4011 #ifdef CONFIG_DVDNAV
4012 mp_dvdnav_context_free(mpctx);
4013 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4014 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4015 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4017 #endif
4019 get_relative_time(mpctx); // reset current delta
4020 mpctx->time_frame = 0;
4021 mpctx->drop_message_shown = 0;
4022 mpctx->update_video_immediately = true;
4023 mpctx->total_avsync_change = 0;
4024 // Make sure VO knows current pause state
4025 if (mpctx->sh_video)
4026 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4027 NULL);
4029 while(!mpctx->stop_play){
4030 float aq_sleep_time=0;
4032 if(dvd_last_chapter>0) {
4033 int cur_chapter = get_current_chapter(mpctx);
4034 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4035 goto goto_next_file;
4038 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4039 mpctx->sh_audio = mpctx->d_audio->sh;
4040 mpctx->sh_audio->ds = mpctx->d_audio;
4041 reinit_audio_chain(mpctx);
4044 /*========================== PLAY AUDIO ============================*/
4046 if (mpctx->sh_audio && !mpctx->paused)
4047 if (!fill_audio_out_buffers(mpctx))
4048 // at eof, all audio at least written to ao
4049 if (!mpctx->sh_video)
4050 mpctx->stop_play = AT_END_OF_FILE;
4053 if(!mpctx->sh_video) {
4054 // handle audio-only case:
4055 double a_pos=0;
4056 // sh_audio can be NULL due to video stream switching
4057 // TODO: handle this better
4058 if (mpctx->sh_audio)
4059 a_pos = playing_audio_pts(mpctx);
4061 print_status(mpctx, a_pos, false);
4063 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4064 mpctx->stop_play = PT_NEXT_ENTRY;
4065 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4066 mpctx->d_sub, 0);
4067 update_osd_msg(mpctx);
4069 } else {
4071 /*========================== PLAY VIDEO ============================*/
4073 vo_pts=mpctx->sh_video->timer*90000.0;
4074 vo_fps=mpctx->sh_video->fps;
4076 blit_frame = mpctx->video_out->frame_loaded;
4077 if (!blit_frame) {
4078 double frame_time = update_video(mpctx);
4079 blit_frame = mpctx->video_out->frame_loaded;
4080 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4081 if (mpctx->sh_video->vf_initialized < 0) {
4082 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4083 mpctx->stop_play = PT_NEXT_ENTRY;
4084 goto goto_next_file;
4086 if (blit_frame) {
4087 struct sh_video *sh_video = mpctx->sh_video;
4088 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4089 mpctx->video_offset, mpctx->d_sub, 0);
4090 update_teletext(sh_video, mpctx->demuxer, 0);
4091 update_osd_msg(mpctx);
4092 struct vf_instance *vf = mpctx->sh_video->vfilter;
4093 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4094 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4095 vo_osd_changed(0);
4097 if (frame_time < 0)
4098 mpctx->stop_play = AT_END_OF_FILE;
4099 else {
4100 if (mpctx->update_video_immediately) {
4101 // Show this frame immediately, rest normally
4102 mpctx->update_video_immediately = false;
4103 } else {
4104 mpctx->time_frame += frame_time / opts->playback_speed;
4105 adjust_sync(mpctx, frame_time);
4109 if (mpctx->timeline) {
4110 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4111 if (mpctx->sh_video->pts >= next->start
4112 || mpctx->stop_play == AT_END_OF_FILE
4113 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4114 seek(mpctx, next->start, SEEK_ABSOLUTE);
4115 continue;
4119 // ==========================================================================
4121 // current_module="draw_osd";
4122 // if(vo_config_count) mpctx->video_out->draw_osd();
4124 current_module="vo_check_events";
4125 vo_check_events(mpctx->video_out);
4127 #ifdef CONFIG_X11
4128 if (stop_xscreensaver) {
4129 current_module = "stop_xscreensaver";
4130 xscreensaver_heartbeat(mpctx->x11_state);
4132 #elif _WIN32
4133 if (stop_screensaver) {
4134 current_module = "stop_screensaver";
4135 SetThreadExecutionState(ES_DISPLAY_REQUIRED);
4137 #endif
4138 if (heartbeat_cmd) {
4139 static unsigned last_heartbeat;
4140 unsigned now = GetTimerMS();
4141 if (now - last_heartbeat > 30000) {
4142 last_heartbeat = now;
4143 system(heartbeat_cmd);
4147 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4149 //====================== FLIP PAGE (VIDEO BLT): =========================
4151 current_module="flip_page";
4152 if (!frame_time_remaining && blit_frame) {
4153 unsigned int t2=GetTimer();
4154 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4155 int duration = -1;
4156 double pts2 = mpctx->video_out->next_pts2;
4157 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4158 // expected A/V sync correction is ignored
4159 double diff = (pts2 - mpctx->sh_video->pts);
4160 diff /= opts->playback_speed;
4161 if (mpctx->time_frame < 0)
4162 diff += mpctx->time_frame;
4163 if (diff < 0)
4164 diff = 0;
4165 if (diff > 10)
4166 diff = 10;
4167 duration = diff * 1e6;
4169 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4171 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4172 vout_time_usage += mpctx->last_vo_flip_duration;
4173 if (mpctx->video_out->driver->flip_page_timed) {
4174 // No need to adjust sync based on flip speed
4175 mpctx->last_vo_flip_duration = 0;
4176 // For print_status - VO call finishing early is OK for sync
4177 mpctx->time_frame -= get_relative_time(mpctx);
4179 print_status(mpctx, MP_NOPTS_VALUE, true);
4181 else
4182 print_status(mpctx, MP_NOPTS_VALUE, false);
4184 //============================ Auto QUALITY ============================
4186 /*Output quality adjustments:*/
4187 if(auto_quality>0){
4188 current_module="autoq";
4189 // float total=0.000001f * (GetTimer()-aq_total_time);
4190 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4191 if(output_quality<auto_quality && aq_sleep_time>0)
4192 ++output_quality;
4193 else
4194 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4195 if(output_quality>1 && aq_sleep_time<0)
4196 --output_quality;
4197 else
4198 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4199 output_quality=0;
4200 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4201 set_video_quality(mpctx->sh_video,output_quality);
4204 if (!frame_time_remaining && blit_frame) {
4205 if (play_n_frames >= 0) {
4206 --play_n_frames;
4207 if (play_n_frames <= 0)
4208 mpctx->stop_play = PT_NEXT_ENTRY;
4210 if (mpctx->step_frames > 0) {
4211 mpctx->step_frames--;
4212 if (mpctx->step_frames == 0)
4213 pause_player(mpctx);
4218 // FIXME: add size based support for -endpos
4219 if (end_at.type == END_AT_TIME &&
4220 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4221 mpctx->stop_play = PT_NEXT_ENTRY;
4223 } // end if(mpctx->sh_video)
4225 #ifdef CONFIG_DVDNAV
4226 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4227 nav_highlight_t hl;
4228 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4229 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4230 vo_osd_changed (OSDTYPE_DVDNAV);
4232 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4233 double ar = -1.0;
4234 if (stream_control (mpctx->demuxer->stream,
4235 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4236 != STREAM_UNSUPPORTED)
4237 mpctx->sh_video->stream_aspect = ar;
4240 #endif
4242 //================= Keyboard events, SEEKing ====================
4244 current_module="key_events";
4247 while (1) {
4248 mp_cmd_t* cmd;
4249 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4250 run_command(mpctx, cmd);
4251 mp_cmd_free(cmd);
4252 if (mpctx->stop_play)
4253 break;
4255 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4256 || mpctx->abs_seek_pos)
4257 break;
4258 if (mpctx->sh_video) {
4259 update_osd_msg(mpctx);
4260 int hack = vo_osd_changed(0);
4261 vo_osd_changed(hack);
4262 if (hack)
4263 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4264 add_step_frame(mpctx);
4265 break;
4267 else
4268 vo_osd_changed(0);
4270 pause_loop(mpctx);
4274 // handle -sstep
4275 if (step_sec > 0 && !mpctx->paused) {
4276 mpctx->osd_function=OSD_FFW;
4277 mpctx->rel_seek_secs+=step_sec;
4280 edl_update(mpctx);
4282 /* Looping. */
4283 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4284 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4286 if(opts->loop_times>1) opts->loop_times--; else
4287 if(opts->loop_times==1) opts->loop_times=-1;
4288 play_n_frames=play_n_frames_mf;
4289 mpctx->stop_play=0;
4290 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4293 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4294 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4296 mpctx->rel_seek_secs=0;
4297 mpctx->abs_seek_pos=0;
4300 } // while(!mpctx->stop_play)
4302 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4304 #ifdef CONFIG_DVBIN
4305 if(mpctx->dvbin_reopen)
4307 mpctx->stop_play = 0;
4308 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4309 cache_uninit(mpctx->stream);
4310 mpctx->dvbin_reopen = 0;
4311 goto goto_enable_cache;
4313 #endif
4316 goto_next_file: // don't jump here after ao/vo/getch initialization!
4318 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4320 if(benchmark){
4321 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4322 double total_time_usage;
4323 total_time_usage_start=GetTimer()-total_time_usage_start;
4324 total_time_usage = (float)total_time_usage_start*0.000001;
4325 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4326 video_time_usage,vout_time_usage,audio_time_usage,
4327 total_time_usage-tot,total_time_usage);
4328 if(total_time_usage>0.0)
4329 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4330 100.0*video_time_usage/total_time_usage,
4331 100.0*vout_time_usage/total_time_usage,
4332 100.0*audio_time_usage/total_time_usage,
4333 100.0*(total_time_usage-tot)/total_time_usage,
4334 100.0);
4335 if(total_frame_cnt && frame_dropping)
4336 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4337 total_frame_cnt-drop_frame_cnt,
4338 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4339 drop_frame_cnt,
4340 100*drop_frame_cnt/total_frame_cnt,
4341 total_frame_cnt,
4342 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4345 // time to uninit all, except global stuff:
4346 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4348 if(mpctx->set_of_sub_size > 0) {
4349 current_module="sub_free";
4350 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4351 sub_free(mpctx->set_of_subtitles[i]);
4352 #ifdef CONFIG_ASS
4353 if(mpctx->set_of_ass_tracks[i])
4354 ass_free_track( mpctx->set_of_ass_tracks[i] );
4355 #endif
4357 mpctx->set_of_sub_size = 0;
4359 vo_sub_last = vo_sub=NULL;
4360 subdata=NULL;
4361 #ifdef CONFIG_ASS
4362 ass_track = NULL;
4363 if(ass_library)
4364 ass_clear_fonts(ass_library);
4365 #endif
4367 if (!mpctx->stop_play) // In case some goto jumped here...
4368 mpctx->stop_play = PT_NEXT_ENTRY;
4370 int playtree_direction = 1;
4372 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4373 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4374 play_tree_iter_free(mpctx->playtree_iter);
4375 mpctx->playtree_iter = NULL;
4377 mpctx->play_tree_step = 1;
4378 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4379 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4380 if(mpctx->playtree_iter) {
4381 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4382 play_tree_iter_free(mpctx->playtree_iter);
4383 mpctx->playtree_iter = NULL;
4386 } else if (mpctx->stop_play == PT_STOP) {
4387 play_tree_iter_free(mpctx->playtree_iter);
4388 mpctx->playtree_iter = NULL;
4389 } else { // NEXT PREV SRC
4390 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4393 while(mpctx->playtree_iter != NULL) {
4394 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4395 if(mpctx->filename == NULL) {
4396 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4397 play_tree_iter_free(mpctx->playtree_iter);
4398 mpctx->playtree_iter = NULL;
4400 } else
4401 break;
4404 if(mpctx->playtree_iter != NULL || player_idle_mode){
4405 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4406 mpctx->stop_play = 0;
4407 goto play_next_file;
4411 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4413 return 1;
4415 #endif /* DISABLE_MAIN */